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 | ee8efa8 | 2019-03-31 10:46:20 +0100 | [diff] [blame^] | 311 | 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, |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 625 | u64 *size_p, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 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 | u32 handle; |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 630 | u64 size; |
| 631 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 632 | |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 633 | size = round_up(*size_p, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 634 | if (size == 0) |
| 635 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 636 | |
| 637 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 638 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 639 | if (IS_ERR(obj)) |
| 640 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 641 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 642 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 643 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 644 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 645 | if (ret) |
| 646 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 647 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 648 | *handle_p = handle; |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 649 | *size_p = obj->base.size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 653 | int |
| 654 | i915_gem_dumb_create(struct drm_file *file, |
| 655 | struct drm_device *dev, |
| 656 | struct drm_mode_create_dumb *args) |
| 657 | { |
| 658 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 659 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 660 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 661 | return i915_gem_create(file, to_i915(dev), |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 662 | &args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 663 | } |
| 664 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 665 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 666 | { |
| 667 | return !(obj->cache_level == I915_CACHE_NONE || |
| 668 | obj->cache_level == I915_CACHE_WT); |
| 669 | } |
| 670 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 671 | /** |
| 672 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 673 | * @dev: drm device pointer |
| 674 | * @data: ioctl data blob |
| 675 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 676 | */ |
| 677 | int |
| 678 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 679 | struct drm_file *file) |
| 680 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 681 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 682 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 683 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 684 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 685 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 686 | return i915_gem_create(file, dev_priv, |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 687 | &args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 688 | } |
| 689 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 690 | static inline enum fb_op_origin |
| 691 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 692 | { |
| 693 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 694 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 695 | } |
| 696 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 697 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 698 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 699 | intel_wakeref_t wakeref; |
| 700 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 701 | /* |
| 702 | * No actual flushing is required for the GTT write domain for reads |
| 703 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 704 | * as far as we know, so there's no chipset flush. It also doesn't |
| 705 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 706 | * |
| 707 | * However, we do have to enforce the order so that all writes through |
| 708 | * the GTT land before any writes to the device, such as updates to |
| 709 | * the GATT itself. |
| 710 | * |
| 711 | * We also have to wait a bit for the writes to land from the GTT. |
| 712 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 713 | * timing. This issue has only been observed when switching quickly |
| 714 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 715 | * 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] | 716 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 717 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 718 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 719 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 720 | wmb(); |
| 721 | |
| 722 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 723 | return; |
| 724 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 725 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 726 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 727 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 728 | spin_lock_irq(&dev_priv->uncore.lock); |
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 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 731 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 732 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 733 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static void |
| 737 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 738 | { |
| 739 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 740 | struct i915_vma *vma; |
| 741 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 742 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 743 | return; |
| 744 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 745 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 746 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 747 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 748 | |
| 749 | intel_fb_obj_flush(obj, |
| 750 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 751 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 752 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 753 | if (vma->iomap) |
| 754 | continue; |
| 755 | |
| 756 | i915_vma_unset_ggtt_write(vma); |
| 757 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 758 | break; |
| 759 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 760 | case I915_GEM_DOMAIN_WC: |
| 761 | wmb(); |
| 762 | break; |
| 763 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 764 | case I915_GEM_DOMAIN_CPU: |
| 765 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 766 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 767 | |
| 768 | case I915_GEM_DOMAIN_RENDER: |
| 769 | if (gpu_write_needs_clflush(obj)) |
| 770 | obj->cache_dirty = true; |
| 771 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 772 | } |
| 773 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 774 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 777 | /* |
| 778 | * Pins the specified object's pages and synchronizes the object with |
| 779 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 780 | * flush the object from the CPU cache. |
| 781 | */ |
| 782 | 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] | 783 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 784 | { |
| 785 | int ret; |
| 786 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 787 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 788 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 789 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 790 | if (!i915_gem_object_has_struct_page(obj)) |
| 791 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 792 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 793 | ret = i915_gem_object_wait(obj, |
| 794 | I915_WAIT_INTERRUPTIBLE | |
| 795 | I915_WAIT_LOCKED, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 796 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 797 | if (ret) |
| 798 | return ret; |
| 799 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 800 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 801 | if (ret) |
| 802 | return ret; |
| 803 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 804 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 805 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 806 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 807 | if (ret) |
| 808 | goto err_unpin; |
| 809 | else |
| 810 | goto out; |
| 811 | } |
| 812 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 813 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 814 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 815 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 816 | * read domain and manually flush cachelines (if required). This |
| 817 | * optimizes for the case when the gpu will dirty the data |
| 818 | * anyway again before the next pread happens. |
| 819 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 820 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 821 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 822 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 823 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 824 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 825 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 826 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 827 | |
| 828 | err_unpin: |
| 829 | i915_gem_object_unpin_pages(obj); |
| 830 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 834 | unsigned int *needs_clflush) |
| 835 | { |
| 836 | int ret; |
| 837 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 838 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 839 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 840 | *needs_clflush = 0; |
| 841 | if (!i915_gem_object_has_struct_page(obj)) |
| 842 | return -ENODEV; |
| 843 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 844 | ret = i915_gem_object_wait(obj, |
| 845 | I915_WAIT_INTERRUPTIBLE | |
| 846 | I915_WAIT_LOCKED | |
| 847 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 848 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 849 | if (ret) |
| 850 | return ret; |
| 851 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 852 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 853 | if (ret) |
| 854 | return ret; |
| 855 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 856 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 857 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 858 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 859 | if (ret) |
| 860 | goto err_unpin; |
| 861 | else |
| 862 | goto out; |
| 863 | } |
| 864 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 865 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 866 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 867 | /* If we're not in the cpu write domain, set ourself into the |
| 868 | * gtt write domain and manually flush cachelines (as required). |
| 869 | * This optimizes for the case when the gpu will use the data |
| 870 | * right away and we therefore have to clflush anyway. |
| 871 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 872 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 873 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 874 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 875 | /* |
| 876 | * Same trick applies to invalidate partially written |
| 877 | * cachelines read before writing. |
| 878 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 879 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 880 | *needs_clflush |= CLFLUSH_BEFORE; |
| 881 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 882 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 883 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 884 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 885 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 886 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 887 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 888 | |
| 889 | err_unpin: |
| 890 | i915_gem_object_unpin_pages(obj); |
| 891 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 894 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 895 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 896 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 897 | { |
| 898 | char *vaddr; |
| 899 | int ret; |
| 900 | |
| 901 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 902 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 903 | if (needs_clflush) |
| 904 | drm_clflush_virt_range(vaddr + offset, len); |
| 905 | |
| 906 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 907 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 908 | kunmap(page); |
| 909 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 910 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static int |
| 914 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 915 | struct drm_i915_gem_pread *args) |
| 916 | { |
| 917 | char __user *user_data; |
| 918 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 919 | unsigned int needs_clflush; |
| 920 | unsigned int idx, offset; |
| 921 | int ret; |
| 922 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 923 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 924 | if (ret) |
| 925 | return ret; |
| 926 | |
| 927 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 928 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 929 | if (ret) |
| 930 | return ret; |
| 931 | |
| 932 | remain = args->size; |
| 933 | user_data = u64_to_user_ptr(args->data_ptr); |
| 934 | offset = offset_in_page(args->offset); |
| 935 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 936 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 937 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 938 | |
| 939 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 940 | needs_clflush); |
| 941 | if (ret) |
| 942 | break; |
| 943 | |
| 944 | remain -= length; |
| 945 | user_data += length; |
| 946 | offset = 0; |
| 947 | } |
| 948 | |
| 949 | i915_gem_obj_finish_shmem_access(obj); |
| 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | static inline bool |
| 954 | gtt_user_read(struct io_mapping *mapping, |
| 955 | loff_t base, int offset, |
| 956 | char __user *user_data, int length) |
| 957 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 958 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 959 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 960 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 961 | /* 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] | 962 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 963 | unwritten = __copy_to_user_inatomic(user_data, |
| 964 | (void __force *)vaddr + offset, |
| 965 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 966 | io_mapping_unmap_atomic(vaddr); |
| 967 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 968 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 969 | unwritten = copy_to_user(user_data, |
| 970 | (void __force *)vaddr + offset, |
| 971 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 972 | io_mapping_unmap(vaddr); |
| 973 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 974 | return unwritten; |
| 975 | } |
| 976 | |
| 977 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 978 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 979 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 980 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 981 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 982 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 983 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 984 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 985 | struct i915_vma *vma; |
| 986 | void __user *user_data; |
| 987 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 988 | int ret; |
| 989 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 990 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 991 | if (ret) |
| 992 | return ret; |
| 993 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 994 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 995 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 996 | PIN_MAPPABLE | |
| 997 | PIN_NONFAULT | |
| 998 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 999 | if (!IS_ERR(vma)) { |
| 1000 | node.start = i915_ggtt_offset(vma); |
| 1001 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1002 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1003 | if (ret) { |
| 1004 | i915_vma_unpin(vma); |
| 1005 | vma = ERR_PTR(ret); |
| 1006 | } |
| 1007 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1008 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1009 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1010 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1011 | goto out_unlock; |
| 1012 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1016 | if (ret) |
| 1017 | goto out_unpin; |
| 1018 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1019 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1020 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1021 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1022 | remain = args->size; |
| 1023 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1024 | |
| 1025 | while (remain > 0) { |
| 1026 | /* Operation in this page |
| 1027 | * |
| 1028 | * page_base = page offset within aperture |
| 1029 | * page_offset = offset within page |
| 1030 | * page_length = bytes to copy for this page |
| 1031 | */ |
| 1032 | u32 page_base = node.start; |
| 1033 | unsigned page_offset = offset_in_page(offset); |
| 1034 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1035 | page_length = remain < page_length ? remain : page_length; |
| 1036 | if (node.allocated) { |
| 1037 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1038 | ggtt->vm.insert_page(&ggtt->vm, |
| 1039 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1040 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1041 | wmb(); |
| 1042 | } else { |
| 1043 | page_base += offset & PAGE_MASK; |
| 1044 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1045 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1046 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1047 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1048 | ret = -EFAULT; |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | remain -= page_length; |
| 1053 | user_data += page_length; |
| 1054 | offset += page_length; |
| 1055 | } |
| 1056 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1057 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1058 | out_unpin: |
| 1059 | if (node.allocated) { |
| 1060 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1061 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1062 | remove_mappable_node(&node); |
| 1063 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1064 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1065 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1066 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1067 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1068 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1069 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1070 | return ret; |
| 1071 | } |
| 1072 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1073 | /** |
| 1074 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1075 | * @dev: drm device pointer |
| 1076 | * @data: ioctl data blob |
| 1077 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1078 | * |
| 1079 | * On error, the contents of *data are undefined. |
| 1080 | */ |
| 1081 | int |
| 1082 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1083 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1084 | { |
| 1085 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1086 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1087 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1088 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1089 | if (args->size == 0) |
| 1090 | return 0; |
| 1091 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1092 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1093 | args->size)) |
| 1094 | return -EFAULT; |
| 1095 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1096 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1097 | if (!obj) |
| 1098 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1099 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1100 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1101 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1102 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1103 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1106 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1107 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1108 | ret = i915_gem_object_wait(obj, |
| 1109 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1110 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1111 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1112 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1113 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1114 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1115 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1116 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1117 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1118 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1119 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1120 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1121 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1122 | i915_gem_object_unpin_pages(obj); |
| 1123 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1124 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1125 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1128 | /* This is the fast write path which cannot handle |
| 1129 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1130 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1131 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1132 | static inline bool |
| 1133 | ggtt_write(struct io_mapping *mapping, |
| 1134 | loff_t base, int offset, |
| 1135 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1136 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1137 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1138 | unsigned long unwritten; |
| 1139 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1140 | /* We can use the cpu mem copy function because this is X86. */ |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1141 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1142 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1143 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1144 | io_mapping_unmap_atomic(vaddr); |
| 1145 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1146 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1147 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1148 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1149 | io_mapping_unmap(vaddr); |
| 1150 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1151 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1152 | return unwritten; |
| 1153 | } |
| 1154 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1155 | /** |
| 1156 | * This is the fast pwrite path, where we copy the data directly from the |
| 1157 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1158 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1159 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1160 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1161 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1162 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1163 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1164 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1165 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1166 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1167 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1168 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1169 | struct i915_vma *vma; |
| 1170 | u64 remain, offset; |
| 1171 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1172 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1173 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1174 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1175 | if (ret) |
| 1176 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1177 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1178 | if (i915_gem_object_has_struct_page(obj)) { |
| 1179 | /* |
| 1180 | * Avoid waking the device up if we can fallback, as |
| 1181 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1182 | * depending on PCI sleeps and our own resume time). |
| 1183 | * This easily dwarfs any performance advantage from |
| 1184 | * using the cache bypass of indirect GGTT access. |
| 1185 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1186 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1187 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1188 | ret = -EFAULT; |
| 1189 | goto out_unlock; |
| 1190 | } |
| 1191 | } else { |
| 1192 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1193 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1196 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1197 | PIN_MAPPABLE | |
| 1198 | PIN_NONFAULT | |
| 1199 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1200 | if (!IS_ERR(vma)) { |
| 1201 | node.start = i915_ggtt_offset(vma); |
| 1202 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1203 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1204 | if (ret) { |
| 1205 | i915_vma_unpin(vma); |
| 1206 | vma = ERR_PTR(ret); |
| 1207 | } |
| 1208 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1209 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1210 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1211 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1212 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1213 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1214 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1215 | |
| 1216 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1217 | if (ret) |
| 1218 | goto out_unpin; |
| 1219 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1220 | mutex_unlock(&i915->drm.struct_mutex); |
| 1221 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1222 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1223 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1224 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1225 | offset = args->offset; |
| 1226 | remain = args->size; |
| 1227 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1228 | /* Operation in this page |
| 1229 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1230 | * page_base = page offset within aperture |
| 1231 | * page_offset = offset within page |
| 1232 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1233 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1234 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1235 | unsigned int page_offset = offset_in_page(offset); |
| 1236 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1237 | page_length = remain < page_length ? remain : page_length; |
| 1238 | if (node.allocated) { |
| 1239 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1240 | ggtt->vm.insert_page(&ggtt->vm, |
| 1241 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1242 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1243 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1244 | } else { |
| 1245 | page_base += offset & PAGE_MASK; |
| 1246 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1247 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1248 | * source page isn't available. Return the error and we'll |
| 1249 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1250 | * If the object is non-shmem backed, we retry again with the |
| 1251 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1252 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1253 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1254 | user_data, page_length)) { |
| 1255 | ret = -EFAULT; |
| 1256 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1257 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1258 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1259 | remain -= page_length; |
| 1260 | user_data += page_length; |
| 1261 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1262 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1263 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1264 | |
| 1265 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1266 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1267 | if (node.allocated) { |
| 1268 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1269 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1270 | remove_mappable_node(&node); |
| 1271 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1272 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1273 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1274 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1275 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1276 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1277 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1278 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1281 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1282 | * Flushes invalid cachelines before writing to the target if |
| 1283 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1284 | * writing if needs_clflush is set. |
| 1285 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1286 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1287 | 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] | 1288 | bool needs_clflush_before, |
| 1289 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1290 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1291 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1292 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1293 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1294 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1295 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1296 | if (needs_clflush_before) |
| 1297 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1298 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1299 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1300 | if (!ret && needs_clflush_after) |
| 1301 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1302 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1303 | kunmap(page); |
| 1304 | |
| 1305 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | static int |
| 1309 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1310 | const struct drm_i915_gem_pwrite *args) |
| 1311 | { |
| 1312 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1313 | void __user *user_data; |
| 1314 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1315 | unsigned int partial_cacheline_write; |
| 1316 | unsigned int needs_clflush; |
| 1317 | unsigned int offset, idx; |
| 1318 | int ret; |
| 1319 | |
| 1320 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1321 | if (ret) |
| 1322 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1323 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1324 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1325 | mutex_unlock(&i915->drm.struct_mutex); |
| 1326 | if (ret) |
| 1327 | return ret; |
| 1328 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1329 | /* If we don't overwrite a cacheline completely we need to be |
| 1330 | * careful to have up-to-date data by first clflushing. Don't |
| 1331 | * overcomplicate things and flush the entire patch. |
| 1332 | */ |
| 1333 | partial_cacheline_write = 0; |
| 1334 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1335 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1336 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1337 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1338 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1339 | offset = offset_in_page(args->offset); |
| 1340 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1341 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1342 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1343 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1344 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1345 | (offset | length) & partial_cacheline_write, |
| 1346 | needs_clflush & CLFLUSH_AFTER); |
| 1347 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1348 | break; |
| 1349 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1350 | remain -= length; |
| 1351 | user_data += length; |
| 1352 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1355 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1356 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1357 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | /** |
| 1361 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1362 | * @dev: drm device |
| 1363 | * @data: ioctl data blob |
| 1364 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1365 | * |
| 1366 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1367 | */ |
| 1368 | int |
| 1369 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1370 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1371 | { |
| 1372 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1373 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1374 | int ret; |
| 1375 | |
| 1376 | if (args->size == 0) |
| 1377 | return 0; |
| 1378 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1379 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1380 | return -EFAULT; |
| 1381 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1382 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1383 | if (!obj) |
| 1384 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1385 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1386 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1387 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1388 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1389 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1392 | /* Writes not allowed into this read-only object */ |
| 1393 | if (i915_gem_object_is_readonly(obj)) { |
| 1394 | ret = -EINVAL; |
| 1395 | goto err; |
| 1396 | } |
| 1397 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1398 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1399 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1400 | ret = -ENODEV; |
| 1401 | if (obj->ops->pwrite) |
| 1402 | ret = obj->ops->pwrite(obj, args); |
| 1403 | if (ret != -ENODEV) |
| 1404 | goto err; |
| 1405 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1406 | ret = i915_gem_object_wait(obj, |
| 1407 | I915_WAIT_INTERRUPTIBLE | |
| 1408 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1409 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1410 | if (ret) |
| 1411 | goto err; |
| 1412 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1413 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1414 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1415 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1416 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1417 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1418 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1419 | * it would end up going through the fenced access, and we'll get |
| 1420 | * different detiling behavior between reading and writing. |
| 1421 | * pread/pwrite currently are reading and writing from the CPU |
| 1422 | * perspective, requiring manual detiling by the client. |
| 1423 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1424 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1425 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1426 | /* Note that the gtt paths might fail with non-page-backed user |
| 1427 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1428 | * textures). Fallback to the shmem path in that case. |
| 1429 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1430 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1431 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1432 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1433 | if (obj->phys_handle) |
| 1434 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1435 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1436 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1437 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1438 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1439 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1440 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1441 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1442 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1445 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1446 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1447 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1448 | struct list_head *list; |
| 1449 | struct i915_vma *vma; |
| 1450 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1451 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1452 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1453 | mutex_lock(&i915->ggtt.vm.mutex); |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1454 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1455 | if (!drm_mm_node_allocated(&vma->node)) |
| 1456 | continue; |
| 1457 | |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 1458 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1459 | } |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1460 | mutex_unlock(&i915->ggtt.vm.mutex); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1461 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1462 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1463 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1464 | list_move_tail(&obj->mm.link, list); |
| 1465 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1468 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1469 | * Called when user space prepares to use an object with the CPU, either |
| 1470 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1471 | * @dev: drm device |
| 1472 | * @data: ioctl data blob |
| 1473 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1474 | */ |
| 1475 | int |
| 1476 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1477 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1478 | { |
| 1479 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1480 | struct drm_i915_gem_object *obj; |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1481 | u32 read_domains = args->read_domains; |
| 1482 | u32 write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1483 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1484 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1485 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1486 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1487 | return -EINVAL; |
| 1488 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1489 | /* |
| 1490 | * Having something in the write domain implies it's in the read |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1491 | * domain, and only that read domain. Enforce that in the request. |
| 1492 | */ |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1493 | if (write_domain && read_domains != write_domain) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1494 | return -EINVAL; |
| 1495 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1496 | if (!read_domains) |
| 1497 | return 0; |
| 1498 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1499 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1500 | if (!obj) |
| 1501 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1502 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1503 | /* |
| 1504 | * Already in the desired write domain? Nothing for us to do! |
| 1505 | * |
| 1506 | * We apply a little bit of cunning here to catch a broader set of |
| 1507 | * no-ops. If obj->write_domain is set, we must be in the same |
| 1508 | * obj->read_domains, and only that domain. Therefore, if that |
| 1509 | * obj->write_domain matches the request read_domains, we are |
| 1510 | * already in the same read/write domain and can skip the operation, |
| 1511 | * without having to further check the requested write_domain. |
| 1512 | */ |
| 1513 | if (READ_ONCE(obj->write_domain) == read_domains) { |
| 1514 | err = 0; |
| 1515 | goto out; |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * Try to flush the object off the GPU without holding the lock. |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1520 | * We will repeat the flush holding the lock in the normal manner |
| 1521 | * to catch cases where we are gazumped. |
| 1522 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1523 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1524 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1525 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1526 | (write_domain ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1527 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1528 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1529 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1530 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1531 | /* |
| 1532 | * Proxy objects do not control access to the backing storage, ergo |
| 1533 | * they cannot be used as a means to manipulate the cache domain |
| 1534 | * tracking for that backing storage. The proxy object is always |
| 1535 | * considered to be outside of any cache domain. |
| 1536 | */ |
| 1537 | if (i915_gem_object_is_proxy(obj)) { |
| 1538 | err = -ENXIO; |
| 1539 | goto out; |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1544 | * direct access in memory with previous cached writes through |
| 1545 | * shmemfs and that our cache domain tracking remains valid. |
| 1546 | * For example, if the obj->filp was moved to swap without us |
| 1547 | * being notified and releasing the pages, we would mistakenly |
| 1548 | * continue to assume that the obj remained out of the CPU cached |
| 1549 | * domain. |
| 1550 | */ |
| 1551 | err = i915_gem_object_pin_pages(obj); |
| 1552 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1553 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1554 | |
| 1555 | err = i915_mutex_lock_interruptible(dev); |
| 1556 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1557 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1558 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1559 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1560 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1561 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1562 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1563 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1564 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1565 | |
| 1566 | /* And bump the LRU for this access */ |
| 1567 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1568 | |
| 1569 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1570 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1571 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1572 | intel_fb_obj_invalidate(obj, |
| 1573 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1574 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1575 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1576 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1577 | out: |
| 1578 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1579 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | /** |
| 1583 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1584 | * @dev: drm device |
| 1585 | * @data: ioctl data blob |
| 1586 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1587 | */ |
| 1588 | int |
| 1589 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1590 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1591 | { |
| 1592 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1593 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1594 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1595 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1596 | if (!obj) |
| 1597 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1598 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1599 | /* |
| 1600 | * Proxy objects are barred from CPU access, so there is no |
| 1601 | * need to ban sw_finish as it is a nop. |
| 1602 | */ |
| 1603 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1604 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1605 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1606 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1607 | |
| 1608 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1611 | static inline bool |
| 1612 | __vma_matches(struct vm_area_struct *vma, struct file *filp, |
| 1613 | unsigned long addr, unsigned long size) |
| 1614 | { |
| 1615 | if (vma->vm_file != filp) |
| 1616 | return false; |
| 1617 | |
Tvrtko Ursulin | a90e194 | 2019-03-05 11:04:08 +0000 | [diff] [blame] | 1618 | return vma->vm_start == addr && |
| 1619 | (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1620 | } |
| 1621 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1622 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1623 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1624 | * it is mapped to. |
| 1625 | * @dev: drm device |
| 1626 | * @data: ioctl data blob |
| 1627 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1628 | * |
| 1629 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1630 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1631 | * |
| 1632 | * IMPORTANT: |
| 1633 | * |
| 1634 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1635 | * mmap support, please don't implement mmap support like here. The modern way |
| 1636 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1637 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1638 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1639 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1640 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1641 | */ |
| 1642 | int |
| 1643 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1644 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1645 | { |
| 1646 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1647 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1648 | unsigned long addr; |
| 1649 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1650 | if (args->flags & ~(I915_MMAP_WC)) |
| 1651 | return -EINVAL; |
| 1652 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1653 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1654 | return -ENODEV; |
| 1655 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1656 | obj = i915_gem_object_lookup(file, args->handle); |
| 1657 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1658 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1659 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1660 | /* prime objects have no backing filp to GEM mmap |
| 1661 | * pages from. |
| 1662 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1663 | if (!obj->base.filp) { |
Chris Wilson | 794a11c | 2019-03-14 07:58:29 +0000 | [diff] [blame] | 1664 | addr = -ENXIO; |
| 1665 | goto err; |
| 1666 | } |
| 1667 | |
| 1668 | if (range_overflows(args->offset, args->size, (u64)obj->base.size)) { |
| 1669 | addr = -EINVAL; |
| 1670 | goto err; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1671 | } |
| 1672 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1673 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1674 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1675 | args->offset); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1676 | if (IS_ERR_VALUE(addr)) |
| 1677 | goto err; |
| 1678 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1679 | if (args->flags & I915_MMAP_WC) { |
| 1680 | struct mm_struct *mm = current->mm; |
| 1681 | struct vm_area_struct *vma; |
| 1682 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1683 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | 794a11c | 2019-03-14 07:58:29 +0000 | [diff] [blame] | 1684 | addr = -EINTR; |
| 1685 | goto err; |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1686 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1687 | vma = find_vma(mm, addr); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1688 | if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1689 | vma->vm_page_prot = |
| 1690 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1691 | else |
| 1692 | addr = -ENOMEM; |
| 1693 | up_write(&mm->mmap_sem); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1694 | if (IS_ERR_VALUE(addr)) |
| 1695 | goto err; |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1696 | |
| 1697 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1698 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1699 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1700 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1701 | |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1702 | args->addr_ptr = (u64)addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1703 | return 0; |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1704 | |
| 1705 | err: |
| 1706 | i915_gem_object_put(obj); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1707 | return addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1710 | 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] | 1711 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1712 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1715 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1716 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1717 | * |
| 1718 | * A history of the GTT mmap interface: |
| 1719 | * |
| 1720 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1721 | * aligned and suitable for fencing, and still fit into the available |
| 1722 | * mappable space left by the pinned display objects. A classic problem |
| 1723 | * we called the page-fault-of-doom where we would ping-pong between |
| 1724 | * two objects that could not fit inside the GTT and so the memcpy |
| 1725 | * would page one object in at the expense of the other between every |
| 1726 | * single byte. |
| 1727 | * |
| 1728 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1729 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1730 | * object is too large for the available space (or simply too large |
| 1731 | * for the mappable aperture!), a view is created instead and faulted |
| 1732 | * into userspace. (This view is aligned and sized appropriately for |
| 1733 | * fenced access.) |
| 1734 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1735 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1736 | * delayed writes via GTT before performing direct access via WC. |
| 1737 | * |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 1738 | * 3 - Remove implicit set-domain(GTT) and synchronisation on initial |
| 1739 | * pagefault; swapin remains transparent. |
| 1740 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1741 | * Restrictions: |
| 1742 | * |
| 1743 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1744 | * hangs on some architectures, corruption on others. An attempt to service |
| 1745 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1746 | * |
| 1747 | * * the object must be able to fit into RAM (physical memory, though no |
| 1748 | * limited to the mappable aperture). |
| 1749 | * |
| 1750 | * |
| 1751 | * Caveats: |
| 1752 | * |
| 1753 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1754 | * all data to system memory. Subsequent access will not be synchronized. |
| 1755 | * |
| 1756 | * * all mappings are revoked on runtime device suspend. |
| 1757 | * |
| 1758 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1759 | * (older machines require fence register for display and blitter access |
| 1760 | * as well). Contention of the fence registers will cause the previous users |
| 1761 | * to be unmapped and any new access will generate new page faults. |
| 1762 | * |
| 1763 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1764 | * rather than the expected SIGSEGV. |
| 1765 | */ |
| 1766 | int i915_gem_mmap_gtt_version(void) |
| 1767 | { |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 1768 | return 3; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1769 | } |
| 1770 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1771 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1772 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1773 | pgoff_t page_offset, |
| 1774 | unsigned int chunk) |
| 1775 | { |
| 1776 | struct i915_ggtt_view view; |
| 1777 | |
| 1778 | if (i915_gem_object_is_tiled(obj)) |
| 1779 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1780 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1781 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1782 | view.partial.offset = rounddown(page_offset, chunk); |
| 1783 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1784 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1785 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1786 | |
| 1787 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1788 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1789 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1790 | |
| 1791 | return view; |
| 1792 | } |
| 1793 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1794 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1795 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1796 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1797 | * |
| 1798 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1799 | * from userspace. The fault handler takes care of binding the object to |
| 1800 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1801 | * only if needed based on whether the old reg is still valid or the object |
| 1802 | * is tiled) and inserting a new PTE into the faulting process. |
| 1803 | * |
| 1804 | * Note that the faulting process may involve evicting existing objects |
| 1805 | * from the GTT and/or fence registers to make room. So performance may |
| 1806 | * suffer if the GTT working set is large or there are few fence registers |
| 1807 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1808 | * |
| 1809 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1810 | * 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] | 1811 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1812 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1813 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1814 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1815 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1816 | 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] | 1817 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1818 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1819 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1820 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1821 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1822 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1823 | pgoff_t page_offset; |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 1824 | int srcu; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1825 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1826 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1827 | /* Sanity check that we allow writing into this object */ |
| 1828 | if (i915_gem_object_is_readonly(obj) && write) |
| 1829 | return VM_FAULT_SIGBUS; |
| 1830 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1831 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1832 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1833 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1834 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1835 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1836 | ret = i915_gem_object_pin_pages(obj); |
| 1837 | if (ret) |
| 1838 | goto err; |
| 1839 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1840 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1841 | |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1842 | srcu = i915_reset_trylock(dev_priv); |
| 1843 | if (srcu < 0) { |
| 1844 | ret = srcu; |
| 1845 | goto err_rpm; |
| 1846 | } |
| 1847 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1848 | ret = i915_mutex_lock_interruptible(dev); |
| 1849 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1850 | goto err_reset; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1851 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1852 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1853 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1854 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1855 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1858 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1859 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1860 | PIN_MAPPABLE | |
| 1861 | PIN_NONBLOCK | |
| 1862 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1863 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1864 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1865 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1866 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1867 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1868 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1869 | flags = PIN_MAPPABLE; |
| 1870 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1871 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1872 | |
| 1873 | /* |
| 1874 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1875 | * all hope that the hardware is able to track future writes. |
| 1876 | */ |
| 1877 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1878 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1879 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1880 | if (IS_ERR(vma) && !view.type) { |
| 1881 | flags = PIN_MAPPABLE; |
| 1882 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1883 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1884 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1885 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1886 | if (IS_ERR(vma)) { |
| 1887 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1888 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1889 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1890 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1891 | ret = i915_vma_pin_fence(vma); |
| 1892 | if (ret) |
| 1893 | goto err_unpin; |
| 1894 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1895 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1896 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1897 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1898 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1899 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1900 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1901 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1902 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1903 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1904 | /* Mark as being mmapped into userspace for later revocation */ |
| 1905 | assert_rpm_wakelock_held(dev_priv); |
| 1906 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1907 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1908 | GEM_BUG_ON(!obj->userfault_count); |
| 1909 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1910 | i915_vma_set_ggtt_write(vma); |
| 1911 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1912 | err_fence: |
| 1913 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1914 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1915 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1916 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1917 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1918 | err_reset: |
| 1919 | i915_reset_unlock(dev_priv, srcu); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1920 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1921 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1922 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1923 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1924 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1925 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1926 | /* |
| 1927 | * We eat errors when the gpu is terminally wedged to avoid |
| 1928 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1929 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1930 | * and so needs to be reported. |
| 1931 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1932 | if (!i915_terminally_wedged(dev_priv)) |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1933 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1934 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1935 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1936 | /* |
| 1937 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1938 | * handler to reset everything when re-faulting in |
| 1939 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1940 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1941 | case 0: |
| 1942 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1943 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1944 | case -EBUSY: |
| 1945 | /* |
| 1946 | * EBUSY is ok: this just means that another thread |
| 1947 | * already did the job. |
| 1948 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1949 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1950 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1951 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1952 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 1953 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1954 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1955 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1956 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1957 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1961 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 1962 | { |
| 1963 | struct i915_vma *vma; |
| 1964 | |
| 1965 | GEM_BUG_ON(!obj->userfault_count); |
| 1966 | |
| 1967 | obj->userfault_count = 0; |
| 1968 | list_del(&obj->userfault_link); |
| 1969 | drm_vma_node_unmap(&obj->base.vma_node, |
| 1970 | obj->base.dev->anon_inode->i_mapping); |
| 1971 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1972 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1973 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1974 | } |
| 1975 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1976 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1977 | * i915_gem_release_mmap - remove physical page mappings |
| 1978 | * @obj: obj in question |
| 1979 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1980 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1981 | * relinquish ownership of the pages back to the system. |
| 1982 | * |
| 1983 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1984 | * object through the GTT and then lose the fence register due to |
| 1985 | * resource pressure. Similarly if the object has been moved out of the |
| 1986 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1987 | * mapping will then trigger a page fault on the next user access, allowing |
| 1988 | * fixup by i915_gem_fault(). |
| 1989 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1990 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1991 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1992 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1993 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1994 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1995 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1996 | /* Serialisation between user GTT access and our code depends upon |
| 1997 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 1998 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1999 | * |
| 2000 | * Note that RPM complicates somewhat by adding an additional |
| 2001 | * requirement that operations to the GGTT be made holding the RPM |
| 2002 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2003 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2004 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2005 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2006 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2007 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2008 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2009 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2010 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2011 | |
| 2012 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2013 | * memory transactions from userspace before we return. The TLB |
| 2014 | * flushing implied above by changing the PTE above *should* be |
| 2015 | * sufficient, an extra barrier here just provides us with a bit |
| 2016 | * of paranoid documentation about our requirement to serialise |
| 2017 | * memory writes before touching registers / GSM. |
| 2018 | */ |
| 2019 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2020 | |
| 2021 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2022 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2025 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2026 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2027 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2028 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2029 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2030 | /* |
| 2031 | * Only called during RPM suspend. All users of the userfault_list |
| 2032 | * must be holding an RPM wakeref to ensure that this can not |
| 2033 | * run concurrently with themselves (and use the struct_mutex for |
| 2034 | * protection between themselves). |
| 2035 | */ |
| 2036 | |
| 2037 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2038 | &dev_priv->mm.userfault_list, userfault_link) |
| 2039 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2040 | |
| 2041 | /* The fence will be lost when the device powers down. If any were |
| 2042 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2043 | * down! All other fences will be reacquired by the user upon waking. |
| 2044 | */ |
| 2045 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2046 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2047 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2048 | /* Ideally we want to assert that the fence register is not |
| 2049 | * live at this point (i.e. that no piece of code will be |
| 2050 | * trying to write through fence + GTT, as that both violates |
| 2051 | * our tracking of activity and associated locking/barriers, |
| 2052 | * but also is illegal given that the hw is powered down). |
| 2053 | * |
| 2054 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2055 | * That is not sufficient, and we need a more fine-grained |
| 2056 | * tool if we want to have a sanity check here. |
| 2057 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2058 | |
| 2059 | if (!reg->vma) |
| 2060 | continue; |
| 2061 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2062 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2063 | reg->dirty = true; |
| 2064 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2065 | } |
| 2066 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2067 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2068 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2069 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2070 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2071 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2072 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2073 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2074 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2075 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2076 | /* Attempt to reap some mmap space from dead objects */ |
| 2077 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2078 | err = i915_gem_wait_for_idle(dev_priv, |
| 2079 | I915_WAIT_INTERRUPTIBLE, |
| 2080 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2081 | if (err) |
| 2082 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2083 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2084 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2085 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2086 | if (!err) |
| 2087 | break; |
| 2088 | |
| 2089 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2090 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2091 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2095 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2096 | drm_gem_free_mmap_offset(&obj->base); |
| 2097 | } |
| 2098 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2099 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2100 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2101 | struct drm_device *dev, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 2102 | u32 handle, |
| 2103 | u64 *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2104 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2105 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2106 | int ret; |
| 2107 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2108 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2109 | if (!obj) |
| 2110 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2111 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2112 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2113 | if (ret == 0) |
| 2114 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2115 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2116 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2117 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2118 | } |
| 2119 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2120 | /** |
| 2121 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2122 | * @dev: DRM device |
| 2123 | * @data: GTT mapping ioctl data |
| 2124 | * @file: GEM object info |
| 2125 | * |
| 2126 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2127 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2128 | * up so we can get faults in the handler above. |
| 2129 | * |
| 2130 | * The fault handler will take care of binding the object into the GTT |
| 2131 | * (since it may have been evicted to make room for something), allocating |
| 2132 | * a fence register, and mapping the appropriate aperture address into |
| 2133 | * userspace. |
| 2134 | */ |
| 2135 | int |
| 2136 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2137 | struct drm_file *file) |
| 2138 | { |
| 2139 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2140 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2141 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2142 | } |
| 2143 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2144 | /* Immediately discard the backing storage */ |
| 2145 | static void |
| 2146 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2147 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2148 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2149 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2150 | if (obj->base.filp == NULL) |
| 2151 | return; |
| 2152 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2153 | /* Our goal here is to return as much of the memory as |
| 2154 | * is possible back to the system as we are called from OOM. |
| 2155 | * To do this we must instruct the shmfs to drop all of its |
| 2156 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2157 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2158 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2159 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2160 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2161 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2162 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2163 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2164 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2165 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2166 | struct address_space *mapping; |
| 2167 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2168 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2169 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2170 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2171 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2172 | case I915_MADV_DONTNEED: |
| 2173 | i915_gem_object_truncate(obj); |
| 2174 | case __I915_MADV_PURGED: |
| 2175 | return; |
| 2176 | } |
| 2177 | |
| 2178 | if (obj->base.filp == NULL) |
| 2179 | return; |
| 2180 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2181 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2182 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2185 | /* |
| 2186 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2187 | * ref count of those pages. |
| 2188 | */ |
| 2189 | static void check_release_pagevec(struct pagevec *pvec) |
| 2190 | { |
| 2191 | check_move_unevictable_pages(pvec); |
| 2192 | __pagevec_release(pvec); |
| 2193 | cond_resched(); |
| 2194 | } |
| 2195 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2196 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2197 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2198 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2199 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2200 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2201 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2202 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2203 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2204 | __i915_gem_object_release_shmem(obj, pages, true); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2205 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2206 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2207 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2208 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2209 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2210 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2211 | |
| 2212 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2213 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2214 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2215 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2216 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2217 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2218 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2219 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2220 | if (!pagevec_add(&pvec, page)) |
| 2221 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2222 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2223 | if (pagevec_count(&pvec)) |
| 2224 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2225 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2226 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2227 | sg_free_table(pages); |
| 2228 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2229 | } |
| 2230 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2231 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2232 | { |
| 2233 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2234 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2235 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2236 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2237 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2238 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2239 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2242 | static struct sg_table * |
| 2243 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2244 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2245 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2246 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2247 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2248 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2249 | if (IS_ERR_OR_NULL(pages)) |
| 2250 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2251 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2252 | spin_lock(&i915->mm.obj_lock); |
| 2253 | list_del(&obj->mm.link); |
| 2254 | spin_unlock(&i915->mm.obj_lock); |
| 2255 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2256 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2257 | void *ptr; |
| 2258 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2259 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2260 | if (is_vmalloc_addr(ptr)) |
| 2261 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2262 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2263 | kunmap(kmap_to_page(ptr)); |
| 2264 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2265 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2268 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2269 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2270 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2271 | return pages; |
| 2272 | } |
| 2273 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2274 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2275 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2276 | { |
| 2277 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2278 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2279 | |
| 2280 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2281 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2282 | |
| 2283 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2284 | |
| 2285 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2286 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2287 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2288 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2289 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2290 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2291 | |
| 2292 | /* |
| 2293 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2294 | * array, hence protect them from being reaped by removing them from gtt |
| 2295 | * lists early. |
| 2296 | */ |
| 2297 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2298 | |
| 2299 | /* |
| 2300 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2301 | * NULL pages. In the future, when we have more asynchronous |
| 2302 | * get_pages backends we should be better able to handle the |
| 2303 | * cancellation of the async task in a more uniform manner. |
| 2304 | */ |
| 2305 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2306 | pages = ERR_PTR(-EINVAL); |
| 2307 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2308 | if (!IS_ERR(pages)) |
| 2309 | obj->ops->put_pages(obj, pages); |
| 2310 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2311 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2312 | unlock: |
| 2313 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2314 | |
| 2315 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2316 | } |
| 2317 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2318 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2319 | { |
| 2320 | struct sg_table new_st; |
| 2321 | struct scatterlist *sg, *new_sg; |
| 2322 | unsigned int i; |
| 2323 | |
| 2324 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2325 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2326 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2327 | if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN)) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2328 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2329 | |
| 2330 | new_sg = new_st.sgl; |
| 2331 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2332 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2333 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2334 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2335 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2336 | new_sg = sg_next(new_sg); |
| 2337 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2338 | GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */ |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2339 | |
| 2340 | sg_free_table(orig_st); |
| 2341 | |
| 2342 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2343 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2346 | static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2347 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2348 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2349 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2350 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2351 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2352 | struct sg_table *st; |
| 2353 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2354 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2355 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2356 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2357 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2358 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2359 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2360 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2361 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2362 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2363 | /* |
| 2364 | * Assert that the object is not currently in any GPU domain. As it |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2365 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2366 | * a GPU cache |
| 2367 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2368 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2369 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2370 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2371 | /* |
| 2372 | * If there's no chance of allocating enough pages for the whole |
| 2373 | * object, bail early. |
| 2374 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2375 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2376 | return -ENOMEM; |
| 2377 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2378 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2379 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2380 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2381 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2382 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2383 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2384 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2385 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2386 | } |
| 2387 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2388 | /* |
| 2389 | * Get the list of pages out of our struct file. They'll be pinned |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2390 | * at this point until we release them. |
| 2391 | * |
| 2392 | * Fail silently without starting the shrinker |
| 2393 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2394 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2395 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2396 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2397 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2398 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2399 | sg = st->sgl; |
| 2400 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2401 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2402 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2403 | const unsigned int shrink[] = { |
| 2404 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2405 | 0, |
| 2406 | }, *s = shrink; |
| 2407 | gfp_t gfp = noreclaim; |
| 2408 | |
| 2409 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2410 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2411 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 2412 | if (!IS_ERR(page)) |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2413 | break; |
| 2414 | |
| 2415 | if (!*s) { |
| 2416 | ret = PTR_ERR(page); |
| 2417 | goto err_sg; |
| 2418 | } |
| 2419 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2420 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2421 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2422 | /* |
| 2423 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2424 | * our own buffer, now let the real VM do its job and |
| 2425 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2426 | * |
| 2427 | * However, since graphics tend to be disposable, |
| 2428 | * defer the oom here by reporting the ENOMEM back |
| 2429 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2430 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2431 | if (!*s) { |
| 2432 | /* reclaim and warn, but no oom */ |
| 2433 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2434 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2435 | /* |
| 2436 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2437 | * kswapd to reclaim our pages (direct reclaim |
| 2438 | * does not effectively begin pageout of our |
| 2439 | * buffers on its own). However, direct reclaim |
| 2440 | * only waits for kswapd when under allocation |
| 2441 | * congestion. So as a result __GFP_RECLAIM is |
| 2442 | * unreliable and fails to actually reclaim our |
| 2443 | * dirty pages -- unless you try over and over |
| 2444 | * again with !__GFP_NORETRY. However, we still |
| 2445 | * want to fail this allocation rather than |
| 2446 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2447 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2448 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2449 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2450 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2451 | } while (1); |
| 2452 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2453 | if (!i || |
| 2454 | sg->length >= max_segment || |
| 2455 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2456 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2457 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2458 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2459 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2460 | st->nents++; |
| 2461 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2462 | } else { |
| 2463 | sg->length += PAGE_SIZE; |
| 2464 | } |
| 2465 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2466 | |
| 2467 | /* Check that the i965g/gm workaround works. */ |
| 2468 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2469 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2470 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2471 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2472 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2473 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2474 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2475 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2476 | i915_sg_trim(st); |
| 2477 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2478 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2479 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2480 | /* |
| 2481 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2482 | * it could not reserve enough large entries, asking |
| 2483 | * for PAGE_SIZE chunks instead may be helpful. |
| 2484 | */ |
| 2485 | if (max_segment > PAGE_SIZE) { |
| 2486 | for_each_sgt_page(page, sgt_iter, st) |
| 2487 | put_page(page); |
| 2488 | sg_free_table(st); |
| 2489 | |
| 2490 | max_segment = PAGE_SIZE; |
| 2491 | goto rebuild_st; |
| 2492 | } else { |
| 2493 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2494 | "Failed to DMA remap %lu pages\n", |
| 2495 | page_count); |
| 2496 | goto err_pages; |
| 2497 | } |
| 2498 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2499 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2500 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2501 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2502 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2503 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2504 | |
| 2505 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2506 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2507 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2508 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2509 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2510 | mapping_clear_unevictable(mapping); |
| 2511 | pagevec_init(&pvec); |
| 2512 | for_each_sgt_page(page, sgt_iter, st) { |
| 2513 | if (!pagevec_add(&pvec, page)) |
| 2514 | check_release_pagevec(&pvec); |
| 2515 | } |
| 2516 | if (pagevec_count(&pvec)) |
| 2517 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2518 | sg_free_table(st); |
| 2519 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2520 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2521 | /* |
| 2522 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2523 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2524 | * ENOMEM for a genuine allocation failure. |
| 2525 | * |
| 2526 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2527 | * space and so want to translate the error from shmemfs back to our |
| 2528 | * usual understanding of ENOMEM. |
| 2529 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2530 | if (ret == -ENOSPC) |
| 2531 | ret = -ENOMEM; |
| 2532 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2533 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2537 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2538 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2539 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2540 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2541 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2542 | int i; |
| 2543 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2544 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2545 | |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 2546 | /* Make the pages coherent with the GPU (flushing any swapin). */ |
| 2547 | if (obj->cache_dirty) { |
| 2548 | obj->write_domain = 0; |
| 2549 | if (i915_gem_object_has_struct_page(obj)) |
| 2550 | drm_clflush_sg(pages); |
| 2551 | obj->cache_dirty = false; |
| 2552 | } |
| 2553 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2554 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2555 | obj->mm.get_page.sg_idx = 0; |
| 2556 | |
| 2557 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2558 | |
| 2559 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2560 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2561 | GEM_BUG_ON(obj->mm.quirked); |
| 2562 | __i915_gem_object_pin_pages(obj); |
| 2563 | obj->mm.quirked = true; |
| 2564 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2565 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2566 | GEM_BUG_ON(!sg_page_sizes); |
| 2567 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2568 | |
| 2569 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2570 | * Calculate the supported page-sizes which fit into the given |
| 2571 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2572 | * to use opportunistically when later inserting into the GTT. For |
| 2573 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2574 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2575 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2576 | */ |
| 2577 | obj->mm.page_sizes.sg = 0; |
| 2578 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2579 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2580 | obj->mm.page_sizes.sg |= BIT(i); |
| 2581 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2582 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2583 | |
| 2584 | spin_lock(&i915->mm.obj_lock); |
| 2585 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2586 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2590 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2591 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2592 | |
| 2593 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2594 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2595 | return -EFAULT; |
| 2596 | } |
| 2597 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2598 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2599 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2600 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2601 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2604 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2605 | * 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] | 2606 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2607 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2608 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2609 | * or as the object is itself released. |
| 2610 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2611 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2612 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2613 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2614 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2615 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2616 | if (err) |
| 2617 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2618 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2619 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2620 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2621 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2622 | err = ____i915_gem_object_get_pages(obj); |
| 2623 | if (err) |
| 2624 | goto unlock; |
| 2625 | |
| 2626 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2627 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2628 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2629 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2630 | unlock: |
| 2631 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2632 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2635 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2636 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2637 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2638 | { |
| 2639 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2640 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2641 | struct sgt_iter sgt_iter; |
| 2642 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2643 | struct page *stack_pages[32]; |
| 2644 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2645 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2646 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2647 | void *addr; |
| 2648 | |
| 2649 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2650 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2651 | return kmap(sg_page(sgt->sgl)); |
| 2652 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2653 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2654 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2655 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2656 | if (!pages) |
| 2657 | return NULL; |
| 2658 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2659 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2660 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2661 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2662 | |
| 2663 | /* Check that we have the expected number of pages */ |
| 2664 | GEM_BUG_ON(i != n_pages); |
| 2665 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2666 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2667 | default: |
| 2668 | MISSING_CASE(type); |
| 2669 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2670 | case I915_MAP_WB: |
| 2671 | pgprot = PAGE_KERNEL; |
| 2672 | break; |
| 2673 | case I915_MAP_WC: |
| 2674 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2675 | break; |
| 2676 | } |
| 2677 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2678 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2679 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2680 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2681 | |
| 2682 | return addr; |
| 2683 | } |
| 2684 | |
| 2685 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2686 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2687 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2688 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2689 | enum i915_map_type has_type; |
| 2690 | bool pinned; |
| 2691 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2692 | int ret; |
| 2693 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2694 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2695 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2696 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2697 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2698 | if (ret) |
| 2699 | return ERR_PTR(ret); |
| 2700 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2701 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2702 | type &= ~I915_MAP_OVERRIDE; |
| 2703 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2704 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2705 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2706 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2707 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2708 | ret = ____i915_gem_object_get_pages(obj); |
| 2709 | if (ret) |
| 2710 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2711 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2712 | smp_mb__before_atomic(); |
| 2713 | } |
| 2714 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2715 | pinned = false; |
| 2716 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2717 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2718 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2719 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2720 | if (ptr && has_type != type) { |
| 2721 | if (pinned) { |
| 2722 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2723 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2724 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2725 | |
| 2726 | if (is_vmalloc_addr(ptr)) |
| 2727 | vunmap(ptr); |
| 2728 | else |
| 2729 | kunmap(kmap_to_page(ptr)); |
| 2730 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2731 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2732 | } |
| 2733 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2734 | if (!ptr) { |
| 2735 | ptr = i915_gem_object_map(obj, type); |
| 2736 | if (!ptr) { |
| 2737 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2738 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2739 | } |
| 2740 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2741 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2744 | out_unlock: |
| 2745 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2746 | return ptr; |
| 2747 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2748 | err_unpin: |
| 2749 | atomic_dec(&obj->mm.pages_pin_count); |
| 2750 | err_unlock: |
| 2751 | ptr = ERR_PTR(ret); |
| 2752 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2753 | } |
| 2754 | |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 2755 | void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj, |
| 2756 | unsigned long offset, |
| 2757 | unsigned long size) |
| 2758 | { |
| 2759 | enum i915_map_type has_type; |
| 2760 | void *ptr; |
| 2761 | |
| 2762 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 2763 | GEM_BUG_ON(range_overflows_t(typeof(obj->base.size), |
| 2764 | offset, size, obj->base.size)); |
| 2765 | |
| 2766 | obj->mm.dirty = true; |
| 2767 | |
| 2768 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) |
| 2769 | return; |
| 2770 | |
| 2771 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
| 2772 | if (has_type == I915_MAP_WC) |
| 2773 | return; |
| 2774 | |
| 2775 | drm_clflush_virt_range(ptr + offset, size); |
| 2776 | if (size == obj->base.size) { |
| 2777 | obj->write_domain &= ~I915_GEM_DOMAIN_CPU; |
| 2778 | obj->cache_dirty = false; |
| 2779 | } |
| 2780 | } |
| 2781 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2782 | static int |
| 2783 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2784 | const struct drm_i915_gem_pwrite *arg) |
| 2785 | { |
| 2786 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2787 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2788 | u64 remain, offset; |
| 2789 | unsigned int pg; |
| 2790 | |
| 2791 | /* Before we instantiate/pin the backing store for our use, we |
| 2792 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2793 | * the pagecache. We avoid the penalty of instantiating all the |
| 2794 | * pages, important if the user is just writing to a few and never |
| 2795 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2796 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2797 | * or clearing-before-use) before it is overwritten. |
| 2798 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2799 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2800 | return -ENODEV; |
| 2801 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2802 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2803 | return -EFAULT; |
| 2804 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2805 | /* Before the pages are instantiated the object is treated as being |
| 2806 | * in the CPU domain. The pages will be clflushed as required before |
| 2807 | * use, and we can freely write into the pages directly. If userspace |
| 2808 | * races pwrite with any other operation; corruption will ensue - |
| 2809 | * that is userspace's prerogative! |
| 2810 | */ |
| 2811 | |
| 2812 | remain = arg->size; |
| 2813 | offset = arg->offset; |
| 2814 | pg = offset_in_page(offset); |
| 2815 | |
| 2816 | do { |
| 2817 | unsigned int len, unwritten; |
| 2818 | struct page *page; |
| 2819 | void *data, *vaddr; |
| 2820 | int err; |
| 2821 | |
| 2822 | len = PAGE_SIZE - pg; |
| 2823 | if (len > remain) |
| 2824 | len = remain; |
| 2825 | |
| 2826 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2827 | offset, len, 0, |
| 2828 | &page, &data); |
| 2829 | if (err < 0) |
| 2830 | return err; |
| 2831 | |
| 2832 | vaddr = kmap(page); |
| 2833 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2834 | kunmap(page); |
| 2835 | |
| 2836 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2837 | offset, len, len - unwritten, |
| 2838 | page, data); |
| 2839 | if (err < 0) |
| 2840 | return err; |
| 2841 | |
| 2842 | if (unwritten) |
| 2843 | return -EFAULT; |
| 2844 | |
| 2845 | remain -= len; |
| 2846 | user_data += len; |
| 2847 | offset += len; |
| 2848 | pg = 0; |
| 2849 | } while (remain); |
| 2850 | |
| 2851 | return 0; |
| 2852 | } |
| 2853 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2854 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2855 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2856 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2857 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2858 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 2859 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2860 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2861 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2862 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2863 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2864 | mutex_unlock(&dev->struct_mutex); |
| 2865 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2866 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2867 | /* |
| 2868 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2869 | * We do not need to do this test under locking as in the worst-case |
| 2870 | * we queue the retire worker once too often. |
| 2871 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2872 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2873 | queue_delayed_work(dev_priv->wq, |
| 2874 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2875 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2876 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2877 | |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 2878 | static bool switch_to_kernel_context_sync(struct drm_i915_private *i915, |
| 2879 | unsigned long mask) |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2880 | { |
| 2881 | bool result = true; |
| 2882 | |
| 2883 | /* |
| 2884 | * Even if we fail to switch, give whatever is running a small chance |
| 2885 | * to save itself before we report the failure. Yes, this may be a |
| 2886 | * false positive due to e.g. ENOMEM, caveat emptor! |
| 2887 | */ |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 2888 | if (i915_gem_switch_to_kernel_context(i915, mask)) |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2889 | result = false; |
| 2890 | |
| 2891 | if (i915_gem_wait_for_idle(i915, |
| 2892 | I915_WAIT_LOCKED | |
| 2893 | I915_WAIT_FOR_IDLE_BOOST, |
| 2894 | I915_GEM_IDLE_TIMEOUT)) |
| 2895 | result = false; |
| 2896 | |
Chris Wilson | 7d6ce55 | 2019-03-08 09:36:57 +0000 | [diff] [blame] | 2897 | if (!result) { |
Chris Wilson | 831ebf1 | 2019-03-08 13:45:12 +0000 | [diff] [blame] | 2898 | if (i915_modparams.reset) { /* XXX hide warning from gem_eio */ |
| 2899 | dev_err(i915->drm.dev, |
| 2900 | "Failed to idle engines, declaring wedged!\n"); |
| 2901 | GEM_TRACE_DUMP(); |
| 2902 | } |
| 2903 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2904 | /* Forcibly cancel outstanding work and leave the gpu quiet. */ |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2905 | i915_gem_set_wedged(i915); |
| 2906 | } |
| 2907 | |
| 2908 | i915_retire_requests(i915); /* ensure we flush after wedging */ |
| 2909 | return result; |
| 2910 | } |
| 2911 | |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 2912 | static bool load_power_context(struct drm_i915_private *i915) |
| 2913 | { |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 2914 | /* Force loading the kernel context on all engines */ |
| 2915 | if (!switch_to_kernel_context_sync(i915, ALL_ENGINES)) |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 2916 | return false; |
| 2917 | |
| 2918 | /* |
| 2919 | * Immediately park the GPU so that we enable powersaving and |
| 2920 | * treat it as idle. The next time we issue a request, we will |
| 2921 | * unpark and start using the engine->pinned_default_state, otherwise |
| 2922 | * it is in limbo and an early reset may fail. |
| 2923 | */ |
| 2924 | __i915_gem_park(i915); |
| 2925 | |
| 2926 | return true; |
| 2927 | } |
| 2928 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2929 | static void |
| 2930 | i915_gem_idle_work_handler(struct work_struct *work) |
| 2931 | { |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2932 | struct drm_i915_private *i915 = |
| 2933 | container_of(work, typeof(*i915), gt.idle_work.work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2934 | bool rearm_hangcheck; |
| 2935 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2936 | if (!READ_ONCE(i915->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2937 | return; |
| 2938 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2939 | if (READ_ONCE(i915->gt.active_requests)) |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 2940 | return; |
| 2941 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2942 | rearm_hangcheck = |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2943 | cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2944 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2945 | if (!mutex_trylock(&i915->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2946 | /* Currently busy, come back later */ |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2947 | mod_delayed_work(i915->wq, |
| 2948 | &i915->gt.idle_work, |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2949 | msecs_to_jiffies(50)); |
| 2950 | goto out_rearm; |
| 2951 | } |
| 2952 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 2953 | /* |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2954 | * Flush out the last user context, leaving only the pinned |
| 2955 | * kernel context resident. Should anything unfortunate happen |
| 2956 | * while we are idle (such as the GPU being power cycled), no users |
| 2957 | * will be harmed. |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 2958 | */ |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2959 | if (!work_pending(&i915->gt.idle_work.work) && |
| 2960 | !i915->gt.active_requests) { |
| 2961 | ++i915->gt.active_requests; /* don't requeue idle */ |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 2962 | |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 2963 | switch_to_kernel_context_sync(i915, i915->gt.active_engines); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 2964 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2965 | if (!--i915->gt.active_requests) { |
| 2966 | __i915_gem_park(i915); |
| 2967 | rearm_hangcheck = false; |
| 2968 | } |
| 2969 | } |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2970 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2971 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 2972 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2973 | out_rearm: |
| 2974 | if (rearm_hangcheck) { |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 2975 | GEM_BUG_ON(!i915->gt.awake); |
| 2976 | i915_queue_hangcheck(i915); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 2977 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2980 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 2981 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2982 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2983 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 2984 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2985 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2986 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2987 | mutex_lock(&i915->drm.struct_mutex); |
| 2988 | |
| 2989 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 2990 | struct i915_gem_context *ctx = lut->ctx; |
| 2991 | struct i915_vma *vma; |
| 2992 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 2993 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2994 | if (ctx->file_priv != fpriv) |
| 2995 | continue; |
| 2996 | |
| 2997 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 2998 | GEM_BUG_ON(vma->obj != obj); |
| 2999 | |
| 3000 | /* We allow the process to have multiple handles to the same |
| 3001 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3002 | */ |
| 3003 | GEM_BUG_ON(!vma->open_count); |
| 3004 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3005 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3006 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3007 | list_del(&lut->obj_link); |
| 3008 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3009 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 3010 | i915_lut_handle_free(lut); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3011 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3012 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3013 | |
| 3014 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3017 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3018 | { |
| 3019 | if (timeout_ns < 0) |
| 3020 | return MAX_SCHEDULE_TIMEOUT; |
| 3021 | |
| 3022 | if (timeout_ns == 0) |
| 3023 | return 0; |
| 3024 | |
| 3025 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3026 | } |
| 3027 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3028 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3029 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3030 | * @dev: drm device pointer |
| 3031 | * @data: ioctl data blob |
| 3032 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3033 | * |
| 3034 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3035 | * the timeout parameter. |
| 3036 | * -ETIME: object is still busy after timeout |
| 3037 | * -ERESTARTSYS: signal interrupted the wait |
| 3038 | * -ENONENT: object doesn't exist |
| 3039 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3040 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3041 | * -ENOMEM: damn |
| 3042 | * -ENODEV: Internal IRQ fail |
| 3043 | * -E?: The add request failed |
| 3044 | * |
| 3045 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3046 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3047 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3048 | * without holding struct_mutex the object may become re-busied before this |
| 3049 | * function completes. A similar but shorter * race condition exists in the busy |
| 3050 | * ioctl |
| 3051 | */ |
| 3052 | int |
| 3053 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3054 | { |
| 3055 | struct drm_i915_gem_wait *args = data; |
| 3056 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3057 | ktime_t start; |
| 3058 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3059 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3060 | if (args->flags != 0) |
| 3061 | return -EINVAL; |
| 3062 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3063 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3064 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3065 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3066 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3067 | start = ktime_get(); |
| 3068 | |
| 3069 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 3070 | I915_WAIT_INTERRUPTIBLE | |
| 3071 | I915_WAIT_PRIORITY | |
| 3072 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3073 | to_wait_timeout(args->timeout_ns)); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3074 | |
| 3075 | if (args->timeout_ns > 0) { |
| 3076 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3077 | if (args->timeout_ns < 0) |
| 3078 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3079 | |
| 3080 | /* |
| 3081 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3082 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3083 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3084 | * |
| 3085 | * This is a regression from the timespec->ktime conversion. |
| 3086 | */ |
| 3087 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3088 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3089 | |
| 3090 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3091 | if (ret == -ETIME && args->timeout_ns) |
| 3092 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3093 | } |
| 3094 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3095 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3096 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3097 | } |
| 3098 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3099 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3100 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3101 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3102 | dev_err(i915->drm.dev, |
| 3103 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3104 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3105 | i915_gem_set_wedged(i915); |
| 3106 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | return 0; |
| 3110 | } |
| 3111 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3112 | static long |
| 3113 | wait_for_timelines(struct drm_i915_private *i915, |
| 3114 | unsigned int flags, long timeout) |
| 3115 | { |
| 3116 | struct i915_gt_timelines *gt = &i915->gt.timelines; |
| 3117 | struct i915_timeline *tl; |
| 3118 | |
| 3119 | if (!READ_ONCE(i915->gt.active_requests)) |
| 3120 | return timeout; |
| 3121 | |
| 3122 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3123 | list_for_each_entry(tl, >->active_list, link) { |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3124 | struct i915_request *rq; |
| 3125 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3126 | rq = i915_active_request_get_unlocked(&tl->last_request); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3127 | if (!rq) |
| 3128 | continue; |
| 3129 | |
| 3130 | mutex_unlock(>->mutex); |
| 3131 | |
| 3132 | /* |
| 3133 | * "Race-to-idle". |
| 3134 | * |
| 3135 | * Switching to the kernel context is often used a synchronous |
| 3136 | * step prior to idling, e.g. in suspend for flushing all |
| 3137 | * current operations to memory before sleeping. These we |
| 3138 | * want to complete as quickly as possible to avoid prolonged |
| 3139 | * stalls, so allow the gpu to boost to maximum clocks. |
| 3140 | */ |
| 3141 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3142 | gen6_rps_boost(rq); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3143 | |
| 3144 | timeout = i915_request_wait(rq, flags, timeout); |
| 3145 | i915_request_put(rq); |
| 3146 | if (timeout < 0) |
| 3147 | return timeout; |
| 3148 | |
| 3149 | /* restart after reacquiring the lock */ |
| 3150 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3151 | tl = list_entry(>->active_list, typeof(*tl), link); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3152 | } |
| 3153 | mutex_unlock(>->mutex); |
| 3154 | |
| 3155 | return timeout; |
| 3156 | } |
| 3157 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3158 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 3159 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3160 | { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3161 | GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", |
| 3162 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
| 3163 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3164 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3165 | /* If the device is asleep, we have no requests outstanding */ |
| 3166 | if (!READ_ONCE(i915->gt.awake)) |
| 3167 | return 0; |
| 3168 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3169 | timeout = wait_for_timelines(i915, flags, timeout); |
| 3170 | if (timeout < 0) |
| 3171 | return timeout; |
| 3172 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3173 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3174 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3175 | |
| 3176 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3177 | |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3178 | err = wait_for_engines(i915); |
| 3179 | if (err) |
| 3180 | return err; |
| 3181 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3182 | i915_retire_requests(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3183 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3184 | |
| 3185 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3186 | } |
| 3187 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3188 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3189 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3190 | /* |
| 3191 | * We manually flush the CPU domain so that we can override and |
| 3192 | * force the flush for the display, and perform it asyncrhonously. |
| 3193 | */ |
| 3194 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3195 | if (obj->cache_dirty) |
| 3196 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3197 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3201 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3202 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3203 | return; |
| 3204 | |
| 3205 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3206 | __i915_gem_object_flush_for_display(obj); |
| 3207 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3208 | } |
| 3209 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3210 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3211 | * Moves a single object to the WC read, and possibly write domain. |
| 3212 | * @obj: object to act on |
| 3213 | * @write: ask for write access or read only |
| 3214 | * |
| 3215 | * This function returns when the move is complete, including waiting on |
| 3216 | * flushes to occur. |
| 3217 | */ |
| 3218 | int |
| 3219 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3220 | { |
| 3221 | int ret; |
| 3222 | |
| 3223 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3224 | |
| 3225 | ret = i915_gem_object_wait(obj, |
| 3226 | I915_WAIT_INTERRUPTIBLE | |
| 3227 | I915_WAIT_LOCKED | |
| 3228 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3229 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3230 | if (ret) |
| 3231 | return ret; |
| 3232 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3233 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3234 | return 0; |
| 3235 | |
| 3236 | /* Flush and acquire obj->pages so that we are coherent through |
| 3237 | * direct access in memory with previous cached writes through |
| 3238 | * shmemfs and that our cache domain tracking remains valid. |
| 3239 | * For example, if the obj->filp was moved to swap without us |
| 3240 | * being notified and releasing the pages, we would mistakenly |
| 3241 | * continue to assume that the obj remained out of the CPU cached |
| 3242 | * domain. |
| 3243 | */ |
| 3244 | ret = i915_gem_object_pin_pages(obj); |
| 3245 | if (ret) |
| 3246 | return ret; |
| 3247 | |
| 3248 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3249 | |
| 3250 | /* Serialise direct access to this object with the barriers for |
| 3251 | * coherent writes from the GPU, by effectively invalidating the |
| 3252 | * WC domain upon first access. |
| 3253 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3254 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3255 | mb(); |
| 3256 | |
| 3257 | /* It should now be out of any other write domains, and we can update |
| 3258 | * the domain values for our changes. |
| 3259 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3260 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3261 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3262 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3263 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3264 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3265 | obj->mm.dirty = true; |
| 3266 | } |
| 3267 | |
| 3268 | i915_gem_object_unpin_pages(obj); |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3273 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3274 | * @obj: object to act on |
| 3275 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3276 | * |
| 3277 | * This function returns when the move is complete, including waiting on |
| 3278 | * flushes to occur. |
| 3279 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3280 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3281 | 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] | 3282 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3283 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3284 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3285 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3286 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3287 | ret = i915_gem_object_wait(obj, |
| 3288 | I915_WAIT_INTERRUPTIBLE | |
| 3289 | I915_WAIT_LOCKED | |
| 3290 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3291 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3292 | if (ret) |
| 3293 | return ret; |
| 3294 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3295 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3296 | return 0; |
| 3297 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3298 | /* Flush and acquire obj->pages so that we are coherent through |
| 3299 | * direct access in memory with previous cached writes through |
| 3300 | * shmemfs and that our cache domain tracking remains valid. |
| 3301 | * For example, if the obj->filp was moved to swap without us |
| 3302 | * being notified and releasing the pages, we would mistakenly |
| 3303 | * continue to assume that the obj remained out of the CPU cached |
| 3304 | * domain. |
| 3305 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3306 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3307 | if (ret) |
| 3308 | return ret; |
| 3309 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3310 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3311 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3312 | /* Serialise direct access to this object with the barriers for |
| 3313 | * coherent writes from the GPU, by effectively invalidating the |
| 3314 | * GTT domain upon first access. |
| 3315 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3316 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3317 | mb(); |
| 3318 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3319 | /* It should now be out of any other write domains, and we can update |
| 3320 | * the domain values for our changes. |
| 3321 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3322 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3323 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3324 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3325 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3326 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3327 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3328 | } |
| 3329 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3330 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3331 | return 0; |
| 3332 | } |
| 3333 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3334 | /** |
| 3335 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3336 | * @obj: object to act on |
| 3337 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3338 | * |
| 3339 | * After this function returns, the object will be in the new cache-level |
| 3340 | * across all GTT and the contents of the backing storage will be coherent, |
| 3341 | * with respect to the new cache-level. In order to keep the backing storage |
| 3342 | * coherent for all users, we only allow a single cache level to be set |
| 3343 | * globally on the object and prevent it from being changed whilst the |
| 3344 | * hardware is reading from the object. That is if the object is currently |
| 3345 | * on the scanout it will be set to uncached (or equivalent display |
| 3346 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3347 | * that all direct access to the scanout remains coherent. |
| 3348 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3349 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3350 | enum i915_cache_level cache_level) |
| 3351 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3352 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3353 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3354 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3355 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3356 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3357 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3358 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3359 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3360 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3361 | * be invalid given the new cache-level. This is principally to |
| 3362 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3363 | * reading an invalid PTE on older architectures. |
| 3364 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3365 | restart: |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3366 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3367 | if (!drm_mm_node_allocated(&vma->node)) |
| 3368 | continue; |
| 3369 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3370 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3371 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3372 | return -EBUSY; |
| 3373 | } |
| 3374 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3375 | if (!i915_vma_is_closed(vma) && |
| 3376 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3377 | continue; |
| 3378 | |
| 3379 | ret = i915_vma_unbind(vma); |
| 3380 | if (ret) |
| 3381 | return ret; |
| 3382 | |
| 3383 | /* As unbinding may affect other elements in the |
| 3384 | * obj->vma_list (due to side-effects from retiring |
| 3385 | * an active vma), play safe and restart the iterator. |
| 3386 | */ |
| 3387 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3388 | } |
| 3389 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3390 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3391 | * cache-level on the PTE. We could simply unbind them all and |
| 3392 | * rebind with the correct cache-level on next use. However since |
| 3393 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3394 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3395 | * state and so involves less work. |
| 3396 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3397 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3398 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3399 | * If we wait upon the object, we know that all the bound |
| 3400 | * VMA are no longer active. |
| 3401 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3402 | ret = i915_gem_object_wait(obj, |
| 3403 | I915_WAIT_INTERRUPTIBLE | |
| 3404 | I915_WAIT_LOCKED | |
| 3405 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3406 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3407 | if (ret) |
| 3408 | return ret; |
| 3409 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3410 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3411 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3412 | /* Access to snoopable pages through the GTT is |
| 3413 | * incoherent and on some machines causes a hard |
| 3414 | * lockup. Relinquish the CPU mmaping to force |
| 3415 | * userspace to refault in the pages and we can |
| 3416 | * then double check if the GTT mapping is still |
| 3417 | * valid for that pointer access. |
| 3418 | */ |
| 3419 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3420 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3421 | /* As we no longer need a fence for GTT access, |
| 3422 | * we can relinquish it now (and so prevent having |
| 3423 | * to steal a fence from someone else on the next |
| 3424 | * fence request). Note GPU activity would have |
| 3425 | * dropped the fence as all snoopable access is |
| 3426 | * supposed to be linear. |
| 3427 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3428 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3429 | ret = i915_vma_put_fence(vma); |
| 3430 | if (ret) |
| 3431 | return ret; |
| 3432 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3433 | } else { |
| 3434 | /* We either have incoherent backing store and |
| 3435 | * so no GTT access or the architecture is fully |
| 3436 | * coherent. In such cases, existing GTT mmaps |
| 3437 | * ignore the cache bit in the PTE and we can |
| 3438 | * rewrite it without confusing the GPU or having |
| 3439 | * to force userspace to fault back in its mmaps. |
| 3440 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3441 | } |
| 3442 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3443 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3444 | if (!drm_mm_node_allocated(&vma->node)) |
| 3445 | continue; |
| 3446 | |
| 3447 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3448 | if (ret) |
| 3449 | return ret; |
| 3450 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3451 | } |
| 3452 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3453 | list_for_each_entry(vma, &obj->vma.list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3454 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3455 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3456 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3457 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3458 | return 0; |
| 3459 | } |
| 3460 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3461 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3462 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3463 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3464 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3465 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3466 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3467 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3468 | rcu_read_lock(); |
| 3469 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3470 | if (!obj) { |
| 3471 | err = -ENOENT; |
| 3472 | goto out; |
| 3473 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3474 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3475 | switch (obj->cache_level) { |
| 3476 | case I915_CACHE_LLC: |
| 3477 | case I915_CACHE_L3_LLC: |
| 3478 | args->caching = I915_CACHING_CACHED; |
| 3479 | break; |
| 3480 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3481 | case I915_CACHE_WT: |
| 3482 | args->caching = I915_CACHING_DISPLAY; |
| 3483 | break; |
| 3484 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3485 | default: |
| 3486 | args->caching = I915_CACHING_NONE; |
| 3487 | break; |
| 3488 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3489 | out: |
| 3490 | rcu_read_unlock(); |
| 3491 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3494 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3495 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3496 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3497 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3498 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3499 | struct drm_i915_gem_object *obj; |
| 3500 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3501 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3502 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3503 | switch (args->caching) { |
| 3504 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3505 | level = I915_CACHE_NONE; |
| 3506 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3507 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3508 | /* |
| 3509 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3510 | * snooped mapping may leave stale data in a corresponding CPU |
| 3511 | * cacheline, whereas normally such cachelines would get |
| 3512 | * invalidated. |
| 3513 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3514 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3515 | return -ENODEV; |
| 3516 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3517 | level = I915_CACHE_LLC; |
| 3518 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3519 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3520 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3521 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3522 | default: |
| 3523 | return -EINVAL; |
| 3524 | } |
| 3525 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3526 | obj = i915_gem_object_lookup(file, args->handle); |
| 3527 | if (!obj) |
| 3528 | return -ENOENT; |
| 3529 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3530 | /* |
| 3531 | * The caching mode of proxy object is handled by its generator, and |
| 3532 | * not allowed to be changed by userspace. |
| 3533 | */ |
| 3534 | if (i915_gem_object_is_proxy(obj)) { |
| 3535 | ret = -ENXIO; |
| 3536 | goto out; |
| 3537 | } |
| 3538 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3539 | if (obj->cache_level == level) |
| 3540 | goto out; |
| 3541 | |
| 3542 | ret = i915_gem_object_wait(obj, |
| 3543 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3544 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3545 | if (ret) |
| 3546 | goto out; |
| 3547 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3548 | ret = i915_mutex_lock_interruptible(dev); |
| 3549 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3550 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3551 | |
| 3552 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3553 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3554 | |
| 3555 | out: |
| 3556 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3557 | return ret; |
| 3558 | } |
| 3559 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3560 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 3561 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 3562 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 3563 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 3564 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3565 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3566 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3567 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3568 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3569 | const struct i915_ggtt_view *view, |
| 3570 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3571 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3572 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3573 | int ret; |
| 3574 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3575 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3576 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3577 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3578 | * display coherency whilst setting up the cache domains. |
| 3579 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3580 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3581 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3582 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3583 | * a result, we make sure that the pinning that is about to occur is |
| 3584 | * done with uncached PTEs. This is lowest common denominator for all |
| 3585 | * chipsets. |
| 3586 | * |
| 3587 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3588 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3589 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3590 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3591 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3592 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3593 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3594 | if (ret) { |
| 3595 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3596 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3597 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3598 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3599 | /* As the user may map the buffer once pinned in the display plane |
| 3600 | * (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] | 3601 | * always use map_and_fenceable for all scanout buffers. However, |
| 3602 | * it may simply be too big to fit into mappable, in which case |
| 3603 | * put it anyway and hope that userspace can cope (but always first |
| 3604 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3605 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3606 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3607 | if ((flags & PIN_MAPPABLE) == 0 && |
| 3608 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3609 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3610 | flags | |
| 3611 | PIN_MAPPABLE | |
| 3612 | PIN_NONBLOCK); |
| 3613 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3614 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3615 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3616 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3617 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 3618 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 3619 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3620 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3621 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3622 | /* It should now be out of any other write domains, and we can update |
| 3623 | * the domain values for our changes. |
| 3624 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3625 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3626 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3627 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3628 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3629 | err_unpin_global: |
| 3630 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3631 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3635 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3636 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 3637 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3638 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3639 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 3640 | return; |
| 3641 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3642 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 3643 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3644 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3645 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 3646 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3647 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3648 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3649 | } |
| 3650 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3651 | /** |
| 3652 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3653 | * @obj: object to act on |
| 3654 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3655 | * |
| 3656 | * This function returns when the move is complete, including waiting on |
| 3657 | * flushes to occur. |
| 3658 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3659 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3660 | 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] | 3661 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3662 | int ret; |
| 3663 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3664 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3665 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3666 | ret = i915_gem_object_wait(obj, |
| 3667 | I915_WAIT_INTERRUPTIBLE | |
| 3668 | I915_WAIT_LOCKED | |
| 3669 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3670 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3671 | if (ret) |
| 3672 | return ret; |
| 3673 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3674 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3675 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3676 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3677 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 3678 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3679 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | /* It should now be out of any other write domains, and we can update |
| 3683 | * the domain values for our changes. |
| 3684 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3685 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3686 | |
| 3687 | /* If we're writing through the CPU, then the GPU read domains will |
| 3688 | * need to be invalidated at next use. |
| 3689 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3690 | if (write) |
| 3691 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3692 | |
| 3693 | return 0; |
| 3694 | } |
| 3695 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3696 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3697 | * emitted over 20 msec ago. |
| 3698 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3699 | * Note that if we were to use the current jiffies each time around the loop, |
| 3700 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3701 | * render a frame was over 20ms. |
| 3702 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3703 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3704 | * relatively low latency when blocking on a particular request to finish. |
| 3705 | */ |
| 3706 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3707 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3708 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3709 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3710 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 3711 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3712 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3713 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3714 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 3715 | /* ABI: return -EIO if already wedged */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 3716 | ret = i915_terminally_wedged(dev_priv); |
| 3717 | if (ret) |
| 3718 | return ret; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3719 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3720 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3721 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3722 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3723 | break; |
| 3724 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3725 | if (target) { |
| 3726 | list_del(&target->client_link); |
| 3727 | target->file_priv = NULL; |
| 3728 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 3729 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3730 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3731 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3732 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3733 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3734 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3735 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3736 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3737 | return 0; |
| 3738 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3739 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3740 | I915_WAIT_INTERRUPTIBLE, |
| 3741 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3742 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3743 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3744 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3745 | } |
| 3746 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3747 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3748 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 3749 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 3750 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 3751 | u64 alignment, |
| 3752 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3753 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3754 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 3755 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3756 | struct i915_vma *vma; |
| 3757 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 3758 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3759 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3760 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 3761 | if (flags & PIN_MAPPABLE && |
| 3762 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3763 | /* If the required space is larger than the available |
| 3764 | * aperture, we will not able to find a slot for the |
| 3765 | * object and unbinding the object now will be in |
| 3766 | * vain. Worse, doing so may cause us to ping-pong |
| 3767 | * the object in and out of the Global GTT and |
| 3768 | * waste a lot of cycles under the mutex. |
| 3769 | */ |
| 3770 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 3771 | return ERR_PTR(-E2BIG); |
| 3772 | |
| 3773 | /* If NONBLOCK is set the caller is optimistically |
| 3774 | * trying to cache the full object within the mappable |
| 3775 | * aperture, and *must* have a fallback in place for |
| 3776 | * situations where we cannot bind the object. We |
| 3777 | * can be a little more lax here and use the fallback |
| 3778 | * more often to avoid costly migrations of ourselves |
| 3779 | * and other objects within the aperture. |
| 3780 | * |
| 3781 | * Half-the-aperture is used as a simple heuristic. |
| 3782 | * More interesting would to do search for a free |
| 3783 | * block prior to making the commitment to unbind. |
| 3784 | * That caters for the self-harm case, and with a |
| 3785 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 3786 | * we could try to minimise harm to others. |
| 3787 | */ |
| 3788 | if (flags & PIN_NONBLOCK && |
| 3789 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 3790 | return ERR_PTR(-ENOSPC); |
| 3791 | } |
| 3792 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 3793 | vma = i915_vma_instance(obj, vm, view); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 3794 | if (IS_ERR(vma)) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3795 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3796 | |
| 3797 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3798 | if (flags & PIN_NONBLOCK) { |
| 3799 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 3800 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3801 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3802 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 3803 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3804 | return ERR_PTR(-ENOSPC); |
| 3805 | } |
| 3806 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3807 | WARN(i915_vma_is_pinned(vma), |
| 3808 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3809 | " offset=%08x, req.alignment=%llx," |
| 3810 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 3811 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3812 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3813 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3814 | ret = i915_vma_unbind(vma); |
| 3815 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3816 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3817 | } |
| 3818 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3819 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 3820 | if (ret) |
| 3821 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3822 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3823 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3824 | } |
| 3825 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3826 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3827 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3828 | if (id == I915_ENGINE_CLASS_INVALID) |
| 3829 | return 0xffff0000; |
| 3830 | |
| 3831 | GEM_BUG_ON(id >= 16); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3832 | return 0x10000 << id; |
| 3833 | } |
| 3834 | |
| 3835 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 3836 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3837 | /* |
| 3838 | * The uABI guarantees an active writer is also amongst the read |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 3839 | * engines. This would be true if we accessed the activity tracking |
| 3840 | * under the lock, but as we perform the lookup of the object and |
| 3841 | * its activity locklessly we can not guarantee that the last_write |
| 3842 | * being active implies that we have set the same engine flag from |
| 3843 | * last_read - hence we always set both read and write busy for |
| 3844 | * last_write. |
| 3845 | */ |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3846 | if (id == I915_ENGINE_CLASS_INVALID) |
| 3847 | return 0xffffffff; |
| 3848 | |
| 3849 | return (id + 1) | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3850 | } |
| 3851 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3852 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3853 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3854 | unsigned int (*flag)(unsigned int id)) |
| 3855 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3856 | const struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3857 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3858 | /* |
| 3859 | * 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] | 3860 | * guarantees forward progress. We could rely on the idle worker |
| 3861 | * to eventually flush us, but to minimise latency just ask the |
| 3862 | * hardware. |
| 3863 | * |
| 3864 | * Note we only report on the status of native fences. |
| 3865 | */ |
| 3866 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3867 | return 0; |
| 3868 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3869 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3870 | rq = container_of(fence, const struct i915_request, fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3871 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3872 | return 0; |
| 3873 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3874 | return flag(rq->engine->uabi_class); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3875 | } |
| 3876 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3877 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3878 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3879 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3880 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3881 | } |
| 3882 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3883 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3884 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3885 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3886 | if (!fence) |
| 3887 | return 0; |
| 3888 | |
| 3889 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3890 | } |
| 3891 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3892 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3893 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3894 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3895 | { |
| 3896 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3897 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3898 | struct reservation_object_list *list; |
| 3899 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3900 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3901 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3902 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3903 | rcu_read_lock(); |
| 3904 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3905 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3906 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3907 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3908 | /* |
| 3909 | * A discrepancy here is that we do not report the status of |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3910 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 3911 | * a call to set-domain may still stall waiting for foreign rendering. |
| 3912 | * This also means that wait-ioctl may report an object as busy, |
| 3913 | * where busy-ioctl considers it idle. |
| 3914 | * |
| 3915 | * We trade the ability to warn of foreign fences to report on which |
| 3916 | * i915 engines are active for the object. |
| 3917 | * |
| 3918 | * Alternatively, we can trade that extra information on read/write |
| 3919 | * activity with |
| 3920 | * args->busy = |
| 3921 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 3922 | * to report the overall busyness. This is what the wait-ioctl does. |
| 3923 | * |
| 3924 | */ |
| 3925 | retry: |
| 3926 | seq = raw_read_seqcount(&obj->resv->seq); |
| 3927 | |
| 3928 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 3929 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 3930 | |
| 3931 | /* Translate shared fences to READ set of engines */ |
| 3932 | list = rcu_dereference(obj->resv->fence); |
| 3933 | if (list) { |
| 3934 | unsigned int shared_count = list->shared_count, i; |
| 3935 | |
| 3936 | for (i = 0; i < shared_count; ++i) { |
| 3937 | struct dma_fence *fence = |
| 3938 | rcu_dereference(list->shared[i]); |
| 3939 | |
| 3940 | args->busy |= busy_check_reader(fence); |
| 3941 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3942 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3943 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3944 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 3945 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 3946 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3947 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3948 | out: |
| 3949 | rcu_read_unlock(); |
| 3950 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | int |
| 3954 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 3955 | struct drm_file *file_priv) |
| 3956 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 3957 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3958 | } |
| 3959 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3960 | int |
| 3961 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 3962 | struct drm_file *file_priv) |
| 3963 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3964 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3965 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3966 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3967 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3968 | |
| 3969 | switch (args->madv) { |
| 3970 | case I915_MADV_DONTNEED: |
| 3971 | case I915_MADV_WILLNEED: |
| 3972 | break; |
| 3973 | default: |
| 3974 | return -EINVAL; |
| 3975 | } |
| 3976 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3977 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3978 | if (!obj) |
| 3979 | return -ENOENT; |
| 3980 | |
| 3981 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 3982 | if (err) |
| 3983 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3984 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 3985 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 3986 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 3987 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3988 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 3989 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3990 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3991 | obj->mm.quirked = false; |
| 3992 | } |
| 3993 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 3994 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3995 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3996 | obj->mm.quirked = true; |
| 3997 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 3998 | } |
| 3999 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4000 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4001 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4002 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4003 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4004 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4005 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4006 | i915_gem_object_truncate(obj); |
| 4007 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4008 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4009 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4010 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4011 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4012 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4013 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4014 | } |
| 4015 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4016 | static void |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4017 | frontbuffer_retire(struct i915_active_request *active, |
| 4018 | struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4019 | { |
| 4020 | struct drm_i915_gem_object *obj = |
| 4021 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4022 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4023 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4026 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4027 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4028 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4029 | mutex_init(&obj->mm.lock); |
| 4030 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4031 | spin_lock_init(&obj->vma.lock); |
| 4032 | INIT_LIST_HEAD(&obj->vma.list); |
| 4033 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4034 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4035 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4036 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4037 | init_rcu_head(&obj->rcu); |
| 4038 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4039 | obj->ops = ops; |
| 4040 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4041 | reservation_object_init(&obj->__builtin_resv); |
| 4042 | obj->resv = &obj->__builtin_resv; |
| 4043 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4044 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4045 | i915_active_request_init(&obj->frontbuffer_write, |
| 4046 | NULL, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4047 | |
| 4048 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4049 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4050 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4051 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4052 | 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] | 4053 | } |
| 4054 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4055 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4056 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4057 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4058 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4059 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4060 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4061 | |
| 4062 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4063 | }; |
| 4064 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4065 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4066 | struct drm_gem_object *obj, |
| 4067 | size_t size) |
| 4068 | { |
| 4069 | struct drm_i915_private *i915 = to_i915(dev); |
| 4070 | unsigned long flags = VM_NORESERVE; |
| 4071 | struct file *filp; |
| 4072 | |
| 4073 | drm_gem_private_object_init(dev, obj, size); |
| 4074 | |
| 4075 | if (i915->mm.gemfs) |
| 4076 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4077 | flags); |
| 4078 | else |
| 4079 | filp = shmem_file_setup("i915", size, flags); |
| 4080 | |
| 4081 | if (IS_ERR(filp)) |
| 4082 | return PTR_ERR(filp); |
| 4083 | |
| 4084 | obj->filp = filp; |
| 4085 | |
| 4086 | return 0; |
| 4087 | } |
| 4088 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4089 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4090 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4091 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4092 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4093 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4094 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4095 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4096 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4097 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4098 | /* There is a prevalence of the assumption that we fit the object's |
| 4099 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4100 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4101 | * such a local variable, please consider fixing! |
| 4102 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4103 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4104 | return ERR_PTR(-E2BIG); |
| 4105 | |
| 4106 | if (overflows_type(size, obj->base.size)) |
| 4107 | return ERR_PTR(-E2BIG); |
| 4108 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 4109 | obj = i915_gem_object_alloc(); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4110 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4111 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4112 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4113 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4114 | if (ret) |
| 4115 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4116 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4117 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4118 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4119 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4120 | mask &= ~__GFP_HIGHMEM; |
| 4121 | mask |= __GFP_DMA32; |
| 4122 | } |
| 4123 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4124 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4125 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4126 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4127 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4128 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4129 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4130 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4131 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4132 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4133 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4134 | /* 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] | 4135 | * cache) for about a 10% performance improvement |
| 4136 | * compared to uncached. Graphics requests other than |
| 4137 | * display scanout are coherent with the CPU in |
| 4138 | * accessing this cache. This means in this mode we |
| 4139 | * don't need to clflush on the CPU side, and on the |
| 4140 | * GPU side we only need to flush internal caches to |
| 4141 | * get data visible to the CPU. |
| 4142 | * |
| 4143 | * However, we maintain the display planes as UC, and so |
| 4144 | * need to rebind when first used as such. |
| 4145 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4146 | cache_level = I915_CACHE_LLC; |
| 4147 | else |
| 4148 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4149 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4150 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4151 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4152 | trace_i915_gem_object_create(obj); |
| 4153 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4154 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4155 | |
| 4156 | fail: |
| 4157 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4158 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4161 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4162 | { |
| 4163 | /* If we are the last user of the backing storage (be it shmemfs |
| 4164 | * pages or stolen etc), we know that the pages are going to be |
| 4165 | * immediately released. In this case, we can then skip copying |
| 4166 | * back the contents from the GPU. |
| 4167 | */ |
| 4168 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4169 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4170 | return false; |
| 4171 | |
| 4172 | if (obj->base.filp == NULL) |
| 4173 | return true; |
| 4174 | |
| 4175 | /* At first glance, this looks racy, but then again so would be |
| 4176 | * userspace racing mmap against close. However, the first external |
| 4177 | * reference to the filp can only be obtained through the |
| 4178 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4179 | * acquiring such a reference whilst we are in the middle of |
| 4180 | * freeing the object. |
| 4181 | */ |
| 4182 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4183 | } |
| 4184 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4185 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4186 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4187 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4188 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4189 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4190 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4191 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4192 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4193 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4194 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4195 | trace_i915_gem_object_destroy(obj); |
| 4196 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4197 | mutex_lock(&i915->drm.struct_mutex); |
| 4198 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4199 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4200 | list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4201 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4202 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4203 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4204 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4205 | GEM_BUG_ON(!list_empty(&obj->vma.list)); |
| 4206 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4207 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4208 | /* This serializes freeing with the shrinker. Since the free |
| 4209 | * is delayed, first by RCU then by the workqueue, we want the |
| 4210 | * shrinker to be able to free pages of unreferenced objects, |
| 4211 | * or else we may oom whilst there are plenty of deferred |
| 4212 | * freed objects. |
| 4213 | */ |
| 4214 | if (i915_gem_object_has_pages(obj)) { |
| 4215 | spin_lock(&i915->mm.obj_lock); |
| 4216 | list_del_init(&obj->mm.link); |
| 4217 | spin_unlock(&i915->mm.obj_lock); |
| 4218 | } |
| 4219 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4220 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4221 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4222 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4223 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4224 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4225 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4226 | |
| 4227 | if (obj->ops->release) |
| 4228 | obj->ops->release(obj); |
| 4229 | |
| 4230 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4231 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4232 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4233 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4234 | |
| 4235 | if (obj->base.import_attach) |
| 4236 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4237 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4238 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4239 | drm_gem_object_release(&obj->base); |
| 4240 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4241 | |
Andy Shevchenko | 6e514e3 | 2019-03-04 11:29:08 +0200 | [diff] [blame] | 4242 | bitmap_free(obj->bit_17); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4243 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4244 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4245 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4246 | atomic_dec(&i915->mm.free_count); |
| 4247 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4248 | if (on) |
| 4249 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4250 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4251 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4255 | { |
| 4256 | struct llist_node *freed; |
| 4257 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4258 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4259 | freed = NULL; |
| 4260 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4261 | /* Only one consumer of llist_del_first() allowed */ |
| 4262 | spin_lock(&i915->mm.free_lock); |
| 4263 | freed = llist_del_first(&i915->mm.free_list); |
| 4264 | spin_unlock(&i915->mm.free_lock); |
| 4265 | } |
| 4266 | if (unlikely(freed)) { |
| 4267 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4268 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4269 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4270 | } |
| 4271 | |
| 4272 | static void __i915_gem_free_work(struct work_struct *work) |
| 4273 | { |
| 4274 | struct drm_i915_private *i915 = |
| 4275 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4276 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4277 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4278 | /* |
| 4279 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4280 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4281 | * However, the object may also be bound into the global GTT (e.g. |
| 4282 | * older GPUs without per-process support, or for direct access through |
| 4283 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4284 | * unbound now. |
| 4285 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4286 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4287 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4288 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4289 | spin_unlock(&i915->mm.free_lock); |
| 4290 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4291 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4292 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4293 | return; |
| 4294 | |
| 4295 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4296 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4297 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4301 | { |
| 4302 | struct drm_i915_gem_object *obj = |
| 4303 | container_of(head, typeof(*obj), rcu); |
| 4304 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4305 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4306 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4307 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4308 | * it like a rcu_head from this point forwards. And we expect all |
| 4309 | * objects to be freed via this path. |
| 4310 | */ |
| 4311 | destroy_rcu_head(&obj->rcu); |
| 4312 | |
| 4313 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4314 | * Since we require blocking on struct_mutex to unbind the freed |
| 4315 | * object from the GPU before releasing resources back to the |
| 4316 | * system, we can not do that directly from the RCU callback (which may |
| 4317 | * be a softirq context), but must instead then defer that work onto a |
| 4318 | * kthread. We use the RCU callback rather than move the freed object |
| 4319 | * directly onto the work queue so that we can mix between using the |
| 4320 | * worker and performing frees directly from subsequent allocations for |
| 4321 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4322 | */ |
| 4323 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4324 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4325 | } |
| 4326 | |
| 4327 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4328 | { |
| 4329 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4330 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4331 | if (obj->mm.quirked) |
| 4332 | __i915_gem_object_unpin_pages(obj); |
| 4333 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4334 | if (discard_backing_storage(obj)) |
| 4335 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4336 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4337 | /* |
| 4338 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4339 | * read-side critical sections are complete, e.g. |
| 4340 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4341 | * lookup see i915_gem_object_lookup_rcu(). |
| 4342 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4343 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4344 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4347 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4348 | { |
| 4349 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4350 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4351 | if (!i915_gem_object_has_active_reference(obj) && |
| 4352 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4353 | i915_gem_object_set_active_reference(obj); |
| 4354 | else |
| 4355 | i915_gem_object_put(obj); |
| 4356 | } |
| 4357 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4358 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4359 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4360 | intel_wakeref_t wakeref; |
| 4361 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4362 | GEM_TRACE("\n"); |
| 4363 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4364 | wakeref = intel_runtime_pm_get(i915); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4365 | intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4366 | |
| 4367 | /* |
| 4368 | * As we have just resumed the machine and woken the device up from |
| 4369 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4370 | * back to defaults, recovering from whatever wedged state we left it |
| 4371 | * in and so worth trying to use the device once more. |
| 4372 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4373 | if (i915_terminally_wedged(i915)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4374 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4375 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4376 | /* |
| 4377 | * If we inherit context state from the BIOS or earlier occupants |
| 4378 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4379 | * try to take over. The only way to remove the earlier state |
| 4380 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4381 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4382 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4383 | */ |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 4384 | intel_engines_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4385 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4386 | intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4387 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4388 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4389 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4390 | i915_gem_contexts_lost(i915); |
| 4391 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 4394 | void i915_gem_suspend(struct drm_i915_private *i915) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4395 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4396 | intel_wakeref_t wakeref; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4397 | |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 4398 | GEM_TRACE("\n"); |
| 4399 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4400 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4401 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4402 | flush_workqueue(i915->wq); |
| 4403 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4404 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4405 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4406 | /* |
| 4407 | * We have to flush all the executing contexts to main memory so |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4408 | * that they can saved in the hibernation image. To ensure the last |
| 4409 | * context image is coherent, we have to switch away from it. That |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4410 | * leaves the i915->kernel_context still active when |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4411 | * we actually suspend, and its image in memory may not match the GPU |
| 4412 | * state. Fortunately, the kernel_context is disposable and we do |
| 4413 | * not rely on its state. |
| 4414 | */ |
Chris Wilson | c6eeb47 | 2019-03-08 09:36:56 +0000 | [diff] [blame] | 4415 | switch_to_kernel_context_sync(i915, i915->gt.active_engines); |
Chris Wilson | 01f8f33 | 2018-07-17 09:41:21 +0100 | [diff] [blame] | 4416 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4417 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4418 | i915_reset_flush(i915); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4419 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4420 | drain_delayed_work(&i915->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4421 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4422 | /* |
| 4423 | * 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] | 4424 | * repeat the flush until it is definitely idle. |
| 4425 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4426 | drain_delayed_work(&i915->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4427 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4428 | /* |
| 4429 | * Assert that we successfully flushed all the work and |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4430 | * reset the GPU back to its idle, low power state. |
| 4431 | */ |
Chris Wilson | 50b022a | 2019-03-07 10:45:30 +0000 | [diff] [blame] | 4432 | GEM_BUG_ON(i915->gt.awake); |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4433 | |
Sujaritha Sundaresan | b9d52d3 | 2019-03-21 13:38:04 -0700 | [diff] [blame] | 4434 | intel_uc_suspend(i915); |
| 4435 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4436 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | void i915_gem_suspend_late(struct drm_i915_private *i915) |
| 4440 | { |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4441 | struct drm_i915_gem_object *obj; |
| 4442 | struct list_head *phases[] = { |
| 4443 | &i915->mm.unbound_list, |
| 4444 | &i915->mm.bound_list, |
| 4445 | NULL |
| 4446 | }, **phase; |
| 4447 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4448 | /* |
| 4449 | * Neither the BIOS, ourselves or any other kernel |
| 4450 | * expects the system to be in execlists mode on startup, |
| 4451 | * so we need to reset the GPU back to legacy mode. And the only |
| 4452 | * known way to disable logical contexts is through a GPU reset. |
| 4453 | * |
| 4454 | * So in order to leave the system in a known default configuration, |
| 4455 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4456 | * clean up the GEM state tracking, flushing off the requests and |
| 4457 | * leaving the system in a known idle state. |
| 4458 | * |
| 4459 | * Note that is of the upmost importance that the GPU is idle and |
| 4460 | * all stray writes are flushed *before* we dismantle the backing |
| 4461 | * storage for the pinned objects. |
| 4462 | * |
| 4463 | * However, since we are uncertain that resetting the GPU on older |
| 4464 | * machines is a good idea, we don't - just in case it leaves the |
| 4465 | * machine in an unusable condition. |
| 4466 | */ |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4467 | |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4468 | mutex_lock(&i915->drm.struct_mutex); |
| 4469 | for (phase = phases; *phase; phase++) { |
| 4470 | list_for_each_entry(obj, *phase, mm.link) |
| 4471 | WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); |
| 4472 | } |
| 4473 | mutex_unlock(&i915->drm.struct_mutex); |
| 4474 | |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4475 | intel_uc_sanitize(i915); |
| 4476 | i915_gem_sanitize(i915); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4477 | } |
| 4478 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4479 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4480 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4481 | GEM_TRACE("\n"); |
| 4482 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4483 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4484 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4485 | mutex_lock(&i915->drm.struct_mutex); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4486 | intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4487 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4488 | i915_gem_restore_gtt_mappings(i915); |
| 4489 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4490 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4491 | /* |
| 4492 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4493 | * guarantee that the context image is complete. So let's just reset |
| 4494 | * it and start again. |
| 4495 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4496 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4497 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4498 | if (i915_gem_init_hw(i915)) |
| 4499 | goto err_wedged; |
| 4500 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 4501 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 4502 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4503 | /* Always reload a context for powersaving. */ |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4504 | if (!load_power_context(i915)) |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4505 | goto err_wedged; |
| 4506 | |
| 4507 | out_unlock: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4508 | intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4509 | mutex_unlock(&i915->drm.struct_mutex); |
| 4510 | return; |
| 4511 | |
| 4512 | err_wedged: |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4513 | if (!i915_reset_failed(i915)) { |
| 4514 | dev_err(i915->drm.dev, |
| 4515 | "Failed to re-initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4516 | i915_gem_set_wedged(i915); |
| 4517 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4518 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4519 | } |
| 4520 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4521 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4522 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4523 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4524 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4525 | return; |
| 4526 | |
| 4527 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4528 | DISP_TILE_SURFACE_SWIZZLING); |
| 4529 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4530 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4531 | return; |
| 4532 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4533 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4534 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4535 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4536 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4537 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4538 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4539 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4540 | else |
| 4541 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4542 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4543 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4544 | 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] | 4545 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4546 | I915_WRITE(RING_CTL(base), 0); |
| 4547 | I915_WRITE(RING_HEAD(base), 0); |
| 4548 | I915_WRITE(RING_TAIL(base), 0); |
| 4549 | I915_WRITE(RING_START(base), 0); |
| 4550 | } |
| 4551 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4552 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4553 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4554 | if (IS_I830(dev_priv)) { |
| 4555 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4556 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4557 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4558 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4559 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4560 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4561 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4562 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4563 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4564 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4565 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4566 | } |
| 4567 | } |
| 4568 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4569 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4570 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4571 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4572 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4573 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4574 | int err; |
| 4575 | |
| 4576 | for_each_engine(engine, i915, id) { |
| 4577 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4578 | if (err) { |
| 4579 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 4580 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4581 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4582 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
Chris Wilson | 2d5eaad | 2019-02-26 10:24:00 +0000 | [diff] [blame] | 4585 | intel_engines_set_scheduler_caps(i915); |
| 4586 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4587 | return 0; |
| 4588 | } |
| 4589 | |
| 4590 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4591 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4592 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4593 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4594 | dev_priv->gt.last_init_time = ktime_get(); |
| 4595 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4596 | /* Double layer security blanket, see i915_gem_init() */ |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4597 | intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4598 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4599 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4600 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4601 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4602 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4603 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4604 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4605 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4606 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4607 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4608 | /* ...and determine whether they are sticking. */ |
| 4609 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 4610 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4611 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4612 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4613 | /* |
| 4614 | * At least 830 can leave some of the unused rings |
| 4615 | * "active" (ie. head != tail) after resume which |
| 4616 | * will prevent c3 entry. Makes sure all unused rings |
| 4617 | * are totally idle. |
| 4618 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4619 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4620 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4621 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4622 | ret = i915_terminally_wedged(dev_priv); |
| 4623 | if (ret) |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4624 | goto out; |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4625 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4626 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4627 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4628 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4629 | goto out; |
| 4630 | } |
| 4631 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 4632 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 4633 | if (ret) { |
| 4634 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 4635 | goto out; |
| 4636 | } |
| 4637 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4638 | /* We can't enable contexts until all firmware is loaded */ |
| 4639 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4640 | if (ret) { |
| 4641 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4642 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4643 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4644 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4645 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 4646 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 4647 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 4648 | ret = __i915_gem_restart_engines(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4649 | if (ret) |
| 4650 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4651 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4652 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4653 | |
| 4654 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4655 | |
| 4656 | cleanup_uc: |
| 4657 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4658 | out: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4659 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4660 | |
| 4661 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4662 | } |
| 4663 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4664 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 4665 | { |
| 4666 | struct i915_gem_context *ctx; |
| 4667 | struct intel_engine_cs *engine; |
| 4668 | enum intel_engine_id id; |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4669 | int err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4670 | |
| 4671 | /* |
| 4672 | * As we reset the gpu during very early sanitisation, the current |
| 4673 | * register state on the GPU should reflect its defaults values. |
| 4674 | * We load a context onto the hw (with restore-inhibit), then switch |
| 4675 | * over to a second context to save that default register state. We |
| 4676 | * can then prime every new context with that state so they all start |
| 4677 | * from the same default HW values. |
| 4678 | */ |
| 4679 | |
| 4680 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 4681 | if (IS_ERR(ctx)) |
| 4682 | return PTR_ERR(ctx); |
| 4683 | |
| 4684 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4685 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4686 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4687 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4688 | if (IS_ERR(rq)) { |
| 4689 | err = PTR_ERR(rq); |
| 4690 | goto out_ctx; |
| 4691 | } |
| 4692 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 4693 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4694 | if (engine->init_context) |
| 4695 | err = engine->init_context(rq); |
| 4696 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 4697 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4698 | if (err) |
| 4699 | goto err_active; |
| 4700 | } |
| 4701 | |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4702 | /* Flush the default context image to memory, and enable powersaving. */ |
| 4703 | if (!load_power_context(i915)) { |
| 4704 | err = -EIO; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4705 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4706 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4707 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4708 | for_each_engine(engine, i915, id) { |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4709 | struct intel_context *ce; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4710 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4711 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4712 | |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4713 | ce = intel_context_lookup(ctx, engine); |
| 4714 | if (!ce) |
| 4715 | continue; |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4716 | |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4717 | state = ce->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4718 | if (!state) |
| 4719 | continue; |
| 4720 | |
Chris Wilson | 0881954 | 2019-03-08 13:25:22 +0000 | [diff] [blame] | 4721 | GEM_BUG_ON(intel_context_is_pinned(ce)); |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4722 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4723 | /* |
| 4724 | * As we will hold a reference to the logical state, it will |
| 4725 | * not be torn down with the context, and importantly the |
| 4726 | * object will hold onto its vma (making it possible for a |
| 4727 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 4728 | * from the GTT to prevent such accidents and reclaim the |
| 4729 | * space. |
| 4730 | */ |
| 4731 | err = i915_vma_unbind(state); |
| 4732 | if (err) |
| 4733 | goto err_active; |
| 4734 | |
| 4735 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 4736 | if (err) |
| 4737 | goto err_active; |
| 4738 | |
| 4739 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 4740 | i915_gem_object_set_cache_coherency(engine->default_state, |
| 4741 | I915_CACHE_LLC); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4742 | |
| 4743 | /* Check we can acquire the image of the context state */ |
| 4744 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4745 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4746 | if (IS_ERR(vaddr)) { |
| 4747 | err = PTR_ERR(vaddr); |
| 4748 | goto err_active; |
| 4749 | } |
| 4750 | |
| 4751 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 4755 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 4756 | |
| 4757 | /* |
| 4758 | * Make sure that classes with multiple engine instances all |
| 4759 | * share the same basic configuration. |
| 4760 | */ |
| 4761 | for_each_engine(engine, i915, id) { |
| 4762 | unsigned int bit = BIT(engine->uabi_class); |
| 4763 | unsigned int expected = engine->default_state ? bit : 0; |
| 4764 | |
| 4765 | if ((found & bit) != expected) { |
| 4766 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 4767 | engine->uabi_class, engine->name); |
| 4768 | } |
| 4769 | } |
| 4770 | } |
| 4771 | |
| 4772 | out_ctx: |
| 4773 | i915_gem_context_set_closed(ctx); |
| 4774 | i915_gem_context_put(ctx); |
| 4775 | return err; |
| 4776 | |
| 4777 | err_active: |
| 4778 | /* |
| 4779 | * 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] | 4780 | * and ready to be torn-down. The quickest way we can accomplish |
| 4781 | * this is by declaring ourselves wedged. |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4782 | */ |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4783 | i915_gem_set_wedged(i915); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4784 | goto out_ctx; |
| 4785 | } |
| 4786 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4787 | static int |
| 4788 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 4789 | { |
| 4790 | struct drm_i915_gem_object *obj; |
| 4791 | struct i915_vma *vma; |
| 4792 | int ret; |
| 4793 | |
| 4794 | obj = i915_gem_object_create_stolen(i915, size); |
| 4795 | if (!obj) |
| 4796 | obj = i915_gem_object_create_internal(i915, size); |
| 4797 | if (IS_ERR(obj)) { |
| 4798 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 4799 | return PTR_ERR(obj); |
| 4800 | } |
| 4801 | |
| 4802 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 4803 | if (IS_ERR(vma)) { |
| 4804 | ret = PTR_ERR(vma); |
| 4805 | goto err_unref; |
| 4806 | } |
| 4807 | |
| 4808 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 4809 | if (ret) |
| 4810 | goto err_unref; |
| 4811 | |
| 4812 | i915->gt.scratch = vma; |
| 4813 | return 0; |
| 4814 | |
| 4815 | err_unref: |
| 4816 | i915_gem_object_put(obj); |
| 4817 | return ret; |
| 4818 | } |
| 4819 | |
| 4820 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 4821 | { |
| 4822 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 4823 | } |
| 4824 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4825 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4826 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4827 | int ret; |
| 4828 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 4829 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 4830 | 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] | 4831 | mkwrite_device_info(dev_priv)->page_sizes = |
| 4832 | I915_GTT_PAGE_SIZE_4K; |
| 4833 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 4834 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4835 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4836 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 4837 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 4838 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4839 | } else { |
| 4840 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 4841 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 4842 | } |
| 4843 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4844 | i915_timelines_init(dev_priv); |
| 4845 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4846 | ret = i915_gem_init_userptr(dev_priv); |
| 4847 | if (ret) |
| 4848 | return ret; |
| 4849 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4850 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 4851 | if (ret) |
| 4852 | return ret; |
| 4853 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4854 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 4855 | if (ret) |
| 4856 | goto err_uc_misc; |
| 4857 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4858 | /* This is just a security blanket to placate dragons. |
| 4859 | * On some systems, we very sporadically observe that the first TLBs |
| 4860 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 4861 | * we hold the forcewake during initialisation these problems |
| 4862 | * just magically go away. |
| 4863 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4864 | mutex_lock(&dev_priv->drm.struct_mutex); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4865 | intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4866 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 4867 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4868 | if (ret) { |
| 4869 | GEM_BUG_ON(ret == -EIO); |
| 4870 | goto err_unlock; |
| 4871 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 4872 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4873 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4874 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4875 | if (ret) { |
| 4876 | GEM_BUG_ON(ret == -EIO); |
| 4877 | goto err_ggtt; |
| 4878 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 4879 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4880 | ret = i915_gem_contexts_init(dev_priv); |
| 4881 | if (ret) { |
| 4882 | GEM_BUG_ON(ret == -EIO); |
| 4883 | goto err_scratch; |
| 4884 | } |
| 4885 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4886 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4887 | if (ret) { |
| 4888 | GEM_BUG_ON(ret == -EIO); |
| 4889 | goto err_context; |
| 4890 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 4891 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 4892 | intel_init_gt_powersave(dev_priv); |
| 4893 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4894 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4895 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4896 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4897 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4898 | ret = i915_gem_init_hw(dev_priv); |
| 4899 | if (ret) |
| 4900 | goto err_uc_init; |
| 4901 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4902 | /* |
| 4903 | * Despite its name intel_init_clock_gating applies both display |
| 4904 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 4905 | * GT power context workaround. Worse, sometimes it includes a context |
| 4906 | * register workaround which we need to apply before we record the |
| 4907 | * default HW state for all contexts. |
| 4908 | * |
| 4909 | * FIXME: break up the workarounds and apply them at the right time! |
| 4910 | */ |
| 4911 | intel_init_clock_gating(dev_priv); |
| 4912 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4913 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4914 | if (ret) |
| 4915 | goto err_init_hw; |
| 4916 | |
| 4917 | if (i915_inject_load_failure()) { |
| 4918 | ret = -ENODEV; |
| 4919 | goto err_init_hw; |
| 4920 | } |
| 4921 | |
| 4922 | if (i915_inject_load_failure()) { |
| 4923 | ret = -EIO; |
| 4924 | goto err_init_hw; |
| 4925 | } |
| 4926 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4927 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4928 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4929 | |
| 4930 | return 0; |
| 4931 | |
| 4932 | /* |
| 4933 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 4934 | * disable GPU submission but keep KMS alive. We want to mark the |
| 4935 | * HW as irrevisibly wedged, but keep enough state around that the |
| 4936 | * driver doesn't explode during runtime. |
| 4937 | */ |
| 4938 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4939 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4940 | |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 4941 | i915_gem_suspend(dev_priv); |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4942 | i915_gem_suspend_late(dev_priv); |
| 4943 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 4944 | i915_gem_drain_workqueue(dev_priv); |
| 4945 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4946 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4947 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4948 | err_uc_init: |
| 4949 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4950 | err_pm: |
| 4951 | if (ret != -EIO) { |
| 4952 | intel_cleanup_gt_powersave(dev_priv); |
| 4953 | i915_gem_cleanup_engines(dev_priv); |
| 4954 | } |
| 4955 | err_context: |
| 4956 | if (ret != -EIO) |
| 4957 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4958 | err_scratch: |
| 4959 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4960 | err_ggtt: |
| 4961 | err_unlock: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4962 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4963 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4964 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4965 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4966 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 4967 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4968 | if (ret != -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4969 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4970 | i915_timelines_fini(dev_priv); |
| 4971 | } |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4972 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4973 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 4974 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 4975 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4976 | /* |
| 4977 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4978 | * wedged. But we only want to do this where the GPU is angry, |
| 4979 | * for all other failure, such as an allocation failure, bail. |
| 4980 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4981 | if (!i915_reset_failed(dev_priv)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 4982 | i915_load_error(dev_priv, |
| 4983 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4984 | i915_gem_set_wedged(dev_priv); |
| 4985 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 4986 | |
| 4987 | /* Minimal basic recovery for KMS */ |
| 4988 | ret = i915_ggtt_enable_hw(dev_priv); |
| 4989 | i915_gem_restore_gtt_mappings(dev_priv); |
| 4990 | i915_gem_restore_fences(dev_priv); |
| 4991 | intel_init_clock_gating(dev_priv); |
| 4992 | |
| 4993 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4994 | } |
| 4995 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4996 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4997 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4998 | } |
| 4999 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5000 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 5001 | { |
| 5002 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5003 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5004 | |
| 5005 | /* Flush any outstanding unpin_work. */ |
| 5006 | i915_gem_drain_workqueue(dev_priv); |
| 5007 | |
| 5008 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5009 | intel_uc_fini_hw(dev_priv); |
| 5010 | intel_uc_fini(dev_priv); |
| 5011 | i915_gem_cleanup_engines(dev_priv); |
| 5012 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5013 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5014 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5015 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5016 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 5017 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5018 | intel_cleanup_gt_powersave(dev_priv); |
| 5019 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5020 | intel_uc_fini_misc(dev_priv); |
| 5021 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5022 | i915_timelines_fini(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5023 | |
| 5024 | i915_gem_drain_freed_objects(dev_priv); |
| 5025 | |
| 5026 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 5027 | } |
| 5028 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5029 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5030 | { |
| 5031 | i915_gem_sanitize(i915); |
| 5032 | } |
| 5033 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5034 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5035 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5036 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5037 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5038 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5039 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5040 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5041 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5042 | } |
| 5043 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5044 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5045 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5046 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5047 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5048 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5049 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5050 | !IS_CHERRYVIEW(dev_priv)) |
| 5051 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5052 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5053 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5054 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5055 | dev_priv->num_fence_regs = 16; |
| 5056 | else |
| 5057 | dev_priv->num_fence_regs = 8; |
| 5058 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5059 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5060 | dev_priv->num_fence_regs = |
| 5061 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5062 | |
| 5063 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5064 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5065 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5066 | |
| 5067 | fence->i915 = dev_priv; |
| 5068 | fence->id = i; |
| 5069 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5070 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5071 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5072 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5073 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5074 | } |
| 5075 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5076 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5077 | { |
| 5078 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5079 | spin_lock_init(&i915->mm.obj_lock); |
| 5080 | spin_lock_init(&i915->mm.free_lock); |
| 5081 | |
| 5082 | init_llist_head(&i915->mm.free_list); |
| 5083 | |
| 5084 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5085 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5086 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5087 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5088 | |
| 5089 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5090 | } |
| 5091 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5092 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5093 | { |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 5094 | int err; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5095 | |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5096 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 5097 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5098 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5099 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5100 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5101 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5102 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5103 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5104 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5105 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5106 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 5107 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 5108 | init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5109 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5110 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5111 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5112 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5113 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5114 | err = i915_gemfs_init(dev_priv); |
| 5115 | if (err) |
| 5116 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5117 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5118 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5119 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5120 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5121 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5122 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5123 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5124 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5125 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5126 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5127 | |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 5128 | cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
| 5129 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5130 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5131 | } |
| 5132 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5133 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5134 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5135 | /* Discard all purgeable objects, let userspace recover those as |
| 5136 | * required after resuming. |
| 5137 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5138 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5139 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5140 | return 0; |
| 5141 | } |
| 5142 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5143 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5144 | { |
| 5145 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5146 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5147 | &i915->mm.unbound_list, |
| 5148 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5149 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5150 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5151 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5152 | /* |
| 5153 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5154 | * |
| 5155 | * We need to update the domain tracking to reflect that the CPU |
| 5156 | * will be accessing all the pages to create and restore from the |
| 5157 | * hibernation, and so upon restoration those pages will be in the |
| 5158 | * CPU domain. |
| 5159 | * |
| 5160 | * To make sure the hibernation image contains the latest state, |
| 5161 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5162 | * |
| 5163 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5164 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5165 | */ |
| 5166 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5167 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 5168 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5169 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5170 | mutex_lock(&i915->drm.struct_mutex); |
| 5171 | for (phase = phases; *phase; phase++) { |
| 5172 | list_for_each_entry(obj, *phase, mm.link) |
| 5173 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5174 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5175 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5176 | |
| 5177 | return 0; |
| 5178 | } |
| 5179 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5180 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5181 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5182 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5183 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5184 | |
| 5185 | /* Clean up our request list when the client is going away, so that |
| 5186 | * later retire_requests won't dereference our soon-to-be-gone |
| 5187 | * file_priv. |
| 5188 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5189 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5190 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5191 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5192 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5193 | } |
| 5194 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5195 | 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] | 5196 | { |
| 5197 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5198 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5199 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5200 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5201 | |
| 5202 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5203 | if (!file_priv) |
| 5204 | return -ENOMEM; |
| 5205 | |
| 5206 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5207 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5208 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5209 | |
| 5210 | spin_lock_init(&file_priv->mm.lock); |
| 5211 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5212 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5213 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 5214 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5215 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5216 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5217 | if (ret) |
| 5218 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5219 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5220 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5221 | } |
| 5222 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5223 | /** |
| 5224 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5225 | * @old: current GEM buffer for the frontbuffer slots |
| 5226 | * @new: new GEM buffer for the frontbuffer slots |
| 5227 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5228 | * |
| 5229 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5230 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5231 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5232 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5233 | struct drm_i915_gem_object *new, |
| 5234 | unsigned frontbuffer_bits) |
| 5235 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5236 | /* Control of individual bits within the mask are guarded by |
| 5237 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5238 | * manipulation of individual bits. But since the bitfield as a whole |
| 5239 | * is updated using RMW, we need to use atomics in order to update |
| 5240 | * the bits. |
| 5241 | */ |
| 5242 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 5243 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5244 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5245 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5246 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5247 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5251 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5252 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5253 | } |
| 5254 | } |
| 5255 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5256 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5257 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5258 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5259 | const void *data, size_t size) |
| 5260 | { |
| 5261 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5262 | struct file *file; |
| 5263 | size_t offset; |
| 5264 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5265 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5266 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5267 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5268 | return obj; |
| 5269 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5270 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5271 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5272 | file = obj->base.filp; |
| 5273 | offset = 0; |
| 5274 | do { |
| 5275 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5276 | struct page *page; |
| 5277 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5278 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5279 | err = pagecache_write_begin(file, file->f_mapping, |
| 5280 | offset, len, 0, |
| 5281 | &page, &pgdata); |
| 5282 | if (err < 0) |
| 5283 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5284 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5285 | vaddr = kmap(page); |
| 5286 | memcpy(vaddr, data, len); |
| 5287 | kunmap(page); |
| 5288 | |
| 5289 | err = pagecache_write_end(file, file->f_mapping, |
| 5290 | offset, len, len, |
| 5291 | page, pgdata); |
| 5292 | if (err < 0) |
| 5293 | goto fail; |
| 5294 | |
| 5295 | size -= len; |
| 5296 | data += len; |
| 5297 | offset += len; |
| 5298 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5299 | |
| 5300 | return obj; |
| 5301 | |
| 5302 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5303 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5304 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5305 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5306 | |
| 5307 | struct scatterlist * |
| 5308 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5309 | unsigned int n, |
| 5310 | unsigned int *offset) |
| 5311 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5312 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5313 | struct scatterlist *sg; |
| 5314 | unsigned int idx, count; |
| 5315 | |
| 5316 | might_sleep(); |
| 5317 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5318 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5319 | |
| 5320 | /* As we iterate forward through the sg, we record each entry in a |
| 5321 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5322 | * this index previously, we will have an entry for it. |
| 5323 | * |
| 5324 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5325 | * sequential page access (where each new request is consecutive |
| 5326 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5327 | * i.e. O(1) with a large constant! |
| 5328 | */ |
| 5329 | if (n < READ_ONCE(iter->sg_idx)) |
| 5330 | goto lookup; |
| 5331 | |
| 5332 | mutex_lock(&iter->lock); |
| 5333 | |
| 5334 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5335 | * (or the subsequent) sg are fast - comparing against the last |
| 5336 | * sg is faster than going through the radixtree. |
| 5337 | */ |
| 5338 | |
| 5339 | sg = iter->sg_pos; |
| 5340 | idx = iter->sg_idx; |
| 5341 | count = __sg_page_count(sg); |
| 5342 | |
| 5343 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5344 | void *entry; |
| 5345 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5346 | int ret; |
| 5347 | |
| 5348 | /* If we cannot allocate and insert this entry, or the |
| 5349 | * individual pages from this range, cancel updating the |
| 5350 | * sg_idx so that on this lookup we are forced to linearly |
| 5351 | * scan onwards, but on future lookups we will try the |
| 5352 | * insertion again (in which case we need to be careful of |
| 5353 | * the error return reporting that we have already inserted |
| 5354 | * this index). |
| 5355 | */ |
| 5356 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5357 | if (ret && ret != -EEXIST) |
| 5358 | goto scan; |
| 5359 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5360 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5361 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5362 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5363 | if (ret && ret != -EEXIST) |
| 5364 | goto scan; |
| 5365 | } |
| 5366 | |
| 5367 | idx += count; |
| 5368 | sg = ____sg_next(sg); |
| 5369 | count = __sg_page_count(sg); |
| 5370 | } |
| 5371 | |
| 5372 | scan: |
| 5373 | iter->sg_pos = sg; |
| 5374 | iter->sg_idx = idx; |
| 5375 | |
| 5376 | mutex_unlock(&iter->lock); |
| 5377 | |
| 5378 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5379 | goto lookup; |
| 5380 | |
| 5381 | /* In case we failed to insert the entry into the radixtree, we need |
| 5382 | * to look beyond the current sg. |
| 5383 | */ |
| 5384 | while (idx + count <= n) { |
| 5385 | idx += count; |
| 5386 | sg = ____sg_next(sg); |
| 5387 | count = __sg_page_count(sg); |
| 5388 | } |
| 5389 | |
| 5390 | *offset = n - idx; |
| 5391 | return sg; |
| 5392 | |
| 5393 | lookup: |
| 5394 | rcu_read_lock(); |
| 5395 | |
| 5396 | sg = radix_tree_lookup(&iter->radix, n); |
| 5397 | GEM_BUG_ON(!sg); |
| 5398 | |
| 5399 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5400 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5401 | * to the start of that range. We will return the pointer to |
| 5402 | * the base page and the offset of this page within the |
| 5403 | * sg entry's range. |
| 5404 | */ |
| 5405 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5406 | if (unlikely(xa_is_value(sg))) { |
| 5407 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5408 | |
| 5409 | sg = radix_tree_lookup(&iter->radix, base); |
| 5410 | GEM_BUG_ON(!sg); |
| 5411 | |
| 5412 | *offset = n - base; |
| 5413 | } |
| 5414 | |
| 5415 | rcu_read_unlock(); |
| 5416 | |
| 5417 | return sg; |
| 5418 | } |
| 5419 | |
| 5420 | struct page * |
| 5421 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5422 | { |
| 5423 | struct scatterlist *sg; |
| 5424 | unsigned int offset; |
| 5425 | |
| 5426 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5427 | |
| 5428 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5429 | return nth_page(sg_page(sg), offset); |
| 5430 | } |
| 5431 | |
| 5432 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5433 | struct page * |
| 5434 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5435 | unsigned int n) |
| 5436 | { |
| 5437 | struct page *page; |
| 5438 | |
| 5439 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5440 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5441 | set_page_dirty(page); |
| 5442 | |
| 5443 | return page; |
| 5444 | } |
| 5445 | |
| 5446 | dma_addr_t |
| 5447 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5448 | unsigned long n) |
| 5449 | { |
| 5450 | struct scatterlist *sg; |
| 5451 | unsigned int offset; |
| 5452 | |
| 5453 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5454 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5455 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5456 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5457 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5458 | { |
| 5459 | struct sg_table *pages; |
| 5460 | int err; |
| 5461 | |
| 5462 | if (align > obj->base.size) |
| 5463 | return -EINVAL; |
| 5464 | |
| 5465 | if (obj->ops == &i915_gem_phys_ops) |
| 5466 | return 0; |
| 5467 | |
| 5468 | if (obj->ops != &i915_gem_object_ops) |
| 5469 | return -EINVAL; |
| 5470 | |
| 5471 | err = i915_gem_object_unbind(obj); |
| 5472 | if (err) |
| 5473 | return err; |
| 5474 | |
| 5475 | mutex_lock(&obj->mm.lock); |
| 5476 | |
| 5477 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5478 | err = -EFAULT; |
| 5479 | goto err_unlock; |
| 5480 | } |
| 5481 | |
| 5482 | if (obj->mm.quirked) { |
| 5483 | err = -EFAULT; |
| 5484 | goto err_unlock; |
| 5485 | } |
| 5486 | |
| 5487 | if (obj->mm.mapping) { |
| 5488 | err = -EBUSY; |
| 5489 | goto err_unlock; |
| 5490 | } |
| 5491 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5492 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5493 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5494 | obj->ops = &i915_gem_phys_ops; |
| 5495 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5496 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5497 | if (err) |
| 5498 | goto err_xfer; |
| 5499 | |
| 5500 | /* Perma-pin (until release) the physical set of pages */ |
| 5501 | __i915_gem_object_pin_pages(obj); |
| 5502 | |
| 5503 | if (!IS_ERR_OR_NULL(pages)) |
| 5504 | i915_gem_object_ops.put_pages(obj, pages); |
| 5505 | mutex_unlock(&obj->mm.lock); |
| 5506 | return 0; |
| 5507 | |
| 5508 | err_xfer: |
| 5509 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5510 | if (!IS_ERR_OR_NULL(pages)) { |
| 5511 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 5512 | |
| 5513 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 5514 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5515 | err_unlock: |
| 5516 | mutex_unlock(&obj->mm.lock); |
| 5517 | return err; |
| 5518 | } |
| 5519 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5520 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5521 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5522 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5523 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5524 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5525 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5526 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 5527 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5528 | #endif |