Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
Daniel Vetter | be6a037 | 2015-03-18 10:46:04 +0100 | [diff] [blame] | 2 | * Copyright © 2008-2015 Intel Corporation |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 28 | #include <drm/drm_vma_manager.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 29 | #include <drm/drm_pci.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/i915_drm.h> |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 31 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 32 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 33 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 34 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 36 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 37 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 38 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 39 | #include <linux/dma-buf.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 40 | #include <linux/mman.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 41 | |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 42 | #include "i915_drv.h" |
| 43 | #include "i915_gem_clflush.h" |
| 44 | #include "i915_gemfs.h" |
Chris Wilson | 32eb6bc | 2019-02-28 10:20:33 +0000 | [diff] [blame] | 45 | #include "i915_globals.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 46 | #include "i915_reset.h" |
| 47 | #include "i915_trace.h" |
| 48 | #include "i915_vgpu.h" |
| 49 | |
| 50 | #include "intel_drv.h" |
| 51 | #include "intel_frontbuffer.h" |
| 52 | #include "intel_mocs.h" |
| 53 | #include "intel_workarounds.h" |
| 54 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 55 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 56 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 57 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 58 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 59 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 60 | return false; |
| 61 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 62 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 63 | return true; |
| 64 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 65 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 68 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 69 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 70 | struct drm_mm_node *node, u32 size) |
| 71 | { |
| 72 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 73 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 74 | size, 0, I915_COLOR_UNEVICTABLE, |
| 75 | 0, ggtt->mappable_end, |
| 76 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void |
| 80 | remove_mappable_node(struct drm_mm_node *node) |
| 81 | { |
| 82 | drm_mm_remove_node(node); |
| 83 | } |
| 84 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 85 | /* some bookkeeping */ |
| 86 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, |
Chris Wilson | 3ef7f22 | 2016-10-18 13:02:48 +0100 | [diff] [blame] | 87 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 88 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 89 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 90 | dev_priv->mm.object_count++; |
| 91 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 92 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, |
Chris Wilson | 3ef7f22 | 2016-10-18 13:02:48 +0100 | [diff] [blame] | 96 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 97 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 98 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 99 | dev_priv->mm.object_count--; |
| 100 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 101 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Chris Wilson | d9948a1 | 2019-02-28 10:20:35 +0000 | [diff] [blame] | 104 | static void __i915_gem_park(struct drm_i915_private *i915) |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 105 | { |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 106 | intel_wakeref_t wakeref; |
| 107 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 108 | GEM_TRACE("\n"); |
| 109 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 110 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 111 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 112 | GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 113 | |
| 114 | if (!i915->gt.awake) |
Chris Wilson | d9948a1 | 2019-02-28 10:20:35 +0000 | [diff] [blame] | 115 | return; |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * Be paranoid and flush a concurrent interrupt to make sure |
| 119 | * we don't reactivate any irq tasklets after parking. |
| 120 | * |
| 121 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 122 | * there may still be an in-flight interrupt even though the CSB |
| 123 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 124 | * is completed before we continue, but it doesn't prevent the HW from |
| 125 | * raising a spurious interrupt later. To complete the shield we should |
| 126 | * coordinate disabling the CS irq with flushing the interrupts. |
| 127 | */ |
| 128 | synchronize_irq(i915->drm.irq); |
| 129 | |
| 130 | intel_engines_park(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 131 | i915_timelines_park(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 132 | |
| 133 | i915_pmu_gt_parked(i915); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 134 | i915_vma_parked(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 135 | |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 136 | wakeref = fetch_and_zero(&i915->gt.awake); |
| 137 | GEM_BUG_ON(!wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 138 | |
| 139 | if (INTEL_GEN(i915) >= 6) |
| 140 | gen6_rps_idle(i915); |
| 141 | |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 142 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 143 | |
Chris Wilson | d9948a1 | 2019-02-28 10:20:35 +0000 | [diff] [blame] | 144 | i915_globals_park(); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void i915_gem_park(struct drm_i915_private *i915) |
| 148 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 149 | GEM_TRACE("\n"); |
| 150 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 151 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 152 | GEM_BUG_ON(i915->gt.active_requests); |
| 153 | |
| 154 | if (!i915->gt.awake) |
| 155 | return; |
| 156 | |
| 157 | /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ |
| 158 | mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); |
| 159 | } |
| 160 | |
| 161 | void i915_gem_unpark(struct drm_i915_private *i915) |
| 162 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 163 | GEM_TRACE("\n"); |
| 164 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 165 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 166 | GEM_BUG_ON(!i915->gt.active_requests); |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 167 | assert_rpm_wakelock_held(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 168 | |
| 169 | if (i915->gt.awake) |
| 170 | return; |
| 171 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 172 | /* |
| 173 | * It seems that the DMC likes to transition between the DC states a lot |
| 174 | * when there are no connected displays (no active power domains) during |
| 175 | * command submission. |
| 176 | * |
| 177 | * This activity has negative impact on the performance of the chip with |
| 178 | * huge latencies observed in the interrupt handler and elsewhere. |
| 179 | * |
| 180 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 181 | * GT activity, preventing any DC state transitions. |
| 182 | */ |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 183 | i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 184 | GEM_BUG_ON(!i915->gt.awake); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 185 | |
Chris Wilson | 32eb6bc | 2019-02-28 10:20:33 +0000 | [diff] [blame] | 186 | i915_globals_unpark(); |
| 187 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 188 | intel_enable_gt_powersave(i915); |
| 189 | i915_update_gfx_val(i915); |
| 190 | if (INTEL_GEN(i915) >= 6) |
| 191 | gen6_rps_busy(i915); |
| 192 | i915_pmu_gt_unparked(i915); |
| 193 | |
| 194 | intel_engines_unpark(i915); |
| 195 | |
| 196 | i915_queue_hangcheck(i915); |
| 197 | |
| 198 | queue_delayed_work(i915->wq, |
| 199 | &i915->gt.retire_work, |
| 200 | round_jiffies_up_relative(HZ)); |
| 201 | } |
| 202 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 203 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 204 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 205 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 206 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 207 | struct i915_ggtt *ggtt = &to_i915(dev)->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 208 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 209 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 210 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 211 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 212 | mutex_lock(&ggtt->vm.mutex); |
| 213 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 214 | pinned = ggtt->vm.reserved; |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 215 | list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 216 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 217 | pinned += vma->node.size; |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 218 | |
| 219 | mutex_unlock(&ggtt->vm.mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 220 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 221 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 222 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 223 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 227 | 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] | 228 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 229 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 230 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 231 | struct sg_table *st; |
| 232 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 233 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 234 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 235 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 236 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 237 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 238 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 239 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 240 | /* Always aligning to the object size, allows a single allocation |
| 241 | * to handle all possible callers, and given typical object sizes, |
| 242 | * the alignment of the buddy allocation will naturally match. |
| 243 | */ |
| 244 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 245 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 246 | roundup_pow_of_two(obj->base.size)); |
| 247 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 248 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 249 | |
| 250 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 251 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 252 | struct page *page; |
| 253 | char *src; |
| 254 | |
| 255 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 256 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 257 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 258 | goto err_phys; |
| 259 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 260 | |
| 261 | src = kmap_atomic(page); |
| 262 | memcpy(vaddr, src, PAGE_SIZE); |
| 263 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 264 | kunmap_atomic(src); |
| 265 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 266 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 267 | vaddr += PAGE_SIZE; |
| 268 | } |
| 269 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 270 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 271 | |
| 272 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 273 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 274 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 275 | goto err_phys; |
| 276 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 277 | |
| 278 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 279 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 280 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 281 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | sg = st->sgl; |
| 285 | sg->offset = 0; |
| 286 | sg->length = obj->base.size; |
| 287 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 288 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 289 | sg_dma_len(sg) = obj->base.size; |
| 290 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 291 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 292 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 293 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 294 | |
| 295 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 296 | |
| 297 | err_phys: |
| 298 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 299 | |
| 300 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 303 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 304 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 305 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 306 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 307 | if (cpu_write_needs_clflush(obj)) |
| 308 | obj->cache_dirty = true; |
| 309 | } |
| 310 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 311 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 312 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 313 | struct sg_table *pages, |
| 314 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 315 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 316 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
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 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 319 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 320 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 321 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 322 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 323 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 324 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 325 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 326 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static void |
| 330 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 331 | struct sg_table *pages) |
| 332 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 333 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 334 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 335 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 336 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 337 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 338 | int i; |
| 339 | |
| 340 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 341 | struct page *page; |
| 342 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 343 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 344 | page = shmem_read_mapping_page(mapping, i); |
| 345 | if (IS_ERR(page)) |
| 346 | continue; |
| 347 | |
| 348 | dst = kmap_atomic(page); |
| 349 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 350 | memcpy(dst, vaddr, PAGE_SIZE); |
| 351 | kunmap_atomic(dst); |
| 352 | |
| 353 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 354 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 355 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 356 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 357 | vaddr += PAGE_SIZE; |
| 358 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 359 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 362 | sg_free_table(pages); |
| 363 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 364 | |
| 365 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static void |
| 369 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 370 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 371 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 375 | .get_pages = i915_gem_object_get_pages_phys, |
| 376 | .put_pages = i915_gem_object_put_pages_phys, |
| 377 | .release = i915_gem_object_release_phys, |
| 378 | }; |
| 379 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 380 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 381 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 382 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 383 | { |
| 384 | struct i915_vma *vma; |
| 385 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 386 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 387 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 388 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 389 | |
| 390 | /* Closed vma are removed from the obj->vma_list - but they may |
| 391 | * still have an active binding on the object. To remove those we |
| 392 | * must wait for all rendering to complete to the object (as unbinding |
| 393 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 394 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 395 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 396 | if (ret) |
| 397 | return ret; |
| 398 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 399 | spin_lock(&obj->vma.lock); |
| 400 | while (!ret && (vma = list_first_entry_or_null(&obj->vma.list, |
| 401 | struct i915_vma, |
| 402 | obj_link))) { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 403 | list_move_tail(&vma->obj_link, &still_in_list); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 404 | spin_unlock(&obj->vma.lock); |
| 405 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 406 | ret = i915_vma_unbind(vma); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 407 | |
| 408 | spin_lock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 409 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 410 | list_splice(&still_in_list, &obj->vma.list); |
| 411 | spin_unlock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 412 | |
| 413 | return ret; |
| 414 | } |
| 415 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 416 | static long |
| 417 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 418 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 419 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 420 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 421 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 422 | |
| 423 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 424 | |
| 425 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 426 | return timeout; |
| 427 | |
| 428 | if (!dma_fence_is_i915(fence)) |
| 429 | return dma_fence_wait_timeout(fence, |
| 430 | flags & I915_WAIT_INTERRUPTIBLE, |
| 431 | timeout); |
| 432 | |
| 433 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 434 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 435 | goto out; |
| 436 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 437 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 438 | |
| 439 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 440 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 441 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 442 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 443 | return timeout; |
| 444 | } |
| 445 | |
| 446 | static long |
| 447 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 448 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 449 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 450 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 451 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 452 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 453 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 454 | |
| 455 | if (flags & I915_WAIT_ALL) { |
| 456 | struct dma_fence **shared; |
| 457 | unsigned int count, i; |
| 458 | int ret; |
| 459 | |
| 460 | ret = reservation_object_get_fences_rcu(resv, |
| 461 | &excl, &count, &shared); |
| 462 | if (ret) |
| 463 | return ret; |
| 464 | |
| 465 | for (i = 0; i < count; i++) { |
| 466 | timeout = i915_gem_object_wait_fence(shared[i], |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 467 | flags, timeout); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 468 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 469 | break; |
| 470 | |
| 471 | dma_fence_put(shared[i]); |
| 472 | } |
| 473 | |
| 474 | for (; i < count; i++) |
| 475 | dma_fence_put(shared[i]); |
| 476 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 477 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 478 | /* |
| 479 | * If both shared fences and an exclusive fence exist, |
| 480 | * then by construction the shared fences must be later |
| 481 | * than the exclusive fence. If we successfully wait for |
| 482 | * all the shared fences, we know that the exclusive fence |
| 483 | * must all be signaled. If all the shared fences are |
| 484 | * signaled, we can prune the array and recover the |
| 485 | * floating references on the fences/requests. |
| 486 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 487 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 488 | } else { |
| 489 | excl = reservation_object_get_excl_rcu(resv); |
| 490 | } |
| 491 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 492 | if (excl && timeout >= 0) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 493 | timeout = i915_gem_object_wait_fence(excl, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 494 | |
| 495 | dma_fence_put(excl); |
| 496 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 497 | /* |
| 498 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 499 | * signaled and that the reservation object has not been changed (i.e. |
| 500 | * no new fences have been added). |
| 501 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 502 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 503 | if (reservation_object_trylock(resv)) { |
| 504 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 505 | reservation_object_add_excl_fence(resv, NULL); |
| 506 | reservation_object_unlock(resv); |
| 507 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 510 | return timeout; |
| 511 | } |
| 512 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 513 | static void __fence_set_priority(struct dma_fence *fence, |
| 514 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 515 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 516 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 517 | struct intel_engine_cs *engine; |
| 518 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 519 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 520 | return; |
| 521 | |
| 522 | rq = to_request(fence); |
| 523 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 524 | |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 525 | local_bh_disable(); |
| 526 | rcu_read_lock(); /* RCU serialisation for set-wedged protection */ |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 527 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 528 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 529 | rcu_read_unlock(); |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 530 | local_bh_enable(); /* kick the tasklets if queues were reprioritised */ |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 533 | static void fence_set_priority(struct dma_fence *fence, |
| 534 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 535 | { |
| 536 | /* Recurse once into a fence-array */ |
| 537 | if (dma_fence_is_array(fence)) { |
| 538 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 539 | int i; |
| 540 | |
| 541 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 542 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 543 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 544 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | int |
| 549 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 550 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 551 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 552 | { |
| 553 | struct dma_fence *excl; |
| 554 | |
| 555 | if (flags & I915_WAIT_ALL) { |
| 556 | struct dma_fence **shared; |
| 557 | unsigned int count, i; |
| 558 | int ret; |
| 559 | |
| 560 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 561 | &excl, &count, &shared); |
| 562 | if (ret) |
| 563 | return ret; |
| 564 | |
| 565 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 566 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 567 | dma_fence_put(shared[i]); |
| 568 | } |
| 569 | |
| 570 | kfree(shared); |
| 571 | } else { |
| 572 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 573 | } |
| 574 | |
| 575 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 576 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 577 | dma_fence_put(excl); |
| 578 | } |
| 579 | return 0; |
| 580 | } |
| 581 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 582 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 583 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 584 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 585 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 586 | * @timeout: how long to wait |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 587 | */ |
| 588 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 589 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 590 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 591 | long timeout) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 592 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 593 | might_sleep(); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 594 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 595 | |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 596 | timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 597 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 600 | static int |
| 601 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 602 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 603 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 604 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 605 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 606 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 607 | |
| 608 | /* We manually control the domain here and pretend that it |
| 609 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 610 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 611 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 612 | if (copy_from_user(vaddr, user_data, args->size)) |
| 613 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 614 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 615 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 616 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 617 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 618 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 619 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 620 | } |
| 621 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 622 | static int |
| 623 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 624 | struct drm_i915_private *dev_priv, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 625 | u64 size, |
| 626 | u32 *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 627 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 628 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 629 | int ret; |
| 630 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 631 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 632 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 633 | if (size == 0) |
| 634 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 635 | |
| 636 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 637 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 638 | if (IS_ERR(obj)) |
| 639 | return PTR_ERR(obj); |
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 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 642 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 643 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 644 | if (ret) |
| 645 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 646 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 647 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 651 | int |
| 652 | i915_gem_dumb_create(struct drm_file *file, |
| 653 | struct drm_device *dev, |
| 654 | struct drm_mode_create_dumb *args) |
| 655 | { |
| 656 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 657 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 658 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 659 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 660 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 661 | } |
| 662 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 663 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 664 | { |
| 665 | return !(obj->cache_level == I915_CACHE_NONE || |
| 666 | obj->cache_level == I915_CACHE_WT); |
| 667 | } |
| 668 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 669 | /** |
| 670 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 671 | * @dev: drm device pointer |
| 672 | * @data: ioctl data blob |
| 673 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 674 | */ |
| 675 | int |
| 676 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 677 | struct drm_file *file) |
| 678 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 679 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 680 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 681 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 682 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 683 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 684 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 685 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 686 | } |
| 687 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 688 | static inline enum fb_op_origin |
| 689 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 690 | { |
| 691 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 692 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 693 | } |
| 694 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 695 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 696 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 697 | intel_wakeref_t wakeref; |
| 698 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 699 | /* |
| 700 | * No actual flushing is required for the GTT write domain for reads |
| 701 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 702 | * as far as we know, so there's no chipset flush. It also doesn't |
| 703 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 704 | * |
| 705 | * However, we do have to enforce the order so that all writes through |
| 706 | * the GTT land before any writes to the device, such as updates to |
| 707 | * the GATT itself. |
| 708 | * |
| 709 | * We also have to wait a bit for the writes to land from the GTT. |
| 710 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 711 | * timing. This issue has only been observed when switching quickly |
| 712 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 713 | * 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] | 714 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 715 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 716 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 717 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 718 | wmb(); |
| 719 | |
| 720 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 721 | return; |
| 722 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 723 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 724 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 725 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 726 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 727 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 728 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 729 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 730 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 731 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | static void |
| 735 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 736 | { |
| 737 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 738 | struct i915_vma *vma; |
| 739 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 740 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 741 | return; |
| 742 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 743 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 744 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 745 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 746 | |
| 747 | intel_fb_obj_flush(obj, |
| 748 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 749 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 750 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 751 | if (vma->iomap) |
| 752 | continue; |
| 753 | |
| 754 | i915_vma_unset_ggtt_write(vma); |
| 755 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 756 | break; |
| 757 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 758 | case I915_GEM_DOMAIN_WC: |
| 759 | wmb(); |
| 760 | break; |
| 761 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 762 | case I915_GEM_DOMAIN_CPU: |
| 763 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 764 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 765 | |
| 766 | case I915_GEM_DOMAIN_RENDER: |
| 767 | if (gpu_write_needs_clflush(obj)) |
| 768 | obj->cache_dirty = true; |
| 769 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 770 | } |
| 771 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 772 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 775 | /* |
| 776 | * Pins the specified object's pages and synchronizes the object with |
| 777 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 778 | * flush the object from the CPU cache. |
| 779 | */ |
| 780 | 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] | 781 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 782 | { |
| 783 | int ret; |
| 784 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 785 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 786 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 787 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 788 | if (!i915_gem_object_has_struct_page(obj)) |
| 789 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 790 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 791 | ret = i915_gem_object_wait(obj, |
| 792 | I915_WAIT_INTERRUPTIBLE | |
| 793 | I915_WAIT_LOCKED, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 794 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 795 | if (ret) |
| 796 | return ret; |
| 797 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 798 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 799 | if (ret) |
| 800 | return ret; |
| 801 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 802 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 803 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 804 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 805 | if (ret) |
| 806 | goto err_unpin; |
| 807 | else |
| 808 | goto out; |
| 809 | } |
| 810 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 811 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 812 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 813 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 814 | * read domain and manually flush cachelines (if required). This |
| 815 | * optimizes for the case when the gpu will dirty the data |
| 816 | * anyway again before the next pread happens. |
| 817 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 818 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 819 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 820 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 821 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 822 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 823 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 824 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 825 | |
| 826 | err_unpin: |
| 827 | i915_gem_object_unpin_pages(obj); |
| 828 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 832 | unsigned int *needs_clflush) |
| 833 | { |
| 834 | int ret; |
| 835 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 836 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 837 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 838 | *needs_clflush = 0; |
| 839 | if (!i915_gem_object_has_struct_page(obj)) |
| 840 | return -ENODEV; |
| 841 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 842 | ret = i915_gem_object_wait(obj, |
| 843 | I915_WAIT_INTERRUPTIBLE | |
| 844 | I915_WAIT_LOCKED | |
| 845 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 846 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 847 | if (ret) |
| 848 | return ret; |
| 849 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 850 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 851 | if (ret) |
| 852 | return ret; |
| 853 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 854 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 855 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 856 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 857 | if (ret) |
| 858 | goto err_unpin; |
| 859 | else |
| 860 | goto out; |
| 861 | } |
| 862 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 863 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 864 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 865 | /* If we're not in the cpu write domain, set ourself into the |
| 866 | * gtt write domain and manually flush cachelines (as required). |
| 867 | * This optimizes for the case when the gpu will use the data |
| 868 | * right away and we therefore have to clflush anyway. |
| 869 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 870 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 871 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 872 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 873 | /* |
| 874 | * Same trick applies to invalidate partially written |
| 875 | * cachelines read before writing. |
| 876 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 877 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 878 | *needs_clflush |= CLFLUSH_BEFORE; |
| 879 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 880 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 881 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 882 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 883 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 884 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 885 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 886 | |
| 887 | err_unpin: |
| 888 | i915_gem_object_unpin_pages(obj); |
| 889 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 892 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 893 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 894 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 895 | { |
| 896 | char *vaddr; |
| 897 | int ret; |
| 898 | |
| 899 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 900 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 901 | if (needs_clflush) |
| 902 | drm_clflush_virt_range(vaddr + offset, len); |
| 903 | |
| 904 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 905 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 906 | kunmap(page); |
| 907 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 908 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | static int |
| 912 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 913 | struct drm_i915_gem_pread *args) |
| 914 | { |
| 915 | char __user *user_data; |
| 916 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 917 | unsigned int needs_clflush; |
| 918 | unsigned int idx, offset; |
| 919 | int ret; |
| 920 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 921 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 922 | if (ret) |
| 923 | return ret; |
| 924 | |
| 925 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 926 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 927 | if (ret) |
| 928 | return ret; |
| 929 | |
| 930 | remain = args->size; |
| 931 | user_data = u64_to_user_ptr(args->data_ptr); |
| 932 | offset = offset_in_page(args->offset); |
| 933 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 934 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 935 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 936 | |
| 937 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 938 | needs_clflush); |
| 939 | if (ret) |
| 940 | break; |
| 941 | |
| 942 | remain -= length; |
| 943 | user_data += length; |
| 944 | offset = 0; |
| 945 | } |
| 946 | |
| 947 | i915_gem_obj_finish_shmem_access(obj); |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | static inline bool |
| 952 | gtt_user_read(struct io_mapping *mapping, |
| 953 | loff_t base, int offset, |
| 954 | char __user *user_data, int length) |
| 955 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 956 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 957 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 958 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 959 | /* 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] | 960 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 961 | unwritten = __copy_to_user_inatomic(user_data, |
| 962 | (void __force *)vaddr + offset, |
| 963 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 964 | io_mapping_unmap_atomic(vaddr); |
| 965 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 966 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 967 | unwritten = copy_to_user(user_data, |
| 968 | (void __force *)vaddr + offset, |
| 969 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 970 | io_mapping_unmap(vaddr); |
| 971 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 972 | return unwritten; |
| 973 | } |
| 974 | |
| 975 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 976 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 977 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 978 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 979 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 980 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 981 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 982 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 983 | struct i915_vma *vma; |
| 984 | void __user *user_data; |
| 985 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 986 | int ret; |
| 987 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 988 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 989 | if (ret) |
| 990 | return ret; |
| 991 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 992 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 993 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 994 | PIN_MAPPABLE | |
| 995 | PIN_NONFAULT | |
| 996 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 997 | if (!IS_ERR(vma)) { |
| 998 | node.start = i915_ggtt_offset(vma); |
| 999 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1000 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1001 | if (ret) { |
| 1002 | i915_vma_unpin(vma); |
| 1003 | vma = ERR_PTR(ret); |
| 1004 | } |
| 1005 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1006 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1007 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1008 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1009 | goto out_unlock; |
| 1010 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1014 | if (ret) |
| 1015 | goto out_unpin; |
| 1016 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1017 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1018 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1019 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1020 | remain = args->size; |
| 1021 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1022 | |
| 1023 | while (remain > 0) { |
| 1024 | /* Operation in this page |
| 1025 | * |
| 1026 | * page_base = page offset within aperture |
| 1027 | * page_offset = offset within page |
| 1028 | * page_length = bytes to copy for this page |
| 1029 | */ |
| 1030 | u32 page_base = node.start; |
| 1031 | unsigned page_offset = offset_in_page(offset); |
| 1032 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1033 | page_length = remain < page_length ? remain : page_length; |
| 1034 | if (node.allocated) { |
| 1035 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1036 | ggtt->vm.insert_page(&ggtt->vm, |
| 1037 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1038 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1039 | wmb(); |
| 1040 | } else { |
| 1041 | page_base += offset & PAGE_MASK; |
| 1042 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1043 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1044 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1045 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1046 | ret = -EFAULT; |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | remain -= page_length; |
| 1051 | user_data += page_length; |
| 1052 | offset += page_length; |
| 1053 | } |
| 1054 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1055 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1056 | out_unpin: |
| 1057 | if (node.allocated) { |
| 1058 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1059 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1060 | remove_mappable_node(&node); |
| 1061 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1062 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1063 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1064 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1065 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1066 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1067 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1068 | return ret; |
| 1069 | } |
| 1070 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1071 | /** |
| 1072 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1073 | * @dev: drm device pointer |
| 1074 | * @data: ioctl data blob |
| 1075 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1076 | * |
| 1077 | * On error, the contents of *data are undefined. |
| 1078 | */ |
| 1079 | int |
| 1080 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1081 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1082 | { |
| 1083 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1084 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1085 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1086 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1087 | if (args->size == 0) |
| 1088 | return 0; |
| 1089 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1090 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1091 | args->size)) |
| 1092 | return -EFAULT; |
| 1093 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1094 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1095 | if (!obj) |
| 1096 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1097 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1098 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1099 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1100 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1101 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1104 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1105 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1106 | ret = i915_gem_object_wait(obj, |
| 1107 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1108 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1109 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1110 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1111 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1112 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1113 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1114 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1115 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1116 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1117 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1118 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1119 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1120 | i915_gem_object_unpin_pages(obj); |
| 1121 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1122 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1123 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1126 | /* This is the fast write path which cannot handle |
| 1127 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1128 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1129 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1130 | static inline bool |
| 1131 | ggtt_write(struct io_mapping *mapping, |
| 1132 | loff_t base, int offset, |
| 1133 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1134 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1135 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1136 | unsigned long unwritten; |
| 1137 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1138 | /* 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] | 1139 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1140 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1141 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1142 | io_mapping_unmap_atomic(vaddr); |
| 1143 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1144 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1145 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1146 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1147 | io_mapping_unmap(vaddr); |
| 1148 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1149 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1150 | return unwritten; |
| 1151 | } |
| 1152 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1153 | /** |
| 1154 | * This is the fast pwrite path, where we copy the data directly from the |
| 1155 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1156 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1157 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1158 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1159 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1160 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1161 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1162 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1163 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1164 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1165 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1166 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1167 | struct i915_vma *vma; |
| 1168 | u64 remain, offset; |
| 1169 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1170 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1171 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1172 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1173 | if (ret) |
| 1174 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1175 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1176 | if (i915_gem_object_has_struct_page(obj)) { |
| 1177 | /* |
| 1178 | * Avoid waking the device up if we can fallback, as |
| 1179 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1180 | * depending on PCI sleeps and our own resume time). |
| 1181 | * This easily dwarfs any performance advantage from |
| 1182 | * using the cache bypass of indirect GGTT access. |
| 1183 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1184 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1185 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1186 | ret = -EFAULT; |
| 1187 | goto out_unlock; |
| 1188 | } |
| 1189 | } else { |
| 1190 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1191 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1194 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1195 | PIN_MAPPABLE | |
| 1196 | PIN_NONFAULT | |
| 1197 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1198 | if (!IS_ERR(vma)) { |
| 1199 | node.start = i915_ggtt_offset(vma); |
| 1200 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1201 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1202 | if (ret) { |
| 1203 | i915_vma_unpin(vma); |
| 1204 | vma = ERR_PTR(ret); |
| 1205 | } |
| 1206 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1207 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1208 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1209 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1210 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1211 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1212 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1213 | |
| 1214 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1215 | if (ret) |
| 1216 | goto out_unpin; |
| 1217 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1218 | mutex_unlock(&i915->drm.struct_mutex); |
| 1219 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1220 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1221 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1222 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1223 | offset = args->offset; |
| 1224 | remain = args->size; |
| 1225 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1226 | /* Operation in this page |
| 1227 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1228 | * page_base = page offset within aperture |
| 1229 | * page_offset = offset within page |
| 1230 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1231 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1232 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1233 | unsigned int page_offset = offset_in_page(offset); |
| 1234 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1235 | page_length = remain < page_length ? remain : page_length; |
| 1236 | if (node.allocated) { |
| 1237 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1238 | ggtt->vm.insert_page(&ggtt->vm, |
| 1239 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1240 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1241 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1242 | } else { |
| 1243 | page_base += offset & PAGE_MASK; |
| 1244 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1245 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1246 | * source page isn't available. Return the error and we'll |
| 1247 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1248 | * If the object is non-shmem backed, we retry again with the |
| 1249 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1250 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1251 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1252 | user_data, page_length)) { |
| 1253 | ret = -EFAULT; |
| 1254 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1255 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1256 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1257 | remain -= page_length; |
| 1258 | user_data += page_length; |
| 1259 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1260 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1261 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1262 | |
| 1263 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1264 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1265 | if (node.allocated) { |
| 1266 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1267 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1268 | remove_mappable_node(&node); |
| 1269 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1270 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1271 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1272 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1273 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1274 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1275 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1276 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1279 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1280 | * Flushes invalid cachelines before writing to the target if |
| 1281 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1282 | * writing if needs_clflush is set. |
| 1283 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1284 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1285 | 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] | 1286 | bool needs_clflush_before, |
| 1287 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1288 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1289 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1290 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1291 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1292 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1293 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1294 | if (needs_clflush_before) |
| 1295 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1296 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1297 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1298 | if (!ret && needs_clflush_after) |
| 1299 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1300 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1301 | kunmap(page); |
| 1302 | |
| 1303 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | static int |
| 1307 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1308 | const struct drm_i915_gem_pwrite *args) |
| 1309 | { |
| 1310 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1311 | void __user *user_data; |
| 1312 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1313 | unsigned int partial_cacheline_write; |
| 1314 | unsigned int needs_clflush; |
| 1315 | unsigned int offset, idx; |
| 1316 | int ret; |
| 1317 | |
| 1318 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1319 | if (ret) |
| 1320 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1321 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1322 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1323 | mutex_unlock(&i915->drm.struct_mutex); |
| 1324 | if (ret) |
| 1325 | return ret; |
| 1326 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1327 | /* If we don't overwrite a cacheline completely we need to be |
| 1328 | * careful to have up-to-date data by first clflushing. Don't |
| 1329 | * overcomplicate things and flush the entire patch. |
| 1330 | */ |
| 1331 | partial_cacheline_write = 0; |
| 1332 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1333 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1334 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1335 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1336 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1337 | offset = offset_in_page(args->offset); |
| 1338 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1339 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1340 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1341 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1342 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1343 | (offset | length) & partial_cacheline_write, |
| 1344 | needs_clflush & CLFLUSH_AFTER); |
| 1345 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1346 | break; |
| 1347 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1348 | remain -= length; |
| 1349 | user_data += length; |
| 1350 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1353 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1354 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1355 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /** |
| 1359 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1360 | * @dev: drm device |
| 1361 | * @data: ioctl data blob |
| 1362 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1363 | * |
| 1364 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1365 | */ |
| 1366 | int |
| 1367 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1368 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1369 | { |
| 1370 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1371 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1372 | int ret; |
| 1373 | |
| 1374 | if (args->size == 0) |
| 1375 | return 0; |
| 1376 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1377 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1378 | return -EFAULT; |
| 1379 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1380 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1381 | if (!obj) |
| 1382 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1383 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1384 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1385 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1386 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1387 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1388 | } |
| 1389 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1390 | /* Writes not allowed into this read-only object */ |
| 1391 | if (i915_gem_object_is_readonly(obj)) { |
| 1392 | ret = -EINVAL; |
| 1393 | goto err; |
| 1394 | } |
| 1395 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1396 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1397 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1398 | ret = -ENODEV; |
| 1399 | if (obj->ops->pwrite) |
| 1400 | ret = obj->ops->pwrite(obj, args); |
| 1401 | if (ret != -ENODEV) |
| 1402 | goto err; |
| 1403 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1404 | ret = i915_gem_object_wait(obj, |
| 1405 | I915_WAIT_INTERRUPTIBLE | |
| 1406 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1407 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1408 | if (ret) |
| 1409 | goto err; |
| 1410 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1411 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1412 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1413 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1414 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1415 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1416 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1417 | * it would end up going through the fenced access, and we'll get |
| 1418 | * different detiling behavior between reading and writing. |
| 1419 | * pread/pwrite currently are reading and writing from the CPU |
| 1420 | * perspective, requiring manual detiling by the client. |
| 1421 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1422 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1423 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1424 | /* Note that the gtt paths might fail with non-page-backed user |
| 1425 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1426 | * textures). Fallback to the shmem path in that case. |
| 1427 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1428 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1429 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1430 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1431 | if (obj->phys_handle) |
| 1432 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1433 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1434 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1435 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1436 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1437 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1438 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1439 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1440 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1443 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1444 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1445 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1446 | struct list_head *list; |
| 1447 | struct i915_vma *vma; |
| 1448 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1449 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1450 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1451 | mutex_lock(&i915->ggtt.vm.mutex); |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1452 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1453 | if (!drm_mm_node_allocated(&vma->node)) |
| 1454 | continue; |
| 1455 | |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 1456 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1457 | } |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1458 | mutex_unlock(&i915->ggtt.vm.mutex); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1459 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1460 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1461 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1462 | list_move_tail(&obj->mm.link, list); |
| 1463 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1466 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1467 | * Called when user space prepares to use an object with the CPU, either |
| 1468 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1469 | * @dev: drm device |
| 1470 | * @data: ioctl data blob |
| 1471 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1472 | */ |
| 1473 | int |
| 1474 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1475 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1476 | { |
| 1477 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1478 | struct drm_i915_gem_object *obj; |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1479 | u32 read_domains = args->read_domains; |
| 1480 | u32 write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1481 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1482 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1483 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1484 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1485 | return -EINVAL; |
| 1486 | |
| 1487 | /* Having something in the write domain implies it's in the read |
| 1488 | * domain, and only that read domain. Enforce that in the request. |
| 1489 | */ |
| 1490 | if (write_domain != 0 && read_domains != write_domain) |
| 1491 | return -EINVAL; |
| 1492 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1493 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1494 | if (!obj) |
| 1495 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1496 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1497 | /* Try to flush the object off the GPU without holding the lock. |
| 1498 | * We will repeat the flush holding the lock in the normal manner |
| 1499 | * to catch cases where we are gazumped. |
| 1500 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1501 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1502 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1503 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1504 | (write_domain ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1505 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1506 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1507 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1508 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1509 | /* |
| 1510 | * Proxy objects do not control access to the backing storage, ergo |
| 1511 | * they cannot be used as a means to manipulate the cache domain |
| 1512 | * tracking for that backing storage. The proxy object is always |
| 1513 | * considered to be outside of any cache domain. |
| 1514 | */ |
| 1515 | if (i915_gem_object_is_proxy(obj)) { |
| 1516 | err = -ENXIO; |
| 1517 | goto out; |
| 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1522 | * direct access in memory with previous cached writes through |
| 1523 | * shmemfs and that our cache domain tracking remains valid. |
| 1524 | * For example, if the obj->filp was moved to swap without us |
| 1525 | * being notified and releasing the pages, we would mistakenly |
| 1526 | * continue to assume that the obj remained out of the CPU cached |
| 1527 | * domain. |
| 1528 | */ |
| 1529 | err = i915_gem_object_pin_pages(obj); |
| 1530 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1531 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1532 | |
| 1533 | err = i915_mutex_lock_interruptible(dev); |
| 1534 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1535 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1536 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1537 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1538 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1539 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1540 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1541 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1542 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1543 | |
| 1544 | /* And bump the LRU for this access */ |
| 1545 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1546 | |
| 1547 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1548 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1549 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1550 | intel_fb_obj_invalidate(obj, |
| 1551 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1552 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1553 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1554 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1555 | out: |
| 1556 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1557 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /** |
| 1561 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1562 | * @dev: drm device |
| 1563 | * @data: ioctl data blob |
| 1564 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1565 | */ |
| 1566 | int |
| 1567 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1568 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1569 | { |
| 1570 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1571 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1572 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1573 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1574 | if (!obj) |
| 1575 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1576 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1577 | /* |
| 1578 | * Proxy objects are barred from CPU access, so there is no |
| 1579 | * need to ban sw_finish as it is a nop. |
| 1580 | */ |
| 1581 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1582 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1583 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1584 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1585 | |
| 1586 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1589 | static inline bool |
| 1590 | __vma_matches(struct vm_area_struct *vma, struct file *filp, |
| 1591 | unsigned long addr, unsigned long size) |
| 1592 | { |
| 1593 | if (vma->vm_file != filp) |
| 1594 | return false; |
| 1595 | |
Tvrtko Ursulin | a90e194 | 2019-03-05 11:04:08 +0000 | [diff] [blame] | 1596 | return vma->vm_start == addr && |
| 1597 | (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1600 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1601 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1602 | * it is mapped to. |
| 1603 | * @dev: drm device |
| 1604 | * @data: ioctl data blob |
| 1605 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1606 | * |
| 1607 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1608 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1609 | * |
| 1610 | * IMPORTANT: |
| 1611 | * |
| 1612 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1613 | * mmap support, please don't implement mmap support like here. The modern way |
| 1614 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1615 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1616 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1617 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1618 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1619 | */ |
| 1620 | int |
| 1621 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1622 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1623 | { |
| 1624 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1625 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1626 | unsigned long addr; |
| 1627 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1628 | if (args->flags & ~(I915_MMAP_WC)) |
| 1629 | return -EINVAL; |
| 1630 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1631 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1632 | return -ENODEV; |
| 1633 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1634 | obj = i915_gem_object_lookup(file, args->handle); |
| 1635 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1636 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1637 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1638 | /* prime objects have no backing filp to GEM mmap |
| 1639 | * pages from. |
| 1640 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1641 | if (!obj->base.filp) { |
Chris Wilson | 794a11c | 2019-03-14 07:58:29 +0000 | [diff] [blame^] | 1642 | addr = -ENXIO; |
| 1643 | goto err; |
| 1644 | } |
| 1645 | |
| 1646 | if (range_overflows(args->offset, args->size, (u64)obj->base.size)) { |
| 1647 | addr = -EINVAL; |
| 1648 | goto err; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1649 | } |
| 1650 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1651 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1652 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1653 | args->offset); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1654 | if (IS_ERR_VALUE(addr)) |
| 1655 | goto err; |
| 1656 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1657 | if (args->flags & I915_MMAP_WC) { |
| 1658 | struct mm_struct *mm = current->mm; |
| 1659 | struct vm_area_struct *vma; |
| 1660 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1661 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | 794a11c | 2019-03-14 07:58:29 +0000 | [diff] [blame^] | 1662 | addr = -EINTR; |
| 1663 | goto err; |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1664 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1665 | vma = find_vma(mm, addr); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1666 | if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1667 | vma->vm_page_prot = |
| 1668 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1669 | else |
| 1670 | addr = -ENOMEM; |
| 1671 | up_write(&mm->mmap_sem); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1672 | if (IS_ERR_VALUE(addr)) |
| 1673 | goto err; |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1674 | |
| 1675 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1676 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1677 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1678 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1679 | |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1680 | args->addr_ptr = (u64)addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1681 | return 0; |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1682 | |
| 1683 | err: |
| 1684 | i915_gem_object_put(obj); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1685 | return addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1688 | 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] | 1689 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1690 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1691 | } |
| 1692 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1693 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1694 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1695 | * |
| 1696 | * A history of the GTT mmap interface: |
| 1697 | * |
| 1698 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1699 | * aligned and suitable for fencing, and still fit into the available |
| 1700 | * mappable space left by the pinned display objects. A classic problem |
| 1701 | * we called the page-fault-of-doom where we would ping-pong between |
| 1702 | * two objects that could not fit inside the GTT and so the memcpy |
| 1703 | * would page one object in at the expense of the other between every |
| 1704 | * single byte. |
| 1705 | * |
| 1706 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1707 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1708 | * object is too large for the available space (or simply too large |
| 1709 | * for the mappable aperture!), a view is created instead and faulted |
| 1710 | * into userspace. (This view is aligned and sized appropriately for |
| 1711 | * fenced access.) |
| 1712 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1713 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1714 | * delayed writes via GTT before performing direct access via WC. |
| 1715 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1716 | * Restrictions: |
| 1717 | * |
| 1718 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1719 | * hangs on some architectures, corruption on others. An attempt to service |
| 1720 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1721 | * |
| 1722 | * * the object must be able to fit into RAM (physical memory, though no |
| 1723 | * limited to the mappable aperture). |
| 1724 | * |
| 1725 | * |
| 1726 | * Caveats: |
| 1727 | * |
| 1728 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1729 | * all data to system memory. Subsequent access will not be synchronized. |
| 1730 | * |
| 1731 | * * all mappings are revoked on runtime device suspend. |
| 1732 | * |
| 1733 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1734 | * (older machines require fence register for display and blitter access |
| 1735 | * as well). Contention of the fence registers will cause the previous users |
| 1736 | * to be unmapped and any new access will generate new page faults. |
| 1737 | * |
| 1738 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1739 | * rather than the expected SIGSEGV. |
| 1740 | */ |
| 1741 | int i915_gem_mmap_gtt_version(void) |
| 1742 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1743 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1744 | } |
| 1745 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1746 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1747 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1748 | pgoff_t page_offset, |
| 1749 | unsigned int chunk) |
| 1750 | { |
| 1751 | struct i915_ggtt_view view; |
| 1752 | |
| 1753 | if (i915_gem_object_is_tiled(obj)) |
| 1754 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1755 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1756 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1757 | view.partial.offset = rounddown(page_offset, chunk); |
| 1758 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1759 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1760 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1761 | |
| 1762 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1763 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1764 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1765 | |
| 1766 | return view; |
| 1767 | } |
| 1768 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1769 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1770 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1771 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1772 | * |
| 1773 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1774 | * from userspace. The fault handler takes care of binding the object to |
| 1775 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1776 | * only if needed based on whether the old reg is still valid or the object |
| 1777 | * is tiled) and inserting a new PTE into the faulting process. |
| 1778 | * |
| 1779 | * Note that the faulting process may involve evicting existing objects |
| 1780 | * from the GTT and/or fence registers to make room. So performance may |
| 1781 | * suffer if the GTT working set is large or there are few fence registers |
| 1782 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1783 | * |
| 1784 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1785 | * 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] | 1786 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1787 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1788 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1789 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1790 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1791 | 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] | 1792 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1793 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1794 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1795 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1796 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1797 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1798 | pgoff_t page_offset; |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 1799 | int srcu; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1800 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1801 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1802 | /* Sanity check that we allow writing into this object */ |
| 1803 | if (i915_gem_object_is_readonly(obj) && write) |
| 1804 | return VM_FAULT_SIGBUS; |
| 1805 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1806 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1807 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1808 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1809 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1810 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1811 | /* 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] | 1812 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1813 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1814 | * where we are gazumped. |
| 1815 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1816 | ret = i915_gem_object_wait(obj, |
| 1817 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1818 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1819 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1820 | goto err; |
| 1821 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1822 | ret = i915_gem_object_pin_pages(obj); |
| 1823 | if (ret) |
| 1824 | goto err; |
| 1825 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1826 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1827 | |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1828 | srcu = i915_reset_trylock(dev_priv); |
| 1829 | if (srcu < 0) { |
| 1830 | ret = srcu; |
| 1831 | goto err_rpm; |
| 1832 | } |
| 1833 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1834 | ret = i915_mutex_lock_interruptible(dev); |
| 1835 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1836 | goto err_reset; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1837 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1838 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1839 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1840 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1841 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1844 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1845 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1846 | PIN_MAPPABLE | |
| 1847 | PIN_NONBLOCK | |
| 1848 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1849 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1850 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1851 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1852 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1853 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1854 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1855 | flags = PIN_MAPPABLE; |
| 1856 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1857 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1858 | |
| 1859 | /* |
| 1860 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1861 | * all hope that the hardware is able to track future writes. |
| 1862 | */ |
| 1863 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1864 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1865 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1866 | if (IS_ERR(vma) && !view.type) { |
| 1867 | flags = PIN_MAPPABLE; |
| 1868 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1869 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1870 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1871 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1872 | if (IS_ERR(vma)) { |
| 1873 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1874 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1875 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1876 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1877 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1878 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1879 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1880 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1881 | ret = i915_vma_pin_fence(vma); |
| 1882 | if (ret) |
| 1883 | goto err_unpin; |
| 1884 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1885 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1886 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1887 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1888 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1889 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1890 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1891 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1892 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1893 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1894 | /* Mark as being mmapped into userspace for later revocation */ |
| 1895 | assert_rpm_wakelock_held(dev_priv); |
| 1896 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1897 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1898 | GEM_BUG_ON(!obj->userfault_count); |
| 1899 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1900 | i915_vma_set_ggtt_write(vma); |
| 1901 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1902 | err_fence: |
| 1903 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1904 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1905 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1906 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1907 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1908 | err_reset: |
| 1909 | i915_reset_unlock(dev_priv, srcu); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1910 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1911 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1912 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1913 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1914 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1915 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1916 | /* |
| 1917 | * We eat errors when the gpu is terminally wedged to avoid |
| 1918 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1919 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1920 | * and so needs to be reported. |
| 1921 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1922 | if (!i915_terminally_wedged(dev_priv)) |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1923 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1924 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1925 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1926 | /* |
| 1927 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1928 | * handler to reset everything when re-faulting in |
| 1929 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1930 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1931 | case 0: |
| 1932 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1933 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1934 | case -EBUSY: |
| 1935 | /* |
| 1936 | * EBUSY is ok: this just means that another thread |
| 1937 | * already did the job. |
| 1938 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1939 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1940 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1941 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1942 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 1943 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1944 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1945 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1946 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1947 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1951 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 1952 | { |
| 1953 | struct i915_vma *vma; |
| 1954 | |
| 1955 | GEM_BUG_ON(!obj->userfault_count); |
| 1956 | |
| 1957 | obj->userfault_count = 0; |
| 1958 | list_del(&obj->userfault_link); |
| 1959 | drm_vma_node_unmap(&obj->base.vma_node, |
| 1960 | obj->base.dev->anon_inode->i_mapping); |
| 1961 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1962 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1963 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1966 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1967 | * i915_gem_release_mmap - remove physical page mappings |
| 1968 | * @obj: obj in question |
| 1969 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1970 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1971 | * relinquish ownership of the pages back to the system. |
| 1972 | * |
| 1973 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1974 | * object through the GTT and then lose the fence register due to |
| 1975 | * resource pressure. Similarly if the object has been moved out of the |
| 1976 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1977 | * mapping will then trigger a page fault on the next user access, allowing |
| 1978 | * fixup by i915_gem_fault(). |
| 1979 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1980 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1981 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1982 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1983 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1984 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1985 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1986 | /* Serialisation between user GTT access and our code depends upon |
| 1987 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 1988 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1989 | * |
| 1990 | * Note that RPM complicates somewhat by adding an additional |
| 1991 | * requirement that operations to the GGTT be made holding the RPM |
| 1992 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1993 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1994 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1995 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1996 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1997 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1998 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1999 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2000 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2001 | |
| 2002 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2003 | * memory transactions from userspace before we return. The TLB |
| 2004 | * flushing implied above by changing the PTE above *should* be |
| 2005 | * sufficient, an extra barrier here just provides us with a bit |
| 2006 | * of paranoid documentation about our requirement to serialise |
| 2007 | * memory writes before touching registers / GSM. |
| 2008 | */ |
| 2009 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2010 | |
| 2011 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2012 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2013 | } |
| 2014 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2015 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2016 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2017 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2018 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2019 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2020 | /* |
| 2021 | * Only called during RPM suspend. All users of the userfault_list |
| 2022 | * must be holding an RPM wakeref to ensure that this can not |
| 2023 | * run concurrently with themselves (and use the struct_mutex for |
| 2024 | * protection between themselves). |
| 2025 | */ |
| 2026 | |
| 2027 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2028 | &dev_priv->mm.userfault_list, userfault_link) |
| 2029 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2030 | |
| 2031 | /* The fence will be lost when the device powers down. If any were |
| 2032 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2033 | * down! All other fences will be reacquired by the user upon waking. |
| 2034 | */ |
| 2035 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2036 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2037 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2038 | /* Ideally we want to assert that the fence register is not |
| 2039 | * live at this point (i.e. that no piece of code will be |
| 2040 | * trying to write through fence + GTT, as that both violates |
| 2041 | * our tracking of activity and associated locking/barriers, |
| 2042 | * but also is illegal given that the hw is powered down). |
| 2043 | * |
| 2044 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2045 | * That is not sufficient, and we need a more fine-grained |
| 2046 | * tool if we want to have a sanity check here. |
| 2047 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2048 | |
| 2049 | if (!reg->vma) |
| 2050 | continue; |
| 2051 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2052 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2053 | reg->dirty = true; |
| 2054 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2057 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2058 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2059 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2060 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2061 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2062 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2063 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2064 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2065 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2066 | /* Attempt to reap some mmap space from dead objects */ |
| 2067 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2068 | err = i915_gem_wait_for_idle(dev_priv, |
| 2069 | I915_WAIT_INTERRUPTIBLE, |
| 2070 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2071 | if (err) |
| 2072 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2073 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2074 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2075 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2076 | if (!err) |
| 2077 | break; |
| 2078 | |
| 2079 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2080 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2081 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2085 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2086 | drm_gem_free_mmap_offset(&obj->base); |
| 2087 | } |
| 2088 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2089 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2090 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2091 | struct drm_device *dev, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 2092 | u32 handle, |
| 2093 | u64 *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2094 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2095 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2096 | int ret; |
| 2097 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2098 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2099 | if (!obj) |
| 2100 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2101 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2102 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2103 | if (ret == 0) |
| 2104 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2105 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2106 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2107 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2108 | } |
| 2109 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2110 | /** |
| 2111 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2112 | * @dev: DRM device |
| 2113 | * @data: GTT mapping ioctl data |
| 2114 | * @file: GEM object info |
| 2115 | * |
| 2116 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2117 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2118 | * up so we can get faults in the handler above. |
| 2119 | * |
| 2120 | * The fault handler will take care of binding the object into the GTT |
| 2121 | * (since it may have been evicted to make room for something), allocating |
| 2122 | * a fence register, and mapping the appropriate aperture address into |
| 2123 | * userspace. |
| 2124 | */ |
| 2125 | int |
| 2126 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2127 | struct drm_file *file) |
| 2128 | { |
| 2129 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2130 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2131 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2132 | } |
| 2133 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2134 | /* Immediately discard the backing storage */ |
| 2135 | static void |
| 2136 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2137 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2138 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2139 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2140 | if (obj->base.filp == NULL) |
| 2141 | return; |
| 2142 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2143 | /* Our goal here is to return as much of the memory as |
| 2144 | * is possible back to the system as we are called from OOM. |
| 2145 | * To do this we must instruct the shmfs to drop all of its |
| 2146 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2147 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2148 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2149 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2150 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2151 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2152 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2153 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2154 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2155 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2156 | struct address_space *mapping; |
| 2157 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2158 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2159 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2160 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2161 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2162 | case I915_MADV_DONTNEED: |
| 2163 | i915_gem_object_truncate(obj); |
| 2164 | case __I915_MADV_PURGED: |
| 2165 | return; |
| 2166 | } |
| 2167 | |
| 2168 | if (obj->base.filp == NULL) |
| 2169 | return; |
| 2170 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2171 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2172 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2173 | } |
| 2174 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2175 | /* |
| 2176 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2177 | * ref count of those pages. |
| 2178 | */ |
| 2179 | static void check_release_pagevec(struct pagevec *pvec) |
| 2180 | { |
| 2181 | check_move_unevictable_pages(pvec); |
| 2182 | __pagevec_release(pvec); |
| 2183 | cond_resched(); |
| 2184 | } |
| 2185 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2186 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2187 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2188 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2189 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2190 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2191 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2192 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2193 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2194 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2195 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2196 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2197 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2198 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2199 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2200 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2201 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2202 | |
| 2203 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2204 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2205 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2206 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2207 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2208 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2209 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2210 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2211 | if (!pagevec_add(&pvec, page)) |
| 2212 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2213 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2214 | if (pagevec_count(&pvec)) |
| 2215 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2216 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2217 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2218 | sg_free_table(pages); |
| 2219 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2220 | } |
| 2221 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2222 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2223 | { |
| 2224 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2225 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2226 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2227 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2228 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2229 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2230 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2231 | } |
| 2232 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2233 | static struct sg_table * |
| 2234 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2235 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2236 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2237 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2238 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2239 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2240 | if (IS_ERR_OR_NULL(pages)) |
| 2241 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2242 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2243 | spin_lock(&i915->mm.obj_lock); |
| 2244 | list_del(&obj->mm.link); |
| 2245 | spin_unlock(&i915->mm.obj_lock); |
| 2246 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2247 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2248 | void *ptr; |
| 2249 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2250 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2251 | if (is_vmalloc_addr(ptr)) |
| 2252 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2253 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2254 | kunmap(kmap_to_page(ptr)); |
| 2255 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2256 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2257 | } |
| 2258 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2259 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2260 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2261 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2262 | return pages; |
| 2263 | } |
| 2264 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2265 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2266 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2267 | { |
| 2268 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2269 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2270 | |
| 2271 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2272 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2273 | |
| 2274 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2275 | |
| 2276 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2277 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2278 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2279 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2280 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2281 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2282 | |
| 2283 | /* |
| 2284 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2285 | * array, hence protect them from being reaped by removing them from gtt |
| 2286 | * lists early. |
| 2287 | */ |
| 2288 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2289 | |
| 2290 | /* |
| 2291 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2292 | * NULL pages. In the future, when we have more asynchronous |
| 2293 | * get_pages backends we should be better able to handle the |
| 2294 | * cancellation of the async task in a more uniform manner. |
| 2295 | */ |
| 2296 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2297 | pages = ERR_PTR(-EINVAL); |
| 2298 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2299 | if (!IS_ERR(pages)) |
| 2300 | obj->ops->put_pages(obj, pages); |
| 2301 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2302 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2303 | unlock: |
| 2304 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2305 | |
| 2306 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2307 | } |
| 2308 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2309 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2310 | { |
| 2311 | struct sg_table new_st; |
| 2312 | struct scatterlist *sg, *new_sg; |
| 2313 | unsigned int i; |
| 2314 | |
| 2315 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2316 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2317 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2318 | 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] | 2319 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2320 | |
| 2321 | new_sg = new_st.sgl; |
| 2322 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2323 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2324 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2325 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2326 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2327 | new_sg = sg_next(new_sg); |
| 2328 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2329 | 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] | 2330 | |
| 2331 | sg_free_table(orig_st); |
| 2332 | |
| 2333 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2334 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2337 | 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] | 2338 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2339 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2340 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2341 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2342 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2343 | struct sg_table *st; |
| 2344 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2345 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2346 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2347 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2348 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2349 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2350 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2351 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2352 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2353 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2354 | /* |
| 2355 | * 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] | 2356 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2357 | * a GPU cache |
| 2358 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2359 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2360 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2361 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2362 | /* |
| 2363 | * If there's no chance of allocating enough pages for the whole |
| 2364 | * object, bail early. |
| 2365 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2366 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2367 | return -ENOMEM; |
| 2368 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2369 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2370 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2371 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2372 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2373 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2374 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2375 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2376 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2379 | /* |
| 2380 | * 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] | 2381 | * at this point until we release them. |
| 2382 | * |
| 2383 | * Fail silently without starting the shrinker |
| 2384 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2385 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2386 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2387 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2388 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2389 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2390 | sg = st->sgl; |
| 2391 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2392 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2393 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2394 | const unsigned int shrink[] = { |
| 2395 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2396 | 0, |
| 2397 | }, *s = shrink; |
| 2398 | gfp_t gfp = noreclaim; |
| 2399 | |
| 2400 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2401 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2402 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 2403 | if (!IS_ERR(page)) |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2404 | break; |
| 2405 | |
| 2406 | if (!*s) { |
| 2407 | ret = PTR_ERR(page); |
| 2408 | goto err_sg; |
| 2409 | } |
| 2410 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2411 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2412 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2413 | /* |
| 2414 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2415 | * our own buffer, now let the real VM do its job and |
| 2416 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2417 | * |
| 2418 | * However, since graphics tend to be disposable, |
| 2419 | * defer the oom here by reporting the ENOMEM back |
| 2420 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2421 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2422 | if (!*s) { |
| 2423 | /* reclaim and warn, but no oom */ |
| 2424 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2425 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2426 | /* |
| 2427 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2428 | * kswapd to reclaim our pages (direct reclaim |
| 2429 | * does not effectively begin pageout of our |
| 2430 | * buffers on its own). However, direct reclaim |
| 2431 | * only waits for kswapd when under allocation |
| 2432 | * congestion. So as a result __GFP_RECLAIM is |
| 2433 | * unreliable and fails to actually reclaim our |
| 2434 | * dirty pages -- unless you try over and over |
| 2435 | * again with !__GFP_NORETRY. However, we still |
| 2436 | * want to fail this allocation rather than |
| 2437 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2438 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2439 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2440 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2441 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2442 | } while (1); |
| 2443 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2444 | if (!i || |
| 2445 | sg->length >= max_segment || |
| 2446 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2447 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2448 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2449 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2450 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2451 | st->nents++; |
| 2452 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2453 | } else { |
| 2454 | sg->length += PAGE_SIZE; |
| 2455 | } |
| 2456 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2457 | |
| 2458 | /* Check that the i965g/gm workaround works. */ |
| 2459 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2460 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2461 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2462 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2463 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2464 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2465 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2466 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2467 | i915_sg_trim(st); |
| 2468 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2469 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2470 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2471 | /* |
| 2472 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2473 | * it could not reserve enough large entries, asking |
| 2474 | * for PAGE_SIZE chunks instead may be helpful. |
| 2475 | */ |
| 2476 | if (max_segment > PAGE_SIZE) { |
| 2477 | for_each_sgt_page(page, sgt_iter, st) |
| 2478 | put_page(page); |
| 2479 | sg_free_table(st); |
| 2480 | |
| 2481 | max_segment = PAGE_SIZE; |
| 2482 | goto rebuild_st; |
| 2483 | } else { |
| 2484 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2485 | "Failed to DMA remap %lu pages\n", |
| 2486 | page_count); |
| 2487 | goto err_pages; |
| 2488 | } |
| 2489 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2490 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2491 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2492 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2493 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2494 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2495 | |
| 2496 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2497 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2498 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2499 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2500 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2501 | mapping_clear_unevictable(mapping); |
| 2502 | pagevec_init(&pvec); |
| 2503 | for_each_sgt_page(page, sgt_iter, st) { |
| 2504 | if (!pagevec_add(&pvec, page)) |
| 2505 | check_release_pagevec(&pvec); |
| 2506 | } |
| 2507 | if (pagevec_count(&pvec)) |
| 2508 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2509 | sg_free_table(st); |
| 2510 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2511 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2512 | /* |
| 2513 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2514 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2515 | * ENOMEM for a genuine allocation failure. |
| 2516 | * |
| 2517 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2518 | * space and so want to translate the error from shmemfs back to our |
| 2519 | * usual understanding of ENOMEM. |
| 2520 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2521 | if (ret == -ENOSPC) |
| 2522 | ret = -ENOMEM; |
| 2523 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2524 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2528 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2529 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2530 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2531 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2532 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2533 | int i; |
| 2534 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2535 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2536 | |
| 2537 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2538 | obj->mm.get_page.sg_idx = 0; |
| 2539 | |
| 2540 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2541 | |
| 2542 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2543 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2544 | GEM_BUG_ON(obj->mm.quirked); |
| 2545 | __i915_gem_object_pin_pages(obj); |
| 2546 | obj->mm.quirked = true; |
| 2547 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2548 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2549 | GEM_BUG_ON(!sg_page_sizes); |
| 2550 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2551 | |
| 2552 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2553 | * Calculate the supported page-sizes which fit into the given |
| 2554 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2555 | * to use opportunistically when later inserting into the GTT. For |
| 2556 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2557 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2558 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2559 | */ |
| 2560 | obj->mm.page_sizes.sg = 0; |
| 2561 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2562 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2563 | obj->mm.page_sizes.sg |= BIT(i); |
| 2564 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2565 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2566 | |
| 2567 | spin_lock(&i915->mm.obj_lock); |
| 2568 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2569 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2573 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2574 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2575 | |
| 2576 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2577 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2578 | return -EFAULT; |
| 2579 | } |
| 2580 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2581 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2582 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2583 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2584 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2587 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2588 | * 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] | 2589 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2590 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2591 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2592 | * or as the object is itself released. |
| 2593 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2594 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2595 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2596 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2597 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2598 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2599 | if (err) |
| 2600 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2601 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2602 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2603 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2604 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2605 | err = ____i915_gem_object_get_pages(obj); |
| 2606 | if (err) |
| 2607 | goto unlock; |
| 2608 | |
| 2609 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2610 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2611 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2612 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2613 | unlock: |
| 2614 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2615 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2616 | } |
| 2617 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2618 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2619 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2620 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2621 | { |
| 2622 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2623 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2624 | struct sgt_iter sgt_iter; |
| 2625 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2626 | struct page *stack_pages[32]; |
| 2627 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2628 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2629 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2630 | void *addr; |
| 2631 | |
| 2632 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2633 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2634 | return kmap(sg_page(sgt->sgl)); |
| 2635 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2636 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2637 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2638 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2639 | if (!pages) |
| 2640 | return NULL; |
| 2641 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2642 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2643 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2644 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2645 | |
| 2646 | /* Check that we have the expected number of pages */ |
| 2647 | GEM_BUG_ON(i != n_pages); |
| 2648 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2649 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2650 | default: |
| 2651 | MISSING_CASE(type); |
| 2652 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2653 | case I915_MAP_WB: |
| 2654 | pgprot = PAGE_KERNEL; |
| 2655 | break; |
| 2656 | case I915_MAP_WC: |
| 2657 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2658 | break; |
| 2659 | } |
| 2660 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2661 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2662 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2663 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2664 | |
| 2665 | return addr; |
| 2666 | } |
| 2667 | |
| 2668 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2669 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2670 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2671 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2672 | enum i915_map_type has_type; |
| 2673 | bool pinned; |
| 2674 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2675 | int ret; |
| 2676 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2677 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2678 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2679 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2680 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2681 | if (ret) |
| 2682 | return ERR_PTR(ret); |
| 2683 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2684 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2685 | type &= ~I915_MAP_OVERRIDE; |
| 2686 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2687 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2688 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2689 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2690 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2691 | ret = ____i915_gem_object_get_pages(obj); |
| 2692 | if (ret) |
| 2693 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2694 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2695 | smp_mb__before_atomic(); |
| 2696 | } |
| 2697 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2698 | pinned = false; |
| 2699 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2700 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2701 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2702 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2703 | if (ptr && has_type != type) { |
| 2704 | if (pinned) { |
| 2705 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2706 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2707 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2708 | |
| 2709 | if (is_vmalloc_addr(ptr)) |
| 2710 | vunmap(ptr); |
| 2711 | else |
| 2712 | kunmap(kmap_to_page(ptr)); |
| 2713 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2714 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2715 | } |
| 2716 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2717 | if (!ptr) { |
| 2718 | ptr = i915_gem_object_map(obj, type); |
| 2719 | if (!ptr) { |
| 2720 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2721 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2722 | } |
| 2723 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2724 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2725 | } |
| 2726 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2727 | out_unlock: |
| 2728 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2729 | return ptr; |
| 2730 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2731 | err_unpin: |
| 2732 | atomic_dec(&obj->mm.pages_pin_count); |
| 2733 | err_unlock: |
| 2734 | ptr = ERR_PTR(ret); |
| 2735 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2736 | } |
| 2737 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2738 | static int |
| 2739 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2740 | const struct drm_i915_gem_pwrite *arg) |
| 2741 | { |
| 2742 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2743 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2744 | u64 remain, offset; |
| 2745 | unsigned int pg; |
| 2746 | |
| 2747 | /* Before we instantiate/pin the backing store for our use, we |
| 2748 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2749 | * the pagecache. We avoid the penalty of instantiating all the |
| 2750 | * pages, important if the user is just writing to a few and never |
| 2751 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2752 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2753 | * or clearing-before-use) before it is overwritten. |
| 2754 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2755 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2756 | return -ENODEV; |
| 2757 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2758 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2759 | return -EFAULT; |
| 2760 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2761 | /* Before the pages are instantiated the object is treated as being |
| 2762 | * in the CPU domain. The pages will be clflushed as required before |
| 2763 | * use, and we can freely write into the pages directly. If userspace |
| 2764 | * races pwrite with any other operation; corruption will ensue - |
| 2765 | * that is userspace's prerogative! |
| 2766 | */ |
| 2767 | |
| 2768 | remain = arg->size; |
| 2769 | offset = arg->offset; |
| 2770 | pg = offset_in_page(offset); |
| 2771 | |
| 2772 | do { |
| 2773 | unsigned int len, unwritten; |
| 2774 | struct page *page; |
| 2775 | void *data, *vaddr; |
| 2776 | int err; |
| 2777 | |
| 2778 | len = PAGE_SIZE - pg; |
| 2779 | if (len > remain) |
| 2780 | len = remain; |
| 2781 | |
| 2782 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2783 | offset, len, 0, |
| 2784 | &page, &data); |
| 2785 | if (err < 0) |
| 2786 | return err; |
| 2787 | |
| 2788 | vaddr = kmap(page); |
| 2789 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2790 | kunmap(page); |
| 2791 | |
| 2792 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2793 | offset, len, len - unwritten, |
| 2794 | page, data); |
| 2795 | if (err < 0) |
| 2796 | return err; |
| 2797 | |
| 2798 | if (unwritten) |
| 2799 | return -EFAULT; |
| 2800 | |
| 2801 | remain -= len; |
| 2802 | user_data += len; |
| 2803 | offset += len; |
| 2804 | pg = 0; |
| 2805 | } while (remain); |
| 2806 | |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2810 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2811 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2812 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2813 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2814 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 2815 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2816 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2817 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2818 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2819 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2820 | mutex_unlock(&dev->struct_mutex); |
| 2821 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2822 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2823 | /* |
| 2824 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2825 | * We do not need to do this test under locking as in the worst-case |
| 2826 | * we queue the retire worker once too often. |
| 2827 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2828 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2829 | queue_delayed_work(dev_priv->wq, |
| 2830 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2831 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2832 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2833 | |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 2834 | static bool switch_to_kernel_context_sync(struct drm_i915_private *i915, |
| 2835 | unsigned long mask) |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2836 | { |
| 2837 | bool result = true; |
| 2838 | |
| 2839 | /* |
| 2840 | * Even if we fail to switch, give whatever is running a small chance |
| 2841 | * to save itself before we report the failure. Yes, this may be a |
| 2842 | * false positive due to e.g. ENOMEM, caveat emptor! |
| 2843 | */ |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 2844 | if (i915_gem_switch_to_kernel_context(i915, mask)) |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2845 | result = false; |
| 2846 | |
| 2847 | if (i915_gem_wait_for_idle(i915, |
| 2848 | I915_WAIT_LOCKED | |
| 2849 | I915_WAIT_FOR_IDLE_BOOST, |
| 2850 | I915_GEM_IDLE_TIMEOUT)) |
| 2851 | result = false; |
| 2852 | |
Chris Wilson | 7d6ce55 | 2019-03-08 09:36:57 +0000 | [diff] [blame] | 2853 | if (!result) { |
Chris Wilson | 831ebf1 | 2019-03-08 13:45:12 +0000 | [diff] [blame] | 2854 | if (i915_modparams.reset) { /* XXX hide warning from gem_eio */ |
| 2855 | dev_err(i915->drm.dev, |
| 2856 | "Failed to idle engines, declaring wedged!\n"); |
| 2857 | GEM_TRACE_DUMP(); |
| 2858 | } |
| 2859 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2860 | /* Forcibly cancel outstanding work and leave the gpu quiet. */ |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2861 | i915_gem_set_wedged(i915); |
| 2862 | } |
| 2863 | |
| 2864 | i915_retire_requests(i915); /* ensure we flush after wedging */ |
| 2865 | return result; |
| 2866 | } |
| 2867 | |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 2868 | static bool load_power_context(struct drm_i915_private *i915) |
| 2869 | { |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 2870 | /* Force loading the kernel context on all engines */ |
| 2871 | if (!switch_to_kernel_context_sync(i915, ALL_ENGINES)) |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 2872 | return false; |
| 2873 | |
| 2874 | /* |
| 2875 | * Immediately park the GPU so that we enable powersaving and |
| 2876 | * treat it as idle. The next time we issue a request, we will |
| 2877 | * unpark and start using the engine->pinned_default_state, otherwise |
| 2878 | * it is in limbo and an early reset may fail. |
| 2879 | */ |
| 2880 | __i915_gem_park(i915); |
| 2881 | |
| 2882 | return true; |
| 2883 | } |
| 2884 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2885 | static void |
| 2886 | i915_gem_idle_work_handler(struct work_struct *work) |
| 2887 | { |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2888 | struct drm_i915_private *i915 = |
| 2889 | container_of(work, typeof(*i915), gt.idle_work.work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2890 | bool rearm_hangcheck; |
| 2891 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2892 | if (!READ_ONCE(i915->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2893 | return; |
| 2894 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2895 | if (READ_ONCE(i915->gt.active_requests)) |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 2896 | return; |
| 2897 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2898 | rearm_hangcheck = |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2899 | cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2900 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2901 | if (!mutex_trylock(&i915->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2902 | /* Currently busy, come back later */ |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2903 | mod_delayed_work(i915->wq, |
| 2904 | &i915->gt.idle_work, |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2905 | msecs_to_jiffies(50)); |
| 2906 | goto out_rearm; |
| 2907 | } |
| 2908 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 2909 | /* |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2910 | * Flush out the last user context, leaving only the pinned |
| 2911 | * kernel context resident. Should anything unfortunate happen |
| 2912 | * while we are idle (such as the GPU being power cycled), no users |
| 2913 | * will be harmed. |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 2914 | */ |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2915 | if (!work_pending(&i915->gt.idle_work.work) && |
| 2916 | !i915->gt.active_requests) { |
| 2917 | ++i915->gt.active_requests; /* don't requeue idle */ |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 2918 | |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 2919 | switch_to_kernel_context_sync(i915, i915->gt.active_engines); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 2920 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2921 | if (!--i915->gt.active_requests) { |
| 2922 | __i915_gem_park(i915); |
| 2923 | rearm_hangcheck = false; |
| 2924 | } |
| 2925 | } |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2926 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2927 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 2928 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2929 | out_rearm: |
| 2930 | if (rearm_hangcheck) { |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2931 | GEM_BUG_ON(!i915->gt.awake); |
| 2932 | i915_queue_hangcheck(i915); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 2933 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2936 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 2937 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2938 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2939 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 2940 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2941 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2942 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2943 | mutex_lock(&i915->drm.struct_mutex); |
| 2944 | |
| 2945 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 2946 | struct i915_gem_context *ctx = lut->ctx; |
| 2947 | struct i915_vma *vma; |
| 2948 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 2949 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2950 | if (ctx->file_priv != fpriv) |
| 2951 | continue; |
| 2952 | |
| 2953 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 2954 | GEM_BUG_ON(vma->obj != obj); |
| 2955 | |
| 2956 | /* We allow the process to have multiple handles to the same |
| 2957 | * vma, in the same fd namespace, by virtue of flink/open. |
| 2958 | */ |
| 2959 | GEM_BUG_ON(!vma->open_count); |
| 2960 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2961 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 2962 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2963 | list_del(&lut->obj_link); |
| 2964 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 2965 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 2966 | i915_lut_handle_free(lut); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2967 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 2968 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2969 | |
| 2970 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2971 | } |
| 2972 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2973 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 2974 | { |
| 2975 | if (timeout_ns < 0) |
| 2976 | return MAX_SCHEDULE_TIMEOUT; |
| 2977 | |
| 2978 | if (timeout_ns == 0) |
| 2979 | return 0; |
| 2980 | |
| 2981 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 2982 | } |
| 2983 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 2984 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2985 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 2986 | * @dev: drm device pointer |
| 2987 | * @data: ioctl data blob |
| 2988 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2989 | * |
| 2990 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 2991 | * the timeout parameter. |
| 2992 | * -ETIME: object is still busy after timeout |
| 2993 | * -ERESTARTSYS: signal interrupted the wait |
| 2994 | * -ENONENT: object doesn't exist |
| 2995 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 2996 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2997 | * -ENOMEM: damn |
| 2998 | * -ENODEV: Internal IRQ fail |
| 2999 | * -E?: The add request failed |
| 3000 | * |
| 3001 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3002 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3003 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3004 | * without holding struct_mutex the object may become re-busied before this |
| 3005 | * function completes. A similar but shorter * race condition exists in the busy |
| 3006 | * ioctl |
| 3007 | */ |
| 3008 | int |
| 3009 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3010 | { |
| 3011 | struct drm_i915_gem_wait *args = data; |
| 3012 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3013 | ktime_t start; |
| 3014 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3015 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3016 | if (args->flags != 0) |
| 3017 | return -EINVAL; |
| 3018 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3019 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3020 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3021 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3022 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3023 | start = ktime_get(); |
| 3024 | |
| 3025 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 3026 | I915_WAIT_INTERRUPTIBLE | |
| 3027 | I915_WAIT_PRIORITY | |
| 3028 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3029 | to_wait_timeout(args->timeout_ns)); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3030 | |
| 3031 | if (args->timeout_ns > 0) { |
| 3032 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3033 | if (args->timeout_ns < 0) |
| 3034 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3035 | |
| 3036 | /* |
| 3037 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3038 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3039 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3040 | * |
| 3041 | * This is a regression from the timespec->ktime conversion. |
| 3042 | */ |
| 3043 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3044 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3045 | |
| 3046 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3047 | if (ret == -ETIME && args->timeout_ns) |
| 3048 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3049 | } |
| 3050 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3051 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3052 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3053 | } |
| 3054 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3055 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3056 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3057 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3058 | dev_err(i915->drm.dev, |
| 3059 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3060 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3061 | i915_gem_set_wedged(i915); |
| 3062 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | return 0; |
| 3066 | } |
| 3067 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3068 | static long |
| 3069 | wait_for_timelines(struct drm_i915_private *i915, |
| 3070 | unsigned int flags, long timeout) |
| 3071 | { |
| 3072 | struct i915_gt_timelines *gt = &i915->gt.timelines; |
| 3073 | struct i915_timeline *tl; |
| 3074 | |
| 3075 | if (!READ_ONCE(i915->gt.active_requests)) |
| 3076 | return timeout; |
| 3077 | |
| 3078 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3079 | list_for_each_entry(tl, >->active_list, link) { |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3080 | struct i915_request *rq; |
| 3081 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3082 | rq = i915_active_request_get_unlocked(&tl->last_request); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3083 | if (!rq) |
| 3084 | continue; |
| 3085 | |
| 3086 | mutex_unlock(>->mutex); |
| 3087 | |
| 3088 | /* |
| 3089 | * "Race-to-idle". |
| 3090 | * |
| 3091 | * Switching to the kernel context is often used a synchronous |
| 3092 | * step prior to idling, e.g. in suspend for flushing all |
| 3093 | * current operations to memory before sleeping. These we |
| 3094 | * want to complete as quickly as possible to avoid prolonged |
| 3095 | * stalls, so allow the gpu to boost to maximum clocks. |
| 3096 | */ |
| 3097 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3098 | gen6_rps_boost(rq); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3099 | |
| 3100 | timeout = i915_request_wait(rq, flags, timeout); |
| 3101 | i915_request_put(rq); |
| 3102 | if (timeout < 0) |
| 3103 | return timeout; |
| 3104 | |
| 3105 | /* restart after reacquiring the lock */ |
| 3106 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3107 | tl = list_entry(>->active_list, typeof(*tl), link); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3108 | } |
| 3109 | mutex_unlock(>->mutex); |
| 3110 | |
| 3111 | return timeout; |
| 3112 | } |
| 3113 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3114 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 3115 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3116 | { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3117 | GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", |
| 3118 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
| 3119 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3120 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3121 | /* If the device is asleep, we have no requests outstanding */ |
| 3122 | if (!READ_ONCE(i915->gt.awake)) |
| 3123 | return 0; |
| 3124 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3125 | timeout = wait_for_timelines(i915, flags, timeout); |
| 3126 | if (timeout < 0) |
| 3127 | return timeout; |
| 3128 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3129 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3130 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3131 | |
| 3132 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3133 | |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3134 | err = wait_for_engines(i915); |
| 3135 | if (err) |
| 3136 | return err; |
| 3137 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3138 | i915_retire_requests(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3139 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3140 | |
| 3141 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3142 | } |
| 3143 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3144 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3145 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3146 | /* |
| 3147 | * We manually flush the CPU domain so that we can override and |
| 3148 | * force the flush for the display, and perform it asyncrhonously. |
| 3149 | */ |
| 3150 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3151 | if (obj->cache_dirty) |
| 3152 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3153 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3157 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3158 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3159 | return; |
| 3160 | |
| 3161 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3162 | __i915_gem_object_flush_for_display(obj); |
| 3163 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3164 | } |
| 3165 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3166 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3167 | * Moves a single object to the WC read, and possibly write domain. |
| 3168 | * @obj: object to act on |
| 3169 | * @write: ask for write access or read only |
| 3170 | * |
| 3171 | * This function returns when the move is complete, including waiting on |
| 3172 | * flushes to occur. |
| 3173 | */ |
| 3174 | int |
| 3175 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3176 | { |
| 3177 | int ret; |
| 3178 | |
| 3179 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3180 | |
| 3181 | ret = i915_gem_object_wait(obj, |
| 3182 | I915_WAIT_INTERRUPTIBLE | |
| 3183 | I915_WAIT_LOCKED | |
| 3184 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3185 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3186 | if (ret) |
| 3187 | return ret; |
| 3188 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3189 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3190 | return 0; |
| 3191 | |
| 3192 | /* Flush and acquire obj->pages so that we are coherent through |
| 3193 | * direct access in memory with previous cached writes through |
| 3194 | * shmemfs and that our cache domain tracking remains valid. |
| 3195 | * For example, if the obj->filp was moved to swap without us |
| 3196 | * being notified and releasing the pages, we would mistakenly |
| 3197 | * continue to assume that the obj remained out of the CPU cached |
| 3198 | * domain. |
| 3199 | */ |
| 3200 | ret = i915_gem_object_pin_pages(obj); |
| 3201 | if (ret) |
| 3202 | return ret; |
| 3203 | |
| 3204 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3205 | |
| 3206 | /* Serialise direct access to this object with the barriers for |
| 3207 | * coherent writes from the GPU, by effectively invalidating the |
| 3208 | * WC domain upon first access. |
| 3209 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3210 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3211 | mb(); |
| 3212 | |
| 3213 | /* It should now be out of any other write domains, and we can update |
| 3214 | * the domain values for our changes. |
| 3215 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3216 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3217 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3218 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3219 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3220 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3221 | obj->mm.dirty = true; |
| 3222 | } |
| 3223 | |
| 3224 | i915_gem_object_unpin_pages(obj); |
| 3225 | return 0; |
| 3226 | } |
| 3227 | |
| 3228 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3229 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3230 | * @obj: object to act on |
| 3231 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3232 | * |
| 3233 | * This function returns when the move is complete, including waiting on |
| 3234 | * flushes to occur. |
| 3235 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3236 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3237 | 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] | 3238 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3239 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3240 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3241 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3242 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3243 | ret = i915_gem_object_wait(obj, |
| 3244 | I915_WAIT_INTERRUPTIBLE | |
| 3245 | I915_WAIT_LOCKED | |
| 3246 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3247 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3248 | if (ret) |
| 3249 | return ret; |
| 3250 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3251 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3252 | return 0; |
| 3253 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3254 | /* Flush and acquire obj->pages so that we are coherent through |
| 3255 | * direct access in memory with previous cached writes through |
| 3256 | * shmemfs and that our cache domain tracking remains valid. |
| 3257 | * For example, if the obj->filp was moved to swap without us |
| 3258 | * being notified and releasing the pages, we would mistakenly |
| 3259 | * continue to assume that the obj remained out of the CPU cached |
| 3260 | * domain. |
| 3261 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3262 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3263 | if (ret) |
| 3264 | return ret; |
| 3265 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3266 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3267 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3268 | /* Serialise direct access to this object with the barriers for |
| 3269 | * coherent writes from the GPU, by effectively invalidating the |
| 3270 | * GTT domain upon first access. |
| 3271 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3272 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3273 | mb(); |
| 3274 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3275 | /* It should now be out of any other write domains, and we can update |
| 3276 | * the domain values for our changes. |
| 3277 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3278 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3279 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3280 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3281 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3282 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3283 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3284 | } |
| 3285 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3286 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3287 | return 0; |
| 3288 | } |
| 3289 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3290 | /** |
| 3291 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3292 | * @obj: object to act on |
| 3293 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3294 | * |
| 3295 | * After this function returns, the object will be in the new cache-level |
| 3296 | * across all GTT and the contents of the backing storage will be coherent, |
| 3297 | * with respect to the new cache-level. In order to keep the backing storage |
| 3298 | * coherent for all users, we only allow a single cache level to be set |
| 3299 | * globally on the object and prevent it from being changed whilst the |
| 3300 | * hardware is reading from the object. That is if the object is currently |
| 3301 | * on the scanout it will be set to uncached (or equivalent display |
| 3302 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3303 | * that all direct access to the scanout remains coherent. |
| 3304 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3305 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3306 | enum i915_cache_level cache_level) |
| 3307 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3308 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3309 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3310 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3311 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3312 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3313 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3314 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3315 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3316 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3317 | * be invalid given the new cache-level. This is principally to |
| 3318 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3319 | * reading an invalid PTE on older architectures. |
| 3320 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3321 | restart: |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3322 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3323 | if (!drm_mm_node_allocated(&vma->node)) |
| 3324 | continue; |
| 3325 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3326 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3327 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3328 | return -EBUSY; |
| 3329 | } |
| 3330 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3331 | if (!i915_vma_is_closed(vma) && |
| 3332 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3333 | continue; |
| 3334 | |
| 3335 | ret = i915_vma_unbind(vma); |
| 3336 | if (ret) |
| 3337 | return ret; |
| 3338 | |
| 3339 | /* As unbinding may affect other elements in the |
| 3340 | * obj->vma_list (due to side-effects from retiring |
| 3341 | * an active vma), play safe and restart the iterator. |
| 3342 | */ |
| 3343 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3344 | } |
| 3345 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3346 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3347 | * cache-level on the PTE. We could simply unbind them all and |
| 3348 | * rebind with the correct cache-level on next use. However since |
| 3349 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3350 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3351 | * state and so involves less work. |
| 3352 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3353 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3354 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3355 | * If we wait upon the object, we know that all the bound |
| 3356 | * VMA are no longer active. |
| 3357 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3358 | ret = i915_gem_object_wait(obj, |
| 3359 | I915_WAIT_INTERRUPTIBLE | |
| 3360 | I915_WAIT_LOCKED | |
| 3361 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3362 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3363 | if (ret) |
| 3364 | return ret; |
| 3365 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3366 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3367 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3368 | /* Access to snoopable pages through the GTT is |
| 3369 | * incoherent and on some machines causes a hard |
| 3370 | * lockup. Relinquish the CPU mmaping to force |
| 3371 | * userspace to refault in the pages and we can |
| 3372 | * then double check if the GTT mapping is still |
| 3373 | * valid for that pointer access. |
| 3374 | */ |
| 3375 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3376 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3377 | /* As we no longer need a fence for GTT access, |
| 3378 | * we can relinquish it now (and so prevent having |
| 3379 | * to steal a fence from someone else on the next |
| 3380 | * fence request). Note GPU activity would have |
| 3381 | * dropped the fence as all snoopable access is |
| 3382 | * supposed to be linear. |
| 3383 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3384 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3385 | ret = i915_vma_put_fence(vma); |
| 3386 | if (ret) |
| 3387 | return ret; |
| 3388 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3389 | } else { |
| 3390 | /* We either have incoherent backing store and |
| 3391 | * so no GTT access or the architecture is fully |
| 3392 | * coherent. In such cases, existing GTT mmaps |
| 3393 | * ignore the cache bit in the PTE and we can |
| 3394 | * rewrite it without confusing the GPU or having |
| 3395 | * to force userspace to fault back in its mmaps. |
| 3396 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3399 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3400 | if (!drm_mm_node_allocated(&vma->node)) |
| 3401 | continue; |
| 3402 | |
| 3403 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3404 | if (ret) |
| 3405 | return ret; |
| 3406 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3407 | } |
| 3408 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3409 | list_for_each_entry(vma, &obj->vma.list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3410 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3411 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3412 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3413 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3414 | return 0; |
| 3415 | } |
| 3416 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3417 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3418 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3419 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3420 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3421 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3422 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3423 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3424 | rcu_read_lock(); |
| 3425 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3426 | if (!obj) { |
| 3427 | err = -ENOENT; |
| 3428 | goto out; |
| 3429 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3430 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3431 | switch (obj->cache_level) { |
| 3432 | case I915_CACHE_LLC: |
| 3433 | case I915_CACHE_L3_LLC: |
| 3434 | args->caching = I915_CACHING_CACHED; |
| 3435 | break; |
| 3436 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3437 | case I915_CACHE_WT: |
| 3438 | args->caching = I915_CACHING_DISPLAY; |
| 3439 | break; |
| 3440 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3441 | default: |
| 3442 | args->caching = I915_CACHING_NONE; |
| 3443 | break; |
| 3444 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3445 | out: |
| 3446 | rcu_read_unlock(); |
| 3447 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3448 | } |
| 3449 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3450 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3451 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3452 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3453 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3454 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3455 | struct drm_i915_gem_object *obj; |
| 3456 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3457 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3458 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3459 | switch (args->caching) { |
| 3460 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3461 | level = I915_CACHE_NONE; |
| 3462 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3463 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3464 | /* |
| 3465 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3466 | * snooped mapping may leave stale data in a corresponding CPU |
| 3467 | * cacheline, whereas normally such cachelines would get |
| 3468 | * invalidated. |
| 3469 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3470 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3471 | return -ENODEV; |
| 3472 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3473 | level = I915_CACHE_LLC; |
| 3474 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3475 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3476 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3477 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3478 | default: |
| 3479 | return -EINVAL; |
| 3480 | } |
| 3481 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3482 | obj = i915_gem_object_lookup(file, args->handle); |
| 3483 | if (!obj) |
| 3484 | return -ENOENT; |
| 3485 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3486 | /* |
| 3487 | * The caching mode of proxy object is handled by its generator, and |
| 3488 | * not allowed to be changed by userspace. |
| 3489 | */ |
| 3490 | if (i915_gem_object_is_proxy(obj)) { |
| 3491 | ret = -ENXIO; |
| 3492 | goto out; |
| 3493 | } |
| 3494 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3495 | if (obj->cache_level == level) |
| 3496 | goto out; |
| 3497 | |
| 3498 | ret = i915_gem_object_wait(obj, |
| 3499 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3500 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3501 | if (ret) |
| 3502 | goto out; |
| 3503 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3504 | ret = i915_mutex_lock_interruptible(dev); |
| 3505 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3506 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3507 | |
| 3508 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3509 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3510 | |
| 3511 | out: |
| 3512 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3513 | return ret; |
| 3514 | } |
| 3515 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3516 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 3517 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 3518 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 3519 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 3520 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3521 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3522 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3523 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3524 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3525 | const struct i915_ggtt_view *view, |
| 3526 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3527 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3528 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3529 | int ret; |
| 3530 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3531 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3532 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3533 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3534 | * display coherency whilst setting up the cache domains. |
| 3535 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3536 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3537 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3538 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3539 | * a result, we make sure that the pinning that is about to occur is |
| 3540 | * done with uncached PTEs. This is lowest common denominator for all |
| 3541 | * chipsets. |
| 3542 | * |
| 3543 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3544 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3545 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3546 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3547 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3548 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3549 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3550 | if (ret) { |
| 3551 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3552 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3553 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3554 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3555 | /* As the user may map the buffer once pinned in the display plane |
| 3556 | * (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] | 3557 | * always use map_and_fenceable for all scanout buffers. However, |
| 3558 | * it may simply be too big to fit into mappable, in which case |
| 3559 | * put it anyway and hope that userspace can cope (but always first |
| 3560 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3561 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3562 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3563 | if ((flags & PIN_MAPPABLE) == 0 && |
| 3564 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3565 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3566 | flags | |
| 3567 | PIN_MAPPABLE | |
| 3568 | PIN_NONBLOCK); |
| 3569 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3570 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3571 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3572 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3573 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 3574 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 3575 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3576 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3577 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3578 | /* It should now be out of any other write domains, and we can update |
| 3579 | * the domain values for our changes. |
| 3580 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3581 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3582 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3583 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3584 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3585 | err_unpin_global: |
| 3586 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3587 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3591 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3592 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 3593 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3594 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3595 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 3596 | return; |
| 3597 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3598 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 3599 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3600 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3601 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 3602 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3603 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3604 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3605 | } |
| 3606 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3607 | /** |
| 3608 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3609 | * @obj: object to act on |
| 3610 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3611 | * |
| 3612 | * This function returns when the move is complete, including waiting on |
| 3613 | * flushes to occur. |
| 3614 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3615 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3616 | 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] | 3617 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3618 | int ret; |
| 3619 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3620 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3621 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3622 | ret = i915_gem_object_wait(obj, |
| 3623 | I915_WAIT_INTERRUPTIBLE | |
| 3624 | I915_WAIT_LOCKED | |
| 3625 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3626 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3627 | if (ret) |
| 3628 | return ret; |
| 3629 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3630 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3631 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3632 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3633 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 3634 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3635 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | /* It should now be out of any other write domains, and we can update |
| 3639 | * the domain values for our changes. |
| 3640 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3641 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3642 | |
| 3643 | /* If we're writing through the CPU, then the GPU read domains will |
| 3644 | * need to be invalidated at next use. |
| 3645 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3646 | if (write) |
| 3647 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3648 | |
| 3649 | return 0; |
| 3650 | } |
| 3651 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3652 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3653 | * emitted over 20 msec ago. |
| 3654 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3655 | * Note that if we were to use the current jiffies each time around the loop, |
| 3656 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3657 | * render a frame was over 20ms. |
| 3658 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3659 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3660 | * relatively low latency when blocking on a particular request to finish. |
| 3661 | */ |
| 3662 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3663 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3664 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3665 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3666 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 3667 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3668 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3669 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3670 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 3671 | /* ABI: return -EIO if already wedged */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 3672 | ret = i915_terminally_wedged(dev_priv); |
| 3673 | if (ret) |
| 3674 | return ret; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3675 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3676 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3677 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3678 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3679 | break; |
| 3680 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3681 | if (target) { |
| 3682 | list_del(&target->client_link); |
| 3683 | target->file_priv = NULL; |
| 3684 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 3685 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3686 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3687 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3688 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3689 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3690 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3691 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3692 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3693 | return 0; |
| 3694 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3695 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3696 | I915_WAIT_INTERRUPTIBLE, |
| 3697 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3698 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3699 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3700 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3703 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3704 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 3705 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 3706 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 3707 | u64 alignment, |
| 3708 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3709 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3710 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 3711 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3712 | struct i915_vma *vma; |
| 3713 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 3714 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3715 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3716 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 3717 | if (flags & PIN_MAPPABLE && |
| 3718 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3719 | /* If the required space is larger than the available |
| 3720 | * aperture, we will not able to find a slot for the |
| 3721 | * object and unbinding the object now will be in |
| 3722 | * vain. Worse, doing so may cause us to ping-pong |
| 3723 | * the object in and out of the Global GTT and |
| 3724 | * waste a lot of cycles under the mutex. |
| 3725 | */ |
| 3726 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 3727 | return ERR_PTR(-E2BIG); |
| 3728 | |
| 3729 | /* If NONBLOCK is set the caller is optimistically |
| 3730 | * trying to cache the full object within the mappable |
| 3731 | * aperture, and *must* have a fallback in place for |
| 3732 | * situations where we cannot bind the object. We |
| 3733 | * can be a little more lax here and use the fallback |
| 3734 | * more often to avoid costly migrations of ourselves |
| 3735 | * and other objects within the aperture. |
| 3736 | * |
| 3737 | * Half-the-aperture is used as a simple heuristic. |
| 3738 | * More interesting would to do search for a free |
| 3739 | * block prior to making the commitment to unbind. |
| 3740 | * That caters for the self-harm case, and with a |
| 3741 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 3742 | * we could try to minimise harm to others. |
| 3743 | */ |
| 3744 | if (flags & PIN_NONBLOCK && |
| 3745 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 3746 | return ERR_PTR(-ENOSPC); |
| 3747 | } |
| 3748 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 3749 | vma = i915_vma_instance(obj, vm, view); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 3750 | if (IS_ERR(vma)) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3751 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3752 | |
| 3753 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3754 | if (flags & PIN_NONBLOCK) { |
| 3755 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 3756 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3757 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3758 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 3759 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3760 | return ERR_PTR(-ENOSPC); |
| 3761 | } |
| 3762 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3763 | WARN(i915_vma_is_pinned(vma), |
| 3764 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3765 | " offset=%08x, req.alignment=%llx," |
| 3766 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 3767 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3768 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3769 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3770 | ret = i915_vma_unbind(vma); |
| 3771 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3772 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3773 | } |
| 3774 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3775 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 3776 | if (ret) |
| 3777 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3778 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3779 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3782 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3783 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3784 | if (id == I915_ENGINE_CLASS_INVALID) |
| 3785 | return 0xffff0000; |
| 3786 | |
| 3787 | GEM_BUG_ON(id >= 16); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3788 | return 0x10000 << id; |
| 3789 | } |
| 3790 | |
| 3791 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 3792 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3793 | /* |
| 3794 | * The uABI guarantees an active writer is also amongst the read |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 3795 | * engines. This would be true if we accessed the activity tracking |
| 3796 | * under the lock, but as we perform the lookup of the object and |
| 3797 | * its activity locklessly we can not guarantee that the last_write |
| 3798 | * being active implies that we have set the same engine flag from |
| 3799 | * last_read - hence we always set both read and write busy for |
| 3800 | * last_write. |
| 3801 | */ |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3802 | if (id == I915_ENGINE_CLASS_INVALID) |
| 3803 | return 0xffffffff; |
| 3804 | |
| 3805 | return (id + 1) | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3806 | } |
| 3807 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3808 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3809 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3810 | unsigned int (*flag)(unsigned int id)) |
| 3811 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3812 | const struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3813 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3814 | /* |
| 3815 | * We have to check the current hw status of the fence as the uABI |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3816 | * guarantees forward progress. We could rely on the idle worker |
| 3817 | * to eventually flush us, but to minimise latency just ask the |
| 3818 | * hardware. |
| 3819 | * |
| 3820 | * Note we only report on the status of native fences. |
| 3821 | */ |
| 3822 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3823 | return 0; |
| 3824 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3825 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3826 | rq = container_of(fence, const struct i915_request, fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3827 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3828 | return 0; |
| 3829 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3830 | return flag(rq->engine->uabi_class); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3831 | } |
| 3832 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3833 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3834 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3835 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3836 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3837 | } |
| 3838 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3839 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3840 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3841 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3842 | if (!fence) |
| 3843 | return 0; |
| 3844 | |
| 3845 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3846 | } |
| 3847 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3848 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3849 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3850 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3851 | { |
| 3852 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3853 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3854 | struct reservation_object_list *list; |
| 3855 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3856 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3857 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3858 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3859 | rcu_read_lock(); |
| 3860 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3861 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3862 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3863 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3864 | /* |
| 3865 | * A discrepancy here is that we do not report the status of |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3866 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 3867 | * a call to set-domain may still stall waiting for foreign rendering. |
| 3868 | * This also means that wait-ioctl may report an object as busy, |
| 3869 | * where busy-ioctl considers it idle. |
| 3870 | * |
| 3871 | * We trade the ability to warn of foreign fences to report on which |
| 3872 | * i915 engines are active for the object. |
| 3873 | * |
| 3874 | * Alternatively, we can trade that extra information on read/write |
| 3875 | * activity with |
| 3876 | * args->busy = |
| 3877 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 3878 | * to report the overall busyness. This is what the wait-ioctl does. |
| 3879 | * |
| 3880 | */ |
| 3881 | retry: |
| 3882 | seq = raw_read_seqcount(&obj->resv->seq); |
| 3883 | |
| 3884 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 3885 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 3886 | |
| 3887 | /* Translate shared fences to READ set of engines */ |
| 3888 | list = rcu_dereference(obj->resv->fence); |
| 3889 | if (list) { |
| 3890 | unsigned int shared_count = list->shared_count, i; |
| 3891 | |
| 3892 | for (i = 0; i < shared_count; ++i) { |
| 3893 | struct dma_fence *fence = |
| 3894 | rcu_dereference(list->shared[i]); |
| 3895 | |
| 3896 | args->busy |= busy_check_reader(fence); |
| 3897 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3898 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3899 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3900 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 3901 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 3902 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3903 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3904 | out: |
| 3905 | rcu_read_unlock(); |
| 3906 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | int |
| 3910 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 3911 | struct drm_file *file_priv) |
| 3912 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 3913 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3914 | } |
| 3915 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3916 | int |
| 3917 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 3918 | struct drm_file *file_priv) |
| 3919 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3920 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3921 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3922 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3923 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3924 | |
| 3925 | switch (args->madv) { |
| 3926 | case I915_MADV_DONTNEED: |
| 3927 | case I915_MADV_WILLNEED: |
| 3928 | break; |
| 3929 | default: |
| 3930 | return -EINVAL; |
| 3931 | } |
| 3932 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3933 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3934 | if (!obj) |
| 3935 | return -ENOENT; |
| 3936 | |
| 3937 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 3938 | if (err) |
| 3939 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3940 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 3941 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 3942 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 3943 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3944 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 3945 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3946 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3947 | obj->mm.quirked = false; |
| 3948 | } |
| 3949 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 3950 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3951 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3952 | obj->mm.quirked = true; |
| 3953 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 3954 | } |
| 3955 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3956 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 3957 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3958 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 3959 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 3960 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 3961 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 3962 | i915_gem_object_truncate(obj); |
| 3963 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3964 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3965 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 3966 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3967 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 3968 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3969 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3970 | } |
| 3971 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3972 | static void |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3973 | frontbuffer_retire(struct i915_active_request *active, |
| 3974 | struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3975 | { |
| 3976 | struct drm_i915_gem_object *obj = |
| 3977 | container_of(active, typeof(*obj), frontbuffer_write); |
| 3978 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 3979 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3982 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 3983 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3984 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3985 | mutex_init(&obj->mm.lock); |
| 3986 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3987 | spin_lock_init(&obj->vma.lock); |
| 3988 | INIT_LIST_HEAD(&obj->vma.list); |
| 3989 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3990 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 3991 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3992 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 3993 | init_rcu_head(&obj->rcu); |
| 3994 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3995 | obj->ops = ops; |
| 3996 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3997 | reservation_object_init(&obj->__builtin_resv); |
| 3998 | obj->resv = &obj->__builtin_resv; |
| 3999 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4000 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4001 | i915_active_request_init(&obj->frontbuffer_write, |
| 4002 | NULL, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4003 | |
| 4004 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4005 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4006 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4007 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4008 | 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] | 4009 | } |
| 4010 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4011 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4012 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4013 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4014 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4015 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4016 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4017 | |
| 4018 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4019 | }; |
| 4020 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4021 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4022 | struct drm_gem_object *obj, |
| 4023 | size_t size) |
| 4024 | { |
| 4025 | struct drm_i915_private *i915 = to_i915(dev); |
| 4026 | unsigned long flags = VM_NORESERVE; |
| 4027 | struct file *filp; |
| 4028 | |
| 4029 | drm_gem_private_object_init(dev, obj, size); |
| 4030 | |
| 4031 | if (i915->mm.gemfs) |
| 4032 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4033 | flags); |
| 4034 | else |
| 4035 | filp = shmem_file_setup("i915", size, flags); |
| 4036 | |
| 4037 | if (IS_ERR(filp)) |
| 4038 | return PTR_ERR(filp); |
| 4039 | |
| 4040 | obj->filp = filp; |
| 4041 | |
| 4042 | return 0; |
| 4043 | } |
| 4044 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4045 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4046 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4047 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4048 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4049 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4050 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4051 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4052 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4053 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4054 | /* There is a prevalence of the assumption that we fit the object's |
| 4055 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4056 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4057 | * such a local variable, please consider fixing! |
| 4058 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4059 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4060 | return ERR_PTR(-E2BIG); |
| 4061 | |
| 4062 | if (overflows_type(size, obj->base.size)) |
| 4063 | return ERR_PTR(-E2BIG); |
| 4064 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 4065 | obj = i915_gem_object_alloc(); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4066 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4067 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4068 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4069 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4070 | if (ret) |
| 4071 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4072 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4073 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4074 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4075 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4076 | mask &= ~__GFP_HIGHMEM; |
| 4077 | mask |= __GFP_DMA32; |
| 4078 | } |
| 4079 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4080 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4081 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4082 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4083 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4084 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4085 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4086 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4087 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4088 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4089 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4090 | /* 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] | 4091 | * cache) for about a 10% performance improvement |
| 4092 | * compared to uncached. Graphics requests other than |
| 4093 | * display scanout are coherent with the CPU in |
| 4094 | * accessing this cache. This means in this mode we |
| 4095 | * don't need to clflush on the CPU side, and on the |
| 4096 | * GPU side we only need to flush internal caches to |
| 4097 | * get data visible to the CPU. |
| 4098 | * |
| 4099 | * However, we maintain the display planes as UC, and so |
| 4100 | * need to rebind when first used as such. |
| 4101 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4102 | cache_level = I915_CACHE_LLC; |
| 4103 | else |
| 4104 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4105 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4106 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4107 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4108 | trace_i915_gem_object_create(obj); |
| 4109 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4110 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4111 | |
| 4112 | fail: |
| 4113 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4114 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4117 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4118 | { |
| 4119 | /* If we are the last user of the backing storage (be it shmemfs |
| 4120 | * pages or stolen etc), we know that the pages are going to be |
| 4121 | * immediately released. In this case, we can then skip copying |
| 4122 | * back the contents from the GPU. |
| 4123 | */ |
| 4124 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4125 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4126 | return false; |
| 4127 | |
| 4128 | if (obj->base.filp == NULL) |
| 4129 | return true; |
| 4130 | |
| 4131 | /* At first glance, this looks racy, but then again so would be |
| 4132 | * userspace racing mmap against close. However, the first external |
| 4133 | * reference to the filp can only be obtained through the |
| 4134 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4135 | * acquiring such a reference whilst we are in the middle of |
| 4136 | * freeing the object. |
| 4137 | */ |
| 4138 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4139 | } |
| 4140 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4141 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4142 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4143 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4144 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4145 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4146 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4147 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4148 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4149 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4150 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4151 | trace_i915_gem_object_destroy(obj); |
| 4152 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4153 | mutex_lock(&i915->drm.struct_mutex); |
| 4154 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4155 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4156 | list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4157 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4158 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4159 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4160 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4161 | GEM_BUG_ON(!list_empty(&obj->vma.list)); |
| 4162 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4163 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4164 | /* This serializes freeing with the shrinker. Since the free |
| 4165 | * is delayed, first by RCU then by the workqueue, we want the |
| 4166 | * shrinker to be able to free pages of unreferenced objects, |
| 4167 | * or else we may oom whilst there are plenty of deferred |
| 4168 | * freed objects. |
| 4169 | */ |
| 4170 | if (i915_gem_object_has_pages(obj)) { |
| 4171 | spin_lock(&i915->mm.obj_lock); |
| 4172 | list_del_init(&obj->mm.link); |
| 4173 | spin_unlock(&i915->mm.obj_lock); |
| 4174 | } |
| 4175 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4176 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4177 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4178 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4179 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4180 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4181 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4182 | |
| 4183 | if (obj->ops->release) |
| 4184 | obj->ops->release(obj); |
| 4185 | |
| 4186 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4187 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4188 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4189 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4190 | |
| 4191 | if (obj->base.import_attach) |
| 4192 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4193 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4194 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4195 | drm_gem_object_release(&obj->base); |
| 4196 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4197 | |
| 4198 | kfree(obj->bit_17); |
| 4199 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4200 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4201 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4202 | atomic_dec(&i915->mm.free_count); |
| 4203 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4204 | if (on) |
| 4205 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4206 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4207 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4211 | { |
| 4212 | struct llist_node *freed; |
| 4213 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4214 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4215 | freed = NULL; |
| 4216 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4217 | /* Only one consumer of llist_del_first() allowed */ |
| 4218 | spin_lock(&i915->mm.free_lock); |
| 4219 | freed = llist_del_first(&i915->mm.free_list); |
| 4220 | spin_unlock(&i915->mm.free_lock); |
| 4221 | } |
| 4222 | if (unlikely(freed)) { |
| 4223 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4224 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4225 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | static void __i915_gem_free_work(struct work_struct *work) |
| 4229 | { |
| 4230 | struct drm_i915_private *i915 = |
| 4231 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4232 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4233 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4234 | /* |
| 4235 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4236 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4237 | * However, the object may also be bound into the global GTT (e.g. |
| 4238 | * older GPUs without per-process support, or for direct access through |
| 4239 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4240 | * unbound now. |
| 4241 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4242 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4243 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4244 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4245 | spin_unlock(&i915->mm.free_lock); |
| 4246 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4247 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4248 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4249 | return; |
| 4250 | |
| 4251 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4252 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4253 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4257 | { |
| 4258 | struct drm_i915_gem_object *obj = |
| 4259 | container_of(head, typeof(*obj), rcu); |
| 4260 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4261 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4262 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4263 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4264 | * it like a rcu_head from this point forwards. And we expect all |
| 4265 | * objects to be freed via this path. |
| 4266 | */ |
| 4267 | destroy_rcu_head(&obj->rcu); |
| 4268 | |
| 4269 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4270 | * Since we require blocking on struct_mutex to unbind the freed |
| 4271 | * object from the GPU before releasing resources back to the |
| 4272 | * system, we can not do that directly from the RCU callback (which may |
| 4273 | * be a softirq context), but must instead then defer that work onto a |
| 4274 | * kthread. We use the RCU callback rather than move the freed object |
| 4275 | * directly onto the work queue so that we can mix between using the |
| 4276 | * worker and performing frees directly from subsequent allocations for |
| 4277 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4278 | */ |
| 4279 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4280 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4284 | { |
| 4285 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4286 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4287 | if (obj->mm.quirked) |
| 4288 | __i915_gem_object_unpin_pages(obj); |
| 4289 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4290 | if (discard_backing_storage(obj)) |
| 4291 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4292 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4293 | /* |
| 4294 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4295 | * read-side critical sections are complete, e.g. |
| 4296 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4297 | * lookup see i915_gem_object_lookup_rcu(). |
| 4298 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4299 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4300 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4301 | } |
| 4302 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4303 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4304 | { |
| 4305 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4306 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4307 | if (!i915_gem_object_has_active_reference(obj) && |
| 4308 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4309 | i915_gem_object_set_active_reference(obj); |
| 4310 | else |
| 4311 | i915_gem_object_put(obj); |
| 4312 | } |
| 4313 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4314 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4315 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4316 | intel_wakeref_t wakeref; |
| 4317 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4318 | GEM_TRACE("\n"); |
| 4319 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4320 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4321 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 4322 | |
| 4323 | /* |
| 4324 | * As we have just resumed the machine and woken the device up from |
| 4325 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4326 | * back to defaults, recovering from whatever wedged state we left it |
| 4327 | * in and so worth trying to use the device once more. |
| 4328 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4329 | if (i915_terminally_wedged(i915)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4330 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4331 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4332 | /* |
| 4333 | * If we inherit context state from the BIOS or earlier occupants |
| 4334 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4335 | * try to take over. The only way to remove the earlier state |
| 4336 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4337 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4338 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4339 | */ |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 4340 | intel_engines_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4341 | |
| 4342 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4343 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4344 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4345 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4346 | i915_gem_contexts_lost(i915); |
| 4347 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 4350 | void i915_gem_suspend(struct drm_i915_private *i915) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4351 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4352 | intel_wakeref_t wakeref; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4353 | |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 4354 | GEM_TRACE("\n"); |
| 4355 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4356 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4357 | intel_suspend_gt_powersave(i915); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4358 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4359 | flush_workqueue(i915->wq); |
| 4360 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4361 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4362 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4363 | /* |
| 4364 | * We have to flush all the executing contexts to main memory so |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4365 | * that they can saved in the hibernation image. To ensure the last |
| 4366 | * context image is coherent, we have to switch away from it. That |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4367 | * leaves the i915->kernel_context still active when |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4368 | * we actually suspend, and its image in memory may not match the GPU |
| 4369 | * state. Fortunately, the kernel_context is disposable and we do |
| 4370 | * not rely on its state. |
| 4371 | */ |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 4372 | switch_to_kernel_context_sync(i915, i915->gt.active_engines); |
Chris Wilson | 01f8f33 | 2018-07-17 09:41:21 +0100 | [diff] [blame] | 4373 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4374 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4375 | i915_reset_flush(i915); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4376 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4377 | drain_delayed_work(&i915->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4378 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4379 | /* |
| 4380 | * 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] | 4381 | * repeat the flush until it is definitely idle. |
| 4382 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4383 | drain_delayed_work(&i915->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4384 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4385 | /* |
| 4386 | * Assert that we successfully flushed all the work and |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4387 | * reset the GPU back to its idle, low power state. |
| 4388 | */ |
Chris Wilson | 50b022a | 2019-03-07 10:45:30 +0000 | [diff] [blame] | 4389 | GEM_BUG_ON(i915->gt.awake); |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4390 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4391 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | void i915_gem_suspend_late(struct drm_i915_private *i915) |
| 4395 | { |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4396 | struct drm_i915_gem_object *obj; |
| 4397 | struct list_head *phases[] = { |
| 4398 | &i915->mm.unbound_list, |
| 4399 | &i915->mm.bound_list, |
| 4400 | NULL |
| 4401 | }, **phase; |
| 4402 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4403 | /* |
| 4404 | * Neither the BIOS, ourselves or any other kernel |
| 4405 | * expects the system to be in execlists mode on startup, |
| 4406 | * so we need to reset the GPU back to legacy mode. And the only |
| 4407 | * known way to disable logical contexts is through a GPU reset. |
| 4408 | * |
| 4409 | * So in order to leave the system in a known default configuration, |
| 4410 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4411 | * clean up the GEM state tracking, flushing off the requests and |
| 4412 | * leaving the system in a known idle state. |
| 4413 | * |
| 4414 | * Note that is of the upmost importance that the GPU is idle and |
| 4415 | * all stray writes are flushed *before* we dismantle the backing |
| 4416 | * storage for the pinned objects. |
| 4417 | * |
| 4418 | * However, since we are uncertain that resetting the GPU on older |
| 4419 | * machines is a good idea, we don't - just in case it leaves the |
| 4420 | * machine in an unusable condition. |
| 4421 | */ |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4422 | |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4423 | mutex_lock(&i915->drm.struct_mutex); |
| 4424 | for (phase = phases; *phase; phase++) { |
| 4425 | list_for_each_entry(obj, *phase, mm.link) |
| 4426 | WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); |
| 4427 | } |
| 4428 | mutex_unlock(&i915->drm.struct_mutex); |
| 4429 | |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4430 | intel_uc_sanitize(i915); |
| 4431 | i915_gem_sanitize(i915); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4432 | } |
| 4433 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4434 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4435 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4436 | GEM_TRACE("\n"); |
| 4437 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4438 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4439 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4440 | mutex_lock(&i915->drm.struct_mutex); |
| 4441 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4442 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4443 | i915_gem_restore_gtt_mappings(i915); |
| 4444 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4445 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4446 | /* |
| 4447 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4448 | * guarantee that the context image is complete. So let's just reset |
| 4449 | * it and start again. |
| 4450 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4451 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4452 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4453 | if (i915_gem_init_hw(i915)) |
| 4454 | goto err_wedged; |
| 4455 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 4456 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 4457 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4458 | /* Always reload a context for powersaving. */ |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4459 | if (!load_power_context(i915)) |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4460 | goto err_wedged; |
| 4461 | |
| 4462 | out_unlock: |
| 4463 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 4464 | mutex_unlock(&i915->drm.struct_mutex); |
| 4465 | return; |
| 4466 | |
| 4467 | err_wedged: |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4468 | if (!i915_reset_failed(i915)) { |
| 4469 | dev_err(i915->drm.dev, |
| 4470 | "Failed to re-initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4471 | i915_gem_set_wedged(i915); |
| 4472 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4473 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4474 | } |
| 4475 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4476 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4477 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4478 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4479 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4480 | return; |
| 4481 | |
| 4482 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4483 | DISP_TILE_SURFACE_SWIZZLING); |
| 4484 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4485 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4486 | return; |
| 4487 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4488 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4489 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4490 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4491 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4492 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4493 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4494 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4495 | else |
| 4496 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4497 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4498 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4499 | 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] | 4500 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4501 | I915_WRITE(RING_CTL(base), 0); |
| 4502 | I915_WRITE(RING_HEAD(base), 0); |
| 4503 | I915_WRITE(RING_TAIL(base), 0); |
| 4504 | I915_WRITE(RING_START(base), 0); |
| 4505 | } |
| 4506 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4507 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4508 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4509 | if (IS_I830(dev_priv)) { |
| 4510 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4511 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4512 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4513 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4514 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4515 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4516 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4517 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4518 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4519 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4520 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4524 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4525 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4526 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4527 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4528 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4529 | int err; |
| 4530 | |
| 4531 | for_each_engine(engine, i915, id) { |
| 4532 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4533 | if (err) { |
| 4534 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 4535 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4536 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4537 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
Chris Wilson | 2d5eaad | 2019-02-26 10:24:00 +0000 | [diff] [blame] | 4540 | intel_engines_set_scheduler_caps(i915); |
| 4541 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4542 | return 0; |
| 4543 | } |
| 4544 | |
| 4545 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4546 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4547 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4548 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4549 | dev_priv->gt.last_init_time = ktime_get(); |
| 4550 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4551 | /* Double layer security blanket, see i915_gem_init() */ |
| 4552 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 4553 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4554 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4555 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4556 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4557 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4558 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4559 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4560 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4561 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4562 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4563 | /* ...and determine whether they are sticking. */ |
| 4564 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 4565 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4566 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4567 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4568 | /* |
| 4569 | * At least 830 can leave some of the unused rings |
| 4570 | * "active" (ie. head != tail) after resume which |
| 4571 | * will prevent c3 entry. Makes sure all unused rings |
| 4572 | * are totally idle. |
| 4573 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4574 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4575 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4576 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4577 | ret = i915_terminally_wedged(dev_priv); |
| 4578 | if (ret) |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4579 | goto out; |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4580 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4581 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4582 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4583 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4584 | goto out; |
| 4585 | } |
| 4586 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 4587 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 4588 | if (ret) { |
| 4589 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 4590 | goto out; |
| 4591 | } |
| 4592 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4593 | /* We can't enable contexts until all firmware is loaded */ |
| 4594 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4595 | if (ret) { |
| 4596 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4597 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4598 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4599 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4600 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 4601 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 4602 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 4603 | ret = __i915_gem_restart_engines(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4604 | if (ret) |
| 4605 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4606 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4607 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4608 | |
| 4609 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4610 | |
| 4611 | cleanup_uc: |
| 4612 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4613 | out: |
| 4614 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 4615 | |
| 4616 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4617 | } |
| 4618 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4619 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 4620 | { |
| 4621 | struct i915_gem_context *ctx; |
| 4622 | struct intel_engine_cs *engine; |
| 4623 | enum intel_engine_id id; |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4624 | int err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4625 | |
| 4626 | /* |
| 4627 | * As we reset the gpu during very early sanitisation, the current |
| 4628 | * register state on the GPU should reflect its defaults values. |
| 4629 | * We load a context onto the hw (with restore-inhibit), then switch |
| 4630 | * over to a second context to save that default register state. We |
| 4631 | * can then prime every new context with that state so they all start |
| 4632 | * from the same default HW values. |
| 4633 | */ |
| 4634 | |
| 4635 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 4636 | if (IS_ERR(ctx)) |
| 4637 | return PTR_ERR(ctx); |
| 4638 | |
| 4639 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4640 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4641 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4642 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4643 | if (IS_ERR(rq)) { |
| 4644 | err = PTR_ERR(rq); |
| 4645 | goto out_ctx; |
| 4646 | } |
| 4647 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 4648 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4649 | if (engine->init_context) |
| 4650 | err = engine->init_context(rq); |
| 4651 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 4652 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4653 | if (err) |
| 4654 | goto err_active; |
| 4655 | } |
| 4656 | |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4657 | /* Flush the default context image to memory, and enable powersaving. */ |
| 4658 | if (!load_power_context(i915)) { |
| 4659 | err = -EIO; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4660 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4661 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4662 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4663 | for_each_engine(engine, i915, id) { |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4664 | struct intel_context *ce; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4665 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4666 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4667 | |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4668 | ce = intel_context_lookup(ctx, engine); |
| 4669 | if (!ce) |
| 4670 | continue; |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4671 | |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4672 | state = ce->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4673 | if (!state) |
| 4674 | continue; |
| 4675 | |
Chris Wilson | 0881954 | 2019-03-08 13:25:22 +0000 | [diff] [blame] | 4676 | GEM_BUG_ON(intel_context_is_pinned(ce)); |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4677 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4678 | /* |
| 4679 | * As we will hold a reference to the logical state, it will |
| 4680 | * not be torn down with the context, and importantly the |
| 4681 | * object will hold onto its vma (making it possible for a |
| 4682 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 4683 | * from the GTT to prevent such accidents and reclaim the |
| 4684 | * space. |
| 4685 | */ |
| 4686 | err = i915_vma_unbind(state); |
| 4687 | if (err) |
| 4688 | goto err_active; |
| 4689 | |
| 4690 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 4691 | if (err) |
| 4692 | goto err_active; |
| 4693 | |
| 4694 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4695 | |
| 4696 | /* Check we can acquire the image of the context state */ |
| 4697 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4698 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4699 | if (IS_ERR(vaddr)) { |
| 4700 | err = PTR_ERR(vaddr); |
| 4701 | goto err_active; |
| 4702 | } |
| 4703 | |
| 4704 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 4708 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 4709 | |
| 4710 | /* |
| 4711 | * Make sure that classes with multiple engine instances all |
| 4712 | * share the same basic configuration. |
| 4713 | */ |
| 4714 | for_each_engine(engine, i915, id) { |
| 4715 | unsigned int bit = BIT(engine->uabi_class); |
| 4716 | unsigned int expected = engine->default_state ? bit : 0; |
| 4717 | |
| 4718 | if ((found & bit) != expected) { |
| 4719 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 4720 | engine->uabi_class, engine->name); |
| 4721 | } |
| 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | out_ctx: |
| 4726 | i915_gem_context_set_closed(ctx); |
| 4727 | i915_gem_context_put(ctx); |
| 4728 | return err; |
| 4729 | |
| 4730 | err_active: |
| 4731 | /* |
| 4732 | * If we have to abandon now, we expect the engines to be idle |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4733 | * and ready to be torn-down. The quickest way we can accomplish |
| 4734 | * this is by declaring ourselves wedged. |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4735 | */ |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4736 | i915_gem_set_wedged(i915); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4737 | goto out_ctx; |
| 4738 | } |
| 4739 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4740 | static int |
| 4741 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 4742 | { |
| 4743 | struct drm_i915_gem_object *obj; |
| 4744 | struct i915_vma *vma; |
| 4745 | int ret; |
| 4746 | |
| 4747 | obj = i915_gem_object_create_stolen(i915, size); |
| 4748 | if (!obj) |
| 4749 | obj = i915_gem_object_create_internal(i915, size); |
| 4750 | if (IS_ERR(obj)) { |
| 4751 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 4752 | return PTR_ERR(obj); |
| 4753 | } |
| 4754 | |
| 4755 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 4756 | if (IS_ERR(vma)) { |
| 4757 | ret = PTR_ERR(vma); |
| 4758 | goto err_unref; |
| 4759 | } |
| 4760 | |
| 4761 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 4762 | if (ret) |
| 4763 | goto err_unref; |
| 4764 | |
| 4765 | i915->gt.scratch = vma; |
| 4766 | return 0; |
| 4767 | |
| 4768 | err_unref: |
| 4769 | i915_gem_object_put(obj); |
| 4770 | return ret; |
| 4771 | } |
| 4772 | |
| 4773 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 4774 | { |
| 4775 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 4776 | } |
| 4777 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4778 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4779 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4780 | int ret; |
| 4781 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 4782 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 4783 | 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] | 4784 | mkwrite_device_info(dev_priv)->page_sizes = |
| 4785 | I915_GTT_PAGE_SIZE_4K; |
| 4786 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 4787 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4788 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4789 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 4790 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 4791 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4792 | } else { |
| 4793 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 4794 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 4795 | } |
| 4796 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4797 | i915_timelines_init(dev_priv); |
| 4798 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4799 | ret = i915_gem_init_userptr(dev_priv); |
| 4800 | if (ret) |
| 4801 | return ret; |
| 4802 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4803 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 4804 | if (ret) |
| 4805 | return ret; |
| 4806 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4807 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 4808 | if (ret) |
| 4809 | goto err_uc_misc; |
| 4810 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4811 | /* This is just a security blanket to placate dragons. |
| 4812 | * On some systems, we very sporadically observe that the first TLBs |
| 4813 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 4814 | * we hold the forcewake during initialisation these problems |
| 4815 | * just magically go away. |
| 4816 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4817 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4818 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 4819 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 4820 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4821 | if (ret) { |
| 4822 | GEM_BUG_ON(ret == -EIO); |
| 4823 | goto err_unlock; |
| 4824 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 4825 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4826 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4827 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4828 | if (ret) { |
| 4829 | GEM_BUG_ON(ret == -EIO); |
| 4830 | goto err_ggtt; |
| 4831 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 4832 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4833 | ret = i915_gem_contexts_init(dev_priv); |
| 4834 | if (ret) { |
| 4835 | GEM_BUG_ON(ret == -EIO); |
| 4836 | goto err_scratch; |
| 4837 | } |
| 4838 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4839 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4840 | if (ret) { |
| 4841 | GEM_BUG_ON(ret == -EIO); |
| 4842 | goto err_context; |
| 4843 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 4844 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 4845 | intel_init_gt_powersave(dev_priv); |
| 4846 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4847 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4848 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4849 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4850 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4851 | ret = i915_gem_init_hw(dev_priv); |
| 4852 | if (ret) |
| 4853 | goto err_uc_init; |
| 4854 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4855 | /* |
| 4856 | * Despite its name intel_init_clock_gating applies both display |
| 4857 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 4858 | * GT power context workaround. Worse, sometimes it includes a context |
| 4859 | * register workaround which we need to apply before we record the |
| 4860 | * default HW state for all contexts. |
| 4861 | * |
| 4862 | * FIXME: break up the workarounds and apply them at the right time! |
| 4863 | */ |
| 4864 | intel_init_clock_gating(dev_priv); |
| 4865 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4866 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4867 | if (ret) |
| 4868 | goto err_init_hw; |
| 4869 | |
| 4870 | if (i915_inject_load_failure()) { |
| 4871 | ret = -ENODEV; |
| 4872 | goto err_init_hw; |
| 4873 | } |
| 4874 | |
| 4875 | if (i915_inject_load_failure()) { |
| 4876 | ret = -EIO; |
| 4877 | goto err_init_hw; |
| 4878 | } |
| 4879 | |
| 4880 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 4881 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4882 | |
| 4883 | return 0; |
| 4884 | |
| 4885 | /* |
| 4886 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 4887 | * disable GPU submission but keep KMS alive. We want to mark the |
| 4888 | * HW as irrevisibly wedged, but keep enough state around that the |
| 4889 | * driver doesn't explode during runtime. |
| 4890 | */ |
| 4891 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4892 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4893 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 4894 | i915_gem_suspend(dev_priv); |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4895 | i915_gem_suspend_late(dev_priv); |
| 4896 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 4897 | i915_gem_drain_workqueue(dev_priv); |
| 4898 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4899 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4900 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4901 | err_uc_init: |
| 4902 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4903 | err_pm: |
| 4904 | if (ret != -EIO) { |
| 4905 | intel_cleanup_gt_powersave(dev_priv); |
| 4906 | i915_gem_cleanup_engines(dev_priv); |
| 4907 | } |
| 4908 | err_context: |
| 4909 | if (ret != -EIO) |
| 4910 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4911 | err_scratch: |
| 4912 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4913 | err_ggtt: |
| 4914 | err_unlock: |
| 4915 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 4916 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4917 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4918 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4919 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 4920 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4921 | if (ret != -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4922 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4923 | i915_timelines_fini(dev_priv); |
| 4924 | } |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4925 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4926 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 4927 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 4928 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4929 | /* |
| 4930 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4931 | * wedged. But we only want to do this where the GPU is angry, |
| 4932 | * for all other failure, such as an allocation failure, bail. |
| 4933 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4934 | if (!i915_reset_failed(dev_priv)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 4935 | i915_load_error(dev_priv, |
| 4936 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4937 | i915_gem_set_wedged(dev_priv); |
| 4938 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 4939 | |
| 4940 | /* Minimal basic recovery for KMS */ |
| 4941 | ret = i915_ggtt_enable_hw(dev_priv); |
| 4942 | i915_gem_restore_gtt_mappings(dev_priv); |
| 4943 | i915_gem_restore_fences(dev_priv); |
| 4944 | intel_init_clock_gating(dev_priv); |
| 4945 | |
| 4946 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4947 | } |
| 4948 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4949 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4950 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4951 | } |
| 4952 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4953 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 4954 | { |
| 4955 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 4956 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4957 | |
| 4958 | /* Flush any outstanding unpin_work. */ |
| 4959 | i915_gem_drain_workqueue(dev_priv); |
| 4960 | |
| 4961 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 4962 | intel_uc_fini_hw(dev_priv); |
| 4963 | intel_uc_fini(dev_priv); |
| 4964 | i915_gem_cleanup_engines(dev_priv); |
| 4965 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4966 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4967 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4968 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4969 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 4970 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 4971 | intel_cleanup_gt_powersave(dev_priv); |
| 4972 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4973 | intel_uc_fini_misc(dev_priv); |
| 4974 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4975 | i915_timelines_fini(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4976 | |
| 4977 | i915_gem_drain_freed_objects(dev_priv); |
| 4978 | |
| 4979 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 4980 | } |
| 4981 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4982 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 4983 | { |
| 4984 | i915_gem_sanitize(i915); |
| 4985 | } |
| 4986 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4987 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 4988 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4989 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4990 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4991 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4992 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4993 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 4994 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4995 | } |
| 4996 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4997 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4998 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 4999 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5000 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5001 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5002 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5003 | !IS_CHERRYVIEW(dev_priv)) |
| 5004 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5005 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5006 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5007 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5008 | dev_priv->num_fence_regs = 16; |
| 5009 | else |
| 5010 | dev_priv->num_fence_regs = 8; |
| 5011 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5012 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5013 | dev_priv->num_fence_regs = |
| 5014 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5015 | |
| 5016 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5017 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5018 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5019 | |
| 5020 | fence->i915 = dev_priv; |
| 5021 | fence->id = i; |
| 5022 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5023 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5024 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5025 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5026 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5027 | } |
| 5028 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5029 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5030 | { |
| 5031 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5032 | spin_lock_init(&i915->mm.obj_lock); |
| 5033 | spin_lock_init(&i915->mm.free_lock); |
| 5034 | |
| 5035 | init_llist_head(&i915->mm.free_list); |
| 5036 | |
| 5037 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5038 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5039 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5040 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5041 | |
| 5042 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5043 | } |
| 5044 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5045 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5046 | { |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 5047 | int err; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5048 | |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5049 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 5050 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5051 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5052 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5053 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5054 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5055 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5056 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5057 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5058 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5059 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 5060 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 5061 | init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5062 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5063 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5064 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5065 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5066 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5067 | err = i915_gemfs_init(dev_priv); |
| 5068 | if (err) |
| 5069 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5070 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5071 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5072 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5073 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5074 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5075 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5076 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5077 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5078 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5079 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5080 | |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 5081 | cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
| 5082 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5083 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5084 | } |
| 5085 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5086 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5087 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5088 | /* Discard all purgeable objects, let userspace recover those as |
| 5089 | * required after resuming. |
| 5090 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5091 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5092 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5093 | return 0; |
| 5094 | } |
| 5095 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5096 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5097 | { |
| 5098 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5099 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5100 | &i915->mm.unbound_list, |
| 5101 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5102 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5103 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5104 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5105 | /* |
| 5106 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5107 | * |
| 5108 | * We need to update the domain tracking to reflect that the CPU |
| 5109 | * will be accessing all the pages to create and restore from the |
| 5110 | * hibernation, and so upon restoration those pages will be in the |
| 5111 | * CPU domain. |
| 5112 | * |
| 5113 | * To make sure the hibernation image contains the latest state, |
| 5114 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5115 | * |
| 5116 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5117 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5118 | */ |
| 5119 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5120 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 5121 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5122 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5123 | mutex_lock(&i915->drm.struct_mutex); |
| 5124 | for (phase = phases; *phase; phase++) { |
| 5125 | list_for_each_entry(obj, *phase, mm.link) |
| 5126 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5127 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5128 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5129 | |
| 5130 | return 0; |
| 5131 | } |
| 5132 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5133 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5134 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5135 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5136 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5137 | |
| 5138 | /* Clean up our request list when the client is going away, so that |
| 5139 | * later retire_requests won't dereference our soon-to-be-gone |
| 5140 | * file_priv. |
| 5141 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5142 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5143 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5144 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5145 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5146 | } |
| 5147 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5148 | 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] | 5149 | { |
| 5150 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5151 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5152 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5153 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5154 | |
| 5155 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5156 | if (!file_priv) |
| 5157 | return -ENOMEM; |
| 5158 | |
| 5159 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5160 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5161 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5162 | |
| 5163 | spin_lock_init(&file_priv->mm.lock); |
| 5164 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5165 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5166 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 5167 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5168 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5169 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5170 | if (ret) |
| 5171 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5172 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5173 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5174 | } |
| 5175 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5176 | /** |
| 5177 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5178 | * @old: current GEM buffer for the frontbuffer slots |
| 5179 | * @new: new GEM buffer for the frontbuffer slots |
| 5180 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5181 | * |
| 5182 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5183 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5184 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5185 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5186 | struct drm_i915_gem_object *new, |
| 5187 | unsigned frontbuffer_bits) |
| 5188 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5189 | /* Control of individual bits within the mask are guarded by |
| 5190 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5191 | * manipulation of individual bits. But since the bitfield as a whole |
| 5192 | * is updated using RMW, we need to use atomics in order to update |
| 5193 | * the bits. |
| 5194 | */ |
| 5195 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 5196 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5197 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5198 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5199 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5200 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5204 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5205 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5206 | } |
| 5207 | } |
| 5208 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5209 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5210 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5211 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5212 | const void *data, size_t size) |
| 5213 | { |
| 5214 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5215 | struct file *file; |
| 5216 | size_t offset; |
| 5217 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5218 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5219 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5220 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5221 | return obj; |
| 5222 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5223 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5224 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5225 | file = obj->base.filp; |
| 5226 | offset = 0; |
| 5227 | do { |
| 5228 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5229 | struct page *page; |
| 5230 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5231 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5232 | err = pagecache_write_begin(file, file->f_mapping, |
| 5233 | offset, len, 0, |
| 5234 | &page, &pgdata); |
| 5235 | if (err < 0) |
| 5236 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5237 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5238 | vaddr = kmap(page); |
| 5239 | memcpy(vaddr, data, len); |
| 5240 | kunmap(page); |
| 5241 | |
| 5242 | err = pagecache_write_end(file, file->f_mapping, |
| 5243 | offset, len, len, |
| 5244 | page, pgdata); |
| 5245 | if (err < 0) |
| 5246 | goto fail; |
| 5247 | |
| 5248 | size -= len; |
| 5249 | data += len; |
| 5250 | offset += len; |
| 5251 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5252 | |
| 5253 | return obj; |
| 5254 | |
| 5255 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5256 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5257 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5258 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5259 | |
| 5260 | struct scatterlist * |
| 5261 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5262 | unsigned int n, |
| 5263 | unsigned int *offset) |
| 5264 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5265 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5266 | struct scatterlist *sg; |
| 5267 | unsigned int idx, count; |
| 5268 | |
| 5269 | might_sleep(); |
| 5270 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5271 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5272 | |
| 5273 | /* As we iterate forward through the sg, we record each entry in a |
| 5274 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5275 | * this index previously, we will have an entry for it. |
| 5276 | * |
| 5277 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5278 | * sequential page access (where each new request is consecutive |
| 5279 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5280 | * i.e. O(1) with a large constant! |
| 5281 | */ |
| 5282 | if (n < READ_ONCE(iter->sg_idx)) |
| 5283 | goto lookup; |
| 5284 | |
| 5285 | mutex_lock(&iter->lock); |
| 5286 | |
| 5287 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5288 | * (or the subsequent) sg are fast - comparing against the last |
| 5289 | * sg is faster than going through the radixtree. |
| 5290 | */ |
| 5291 | |
| 5292 | sg = iter->sg_pos; |
| 5293 | idx = iter->sg_idx; |
| 5294 | count = __sg_page_count(sg); |
| 5295 | |
| 5296 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5297 | void *entry; |
| 5298 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5299 | int ret; |
| 5300 | |
| 5301 | /* If we cannot allocate and insert this entry, or the |
| 5302 | * individual pages from this range, cancel updating the |
| 5303 | * sg_idx so that on this lookup we are forced to linearly |
| 5304 | * scan onwards, but on future lookups we will try the |
| 5305 | * insertion again (in which case we need to be careful of |
| 5306 | * the error return reporting that we have already inserted |
| 5307 | * this index). |
| 5308 | */ |
| 5309 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5310 | if (ret && ret != -EEXIST) |
| 5311 | goto scan; |
| 5312 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5313 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5314 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5315 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5316 | if (ret && ret != -EEXIST) |
| 5317 | goto scan; |
| 5318 | } |
| 5319 | |
| 5320 | idx += count; |
| 5321 | sg = ____sg_next(sg); |
| 5322 | count = __sg_page_count(sg); |
| 5323 | } |
| 5324 | |
| 5325 | scan: |
| 5326 | iter->sg_pos = sg; |
| 5327 | iter->sg_idx = idx; |
| 5328 | |
| 5329 | mutex_unlock(&iter->lock); |
| 5330 | |
| 5331 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5332 | goto lookup; |
| 5333 | |
| 5334 | /* In case we failed to insert the entry into the radixtree, we need |
| 5335 | * to look beyond the current sg. |
| 5336 | */ |
| 5337 | while (idx + count <= n) { |
| 5338 | idx += count; |
| 5339 | sg = ____sg_next(sg); |
| 5340 | count = __sg_page_count(sg); |
| 5341 | } |
| 5342 | |
| 5343 | *offset = n - idx; |
| 5344 | return sg; |
| 5345 | |
| 5346 | lookup: |
| 5347 | rcu_read_lock(); |
| 5348 | |
| 5349 | sg = radix_tree_lookup(&iter->radix, n); |
| 5350 | GEM_BUG_ON(!sg); |
| 5351 | |
| 5352 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5353 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5354 | * to the start of that range. We will return the pointer to |
| 5355 | * the base page and the offset of this page within the |
| 5356 | * sg entry's range. |
| 5357 | */ |
| 5358 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5359 | if (unlikely(xa_is_value(sg))) { |
| 5360 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5361 | |
| 5362 | sg = radix_tree_lookup(&iter->radix, base); |
| 5363 | GEM_BUG_ON(!sg); |
| 5364 | |
| 5365 | *offset = n - base; |
| 5366 | } |
| 5367 | |
| 5368 | rcu_read_unlock(); |
| 5369 | |
| 5370 | return sg; |
| 5371 | } |
| 5372 | |
| 5373 | struct page * |
| 5374 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5375 | { |
| 5376 | struct scatterlist *sg; |
| 5377 | unsigned int offset; |
| 5378 | |
| 5379 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5380 | |
| 5381 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5382 | return nth_page(sg_page(sg), offset); |
| 5383 | } |
| 5384 | |
| 5385 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5386 | struct page * |
| 5387 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5388 | unsigned int n) |
| 5389 | { |
| 5390 | struct page *page; |
| 5391 | |
| 5392 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5393 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5394 | set_page_dirty(page); |
| 5395 | |
| 5396 | return page; |
| 5397 | } |
| 5398 | |
| 5399 | dma_addr_t |
| 5400 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5401 | unsigned long n) |
| 5402 | { |
| 5403 | struct scatterlist *sg; |
| 5404 | unsigned int offset; |
| 5405 | |
| 5406 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5407 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5408 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5409 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5410 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5411 | { |
| 5412 | struct sg_table *pages; |
| 5413 | int err; |
| 5414 | |
| 5415 | if (align > obj->base.size) |
| 5416 | return -EINVAL; |
| 5417 | |
| 5418 | if (obj->ops == &i915_gem_phys_ops) |
| 5419 | return 0; |
| 5420 | |
| 5421 | if (obj->ops != &i915_gem_object_ops) |
| 5422 | return -EINVAL; |
| 5423 | |
| 5424 | err = i915_gem_object_unbind(obj); |
| 5425 | if (err) |
| 5426 | return err; |
| 5427 | |
| 5428 | mutex_lock(&obj->mm.lock); |
| 5429 | |
| 5430 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5431 | err = -EFAULT; |
| 5432 | goto err_unlock; |
| 5433 | } |
| 5434 | |
| 5435 | if (obj->mm.quirked) { |
| 5436 | err = -EFAULT; |
| 5437 | goto err_unlock; |
| 5438 | } |
| 5439 | |
| 5440 | if (obj->mm.mapping) { |
| 5441 | err = -EBUSY; |
| 5442 | goto err_unlock; |
| 5443 | } |
| 5444 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5445 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5446 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5447 | obj->ops = &i915_gem_phys_ops; |
| 5448 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5449 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5450 | if (err) |
| 5451 | goto err_xfer; |
| 5452 | |
| 5453 | /* Perma-pin (until release) the physical set of pages */ |
| 5454 | __i915_gem_object_pin_pages(obj); |
| 5455 | |
| 5456 | if (!IS_ERR_OR_NULL(pages)) |
| 5457 | i915_gem_object_ops.put_pages(obj, pages); |
| 5458 | mutex_unlock(&obj->mm.lock); |
| 5459 | return 0; |
| 5460 | |
| 5461 | err_xfer: |
| 5462 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5463 | if (!IS_ERR_OR_NULL(pages)) { |
| 5464 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 5465 | |
| 5466 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 5467 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5468 | err_unlock: |
| 5469 | mutex_unlock(&obj->mm.lock); |
| 5470 | return err; |
| 5471 | } |
| 5472 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5473 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5474 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5475 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5476 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5477 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5478 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5479 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 5480 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5481 | #endif |