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" |
| 45 | #include "i915_reset.h" |
| 46 | #include "i915_trace.h" |
| 47 | #include "i915_vgpu.h" |
| 48 | |
| 49 | #include "intel_drv.h" |
| 50 | #include "intel_frontbuffer.h" |
| 51 | #include "intel_mocs.h" |
| 52 | #include "intel_workarounds.h" |
| 53 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 54 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 55 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 56 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 57 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 58 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 59 | return false; |
| 60 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 61 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 62 | return true; |
| 63 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 64 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 67 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 68 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 69 | struct drm_mm_node *node, u32 size) |
| 70 | { |
| 71 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 72 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 73 | size, 0, I915_COLOR_UNEVICTABLE, |
| 74 | 0, ggtt->mappable_end, |
| 75 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void |
| 79 | remove_mappable_node(struct drm_mm_node *node) |
| 80 | { |
| 81 | drm_mm_remove_node(node); |
| 82 | } |
| 83 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 84 | /* some bookkeeping */ |
| 85 | 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] | 86 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 87 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 88 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 89 | dev_priv->mm.object_count++; |
| 90 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 91 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | 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] | 95 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 96 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 97 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 98 | dev_priv->mm.object_count--; |
| 99 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 100 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 103 | static u32 __i915_gem_park(struct drm_i915_private *i915) |
| 104 | { |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 105 | intel_wakeref_t wakeref; |
| 106 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 107 | GEM_TRACE("\n"); |
| 108 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 109 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 110 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 111 | GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 112 | |
| 113 | if (!i915->gt.awake) |
| 114 | return I915_EPOCH_INVALID; |
| 115 | |
| 116 | GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); |
| 117 | |
| 118 | /* |
| 119 | * Be paranoid and flush a concurrent interrupt to make sure |
| 120 | * we don't reactivate any irq tasklets after parking. |
| 121 | * |
| 122 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 123 | * there may still be an in-flight interrupt even though the CSB |
| 124 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 125 | * is completed before we continue, but it doesn't prevent the HW from |
| 126 | * raising a spurious interrupt later. To complete the shield we should |
| 127 | * coordinate disabling the CS irq with flushing the interrupts. |
| 128 | */ |
| 129 | synchronize_irq(i915->drm.irq); |
| 130 | |
| 131 | intel_engines_park(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 132 | i915_timelines_park(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 133 | |
| 134 | i915_pmu_gt_parked(i915); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 135 | i915_vma_parked(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 136 | |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 137 | wakeref = fetch_and_zero(&i915->gt.awake); |
| 138 | GEM_BUG_ON(!wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 139 | |
| 140 | if (INTEL_GEN(i915) >= 6) |
| 141 | gen6_rps_idle(i915); |
| 142 | |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 143 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 144 | |
| 145 | return i915->gt.epoch; |
| 146 | } |
| 147 | |
| 148 | void i915_gem_park(struct drm_i915_private *i915) |
| 149 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 150 | GEM_TRACE("\n"); |
| 151 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 152 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 153 | GEM_BUG_ON(i915->gt.active_requests); |
| 154 | |
| 155 | if (!i915->gt.awake) |
| 156 | return; |
| 157 | |
| 158 | /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ |
| 159 | mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); |
| 160 | } |
| 161 | |
| 162 | void i915_gem_unpark(struct drm_i915_private *i915) |
| 163 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 164 | GEM_TRACE("\n"); |
| 165 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 166 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 167 | GEM_BUG_ON(!i915->gt.active_requests); |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 168 | assert_rpm_wakelock_held(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 169 | |
| 170 | if (i915->gt.awake) |
| 171 | return; |
| 172 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 173 | /* |
| 174 | * It seems that the DMC likes to transition between the DC states a lot |
| 175 | * when there are no connected displays (no active power domains) during |
| 176 | * command submission. |
| 177 | * |
| 178 | * This activity has negative impact on the performance of the chip with |
| 179 | * huge latencies observed in the interrupt handler and elsewhere. |
| 180 | * |
| 181 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 182 | * GT activity, preventing any DC state transitions. |
| 183 | */ |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 184 | i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 185 | GEM_BUG_ON(!i915->gt.awake); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 186 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 187 | if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ |
| 188 | i915->gt.epoch = 1; |
| 189 | |
| 190 | intel_enable_gt_powersave(i915); |
| 191 | i915_update_gfx_val(i915); |
| 192 | if (INTEL_GEN(i915) >= 6) |
| 193 | gen6_rps_busy(i915); |
| 194 | i915_pmu_gt_unparked(i915); |
| 195 | |
| 196 | intel_engines_unpark(i915); |
| 197 | |
| 198 | i915_queue_hangcheck(i915); |
| 199 | |
| 200 | queue_delayed_work(i915->wq, |
| 201 | &i915->gt.retire_work, |
| 202 | round_jiffies_up_relative(HZ)); |
| 203 | } |
| 204 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 205 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 206 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 207 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 208 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 209 | struct i915_ggtt *ggtt = &to_i915(dev)->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 210 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 211 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 212 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 213 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 214 | mutex_lock(&ggtt->vm.mutex); |
| 215 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 216 | pinned = ggtt->vm.reserved; |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 217 | list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 218 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 219 | pinned += vma->node.size; |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 220 | |
| 221 | mutex_unlock(&ggtt->vm.mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 222 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 223 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 224 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 225 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 229 | 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] | 230 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 231 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 232 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 233 | struct sg_table *st; |
| 234 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 235 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 236 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 237 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 238 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 239 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 240 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 241 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 242 | /* Always aligning to the object size, allows a single allocation |
| 243 | * to handle all possible callers, and given typical object sizes, |
| 244 | * the alignment of the buddy allocation will naturally match. |
| 245 | */ |
| 246 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 247 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 248 | roundup_pow_of_two(obj->base.size)); |
| 249 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 250 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 251 | |
| 252 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 253 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 254 | struct page *page; |
| 255 | char *src; |
| 256 | |
| 257 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 258 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 259 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 260 | goto err_phys; |
| 261 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 262 | |
| 263 | src = kmap_atomic(page); |
| 264 | memcpy(vaddr, src, PAGE_SIZE); |
| 265 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 266 | kunmap_atomic(src); |
| 267 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 268 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 269 | vaddr += PAGE_SIZE; |
| 270 | } |
| 271 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 272 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 273 | |
| 274 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 275 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 276 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 277 | goto err_phys; |
| 278 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 279 | |
| 280 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 281 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 282 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 283 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | sg = st->sgl; |
| 287 | sg->offset = 0; |
| 288 | sg->length = obj->base.size; |
| 289 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 290 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 291 | sg_dma_len(sg) = obj->base.size; |
| 292 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 293 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 294 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 295 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 296 | |
| 297 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 298 | |
| 299 | err_phys: |
| 300 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 301 | |
| 302 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 305 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 306 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 307 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 308 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 309 | if (cpu_write_needs_clflush(obj)) |
| 310 | obj->cache_dirty = true; |
| 311 | } |
| 312 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 313 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 314 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 315 | struct sg_table *pages, |
| 316 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 317 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 318 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 319 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 320 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 321 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 322 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 323 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 324 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 325 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 326 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 327 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 328 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static void |
| 332 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 333 | struct sg_table *pages) |
| 334 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 335 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 336 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 337 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 338 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 339 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 340 | int i; |
| 341 | |
| 342 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 343 | struct page *page; |
| 344 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 345 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 346 | page = shmem_read_mapping_page(mapping, i); |
| 347 | if (IS_ERR(page)) |
| 348 | continue; |
| 349 | |
| 350 | dst = kmap_atomic(page); |
| 351 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 352 | memcpy(dst, vaddr, PAGE_SIZE); |
| 353 | kunmap_atomic(dst); |
| 354 | |
| 355 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 356 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 357 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 358 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 359 | vaddr += PAGE_SIZE; |
| 360 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 361 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 364 | sg_free_table(pages); |
| 365 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 366 | |
| 367 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void |
| 371 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 372 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 373 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 377 | .get_pages = i915_gem_object_get_pages_phys, |
| 378 | .put_pages = i915_gem_object_put_pages_phys, |
| 379 | .release = i915_gem_object_release_phys, |
| 380 | }; |
| 381 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 382 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 383 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 384 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 385 | { |
| 386 | struct i915_vma *vma; |
| 387 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 388 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 389 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 390 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 391 | |
| 392 | /* Closed vma are removed from the obj->vma_list - but they may |
| 393 | * still have an active binding on the object. To remove those we |
| 394 | * must wait for all rendering to complete to the object (as unbinding |
| 395 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 396 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 397 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 398 | if (ret) |
| 399 | return ret; |
| 400 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 401 | spin_lock(&obj->vma.lock); |
| 402 | while (!ret && (vma = list_first_entry_or_null(&obj->vma.list, |
| 403 | struct i915_vma, |
| 404 | obj_link))) { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 405 | list_move_tail(&vma->obj_link, &still_in_list); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 406 | spin_unlock(&obj->vma.lock); |
| 407 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 408 | ret = i915_vma_unbind(vma); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 409 | |
| 410 | spin_lock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 411 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 412 | list_splice(&still_in_list, &obj->vma.list); |
| 413 | spin_unlock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 418 | static long |
| 419 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 420 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 421 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 422 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 423 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 424 | |
| 425 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 426 | |
| 427 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 428 | return timeout; |
| 429 | |
| 430 | if (!dma_fence_is_i915(fence)) |
| 431 | return dma_fence_wait_timeout(fence, |
| 432 | flags & I915_WAIT_INTERRUPTIBLE, |
| 433 | timeout); |
| 434 | |
| 435 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 436 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 437 | goto out; |
| 438 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 439 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 440 | |
| 441 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 442 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 443 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 444 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 445 | return timeout; |
| 446 | } |
| 447 | |
| 448 | static long |
| 449 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 450 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 451 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 452 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 453 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 454 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 455 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 456 | |
| 457 | if (flags & I915_WAIT_ALL) { |
| 458 | struct dma_fence **shared; |
| 459 | unsigned int count, i; |
| 460 | int ret; |
| 461 | |
| 462 | ret = reservation_object_get_fences_rcu(resv, |
| 463 | &excl, &count, &shared); |
| 464 | if (ret) |
| 465 | return ret; |
| 466 | |
| 467 | for (i = 0; i < count; i++) { |
| 468 | timeout = i915_gem_object_wait_fence(shared[i], |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 469 | flags, timeout); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 470 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 471 | break; |
| 472 | |
| 473 | dma_fence_put(shared[i]); |
| 474 | } |
| 475 | |
| 476 | for (; i < count; i++) |
| 477 | dma_fence_put(shared[i]); |
| 478 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 479 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 480 | /* |
| 481 | * If both shared fences and an exclusive fence exist, |
| 482 | * then by construction the shared fences must be later |
| 483 | * than the exclusive fence. If we successfully wait for |
| 484 | * all the shared fences, we know that the exclusive fence |
| 485 | * must all be signaled. If all the shared fences are |
| 486 | * signaled, we can prune the array and recover the |
| 487 | * floating references on the fences/requests. |
| 488 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 489 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 490 | } else { |
| 491 | excl = reservation_object_get_excl_rcu(resv); |
| 492 | } |
| 493 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 494 | if (excl && timeout >= 0) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 495 | timeout = i915_gem_object_wait_fence(excl, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 496 | |
| 497 | dma_fence_put(excl); |
| 498 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 499 | /* |
| 500 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 501 | * signaled and that the reservation object has not been changed (i.e. |
| 502 | * no new fences have been added). |
| 503 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 504 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 505 | if (reservation_object_trylock(resv)) { |
| 506 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 507 | reservation_object_add_excl_fence(resv, NULL); |
| 508 | reservation_object_unlock(resv); |
| 509 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 512 | return timeout; |
| 513 | } |
| 514 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 515 | static void __fence_set_priority(struct dma_fence *fence, |
| 516 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 517 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 518 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 519 | struct intel_engine_cs *engine; |
| 520 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 521 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 522 | return; |
| 523 | |
| 524 | rq = to_request(fence); |
| 525 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 526 | |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 527 | local_bh_disable(); |
| 528 | rcu_read_lock(); /* RCU serialisation for set-wedged protection */ |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 529 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 530 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 531 | rcu_read_unlock(); |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 532 | local_bh_enable(); /* kick the tasklets if queues were reprioritised */ |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 535 | static void fence_set_priority(struct dma_fence *fence, |
| 536 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 537 | { |
| 538 | /* Recurse once into a fence-array */ |
| 539 | if (dma_fence_is_array(fence)) { |
| 540 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 541 | int i; |
| 542 | |
| 543 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 544 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 545 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 546 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
| 550 | int |
| 551 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 552 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 553 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 554 | { |
| 555 | struct dma_fence *excl; |
| 556 | |
| 557 | if (flags & I915_WAIT_ALL) { |
| 558 | struct dma_fence **shared; |
| 559 | unsigned int count, i; |
| 560 | int ret; |
| 561 | |
| 562 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 563 | &excl, &count, &shared); |
| 564 | if (ret) |
| 565 | return ret; |
| 566 | |
| 567 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 568 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 569 | dma_fence_put(shared[i]); |
| 570 | } |
| 571 | |
| 572 | kfree(shared); |
| 573 | } else { |
| 574 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 575 | } |
| 576 | |
| 577 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 578 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 579 | dma_fence_put(excl); |
| 580 | } |
| 581 | return 0; |
| 582 | } |
| 583 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 584 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 585 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 586 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 587 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 588 | * @timeout: how long to wait |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 589 | */ |
| 590 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 591 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 592 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 593 | long timeout) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 594 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 595 | might_sleep(); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 596 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 597 | |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 598 | timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 599 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 602 | static int |
| 603 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 604 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 605 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 606 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 607 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 608 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 609 | |
| 610 | /* We manually control the domain here and pretend that it |
| 611 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 612 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 613 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 614 | if (copy_from_user(vaddr, user_data, args->size)) |
| 615 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 616 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 617 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 618 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 619 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 620 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 621 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 624 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 625 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 626 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 630 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 631 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 632 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 635 | static int |
| 636 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 637 | struct drm_i915_private *dev_priv, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 638 | u64 size, |
| 639 | u32 *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 640 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 641 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 642 | int ret; |
| 643 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 644 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 645 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 646 | if (size == 0) |
| 647 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 648 | |
| 649 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 650 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 651 | if (IS_ERR(obj)) |
| 652 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 653 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 654 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 655 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 656 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 657 | if (ret) |
| 658 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 659 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 660 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 664 | int |
| 665 | i915_gem_dumb_create(struct drm_file *file, |
| 666 | struct drm_device *dev, |
| 667 | struct drm_mode_create_dumb *args) |
| 668 | { |
| 669 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 670 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 671 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 672 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 673 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 674 | } |
| 675 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 676 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 677 | { |
| 678 | return !(obj->cache_level == I915_CACHE_NONE || |
| 679 | obj->cache_level == I915_CACHE_WT); |
| 680 | } |
| 681 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 682 | /** |
| 683 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 684 | * @dev: drm device pointer |
| 685 | * @data: ioctl data blob |
| 686 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 687 | */ |
| 688 | int |
| 689 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 690 | struct drm_file *file) |
| 691 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 692 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 693 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 694 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 695 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 696 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 697 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 698 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 699 | } |
| 700 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 701 | static inline enum fb_op_origin |
| 702 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 703 | { |
| 704 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 705 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 706 | } |
| 707 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 708 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 709 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 710 | intel_wakeref_t wakeref; |
| 711 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 712 | /* |
| 713 | * No actual flushing is required for the GTT write domain for reads |
| 714 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 715 | * as far as we know, so there's no chipset flush. It also doesn't |
| 716 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 717 | * |
| 718 | * However, we do have to enforce the order so that all writes through |
| 719 | * the GTT land before any writes to the device, such as updates to |
| 720 | * the GATT itself. |
| 721 | * |
| 722 | * We also have to wait a bit for the writes to land from the GTT. |
| 723 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 724 | * timing. This issue has only been observed when switching quickly |
| 725 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 726 | * 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] | 727 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 728 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 729 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 730 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 731 | wmb(); |
| 732 | |
| 733 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 734 | return; |
| 735 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 736 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 737 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 738 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 739 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 740 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 741 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 742 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 743 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 744 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static void |
| 748 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 749 | { |
| 750 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 751 | struct i915_vma *vma; |
| 752 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 753 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 754 | return; |
| 755 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 756 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 757 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 758 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 759 | |
| 760 | intel_fb_obj_flush(obj, |
| 761 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 762 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 763 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 764 | if (vma->iomap) |
| 765 | continue; |
| 766 | |
| 767 | i915_vma_unset_ggtt_write(vma); |
| 768 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 769 | break; |
| 770 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 771 | case I915_GEM_DOMAIN_WC: |
| 772 | wmb(); |
| 773 | break; |
| 774 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 775 | case I915_GEM_DOMAIN_CPU: |
| 776 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 777 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 778 | |
| 779 | case I915_GEM_DOMAIN_RENDER: |
| 780 | if (gpu_write_needs_clflush(obj)) |
| 781 | obj->cache_dirty = true; |
| 782 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 785 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 788 | /* |
| 789 | * Pins the specified object's pages and synchronizes the object with |
| 790 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 791 | * flush the object from the CPU cache. |
| 792 | */ |
| 793 | 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] | 794 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 795 | { |
| 796 | int ret; |
| 797 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 798 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 799 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 800 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 801 | if (!i915_gem_object_has_struct_page(obj)) |
| 802 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 803 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 804 | ret = i915_gem_object_wait(obj, |
| 805 | I915_WAIT_INTERRUPTIBLE | |
| 806 | I915_WAIT_LOCKED, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 807 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 808 | if (ret) |
| 809 | return ret; |
| 810 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 811 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 812 | if (ret) |
| 813 | return ret; |
| 814 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 815 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 816 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 817 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 818 | if (ret) |
| 819 | goto err_unpin; |
| 820 | else |
| 821 | goto out; |
| 822 | } |
| 823 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 824 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 825 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 826 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 827 | * read domain and manually flush cachelines (if required). This |
| 828 | * optimizes for the case when the gpu will dirty the data |
| 829 | * anyway again before the next pread happens. |
| 830 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 831 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 832 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 833 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 834 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 835 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 836 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 837 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 838 | |
| 839 | err_unpin: |
| 840 | i915_gem_object_unpin_pages(obj); |
| 841 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 845 | unsigned int *needs_clflush) |
| 846 | { |
| 847 | int ret; |
| 848 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 849 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 850 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 851 | *needs_clflush = 0; |
| 852 | if (!i915_gem_object_has_struct_page(obj)) |
| 853 | return -ENODEV; |
| 854 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 855 | ret = i915_gem_object_wait(obj, |
| 856 | I915_WAIT_INTERRUPTIBLE | |
| 857 | I915_WAIT_LOCKED | |
| 858 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 859 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 860 | if (ret) |
| 861 | return ret; |
| 862 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 863 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 864 | if (ret) |
| 865 | return ret; |
| 866 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 867 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 868 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 869 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 870 | if (ret) |
| 871 | goto err_unpin; |
| 872 | else |
| 873 | goto out; |
| 874 | } |
| 875 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 876 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 877 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 878 | /* If we're not in the cpu write domain, set ourself into the |
| 879 | * gtt write domain and manually flush cachelines (as required). |
| 880 | * This optimizes for the case when the gpu will use the data |
| 881 | * right away and we therefore have to clflush anyway. |
| 882 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 883 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 884 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 885 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 886 | /* |
| 887 | * Same trick applies to invalidate partially written |
| 888 | * cachelines read before writing. |
| 889 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 890 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 891 | *needs_clflush |= CLFLUSH_BEFORE; |
| 892 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 893 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 894 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 895 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 896 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 897 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 898 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 899 | |
| 900 | err_unpin: |
| 901 | i915_gem_object_unpin_pages(obj); |
| 902 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 905 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 906 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 907 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 908 | { |
| 909 | char *vaddr; |
| 910 | int ret; |
| 911 | |
| 912 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 913 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 914 | if (needs_clflush) |
| 915 | drm_clflush_virt_range(vaddr + offset, len); |
| 916 | |
| 917 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 918 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 919 | kunmap(page); |
| 920 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 921 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static int |
| 925 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 926 | struct drm_i915_gem_pread *args) |
| 927 | { |
| 928 | char __user *user_data; |
| 929 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 930 | unsigned int needs_clflush; |
| 931 | unsigned int idx, offset; |
| 932 | int ret; |
| 933 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 934 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 935 | if (ret) |
| 936 | return ret; |
| 937 | |
| 938 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 939 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 940 | if (ret) |
| 941 | return ret; |
| 942 | |
| 943 | remain = args->size; |
| 944 | user_data = u64_to_user_ptr(args->data_ptr); |
| 945 | offset = offset_in_page(args->offset); |
| 946 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 947 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 948 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 949 | |
| 950 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 951 | needs_clflush); |
| 952 | if (ret) |
| 953 | break; |
| 954 | |
| 955 | remain -= length; |
| 956 | user_data += length; |
| 957 | offset = 0; |
| 958 | } |
| 959 | |
| 960 | i915_gem_obj_finish_shmem_access(obj); |
| 961 | return ret; |
| 962 | } |
| 963 | |
| 964 | static inline bool |
| 965 | gtt_user_read(struct io_mapping *mapping, |
| 966 | loff_t base, int offset, |
| 967 | char __user *user_data, int length) |
| 968 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 969 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 970 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 971 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 972 | /* 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] | 973 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 974 | unwritten = __copy_to_user_inatomic(user_data, |
| 975 | (void __force *)vaddr + offset, |
| 976 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 977 | io_mapping_unmap_atomic(vaddr); |
| 978 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 979 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 980 | unwritten = copy_to_user(user_data, |
| 981 | (void __force *)vaddr + offset, |
| 982 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 983 | io_mapping_unmap(vaddr); |
| 984 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 985 | return unwritten; |
| 986 | } |
| 987 | |
| 988 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 989 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 990 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 991 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 992 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 993 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 994 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 995 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 996 | struct i915_vma *vma; |
| 997 | void __user *user_data; |
| 998 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 999 | int ret; |
| 1000 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1001 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1002 | if (ret) |
| 1003 | return ret; |
| 1004 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1005 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1006 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1007 | PIN_MAPPABLE | |
| 1008 | PIN_NONFAULT | |
| 1009 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1010 | if (!IS_ERR(vma)) { |
| 1011 | node.start = i915_ggtt_offset(vma); |
| 1012 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1013 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1014 | if (ret) { |
| 1015 | i915_vma_unpin(vma); |
| 1016 | vma = ERR_PTR(ret); |
| 1017 | } |
| 1018 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1019 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1020 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1021 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1022 | goto out_unlock; |
| 1023 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1027 | if (ret) |
| 1028 | goto out_unpin; |
| 1029 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1030 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1031 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1032 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1033 | remain = args->size; |
| 1034 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1035 | |
| 1036 | while (remain > 0) { |
| 1037 | /* Operation in this page |
| 1038 | * |
| 1039 | * page_base = page offset within aperture |
| 1040 | * page_offset = offset within page |
| 1041 | * page_length = bytes to copy for this page |
| 1042 | */ |
| 1043 | u32 page_base = node.start; |
| 1044 | unsigned page_offset = offset_in_page(offset); |
| 1045 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1046 | page_length = remain < page_length ? remain : page_length; |
| 1047 | if (node.allocated) { |
| 1048 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1049 | ggtt->vm.insert_page(&ggtt->vm, |
| 1050 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1051 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1052 | wmb(); |
| 1053 | } else { |
| 1054 | page_base += offset & PAGE_MASK; |
| 1055 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1056 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1057 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1058 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1059 | ret = -EFAULT; |
| 1060 | break; |
| 1061 | } |
| 1062 | |
| 1063 | remain -= page_length; |
| 1064 | user_data += page_length; |
| 1065 | offset += page_length; |
| 1066 | } |
| 1067 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1068 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1069 | out_unpin: |
| 1070 | if (node.allocated) { |
| 1071 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1072 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1073 | remove_mappable_node(&node); |
| 1074 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1075 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1076 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1077 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1078 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1079 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1080 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1081 | return ret; |
| 1082 | } |
| 1083 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1084 | /** |
| 1085 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1086 | * @dev: drm device pointer |
| 1087 | * @data: ioctl data blob |
| 1088 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1089 | * |
| 1090 | * On error, the contents of *data are undefined. |
| 1091 | */ |
| 1092 | int |
| 1093 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1094 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1095 | { |
| 1096 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1097 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1098 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1099 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1100 | if (args->size == 0) |
| 1101 | return 0; |
| 1102 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1103 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1104 | args->size)) |
| 1105 | return -EFAULT; |
| 1106 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1107 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1108 | if (!obj) |
| 1109 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1110 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1111 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1112 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1113 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1114 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1117 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1118 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1119 | ret = i915_gem_object_wait(obj, |
| 1120 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1121 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1122 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1123 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1124 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1125 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1126 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1127 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1128 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1129 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1130 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1131 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1132 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1133 | i915_gem_object_unpin_pages(obj); |
| 1134 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1135 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1136 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1139 | /* This is the fast write path which cannot handle |
| 1140 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1141 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1142 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1143 | static inline bool |
| 1144 | ggtt_write(struct io_mapping *mapping, |
| 1145 | loff_t base, int offset, |
| 1146 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1147 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1148 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1149 | unsigned long unwritten; |
| 1150 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1151 | /* 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] | 1152 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1153 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1154 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1155 | io_mapping_unmap_atomic(vaddr); |
| 1156 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1157 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1158 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1159 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1160 | io_mapping_unmap(vaddr); |
| 1161 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1162 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1163 | return unwritten; |
| 1164 | } |
| 1165 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1166 | /** |
| 1167 | * This is the fast pwrite path, where we copy the data directly from the |
| 1168 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1169 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1170 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1171 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1172 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1173 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1174 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1175 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1176 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1177 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1178 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1179 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1180 | struct i915_vma *vma; |
| 1181 | u64 remain, offset; |
| 1182 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1183 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1184 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1185 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1186 | if (ret) |
| 1187 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1188 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1189 | if (i915_gem_object_has_struct_page(obj)) { |
| 1190 | /* |
| 1191 | * Avoid waking the device up if we can fallback, as |
| 1192 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1193 | * depending on PCI sleeps and our own resume time). |
| 1194 | * This easily dwarfs any performance advantage from |
| 1195 | * using the cache bypass of indirect GGTT access. |
| 1196 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1197 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1198 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1199 | ret = -EFAULT; |
| 1200 | goto out_unlock; |
| 1201 | } |
| 1202 | } else { |
| 1203 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1204 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1205 | } |
| 1206 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1207 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1208 | PIN_MAPPABLE | |
| 1209 | PIN_NONFAULT | |
| 1210 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1211 | if (!IS_ERR(vma)) { |
| 1212 | node.start = i915_ggtt_offset(vma); |
| 1213 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1214 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1215 | if (ret) { |
| 1216 | i915_vma_unpin(vma); |
| 1217 | vma = ERR_PTR(ret); |
| 1218 | } |
| 1219 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1220 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1221 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1222 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1223 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1224 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1225 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1226 | |
| 1227 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1228 | if (ret) |
| 1229 | goto out_unpin; |
| 1230 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1231 | mutex_unlock(&i915->drm.struct_mutex); |
| 1232 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1233 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1234 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1235 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1236 | offset = args->offset; |
| 1237 | remain = args->size; |
| 1238 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1239 | /* Operation in this page |
| 1240 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1241 | * page_base = page offset within aperture |
| 1242 | * page_offset = offset within page |
| 1243 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1244 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1245 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1246 | unsigned int page_offset = offset_in_page(offset); |
| 1247 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1248 | page_length = remain < page_length ? remain : page_length; |
| 1249 | if (node.allocated) { |
| 1250 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1251 | ggtt->vm.insert_page(&ggtt->vm, |
| 1252 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1253 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1254 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1255 | } else { |
| 1256 | page_base += offset & PAGE_MASK; |
| 1257 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1258 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1259 | * source page isn't available. Return the error and we'll |
| 1260 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1261 | * If the object is non-shmem backed, we retry again with the |
| 1262 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1263 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1264 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1265 | user_data, page_length)) { |
| 1266 | ret = -EFAULT; |
| 1267 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1268 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1269 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1270 | remain -= page_length; |
| 1271 | user_data += page_length; |
| 1272 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1273 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1274 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1275 | |
| 1276 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1277 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1278 | if (node.allocated) { |
| 1279 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1280 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1281 | remove_mappable_node(&node); |
| 1282 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1283 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1284 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1285 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1286 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1287 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1288 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1289 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1292 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1293 | * Flushes invalid cachelines before writing to the target if |
| 1294 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1295 | * writing if needs_clflush is set. |
| 1296 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1297 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1298 | 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] | 1299 | bool needs_clflush_before, |
| 1300 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1301 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1302 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1303 | int ret; |
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 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1306 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1307 | if (needs_clflush_before) |
| 1308 | drm_clflush_virt_range(vaddr + offset, len); |
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 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1311 | if (!ret && needs_clflush_after) |
| 1312 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1313 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1314 | kunmap(page); |
| 1315 | |
| 1316 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | static int |
| 1320 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1321 | const struct drm_i915_gem_pwrite *args) |
| 1322 | { |
| 1323 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1324 | void __user *user_data; |
| 1325 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1326 | unsigned int partial_cacheline_write; |
| 1327 | unsigned int needs_clflush; |
| 1328 | unsigned int offset, idx; |
| 1329 | int ret; |
| 1330 | |
| 1331 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1332 | if (ret) |
| 1333 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1334 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1335 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1336 | mutex_unlock(&i915->drm.struct_mutex); |
| 1337 | if (ret) |
| 1338 | return ret; |
| 1339 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1340 | /* If we don't overwrite a cacheline completely we need to be |
| 1341 | * careful to have up-to-date data by first clflushing. Don't |
| 1342 | * overcomplicate things and flush the entire patch. |
| 1343 | */ |
| 1344 | partial_cacheline_write = 0; |
| 1345 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1346 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1347 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1348 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1349 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1350 | offset = offset_in_page(args->offset); |
| 1351 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1352 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1353 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1354 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1355 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1356 | (offset | length) & partial_cacheline_write, |
| 1357 | needs_clflush & CLFLUSH_AFTER); |
| 1358 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1359 | break; |
| 1360 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1361 | remain -= length; |
| 1362 | user_data += length; |
| 1363 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1366 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1367 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1368 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /** |
| 1372 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1373 | * @dev: drm device |
| 1374 | * @data: ioctl data blob |
| 1375 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1376 | * |
| 1377 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1378 | */ |
| 1379 | int |
| 1380 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1381 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1382 | { |
| 1383 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1384 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1385 | int ret; |
| 1386 | |
| 1387 | if (args->size == 0) |
| 1388 | return 0; |
| 1389 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1390 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1391 | return -EFAULT; |
| 1392 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1393 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1394 | if (!obj) |
| 1395 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1396 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1397 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1398 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1399 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1400 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1401 | } |
| 1402 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1403 | /* Writes not allowed into this read-only object */ |
| 1404 | if (i915_gem_object_is_readonly(obj)) { |
| 1405 | ret = -EINVAL; |
| 1406 | goto err; |
| 1407 | } |
| 1408 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1409 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1410 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1411 | ret = -ENODEV; |
| 1412 | if (obj->ops->pwrite) |
| 1413 | ret = obj->ops->pwrite(obj, args); |
| 1414 | if (ret != -ENODEV) |
| 1415 | goto err; |
| 1416 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1417 | ret = i915_gem_object_wait(obj, |
| 1418 | I915_WAIT_INTERRUPTIBLE | |
| 1419 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1420 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1421 | if (ret) |
| 1422 | goto err; |
| 1423 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1424 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1425 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1426 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1427 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1428 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1429 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1430 | * it would end up going through the fenced access, and we'll get |
| 1431 | * different detiling behavior between reading and writing. |
| 1432 | * pread/pwrite currently are reading and writing from the CPU |
| 1433 | * perspective, requiring manual detiling by the client. |
| 1434 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1435 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1436 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1437 | /* Note that the gtt paths might fail with non-page-backed user |
| 1438 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1439 | * textures). Fallback to the shmem path in that case. |
| 1440 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1441 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1442 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1443 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1444 | if (obj->phys_handle) |
| 1445 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1446 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1447 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1448 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1449 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1450 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1451 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1452 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1453 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1456 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1457 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1458 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1459 | struct list_head *list; |
| 1460 | struct i915_vma *vma; |
| 1461 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1462 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1463 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1464 | mutex_lock(&i915->ggtt.vm.mutex); |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1465 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1466 | if (!drm_mm_node_allocated(&vma->node)) |
| 1467 | continue; |
| 1468 | |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 1469 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1470 | } |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1471 | mutex_unlock(&i915->ggtt.vm.mutex); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1472 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1473 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1474 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1475 | list_move_tail(&obj->mm.link, list); |
| 1476 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1477 | } |
| 1478 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1479 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1480 | * Called when user space prepares to use an object with the CPU, either |
| 1481 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1482 | * @dev: drm device |
| 1483 | * @data: ioctl data blob |
| 1484 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1485 | */ |
| 1486 | int |
| 1487 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1488 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1489 | { |
| 1490 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1491 | struct drm_i915_gem_object *obj; |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1492 | u32 read_domains = args->read_domains; |
| 1493 | u32 write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1494 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1495 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1496 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1497 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1498 | return -EINVAL; |
| 1499 | |
| 1500 | /* Having something in the write domain implies it's in the read |
| 1501 | * domain, and only that read domain. Enforce that in the request. |
| 1502 | */ |
| 1503 | if (write_domain != 0 && read_domains != write_domain) |
| 1504 | return -EINVAL; |
| 1505 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1506 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1507 | if (!obj) |
| 1508 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1509 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1510 | /* Try to flush the object off the GPU without holding the lock. |
| 1511 | * We will repeat the flush holding the lock in the normal manner |
| 1512 | * to catch cases where we are gazumped. |
| 1513 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1514 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1515 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1516 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1517 | (write_domain ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1518 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1519 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1520 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1521 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1522 | /* |
| 1523 | * Proxy objects do not control access to the backing storage, ergo |
| 1524 | * they cannot be used as a means to manipulate the cache domain |
| 1525 | * tracking for that backing storage. The proxy object is always |
| 1526 | * considered to be outside of any cache domain. |
| 1527 | */ |
| 1528 | if (i915_gem_object_is_proxy(obj)) { |
| 1529 | err = -ENXIO; |
| 1530 | goto out; |
| 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1535 | * direct access in memory with previous cached writes through |
| 1536 | * shmemfs and that our cache domain tracking remains valid. |
| 1537 | * For example, if the obj->filp was moved to swap without us |
| 1538 | * being notified and releasing the pages, we would mistakenly |
| 1539 | * continue to assume that the obj remained out of the CPU cached |
| 1540 | * domain. |
| 1541 | */ |
| 1542 | err = i915_gem_object_pin_pages(obj); |
| 1543 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1544 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1545 | |
| 1546 | err = i915_mutex_lock_interruptible(dev); |
| 1547 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1548 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1549 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1550 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1551 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1552 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1553 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1554 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1555 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1556 | |
| 1557 | /* And bump the LRU for this access */ |
| 1558 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1559 | |
| 1560 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1561 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1562 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1563 | intel_fb_obj_invalidate(obj, |
| 1564 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1565 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1566 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1567 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1568 | out: |
| 1569 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1570 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /** |
| 1574 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1575 | * @dev: drm device |
| 1576 | * @data: ioctl data blob |
| 1577 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1578 | */ |
| 1579 | int |
| 1580 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1581 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1582 | { |
| 1583 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1584 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1585 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1586 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1587 | if (!obj) |
| 1588 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1589 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1590 | /* |
| 1591 | * Proxy objects are barred from CPU access, so there is no |
| 1592 | * need to ban sw_finish as it is a nop. |
| 1593 | */ |
| 1594 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1595 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1596 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1597 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1598 | |
| 1599 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1602 | static inline bool |
| 1603 | __vma_matches(struct vm_area_struct *vma, struct file *filp, |
| 1604 | unsigned long addr, unsigned long size) |
| 1605 | { |
| 1606 | if (vma->vm_file != filp) |
| 1607 | return false; |
| 1608 | |
| 1609 | return vma->vm_start == addr && (vma->vm_end - vma->vm_start) == size; |
| 1610 | } |
| 1611 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1612 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1613 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1614 | * it is mapped to. |
| 1615 | * @dev: drm device |
| 1616 | * @data: ioctl data blob |
| 1617 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1618 | * |
| 1619 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1620 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1621 | * |
| 1622 | * IMPORTANT: |
| 1623 | * |
| 1624 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1625 | * mmap support, please don't implement mmap support like here. The modern way |
| 1626 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1627 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1628 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1629 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1630 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1631 | */ |
| 1632 | int |
| 1633 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1634 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1635 | { |
| 1636 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1637 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1638 | unsigned long addr; |
| 1639 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1640 | if (args->flags & ~(I915_MMAP_WC)) |
| 1641 | return -EINVAL; |
| 1642 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1643 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1644 | return -ENODEV; |
| 1645 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1646 | obj = i915_gem_object_lookup(file, args->handle); |
| 1647 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1648 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1649 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1650 | /* prime objects have no backing filp to GEM mmap |
| 1651 | * pages from. |
| 1652 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1653 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1654 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1655 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1656 | } |
| 1657 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1658 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1659 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1660 | args->offset); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1661 | if (IS_ERR_VALUE(addr)) |
| 1662 | goto err; |
| 1663 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1664 | if (args->flags & I915_MMAP_WC) { |
| 1665 | struct mm_struct *mm = current->mm; |
| 1666 | struct vm_area_struct *vma; |
| 1667 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1668 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1669 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1670 | return -EINTR; |
| 1671 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1672 | vma = find_vma(mm, addr); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1673 | if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1674 | vma->vm_page_prot = |
| 1675 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1676 | else |
| 1677 | addr = -ENOMEM; |
| 1678 | up_write(&mm->mmap_sem); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1679 | if (IS_ERR_VALUE(addr)) |
| 1680 | goto err; |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1681 | |
| 1682 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1683 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1684 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1685 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1686 | |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1687 | args->addr_ptr = (u64)addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1688 | |
| 1689 | return 0; |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1690 | |
| 1691 | err: |
| 1692 | i915_gem_object_put(obj); |
| 1693 | |
| 1694 | return addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1697 | 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] | 1698 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1699 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1700 | } |
| 1701 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1702 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1703 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1704 | * |
| 1705 | * A history of the GTT mmap interface: |
| 1706 | * |
| 1707 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1708 | * aligned and suitable for fencing, and still fit into the available |
| 1709 | * mappable space left by the pinned display objects. A classic problem |
| 1710 | * we called the page-fault-of-doom where we would ping-pong between |
| 1711 | * two objects that could not fit inside the GTT and so the memcpy |
| 1712 | * would page one object in at the expense of the other between every |
| 1713 | * single byte. |
| 1714 | * |
| 1715 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1716 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1717 | * object is too large for the available space (or simply too large |
| 1718 | * for the mappable aperture!), a view is created instead and faulted |
| 1719 | * into userspace. (This view is aligned and sized appropriately for |
| 1720 | * fenced access.) |
| 1721 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1722 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1723 | * delayed writes via GTT before performing direct access via WC. |
| 1724 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1725 | * Restrictions: |
| 1726 | * |
| 1727 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1728 | * hangs on some architectures, corruption on others. An attempt to service |
| 1729 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1730 | * |
| 1731 | * * the object must be able to fit into RAM (physical memory, though no |
| 1732 | * limited to the mappable aperture). |
| 1733 | * |
| 1734 | * |
| 1735 | * Caveats: |
| 1736 | * |
| 1737 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1738 | * all data to system memory. Subsequent access will not be synchronized. |
| 1739 | * |
| 1740 | * * all mappings are revoked on runtime device suspend. |
| 1741 | * |
| 1742 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1743 | * (older machines require fence register for display and blitter access |
| 1744 | * as well). Contention of the fence registers will cause the previous users |
| 1745 | * to be unmapped and any new access will generate new page faults. |
| 1746 | * |
| 1747 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1748 | * rather than the expected SIGSEGV. |
| 1749 | */ |
| 1750 | int i915_gem_mmap_gtt_version(void) |
| 1751 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1752 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1753 | } |
| 1754 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1755 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1756 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1757 | pgoff_t page_offset, |
| 1758 | unsigned int chunk) |
| 1759 | { |
| 1760 | struct i915_ggtt_view view; |
| 1761 | |
| 1762 | if (i915_gem_object_is_tiled(obj)) |
| 1763 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1764 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1765 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1766 | view.partial.offset = rounddown(page_offset, chunk); |
| 1767 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1768 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1769 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1772 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1773 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1774 | |
| 1775 | return view; |
| 1776 | } |
| 1777 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1778 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1779 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1780 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1781 | * |
| 1782 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1783 | * from userspace. The fault handler takes care of binding the object to |
| 1784 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1785 | * only if needed based on whether the old reg is still valid or the object |
| 1786 | * is tiled) and inserting a new PTE into the faulting process. |
| 1787 | * |
| 1788 | * Note that the faulting process may involve evicting existing objects |
| 1789 | * from the GTT and/or fence registers to make room. So performance may |
| 1790 | * suffer if the GTT working set is large or there are few fence registers |
| 1791 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1792 | * |
| 1793 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1794 | * 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] | 1795 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1796 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1797 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1798 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1799 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1800 | 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] | 1801 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1802 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1803 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1804 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1805 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1806 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1807 | pgoff_t page_offset; |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 1808 | int srcu; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1809 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1810 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1811 | /* Sanity check that we allow writing into this object */ |
| 1812 | if (i915_gem_object_is_readonly(obj) && write) |
| 1813 | return VM_FAULT_SIGBUS; |
| 1814 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1815 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1816 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1817 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1818 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1819 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1820 | /* 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] | 1821 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1822 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1823 | * where we are gazumped. |
| 1824 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1825 | ret = i915_gem_object_wait(obj, |
| 1826 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1827 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1828 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1829 | goto err; |
| 1830 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1831 | ret = i915_gem_object_pin_pages(obj); |
| 1832 | if (ret) |
| 1833 | goto err; |
| 1834 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1835 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1836 | |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1837 | srcu = i915_reset_trylock(dev_priv); |
| 1838 | if (srcu < 0) { |
| 1839 | ret = srcu; |
| 1840 | goto err_rpm; |
| 1841 | } |
| 1842 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1843 | ret = i915_mutex_lock_interruptible(dev); |
| 1844 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1845 | goto err_reset; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1846 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1847 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1848 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1849 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1850 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1853 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1854 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1855 | PIN_MAPPABLE | |
| 1856 | PIN_NONBLOCK | |
| 1857 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1858 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1859 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1860 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1861 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1862 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1863 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1864 | flags = PIN_MAPPABLE; |
| 1865 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1866 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1867 | |
| 1868 | /* |
| 1869 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1870 | * all hope that the hardware is able to track future writes. |
| 1871 | */ |
| 1872 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1873 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1874 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1875 | if (IS_ERR(vma) && !view.type) { |
| 1876 | flags = PIN_MAPPABLE; |
| 1877 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1878 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1879 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1880 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1881 | if (IS_ERR(vma)) { |
| 1882 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1883 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1884 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1885 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1886 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1887 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1888 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1889 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1890 | ret = i915_vma_pin_fence(vma); |
| 1891 | if (ret) |
| 1892 | goto err_unpin; |
| 1893 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1894 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1895 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1896 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1897 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1898 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1899 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1900 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1901 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1902 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1903 | /* Mark as being mmapped into userspace for later revocation */ |
| 1904 | assert_rpm_wakelock_held(dev_priv); |
| 1905 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1906 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1907 | GEM_BUG_ON(!obj->userfault_count); |
| 1908 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1909 | i915_vma_set_ggtt_write(vma); |
| 1910 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1911 | err_fence: |
| 1912 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1913 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1914 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1915 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1916 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1917 | err_reset: |
| 1918 | i915_reset_unlock(dev_priv, srcu); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1919 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1920 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1921 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1922 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1923 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1924 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1925 | /* |
| 1926 | * We eat errors when the gpu is terminally wedged to avoid |
| 1927 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1928 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1929 | * and so needs to be reported. |
| 1930 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1931 | if (!i915_terminally_wedged(dev_priv)) |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1932 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1933 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1934 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1935 | /* |
| 1936 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1937 | * handler to reset everything when re-faulting in |
| 1938 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1939 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1940 | case 0: |
| 1941 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1942 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1943 | case -EBUSY: |
| 1944 | /* |
| 1945 | * EBUSY is ok: this just means that another thread |
| 1946 | * already did the job. |
| 1947 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1948 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1949 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1950 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1951 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 1952 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1953 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1954 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1955 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
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 | } |
| 1958 | } |
| 1959 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1960 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 1961 | { |
| 1962 | struct i915_vma *vma; |
| 1963 | |
| 1964 | GEM_BUG_ON(!obj->userfault_count); |
| 1965 | |
| 1966 | obj->userfault_count = 0; |
| 1967 | list_del(&obj->userfault_link); |
| 1968 | drm_vma_node_unmap(&obj->base.vma_node, |
| 1969 | obj->base.dev->anon_inode->i_mapping); |
| 1970 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1971 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1972 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1973 | } |
| 1974 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1975 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1976 | * i915_gem_release_mmap - remove physical page mappings |
| 1977 | * @obj: obj in question |
| 1978 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1979 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1980 | * relinquish ownership of the pages back to the system. |
| 1981 | * |
| 1982 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1983 | * object through the GTT and then lose the fence register due to |
| 1984 | * resource pressure. Similarly if the object has been moved out of the |
| 1985 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1986 | * mapping will then trigger a page fault on the next user access, allowing |
| 1987 | * fixup by i915_gem_fault(). |
| 1988 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1989 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1990 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1991 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1992 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1993 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1994 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1995 | /* Serialisation between user GTT access and our code depends upon |
| 1996 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 1997 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1998 | * |
| 1999 | * Note that RPM complicates somewhat by adding an additional |
| 2000 | * requirement that operations to the GGTT be made holding the RPM |
| 2001 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2002 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2003 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2004 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2005 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2006 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2007 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2008 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2009 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2010 | |
| 2011 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2012 | * memory transactions from userspace before we return. The TLB |
| 2013 | * flushing implied above by changing the PTE above *should* be |
| 2014 | * sufficient, an extra barrier here just provides us with a bit |
| 2015 | * of paranoid documentation about our requirement to serialise |
| 2016 | * memory writes before touching registers / GSM. |
| 2017 | */ |
| 2018 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2019 | |
| 2020 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2021 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2022 | } |
| 2023 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2024 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2025 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2026 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2027 | int i; |
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 | /* |
| 2030 | * Only called during RPM suspend. All users of the userfault_list |
| 2031 | * must be holding an RPM wakeref to ensure that this can not |
| 2032 | * run concurrently with themselves (and use the struct_mutex for |
| 2033 | * protection between themselves). |
| 2034 | */ |
| 2035 | |
| 2036 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2037 | &dev_priv->mm.userfault_list, userfault_link) |
| 2038 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2039 | |
| 2040 | /* The fence will be lost when the device powers down. If any were |
| 2041 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2042 | * down! All other fences will be reacquired by the user upon waking. |
| 2043 | */ |
| 2044 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2045 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2046 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2047 | /* Ideally we want to assert that the fence register is not |
| 2048 | * live at this point (i.e. that no piece of code will be |
| 2049 | * trying to write through fence + GTT, as that both violates |
| 2050 | * our tracking of activity and associated locking/barriers, |
| 2051 | * but also is illegal given that the hw is powered down). |
| 2052 | * |
| 2053 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2054 | * That is not sufficient, and we need a more fine-grained |
| 2055 | * tool if we want to have a sanity check here. |
| 2056 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2057 | |
| 2058 | if (!reg->vma) |
| 2059 | continue; |
| 2060 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2061 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2062 | reg->dirty = true; |
| 2063 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2064 | } |
| 2065 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2066 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2067 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2068 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2069 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2070 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2071 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2072 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2073 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2074 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2075 | /* Attempt to reap some mmap space from dead objects */ |
| 2076 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2077 | err = i915_gem_wait_for_idle(dev_priv, |
| 2078 | I915_WAIT_INTERRUPTIBLE, |
| 2079 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2080 | if (err) |
| 2081 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2082 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2083 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2084 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2085 | if (!err) |
| 2086 | break; |
| 2087 | |
| 2088 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2089 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2090 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2094 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2095 | drm_gem_free_mmap_offset(&obj->base); |
| 2096 | } |
| 2097 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2098 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2099 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2100 | struct drm_device *dev, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 2101 | u32 handle, |
| 2102 | u64 *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2103 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2104 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2105 | int ret; |
| 2106 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2107 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2108 | if (!obj) |
| 2109 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2110 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2111 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2112 | if (ret == 0) |
| 2113 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2114 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2115 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2116 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2117 | } |
| 2118 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2119 | /** |
| 2120 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2121 | * @dev: DRM device |
| 2122 | * @data: GTT mapping ioctl data |
| 2123 | * @file: GEM object info |
| 2124 | * |
| 2125 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2126 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2127 | * up so we can get faults in the handler above. |
| 2128 | * |
| 2129 | * The fault handler will take care of binding the object into the GTT |
| 2130 | * (since it may have been evicted to make room for something), allocating |
| 2131 | * a fence register, and mapping the appropriate aperture address into |
| 2132 | * userspace. |
| 2133 | */ |
| 2134 | int |
| 2135 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2136 | struct drm_file *file) |
| 2137 | { |
| 2138 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2139 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2140 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2141 | } |
| 2142 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2143 | /* Immediately discard the backing storage */ |
| 2144 | static void |
| 2145 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2146 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2147 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2148 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2149 | if (obj->base.filp == NULL) |
| 2150 | return; |
| 2151 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2152 | /* Our goal here is to return as much of the memory as |
| 2153 | * is possible back to the system as we are called from OOM. |
| 2154 | * To do this we must instruct the shmfs to drop all of its |
| 2155 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2156 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2157 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2158 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2159 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2160 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2161 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2162 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2163 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2164 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2165 | struct address_space *mapping; |
| 2166 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2167 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2168 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2169 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2170 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2171 | case I915_MADV_DONTNEED: |
| 2172 | i915_gem_object_truncate(obj); |
| 2173 | case __I915_MADV_PURGED: |
| 2174 | return; |
| 2175 | } |
| 2176 | |
| 2177 | if (obj->base.filp == NULL) |
| 2178 | return; |
| 2179 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2180 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2181 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2184 | /* |
| 2185 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2186 | * ref count of those pages. |
| 2187 | */ |
| 2188 | static void check_release_pagevec(struct pagevec *pvec) |
| 2189 | { |
| 2190 | check_move_unevictable_pages(pvec); |
| 2191 | __pagevec_release(pvec); |
| 2192 | cond_resched(); |
| 2193 | } |
| 2194 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2195 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2196 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2197 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2198 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2199 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2200 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2201 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2202 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2203 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2204 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2205 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2206 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2207 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2208 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2209 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2210 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2211 | |
| 2212 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2213 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2214 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2215 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2216 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2217 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2218 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2219 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2220 | if (!pagevec_add(&pvec, page)) |
| 2221 | check_release_pagevec(&pvec); |
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_count(&pvec)) |
| 2224 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2225 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2226 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2227 | sg_free_table(pages); |
| 2228 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2229 | } |
| 2230 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2231 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2232 | { |
| 2233 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2234 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2235 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2236 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2237 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2238 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2239 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2242 | static struct sg_table * |
| 2243 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2244 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2245 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2246 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2247 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2248 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2249 | if (IS_ERR_OR_NULL(pages)) |
| 2250 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2251 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2252 | spin_lock(&i915->mm.obj_lock); |
| 2253 | list_del(&obj->mm.link); |
| 2254 | spin_unlock(&i915->mm.obj_lock); |
| 2255 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2256 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2257 | void *ptr; |
| 2258 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2259 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2260 | if (is_vmalloc_addr(ptr)) |
| 2261 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2262 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2263 | kunmap(kmap_to_page(ptr)); |
| 2264 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2265 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2268 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2269 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2270 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2271 | return pages; |
| 2272 | } |
| 2273 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2274 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2275 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2276 | { |
| 2277 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2278 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2279 | |
| 2280 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2281 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2282 | |
| 2283 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2284 | |
| 2285 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2286 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2287 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2288 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2289 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2290 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2291 | |
| 2292 | /* |
| 2293 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2294 | * array, hence protect them from being reaped by removing them from gtt |
| 2295 | * lists early. |
| 2296 | */ |
| 2297 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2298 | |
| 2299 | /* |
| 2300 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2301 | * NULL pages. In the future, when we have more asynchronous |
| 2302 | * get_pages backends we should be better able to handle the |
| 2303 | * cancellation of the async task in a more uniform manner. |
| 2304 | */ |
| 2305 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2306 | pages = ERR_PTR(-EINVAL); |
| 2307 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2308 | if (!IS_ERR(pages)) |
| 2309 | obj->ops->put_pages(obj, pages); |
| 2310 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2311 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2312 | unlock: |
| 2313 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2314 | |
| 2315 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2316 | } |
| 2317 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2318 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2319 | { |
| 2320 | struct sg_table new_st; |
| 2321 | struct scatterlist *sg, *new_sg; |
| 2322 | unsigned int i; |
| 2323 | |
| 2324 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2325 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2326 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2327 | 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] | 2328 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2329 | |
| 2330 | new_sg = new_st.sgl; |
| 2331 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2332 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2333 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2334 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2335 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2336 | new_sg = sg_next(new_sg); |
| 2337 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2338 | 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] | 2339 | |
| 2340 | sg_free_table(orig_st); |
| 2341 | |
| 2342 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2343 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2346 | 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] | 2347 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2348 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2349 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2350 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2351 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2352 | struct sg_table *st; |
| 2353 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2354 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2355 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2356 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2357 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2358 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2359 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2360 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2361 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2362 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2363 | /* |
| 2364 | * 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] | 2365 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2366 | * a GPU cache |
| 2367 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2368 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2369 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2370 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2371 | /* |
| 2372 | * If there's no chance of allocating enough pages for the whole |
| 2373 | * object, bail early. |
| 2374 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2375 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2376 | return -ENOMEM; |
| 2377 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2378 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2379 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2380 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2381 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2382 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2383 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2384 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2385 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2386 | } |
| 2387 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2388 | /* |
| 2389 | * 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] | 2390 | * at this point until we release them. |
| 2391 | * |
| 2392 | * Fail silently without starting the shrinker |
| 2393 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2394 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2395 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2396 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2397 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2398 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2399 | sg = st->sgl; |
| 2400 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2401 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2402 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2403 | const unsigned int shrink[] = { |
| 2404 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2405 | 0, |
| 2406 | }, *s = shrink; |
| 2407 | gfp_t gfp = noreclaim; |
| 2408 | |
| 2409 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2410 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2411 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame^] | 2412 | if (!IS_ERR(page)) |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2413 | break; |
| 2414 | |
| 2415 | if (!*s) { |
| 2416 | ret = PTR_ERR(page); |
| 2417 | goto err_sg; |
| 2418 | } |
| 2419 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2420 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2421 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2422 | /* |
| 2423 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2424 | * our own buffer, now let the real VM do its job and |
| 2425 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2426 | * |
| 2427 | * However, since graphics tend to be disposable, |
| 2428 | * defer the oom here by reporting the ENOMEM back |
| 2429 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2430 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2431 | if (!*s) { |
| 2432 | /* reclaim and warn, but no oom */ |
| 2433 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2434 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2435 | /* |
| 2436 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2437 | * kswapd to reclaim our pages (direct reclaim |
| 2438 | * does not effectively begin pageout of our |
| 2439 | * buffers on its own). However, direct reclaim |
| 2440 | * only waits for kswapd when under allocation |
| 2441 | * congestion. So as a result __GFP_RECLAIM is |
| 2442 | * unreliable and fails to actually reclaim our |
| 2443 | * dirty pages -- unless you try over and over |
| 2444 | * again with !__GFP_NORETRY. However, we still |
| 2445 | * want to fail this allocation rather than |
| 2446 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2447 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2448 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2449 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2450 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2451 | } while (1); |
| 2452 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2453 | if (!i || |
| 2454 | sg->length >= max_segment || |
| 2455 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2456 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2457 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2458 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2459 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2460 | st->nents++; |
| 2461 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2462 | } else { |
| 2463 | sg->length += PAGE_SIZE; |
| 2464 | } |
| 2465 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2466 | |
| 2467 | /* Check that the i965g/gm workaround works. */ |
| 2468 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2469 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2470 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2471 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2472 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2473 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2474 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2475 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2476 | i915_sg_trim(st); |
| 2477 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2478 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2479 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2480 | /* |
| 2481 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2482 | * it could not reserve enough large entries, asking |
| 2483 | * for PAGE_SIZE chunks instead may be helpful. |
| 2484 | */ |
| 2485 | if (max_segment > PAGE_SIZE) { |
| 2486 | for_each_sgt_page(page, sgt_iter, st) |
| 2487 | put_page(page); |
| 2488 | sg_free_table(st); |
| 2489 | |
| 2490 | max_segment = PAGE_SIZE; |
| 2491 | goto rebuild_st; |
| 2492 | } else { |
| 2493 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2494 | "Failed to DMA remap %lu pages\n", |
| 2495 | page_count); |
| 2496 | goto err_pages; |
| 2497 | } |
| 2498 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2499 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2500 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2501 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2502 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2503 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2504 | |
| 2505 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2506 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2507 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2508 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2509 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2510 | mapping_clear_unevictable(mapping); |
| 2511 | pagevec_init(&pvec); |
| 2512 | for_each_sgt_page(page, sgt_iter, st) { |
| 2513 | if (!pagevec_add(&pvec, page)) |
| 2514 | check_release_pagevec(&pvec); |
| 2515 | } |
| 2516 | if (pagevec_count(&pvec)) |
| 2517 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2518 | sg_free_table(st); |
| 2519 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2520 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2521 | /* |
| 2522 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2523 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2524 | * ENOMEM for a genuine allocation failure. |
| 2525 | * |
| 2526 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2527 | * space and so want to translate the error from shmemfs back to our |
| 2528 | * usual understanding of ENOMEM. |
| 2529 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2530 | if (ret == -ENOSPC) |
| 2531 | ret = -ENOMEM; |
| 2532 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2533 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2537 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2538 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2539 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2540 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2541 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2542 | int i; |
| 2543 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2544 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2545 | |
| 2546 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2547 | obj->mm.get_page.sg_idx = 0; |
| 2548 | |
| 2549 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2550 | |
| 2551 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2552 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2553 | GEM_BUG_ON(obj->mm.quirked); |
| 2554 | __i915_gem_object_pin_pages(obj); |
| 2555 | obj->mm.quirked = true; |
| 2556 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2557 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2558 | GEM_BUG_ON(!sg_page_sizes); |
| 2559 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2560 | |
| 2561 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2562 | * Calculate the supported page-sizes which fit into the given |
| 2563 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2564 | * to use opportunistically when later inserting into the GTT. For |
| 2565 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2566 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2567 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2568 | */ |
| 2569 | obj->mm.page_sizes.sg = 0; |
| 2570 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2571 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2572 | obj->mm.page_sizes.sg |= BIT(i); |
| 2573 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2574 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2575 | |
| 2576 | spin_lock(&i915->mm.obj_lock); |
| 2577 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2578 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2582 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2583 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2584 | |
| 2585 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2586 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2587 | return -EFAULT; |
| 2588 | } |
| 2589 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2590 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2591 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2592 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2593 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2596 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2597 | * 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] | 2598 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2599 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2600 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2601 | * or as the object is itself released. |
| 2602 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2603 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2604 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2605 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2606 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2607 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2608 | if (err) |
| 2609 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2610 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2611 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2612 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2613 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2614 | err = ____i915_gem_object_get_pages(obj); |
| 2615 | if (err) |
| 2616 | goto unlock; |
| 2617 | |
| 2618 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2619 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2620 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2621 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2622 | unlock: |
| 2623 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2624 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2627 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2628 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2629 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2630 | { |
| 2631 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2632 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2633 | struct sgt_iter sgt_iter; |
| 2634 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2635 | struct page *stack_pages[32]; |
| 2636 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2637 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2638 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2639 | void *addr; |
| 2640 | |
| 2641 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2642 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2643 | return kmap(sg_page(sgt->sgl)); |
| 2644 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2645 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2646 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2647 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2648 | if (!pages) |
| 2649 | return NULL; |
| 2650 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2651 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2652 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2653 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2654 | |
| 2655 | /* Check that we have the expected number of pages */ |
| 2656 | GEM_BUG_ON(i != n_pages); |
| 2657 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2658 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2659 | default: |
| 2660 | MISSING_CASE(type); |
| 2661 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2662 | case I915_MAP_WB: |
| 2663 | pgprot = PAGE_KERNEL; |
| 2664 | break; |
| 2665 | case I915_MAP_WC: |
| 2666 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2667 | break; |
| 2668 | } |
| 2669 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2670 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2671 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2672 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2673 | |
| 2674 | return addr; |
| 2675 | } |
| 2676 | |
| 2677 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2678 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2679 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2680 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2681 | enum i915_map_type has_type; |
| 2682 | bool pinned; |
| 2683 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2684 | int ret; |
| 2685 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2686 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2687 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2688 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2689 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2690 | if (ret) |
| 2691 | return ERR_PTR(ret); |
| 2692 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2693 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2694 | type &= ~I915_MAP_OVERRIDE; |
| 2695 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2696 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2697 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2698 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2699 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2700 | ret = ____i915_gem_object_get_pages(obj); |
| 2701 | if (ret) |
| 2702 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2703 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2704 | smp_mb__before_atomic(); |
| 2705 | } |
| 2706 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2707 | pinned = false; |
| 2708 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2709 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2710 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2711 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2712 | if (ptr && has_type != type) { |
| 2713 | if (pinned) { |
| 2714 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2715 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2716 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2717 | |
| 2718 | if (is_vmalloc_addr(ptr)) |
| 2719 | vunmap(ptr); |
| 2720 | else |
| 2721 | kunmap(kmap_to_page(ptr)); |
| 2722 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2723 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2724 | } |
| 2725 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2726 | if (!ptr) { |
| 2727 | ptr = i915_gem_object_map(obj, type); |
| 2728 | if (!ptr) { |
| 2729 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2730 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2731 | } |
| 2732 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2733 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2734 | } |
| 2735 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2736 | out_unlock: |
| 2737 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2738 | return ptr; |
| 2739 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2740 | err_unpin: |
| 2741 | atomic_dec(&obj->mm.pages_pin_count); |
| 2742 | err_unlock: |
| 2743 | ptr = ERR_PTR(ret); |
| 2744 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2745 | } |
| 2746 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2747 | static int |
| 2748 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2749 | const struct drm_i915_gem_pwrite *arg) |
| 2750 | { |
| 2751 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2752 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2753 | u64 remain, offset; |
| 2754 | unsigned int pg; |
| 2755 | |
| 2756 | /* Before we instantiate/pin the backing store for our use, we |
| 2757 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2758 | * the pagecache. We avoid the penalty of instantiating all the |
| 2759 | * pages, important if the user is just writing to a few and never |
| 2760 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2761 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2762 | * or clearing-before-use) before it is overwritten. |
| 2763 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2764 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2765 | return -ENODEV; |
| 2766 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2767 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2768 | return -EFAULT; |
| 2769 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2770 | /* Before the pages are instantiated the object is treated as being |
| 2771 | * in the CPU domain. The pages will be clflushed as required before |
| 2772 | * use, and we can freely write into the pages directly. If userspace |
| 2773 | * races pwrite with any other operation; corruption will ensue - |
| 2774 | * that is userspace's prerogative! |
| 2775 | */ |
| 2776 | |
| 2777 | remain = arg->size; |
| 2778 | offset = arg->offset; |
| 2779 | pg = offset_in_page(offset); |
| 2780 | |
| 2781 | do { |
| 2782 | unsigned int len, unwritten; |
| 2783 | struct page *page; |
| 2784 | void *data, *vaddr; |
| 2785 | int err; |
| 2786 | |
| 2787 | len = PAGE_SIZE - pg; |
| 2788 | if (len > remain) |
| 2789 | len = remain; |
| 2790 | |
| 2791 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2792 | offset, len, 0, |
| 2793 | &page, &data); |
| 2794 | if (err < 0) |
| 2795 | return err; |
| 2796 | |
| 2797 | vaddr = kmap(page); |
| 2798 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2799 | kunmap(page); |
| 2800 | |
| 2801 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2802 | offset, len, len - unwritten, |
| 2803 | page, data); |
| 2804 | if (err < 0) |
| 2805 | return err; |
| 2806 | |
| 2807 | if (unwritten) |
| 2808 | return -EFAULT; |
| 2809 | |
| 2810 | remain -= len; |
| 2811 | user_data += len; |
| 2812 | offset += len; |
| 2813 | pg = 0; |
| 2814 | } while (remain); |
| 2815 | |
| 2816 | return 0; |
| 2817 | } |
| 2818 | |
Chris Wilson | 8547444 | 2019-01-29 18:54:50 +0000 | [diff] [blame] | 2819 | static bool match_ring(struct i915_request *rq) |
| 2820 | { |
| 2821 | struct drm_i915_private *dev_priv = rq->i915; |
| 2822 | u32 ring = I915_READ(RING_START(rq->engine->mmio_base)); |
| 2823 | |
| 2824 | return ring == i915_ggtt_offset(rq->ring->vma); |
| 2825 | } |
| 2826 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2827 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2828 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2829 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2830 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2831 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2832 | |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2833 | /* |
| 2834 | * We are called by the error capture, reset and to dump engine |
| 2835 | * state at random points in time. In particular, note that neither is |
| 2836 | * crucially ordered with an interrupt. After a hang, the GPU is dead |
| 2837 | * and we assume that no more writes can happen (we waited long enough |
| 2838 | * for all writes that were in transaction to be flushed) - adding an |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2839 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2840 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2841 | * At all other times, we must assume the GPU is still running, but |
| 2842 | * we only care about the snapshot of this moment. |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2843 | */ |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2844 | spin_lock_irqsave(&engine->timeline.lock, flags); |
| 2845 | list_for_each_entry(request, &engine->timeline.requests, link) { |
Chris Wilson | 5013eb8 | 2019-01-28 18:18:11 +0000 | [diff] [blame] | 2846 | if (i915_request_completed(request)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2847 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2848 | |
Chris Wilson | 8547444 | 2019-01-29 18:54:50 +0000 | [diff] [blame] | 2849 | if (!i915_request_started(request)) |
| 2850 | break; |
| 2851 | |
| 2852 | /* More than one preemptible request may match! */ |
| 2853 | if (!match_ring(request)) |
| 2854 | break; |
| 2855 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2856 | active = request; |
| 2857 | break; |
| 2858 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2859 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2860 | |
| 2861 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2864 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2865 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2866 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2867 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2868 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 2869 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2870 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2871 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2872 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2873 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2874 | mutex_unlock(&dev->struct_mutex); |
| 2875 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2876 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2877 | /* |
| 2878 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2879 | * We do not need to do this test under locking as in the worst-case |
| 2880 | * we queue the retire worker once too often. |
| 2881 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2882 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2883 | queue_delayed_work(dev_priv->wq, |
| 2884 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2885 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2886 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2887 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2888 | static void shrink_caches(struct drm_i915_private *i915) |
| 2889 | { |
| 2890 | /* |
| 2891 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 2892 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 2893 | * with the aim of reducing fragmentation. |
| 2894 | */ |
| 2895 | kmem_cache_shrink(i915->priorities); |
| 2896 | kmem_cache_shrink(i915->dependencies); |
| 2897 | kmem_cache_shrink(i915->requests); |
| 2898 | kmem_cache_shrink(i915->luts); |
| 2899 | kmem_cache_shrink(i915->vmas); |
| 2900 | kmem_cache_shrink(i915->objects); |
| 2901 | } |
| 2902 | |
| 2903 | struct sleep_rcu_work { |
| 2904 | union { |
| 2905 | struct rcu_head rcu; |
| 2906 | struct work_struct work; |
| 2907 | }; |
| 2908 | struct drm_i915_private *i915; |
| 2909 | unsigned int epoch; |
| 2910 | }; |
| 2911 | |
| 2912 | static inline bool |
| 2913 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 2914 | { |
| 2915 | /* |
| 2916 | * There is a small chance that the epoch wrapped since we started |
| 2917 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 2918 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 2919 | */ |
| 2920 | return epoch == READ_ONCE(i915->gt.epoch); |
| 2921 | } |
| 2922 | |
| 2923 | static void __sleep_work(struct work_struct *work) |
| 2924 | { |
| 2925 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 2926 | struct drm_i915_private *i915 = s->i915; |
| 2927 | unsigned int epoch = s->epoch; |
| 2928 | |
| 2929 | kfree(s); |
| 2930 | if (same_epoch(i915, epoch)) |
| 2931 | shrink_caches(i915); |
| 2932 | } |
| 2933 | |
| 2934 | static void __sleep_rcu(struct rcu_head *rcu) |
| 2935 | { |
| 2936 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 2937 | struct drm_i915_private *i915 = s->i915; |
| 2938 | |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 2939 | destroy_rcu_head(&s->rcu); |
| 2940 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2941 | if (same_epoch(i915, s->epoch)) { |
| 2942 | INIT_WORK(&s->work, __sleep_work); |
| 2943 | queue_work(i915->wq, &s->work); |
| 2944 | } else { |
| 2945 | kfree(s); |
| 2946 | } |
| 2947 | } |
| 2948 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 2949 | static inline bool |
| 2950 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 2951 | { |
| 2952 | return (READ_ONCE(i915->gt.active_requests) || |
| 2953 | work_pending(&i915->gt.idle_work.work)); |
| 2954 | } |
| 2955 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2956 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
| 2957 | { |
| 2958 | struct intel_engine_cs *engine; |
| 2959 | enum intel_engine_id id; |
| 2960 | |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 2961 | if (i915_reset_failed(i915)) |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2962 | return; |
| 2963 | |
| 2964 | GEM_BUG_ON(i915->gt.active_requests); |
| 2965 | for_each_engine(engine, i915, id) { |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 2966 | GEM_BUG_ON(__i915_active_request_peek(&engine->timeline.last_request)); |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2967 | GEM_BUG_ON(engine->last_retired_context != |
| 2968 | to_intel_context(i915->kernel_context, engine)); |
| 2969 | } |
| 2970 | } |
| 2971 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2972 | static void |
| 2973 | i915_gem_idle_work_handler(struct work_struct *work) |
| 2974 | { |
| 2975 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2976 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2977 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2978 | bool rearm_hangcheck; |
| 2979 | |
| 2980 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 2981 | return; |
| 2982 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 2983 | if (READ_ONCE(dev_priv->gt.active_requests)) |
| 2984 | return; |
| 2985 | |
| 2986 | /* |
| 2987 | * Flush out the last user context, leaving only the pinned |
| 2988 | * kernel context resident. When we are idling on the kernel_context, |
| 2989 | * no more new requests (with a context switch) are emitted and we |
| 2990 | * can finally rest. A consequence is that the idle work handler is |
| 2991 | * always called at least twice before idling (and if the system is |
| 2992 | * idle that implies a round trip through the retire worker). |
| 2993 | */ |
| 2994 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 2995 | i915_gem_switch_to_kernel_context(dev_priv); |
| 2996 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 2997 | |
| 2998 | GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n", |
| 2999 | READ_ONCE(dev_priv->gt.active_requests)); |
| 3000 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3001 | /* |
| 3002 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3003 | * new request is submitted. As we don't trust the hardware, we |
| 3004 | * continue on if the wait times out. This is necessary to allow |
| 3005 | * the machine to suspend even if the hardware dies, and we will |
| 3006 | * try to recover in resume (after depriving the hardware of power, |
| 3007 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3008 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3009 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3010 | intel_engines_are_idle(dev_priv), |
| 3011 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3012 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3013 | |
| 3014 | rearm_hangcheck = |
| 3015 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3016 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3017 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3018 | /* Currently busy, come back later */ |
| 3019 | mod_delayed_work(dev_priv->wq, |
| 3020 | &dev_priv->gt.idle_work, |
| 3021 | msecs_to_jiffies(50)); |
| 3022 | goto out_rearm; |
| 3023 | } |
| 3024 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3025 | /* |
| 3026 | * New request retired after this work handler started, extend active |
| 3027 | * period until next instance of the work. |
| 3028 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3029 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3030 | goto out_unlock; |
| 3031 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 3032 | epoch = __i915_gem_park(dev_priv); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3033 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3034 | assert_kernel_context_is_current(dev_priv); |
| 3035 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3036 | rearm_hangcheck = false; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3037 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3038 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3039 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3040 | out_rearm: |
| 3041 | if (rearm_hangcheck) { |
| 3042 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3043 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3044 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3045 | |
| 3046 | /* |
| 3047 | * When we are idle, it is an opportune time to reap our caches. |
| 3048 | * However, we have many objects that utilise RCU and the ordered |
| 3049 | * i915->wq that this work is executing on. To try and flush any |
| 3050 | * pending frees now we are idle, we first wait for an RCU grace |
| 3051 | * period, and then queue a task (that will run last on the wq) to |
| 3052 | * shrink and re-optimize the caches. |
| 3053 | */ |
| 3054 | if (same_epoch(dev_priv, epoch)) { |
| 3055 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3056 | if (s) { |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3057 | init_rcu_head(&s->rcu); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3058 | s->i915 = dev_priv; |
| 3059 | s->epoch = epoch; |
| 3060 | call_rcu(&s->rcu, __sleep_rcu); |
| 3061 | } |
| 3062 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3063 | } |
| 3064 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3065 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3066 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3067 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3068 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3069 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3070 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3071 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3072 | mutex_lock(&i915->drm.struct_mutex); |
| 3073 | |
| 3074 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3075 | struct i915_gem_context *ctx = lut->ctx; |
| 3076 | struct i915_vma *vma; |
| 3077 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3078 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3079 | if (ctx->file_priv != fpriv) |
| 3080 | continue; |
| 3081 | |
| 3082 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3083 | GEM_BUG_ON(vma->obj != obj); |
| 3084 | |
| 3085 | /* We allow the process to have multiple handles to the same |
| 3086 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3087 | */ |
| 3088 | GEM_BUG_ON(!vma->open_count); |
| 3089 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3090 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3091 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3092 | list_del(&lut->obj_link); |
| 3093 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3094 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3095 | kmem_cache_free(i915->luts, lut); |
| 3096 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3097 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3098 | |
| 3099 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3100 | } |
| 3101 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3102 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3103 | { |
| 3104 | if (timeout_ns < 0) |
| 3105 | return MAX_SCHEDULE_TIMEOUT; |
| 3106 | |
| 3107 | if (timeout_ns == 0) |
| 3108 | return 0; |
| 3109 | |
| 3110 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3111 | } |
| 3112 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3113 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3114 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3115 | * @dev: drm device pointer |
| 3116 | * @data: ioctl data blob |
| 3117 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3118 | * |
| 3119 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3120 | * the timeout parameter. |
| 3121 | * -ETIME: object is still busy after timeout |
| 3122 | * -ERESTARTSYS: signal interrupted the wait |
| 3123 | * -ENONENT: object doesn't exist |
| 3124 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3125 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3126 | * -ENOMEM: damn |
| 3127 | * -ENODEV: Internal IRQ fail |
| 3128 | * -E?: The add request failed |
| 3129 | * |
| 3130 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3131 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3132 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3133 | * without holding struct_mutex the object may become re-busied before this |
| 3134 | * function completes. A similar but shorter * race condition exists in the busy |
| 3135 | * ioctl |
| 3136 | */ |
| 3137 | int |
| 3138 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3139 | { |
| 3140 | struct drm_i915_gem_wait *args = data; |
| 3141 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3142 | ktime_t start; |
| 3143 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3144 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3145 | if (args->flags != 0) |
| 3146 | return -EINVAL; |
| 3147 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3148 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3149 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3150 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3151 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3152 | start = ktime_get(); |
| 3153 | |
| 3154 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 3155 | I915_WAIT_INTERRUPTIBLE | |
| 3156 | I915_WAIT_PRIORITY | |
| 3157 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3158 | to_wait_timeout(args->timeout_ns)); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3159 | |
| 3160 | if (args->timeout_ns > 0) { |
| 3161 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3162 | if (args->timeout_ns < 0) |
| 3163 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3164 | |
| 3165 | /* |
| 3166 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3167 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3168 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3169 | * |
| 3170 | * This is a regression from the timespec->ktime conversion. |
| 3171 | */ |
| 3172 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3173 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3174 | |
| 3175 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3176 | if (ret == -ETIME && args->timeout_ns) |
| 3177 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3180 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3181 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3182 | } |
| 3183 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3184 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3185 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3186 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3187 | dev_err(i915->drm.dev, |
| 3188 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3189 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3190 | i915_gem_set_wedged(i915); |
| 3191 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | return 0; |
| 3195 | } |
| 3196 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3197 | static long |
| 3198 | wait_for_timelines(struct drm_i915_private *i915, |
| 3199 | unsigned int flags, long timeout) |
| 3200 | { |
| 3201 | struct i915_gt_timelines *gt = &i915->gt.timelines; |
| 3202 | struct i915_timeline *tl; |
| 3203 | |
| 3204 | if (!READ_ONCE(i915->gt.active_requests)) |
| 3205 | return timeout; |
| 3206 | |
| 3207 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3208 | list_for_each_entry(tl, >->active_list, link) { |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3209 | struct i915_request *rq; |
| 3210 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3211 | rq = i915_active_request_get_unlocked(&tl->last_request); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3212 | if (!rq) |
| 3213 | continue; |
| 3214 | |
| 3215 | mutex_unlock(>->mutex); |
| 3216 | |
| 3217 | /* |
| 3218 | * "Race-to-idle". |
| 3219 | * |
| 3220 | * Switching to the kernel context is often used a synchronous |
| 3221 | * step prior to idling, e.g. in suspend for flushing all |
| 3222 | * current operations to memory before sleeping. These we |
| 3223 | * want to complete as quickly as possible to avoid prolonged |
| 3224 | * stalls, so allow the gpu to boost to maximum clocks. |
| 3225 | */ |
| 3226 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3227 | gen6_rps_boost(rq); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3228 | |
| 3229 | timeout = i915_request_wait(rq, flags, timeout); |
| 3230 | i915_request_put(rq); |
| 3231 | if (timeout < 0) |
| 3232 | return timeout; |
| 3233 | |
| 3234 | /* restart after reacquiring the lock */ |
| 3235 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3236 | tl = list_entry(>->active_list, typeof(*tl), link); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3237 | } |
| 3238 | mutex_unlock(>->mutex); |
| 3239 | |
| 3240 | return timeout; |
| 3241 | } |
| 3242 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3243 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 3244 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3245 | { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3246 | GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", |
| 3247 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
| 3248 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3249 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3250 | /* If the device is asleep, we have no requests outstanding */ |
| 3251 | if (!READ_ONCE(i915->gt.awake)) |
| 3252 | return 0; |
| 3253 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3254 | timeout = wait_for_timelines(i915, flags, timeout); |
| 3255 | if (timeout < 0) |
| 3256 | return timeout; |
| 3257 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3258 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3259 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3260 | |
| 3261 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3262 | |
Chris Wilson | c1e63f6 | 2018-08-08 11:50:59 +0100 | [diff] [blame] | 3263 | if (GEM_SHOW_DEBUG() && !timeout) { |
| 3264 | /* Presume that timeout was non-zero to begin with! */ |
| 3265 | dev_warn(&i915->drm.pdev->dev, |
| 3266 | "Missed idle-completion interrupt!\n"); |
| 3267 | GEM_TRACE_DUMP(); |
| 3268 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3269 | |
| 3270 | err = wait_for_engines(i915); |
| 3271 | if (err) |
| 3272 | return err; |
| 3273 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3274 | i915_retire_requests(i915); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3275 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3276 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3277 | |
| 3278 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3279 | } |
| 3280 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3281 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3282 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3283 | /* |
| 3284 | * We manually flush the CPU domain so that we can override and |
| 3285 | * force the flush for the display, and perform it asyncrhonously. |
| 3286 | */ |
| 3287 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3288 | if (obj->cache_dirty) |
| 3289 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3290 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3294 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3295 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3296 | return; |
| 3297 | |
| 3298 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3299 | __i915_gem_object_flush_for_display(obj); |
| 3300 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3301 | } |
| 3302 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3303 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3304 | * Moves a single object to the WC read, and possibly write domain. |
| 3305 | * @obj: object to act on |
| 3306 | * @write: ask for write access or read only |
| 3307 | * |
| 3308 | * This function returns when the move is complete, including waiting on |
| 3309 | * flushes to occur. |
| 3310 | */ |
| 3311 | int |
| 3312 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3313 | { |
| 3314 | int ret; |
| 3315 | |
| 3316 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3317 | |
| 3318 | ret = i915_gem_object_wait(obj, |
| 3319 | I915_WAIT_INTERRUPTIBLE | |
| 3320 | I915_WAIT_LOCKED | |
| 3321 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3322 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3323 | if (ret) |
| 3324 | return ret; |
| 3325 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3326 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3327 | return 0; |
| 3328 | |
| 3329 | /* Flush and acquire obj->pages so that we are coherent through |
| 3330 | * direct access in memory with previous cached writes through |
| 3331 | * shmemfs and that our cache domain tracking remains valid. |
| 3332 | * For example, if the obj->filp was moved to swap without us |
| 3333 | * being notified and releasing the pages, we would mistakenly |
| 3334 | * continue to assume that the obj remained out of the CPU cached |
| 3335 | * domain. |
| 3336 | */ |
| 3337 | ret = i915_gem_object_pin_pages(obj); |
| 3338 | if (ret) |
| 3339 | return ret; |
| 3340 | |
| 3341 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3342 | |
| 3343 | /* Serialise direct access to this object with the barriers for |
| 3344 | * coherent writes from the GPU, by effectively invalidating the |
| 3345 | * WC domain upon first access. |
| 3346 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3347 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3348 | mb(); |
| 3349 | |
| 3350 | /* It should now be out of any other write domains, and we can update |
| 3351 | * the domain values for our changes. |
| 3352 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3353 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3354 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3355 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3356 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3357 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3358 | obj->mm.dirty = true; |
| 3359 | } |
| 3360 | |
| 3361 | i915_gem_object_unpin_pages(obj); |
| 3362 | return 0; |
| 3363 | } |
| 3364 | |
| 3365 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3366 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3367 | * @obj: object to act on |
| 3368 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3369 | * |
| 3370 | * This function returns when the move is complete, including waiting on |
| 3371 | * flushes to occur. |
| 3372 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3373 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3374 | 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] | 3375 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3376 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3377 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3378 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3379 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3380 | ret = i915_gem_object_wait(obj, |
| 3381 | I915_WAIT_INTERRUPTIBLE | |
| 3382 | I915_WAIT_LOCKED | |
| 3383 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3384 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3385 | if (ret) |
| 3386 | return ret; |
| 3387 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3388 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3389 | return 0; |
| 3390 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3391 | /* Flush and acquire obj->pages so that we are coherent through |
| 3392 | * direct access in memory with previous cached writes through |
| 3393 | * shmemfs and that our cache domain tracking remains valid. |
| 3394 | * For example, if the obj->filp was moved to swap without us |
| 3395 | * being notified and releasing the pages, we would mistakenly |
| 3396 | * continue to assume that the obj remained out of the CPU cached |
| 3397 | * domain. |
| 3398 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3399 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3400 | if (ret) |
| 3401 | return ret; |
| 3402 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3403 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3404 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3405 | /* Serialise direct access to this object with the barriers for |
| 3406 | * coherent writes from the GPU, by effectively invalidating the |
| 3407 | * GTT domain upon first access. |
| 3408 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3409 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3410 | mb(); |
| 3411 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3412 | /* It should now be out of any other write domains, and we can update |
| 3413 | * the domain values for our changes. |
| 3414 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3415 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3416 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3417 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3418 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3419 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3420 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3421 | } |
| 3422 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3423 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3424 | return 0; |
| 3425 | } |
| 3426 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3427 | /** |
| 3428 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3429 | * @obj: object to act on |
| 3430 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3431 | * |
| 3432 | * After this function returns, the object will be in the new cache-level |
| 3433 | * across all GTT and the contents of the backing storage will be coherent, |
| 3434 | * with respect to the new cache-level. In order to keep the backing storage |
| 3435 | * coherent for all users, we only allow a single cache level to be set |
| 3436 | * globally on the object and prevent it from being changed whilst the |
| 3437 | * hardware is reading from the object. That is if the object is currently |
| 3438 | * on the scanout it will be set to uncached (or equivalent display |
| 3439 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3440 | * that all direct access to the scanout remains coherent. |
| 3441 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3442 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3443 | enum i915_cache_level cache_level) |
| 3444 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3445 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3446 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3447 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3448 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3449 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3450 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3451 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3452 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3453 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3454 | * be invalid given the new cache-level. This is principally to |
| 3455 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3456 | * reading an invalid PTE on older architectures. |
| 3457 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3458 | restart: |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3459 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3460 | if (!drm_mm_node_allocated(&vma->node)) |
| 3461 | continue; |
| 3462 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3463 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3464 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3465 | return -EBUSY; |
| 3466 | } |
| 3467 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3468 | if (!i915_vma_is_closed(vma) && |
| 3469 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3470 | continue; |
| 3471 | |
| 3472 | ret = i915_vma_unbind(vma); |
| 3473 | if (ret) |
| 3474 | return ret; |
| 3475 | |
| 3476 | /* As unbinding may affect other elements in the |
| 3477 | * obj->vma_list (due to side-effects from retiring |
| 3478 | * an active vma), play safe and restart the iterator. |
| 3479 | */ |
| 3480 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3481 | } |
| 3482 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3483 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3484 | * cache-level on the PTE. We could simply unbind them all and |
| 3485 | * rebind with the correct cache-level on next use. However since |
| 3486 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3487 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3488 | * state and so involves less work. |
| 3489 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3490 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3491 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3492 | * If we wait upon the object, we know that all the bound |
| 3493 | * VMA are no longer active. |
| 3494 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3495 | ret = i915_gem_object_wait(obj, |
| 3496 | I915_WAIT_INTERRUPTIBLE | |
| 3497 | I915_WAIT_LOCKED | |
| 3498 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3499 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3500 | if (ret) |
| 3501 | return ret; |
| 3502 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3503 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3504 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3505 | /* Access to snoopable pages through the GTT is |
| 3506 | * incoherent and on some machines causes a hard |
| 3507 | * lockup. Relinquish the CPU mmaping to force |
| 3508 | * userspace to refault in the pages and we can |
| 3509 | * then double check if the GTT mapping is still |
| 3510 | * valid for that pointer access. |
| 3511 | */ |
| 3512 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3513 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3514 | /* As we no longer need a fence for GTT access, |
| 3515 | * we can relinquish it now (and so prevent having |
| 3516 | * to steal a fence from someone else on the next |
| 3517 | * fence request). Note GPU activity would have |
| 3518 | * dropped the fence as all snoopable access is |
| 3519 | * supposed to be linear. |
| 3520 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3521 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3522 | ret = i915_vma_put_fence(vma); |
| 3523 | if (ret) |
| 3524 | return ret; |
| 3525 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3526 | } else { |
| 3527 | /* We either have incoherent backing store and |
| 3528 | * so no GTT access or the architecture is fully |
| 3529 | * coherent. In such cases, existing GTT mmaps |
| 3530 | * ignore the cache bit in the PTE and we can |
| 3531 | * rewrite it without confusing the GPU or having |
| 3532 | * to force userspace to fault back in its mmaps. |
| 3533 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3534 | } |
| 3535 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3536 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3537 | if (!drm_mm_node_allocated(&vma->node)) |
| 3538 | continue; |
| 3539 | |
| 3540 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3541 | if (ret) |
| 3542 | return ret; |
| 3543 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3544 | } |
| 3545 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3546 | list_for_each_entry(vma, &obj->vma.list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3547 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3548 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3549 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3550 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3551 | return 0; |
| 3552 | } |
| 3553 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3554 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3555 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3556 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3557 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3558 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3559 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3560 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3561 | rcu_read_lock(); |
| 3562 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3563 | if (!obj) { |
| 3564 | err = -ENOENT; |
| 3565 | goto out; |
| 3566 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3567 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3568 | switch (obj->cache_level) { |
| 3569 | case I915_CACHE_LLC: |
| 3570 | case I915_CACHE_L3_LLC: |
| 3571 | args->caching = I915_CACHING_CACHED; |
| 3572 | break; |
| 3573 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3574 | case I915_CACHE_WT: |
| 3575 | args->caching = I915_CACHING_DISPLAY; |
| 3576 | break; |
| 3577 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3578 | default: |
| 3579 | args->caching = I915_CACHING_NONE; |
| 3580 | break; |
| 3581 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3582 | out: |
| 3583 | rcu_read_unlock(); |
| 3584 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3585 | } |
| 3586 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3587 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3588 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3589 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3590 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3591 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3592 | struct drm_i915_gem_object *obj; |
| 3593 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3594 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3595 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3596 | switch (args->caching) { |
| 3597 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3598 | level = I915_CACHE_NONE; |
| 3599 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3600 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3601 | /* |
| 3602 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3603 | * snooped mapping may leave stale data in a corresponding CPU |
| 3604 | * cacheline, whereas normally such cachelines would get |
| 3605 | * invalidated. |
| 3606 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3607 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3608 | return -ENODEV; |
| 3609 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3610 | level = I915_CACHE_LLC; |
| 3611 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3612 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3613 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3614 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3615 | default: |
| 3616 | return -EINVAL; |
| 3617 | } |
| 3618 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3619 | obj = i915_gem_object_lookup(file, args->handle); |
| 3620 | if (!obj) |
| 3621 | return -ENOENT; |
| 3622 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3623 | /* |
| 3624 | * The caching mode of proxy object is handled by its generator, and |
| 3625 | * not allowed to be changed by userspace. |
| 3626 | */ |
| 3627 | if (i915_gem_object_is_proxy(obj)) { |
| 3628 | ret = -ENXIO; |
| 3629 | goto out; |
| 3630 | } |
| 3631 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3632 | if (obj->cache_level == level) |
| 3633 | goto out; |
| 3634 | |
| 3635 | ret = i915_gem_object_wait(obj, |
| 3636 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3637 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3638 | if (ret) |
| 3639 | goto out; |
| 3640 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3641 | ret = i915_mutex_lock_interruptible(dev); |
| 3642 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3643 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3644 | |
| 3645 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3646 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3647 | |
| 3648 | out: |
| 3649 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3650 | return ret; |
| 3651 | } |
| 3652 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3653 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 3654 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 3655 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 3656 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 3657 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3658 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3659 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3660 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3661 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3662 | const struct i915_ggtt_view *view, |
| 3663 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3664 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3665 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3666 | int ret; |
| 3667 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3668 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3669 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3670 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3671 | * display coherency whilst setting up the cache domains. |
| 3672 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3673 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3674 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3675 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3676 | * a result, we make sure that the pinning that is about to occur is |
| 3677 | * done with uncached PTEs. This is lowest common denominator for all |
| 3678 | * chipsets. |
| 3679 | * |
| 3680 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3681 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3682 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3683 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3684 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3685 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3686 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3687 | if (ret) { |
| 3688 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3689 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3690 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3691 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3692 | /* As the user may map the buffer once pinned in the display plane |
| 3693 | * (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] | 3694 | * always use map_and_fenceable for all scanout buffers. However, |
| 3695 | * it may simply be too big to fit into mappable, in which case |
| 3696 | * put it anyway and hope that userspace can cope (but always first |
| 3697 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3698 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3699 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3700 | if ((flags & PIN_MAPPABLE) == 0 && |
| 3701 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3702 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3703 | flags | |
| 3704 | PIN_MAPPABLE | |
| 3705 | PIN_NONBLOCK); |
| 3706 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3707 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3708 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3709 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3710 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 3711 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 3712 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3713 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3714 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3715 | /* It should now be out of any other write domains, and we can update |
| 3716 | * the domain values for our changes. |
| 3717 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3718 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3719 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3720 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3721 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3722 | err_unpin_global: |
| 3723 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3724 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3728 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3729 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 3730 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3731 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3732 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 3733 | return; |
| 3734 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3735 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 3736 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3737 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3738 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 3739 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3740 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3741 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3742 | } |
| 3743 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3744 | /** |
| 3745 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3746 | * @obj: object to act on |
| 3747 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3748 | * |
| 3749 | * This function returns when the move is complete, including waiting on |
| 3750 | * flushes to occur. |
| 3751 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3752 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3753 | 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] | 3754 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3755 | int ret; |
| 3756 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3757 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3758 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3759 | ret = i915_gem_object_wait(obj, |
| 3760 | I915_WAIT_INTERRUPTIBLE | |
| 3761 | I915_WAIT_LOCKED | |
| 3762 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3763 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3764 | if (ret) |
| 3765 | return ret; |
| 3766 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3767 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3768 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3769 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3770 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 3771 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3772 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | /* It should now be out of any other write domains, and we can update |
| 3776 | * the domain values for our changes. |
| 3777 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3778 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3779 | |
| 3780 | /* If we're writing through the CPU, then the GPU read domains will |
| 3781 | * need to be invalidated at next use. |
| 3782 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3783 | if (write) |
| 3784 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3785 | |
| 3786 | return 0; |
| 3787 | } |
| 3788 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3789 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3790 | * emitted over 20 msec ago. |
| 3791 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3792 | * Note that if we were to use the current jiffies each time around the loop, |
| 3793 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3794 | * render a frame was over 20ms. |
| 3795 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3796 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3797 | * relatively low latency when blocking on a particular request to finish. |
| 3798 | */ |
| 3799 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3800 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3801 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3802 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3803 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 3804 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3805 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3806 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3807 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 3808 | /* ABI: return -EIO if already wedged */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 3809 | ret = i915_terminally_wedged(dev_priv); |
| 3810 | if (ret) |
| 3811 | return ret; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3812 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3813 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3814 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3815 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3816 | break; |
| 3817 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3818 | if (target) { |
| 3819 | list_del(&target->client_link); |
| 3820 | target->file_priv = NULL; |
| 3821 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 3822 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3823 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3824 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3825 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3826 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3827 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3828 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3829 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3830 | return 0; |
| 3831 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3832 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3833 | I915_WAIT_INTERRUPTIBLE, |
| 3834 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3835 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3836 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3837 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3838 | } |
| 3839 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3840 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3841 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 3842 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 3843 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 3844 | u64 alignment, |
| 3845 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3846 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3847 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 3848 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3849 | struct i915_vma *vma; |
| 3850 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 3851 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3852 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3853 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 3854 | if (flags & PIN_MAPPABLE && |
| 3855 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3856 | /* If the required space is larger than the available |
| 3857 | * aperture, we will not able to find a slot for the |
| 3858 | * object and unbinding the object now will be in |
| 3859 | * vain. Worse, doing so may cause us to ping-pong |
| 3860 | * the object in and out of the Global GTT and |
| 3861 | * waste a lot of cycles under the mutex. |
| 3862 | */ |
| 3863 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 3864 | return ERR_PTR(-E2BIG); |
| 3865 | |
| 3866 | /* If NONBLOCK is set the caller is optimistically |
| 3867 | * trying to cache the full object within the mappable |
| 3868 | * aperture, and *must* have a fallback in place for |
| 3869 | * situations where we cannot bind the object. We |
| 3870 | * can be a little more lax here and use the fallback |
| 3871 | * more often to avoid costly migrations of ourselves |
| 3872 | * and other objects within the aperture. |
| 3873 | * |
| 3874 | * Half-the-aperture is used as a simple heuristic. |
| 3875 | * More interesting would to do search for a free |
| 3876 | * block prior to making the commitment to unbind. |
| 3877 | * That caters for the self-harm case, and with a |
| 3878 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 3879 | * we could try to minimise harm to others. |
| 3880 | */ |
| 3881 | if (flags & PIN_NONBLOCK && |
| 3882 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 3883 | return ERR_PTR(-ENOSPC); |
| 3884 | } |
| 3885 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 3886 | vma = i915_vma_instance(obj, vm, view); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame^] | 3887 | if (IS_ERR(vma)) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3888 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3889 | |
| 3890 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3891 | if (flags & PIN_NONBLOCK) { |
| 3892 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 3893 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3894 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3895 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 3896 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3897 | return ERR_PTR(-ENOSPC); |
| 3898 | } |
| 3899 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3900 | WARN(i915_vma_is_pinned(vma), |
| 3901 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3902 | " offset=%08x, req.alignment=%llx," |
| 3903 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 3904 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3905 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3906 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3907 | ret = i915_vma_unbind(vma); |
| 3908 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3909 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3910 | } |
| 3911 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3912 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 3913 | if (ret) |
| 3914 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3915 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3916 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3919 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3920 | { |
| 3921 | /* Note that we could alias engines in the execbuf API, but |
| 3922 | * that would be very unwise as it prevents userspace from |
| 3923 | * fine control over engine selection. Ahem. |
| 3924 | * |
| 3925 | * This should be something like EXEC_MAX_ENGINE instead of |
| 3926 | * I915_NUM_ENGINES. |
| 3927 | */ |
| 3928 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 3929 | return 0x10000 << id; |
| 3930 | } |
| 3931 | |
| 3932 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 3933 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 3934 | /* The uABI guarantees an active writer is also amongst the read |
| 3935 | * engines. This would be true if we accessed the activity tracking |
| 3936 | * under the lock, but as we perform the lookup of the object and |
| 3937 | * its activity locklessly we can not guarantee that the last_write |
| 3938 | * being active implies that we have set the same engine flag from |
| 3939 | * last_read - hence we always set both read and write busy for |
| 3940 | * last_write. |
| 3941 | */ |
| 3942 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3943 | } |
| 3944 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3945 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3946 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3947 | unsigned int (*flag)(unsigned int id)) |
| 3948 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3949 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3950 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3951 | /* We have to check the current hw status of the fence as the uABI |
| 3952 | * guarantees forward progress. We could rely on the idle worker |
| 3953 | * to eventually flush us, but to minimise latency just ask the |
| 3954 | * hardware. |
| 3955 | * |
| 3956 | * Note we only report on the status of native fences. |
| 3957 | */ |
| 3958 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3959 | return 0; |
| 3960 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3961 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3962 | rq = container_of(fence, struct i915_request, fence); |
| 3963 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3964 | return 0; |
| 3965 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 3966 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3967 | } |
| 3968 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3969 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3970 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3971 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3972 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3973 | } |
| 3974 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3975 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3976 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3977 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3978 | if (!fence) |
| 3979 | return 0; |
| 3980 | |
| 3981 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3982 | } |
| 3983 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3984 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3985 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3986 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3987 | { |
| 3988 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3989 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3990 | struct reservation_object_list *list; |
| 3991 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3992 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3993 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3994 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3995 | rcu_read_lock(); |
| 3996 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3997 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3998 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3999 | |
| 4000 | /* A discrepancy here is that we do not report the status of |
| 4001 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4002 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4003 | * This also means that wait-ioctl may report an object as busy, |
| 4004 | * where busy-ioctl considers it idle. |
| 4005 | * |
| 4006 | * We trade the ability to warn of foreign fences to report on which |
| 4007 | * i915 engines are active for the object. |
| 4008 | * |
| 4009 | * Alternatively, we can trade that extra information on read/write |
| 4010 | * activity with |
| 4011 | * args->busy = |
| 4012 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4013 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4014 | * |
| 4015 | */ |
| 4016 | retry: |
| 4017 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4018 | |
| 4019 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4020 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4021 | |
| 4022 | /* Translate shared fences to READ set of engines */ |
| 4023 | list = rcu_dereference(obj->resv->fence); |
| 4024 | if (list) { |
| 4025 | unsigned int shared_count = list->shared_count, i; |
| 4026 | |
| 4027 | for (i = 0; i < shared_count; ++i) { |
| 4028 | struct dma_fence *fence = |
| 4029 | rcu_dereference(list->shared[i]); |
| 4030 | |
| 4031 | args->busy |= busy_check_reader(fence); |
| 4032 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4033 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4034 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4035 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4036 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4037 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4038 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4039 | out: |
| 4040 | rcu_read_unlock(); |
| 4041 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | int |
| 4045 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4046 | struct drm_file *file_priv) |
| 4047 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4048 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4049 | } |
| 4050 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4051 | int |
| 4052 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4053 | struct drm_file *file_priv) |
| 4054 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4055 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4056 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4057 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4058 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4059 | |
| 4060 | switch (args->madv) { |
| 4061 | case I915_MADV_DONTNEED: |
| 4062 | case I915_MADV_WILLNEED: |
| 4063 | break; |
| 4064 | default: |
| 4065 | return -EINVAL; |
| 4066 | } |
| 4067 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4068 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4069 | if (!obj) |
| 4070 | return -ENOENT; |
| 4071 | |
| 4072 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4073 | if (err) |
| 4074 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4075 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4076 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4077 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4078 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4079 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4080 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4081 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4082 | obj->mm.quirked = false; |
| 4083 | } |
| 4084 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4085 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4086 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4087 | obj->mm.quirked = true; |
| 4088 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4089 | } |
| 4090 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4091 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4092 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4093 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4094 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4095 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4096 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4097 | i915_gem_object_truncate(obj); |
| 4098 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4099 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4100 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4101 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4102 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4103 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4104 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4105 | } |
| 4106 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4107 | static void |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4108 | frontbuffer_retire(struct i915_active_request *active, |
| 4109 | struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4110 | { |
| 4111 | struct drm_i915_gem_object *obj = |
| 4112 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4113 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4114 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4117 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4118 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4119 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4120 | mutex_init(&obj->mm.lock); |
| 4121 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4122 | spin_lock_init(&obj->vma.lock); |
| 4123 | INIT_LIST_HEAD(&obj->vma.list); |
| 4124 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4125 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4126 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4127 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4128 | init_rcu_head(&obj->rcu); |
| 4129 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4130 | obj->ops = ops; |
| 4131 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4132 | reservation_object_init(&obj->__builtin_resv); |
| 4133 | obj->resv = &obj->__builtin_resv; |
| 4134 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4135 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4136 | i915_active_request_init(&obj->frontbuffer_write, |
| 4137 | NULL, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4138 | |
| 4139 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4140 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4141 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4142 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4143 | 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] | 4144 | } |
| 4145 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4146 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4147 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4148 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4149 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4150 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4151 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4152 | |
| 4153 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4154 | }; |
| 4155 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4156 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4157 | struct drm_gem_object *obj, |
| 4158 | size_t size) |
| 4159 | { |
| 4160 | struct drm_i915_private *i915 = to_i915(dev); |
| 4161 | unsigned long flags = VM_NORESERVE; |
| 4162 | struct file *filp; |
| 4163 | |
| 4164 | drm_gem_private_object_init(dev, obj, size); |
| 4165 | |
| 4166 | if (i915->mm.gemfs) |
| 4167 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4168 | flags); |
| 4169 | else |
| 4170 | filp = shmem_file_setup("i915", size, flags); |
| 4171 | |
| 4172 | if (IS_ERR(filp)) |
| 4173 | return PTR_ERR(filp); |
| 4174 | |
| 4175 | obj->filp = filp; |
| 4176 | |
| 4177 | return 0; |
| 4178 | } |
| 4179 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4180 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4181 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4182 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4183 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4184 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4185 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4186 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4187 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4188 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4189 | /* There is a prevalence of the assumption that we fit the object's |
| 4190 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4191 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4192 | * such a local variable, please consider fixing! |
| 4193 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4194 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4195 | return ERR_PTR(-E2BIG); |
| 4196 | |
| 4197 | if (overflows_type(size, obj->base.size)) |
| 4198 | return ERR_PTR(-E2BIG); |
| 4199 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4200 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4201 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4202 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4203 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4204 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4205 | if (ret) |
| 4206 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4207 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4208 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4209 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4210 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4211 | mask &= ~__GFP_HIGHMEM; |
| 4212 | mask |= __GFP_DMA32; |
| 4213 | } |
| 4214 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4215 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4216 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4217 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4218 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4219 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4220 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4221 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4222 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4223 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4224 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4225 | /* 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] | 4226 | * cache) for about a 10% performance improvement |
| 4227 | * compared to uncached. Graphics requests other than |
| 4228 | * display scanout are coherent with the CPU in |
| 4229 | * accessing this cache. This means in this mode we |
| 4230 | * don't need to clflush on the CPU side, and on the |
| 4231 | * GPU side we only need to flush internal caches to |
| 4232 | * get data visible to the CPU. |
| 4233 | * |
| 4234 | * However, we maintain the display planes as UC, and so |
| 4235 | * need to rebind when first used as such. |
| 4236 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4237 | cache_level = I915_CACHE_LLC; |
| 4238 | else |
| 4239 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4240 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4241 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4242 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4243 | trace_i915_gem_object_create(obj); |
| 4244 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4245 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4246 | |
| 4247 | fail: |
| 4248 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4249 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4252 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4253 | { |
| 4254 | /* If we are the last user of the backing storage (be it shmemfs |
| 4255 | * pages or stolen etc), we know that the pages are going to be |
| 4256 | * immediately released. In this case, we can then skip copying |
| 4257 | * back the contents from the GPU. |
| 4258 | */ |
| 4259 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4260 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4261 | return false; |
| 4262 | |
| 4263 | if (obj->base.filp == NULL) |
| 4264 | return true; |
| 4265 | |
| 4266 | /* At first glance, this looks racy, but then again so would be |
| 4267 | * userspace racing mmap against close. However, the first external |
| 4268 | * reference to the filp can only be obtained through the |
| 4269 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4270 | * acquiring such a reference whilst we are in the middle of |
| 4271 | * freeing the object. |
| 4272 | */ |
| 4273 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4274 | } |
| 4275 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4276 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4277 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4278 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4279 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4280 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4281 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4282 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4283 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4284 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4285 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4286 | trace_i915_gem_object_destroy(obj); |
| 4287 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4288 | mutex_lock(&i915->drm.struct_mutex); |
| 4289 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4290 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4291 | list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4292 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4293 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4294 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4295 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4296 | GEM_BUG_ON(!list_empty(&obj->vma.list)); |
| 4297 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4298 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4299 | /* This serializes freeing with the shrinker. Since the free |
| 4300 | * is delayed, first by RCU then by the workqueue, we want the |
| 4301 | * shrinker to be able to free pages of unreferenced objects, |
| 4302 | * or else we may oom whilst there are plenty of deferred |
| 4303 | * freed objects. |
| 4304 | */ |
| 4305 | if (i915_gem_object_has_pages(obj)) { |
| 4306 | spin_lock(&i915->mm.obj_lock); |
| 4307 | list_del_init(&obj->mm.link); |
| 4308 | spin_unlock(&i915->mm.obj_lock); |
| 4309 | } |
| 4310 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4311 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4312 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4313 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4314 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4315 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4316 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4317 | |
| 4318 | if (obj->ops->release) |
| 4319 | obj->ops->release(obj); |
| 4320 | |
| 4321 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4322 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4323 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4324 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4325 | |
| 4326 | if (obj->base.import_attach) |
| 4327 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4328 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4329 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4330 | drm_gem_object_release(&obj->base); |
| 4331 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4332 | |
| 4333 | kfree(obj->bit_17); |
| 4334 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4335 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4336 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4337 | atomic_dec(&i915->mm.free_count); |
| 4338 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4339 | if (on) |
| 4340 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4341 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4342 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4343 | } |
| 4344 | |
| 4345 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4346 | { |
| 4347 | struct llist_node *freed; |
| 4348 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4349 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4350 | freed = NULL; |
| 4351 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4352 | /* Only one consumer of llist_del_first() allowed */ |
| 4353 | spin_lock(&i915->mm.free_lock); |
| 4354 | freed = llist_del_first(&i915->mm.free_list); |
| 4355 | spin_unlock(&i915->mm.free_lock); |
| 4356 | } |
| 4357 | if (unlikely(freed)) { |
| 4358 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4359 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4360 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | static void __i915_gem_free_work(struct work_struct *work) |
| 4364 | { |
| 4365 | struct drm_i915_private *i915 = |
| 4366 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4367 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4368 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4369 | /* |
| 4370 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4371 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4372 | * However, the object may also be bound into the global GTT (e.g. |
| 4373 | * older GPUs without per-process support, or for direct access through |
| 4374 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4375 | * unbound now. |
| 4376 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4377 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4378 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4379 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4380 | spin_unlock(&i915->mm.free_lock); |
| 4381 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4382 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4383 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4384 | return; |
| 4385 | |
| 4386 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4387 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4388 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4392 | { |
| 4393 | struct drm_i915_gem_object *obj = |
| 4394 | container_of(head, typeof(*obj), rcu); |
| 4395 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4396 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4397 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4398 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4399 | * it like a rcu_head from this point forwards. And we expect all |
| 4400 | * objects to be freed via this path. |
| 4401 | */ |
| 4402 | destroy_rcu_head(&obj->rcu); |
| 4403 | |
| 4404 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4405 | * Since we require blocking on struct_mutex to unbind the freed |
| 4406 | * object from the GPU before releasing resources back to the |
| 4407 | * system, we can not do that directly from the RCU callback (which may |
| 4408 | * be a softirq context), but must instead then defer that work onto a |
| 4409 | * kthread. We use the RCU callback rather than move the freed object |
| 4410 | * directly onto the work queue so that we can mix between using the |
| 4411 | * worker and performing frees directly from subsequent allocations for |
| 4412 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4413 | */ |
| 4414 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4415 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4419 | { |
| 4420 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4421 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4422 | if (obj->mm.quirked) |
| 4423 | __i915_gem_object_unpin_pages(obj); |
| 4424 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4425 | if (discard_backing_storage(obj)) |
| 4426 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4427 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4428 | /* |
| 4429 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4430 | * read-side critical sections are complete, e.g. |
| 4431 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4432 | * lookup see i915_gem_object_lookup_rcu(). |
| 4433 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4434 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4435 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4436 | } |
| 4437 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4438 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4439 | { |
| 4440 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4441 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4442 | if (!i915_gem_object_has_active_reference(obj) && |
| 4443 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4444 | i915_gem_object_set_active_reference(obj); |
| 4445 | else |
| 4446 | i915_gem_object_put(obj); |
| 4447 | } |
| 4448 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4449 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4450 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4451 | intel_wakeref_t wakeref; |
| 4452 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4453 | GEM_TRACE("\n"); |
| 4454 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4455 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4456 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 4457 | |
| 4458 | /* |
| 4459 | * As we have just resumed the machine and woken the device up from |
| 4460 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4461 | * back to defaults, recovering from whatever wedged state we left it |
| 4462 | * in and so worth trying to use the device once more. |
| 4463 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4464 | if (i915_terminally_wedged(i915)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4465 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4466 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4467 | /* |
| 4468 | * If we inherit context state from the BIOS or earlier occupants |
| 4469 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4470 | * try to take over. The only way to remove the earlier state |
| 4471 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4472 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4473 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4474 | */ |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 4475 | intel_engines_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4476 | |
| 4477 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4478 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4479 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4480 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4481 | i915_gem_contexts_lost(i915); |
| 4482 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4485 | int i915_gem_suspend(struct drm_i915_private *i915) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4486 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4487 | intel_wakeref_t wakeref; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4488 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4489 | |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 4490 | GEM_TRACE("\n"); |
| 4491 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4492 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4493 | intel_suspend_gt_powersave(i915); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4494 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4495 | flush_workqueue(i915->wq); |
| 4496 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4497 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4498 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4499 | /* |
| 4500 | * We have to flush all the executing contexts to main memory so |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4501 | * that they can saved in the hibernation image. To ensure the last |
| 4502 | * context image is coherent, we have to switch away from it. That |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4503 | * leaves the i915->kernel_context still active when |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4504 | * we actually suspend, and its image in memory may not match the GPU |
| 4505 | * state. Fortunately, the kernel_context is disposable and we do |
| 4506 | * not rely on its state. |
| 4507 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4508 | if (!i915_reset_failed(i915)) { |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4509 | ret = i915_gem_switch_to_kernel_context(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4510 | if (ret) |
| 4511 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4512 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4513 | ret = i915_gem_wait_for_idle(i915, |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4514 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 4515 | I915_WAIT_LOCKED | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4516 | I915_WAIT_FOR_IDLE_BOOST, |
| 4517 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4518 | if (ret && ret != -EIO) |
| 4519 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4520 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4521 | assert_kernel_context_is_current(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4522 | } |
Chris Wilson | 01f8f33 | 2018-07-17 09:41:21 +0100 | [diff] [blame] | 4523 | i915_retire_requests(i915); /* ensure we flush after wedging */ |
| 4524 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4525 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4526 | i915_reset_flush(i915); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4527 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4528 | drain_delayed_work(&i915->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4529 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4530 | /* |
| 4531 | * 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] | 4532 | * repeat the flush until it is definitely idle. |
| 4533 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4534 | drain_delayed_work(&i915->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4535 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4536 | intel_uc_suspend(i915); |
| 4537 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4538 | /* |
| 4539 | * Assert that we successfully flushed all the work and |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4540 | * reset the GPU back to its idle, low power state. |
| 4541 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4542 | WARN_ON(i915->gt.awake); |
| 4543 | if (WARN_ON(!intel_engines_are_idle(i915))) |
| 4544 | i915_gem_set_wedged(i915); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4545 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4546 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4547 | return 0; |
| 4548 | |
| 4549 | err_unlock: |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4550 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4551 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4552 | return ret; |
| 4553 | } |
| 4554 | |
| 4555 | void i915_gem_suspend_late(struct drm_i915_private *i915) |
| 4556 | { |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4557 | struct drm_i915_gem_object *obj; |
| 4558 | struct list_head *phases[] = { |
| 4559 | &i915->mm.unbound_list, |
| 4560 | &i915->mm.bound_list, |
| 4561 | NULL |
| 4562 | }, **phase; |
| 4563 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4564 | /* |
| 4565 | * Neither the BIOS, ourselves or any other kernel |
| 4566 | * expects the system to be in execlists mode on startup, |
| 4567 | * so we need to reset the GPU back to legacy mode. And the only |
| 4568 | * known way to disable logical contexts is through a GPU reset. |
| 4569 | * |
| 4570 | * So in order to leave the system in a known default configuration, |
| 4571 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4572 | * clean up the GEM state tracking, flushing off the requests and |
| 4573 | * leaving the system in a known idle state. |
| 4574 | * |
| 4575 | * Note that is of the upmost importance that the GPU is idle and |
| 4576 | * all stray writes are flushed *before* we dismantle the backing |
| 4577 | * storage for the pinned objects. |
| 4578 | * |
| 4579 | * However, since we are uncertain that resetting the GPU on older |
| 4580 | * machines is a good idea, we don't - just in case it leaves the |
| 4581 | * machine in an unusable condition. |
| 4582 | */ |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4583 | |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4584 | mutex_lock(&i915->drm.struct_mutex); |
| 4585 | for (phase = phases; *phase; phase++) { |
| 4586 | list_for_each_entry(obj, *phase, mm.link) |
| 4587 | WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); |
| 4588 | } |
| 4589 | mutex_unlock(&i915->drm.struct_mutex); |
| 4590 | |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4591 | intel_uc_sanitize(i915); |
| 4592 | i915_gem_sanitize(i915); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4593 | } |
| 4594 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4595 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4596 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4597 | GEM_TRACE("\n"); |
| 4598 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4599 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4600 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4601 | mutex_lock(&i915->drm.struct_mutex); |
| 4602 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4603 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4604 | i915_gem_restore_gtt_mappings(i915); |
| 4605 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4606 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4607 | /* |
| 4608 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4609 | * guarantee that the context image is complete. So let's just reset |
| 4610 | * it and start again. |
| 4611 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4612 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4613 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4614 | if (i915_gem_init_hw(i915)) |
| 4615 | goto err_wedged; |
| 4616 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 4617 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 4618 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4619 | /* Always reload a context for powersaving. */ |
| 4620 | if (i915_gem_switch_to_kernel_context(i915)) |
| 4621 | goto err_wedged; |
| 4622 | |
| 4623 | out_unlock: |
| 4624 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 4625 | mutex_unlock(&i915->drm.struct_mutex); |
| 4626 | return; |
| 4627 | |
| 4628 | err_wedged: |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4629 | if (!i915_reset_failed(i915)) { |
| 4630 | dev_err(i915->drm.dev, |
| 4631 | "Failed to re-initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4632 | i915_gem_set_wedged(i915); |
| 4633 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4634 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4635 | } |
| 4636 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4637 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4638 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4639 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4640 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4641 | return; |
| 4642 | |
| 4643 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4644 | DISP_TILE_SURFACE_SWIZZLING); |
| 4645 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4646 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4647 | return; |
| 4648 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4649 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4650 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4651 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4652 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4653 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4654 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4655 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4656 | else |
| 4657 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4658 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4659 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4660 | 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] | 4661 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4662 | I915_WRITE(RING_CTL(base), 0); |
| 4663 | I915_WRITE(RING_HEAD(base), 0); |
| 4664 | I915_WRITE(RING_TAIL(base), 0); |
| 4665 | I915_WRITE(RING_START(base), 0); |
| 4666 | } |
| 4667 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4668 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4669 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4670 | if (IS_I830(dev_priv)) { |
| 4671 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4672 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4673 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4674 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4675 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4676 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4677 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4678 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4679 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4680 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4681 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4685 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4686 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4687 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4688 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4689 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4690 | int err; |
| 4691 | |
| 4692 | for_each_engine(engine, i915, id) { |
| 4693 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4694 | if (err) { |
| 4695 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 4696 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4697 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4698 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | return 0; |
| 4702 | } |
| 4703 | |
| 4704 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4705 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4706 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4707 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4708 | dev_priv->gt.last_init_time = ktime_get(); |
| 4709 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4710 | /* Double layer security blanket, see i915_gem_init() */ |
| 4711 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 4712 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4713 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4714 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4715 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4716 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4717 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4718 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4719 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4720 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4721 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4722 | /* ...and determine whether they are sticking. */ |
| 4723 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 4724 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4725 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4726 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4727 | /* |
| 4728 | * At least 830 can leave some of the unused rings |
| 4729 | * "active" (ie. head != tail) after resume which |
| 4730 | * will prevent c3 entry. Makes sure all unused rings |
| 4731 | * are totally idle. |
| 4732 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4733 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4734 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4735 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4736 | ret = i915_terminally_wedged(dev_priv); |
| 4737 | if (ret) |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4738 | goto out; |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4739 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4740 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4741 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4742 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4743 | goto out; |
| 4744 | } |
| 4745 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 4746 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 4747 | if (ret) { |
| 4748 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 4749 | goto out; |
| 4750 | } |
| 4751 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4752 | /* We can't enable contexts until all firmware is loaded */ |
| 4753 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4754 | if (ret) { |
| 4755 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4756 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4757 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4758 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4759 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 4760 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 4761 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 4762 | ret = __i915_gem_restart_engines(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4763 | if (ret) |
| 4764 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4765 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4766 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4767 | |
| 4768 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4769 | |
| 4770 | cleanup_uc: |
| 4771 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4772 | out: |
| 4773 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 4774 | |
| 4775 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4776 | } |
| 4777 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4778 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 4779 | { |
| 4780 | struct i915_gem_context *ctx; |
| 4781 | struct intel_engine_cs *engine; |
| 4782 | enum intel_engine_id id; |
| 4783 | int err; |
| 4784 | |
| 4785 | /* |
| 4786 | * As we reset the gpu during very early sanitisation, the current |
| 4787 | * register state on the GPU should reflect its defaults values. |
| 4788 | * We load a context onto the hw (with restore-inhibit), then switch |
| 4789 | * over to a second context to save that default register state. We |
| 4790 | * can then prime every new context with that state so they all start |
| 4791 | * from the same default HW values. |
| 4792 | */ |
| 4793 | |
| 4794 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 4795 | if (IS_ERR(ctx)) |
| 4796 | return PTR_ERR(ctx); |
| 4797 | |
| 4798 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4799 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4800 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4801 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4802 | if (IS_ERR(rq)) { |
| 4803 | err = PTR_ERR(rq); |
| 4804 | goto out_ctx; |
| 4805 | } |
| 4806 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 4807 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4808 | if (engine->init_context) |
| 4809 | err = engine->init_context(rq); |
| 4810 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 4811 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4812 | if (err) |
| 4813 | goto err_active; |
| 4814 | } |
| 4815 | |
| 4816 | err = i915_gem_switch_to_kernel_context(i915); |
| 4817 | if (err) |
| 4818 | goto err_active; |
| 4819 | |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4820 | if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) { |
| 4821 | i915_gem_set_wedged(i915); |
| 4822 | err = -EIO; /* Caller will declare us wedged */ |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4823 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4824 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4825 | |
| 4826 | assert_kernel_context_is_current(i915); |
| 4827 | |
Chris Wilson | 8e1cb32 | 2018-09-20 17:13:43 +0100 | [diff] [blame] | 4828 | /* |
| 4829 | * Immediately park the GPU so that we enable powersaving and |
| 4830 | * treat it as idle. The next time we issue a request, we will |
| 4831 | * unpark and start using the engine->pinned_default_state, otherwise |
| 4832 | * it is in limbo and an early reset may fail. |
| 4833 | */ |
| 4834 | __i915_gem_park(i915); |
| 4835 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4836 | for_each_engine(engine, i915, id) { |
| 4837 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4838 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4839 | |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4840 | GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count); |
| 4841 | |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 4842 | state = to_intel_context(ctx, engine)->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4843 | if (!state) |
| 4844 | continue; |
| 4845 | |
| 4846 | /* |
| 4847 | * As we will hold a reference to the logical state, it will |
| 4848 | * not be torn down with the context, and importantly the |
| 4849 | * object will hold onto its vma (making it possible for a |
| 4850 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 4851 | * from the GTT to prevent such accidents and reclaim the |
| 4852 | * space. |
| 4853 | */ |
| 4854 | err = i915_vma_unbind(state); |
| 4855 | if (err) |
| 4856 | goto err_active; |
| 4857 | |
| 4858 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 4859 | if (err) |
| 4860 | goto err_active; |
| 4861 | |
| 4862 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4863 | |
| 4864 | /* Check we can acquire the image of the context state */ |
| 4865 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4866 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4867 | if (IS_ERR(vaddr)) { |
| 4868 | err = PTR_ERR(vaddr); |
| 4869 | goto err_active; |
| 4870 | } |
| 4871 | |
| 4872 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 4876 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 4877 | |
| 4878 | /* |
| 4879 | * Make sure that classes with multiple engine instances all |
| 4880 | * share the same basic configuration. |
| 4881 | */ |
| 4882 | for_each_engine(engine, i915, id) { |
| 4883 | unsigned int bit = BIT(engine->uabi_class); |
| 4884 | unsigned int expected = engine->default_state ? bit : 0; |
| 4885 | |
| 4886 | if ((found & bit) != expected) { |
| 4887 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 4888 | engine->uabi_class, engine->name); |
| 4889 | } |
| 4890 | } |
| 4891 | } |
| 4892 | |
| 4893 | out_ctx: |
| 4894 | i915_gem_context_set_closed(ctx); |
| 4895 | i915_gem_context_put(ctx); |
| 4896 | return err; |
| 4897 | |
| 4898 | err_active: |
| 4899 | /* |
| 4900 | * If we have to abandon now, we expect the engines to be idle |
| 4901 | * and ready to be torn-down. First try to flush any remaining |
| 4902 | * request, ensure we are pointing at the kernel context and |
| 4903 | * then remove it. |
| 4904 | */ |
| 4905 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 4906 | goto out_ctx; |
| 4907 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4908 | if (WARN_ON(i915_gem_wait_for_idle(i915, |
| 4909 | I915_WAIT_LOCKED, |
| 4910 | MAX_SCHEDULE_TIMEOUT))) |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4911 | goto out_ctx; |
| 4912 | |
| 4913 | i915_gem_contexts_lost(i915); |
| 4914 | goto out_ctx; |
| 4915 | } |
| 4916 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4917 | static int |
| 4918 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 4919 | { |
| 4920 | struct drm_i915_gem_object *obj; |
| 4921 | struct i915_vma *vma; |
| 4922 | int ret; |
| 4923 | |
| 4924 | obj = i915_gem_object_create_stolen(i915, size); |
| 4925 | if (!obj) |
| 4926 | obj = i915_gem_object_create_internal(i915, size); |
| 4927 | if (IS_ERR(obj)) { |
| 4928 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 4929 | return PTR_ERR(obj); |
| 4930 | } |
| 4931 | |
| 4932 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 4933 | if (IS_ERR(vma)) { |
| 4934 | ret = PTR_ERR(vma); |
| 4935 | goto err_unref; |
| 4936 | } |
| 4937 | |
| 4938 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 4939 | if (ret) |
| 4940 | goto err_unref; |
| 4941 | |
| 4942 | i915->gt.scratch = vma; |
| 4943 | return 0; |
| 4944 | |
| 4945 | err_unref: |
| 4946 | i915_gem_object_put(obj); |
| 4947 | return ret; |
| 4948 | } |
| 4949 | |
| 4950 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 4951 | { |
| 4952 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 4953 | } |
| 4954 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4955 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4956 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4957 | int ret; |
| 4958 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 4959 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 4960 | 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] | 4961 | mkwrite_device_info(dev_priv)->page_sizes = |
| 4962 | I915_GTT_PAGE_SIZE_4K; |
| 4963 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 4964 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4965 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4966 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 4967 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 4968 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4969 | } else { |
| 4970 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 4971 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 4972 | } |
| 4973 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4974 | i915_timelines_init(dev_priv); |
| 4975 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4976 | ret = i915_gem_init_userptr(dev_priv); |
| 4977 | if (ret) |
| 4978 | return ret; |
| 4979 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4980 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 4981 | if (ret) |
| 4982 | return ret; |
| 4983 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4984 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 4985 | if (ret) |
| 4986 | goto err_uc_misc; |
| 4987 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4988 | /* This is just a security blanket to placate dragons. |
| 4989 | * On some systems, we very sporadically observe that the first TLBs |
| 4990 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 4991 | * we hold the forcewake during initialisation these problems |
| 4992 | * just magically go away. |
| 4993 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4994 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4995 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 4996 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 4997 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4998 | if (ret) { |
| 4999 | GEM_BUG_ON(ret == -EIO); |
| 5000 | goto err_unlock; |
| 5001 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5002 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5003 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5004 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5005 | if (ret) { |
| 5006 | GEM_BUG_ON(ret == -EIO); |
| 5007 | goto err_ggtt; |
| 5008 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5009 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5010 | ret = i915_gem_contexts_init(dev_priv); |
| 5011 | if (ret) { |
| 5012 | GEM_BUG_ON(ret == -EIO); |
| 5013 | goto err_scratch; |
| 5014 | } |
| 5015 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5016 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5017 | if (ret) { |
| 5018 | GEM_BUG_ON(ret == -EIO); |
| 5019 | goto err_context; |
| 5020 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5021 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5022 | intel_init_gt_powersave(dev_priv); |
| 5023 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5024 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5025 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5026 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5027 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5028 | ret = i915_gem_init_hw(dev_priv); |
| 5029 | if (ret) |
| 5030 | goto err_uc_init; |
| 5031 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5032 | /* |
| 5033 | * Despite its name intel_init_clock_gating applies both display |
| 5034 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5035 | * GT power context workaround. Worse, sometimes it includes a context |
| 5036 | * register workaround which we need to apply before we record the |
| 5037 | * default HW state for all contexts. |
| 5038 | * |
| 5039 | * FIXME: break up the workarounds and apply them at the right time! |
| 5040 | */ |
| 5041 | intel_init_clock_gating(dev_priv); |
| 5042 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5043 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5044 | if (ret) |
| 5045 | goto err_init_hw; |
| 5046 | |
| 5047 | if (i915_inject_load_failure()) { |
| 5048 | ret = -ENODEV; |
| 5049 | goto err_init_hw; |
| 5050 | } |
| 5051 | |
| 5052 | if (i915_inject_load_failure()) { |
| 5053 | ret = -EIO; |
| 5054 | goto err_init_hw; |
| 5055 | } |
| 5056 | |
| 5057 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5058 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5059 | |
| 5060 | return 0; |
| 5061 | |
| 5062 | /* |
| 5063 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5064 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5065 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5066 | * driver doesn't explode during runtime. |
| 5067 | */ |
| 5068 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5069 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5070 | |
| 5071 | WARN_ON(i915_gem_suspend(dev_priv)); |
| 5072 | i915_gem_suspend_late(dev_priv); |
| 5073 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 5074 | i915_gem_drain_workqueue(dev_priv); |
| 5075 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5076 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5077 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5078 | err_uc_init: |
| 5079 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5080 | err_pm: |
| 5081 | if (ret != -EIO) { |
| 5082 | intel_cleanup_gt_powersave(dev_priv); |
| 5083 | i915_gem_cleanup_engines(dev_priv); |
| 5084 | } |
| 5085 | err_context: |
| 5086 | if (ret != -EIO) |
| 5087 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5088 | err_scratch: |
| 5089 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5090 | err_ggtt: |
| 5091 | err_unlock: |
| 5092 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5093 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5094 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5095 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5096 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5097 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5098 | if (ret != -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5099 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5100 | i915_timelines_fini(dev_priv); |
| 5101 | } |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5102 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5103 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5104 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5105 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5106 | /* |
| 5107 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5108 | * wedged. But we only want to do this where the GPU is angry, |
| 5109 | * for all other failure, such as an allocation failure, bail. |
| 5110 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 5111 | if (!i915_reset_failed(dev_priv)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 5112 | i915_load_error(dev_priv, |
| 5113 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5114 | i915_gem_set_wedged(dev_priv); |
| 5115 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5116 | |
| 5117 | /* Minimal basic recovery for KMS */ |
| 5118 | ret = i915_ggtt_enable_hw(dev_priv); |
| 5119 | i915_gem_restore_gtt_mappings(dev_priv); |
| 5120 | i915_gem_restore_fences(dev_priv); |
| 5121 | intel_init_clock_gating(dev_priv); |
| 5122 | |
| 5123 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5124 | } |
| 5125 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5126 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5127 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5128 | } |
| 5129 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5130 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 5131 | { |
| 5132 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5133 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5134 | |
| 5135 | /* Flush any outstanding unpin_work. */ |
| 5136 | i915_gem_drain_workqueue(dev_priv); |
| 5137 | |
| 5138 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5139 | intel_uc_fini_hw(dev_priv); |
| 5140 | intel_uc_fini(dev_priv); |
| 5141 | i915_gem_cleanup_engines(dev_priv); |
| 5142 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5143 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5144 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5145 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5146 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 5147 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5148 | intel_cleanup_gt_powersave(dev_priv); |
| 5149 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5150 | intel_uc_fini_misc(dev_priv); |
| 5151 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5152 | i915_timelines_fini(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5153 | |
| 5154 | i915_gem_drain_freed_objects(dev_priv); |
| 5155 | |
| 5156 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 5157 | } |
| 5158 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5159 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5160 | { |
| 5161 | i915_gem_sanitize(i915); |
| 5162 | } |
| 5163 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5164 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5165 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5166 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5167 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5168 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5169 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5170 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5171 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5172 | } |
| 5173 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5174 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5175 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5176 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5177 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5178 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5179 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5180 | !IS_CHERRYVIEW(dev_priv)) |
| 5181 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5182 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5183 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5184 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5185 | dev_priv->num_fence_regs = 16; |
| 5186 | else |
| 5187 | dev_priv->num_fence_regs = 8; |
| 5188 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5189 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5190 | dev_priv->num_fence_regs = |
| 5191 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5192 | |
| 5193 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5194 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5195 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5196 | |
| 5197 | fence->i915 = dev_priv; |
| 5198 | fence->id = i; |
| 5199 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5200 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5201 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5202 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5203 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5204 | } |
| 5205 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5206 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5207 | { |
| 5208 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5209 | spin_lock_init(&i915->mm.obj_lock); |
| 5210 | spin_lock_init(&i915->mm.free_lock); |
| 5211 | |
| 5212 | init_llist_head(&i915->mm.free_list); |
| 5213 | |
| 5214 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5215 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5216 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5217 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5218 | |
| 5219 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5220 | } |
| 5221 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5222 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5223 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5224 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5225 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5226 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5227 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5228 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5229 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5230 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5231 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5232 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5233 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5234 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5235 | if (!dev_priv->luts) |
| 5236 | goto err_vmas; |
| 5237 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5238 | dev_priv->requests = KMEM_CACHE(i915_request, |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5239 | SLAB_HWCACHE_ALIGN | |
| 5240 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5241 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5242 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5243 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5244 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5245 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5246 | SLAB_HWCACHE_ALIGN | |
| 5247 | SLAB_RECLAIM_ACCOUNT); |
| 5248 | if (!dev_priv->dependencies) |
| 5249 | goto err_requests; |
| 5250 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5251 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5252 | if (!dev_priv->priorities) |
| 5253 | goto err_dependencies; |
| 5254 | |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5255 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 5256 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5257 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5258 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5259 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5260 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5261 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5262 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5263 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5264 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5265 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 5266 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 5267 | init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5268 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5269 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5270 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5271 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5272 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5273 | err = i915_gemfs_init(dev_priv); |
| 5274 | if (err) |
| 5275 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5276 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5277 | return 0; |
| 5278 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5279 | err_dependencies: |
| 5280 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5281 | err_requests: |
| 5282 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5283 | err_luts: |
| 5284 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5285 | err_vmas: |
| 5286 | kmem_cache_destroy(dev_priv->vmas); |
| 5287 | err_objects: |
| 5288 | kmem_cache_destroy(dev_priv->objects); |
| 5289 | err_out: |
| 5290 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5291 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5292 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5293 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5294 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5295 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5296 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5297 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5298 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5299 | |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 5300 | cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
| 5301 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5302 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5303 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5304 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5305 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5306 | kmem_cache_destroy(dev_priv->vmas); |
| 5307 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5308 | |
| 5309 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5310 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5311 | |
| 5312 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5313 | } |
| 5314 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5315 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5316 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5317 | /* Discard all purgeable objects, let userspace recover those as |
| 5318 | * required after resuming. |
| 5319 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5320 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5321 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5322 | return 0; |
| 5323 | } |
| 5324 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5325 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5326 | { |
| 5327 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5328 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5329 | &i915->mm.unbound_list, |
| 5330 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5331 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5332 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5333 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5334 | /* |
| 5335 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5336 | * |
| 5337 | * We need to update the domain tracking to reflect that the CPU |
| 5338 | * will be accessing all the pages to create and restore from the |
| 5339 | * hibernation, and so upon restoration those pages will be in the |
| 5340 | * CPU domain. |
| 5341 | * |
| 5342 | * To make sure the hibernation image contains the latest state, |
| 5343 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5344 | * |
| 5345 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5346 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5347 | */ |
| 5348 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5349 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 5350 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5351 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5352 | mutex_lock(&i915->drm.struct_mutex); |
| 5353 | for (phase = phases; *phase; phase++) { |
| 5354 | list_for_each_entry(obj, *phase, mm.link) |
| 5355 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5356 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5357 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5358 | |
| 5359 | return 0; |
| 5360 | } |
| 5361 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5362 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5363 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5364 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5365 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5366 | |
| 5367 | /* Clean up our request list when the client is going away, so that |
| 5368 | * later retire_requests won't dereference our soon-to-be-gone |
| 5369 | * file_priv. |
| 5370 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5371 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5372 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5373 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5374 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5375 | } |
| 5376 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5377 | 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] | 5378 | { |
| 5379 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5380 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5381 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5382 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5383 | |
| 5384 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5385 | if (!file_priv) |
| 5386 | return -ENOMEM; |
| 5387 | |
| 5388 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5389 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5390 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5391 | |
| 5392 | spin_lock_init(&file_priv->mm.lock); |
| 5393 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5394 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5395 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 5396 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5397 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5398 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5399 | if (ret) |
| 5400 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5401 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5402 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5403 | } |
| 5404 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5405 | /** |
| 5406 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5407 | * @old: current GEM buffer for the frontbuffer slots |
| 5408 | * @new: new GEM buffer for the frontbuffer slots |
| 5409 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5410 | * |
| 5411 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5412 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5413 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5414 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5415 | struct drm_i915_gem_object *new, |
| 5416 | unsigned frontbuffer_bits) |
| 5417 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5418 | /* Control of individual bits within the mask are guarded by |
| 5419 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5420 | * manipulation of individual bits. But since the bitfield as a whole |
| 5421 | * is updated using RMW, we need to use atomics in order to update |
| 5422 | * the bits. |
| 5423 | */ |
| 5424 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 5425 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5426 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5427 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5428 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5429 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5430 | } |
| 5431 | |
| 5432 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5433 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5434 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5435 | } |
| 5436 | } |
| 5437 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5438 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5439 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5440 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5441 | const void *data, size_t size) |
| 5442 | { |
| 5443 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5444 | struct file *file; |
| 5445 | size_t offset; |
| 5446 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5447 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5448 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5449 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5450 | return obj; |
| 5451 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5452 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5453 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5454 | file = obj->base.filp; |
| 5455 | offset = 0; |
| 5456 | do { |
| 5457 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5458 | struct page *page; |
| 5459 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5460 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5461 | err = pagecache_write_begin(file, file->f_mapping, |
| 5462 | offset, len, 0, |
| 5463 | &page, &pgdata); |
| 5464 | if (err < 0) |
| 5465 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5466 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5467 | vaddr = kmap(page); |
| 5468 | memcpy(vaddr, data, len); |
| 5469 | kunmap(page); |
| 5470 | |
| 5471 | err = pagecache_write_end(file, file->f_mapping, |
| 5472 | offset, len, len, |
| 5473 | page, pgdata); |
| 5474 | if (err < 0) |
| 5475 | goto fail; |
| 5476 | |
| 5477 | size -= len; |
| 5478 | data += len; |
| 5479 | offset += len; |
| 5480 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5481 | |
| 5482 | return obj; |
| 5483 | |
| 5484 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5485 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5486 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5487 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5488 | |
| 5489 | struct scatterlist * |
| 5490 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5491 | unsigned int n, |
| 5492 | unsigned int *offset) |
| 5493 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5494 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5495 | struct scatterlist *sg; |
| 5496 | unsigned int idx, count; |
| 5497 | |
| 5498 | might_sleep(); |
| 5499 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5500 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5501 | |
| 5502 | /* As we iterate forward through the sg, we record each entry in a |
| 5503 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5504 | * this index previously, we will have an entry for it. |
| 5505 | * |
| 5506 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5507 | * sequential page access (where each new request is consecutive |
| 5508 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5509 | * i.e. O(1) with a large constant! |
| 5510 | */ |
| 5511 | if (n < READ_ONCE(iter->sg_idx)) |
| 5512 | goto lookup; |
| 5513 | |
| 5514 | mutex_lock(&iter->lock); |
| 5515 | |
| 5516 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5517 | * (or the subsequent) sg are fast - comparing against the last |
| 5518 | * sg is faster than going through the radixtree. |
| 5519 | */ |
| 5520 | |
| 5521 | sg = iter->sg_pos; |
| 5522 | idx = iter->sg_idx; |
| 5523 | count = __sg_page_count(sg); |
| 5524 | |
| 5525 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5526 | void *entry; |
| 5527 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5528 | int ret; |
| 5529 | |
| 5530 | /* If we cannot allocate and insert this entry, or the |
| 5531 | * individual pages from this range, cancel updating the |
| 5532 | * sg_idx so that on this lookup we are forced to linearly |
| 5533 | * scan onwards, but on future lookups we will try the |
| 5534 | * insertion again (in which case we need to be careful of |
| 5535 | * the error return reporting that we have already inserted |
| 5536 | * this index). |
| 5537 | */ |
| 5538 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5539 | if (ret && ret != -EEXIST) |
| 5540 | goto scan; |
| 5541 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5542 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5543 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5544 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5545 | if (ret && ret != -EEXIST) |
| 5546 | goto scan; |
| 5547 | } |
| 5548 | |
| 5549 | idx += count; |
| 5550 | sg = ____sg_next(sg); |
| 5551 | count = __sg_page_count(sg); |
| 5552 | } |
| 5553 | |
| 5554 | scan: |
| 5555 | iter->sg_pos = sg; |
| 5556 | iter->sg_idx = idx; |
| 5557 | |
| 5558 | mutex_unlock(&iter->lock); |
| 5559 | |
| 5560 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5561 | goto lookup; |
| 5562 | |
| 5563 | /* In case we failed to insert the entry into the radixtree, we need |
| 5564 | * to look beyond the current sg. |
| 5565 | */ |
| 5566 | while (idx + count <= n) { |
| 5567 | idx += count; |
| 5568 | sg = ____sg_next(sg); |
| 5569 | count = __sg_page_count(sg); |
| 5570 | } |
| 5571 | |
| 5572 | *offset = n - idx; |
| 5573 | return sg; |
| 5574 | |
| 5575 | lookup: |
| 5576 | rcu_read_lock(); |
| 5577 | |
| 5578 | sg = radix_tree_lookup(&iter->radix, n); |
| 5579 | GEM_BUG_ON(!sg); |
| 5580 | |
| 5581 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5582 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5583 | * to the start of that range. We will return the pointer to |
| 5584 | * the base page and the offset of this page within the |
| 5585 | * sg entry's range. |
| 5586 | */ |
| 5587 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5588 | if (unlikely(xa_is_value(sg))) { |
| 5589 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5590 | |
| 5591 | sg = radix_tree_lookup(&iter->radix, base); |
| 5592 | GEM_BUG_ON(!sg); |
| 5593 | |
| 5594 | *offset = n - base; |
| 5595 | } |
| 5596 | |
| 5597 | rcu_read_unlock(); |
| 5598 | |
| 5599 | return sg; |
| 5600 | } |
| 5601 | |
| 5602 | struct page * |
| 5603 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5604 | { |
| 5605 | struct scatterlist *sg; |
| 5606 | unsigned int offset; |
| 5607 | |
| 5608 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5609 | |
| 5610 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5611 | return nth_page(sg_page(sg), offset); |
| 5612 | } |
| 5613 | |
| 5614 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5615 | struct page * |
| 5616 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5617 | unsigned int n) |
| 5618 | { |
| 5619 | struct page *page; |
| 5620 | |
| 5621 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5622 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5623 | set_page_dirty(page); |
| 5624 | |
| 5625 | return page; |
| 5626 | } |
| 5627 | |
| 5628 | dma_addr_t |
| 5629 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5630 | unsigned long n) |
| 5631 | { |
| 5632 | struct scatterlist *sg; |
| 5633 | unsigned int offset; |
| 5634 | |
| 5635 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5636 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5637 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5638 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5639 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5640 | { |
| 5641 | struct sg_table *pages; |
| 5642 | int err; |
| 5643 | |
| 5644 | if (align > obj->base.size) |
| 5645 | return -EINVAL; |
| 5646 | |
| 5647 | if (obj->ops == &i915_gem_phys_ops) |
| 5648 | return 0; |
| 5649 | |
| 5650 | if (obj->ops != &i915_gem_object_ops) |
| 5651 | return -EINVAL; |
| 5652 | |
| 5653 | err = i915_gem_object_unbind(obj); |
| 5654 | if (err) |
| 5655 | return err; |
| 5656 | |
| 5657 | mutex_lock(&obj->mm.lock); |
| 5658 | |
| 5659 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5660 | err = -EFAULT; |
| 5661 | goto err_unlock; |
| 5662 | } |
| 5663 | |
| 5664 | if (obj->mm.quirked) { |
| 5665 | err = -EFAULT; |
| 5666 | goto err_unlock; |
| 5667 | } |
| 5668 | |
| 5669 | if (obj->mm.mapping) { |
| 5670 | err = -EBUSY; |
| 5671 | goto err_unlock; |
| 5672 | } |
| 5673 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5674 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5675 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5676 | obj->ops = &i915_gem_phys_ops; |
| 5677 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5678 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5679 | if (err) |
| 5680 | goto err_xfer; |
| 5681 | |
| 5682 | /* Perma-pin (until release) the physical set of pages */ |
| 5683 | __i915_gem_object_pin_pages(obj); |
| 5684 | |
| 5685 | if (!IS_ERR_OR_NULL(pages)) |
| 5686 | i915_gem_object_ops.put_pages(obj, pages); |
| 5687 | mutex_unlock(&obj->mm.lock); |
| 5688 | return 0; |
| 5689 | |
| 5690 | err_xfer: |
| 5691 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5692 | if (!IS_ERR_OR_NULL(pages)) { |
| 5693 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 5694 | |
| 5695 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 5696 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5697 | err_unlock: |
| 5698 | mutex_unlock(&obj->mm.lock); |
| 5699 | return err; |
| 5700 | } |
| 5701 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5702 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5703 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5704 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5705 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5706 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5707 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5708 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 5709 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5710 | #endif |