Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
Daniel Vetter | be6a037 | 2015-03-18 10:46:04 +0100 | [diff] [blame] | 2 | * Copyright © 2008-2015 Intel Corporation |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 28 | #include <drm/drmP.h> |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 29 | #include <drm/drm_vma_manager.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/i915_drm.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 31 | #include "i915_drv.h" |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 32 | #include "i915_gem_clflush.h" |
Yu Zhang | eb82289 | 2015-02-10 19:05:49 +0800 | [diff] [blame] | 33 | #include "i915_vgpu.h" |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 34 | #include "i915_trace.h" |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 35 | #include "intel_drv.h" |
Chris Wilson | 5d723d7 | 2016-08-04 16:32:35 +0100 | [diff] [blame] | 36 | #include "intel_frontbuffer.h" |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 37 | #include "intel_mocs.h" |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 38 | #include "i915_gemfs.h" |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 39 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 40 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 41 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 42 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 44 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 45 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 46 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 47 | #include <linux/dma-buf.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 48 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 49 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 50 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 51 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 52 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 53 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 54 | return false; |
| 55 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 56 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 57 | return true; |
| 58 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 59 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 62 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 63 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 64 | struct drm_mm_node *node, u32 size) |
| 65 | { |
| 66 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 67 | return drm_mm_insert_node_in_range(&ggtt->base.mm, node, |
| 68 | size, 0, I915_COLOR_UNEVICTABLE, |
| 69 | 0, ggtt->mappable_end, |
| 70 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void |
| 74 | remove_mappable_node(struct drm_mm_node *node) |
| 75 | { |
| 76 | drm_mm_remove_node(node); |
| 77 | } |
| 78 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 79 | /* some bookkeeping */ |
| 80 | 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] | 81 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 82 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 83 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 84 | dev_priv->mm.object_count++; |
| 85 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 86 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | 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] | 90 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 91 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 92 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 93 | dev_priv->mm.object_count--; |
| 94 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 95 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Chris Wilson | 21dd373 | 2011-01-26 15:55:56 +0000 | [diff] [blame] | 98 | static int |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 99 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 100 | { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 101 | int ret; |
| 102 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 103 | might_sleep(); |
| 104 | |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 105 | /* |
| 106 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging |
| 107 | * userspace. If it takes that long something really bad is going on and |
| 108 | * we should simply try to bail out and fail as gracefully as possible. |
| 109 | */ |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 110 | ret = wait_event_interruptible_timeout(error->reset_queue, |
Chris Wilson | 8c185ec | 2017-03-16 17:13:02 +0000 | [diff] [blame] | 111 | !i915_reset_backoff(error), |
Chris Wilson | b52992c | 2016-10-28 13:58:24 +0100 | [diff] [blame] | 112 | I915_RESET_TIMEOUT); |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 113 | if (ret == 0) { |
| 114 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); |
| 115 | return -EIO; |
| 116 | } else if (ret < 0) { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 117 | return ret; |
Chris Wilson | d98c52c | 2016-04-13 17:35:05 +0100 | [diff] [blame] | 118 | } else { |
| 119 | return 0; |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 120 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 123 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 124 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 125 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 126 | int ret; |
| 127 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 128 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 129 | if (ret) |
| 130 | return ret; |
| 131 | |
| 132 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 136 | return 0; |
| 137 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 138 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 139 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 140 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 141 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 142 | { |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 143 | struct drm_i915_private *dev_priv = to_i915(dev); |
Joonas Lahtinen | 62106b4 | 2016-03-18 10:42:57 +0200 | [diff] [blame] | 144 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 145 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 146 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 147 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 148 | |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 149 | pinned = ggtt->base.reserved; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 150 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 151 | list_for_each_entry(vma, &ggtt->base.active_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 152 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 153 | pinned += vma->node.size; |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 154 | list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 155 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 156 | pinned += vma->node.size; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 157 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 158 | |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 159 | args->aper_size = ggtt->base.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 160 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 161 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 165 | 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] | 166 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 167 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 168 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 169 | struct sg_table *st; |
| 170 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 171 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 172 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 173 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 174 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 175 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 176 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 177 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 178 | /* Always aligning to the object size, allows a single allocation |
| 179 | * to handle all possible callers, and given typical object sizes, |
| 180 | * the alignment of the buddy allocation will naturally match. |
| 181 | */ |
| 182 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 183 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 184 | roundup_pow_of_two(obj->base.size)); |
| 185 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 186 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 187 | |
| 188 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 189 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 190 | struct page *page; |
| 191 | char *src; |
| 192 | |
| 193 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 194 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 195 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 196 | goto err_phys; |
| 197 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 198 | |
| 199 | src = kmap_atomic(page); |
| 200 | memcpy(vaddr, src, PAGE_SIZE); |
| 201 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 202 | kunmap_atomic(src); |
| 203 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 204 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 205 | vaddr += PAGE_SIZE; |
| 206 | } |
| 207 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 208 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 209 | |
| 210 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 211 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 212 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 213 | goto err_phys; |
| 214 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 215 | |
| 216 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 217 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 218 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 219 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | sg = st->sgl; |
| 223 | sg->offset = 0; |
| 224 | sg->length = obj->base.size; |
| 225 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 226 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 227 | sg_dma_len(sg) = obj->base.size; |
| 228 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 229 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 230 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 231 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 232 | |
| 233 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 234 | |
| 235 | err_phys: |
| 236 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 237 | |
| 238 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 241 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 242 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 243 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 244 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 245 | if (cpu_write_needs_clflush(obj)) |
| 246 | obj->cache_dirty = true; |
| 247 | } |
| 248 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 249 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 250 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 251 | struct sg_table *pages, |
| 252 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 253 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 254 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 255 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 256 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 257 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 258 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 259 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 260 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 261 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 262 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 263 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 264 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void |
| 268 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 269 | struct sg_table *pages) |
| 270 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 271 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 272 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 273 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 274 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 275 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 276 | int i; |
| 277 | |
| 278 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 279 | struct page *page; |
| 280 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 281 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 282 | page = shmem_read_mapping_page(mapping, i); |
| 283 | if (IS_ERR(page)) |
| 284 | continue; |
| 285 | |
| 286 | dst = kmap_atomic(page); |
| 287 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 288 | memcpy(dst, vaddr, PAGE_SIZE); |
| 289 | kunmap_atomic(dst); |
| 290 | |
| 291 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 292 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 293 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 294 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 295 | vaddr += PAGE_SIZE; |
| 296 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 297 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 300 | sg_free_table(pages); |
| 301 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 302 | |
| 303 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static void |
| 307 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 308 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 309 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 313 | .get_pages = i915_gem_object_get_pages_phys, |
| 314 | .put_pages = i915_gem_object_put_pages_phys, |
| 315 | .release = i915_gem_object_release_phys, |
| 316 | }; |
| 317 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 318 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 319 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 320 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 321 | { |
| 322 | struct i915_vma *vma; |
| 323 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 324 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 325 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 326 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 327 | |
| 328 | /* Closed vma are removed from the obj->vma_list - but they may |
| 329 | * still have an active binding on the object. To remove those we |
| 330 | * must wait for all rendering to complete to the object (as unbinding |
| 331 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 332 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 333 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 334 | if (ret) |
| 335 | return ret; |
| 336 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 337 | while ((vma = list_first_entry_or_null(&obj->vma_list, |
| 338 | struct i915_vma, |
| 339 | obj_link))) { |
| 340 | list_move_tail(&vma->obj_link, &still_in_list); |
| 341 | ret = i915_vma_unbind(vma); |
| 342 | if (ret) |
| 343 | break; |
| 344 | } |
| 345 | list_splice(&still_in_list, &obj->vma_list); |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 350 | static long |
| 351 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 352 | unsigned int flags, |
| 353 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 354 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 355 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 356 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 357 | |
| 358 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 359 | |
| 360 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 361 | return timeout; |
| 362 | |
| 363 | if (!dma_fence_is_i915(fence)) |
| 364 | return dma_fence_wait_timeout(fence, |
| 365 | flags & I915_WAIT_INTERRUPTIBLE, |
| 366 | timeout); |
| 367 | |
| 368 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 369 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 370 | goto out; |
| 371 | |
Chris Wilson | e9af4ea | 2018-01-18 13:16:09 +0000 | [diff] [blame] | 372 | /* |
| 373 | * This client is about to stall waiting for the GPU. In many cases |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 374 | * this is undesirable and limits the throughput of the system, as |
| 375 | * many clients cannot continue processing user input/output whilst |
| 376 | * blocked. RPS autotuning may take tens of milliseconds to respond |
| 377 | * to the GPU load and thus incurs additional latency for the client. |
| 378 | * We can circumvent that by promoting the GPU frequency to maximum |
| 379 | * before we wait. This makes the GPU throttle up much more quickly |
| 380 | * (good for benchmarks and user experience, e.g. window animations), |
| 381 | * but at a cost of spending more power processing the workload |
| 382 | * (bad for battery). Not all clients even want their results |
| 383 | * immediately and for them we should just let the GPU select its own |
| 384 | * frequency to maximise efficiency. To prevent a single client from |
| 385 | * forcing the clocks too high for the whole system, we only allow |
| 386 | * each client to waitboost once in a busy period. |
| 387 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 388 | if (rps_client && !i915_request_started(rq)) { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 389 | if (INTEL_GEN(rq->i915) >= 6) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 390 | gen6_rps_boost(rq, rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 393 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 394 | |
| 395 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 396 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 397 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 398 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 399 | return timeout; |
| 400 | } |
| 401 | |
| 402 | static long |
| 403 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 404 | unsigned int flags, |
| 405 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 406 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 407 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 408 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 409 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 410 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 411 | |
| 412 | if (flags & I915_WAIT_ALL) { |
| 413 | struct dma_fence **shared; |
| 414 | unsigned int count, i; |
| 415 | int ret; |
| 416 | |
| 417 | ret = reservation_object_get_fences_rcu(resv, |
| 418 | &excl, &count, &shared); |
| 419 | if (ret) |
| 420 | return ret; |
| 421 | |
| 422 | for (i = 0; i < count; i++) { |
| 423 | timeout = i915_gem_object_wait_fence(shared[i], |
| 424 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 425 | rps_client); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 426 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | dma_fence_put(shared[i]); |
| 430 | } |
| 431 | |
| 432 | for (; i < count; i++) |
| 433 | dma_fence_put(shared[i]); |
| 434 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 435 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 436 | /* |
| 437 | * If both shared fences and an exclusive fence exist, |
| 438 | * then by construction the shared fences must be later |
| 439 | * than the exclusive fence. If we successfully wait for |
| 440 | * all the shared fences, we know that the exclusive fence |
| 441 | * must all be signaled. If all the shared fences are |
| 442 | * signaled, we can prune the array and recover the |
| 443 | * floating references on the fences/requests. |
| 444 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 445 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 446 | } else { |
| 447 | excl = reservation_object_get_excl_rcu(resv); |
| 448 | } |
| 449 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 450 | if (excl && timeout >= 0) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 451 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
| 452 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 453 | |
| 454 | dma_fence_put(excl); |
| 455 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 456 | /* |
| 457 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 458 | * signaled and that the reservation object has not been changed (i.e. |
| 459 | * no new fences have been added). |
| 460 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 461 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 462 | if (reservation_object_trylock(resv)) { |
| 463 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 464 | reservation_object_add_excl_fence(resv, NULL); |
| 465 | reservation_object_unlock(resv); |
| 466 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 469 | return timeout; |
| 470 | } |
| 471 | |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 472 | static void __fence_set_priority(struct dma_fence *fence, int prio) |
| 473 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 474 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 475 | struct intel_engine_cs *engine; |
| 476 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 477 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 478 | return; |
| 479 | |
| 480 | rq = to_request(fence); |
| 481 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 482 | |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 483 | rcu_read_lock(); |
| 484 | if (engine->schedule) |
| 485 | engine->schedule(rq, prio); |
| 486 | rcu_read_unlock(); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void fence_set_priority(struct dma_fence *fence, int prio) |
| 490 | { |
| 491 | /* Recurse once into a fence-array */ |
| 492 | if (dma_fence_is_array(fence)) { |
| 493 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 494 | int i; |
| 495 | |
| 496 | for (i = 0; i < array->num_fences; i++) |
| 497 | __fence_set_priority(array->fences[i], prio); |
| 498 | } else { |
| 499 | __fence_set_priority(fence, prio); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | int |
| 504 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 505 | unsigned int flags, |
| 506 | int prio) |
| 507 | { |
| 508 | struct dma_fence *excl; |
| 509 | |
| 510 | if (flags & I915_WAIT_ALL) { |
| 511 | struct dma_fence **shared; |
| 512 | unsigned int count, i; |
| 513 | int ret; |
| 514 | |
| 515 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 516 | &excl, &count, &shared); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | |
| 520 | for (i = 0; i < count; i++) { |
| 521 | fence_set_priority(shared[i], prio); |
| 522 | dma_fence_put(shared[i]); |
| 523 | } |
| 524 | |
| 525 | kfree(shared); |
| 526 | } else { |
| 527 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 528 | } |
| 529 | |
| 530 | if (excl) { |
| 531 | fence_set_priority(excl, prio); |
| 532 | dma_fence_put(excl); |
| 533 | } |
| 534 | return 0; |
| 535 | } |
| 536 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 537 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 538 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 539 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 540 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 541 | * @timeout: how long to wait |
Chris Wilson | a0a8b1c | 2017-11-09 14:06:44 +0000 | [diff] [blame] | 542 | * @rps_client: client (user process) to charge for any waitboosting |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 543 | */ |
| 544 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 545 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 546 | unsigned int flags, |
| 547 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 548 | struct intel_rps_client *rps_client) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 549 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 550 | might_sleep(); |
| 551 | #if IS_ENABLED(CONFIG_LOCKDEP) |
| 552 | GEM_BUG_ON(debug_locks && |
| 553 | !!lockdep_is_held(&obj->base.dev->struct_mutex) != |
| 554 | !!(flags & I915_WAIT_LOCKED)); |
| 555 | #endif |
| 556 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 557 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 558 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 559 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 560 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 561 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 565 | { |
| 566 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 567 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 568 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 571 | static int |
| 572 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 573 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 574 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 575 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 576 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 577 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 578 | |
| 579 | /* We manually control the domain here and pretend that it |
| 580 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 581 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 582 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 583 | if (copy_from_user(vaddr, user_data, args->size)) |
| 584 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 585 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 586 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 587 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 588 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 589 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 590 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 593 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 594 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 595 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 599 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 600 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 601 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 604 | static int |
| 605 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 606 | struct drm_i915_private *dev_priv, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 607 | uint64_t size, |
| 608 | uint32_t *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 609 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 610 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 611 | int ret; |
| 612 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 613 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 614 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 615 | if (size == 0) |
| 616 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 617 | |
| 618 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 619 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 620 | if (IS_ERR(obj)) |
| 621 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 622 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 623 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 624 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 625 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 626 | if (ret) |
| 627 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 628 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 629 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 633 | int |
| 634 | i915_gem_dumb_create(struct drm_file *file, |
| 635 | struct drm_device *dev, |
| 636 | struct drm_mode_create_dumb *args) |
| 637 | { |
| 638 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 639 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 640 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 641 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 642 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 643 | } |
| 644 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 645 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 646 | { |
| 647 | return !(obj->cache_level == I915_CACHE_NONE || |
| 648 | obj->cache_level == I915_CACHE_WT); |
| 649 | } |
| 650 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 651 | /** |
| 652 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 653 | * @dev: drm device pointer |
| 654 | * @data: ioctl data blob |
| 655 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 656 | */ |
| 657 | int |
| 658 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 659 | struct drm_file *file) |
| 660 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 661 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 662 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 663 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 664 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 665 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 666 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 667 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 668 | } |
| 669 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 670 | static inline enum fb_op_origin |
| 671 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 672 | { |
| 673 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 674 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 675 | } |
| 676 | |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 677 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 678 | { |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 679 | /* |
| 680 | * No actual flushing is required for the GTT write domain for reads |
| 681 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 682 | * as far as we know, so there's no chipset flush. It also doesn't |
| 683 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 684 | * |
| 685 | * However, we do have to enforce the order so that all writes through |
| 686 | * the GTT land before any writes to the device, such as updates to |
| 687 | * the GATT itself. |
| 688 | * |
| 689 | * We also have to wait a bit for the writes to land from the GTT. |
| 690 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 691 | * timing. This issue has only been observed when switching quickly |
| 692 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 693 | * and it appears to only affect discrete GTT blocks (i.e. on LLC |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 694 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 695 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 696 | */ |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 697 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 698 | wmb(); |
| 699 | |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 700 | intel_runtime_pm_get(dev_priv); |
| 701 | spin_lock_irq(&dev_priv->uncore.lock); |
| 702 | |
| 703 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
| 704 | |
| 705 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 706 | intel_runtime_pm_put(dev_priv); |
| 707 | } |
| 708 | |
| 709 | static void |
| 710 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 711 | { |
| 712 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 713 | struct i915_vma *vma; |
| 714 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 715 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 716 | return; |
| 717 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 718 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 719 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 720 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 721 | |
| 722 | intel_fb_obj_flush(obj, |
| 723 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 724 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 725 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 726 | if (vma->iomap) |
| 727 | continue; |
| 728 | |
| 729 | i915_vma_unset_ggtt_write(vma); |
| 730 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 731 | break; |
| 732 | |
| 733 | case I915_GEM_DOMAIN_CPU: |
| 734 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 735 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 736 | |
| 737 | case I915_GEM_DOMAIN_RENDER: |
| 738 | if (gpu_write_needs_clflush(obj)) |
| 739 | obj->cache_dirty = true; |
| 740 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 743 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 746 | static inline int |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 747 | __copy_to_user_swizzled(char __user *cpu_vaddr, |
| 748 | const char *gpu_vaddr, int gpu_offset, |
| 749 | int length) |
| 750 | { |
| 751 | int ret, cpu_offset = 0; |
| 752 | |
| 753 | while (length > 0) { |
| 754 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 755 | int this_length = min(cacheline_end - gpu_offset, length); |
| 756 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 757 | |
| 758 | ret = __copy_to_user(cpu_vaddr + cpu_offset, |
| 759 | gpu_vaddr + swizzled_gpu_offset, |
| 760 | this_length); |
| 761 | if (ret) |
| 762 | return ret + length; |
| 763 | |
| 764 | cpu_offset += this_length; |
| 765 | gpu_offset += this_length; |
| 766 | length -= this_length; |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static inline int |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 773 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
| 774 | const char __user *cpu_vaddr, |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 775 | int length) |
| 776 | { |
| 777 | int ret, cpu_offset = 0; |
| 778 | |
| 779 | while (length > 0) { |
| 780 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 781 | int this_length = min(cacheline_end - gpu_offset, length); |
| 782 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 783 | |
| 784 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, |
| 785 | cpu_vaddr + cpu_offset, |
| 786 | this_length); |
| 787 | if (ret) |
| 788 | return ret + length; |
| 789 | |
| 790 | cpu_offset += this_length; |
| 791 | gpu_offset += this_length; |
| 792 | length -= this_length; |
| 793 | } |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 798 | /* |
| 799 | * Pins the specified object's pages and synchronizes the object with |
| 800 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 801 | * flush the object from the CPU cache. |
| 802 | */ |
| 803 | 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] | 804 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 805 | { |
| 806 | int ret; |
| 807 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 808 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 809 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 810 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 811 | if (!i915_gem_object_has_struct_page(obj)) |
| 812 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 813 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 814 | ret = i915_gem_object_wait(obj, |
| 815 | I915_WAIT_INTERRUPTIBLE | |
| 816 | I915_WAIT_LOCKED, |
| 817 | MAX_SCHEDULE_TIMEOUT, |
| 818 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 819 | if (ret) |
| 820 | return ret; |
| 821 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 822 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 823 | if (ret) |
| 824 | return ret; |
| 825 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 826 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 827 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 828 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 829 | if (ret) |
| 830 | goto err_unpin; |
| 831 | else |
| 832 | goto out; |
| 833 | } |
| 834 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 835 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 836 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 837 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 838 | * read domain and manually flush cachelines (if required). This |
| 839 | * optimizes for the case when the gpu will dirty the data |
| 840 | * anyway again before the next pread happens. |
| 841 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 842 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 843 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 844 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 845 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 846 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 847 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 848 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 849 | |
| 850 | err_unpin: |
| 851 | i915_gem_object_unpin_pages(obj); |
| 852 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 856 | unsigned int *needs_clflush) |
| 857 | { |
| 858 | int ret; |
| 859 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 860 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 861 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 862 | *needs_clflush = 0; |
| 863 | if (!i915_gem_object_has_struct_page(obj)) |
| 864 | return -ENODEV; |
| 865 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 866 | ret = i915_gem_object_wait(obj, |
| 867 | I915_WAIT_INTERRUPTIBLE | |
| 868 | I915_WAIT_LOCKED | |
| 869 | I915_WAIT_ALL, |
| 870 | MAX_SCHEDULE_TIMEOUT, |
| 871 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 872 | if (ret) |
| 873 | return ret; |
| 874 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 875 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 876 | if (ret) |
| 877 | return ret; |
| 878 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 879 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 880 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 881 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 882 | if (ret) |
| 883 | goto err_unpin; |
| 884 | else |
| 885 | goto out; |
| 886 | } |
| 887 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 888 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 889 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 890 | /* If we're not in the cpu write domain, set ourself into the |
| 891 | * gtt write domain and manually flush cachelines (as required). |
| 892 | * This optimizes for the case when the gpu will use the data |
| 893 | * right away and we therefore have to clflush anyway. |
| 894 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 895 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 896 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 897 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 898 | /* |
| 899 | * Same trick applies to invalidate partially written |
| 900 | * cachelines read before writing. |
| 901 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 902 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 903 | *needs_clflush |= CLFLUSH_BEFORE; |
| 904 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 905 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 906 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 907 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 908 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 909 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 910 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 911 | |
| 912 | err_unpin: |
| 913 | i915_gem_object_unpin_pages(obj); |
| 914 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 917 | static void |
| 918 | shmem_clflush_swizzled_range(char *addr, unsigned long length, |
| 919 | bool swizzled) |
| 920 | { |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 921 | if (unlikely(swizzled)) { |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 922 | unsigned long start = (unsigned long) addr; |
| 923 | unsigned long end = (unsigned long) addr + length; |
| 924 | |
| 925 | /* For swizzling simply ensure that we always flush both |
| 926 | * channels. Lame, but simple and it works. Swizzled |
| 927 | * pwrite/pread is far from a hotpath - current userspace |
| 928 | * doesn't use it at all. */ |
| 929 | start = round_down(start, 128); |
| 930 | end = round_up(end, 128); |
| 931 | |
| 932 | drm_clflush_virt_range((void *)start, end - start); |
| 933 | } else { |
| 934 | drm_clflush_virt_range(addr, length); |
| 935 | } |
| 936 | |
| 937 | } |
| 938 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 939 | /* Only difference to the fast-path function is that this can handle bit17 |
| 940 | * and uses non-atomic copy and kmap functions. */ |
| 941 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 942 | shmem_pread_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 943 | char __user *user_data, |
| 944 | bool page_do_bit17_swizzling, bool needs_clflush) |
| 945 | { |
| 946 | char *vaddr; |
| 947 | int ret; |
| 948 | |
| 949 | vaddr = kmap(page); |
| 950 | if (needs_clflush) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 951 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 952 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 953 | |
| 954 | if (page_do_bit17_swizzling) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 955 | ret = __copy_to_user_swizzled(user_data, vaddr, offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 956 | else |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 957 | ret = __copy_to_user(user_data, vaddr + offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 958 | kunmap(page); |
| 959 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 960 | return ret ? - EFAULT : 0; |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 961 | } |
| 962 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 963 | static int |
| 964 | shmem_pread(struct page *page, int offset, int length, char __user *user_data, |
| 965 | bool page_do_bit17_swizzling, bool needs_clflush) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 966 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 967 | int ret; |
| 968 | |
| 969 | ret = -ENODEV; |
| 970 | if (!page_do_bit17_swizzling) { |
| 971 | char *vaddr = kmap_atomic(page); |
| 972 | |
| 973 | if (needs_clflush) |
| 974 | drm_clflush_virt_range(vaddr + offset, length); |
| 975 | ret = __copy_to_user_inatomic(user_data, vaddr + offset, length); |
| 976 | kunmap_atomic(vaddr); |
| 977 | } |
| 978 | if (ret == 0) |
| 979 | return 0; |
| 980 | |
| 981 | return shmem_pread_slow(page, offset, length, user_data, |
| 982 | page_do_bit17_swizzling, needs_clflush); |
| 983 | } |
| 984 | |
| 985 | static int |
| 986 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 987 | struct drm_i915_gem_pread *args) |
| 988 | { |
| 989 | char __user *user_data; |
| 990 | u64 remain; |
| 991 | unsigned int obj_do_bit17_swizzling; |
| 992 | unsigned int needs_clflush; |
| 993 | unsigned int idx, offset; |
| 994 | int ret; |
| 995 | |
| 996 | obj_do_bit17_swizzling = 0; |
| 997 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 998 | obj_do_bit17_swizzling = BIT(17); |
| 999 | |
| 1000 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 1001 | if (ret) |
| 1002 | return ret; |
| 1003 | |
| 1004 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 1005 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 1006 | if (ret) |
| 1007 | return ret; |
| 1008 | |
| 1009 | remain = args->size; |
| 1010 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1011 | offset = offset_in_page(args->offset); |
| 1012 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1013 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1014 | int length; |
| 1015 | |
| 1016 | length = remain; |
| 1017 | if (offset + length > PAGE_SIZE) |
| 1018 | length = PAGE_SIZE - offset; |
| 1019 | |
| 1020 | ret = shmem_pread(page, offset, length, user_data, |
| 1021 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1022 | needs_clflush); |
| 1023 | if (ret) |
| 1024 | break; |
| 1025 | |
| 1026 | remain -= length; |
| 1027 | user_data += length; |
| 1028 | offset = 0; |
| 1029 | } |
| 1030 | |
| 1031 | i915_gem_obj_finish_shmem_access(obj); |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | static inline bool |
| 1036 | gtt_user_read(struct io_mapping *mapping, |
| 1037 | loff_t base, int offset, |
| 1038 | char __user *user_data, int length) |
| 1039 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1040 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1041 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1042 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1043 | /* 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] | 1044 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1045 | unwritten = __copy_to_user_inatomic(user_data, |
| 1046 | (void __force *)vaddr + offset, |
| 1047 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1048 | io_mapping_unmap_atomic(vaddr); |
| 1049 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1050 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1051 | unwritten = copy_to_user(user_data, |
| 1052 | (void __force *)vaddr + offset, |
| 1053 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1054 | io_mapping_unmap(vaddr); |
| 1055 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1056 | return unwritten; |
| 1057 | } |
| 1058 | |
| 1059 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1060 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1061 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1062 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1063 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1064 | struct i915_ggtt *ggtt = &i915->ggtt; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1065 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1066 | struct i915_vma *vma; |
| 1067 | void __user *user_data; |
| 1068 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1069 | int ret; |
| 1070 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1071 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1072 | if (ret) |
| 1073 | return ret; |
| 1074 | |
| 1075 | intel_runtime_pm_get(i915); |
| 1076 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1077 | PIN_MAPPABLE | |
| 1078 | PIN_NONFAULT | |
| 1079 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1080 | if (!IS_ERR(vma)) { |
| 1081 | node.start = i915_ggtt_offset(vma); |
| 1082 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1083 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1084 | if (ret) { |
| 1085 | i915_vma_unpin(vma); |
| 1086 | vma = ERR_PTR(ret); |
| 1087 | } |
| 1088 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1089 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1090 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1091 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1092 | goto out_unlock; |
| 1093 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1097 | if (ret) |
| 1098 | goto out_unpin; |
| 1099 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1100 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1101 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1102 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1103 | remain = args->size; |
| 1104 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1105 | |
| 1106 | while (remain > 0) { |
| 1107 | /* Operation in this page |
| 1108 | * |
| 1109 | * page_base = page offset within aperture |
| 1110 | * page_offset = offset within page |
| 1111 | * page_length = bytes to copy for this page |
| 1112 | */ |
| 1113 | u32 page_base = node.start; |
| 1114 | unsigned page_offset = offset_in_page(offset); |
| 1115 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1116 | page_length = remain < page_length ? remain : page_length; |
| 1117 | if (node.allocated) { |
| 1118 | wmb(); |
| 1119 | ggtt->base.insert_page(&ggtt->base, |
| 1120 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1121 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1122 | wmb(); |
| 1123 | } else { |
| 1124 | page_base += offset & PAGE_MASK; |
| 1125 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1126 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1127 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1128 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1129 | ret = -EFAULT; |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | remain -= page_length; |
| 1134 | user_data += page_length; |
| 1135 | offset += page_length; |
| 1136 | } |
| 1137 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1138 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1139 | out_unpin: |
| 1140 | if (node.allocated) { |
| 1141 | wmb(); |
| 1142 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1143 | node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1144 | remove_mappable_node(&node); |
| 1145 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1146 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1147 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1148 | out_unlock: |
| 1149 | intel_runtime_pm_put(i915); |
| 1150 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1151 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1152 | return ret; |
| 1153 | } |
| 1154 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1155 | /** |
| 1156 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1157 | * @dev: drm device pointer |
| 1158 | * @data: ioctl data blob |
| 1159 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1160 | * |
| 1161 | * On error, the contents of *data are undefined. |
| 1162 | */ |
| 1163 | int |
| 1164 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1165 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1166 | { |
| 1167 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1168 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1169 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1170 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1171 | if (args->size == 0) |
| 1172 | return 0; |
| 1173 | |
| 1174 | if (!access_ok(VERIFY_WRITE, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1175 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1176 | args->size)) |
| 1177 | return -EFAULT; |
| 1178 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1179 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1180 | if (!obj) |
| 1181 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1182 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1183 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1184 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1185 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1186 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1189 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1190 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1191 | ret = i915_gem_object_wait(obj, |
| 1192 | I915_WAIT_INTERRUPTIBLE, |
| 1193 | MAX_SCHEDULE_TIMEOUT, |
| 1194 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1195 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1196 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1197 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1198 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1199 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1200 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1201 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1202 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1203 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1204 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1205 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1206 | i915_gem_object_unpin_pages(obj); |
| 1207 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1208 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1209 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1212 | /* This is the fast write path which cannot handle |
| 1213 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1214 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1215 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1216 | static inline bool |
| 1217 | ggtt_write(struct io_mapping *mapping, |
| 1218 | loff_t base, int offset, |
| 1219 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1220 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1221 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1222 | unsigned long unwritten; |
| 1223 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1224 | /* 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] | 1225 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1226 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1227 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1228 | io_mapping_unmap_atomic(vaddr); |
| 1229 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1230 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1231 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1232 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1233 | io_mapping_unmap(vaddr); |
| 1234 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1235 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1236 | return unwritten; |
| 1237 | } |
| 1238 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1239 | /** |
| 1240 | * This is the fast pwrite path, where we copy the data directly from the |
| 1241 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1242 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1243 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1244 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1245 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1246 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1247 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1248 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1249 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1250 | struct i915_ggtt *ggtt = &i915->ggtt; |
| 1251 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1252 | struct i915_vma *vma; |
| 1253 | u64 remain, offset; |
| 1254 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1255 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1256 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1257 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1258 | if (ret) |
| 1259 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1260 | |
Chris Wilson | 8bd8181 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1261 | if (i915_gem_object_has_struct_page(obj)) { |
| 1262 | /* |
| 1263 | * Avoid waking the device up if we can fallback, as |
| 1264 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1265 | * depending on PCI sleeps and our own resume time). |
| 1266 | * This easily dwarfs any performance advantage from |
| 1267 | * using the cache bypass of indirect GGTT access. |
| 1268 | */ |
| 1269 | if (!intel_runtime_pm_get_if_in_use(i915)) { |
| 1270 | ret = -EFAULT; |
| 1271 | goto out_unlock; |
| 1272 | } |
| 1273 | } else { |
| 1274 | /* No backing pages, no fallback, we must force GGTT access */ |
| 1275 | intel_runtime_pm_get(i915); |
| 1276 | } |
| 1277 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1278 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1279 | PIN_MAPPABLE | |
| 1280 | PIN_NONFAULT | |
| 1281 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1282 | if (!IS_ERR(vma)) { |
| 1283 | node.start = i915_ggtt_offset(vma); |
| 1284 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1285 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1286 | if (ret) { |
| 1287 | i915_vma_unpin(vma); |
| 1288 | vma = ERR_PTR(ret); |
| 1289 | } |
| 1290 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1291 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1292 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1293 | if (ret) |
Chris Wilson | 8bd8181 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1294 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1295 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1296 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1297 | |
| 1298 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1299 | if (ret) |
| 1300 | goto out_unpin; |
| 1301 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1302 | mutex_unlock(&i915->drm.struct_mutex); |
| 1303 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1304 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1305 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1306 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1307 | offset = args->offset; |
| 1308 | remain = args->size; |
| 1309 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1310 | /* Operation in this page |
| 1311 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1312 | * page_base = page offset within aperture |
| 1313 | * page_offset = offset within page |
| 1314 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1315 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1316 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1317 | unsigned int page_offset = offset_in_page(offset); |
| 1318 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1319 | page_length = remain < page_length ? remain : page_length; |
| 1320 | if (node.allocated) { |
| 1321 | wmb(); /* flush the write before we modify the GGTT */ |
| 1322 | ggtt->base.insert_page(&ggtt->base, |
| 1323 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1324 | node.start, I915_CACHE_NONE, 0); |
| 1325 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1326 | } else { |
| 1327 | page_base += offset & PAGE_MASK; |
| 1328 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1329 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1330 | * source page isn't available. Return the error and we'll |
| 1331 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1332 | * If the object is non-shmem backed, we retry again with the |
| 1333 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1334 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1335 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1336 | user_data, page_length)) { |
| 1337 | ret = -EFAULT; |
| 1338 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1339 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1340 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1341 | remain -= page_length; |
| 1342 | user_data += page_length; |
| 1343 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1344 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1345 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1346 | |
| 1347 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1348 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1349 | if (node.allocated) { |
| 1350 | wmb(); |
| 1351 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1352 | node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1353 | remove_mappable_node(&node); |
| 1354 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1355 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1356 | } |
Chris Wilson | 8bd8181 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1357 | out_rpm: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1358 | intel_runtime_pm_put(i915); |
Chris Wilson | 8bd8181 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1359 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1360 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1361 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1364 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1365 | shmem_pwrite_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1366 | char __user *user_data, |
| 1367 | bool page_do_bit17_swizzling, |
| 1368 | bool needs_clflush_before, |
| 1369 | bool needs_clflush_after) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1370 | { |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1371 | char *vaddr; |
| 1372 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1373 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1374 | vaddr = kmap(page); |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 1375 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1376 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1377 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1378 | if (page_do_bit17_swizzling) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1379 | ret = __copy_from_user_swizzled(vaddr, offset, user_data, |
| 1380 | length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1381 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1382 | ret = __copy_from_user(vaddr + offset, user_data, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1383 | if (needs_clflush_after) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1384 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1385 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1386 | kunmap(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1387 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 1388 | return ret ? -EFAULT : 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1391 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1392 | * Flushes invalid cachelines before writing to the target if |
| 1393 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1394 | * writing if needs_clflush is set. |
| 1395 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1396 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1397 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
| 1398 | bool page_do_bit17_swizzling, |
| 1399 | bool needs_clflush_before, |
| 1400 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1401 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1402 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1403 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1404 | ret = -ENODEV; |
| 1405 | if (!page_do_bit17_swizzling) { |
| 1406 | char *vaddr = kmap_atomic(page); |
| 1407 | |
| 1408 | if (needs_clflush_before) |
| 1409 | drm_clflush_virt_range(vaddr + offset, len); |
| 1410 | ret = __copy_from_user_inatomic(vaddr + offset, user_data, len); |
| 1411 | if (needs_clflush_after) |
| 1412 | drm_clflush_virt_range(vaddr + offset, len); |
| 1413 | |
| 1414 | kunmap_atomic(vaddr); |
| 1415 | } |
| 1416 | if (ret == 0) |
| 1417 | return ret; |
| 1418 | |
| 1419 | return shmem_pwrite_slow(page, offset, len, user_data, |
| 1420 | page_do_bit17_swizzling, |
| 1421 | needs_clflush_before, |
| 1422 | needs_clflush_after); |
| 1423 | } |
| 1424 | |
| 1425 | static int |
| 1426 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1427 | const struct drm_i915_gem_pwrite *args) |
| 1428 | { |
| 1429 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1430 | void __user *user_data; |
| 1431 | u64 remain; |
| 1432 | unsigned int obj_do_bit17_swizzling; |
| 1433 | unsigned int partial_cacheline_write; |
| 1434 | unsigned int needs_clflush; |
| 1435 | unsigned int offset, idx; |
| 1436 | int ret; |
| 1437 | |
| 1438 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1439 | if (ret) |
| 1440 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1441 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1442 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1443 | mutex_unlock(&i915->drm.struct_mutex); |
| 1444 | if (ret) |
| 1445 | return ret; |
| 1446 | |
| 1447 | obj_do_bit17_swizzling = 0; |
| 1448 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 1449 | obj_do_bit17_swizzling = BIT(17); |
| 1450 | |
| 1451 | /* If we don't overwrite a cacheline completely we need to be |
| 1452 | * careful to have up-to-date data by first clflushing. Don't |
| 1453 | * overcomplicate things and flush the entire patch. |
| 1454 | */ |
| 1455 | partial_cacheline_write = 0; |
| 1456 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1457 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1458 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1459 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1460 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1461 | offset = offset_in_page(args->offset); |
| 1462 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1463 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1464 | int length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1465 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1466 | length = remain; |
| 1467 | if (offset + length > PAGE_SIZE) |
| 1468 | length = PAGE_SIZE - offset; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1469 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1470 | ret = shmem_pwrite(page, offset, length, user_data, |
| 1471 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1472 | (offset | length) & partial_cacheline_write, |
| 1473 | needs_clflush & CLFLUSH_AFTER); |
| 1474 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1475 | break; |
| 1476 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1477 | remain -= length; |
| 1478 | user_data += length; |
| 1479 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1482 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1483 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1484 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1489 | * @dev: drm device |
| 1490 | * @data: ioctl data blob |
| 1491 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1492 | * |
| 1493 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1494 | */ |
| 1495 | int |
| 1496 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1497 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1498 | { |
| 1499 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1500 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1501 | int ret; |
| 1502 | |
| 1503 | if (args->size == 0) |
| 1504 | return 0; |
| 1505 | |
| 1506 | if (!access_ok(VERIFY_READ, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1507 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1508 | args->size)) |
| 1509 | return -EFAULT; |
| 1510 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1511 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1512 | if (!obj) |
| 1513 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1514 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1515 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1516 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1517 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1518 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1521 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1522 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1523 | ret = -ENODEV; |
| 1524 | if (obj->ops->pwrite) |
| 1525 | ret = obj->ops->pwrite(obj, args); |
| 1526 | if (ret != -ENODEV) |
| 1527 | goto err; |
| 1528 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1529 | ret = i915_gem_object_wait(obj, |
| 1530 | I915_WAIT_INTERRUPTIBLE | |
| 1531 | I915_WAIT_ALL, |
| 1532 | MAX_SCHEDULE_TIMEOUT, |
| 1533 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1534 | if (ret) |
| 1535 | goto err; |
| 1536 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1537 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1538 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1539 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1540 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1541 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1542 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1543 | * it would end up going through the fenced access, and we'll get |
| 1544 | * different detiling behavior between reading and writing. |
| 1545 | * pread/pwrite currently are reading and writing from the CPU |
| 1546 | * perspective, requiring manual detiling by the client. |
| 1547 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1548 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1549 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1550 | /* Note that the gtt paths might fail with non-page-backed user |
| 1551 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1552 | * textures). Fallback to the shmem path in that case. |
| 1553 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1554 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1555 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1556 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1557 | if (obj->phys_handle) |
| 1558 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1559 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1560 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1561 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1562 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1563 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1564 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1565 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1566 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1569 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1570 | { |
| 1571 | struct drm_i915_private *i915; |
| 1572 | struct list_head *list; |
| 1573 | struct i915_vma *vma; |
| 1574 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1575 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1576 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1577 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1578 | if (i915_vma_is_active(vma)) |
| 1579 | continue; |
| 1580 | |
| 1581 | if (!drm_mm_node_allocated(&vma->node)) |
| 1582 | continue; |
| 1583 | |
| 1584 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 1585 | } |
| 1586 | |
| 1587 | i915 = to_i915(obj->base.dev); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1588 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1589 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1590 | list_move_tail(&obj->mm.link, list); |
| 1591 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1592 | } |
| 1593 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1594 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1595 | * Called when user space prepares to use an object with the CPU, either |
| 1596 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1597 | * @dev: drm device |
| 1598 | * @data: ioctl data blob |
| 1599 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1600 | */ |
| 1601 | int |
| 1602 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1603 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1604 | { |
| 1605 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1606 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1607 | uint32_t read_domains = args->read_domains; |
| 1608 | uint32_t write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1609 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1610 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1611 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1612 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1613 | return -EINVAL; |
| 1614 | |
| 1615 | /* Having something in the write domain implies it's in the read |
| 1616 | * domain, and only that read domain. Enforce that in the request. |
| 1617 | */ |
| 1618 | if (write_domain != 0 && read_domains != write_domain) |
| 1619 | return -EINVAL; |
| 1620 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1621 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1622 | if (!obj) |
| 1623 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1624 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1625 | /* Try to flush the object off the GPU without holding the lock. |
| 1626 | * We will repeat the flush holding the lock in the normal manner |
| 1627 | * to catch cases where we are gazumped. |
| 1628 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1629 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1630 | I915_WAIT_INTERRUPTIBLE | |
| 1631 | (write_domain ? I915_WAIT_ALL : 0), |
| 1632 | MAX_SCHEDULE_TIMEOUT, |
| 1633 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1634 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1635 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1636 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1637 | /* |
| 1638 | * Proxy objects do not control access to the backing storage, ergo |
| 1639 | * they cannot be used as a means to manipulate the cache domain |
| 1640 | * tracking for that backing storage. The proxy object is always |
| 1641 | * considered to be outside of any cache domain. |
| 1642 | */ |
| 1643 | if (i915_gem_object_is_proxy(obj)) { |
| 1644 | err = -ENXIO; |
| 1645 | goto out; |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1650 | * direct access in memory with previous cached writes through |
| 1651 | * shmemfs and that our cache domain tracking remains valid. |
| 1652 | * For example, if the obj->filp was moved to swap without us |
| 1653 | * being notified and releasing the pages, we would mistakenly |
| 1654 | * continue to assume that the obj remained out of the CPU cached |
| 1655 | * domain. |
| 1656 | */ |
| 1657 | err = i915_gem_object_pin_pages(obj); |
| 1658 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1659 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1660 | |
| 1661 | err = i915_mutex_lock_interruptible(dev); |
| 1662 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1663 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1664 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1665 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1666 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1667 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1668 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1669 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1670 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1671 | |
| 1672 | /* And bump the LRU for this access */ |
| 1673 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1674 | |
| 1675 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1676 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1677 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1678 | intel_fb_obj_invalidate(obj, |
| 1679 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1680 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1681 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1682 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1683 | out: |
| 1684 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1685 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /** |
| 1689 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1690 | * @dev: drm device |
| 1691 | * @data: ioctl data blob |
| 1692 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1693 | */ |
| 1694 | int |
| 1695 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1696 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1697 | { |
| 1698 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1699 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1700 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1701 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1702 | if (!obj) |
| 1703 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1704 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1705 | /* |
| 1706 | * Proxy objects are barred from CPU access, so there is no |
| 1707 | * need to ban sw_finish as it is a nop. |
| 1708 | */ |
| 1709 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1710 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1711 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1712 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1713 | |
| 1714 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1718 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1719 | * it is mapped to. |
| 1720 | * @dev: drm device |
| 1721 | * @data: ioctl data blob |
| 1722 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1723 | * |
| 1724 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1725 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1726 | * |
| 1727 | * IMPORTANT: |
| 1728 | * |
| 1729 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1730 | * mmap support, please don't implement mmap support like here. The modern way |
| 1731 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1732 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1733 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1734 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1735 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1736 | */ |
| 1737 | int |
| 1738 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1739 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1740 | { |
| 1741 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1742 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1743 | unsigned long addr; |
| 1744 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1745 | if (args->flags & ~(I915_MMAP_WC)) |
| 1746 | return -EINVAL; |
| 1747 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1748 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1749 | return -ENODEV; |
| 1750 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1751 | obj = i915_gem_object_lookup(file, args->handle); |
| 1752 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1753 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1754 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1755 | /* prime objects have no backing filp to GEM mmap |
| 1756 | * pages from. |
| 1757 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1758 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1759 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1760 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1761 | } |
| 1762 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1763 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1764 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1765 | args->offset); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1766 | if (args->flags & I915_MMAP_WC) { |
| 1767 | struct mm_struct *mm = current->mm; |
| 1768 | struct vm_area_struct *vma; |
| 1769 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1770 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1771 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1772 | return -EINTR; |
| 1773 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1774 | vma = find_vma(mm, addr); |
| 1775 | if (vma) |
| 1776 | vma->vm_page_prot = |
| 1777 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1778 | else |
| 1779 | addr = -ENOMEM; |
| 1780 | up_write(&mm->mmap_sem); |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1781 | |
| 1782 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1783 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1784 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1785 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1786 | if (IS_ERR((void *)addr)) |
| 1787 | return addr; |
| 1788 | |
| 1789 | args->addr_ptr = (uint64_t) addr; |
| 1790 | |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1794 | static unsigned int tile_row_pages(struct drm_i915_gem_object *obj) |
| 1795 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1796 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1797 | } |
| 1798 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1799 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1800 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1801 | * |
| 1802 | * A history of the GTT mmap interface: |
| 1803 | * |
| 1804 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1805 | * aligned and suitable for fencing, and still fit into the available |
| 1806 | * mappable space left by the pinned display objects. A classic problem |
| 1807 | * we called the page-fault-of-doom where we would ping-pong between |
| 1808 | * two objects that could not fit inside the GTT and so the memcpy |
| 1809 | * would page one object in at the expense of the other between every |
| 1810 | * single byte. |
| 1811 | * |
| 1812 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1813 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1814 | * object is too large for the available space (or simply too large |
| 1815 | * for the mappable aperture!), a view is created instead and faulted |
| 1816 | * into userspace. (This view is aligned and sized appropriately for |
| 1817 | * fenced access.) |
| 1818 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1819 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1820 | * delayed writes via GTT before performing direct access via WC. |
| 1821 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1822 | * Restrictions: |
| 1823 | * |
| 1824 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1825 | * hangs on some architectures, corruption on others. An attempt to service |
| 1826 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1827 | * |
| 1828 | * * the object must be able to fit into RAM (physical memory, though no |
| 1829 | * limited to the mappable aperture). |
| 1830 | * |
| 1831 | * |
| 1832 | * Caveats: |
| 1833 | * |
| 1834 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1835 | * all data to system memory. Subsequent access will not be synchronized. |
| 1836 | * |
| 1837 | * * all mappings are revoked on runtime device suspend. |
| 1838 | * |
| 1839 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1840 | * (older machines require fence register for display and blitter access |
| 1841 | * as well). Contention of the fence registers will cause the previous users |
| 1842 | * to be unmapped and any new access will generate new page faults. |
| 1843 | * |
| 1844 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1845 | * rather than the expected SIGSEGV. |
| 1846 | */ |
| 1847 | int i915_gem_mmap_gtt_version(void) |
| 1848 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1849 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1850 | } |
| 1851 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1852 | static inline struct i915_ggtt_view |
| 1853 | compute_partial_view(struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1854 | pgoff_t page_offset, |
| 1855 | unsigned int chunk) |
| 1856 | { |
| 1857 | struct i915_ggtt_view view; |
| 1858 | |
| 1859 | if (i915_gem_object_is_tiled(obj)) |
| 1860 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1861 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1862 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1863 | view.partial.offset = rounddown(page_offset, chunk); |
| 1864 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1865 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1866 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1867 | |
| 1868 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1869 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1870 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1871 | |
| 1872 | return view; |
| 1873 | } |
| 1874 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1875 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1876 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1877 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1878 | * |
| 1879 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1880 | * from userspace. The fault handler takes care of binding the object to |
| 1881 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1882 | * only if needed based on whether the old reg is still valid or the object |
| 1883 | * is tiled) and inserting a new PTE into the faulting process. |
| 1884 | * |
| 1885 | * Note that the faulting process may involve evicting existing objects |
| 1886 | * from the GTT and/or fence registers to make room. So performance may |
| 1887 | * suffer if the GTT working set is large or there are few fence registers |
| 1888 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1889 | * |
| 1890 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1891 | * 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] | 1892 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1893 | int i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1894 | { |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1895 | #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1896 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1897 | 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] | 1898 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1899 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1900 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1901 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1902 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1903 | pgoff_t page_offset; |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1904 | unsigned int flags; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1905 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1906 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1907 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1908 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1909 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1910 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1911 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1912 | /* Try to flush the object off the GPU first without holding the lock. |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1913 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1914 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1915 | * where we are gazumped. |
| 1916 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1917 | ret = i915_gem_object_wait(obj, |
| 1918 | I915_WAIT_INTERRUPTIBLE, |
| 1919 | MAX_SCHEDULE_TIMEOUT, |
| 1920 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1921 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1922 | goto err; |
| 1923 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1924 | ret = i915_gem_object_pin_pages(obj); |
| 1925 | if (ret) |
| 1926 | goto err; |
| 1927 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1928 | intel_runtime_pm_get(dev_priv); |
| 1929 | |
| 1930 | ret = i915_mutex_lock_interruptible(dev); |
| 1931 | if (ret) |
| 1932 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1933 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1934 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1935 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1936 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1937 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1940 | /* If the object is smaller than a couple of partial vma, it is |
| 1941 | * not worth only creating a single partial vma - we may as well |
| 1942 | * clear enough space for the full object. |
| 1943 | */ |
| 1944 | flags = PIN_MAPPABLE; |
| 1945 | if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT) |
| 1946 | flags |= PIN_NONBLOCK | PIN_NONFAULT; |
| 1947 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1948 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1949 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1950 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1951 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1952 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1953 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1954 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1955 | /* Userspace is now writing through an untracked VMA, abandon |
| 1956 | * all hope that the hardware is able to track future writes. |
| 1957 | */ |
| 1958 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1959 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1960 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); |
| 1961 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1962 | if (IS_ERR(vma)) { |
| 1963 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1964 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1965 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1966 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1967 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1968 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1969 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1970 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1971 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1972 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1973 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1974 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1975 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1976 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1977 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1978 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1979 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1980 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1981 | if (ret) |
| 1982 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1983 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1984 | /* Mark as being mmapped into userspace for later revocation */ |
| 1985 | assert_rpm_wakelock_held(dev_priv); |
| 1986 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1987 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1988 | GEM_BUG_ON(!obj->userfault_count); |
| 1989 | |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1990 | i915_vma_set_ggtt_write(vma); |
| 1991 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1992 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1993 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1994 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1995 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1996 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1997 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1998 | err_rpm: |
| 1999 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 2000 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2001 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2002 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2003 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 2004 | /* |
| 2005 | * We eat errors when the gpu is terminally wedged to avoid |
| 2006 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 2007 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 2008 | * and so needs to be reported. |
| 2009 | */ |
| 2010 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2011 | ret = VM_FAULT_SIGBUS; |
| 2012 | break; |
| 2013 | } |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 2014 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 2015 | /* |
| 2016 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 2017 | * handler to reset everything when re-faulting in |
| 2018 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2019 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 2020 | case 0: |
| 2021 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 2022 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 2023 | case -EBUSY: |
| 2024 | /* |
| 2025 | * EBUSY is ok: this just means that another thread |
| 2026 | * already did the job. |
| 2027 | */ |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2028 | ret = VM_FAULT_NOPAGE; |
| 2029 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2030 | case -ENOMEM: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2031 | ret = VM_FAULT_OOM; |
| 2032 | break; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2033 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2034 | case -EFAULT: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2035 | ret = VM_FAULT_SIGBUS; |
| 2036 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2037 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2038 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2039 | ret = VM_FAULT_SIGBUS; |
| 2040 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2041 | } |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2042 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2043 | } |
| 2044 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2045 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2046 | { |
| 2047 | struct i915_vma *vma; |
| 2048 | |
| 2049 | GEM_BUG_ON(!obj->userfault_count); |
| 2050 | |
| 2051 | obj->userfault_count = 0; |
| 2052 | list_del(&obj->userfault_link); |
| 2053 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2054 | obj->base.dev->anon_inode->i_mapping); |
| 2055 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 2056 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2057 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2058 | } |
| 2059 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2060 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2061 | * i915_gem_release_mmap - remove physical page mappings |
| 2062 | * @obj: obj in question |
| 2063 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2064 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2065 | * relinquish ownership of the pages back to the system. |
| 2066 | * |
| 2067 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2068 | * object through the GTT and then lose the fence register due to |
| 2069 | * resource pressure. Similarly if the object has been moved out of the |
| 2070 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2071 | * mapping will then trigger a page fault on the next user access, allowing |
| 2072 | * fixup by i915_gem_fault(). |
| 2073 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2074 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2075 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2076 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2077 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2078 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2079 | /* Serialisation between user GTT access and our code depends upon |
| 2080 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2081 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2082 | * |
| 2083 | * Note that RPM complicates somewhat by adding an additional |
| 2084 | * requirement that operations to the GGTT be made holding the RPM |
| 2085 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2086 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2087 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2088 | intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2089 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2090 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2091 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2092 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2093 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2094 | |
| 2095 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2096 | * memory transactions from userspace before we return. The TLB |
| 2097 | * flushing implied above by changing the PTE above *should* be |
| 2098 | * sufficient, an extra barrier here just provides us with a bit |
| 2099 | * of paranoid documentation about our requirement to serialise |
| 2100 | * memory writes before touching registers / GSM. |
| 2101 | */ |
| 2102 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2103 | |
| 2104 | out: |
| 2105 | intel_runtime_pm_put(i915); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2108 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2109 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2110 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2111 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2112 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2113 | /* |
| 2114 | * Only called during RPM suspend. All users of the userfault_list |
| 2115 | * must be holding an RPM wakeref to ensure that this can not |
| 2116 | * run concurrently with themselves (and use the struct_mutex for |
| 2117 | * protection between themselves). |
| 2118 | */ |
| 2119 | |
| 2120 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2121 | &dev_priv->mm.userfault_list, userfault_link) |
| 2122 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2123 | |
| 2124 | /* The fence will be lost when the device powers down. If any were |
| 2125 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2126 | * down! All other fences will be reacquired by the user upon waking. |
| 2127 | */ |
| 2128 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2129 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2130 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2131 | /* Ideally we want to assert that the fence register is not |
| 2132 | * live at this point (i.e. that no piece of code will be |
| 2133 | * trying to write through fence + GTT, as that both violates |
| 2134 | * our tracking of activity and associated locking/barriers, |
| 2135 | * but also is illegal given that the hw is powered down). |
| 2136 | * |
| 2137 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2138 | * That is not sufficient, and we need a more fine-grained |
| 2139 | * tool if we want to have a sanity check here. |
| 2140 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2141 | |
| 2142 | if (!reg->vma) |
| 2143 | continue; |
| 2144 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2145 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2146 | reg->dirty = true; |
| 2147 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2150 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2151 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2152 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2153 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2154 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2155 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2156 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2157 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2158 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2159 | /* Attempt to reap some mmap space from dead objects */ |
| 2160 | do { |
| 2161 | err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE); |
| 2162 | if (err) |
| 2163 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2164 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2165 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2166 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2167 | if (!err) |
| 2168 | break; |
| 2169 | |
| 2170 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2171 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2172 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2176 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2177 | drm_gem_free_mmap_offset(&obj->base); |
| 2178 | } |
| 2179 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2180 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2181 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2182 | struct drm_device *dev, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2183 | uint32_t handle, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2184 | uint64_t *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2185 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2186 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2187 | int ret; |
| 2188 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2189 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2190 | if (!obj) |
| 2191 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2192 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2193 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2194 | if (ret == 0) |
| 2195 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2196 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2197 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2198 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2199 | } |
| 2200 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2201 | /** |
| 2202 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2203 | * @dev: DRM device |
| 2204 | * @data: GTT mapping ioctl data |
| 2205 | * @file: GEM object info |
| 2206 | * |
| 2207 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2208 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2209 | * up so we can get faults in the handler above. |
| 2210 | * |
| 2211 | * The fault handler will take care of binding the object into the GTT |
| 2212 | * (since it may have been evicted to make room for something), allocating |
| 2213 | * a fence register, and mapping the appropriate aperture address into |
| 2214 | * userspace. |
| 2215 | */ |
| 2216 | int |
| 2217 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2218 | struct drm_file *file) |
| 2219 | { |
| 2220 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2221 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2222 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2223 | } |
| 2224 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2225 | /* Immediately discard the backing storage */ |
| 2226 | static void |
| 2227 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2228 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2229 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2230 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2231 | if (obj->base.filp == NULL) |
| 2232 | return; |
| 2233 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2234 | /* Our goal here is to return as much of the memory as |
| 2235 | * is possible back to the system as we are called from OOM. |
| 2236 | * To do this we must instruct the shmfs to drop all of its |
| 2237 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2238 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2239 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2240 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2241 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2242 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2243 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2244 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2245 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2246 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2247 | struct address_space *mapping; |
| 2248 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2249 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2250 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2251 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2252 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2253 | case I915_MADV_DONTNEED: |
| 2254 | i915_gem_object_truncate(obj); |
| 2255 | case __I915_MADV_PURGED: |
| 2256 | return; |
| 2257 | } |
| 2258 | |
| 2259 | if (obj->base.filp == NULL) |
| 2260 | return; |
| 2261 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2262 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2263 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2266 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2267 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2268 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2269 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2270 | struct sgt_iter sgt_iter; |
| 2271 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2272 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2273 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2274 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2275 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2276 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2277 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2278 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2279 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2280 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2281 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2282 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2283 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2284 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2285 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2286 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2287 | put_page(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2288 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2289 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2290 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2291 | sg_free_table(pages); |
| 2292 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2293 | } |
| 2294 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2295 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2296 | { |
| 2297 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2298 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2299 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2300 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2301 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2302 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2303 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2304 | } |
| 2305 | |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2306 | void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2307 | enum i915_mm_subclass subclass) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2308 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2309 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2310 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2311 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2312 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2313 | return; |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 2314 | |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 2315 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2316 | if (!i915_gem_object_has_pages(obj)) |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2317 | return; |
| 2318 | |
| 2319 | /* May be called by shrinker from within get_pages() (on another bo) */ |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2320 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2321 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) |
| 2322 | goto unlock; |
Ben Widawsky | 3e12302 | 2013-07-31 17:00:04 -0700 | [diff] [blame] | 2323 | |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2324 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
| 2325 | * array, hence protect them from being reaped by removing them from gtt |
| 2326 | * lists early. */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2327 | pages = fetch_and_zero(&obj->mm.pages); |
| 2328 | GEM_BUG_ON(!pages); |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2329 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2330 | spin_lock(&i915->mm.obj_lock); |
| 2331 | list_del(&obj->mm.link); |
| 2332 | spin_unlock(&i915->mm.obj_lock); |
| 2333 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2334 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2335 | void *ptr; |
| 2336 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2337 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2338 | if (is_vmalloc_addr(ptr)) |
| 2339 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2340 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2341 | kunmap(kmap_to_page(ptr)); |
| 2342 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2343 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2344 | } |
| 2345 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2346 | __i915_gem_object_reset_page_iter(obj); |
| 2347 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2348 | if (!IS_ERR(pages)) |
| 2349 | obj->ops->put_pages(obj, pages); |
| 2350 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2351 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
| 2352 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2353 | unlock: |
| 2354 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2355 | } |
| 2356 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2357 | static bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2358 | { |
| 2359 | struct sg_table new_st; |
| 2360 | struct scatterlist *sg, *new_sg; |
| 2361 | unsigned int i; |
| 2362 | |
| 2363 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2364 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2365 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2366 | 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] | 2367 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2368 | |
| 2369 | new_sg = new_st.sgl; |
| 2370 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2371 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
| 2372 | /* called before being DMA mapped, no need to copy sg->dma_* */ |
| 2373 | new_sg = sg_next(new_sg); |
| 2374 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2375 | 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] | 2376 | |
| 2377 | sg_free_table(orig_st); |
| 2378 | |
| 2379 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2380 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2383 | 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] | 2384 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2385 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2386 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2387 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2388 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2389 | struct sg_table *st; |
| 2390 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2391 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2392 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2393 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2394 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2395 | unsigned int sg_page_sizes; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2396 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2397 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2398 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2399 | /* Assert that the object is not currently in any GPU domain. As it |
| 2400 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2401 | * a GPU cache |
| 2402 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2403 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2404 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2405 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2406 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2407 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2408 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2409 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2410 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2411 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2412 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2413 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | /* Get the list of pages out of our struct file. They'll be pinned |
| 2417 | * at this point until we release them. |
| 2418 | * |
| 2419 | * Fail silently without starting the shrinker |
| 2420 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2421 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2422 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2423 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2424 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2425 | sg = st->sgl; |
| 2426 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2427 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2428 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2429 | const unsigned int shrink[] = { |
| 2430 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2431 | 0, |
| 2432 | }, *s = shrink; |
| 2433 | gfp_t gfp = noreclaim; |
| 2434 | |
| 2435 | do { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2436 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2437 | if (likely(!IS_ERR(page))) |
| 2438 | break; |
| 2439 | |
| 2440 | if (!*s) { |
| 2441 | ret = PTR_ERR(page); |
| 2442 | goto err_sg; |
| 2443 | } |
| 2444 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2445 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2446 | cond_resched(); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2447 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2448 | /* We've tried hard to allocate the memory by reaping |
| 2449 | * our own buffer, now let the real VM do its job and |
| 2450 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2451 | * |
| 2452 | * However, since graphics tend to be disposable, |
| 2453 | * defer the oom here by reporting the ENOMEM back |
| 2454 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2455 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2456 | if (!*s) { |
| 2457 | /* reclaim and warn, but no oom */ |
| 2458 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2459 | |
| 2460 | /* Our bo are always dirty and so we require |
| 2461 | * kswapd to reclaim our pages (direct reclaim |
| 2462 | * does not effectively begin pageout of our |
| 2463 | * buffers on its own). However, direct reclaim |
| 2464 | * only waits for kswapd when under allocation |
| 2465 | * congestion. So as a result __GFP_RECLAIM is |
| 2466 | * unreliable and fails to actually reclaim our |
| 2467 | * dirty pages -- unless you try over and over |
| 2468 | * again with !__GFP_NORETRY. However, we still |
| 2469 | * want to fail this allocation rather than |
| 2470 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2471 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2472 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2473 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2474 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2475 | } while (1); |
| 2476 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2477 | if (!i || |
| 2478 | sg->length >= max_segment || |
| 2479 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2480 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2481 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2482 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2483 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2484 | st->nents++; |
| 2485 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2486 | } else { |
| 2487 | sg->length += PAGE_SIZE; |
| 2488 | } |
| 2489 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2490 | |
| 2491 | /* Check that the i965g/gm workaround works. */ |
| 2492 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2493 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2494 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2495 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2496 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2497 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2498 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2499 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2500 | i915_sg_trim(st); |
| 2501 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2502 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2503 | if (ret) { |
| 2504 | /* DMA remapping failed? One possible cause is that |
| 2505 | * it could not reserve enough large entries, asking |
| 2506 | * for PAGE_SIZE chunks instead may be helpful. |
| 2507 | */ |
| 2508 | if (max_segment > PAGE_SIZE) { |
| 2509 | for_each_sgt_page(page, sgt_iter, st) |
| 2510 | put_page(page); |
| 2511 | sg_free_table(st); |
| 2512 | |
| 2513 | max_segment = PAGE_SIZE; |
| 2514 | goto rebuild_st; |
| 2515 | } else { |
| 2516 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2517 | "Failed to DMA remap %lu pages\n", |
| 2518 | page_count); |
| 2519 | goto err_pages; |
| 2520 | } |
| 2521 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2522 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2523 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2524 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2525 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2526 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2527 | |
| 2528 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2529 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2530 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2531 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2532 | err_pages: |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2533 | for_each_sgt_page(page, sgt_iter, st) |
| 2534 | put_page(page); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2535 | sg_free_table(st); |
| 2536 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2537 | |
| 2538 | /* shmemfs first checks if there is enough memory to allocate the page |
| 2539 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2540 | * ENOMEM for a genuine allocation failure. |
| 2541 | * |
| 2542 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2543 | * space and so want to translate the error from shmemfs back to our |
| 2544 | * usual understanding of ENOMEM. |
| 2545 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2546 | if (ret == -ENOSPC) |
| 2547 | ret = -ENOMEM; |
| 2548 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2549 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2553 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2554 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2555 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2556 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2557 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2558 | int i; |
| 2559 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2560 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2561 | |
| 2562 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2563 | obj->mm.get_page.sg_idx = 0; |
| 2564 | |
| 2565 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2566 | |
| 2567 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2568 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2569 | GEM_BUG_ON(obj->mm.quirked); |
| 2570 | __i915_gem_object_pin_pages(obj); |
| 2571 | obj->mm.quirked = true; |
| 2572 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2573 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2574 | GEM_BUG_ON(!sg_page_sizes); |
| 2575 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2576 | |
| 2577 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2578 | * Calculate the supported page-sizes which fit into the given |
| 2579 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2580 | * to use opportunistically when later inserting into the GTT. For |
| 2581 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2582 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2583 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2584 | */ |
| 2585 | obj->mm.page_sizes.sg = 0; |
| 2586 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2587 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2588 | obj->mm.page_sizes.sg |= BIT(i); |
| 2589 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2590 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2591 | |
| 2592 | spin_lock(&i915->mm.obj_lock); |
| 2593 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2594 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2598 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2599 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2600 | |
| 2601 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2602 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2603 | return -EFAULT; |
| 2604 | } |
| 2605 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2606 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2607 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2608 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2609 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2610 | } |
| 2611 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2612 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2613 | * 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] | 2614 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2615 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2616 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2617 | * or as the object is itself released. |
| 2618 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2619 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2620 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2621 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2622 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2623 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2624 | if (err) |
| 2625 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2626 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2627 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2628 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2629 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2630 | err = ____i915_gem_object_get_pages(obj); |
| 2631 | if (err) |
| 2632 | goto unlock; |
| 2633 | |
| 2634 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2635 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2636 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2637 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2638 | unlock: |
| 2639 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2640 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2643 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2644 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2645 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2646 | { |
| 2647 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2648 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2649 | struct sgt_iter sgt_iter; |
| 2650 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2651 | struct page *stack_pages[32]; |
| 2652 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2653 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2654 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2655 | void *addr; |
| 2656 | |
| 2657 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2658 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2659 | return kmap(sg_page(sgt->sgl)); |
| 2660 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2661 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2662 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2663 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2664 | if (!pages) |
| 2665 | return NULL; |
| 2666 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2667 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2668 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2669 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2670 | |
| 2671 | /* Check that we have the expected number of pages */ |
| 2672 | GEM_BUG_ON(i != n_pages); |
| 2673 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2674 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2675 | default: |
| 2676 | MISSING_CASE(type); |
| 2677 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2678 | case I915_MAP_WB: |
| 2679 | pgprot = PAGE_KERNEL; |
| 2680 | break; |
| 2681 | case I915_MAP_WC: |
| 2682 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2683 | break; |
| 2684 | } |
| 2685 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2686 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2687 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2688 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2689 | |
| 2690 | return addr; |
| 2691 | } |
| 2692 | |
| 2693 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2694 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2695 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2696 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2697 | enum i915_map_type has_type; |
| 2698 | bool pinned; |
| 2699 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2700 | int ret; |
| 2701 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2702 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2703 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2704 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2705 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2706 | if (ret) |
| 2707 | return ERR_PTR(ret); |
| 2708 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2709 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2710 | type &= ~I915_MAP_OVERRIDE; |
| 2711 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2712 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2713 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2714 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2715 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2716 | ret = ____i915_gem_object_get_pages(obj); |
| 2717 | if (ret) |
| 2718 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2719 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2720 | smp_mb__before_atomic(); |
| 2721 | } |
| 2722 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2723 | pinned = false; |
| 2724 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2725 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2726 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2727 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2728 | if (ptr && has_type != type) { |
| 2729 | if (pinned) { |
| 2730 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2731 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2732 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2733 | |
| 2734 | if (is_vmalloc_addr(ptr)) |
| 2735 | vunmap(ptr); |
| 2736 | else |
| 2737 | kunmap(kmap_to_page(ptr)); |
| 2738 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2739 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2740 | } |
| 2741 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2742 | if (!ptr) { |
| 2743 | ptr = i915_gem_object_map(obj, type); |
| 2744 | if (!ptr) { |
| 2745 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2746 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2747 | } |
| 2748 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2749 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2750 | } |
| 2751 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2752 | out_unlock: |
| 2753 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2754 | return ptr; |
| 2755 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2756 | err_unpin: |
| 2757 | atomic_dec(&obj->mm.pages_pin_count); |
| 2758 | err_unlock: |
| 2759 | ptr = ERR_PTR(ret); |
| 2760 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2761 | } |
| 2762 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2763 | static int |
| 2764 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2765 | const struct drm_i915_gem_pwrite *arg) |
| 2766 | { |
| 2767 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2768 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2769 | u64 remain, offset; |
| 2770 | unsigned int pg; |
| 2771 | |
| 2772 | /* Before we instantiate/pin the backing store for our use, we |
| 2773 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2774 | * the pagecache. We avoid the penalty of instantiating all the |
| 2775 | * pages, important if the user is just writing to a few and never |
| 2776 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2777 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2778 | * or clearing-before-use) before it is overwritten. |
| 2779 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2780 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2781 | return -ENODEV; |
| 2782 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2783 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2784 | return -EFAULT; |
| 2785 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2786 | /* Before the pages are instantiated the object is treated as being |
| 2787 | * in the CPU domain. The pages will be clflushed as required before |
| 2788 | * use, and we can freely write into the pages directly. If userspace |
| 2789 | * races pwrite with any other operation; corruption will ensue - |
| 2790 | * that is userspace's prerogative! |
| 2791 | */ |
| 2792 | |
| 2793 | remain = arg->size; |
| 2794 | offset = arg->offset; |
| 2795 | pg = offset_in_page(offset); |
| 2796 | |
| 2797 | do { |
| 2798 | unsigned int len, unwritten; |
| 2799 | struct page *page; |
| 2800 | void *data, *vaddr; |
| 2801 | int err; |
| 2802 | |
| 2803 | len = PAGE_SIZE - pg; |
| 2804 | if (len > remain) |
| 2805 | len = remain; |
| 2806 | |
| 2807 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2808 | offset, len, 0, |
| 2809 | &page, &data); |
| 2810 | if (err < 0) |
| 2811 | return err; |
| 2812 | |
| 2813 | vaddr = kmap(page); |
| 2814 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2815 | kunmap(page); |
| 2816 | |
| 2817 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2818 | offset, len, len - unwritten, |
| 2819 | page, data); |
| 2820 | if (err < 0) |
| 2821 | return err; |
| 2822 | |
| 2823 | if (unwritten) |
| 2824 | return -EFAULT; |
| 2825 | |
| 2826 | remain -= len; |
| 2827 | user_data += len; |
| 2828 | offset += len; |
| 2829 | pg = 0; |
| 2830 | } while (remain); |
| 2831 | |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2835 | static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2836 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2837 | bool banned; |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2838 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2839 | atomic_inc(&ctx->guilty_count); |
| 2840 | |
Chris Wilson | 24eae08 | 2018-02-05 09:22:01 +0000 | [diff] [blame] | 2841 | banned = false; |
| 2842 | if (i915_gem_context_is_bannable(ctx)) { |
| 2843 | unsigned int score; |
| 2844 | |
| 2845 | score = atomic_add_return(CONTEXT_SCORE_GUILTY, |
| 2846 | &ctx->ban_score); |
| 2847 | banned = score >= CONTEXT_SCORE_BAN_THRESHOLD; |
| 2848 | |
| 2849 | DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n", |
| 2850 | ctx->name, score, yesno(banned)); |
| 2851 | } |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2852 | if (!banned) |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2853 | return; |
| 2854 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2855 | i915_gem_context_set_banned(ctx); |
| 2856 | if (!IS_ERR_OR_NULL(ctx->file_priv)) { |
| 2857 | atomic_inc(&ctx->file_priv->context_bans); |
| 2858 | DRM_DEBUG_DRIVER("client %s has had %d context banned\n", |
| 2859 | ctx->name, atomic_read(&ctx->file_priv->context_bans)); |
| 2860 | } |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) |
| 2864 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2865 | atomic_inc(&ctx->active_count); |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2866 | } |
| 2867 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2868 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2869 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2870 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2871 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2872 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2873 | |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2874 | /* We are called by the error capture and reset at a random |
| 2875 | * point in time. In particular, note that neither is crucially |
| 2876 | * ordered with an interrupt. After a hang, the GPU is dead and we |
| 2877 | * assume that no more writes can happen (we waited long enough for |
| 2878 | * all writes that were in transaction to be flushed) - adding an |
| 2879 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2880 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
| 2881 | */ |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2882 | spin_lock_irqsave(&engine->timeline->lock, flags); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 2883 | list_for_each_entry(request, &engine->timeline->requests, link) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2884 | if (__i915_request_completed(request, request->global_seqno)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2885 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2886 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2887 | GEM_BUG_ON(request->engine != engine); |
Chris Wilson | c00122f3 | 2017-02-12 17:19:58 +0000 | [diff] [blame] | 2888 | GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 2889 | &request->fence.flags)); |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2890 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2891 | active = request; |
| 2892 | break; |
| 2893 | } |
| 2894 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 2895 | |
| 2896 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2897 | } |
| 2898 | |
Mika Kuoppala | bf2f043 | 2017-01-17 17:59:04 +0200 | [diff] [blame] | 2899 | static bool engine_stalled(struct intel_engine_cs *engine) |
| 2900 | { |
| 2901 | if (!engine->hangcheck.stalled) |
| 2902 | return false; |
| 2903 | |
| 2904 | /* Check for possible seqno movement after hang declaration */ |
| 2905 | if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) { |
| 2906 | DRM_DEBUG_DRIVER("%s pardoned\n", engine->name); |
| 2907 | return false; |
| 2908 | } |
| 2909 | |
| 2910 | return true; |
| 2911 | } |
| 2912 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2913 | /* |
| 2914 | * Ensure irq handler finishes, and not run again. |
| 2915 | * Also return the active request so that we only search for it once. |
| 2916 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2917 | struct i915_request * |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2918 | i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) |
| 2919 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2920 | struct i915_request *request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2921 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 2922 | /* |
| 2923 | * During the reset sequence, we must prevent the engine from |
| 2924 | * entering RC6. As the context state is undefined until we restart |
| 2925 | * the engine, if it does enter RC6 during the reset, the state |
| 2926 | * written to the powercontext is undefined and so we may lose |
| 2927 | * GPU state upon resume, i.e. fail to restart after a reset. |
| 2928 | */ |
| 2929 | intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); |
| 2930 | |
| 2931 | /* |
| 2932 | * Prevent the signaler thread from updating the request |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2933 | * state (by calling dma_fence_signal) as we are processing |
| 2934 | * the reset. The write from the GPU of the seqno is |
| 2935 | * asynchronous and the signaler thread may see a different |
| 2936 | * value to us and declare the request complete, even though |
| 2937 | * the reset routine have picked that request as the active |
| 2938 | * (incomplete) request. This conflict is not handled |
| 2939 | * gracefully! |
| 2940 | */ |
| 2941 | kthread_park(engine->breadcrumbs.signaler); |
| 2942 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 2943 | /* |
| 2944 | * Prevent request submission to the hardware until we have |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2945 | * completed the reset in i915_gem_reset_finish(). If a request |
| 2946 | * is completed by one engine, it may then queue a request |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2947 | * to a second via its execlists->tasklet *just* as we are |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2948 | * calling engine->init_hw() and also writing the ELSP. |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2949 | * Turning off the execlists->tasklet until the reset is over |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2950 | * prevents the race. |
Chris Wilson | 68ad361 | 2018-03-07 13:42:26 +0000 | [diff] [blame] | 2951 | * |
| 2952 | * Note that this needs to be a single atomic operation on the |
| 2953 | * tasklet (flush existing tasks, prevent new tasks) to prevent |
| 2954 | * a race between reset and set-wedged. It is not, so we do the best |
| 2955 | * we can atm and make sure we don't lock the machine up in the more |
| 2956 | * common case of recursively being called from set-wedged from inside |
| 2957 | * i915_reset. |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2958 | */ |
Chris Wilson | 68ad361 | 2018-03-07 13:42:26 +0000 | [diff] [blame] | 2959 | if (!atomic_read(&engine->execlists.tasklet.count)) |
| 2960 | tasklet_kill(&engine->execlists.tasklet); |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2961 | tasklet_disable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2962 | |
Michał Winiarski | c41937f | 2017-10-26 15:35:58 +0200 | [diff] [blame] | 2963 | /* |
| 2964 | * We're using worker to queue preemption requests from the tasklet in |
| 2965 | * GuC submission mode. |
| 2966 | * Even though tasklet was disabled, we may still have a worker queued. |
| 2967 | * Let's make sure that all workers scheduled before disabling the |
| 2968 | * tasklet are completed before continuing with the reset. |
| 2969 | */ |
| 2970 | if (engine->i915->guc.preempt_wq) |
| 2971 | flush_workqueue(engine->i915->guc.preempt_wq); |
| 2972 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2973 | if (engine->irq_seqno_barrier) |
| 2974 | engine->irq_seqno_barrier(engine); |
| 2975 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 2976 | request = i915_gem_find_active_request(engine); |
| 2977 | if (request && request->fence.error == -EIO) |
| 2978 | request = ERR_PTR(-EIO); /* Previous reset failed! */ |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2979 | |
| 2980 | return request; |
| 2981 | } |
| 2982 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2983 | int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2984 | { |
| 2985 | struct intel_engine_cs *engine; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2986 | struct i915_request *request; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2987 | enum intel_engine_id id; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2988 | int err = 0; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2989 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2990 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2991 | request = i915_gem_reset_prepare_engine(engine); |
| 2992 | if (IS_ERR(request)) { |
| 2993 | err = PTR_ERR(request); |
| 2994 | continue; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2995 | } |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 2996 | |
| 2997 | engine->hangcheck.active_request = request; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2998 | } |
| 2999 | |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3000 | i915_gem_revoke_fences(dev_priv); |
Michal Wajdeczko | c37d572 | 2018-03-12 13:03:07 +0000 | [diff] [blame] | 3001 | intel_uc_sanitize(dev_priv); |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3002 | |
| 3003 | return err; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3004 | } |
| 3005 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3006 | static void skip_request(struct i915_request *request) |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 3007 | { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3008 | void *vaddr = request->ring->vaddr; |
| 3009 | u32 head; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 3010 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3011 | /* As this request likely depends on state from the lost |
| 3012 | * context, clear out all the user operations leaving the |
| 3013 | * breadcrumb at the end (so we get the fence notifications). |
| 3014 | */ |
| 3015 | head = request->head; |
| 3016 | if (request->postfix < head) { |
| 3017 | memset(vaddr + head, 0, request->ring->size - head); |
| 3018 | head = 0; |
| 3019 | } |
| 3020 | memset(vaddr + head, 0, request->postfix - head); |
Chris Wilson | c0d5f32 | 2017-01-10 17:22:43 +0000 | [diff] [blame] | 3021 | |
| 3022 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3025 | static void engine_skip_context(struct i915_request *request) |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3026 | { |
| 3027 | struct intel_engine_cs *engine = request->engine; |
| 3028 | struct i915_gem_context *hung_ctx = request->ctx; |
| 3029 | struct intel_timeline *timeline; |
| 3030 | unsigned long flags; |
| 3031 | |
| 3032 | timeline = i915_gem_context_lookup_timeline(hung_ctx, engine); |
| 3033 | |
| 3034 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3035 | spin_lock(&timeline->lock); |
| 3036 | |
| 3037 | list_for_each_entry_continue(request, &engine->timeline->requests, link) |
| 3038 | if (request->ctx == hung_ctx) |
| 3039 | skip_request(request); |
| 3040 | |
| 3041 | list_for_each_entry(request, &timeline->requests, link) |
| 3042 | skip_request(request); |
| 3043 | |
| 3044 | spin_unlock(&timeline->lock); |
| 3045 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 3046 | } |
| 3047 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3048 | /* Returns the request if it was guilty of the hang */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3049 | static struct i915_request * |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3050 | i915_gem_reset_request(struct intel_engine_cs *engine, |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3051 | struct i915_request *request) |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3052 | { |
Mika Kuoppala | 71895a0 | 2017-01-17 17:59:07 +0200 | [diff] [blame] | 3053 | /* The guilty request will get skipped on a hung engine. |
| 3054 | * |
| 3055 | * Users of client default contexts do not rely on logical |
| 3056 | * state preserved between batches so it is safe to execute |
| 3057 | * queued requests following the hang. Non default contexts |
| 3058 | * rely on preserved state, so skipping a batch loses the |
| 3059 | * evolution of the state and it needs to be considered corrupted. |
| 3060 | * Executing more queued batches on top of corrupted state is |
| 3061 | * risky. But we take the risk by trying to advance through |
| 3062 | * the queued requests in order to make the client behaviour |
| 3063 | * more predictable around resets, by not throwing away random |
| 3064 | * amount of batches it has prepared for execution. Sophisticated |
| 3065 | * clients can use gem_reset_stats_ioctl and dma fence status |
| 3066 | * (exported via sync_file info ioctl on explicit fences) to observe |
| 3067 | * when it loses the context state and should rebuild accordingly. |
| 3068 | * |
| 3069 | * The context ban, and ultimately the client ban, mechanism are safety |
| 3070 | * valves if client submission ends up resulting in nothing more than |
| 3071 | * subsequent hangs. |
| 3072 | */ |
| 3073 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3074 | if (engine_stalled(engine)) { |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3075 | i915_gem_context_mark_guilty(request->ctx); |
| 3076 | skip_request(request); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3077 | |
| 3078 | /* If this context is now banned, skip all pending requests. */ |
| 3079 | if (i915_gem_context_is_banned(request->ctx)) |
| 3080 | engine_skip_context(request); |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3081 | } else { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3082 | /* |
| 3083 | * Since this is not the hung engine, it may have advanced |
| 3084 | * since the hang declaration. Double check by refinding |
| 3085 | * the active request at the time of the reset. |
| 3086 | */ |
| 3087 | request = i915_gem_find_active_request(engine); |
| 3088 | if (request) { |
| 3089 | i915_gem_context_mark_innocent(request->ctx); |
| 3090 | dma_fence_set_error(&request->fence, -EAGAIN); |
| 3091 | |
| 3092 | /* Rewind the engine to replay the incomplete rq */ |
| 3093 | spin_lock_irq(&engine->timeline->lock); |
| 3094 | request = list_prev_entry(request, link); |
| 3095 | if (&request->link == &engine->timeline->requests) |
| 3096 | request = NULL; |
| 3097 | spin_unlock_irq(&engine->timeline->lock); |
| 3098 | } |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3099 | } |
| 3100 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3101 | return request; |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3102 | } |
| 3103 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3104 | void i915_gem_reset_engine(struct intel_engine_cs *engine, |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3105 | struct i915_request *request) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3106 | { |
Chris Wilson | fcb1de5 | 2017-12-19 09:01:10 +0000 | [diff] [blame] | 3107 | /* |
| 3108 | * Make sure this write is visible before we re-enable the interrupt |
| 3109 | * handlers on another CPU, as tasklet_enable() resolves to just |
| 3110 | * a compiler barrier which is insufficient for our purpose here. |
| 3111 | */ |
| 3112 | smp_store_mb(engine->irq_posted, 0); |
Chris Wilson | ed454f2 | 2017-07-21 13:32:29 +0100 | [diff] [blame] | 3113 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3114 | if (request) |
| 3115 | request = i915_gem_reset_request(engine, request); |
| 3116 | |
| 3117 | if (request) { |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3118 | DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n", |
| 3119 | engine->name, request->global_seqno); |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3120 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3121 | |
| 3122 | /* Setup the CS to resume from the breadcrumb of the hung request */ |
| 3123 | engine->reset_hw(engine, request); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3124 | } |
| 3125 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3126 | void i915_gem_reset(struct drm_i915_private *dev_priv) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3127 | { |
| 3128 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3129 | enum intel_engine_id id; |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3130 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3131 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 3132 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3133 | i915_retire_requests(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3134 | |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3135 | for_each_engine(engine, dev_priv, id) { |
| 3136 | struct i915_gem_context *ctx; |
| 3137 | |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3138 | i915_gem_reset_engine(engine, engine->hangcheck.active_request); |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3139 | ctx = fetch_and_zero(&engine->last_retired_context); |
| 3140 | if (ctx) |
| 3141 | engine->context_unpin(engine, ctx); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3142 | |
| 3143 | /* |
| 3144 | * Ostensibily, we always want a context loaded for powersaving, |
| 3145 | * so if the engine is idle after the reset, send a request |
| 3146 | * to load our scratch kernel_context. |
| 3147 | * |
| 3148 | * More mysteriously, if we leave the engine idle after a reset, |
| 3149 | * the next userspace batch may hang, with what appears to be |
| 3150 | * an incoherent read by the CS (presumably stale TLB). An |
| 3151 | * empty request appears sufficient to paper over the glitch. |
| 3152 | */ |
Chris Wilson | 01b8fdc | 2018-02-05 15:24:31 +0000 | [diff] [blame] | 3153 | if (intel_engine_is_idle(engine)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3154 | struct i915_request *rq; |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3155 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3156 | rq = i915_request_alloc(engine, |
| 3157 | dev_priv->kernel_context); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3158 | if (!IS_ERR(rq)) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3159 | __i915_request_add(rq, false); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3160 | } |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3161 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3162 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 3163 | i915_gem_restore_fences(dev_priv); |
Chris Wilson | f2a91d1 | 2016-09-21 14:51:06 +0100 | [diff] [blame] | 3164 | |
| 3165 | if (dev_priv->gt.awake) { |
| 3166 | intel_sanitize_gt_powersave(dev_priv); |
| 3167 | intel_enable_gt_powersave(dev_priv); |
| 3168 | if (INTEL_GEN(dev_priv) >= 6) |
| 3169 | gen6_rps_busy(dev_priv); |
| 3170 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3171 | } |
| 3172 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3173 | void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) |
| 3174 | { |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3175 | tasklet_enable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3176 | kthread_unpark(engine->breadcrumbs.signaler); |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3177 | |
| 3178 | intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3179 | } |
| 3180 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3181 | void i915_gem_reset_finish(struct drm_i915_private *dev_priv) |
| 3182 | { |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3183 | struct intel_engine_cs *engine; |
| 3184 | enum intel_engine_id id; |
| 3185 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3186 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3187 | |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3188 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3189 | engine->hangcheck.active_request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3190 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3191 | } |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3194 | static void nop_submit_request(struct i915_request *request) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3195 | { |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3196 | dma_fence_set_error(&request->fence, -EIO); |
| 3197 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3198 | i915_request_submit(request); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3199 | } |
| 3200 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3201 | static void nop_complete_submit_request(struct i915_request *request) |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3202 | { |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3203 | unsigned long flags; |
| 3204 | |
Chris Wilson | 3cd9442 | 2017-01-10 17:22:45 +0000 | [diff] [blame] | 3205 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3206 | |
| 3207 | spin_lock_irqsave(&request->engine->timeline->lock, flags); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3208 | __i915_request_submit(request); |
Chris Wilson | 3dcf93f | 2016-11-22 14:41:20 +0000 | [diff] [blame] | 3209 | intel_engine_init_global_seqno(request->engine, request->global_seqno); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3210 | spin_unlock_irqrestore(&request->engine->timeline->lock, flags); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3211 | } |
| 3212 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3213 | void i915_gem_set_wedged(struct drm_i915_private *i915) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3214 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 3215 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3216 | enum intel_engine_id id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3217 | |
Chris Wilson | 559e040 | 2018-02-05 09:21:59 +0000 | [diff] [blame] | 3218 | if (drm_debug & DRM_UT_DRIVER) { |
| 3219 | struct drm_printer p = drm_debug_printer(__func__); |
| 3220 | |
| 3221 | for_each_engine(engine, i915, id) |
| 3222 | intel_engine_dump(engine, &p, "%s\n", engine->name); |
| 3223 | } |
| 3224 | |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3225 | set_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3226 | smp_mb__after_atomic(); |
| 3227 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3228 | /* |
| 3229 | * First, stop submission to hw, but do not yet complete requests by |
| 3230 | * rolling the global seqno forward (since this would complete requests |
| 3231 | * for which we haven't set the fence error to EIO yet). |
| 3232 | */ |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3233 | for_each_engine(engine, i915, id) { |
| 3234 | i915_gem_reset_prepare_engine(engine); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3235 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3236 | engine->submit_request = nop_submit_request; |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3237 | engine->schedule = NULL; |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3238 | } |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3239 | i915->caps.scheduler = 0; |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3240 | |
| 3241 | /* |
| 3242 | * Make sure no one is running the old callback before we proceed with |
| 3243 | * cancelling requests and resetting the completion tracking. Otherwise |
| 3244 | * we might submit a request to the hardware which never completes. |
| 3245 | */ |
| 3246 | synchronize_rcu(); |
| 3247 | |
| 3248 | for_each_engine(engine, i915, id) { |
| 3249 | /* Mark all executing requests as skipped */ |
| 3250 | engine->cancel_requests(engine); |
| 3251 | |
| 3252 | /* |
| 3253 | * Only once we've force-cancelled all in-flight requests can we |
| 3254 | * start to complete all requests. |
| 3255 | */ |
| 3256 | engine->submit_request = nop_complete_submit_request; |
| 3257 | } |
| 3258 | |
| 3259 | /* |
| 3260 | * Make sure no request can slip through without getting completed by |
| 3261 | * either this call here to intel_engine_init_global_seqno, or the one |
| 3262 | * in nop_complete_submit_request. |
| 3263 | */ |
| 3264 | synchronize_rcu(); |
| 3265 | |
| 3266 | for_each_engine(engine, i915, id) { |
| 3267 | unsigned long flags; |
| 3268 | |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3269 | /* |
| 3270 | * Mark all pending requests as complete so that any concurrent |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3271 | * (lockless) lookup doesn't try and wait upon the request as we |
| 3272 | * reset it. |
| 3273 | */ |
| 3274 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3275 | intel_engine_init_global_seqno(engine, |
| 3276 | intel_engine_last_submit(engine)); |
| 3277 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3278 | |
| 3279 | i915_gem_reset_finish_engine(engine); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3280 | } |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3281 | |
Chris Wilson | 3d7adbb | 2017-07-21 13:32:27 +0100 | [diff] [blame] | 3282 | wake_up_all(&i915->gpu_error.reset_queue); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3283 | } |
| 3284 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3285 | bool i915_gem_unset_wedged(struct drm_i915_private *i915) |
| 3286 | { |
| 3287 | struct i915_gem_timeline *tl; |
| 3288 | int i; |
| 3289 | |
| 3290 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3291 | if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3292 | return true; |
| 3293 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3294 | /* |
| 3295 | * Before unwedging, make sure that all pending operations |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3296 | * are flushed and errored out - we may have requests waiting upon |
| 3297 | * third party fences. We marked all inflight requests as EIO, and |
| 3298 | * every execbuf since returned EIO, for consistency we want all |
| 3299 | * the currently pending requests to also be marked as EIO, which |
| 3300 | * is done inside our nop_submit_request - and so we must wait. |
| 3301 | * |
| 3302 | * No more can be submitted until we reset the wedged bit. |
| 3303 | */ |
| 3304 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3305 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3306 | struct i915_request *rq; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3307 | |
| 3308 | rq = i915_gem_active_peek(&tl->engine[i].last_request, |
| 3309 | &i915->drm.struct_mutex); |
| 3310 | if (!rq) |
| 3311 | continue; |
| 3312 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3313 | /* |
| 3314 | * We can't use our normal waiter as we want to |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3315 | * avoid recursively trying to handle the current |
| 3316 | * reset. The basic dma_fence_default_wait() installs |
| 3317 | * a callback for dma_fence_signal(), which is |
| 3318 | * triggered by our nop handler (indirectly, the |
| 3319 | * callback enables the signaler thread which is |
| 3320 | * woken by the nop_submit_request() advancing the seqno |
| 3321 | * and when the seqno passes the fence, the signaler |
| 3322 | * then signals the fence waking us up). |
| 3323 | */ |
| 3324 | if (dma_fence_default_wait(&rq->fence, true, |
| 3325 | MAX_SCHEDULE_TIMEOUT) < 0) |
| 3326 | return false; |
| 3327 | } |
| 3328 | } |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3329 | i915_retire_requests(i915); |
| 3330 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3331 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3332 | /* |
| 3333 | * Undo nop_submit_request. We prevent all new i915 requests from |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3334 | * being queued (by disallowing execbuf whilst wedged) so having |
| 3335 | * waited for all active requests above, we know the system is idle |
| 3336 | * and do not have to worry about a thread being inside |
| 3337 | * engine->submit_request() as we swap over. So unlike installing |
| 3338 | * the nop_submit_request on reset, we can do this from normal |
| 3339 | * context and do not require stop_machine(). |
| 3340 | */ |
| 3341 | intel_engines_reset_default_submission(i915); |
Chris Wilson | 36703e7 | 2017-06-22 11:56:25 +0100 | [diff] [blame] | 3342 | i915_gem_contexts_lost(i915); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3343 | |
| 3344 | smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ |
| 3345 | clear_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3346 | |
| 3347 | return true; |
| 3348 | } |
| 3349 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 3350 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3351 | i915_gem_retire_work_handler(struct work_struct *work) |
| 3352 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3353 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3354 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 3355 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3356 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3357 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3358 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3359 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3360 | mutex_unlock(&dev->struct_mutex); |
| 3361 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3362 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3363 | /* |
| 3364 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3365 | * We do not need to do this test under locking as in the worst-case |
| 3366 | * we queue the retire worker once too often. |
| 3367 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3368 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3369 | queue_delayed_work(dev_priv->wq, |
| 3370 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 3371 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3372 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3373 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3374 | static void shrink_caches(struct drm_i915_private *i915) |
| 3375 | { |
| 3376 | /* |
| 3377 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 3378 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 3379 | * with the aim of reducing fragmentation. |
| 3380 | */ |
| 3381 | kmem_cache_shrink(i915->priorities); |
| 3382 | kmem_cache_shrink(i915->dependencies); |
| 3383 | kmem_cache_shrink(i915->requests); |
| 3384 | kmem_cache_shrink(i915->luts); |
| 3385 | kmem_cache_shrink(i915->vmas); |
| 3386 | kmem_cache_shrink(i915->objects); |
| 3387 | } |
| 3388 | |
| 3389 | struct sleep_rcu_work { |
| 3390 | union { |
| 3391 | struct rcu_head rcu; |
| 3392 | struct work_struct work; |
| 3393 | }; |
| 3394 | struct drm_i915_private *i915; |
| 3395 | unsigned int epoch; |
| 3396 | }; |
| 3397 | |
| 3398 | static inline bool |
| 3399 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 3400 | { |
| 3401 | /* |
| 3402 | * There is a small chance that the epoch wrapped since we started |
| 3403 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 3404 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 3405 | */ |
| 3406 | return epoch == READ_ONCE(i915->gt.epoch); |
| 3407 | } |
| 3408 | |
| 3409 | static void __sleep_work(struct work_struct *work) |
| 3410 | { |
| 3411 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 3412 | struct drm_i915_private *i915 = s->i915; |
| 3413 | unsigned int epoch = s->epoch; |
| 3414 | |
| 3415 | kfree(s); |
| 3416 | if (same_epoch(i915, epoch)) |
| 3417 | shrink_caches(i915); |
| 3418 | } |
| 3419 | |
| 3420 | static void __sleep_rcu(struct rcu_head *rcu) |
| 3421 | { |
| 3422 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 3423 | struct drm_i915_private *i915 = s->i915; |
| 3424 | |
| 3425 | if (same_epoch(i915, s->epoch)) { |
| 3426 | INIT_WORK(&s->work, __sleep_work); |
| 3427 | queue_work(i915->wq, &s->work); |
| 3428 | } else { |
| 3429 | kfree(s); |
| 3430 | } |
| 3431 | } |
| 3432 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3433 | static inline bool |
| 3434 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 3435 | { |
| 3436 | return (READ_ONCE(i915->gt.active_requests) || |
| 3437 | work_pending(&i915->gt.idle_work.work)); |
| 3438 | } |
| 3439 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3440 | static void |
| 3441 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3442 | { |
| 3443 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3444 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3445 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3446 | bool rearm_hangcheck; |
| 3447 | |
| 3448 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3449 | return; |
| 3450 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3451 | /* |
| 3452 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3453 | * new request is submitted. As we don't trust the hardware, we |
| 3454 | * continue on if the wait times out. This is necessary to allow |
| 3455 | * the machine to suspend even if the hardware dies, and we will |
| 3456 | * try to recover in resume (after depriving the hardware of power, |
| 3457 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3458 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3459 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3460 | intel_engines_are_idle(dev_priv), |
| 3461 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3462 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3463 | |
| 3464 | rearm_hangcheck = |
| 3465 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3466 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3467 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3468 | /* Currently busy, come back later */ |
| 3469 | mod_delayed_work(dev_priv->wq, |
| 3470 | &dev_priv->gt.idle_work, |
| 3471 | msecs_to_jiffies(50)); |
| 3472 | goto out_rearm; |
| 3473 | } |
| 3474 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3475 | /* |
| 3476 | * New request retired after this work handler started, extend active |
| 3477 | * period until next instance of the work. |
| 3478 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3479 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3480 | goto out_unlock; |
| 3481 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3482 | /* |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3483 | * Be paranoid and flush a concurrent interrupt to make sure |
| 3484 | * we don't reactivate any irq tasklets after parking. |
| 3485 | * |
| 3486 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 3487 | * there may still be an in-flight interrupt even though the CSB |
| 3488 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 3489 | * is completed before we continue, but it doesn't prevent the HW from |
| 3490 | * raising a spurious interrupt later. To complete the shield we should |
| 3491 | * coordinate disabling the CS irq with flushing the interrupts. |
| 3492 | */ |
| 3493 | synchronize_irq(dev_priv->drm.irq); |
| 3494 | |
Chris Wilson | aba5e27 | 2017-10-25 15:39:41 +0100 | [diff] [blame] | 3495 | intel_engines_park(dev_priv); |
Chris Wilson | d02a1d8 | 2017-11-27 12:30:54 +0000 | [diff] [blame] | 3496 | i915_gem_timelines_park(dev_priv); |
| 3497 | |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 3498 | i915_pmu_gt_parked(dev_priv); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3499 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3500 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3501 | dev_priv->gt.awake = false; |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3502 | epoch = dev_priv->gt.epoch; |
| 3503 | GEM_BUG_ON(epoch == I915_EPOCH_INVALID); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3504 | rearm_hangcheck = false; |
Daniel Vetter | 30ecad7 | 2015-12-09 09:29:36 +0100 | [diff] [blame] | 3505 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3506 | if (INTEL_GEN(dev_priv) >= 6) |
| 3507 | gen6_rps_idle(dev_priv); |
Tvrtko Ursulin | b687637 | 2017-12-05 13:28:54 +0000 | [diff] [blame] | 3508 | |
| 3509 | intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ); |
| 3510 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3511 | intel_runtime_pm_put(dev_priv); |
| 3512 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3513 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3514 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3515 | out_rearm: |
| 3516 | if (rearm_hangcheck) { |
| 3517 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3518 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3519 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3520 | |
| 3521 | /* |
| 3522 | * When we are idle, it is an opportune time to reap our caches. |
| 3523 | * However, we have many objects that utilise RCU and the ordered |
| 3524 | * i915->wq that this work is executing on. To try and flush any |
| 3525 | * pending frees now we are idle, we first wait for an RCU grace |
| 3526 | * period, and then queue a task (that will run last on the wq) to |
| 3527 | * shrink and re-optimize the caches. |
| 3528 | */ |
| 3529 | if (same_epoch(dev_priv, epoch)) { |
| 3530 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3531 | if (s) { |
| 3532 | s->i915 = dev_priv; |
| 3533 | s->epoch = epoch; |
| 3534 | call_rcu(&s->rcu, __sleep_rcu); |
| 3535 | } |
| 3536 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3539 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3540 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3541 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3542 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3543 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3544 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3545 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3546 | mutex_lock(&i915->drm.struct_mutex); |
| 3547 | |
| 3548 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3549 | struct i915_gem_context *ctx = lut->ctx; |
| 3550 | struct i915_vma *vma; |
| 3551 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3552 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3553 | if (ctx->file_priv != fpriv) |
| 3554 | continue; |
| 3555 | |
| 3556 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3557 | GEM_BUG_ON(vma->obj != obj); |
| 3558 | |
| 3559 | /* We allow the process to have multiple handles to the same |
| 3560 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3561 | */ |
| 3562 | GEM_BUG_ON(!vma->open_count); |
| 3563 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3564 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3565 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3566 | list_del(&lut->obj_link); |
| 3567 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3568 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3569 | kmem_cache_free(i915->luts, lut); |
| 3570 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3571 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3572 | |
| 3573 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3574 | } |
| 3575 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3576 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3577 | { |
| 3578 | if (timeout_ns < 0) |
| 3579 | return MAX_SCHEDULE_TIMEOUT; |
| 3580 | |
| 3581 | if (timeout_ns == 0) |
| 3582 | return 0; |
| 3583 | |
| 3584 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3585 | } |
| 3586 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3587 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3588 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3589 | * @dev: drm device pointer |
| 3590 | * @data: ioctl data blob |
| 3591 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3592 | * |
| 3593 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3594 | * the timeout parameter. |
| 3595 | * -ETIME: object is still busy after timeout |
| 3596 | * -ERESTARTSYS: signal interrupted the wait |
| 3597 | * -ENONENT: object doesn't exist |
| 3598 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3599 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3600 | * -ENOMEM: damn |
| 3601 | * -ENODEV: Internal IRQ fail |
| 3602 | * -E?: The add request failed |
| 3603 | * |
| 3604 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3605 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3606 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3607 | * without holding struct_mutex the object may become re-busied before this |
| 3608 | * function completes. A similar but shorter * race condition exists in the busy |
| 3609 | * ioctl |
| 3610 | */ |
| 3611 | int |
| 3612 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3613 | { |
| 3614 | struct drm_i915_gem_wait *args = data; |
| 3615 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3616 | ktime_t start; |
| 3617 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3618 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3619 | if (args->flags != 0) |
| 3620 | return -EINVAL; |
| 3621 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3622 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3623 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3624 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3625 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3626 | start = ktime_get(); |
| 3627 | |
| 3628 | ret = i915_gem_object_wait(obj, |
| 3629 | I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL, |
| 3630 | to_wait_timeout(args->timeout_ns), |
| 3631 | to_rps_client(file)); |
| 3632 | |
| 3633 | if (args->timeout_ns > 0) { |
| 3634 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3635 | if (args->timeout_ns < 0) |
| 3636 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3637 | |
| 3638 | /* |
| 3639 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3640 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3641 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3642 | * |
| 3643 | * This is a regression from the timespec->ktime conversion. |
| 3644 | */ |
| 3645 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3646 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3647 | |
| 3648 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3649 | if (ret == -ETIME && args->timeout_ns) |
| 3650 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3651 | } |
| 3652 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3653 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3654 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3655 | } |
| 3656 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3657 | static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags) |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3658 | { |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3659 | int ret, i; |
| 3660 | |
| 3661 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
| 3662 | ret = i915_gem_active_wait(&tl->engine[i].last_request, flags); |
| 3663 | if (ret) |
| 3664 | return ret; |
| 3665 | } |
| 3666 | |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3670 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3671 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3672 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3673 | dev_err(i915->drm.dev, |
| 3674 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3675 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3676 | i915_gem_set_wedged(i915); |
| 3677 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | return 0; |
| 3681 | } |
| 3682 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3683 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags) |
| 3684 | { |
Dave Gordon | b4ac5af | 2016-03-24 11:20:38 +0000 | [diff] [blame] | 3685 | int ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3686 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3687 | /* If the device is asleep, we have no requests outstanding */ |
| 3688 | if (!READ_ONCE(i915->gt.awake)) |
| 3689 | return 0; |
| 3690 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3691 | if (flags & I915_WAIT_LOCKED) { |
| 3692 | struct i915_gem_timeline *tl; |
| 3693 | |
| 3694 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3695 | |
| 3696 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3697 | ret = wait_for_timeline(tl, flags); |
| 3698 | if (ret) |
| 3699 | return ret; |
| 3700 | } |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3701 | i915_retire_requests(i915); |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3702 | |
| 3703 | ret = wait_for_engines(i915); |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3704 | } else { |
| 3705 | ret = wait_for_timeline(&i915->gt.global_timeline, flags); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 3706 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3707 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3708 | return ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3709 | } |
| 3710 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3711 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3712 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3713 | /* |
| 3714 | * We manually flush the CPU domain so that we can override and |
| 3715 | * force the flush for the display, and perform it asyncrhonously. |
| 3716 | */ |
| 3717 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3718 | if (obj->cache_dirty) |
| 3719 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3720 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3724 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3725 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3726 | return; |
| 3727 | |
| 3728 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3729 | __i915_gem_object_flush_for_display(obj); |
| 3730 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3731 | } |
| 3732 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3733 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3734 | * Moves a single object to the WC read, and possibly write domain. |
| 3735 | * @obj: object to act on |
| 3736 | * @write: ask for write access or read only |
| 3737 | * |
| 3738 | * This function returns when the move is complete, including waiting on |
| 3739 | * flushes to occur. |
| 3740 | */ |
| 3741 | int |
| 3742 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3743 | { |
| 3744 | int ret; |
| 3745 | |
| 3746 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3747 | |
| 3748 | ret = i915_gem_object_wait(obj, |
| 3749 | I915_WAIT_INTERRUPTIBLE | |
| 3750 | I915_WAIT_LOCKED | |
| 3751 | (write ? I915_WAIT_ALL : 0), |
| 3752 | MAX_SCHEDULE_TIMEOUT, |
| 3753 | NULL); |
| 3754 | if (ret) |
| 3755 | return ret; |
| 3756 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3757 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3758 | return 0; |
| 3759 | |
| 3760 | /* Flush and acquire obj->pages so that we are coherent through |
| 3761 | * direct access in memory with previous cached writes through |
| 3762 | * shmemfs and that our cache domain tracking remains valid. |
| 3763 | * For example, if the obj->filp was moved to swap without us |
| 3764 | * being notified and releasing the pages, we would mistakenly |
| 3765 | * continue to assume that the obj remained out of the CPU cached |
| 3766 | * domain. |
| 3767 | */ |
| 3768 | ret = i915_gem_object_pin_pages(obj); |
| 3769 | if (ret) |
| 3770 | return ret; |
| 3771 | |
| 3772 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3773 | |
| 3774 | /* Serialise direct access to this object with the barriers for |
| 3775 | * coherent writes from the GPU, by effectively invalidating the |
| 3776 | * WC domain upon first access. |
| 3777 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3778 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3779 | mb(); |
| 3780 | |
| 3781 | /* It should now be out of any other write domains, and we can update |
| 3782 | * the domain values for our changes. |
| 3783 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3784 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3785 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3786 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3787 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3788 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3789 | obj->mm.dirty = true; |
| 3790 | } |
| 3791 | |
| 3792 | i915_gem_object_unpin_pages(obj); |
| 3793 | return 0; |
| 3794 | } |
| 3795 | |
| 3796 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3797 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3798 | * @obj: object to act on |
| 3799 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3800 | * |
| 3801 | * This function returns when the move is complete, including waiting on |
| 3802 | * flushes to occur. |
| 3803 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3804 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3805 | 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] | 3806 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3807 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3808 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3809 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3810 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3811 | ret = i915_gem_object_wait(obj, |
| 3812 | I915_WAIT_INTERRUPTIBLE | |
| 3813 | I915_WAIT_LOCKED | |
| 3814 | (write ? I915_WAIT_ALL : 0), |
| 3815 | MAX_SCHEDULE_TIMEOUT, |
| 3816 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3817 | if (ret) |
| 3818 | return ret; |
| 3819 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3820 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3821 | return 0; |
| 3822 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3823 | /* Flush and acquire obj->pages so that we are coherent through |
| 3824 | * direct access in memory with previous cached writes through |
| 3825 | * shmemfs and that our cache domain tracking remains valid. |
| 3826 | * For example, if the obj->filp was moved to swap without us |
| 3827 | * being notified and releasing the pages, we would mistakenly |
| 3828 | * continue to assume that the obj remained out of the CPU cached |
| 3829 | * domain. |
| 3830 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3831 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3832 | if (ret) |
| 3833 | return ret; |
| 3834 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3835 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3836 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3837 | /* Serialise direct access to this object with the barriers for |
| 3838 | * coherent writes from the GPU, by effectively invalidating the |
| 3839 | * GTT domain upon first access. |
| 3840 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3841 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3842 | mb(); |
| 3843 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3844 | /* It should now be out of any other write domains, and we can update |
| 3845 | * the domain values for our changes. |
| 3846 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3847 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3848 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3849 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3850 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3851 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3852 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3853 | } |
| 3854 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3855 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3856 | return 0; |
| 3857 | } |
| 3858 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3859 | /** |
| 3860 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3861 | * @obj: object to act on |
| 3862 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3863 | * |
| 3864 | * After this function returns, the object will be in the new cache-level |
| 3865 | * across all GTT and the contents of the backing storage will be coherent, |
| 3866 | * with respect to the new cache-level. In order to keep the backing storage |
| 3867 | * coherent for all users, we only allow a single cache level to be set |
| 3868 | * globally on the object and prevent it from being changed whilst the |
| 3869 | * hardware is reading from the object. That is if the object is currently |
| 3870 | * on the scanout it will be set to uncached (or equivalent display |
| 3871 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3872 | * that all direct access to the scanout remains coherent. |
| 3873 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3874 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3875 | enum i915_cache_level cache_level) |
| 3876 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3877 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3878 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3879 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3880 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3881 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3882 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3883 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3884 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3885 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3886 | * be invalid given the new cache-level. This is principally to |
| 3887 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3888 | * reading an invalid PTE on older architectures. |
| 3889 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3890 | restart: |
| 3891 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3892 | if (!drm_mm_node_allocated(&vma->node)) |
| 3893 | continue; |
| 3894 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3895 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3896 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3897 | return -EBUSY; |
| 3898 | } |
| 3899 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3900 | if (!i915_vma_is_closed(vma) && |
| 3901 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3902 | continue; |
| 3903 | |
| 3904 | ret = i915_vma_unbind(vma); |
| 3905 | if (ret) |
| 3906 | return ret; |
| 3907 | |
| 3908 | /* As unbinding may affect other elements in the |
| 3909 | * obj->vma_list (due to side-effects from retiring |
| 3910 | * an active vma), play safe and restart the iterator. |
| 3911 | */ |
| 3912 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3913 | } |
| 3914 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3915 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3916 | * cache-level on the PTE. We could simply unbind them all and |
| 3917 | * rebind with the correct cache-level on next use. However since |
| 3918 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3919 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3920 | * state and so involves less work. |
| 3921 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3922 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3923 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3924 | * If we wait upon the object, we know that all the bound |
| 3925 | * VMA are no longer active. |
| 3926 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3927 | ret = i915_gem_object_wait(obj, |
| 3928 | I915_WAIT_INTERRUPTIBLE | |
| 3929 | I915_WAIT_LOCKED | |
| 3930 | I915_WAIT_ALL, |
| 3931 | MAX_SCHEDULE_TIMEOUT, |
| 3932 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3933 | if (ret) |
| 3934 | return ret; |
| 3935 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3936 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3937 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3938 | /* Access to snoopable pages through the GTT is |
| 3939 | * incoherent and on some machines causes a hard |
| 3940 | * lockup. Relinquish the CPU mmaping to force |
| 3941 | * userspace to refault in the pages and we can |
| 3942 | * then double check if the GTT mapping is still |
| 3943 | * valid for that pointer access. |
| 3944 | */ |
| 3945 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3946 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3947 | /* As we no longer need a fence for GTT access, |
| 3948 | * we can relinquish it now (and so prevent having |
| 3949 | * to steal a fence from someone else on the next |
| 3950 | * fence request). Note GPU activity would have |
| 3951 | * dropped the fence as all snoopable access is |
| 3952 | * supposed to be linear. |
| 3953 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3954 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3955 | ret = i915_vma_put_fence(vma); |
| 3956 | if (ret) |
| 3957 | return ret; |
| 3958 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3959 | } else { |
| 3960 | /* We either have incoherent backing store and |
| 3961 | * so no GTT access or the architecture is fully |
| 3962 | * coherent. In such cases, existing GTT mmaps |
| 3963 | * ignore the cache bit in the PTE and we can |
| 3964 | * rewrite it without confusing the GPU or having |
| 3965 | * to force userspace to fault back in its mmaps. |
| 3966 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3967 | } |
| 3968 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3969 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3970 | if (!drm_mm_node_allocated(&vma->node)) |
| 3971 | continue; |
| 3972 | |
| 3973 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3974 | if (ret) |
| 3975 | return ret; |
| 3976 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3977 | } |
| 3978 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3979 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3980 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3981 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3982 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3983 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3984 | return 0; |
| 3985 | } |
| 3986 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3987 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3988 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3989 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3990 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3991 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3992 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3993 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3994 | rcu_read_lock(); |
| 3995 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3996 | if (!obj) { |
| 3997 | err = -ENOENT; |
| 3998 | goto out; |
| 3999 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4000 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4001 | switch (obj->cache_level) { |
| 4002 | case I915_CACHE_LLC: |
| 4003 | case I915_CACHE_L3_LLC: |
| 4004 | args->caching = I915_CACHING_CACHED; |
| 4005 | break; |
| 4006 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4007 | case I915_CACHE_WT: |
| 4008 | args->caching = I915_CACHING_DISPLAY; |
| 4009 | break; |
| 4010 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4011 | default: |
| 4012 | args->caching = I915_CACHING_NONE; |
| 4013 | break; |
| 4014 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4015 | out: |
| 4016 | rcu_read_unlock(); |
| 4017 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4018 | } |
| 4019 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4020 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 4021 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4022 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4023 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4024 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4025 | struct drm_i915_gem_object *obj; |
| 4026 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4027 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4028 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4029 | switch (args->caching) { |
| 4030 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4031 | level = I915_CACHE_NONE; |
| 4032 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4033 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4034 | /* |
| 4035 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 4036 | * snooped mapping may leave stale data in a corresponding CPU |
| 4037 | * cacheline, whereas normally such cachelines would get |
| 4038 | * invalidated. |
| 4039 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4040 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4041 | return -ENODEV; |
| 4042 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4043 | level = I915_CACHE_LLC; |
| 4044 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4045 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4046 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4047 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4048 | default: |
| 4049 | return -EINVAL; |
| 4050 | } |
| 4051 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4052 | obj = i915_gem_object_lookup(file, args->handle); |
| 4053 | if (!obj) |
| 4054 | return -ENOENT; |
| 4055 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 4056 | /* |
| 4057 | * The caching mode of proxy object is handled by its generator, and |
| 4058 | * not allowed to be changed by userspace. |
| 4059 | */ |
| 4060 | if (i915_gem_object_is_proxy(obj)) { |
| 4061 | ret = -ENXIO; |
| 4062 | goto out; |
| 4063 | } |
| 4064 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4065 | if (obj->cache_level == level) |
| 4066 | goto out; |
| 4067 | |
| 4068 | ret = i915_gem_object_wait(obj, |
| 4069 | I915_WAIT_INTERRUPTIBLE, |
| 4070 | MAX_SCHEDULE_TIMEOUT, |
| 4071 | to_rps_client(file)); |
| 4072 | if (ret) |
| 4073 | goto out; |
| 4074 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 4075 | ret = i915_mutex_lock_interruptible(dev); |
| 4076 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4077 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4078 | |
| 4079 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4080 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4081 | |
| 4082 | out: |
| 4083 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4084 | return ret; |
| 4085 | } |
| 4086 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4087 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 4088 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 4089 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 4090 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 4091 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4092 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4093 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4094 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 4095 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4096 | const struct i915_ggtt_view *view, |
| 4097 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4098 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4099 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4100 | int ret; |
| 4101 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4102 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4103 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4104 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4105 | * display coherency whilst setting up the cache domains. |
| 4106 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4107 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4108 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4109 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 4110 | * a result, we make sure that the pinning that is about to occur is |
| 4111 | * done with uncached PTEs. This is lowest common denominator for all |
| 4112 | * chipsets. |
| 4113 | * |
| 4114 | * However for gen6+, we could do better by using the GFDT bit instead |
| 4115 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 4116 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 4117 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4118 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 4119 | HAS_WT(to_i915(obj->base.dev)) ? |
| 4120 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4121 | if (ret) { |
| 4122 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4123 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4124 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4125 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4126 | /* As the user may map the buffer once pinned in the display plane |
| 4127 | * (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] | 4128 | * always use map_and_fenceable for all scanout buffers. However, |
| 4129 | * it may simply be too big to fit into mappable, in which case |
| 4130 | * put it anyway and hope that userspace can cope (but always first |
| 4131 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4132 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4133 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4134 | if ((flags & PIN_MAPPABLE) == 0 && |
| 4135 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4136 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4137 | flags | |
| 4138 | PIN_MAPPABLE | |
| 4139 | PIN_NONBLOCK); |
| 4140 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 4141 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4142 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4143 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4144 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 4145 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 4146 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 4147 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 4148 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4149 | /* It should now be out of any other write domains, and we can update |
| 4150 | * the domain values for our changes. |
| 4151 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4152 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4153 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4154 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4155 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4156 | err_unpin_global: |
| 4157 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4158 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4162 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4163 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 4164 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4165 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4166 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 4167 | return; |
| 4168 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4169 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 4170 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 4171 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4172 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 4173 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4174 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4175 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4176 | } |
| 4177 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4178 | /** |
| 4179 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 4180 | * @obj: object to act on |
| 4181 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4182 | * |
| 4183 | * This function returns when the move is complete, including waiting on |
| 4184 | * flushes to occur. |
| 4185 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 4186 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 4187 | 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] | 4188 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4189 | int ret; |
| 4190 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4191 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4192 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4193 | ret = i915_gem_object_wait(obj, |
| 4194 | I915_WAIT_INTERRUPTIBLE | |
| 4195 | I915_WAIT_LOCKED | |
| 4196 | (write ? I915_WAIT_ALL : 0), |
| 4197 | MAX_SCHEDULE_TIMEOUT, |
| 4198 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 4199 | if (ret) |
| 4200 | return ret; |
| 4201 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 4202 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4203 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4204 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4205 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4206 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4207 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | /* It should now be out of any other write domains, and we can update |
| 4211 | * the domain values for our changes. |
| 4212 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4213 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4214 | |
| 4215 | /* If we're writing through the CPU, then the GPU read domains will |
| 4216 | * need to be invalidated at next use. |
| 4217 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4218 | if (write) |
| 4219 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 4220 | |
| 4221 | return 0; |
| 4222 | } |
| 4223 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4224 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 4225 | * emitted over 20 msec ago. |
| 4226 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4227 | * Note that if we were to use the current jiffies each time around the loop, |
| 4228 | * we wouldn't escape the function with any frames outstanding if the time to |
| 4229 | * render a frame was over 20ms. |
| 4230 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4231 | * This should get us reasonable parallelism between CPU and GPU but also |
| 4232 | * relatively low latency when blocking on a particular request to finish. |
| 4233 | */ |
| 4234 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4235 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4236 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4237 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4238 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 4239 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4240 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4241 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4242 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 4243 | /* ABI: return -EIO if already wedged */ |
| 4244 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 4245 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 4246 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4247 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4248 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4249 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 4250 | break; |
| 4251 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4252 | if (target) { |
| 4253 | list_del(&target->client_link); |
| 4254 | target->file_priv = NULL; |
| 4255 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 4256 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4257 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4258 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4259 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4260 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4261 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4262 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4263 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4264 | return 0; |
| 4265 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4266 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4267 | I915_WAIT_INTERRUPTIBLE, |
| 4268 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4269 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4270 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4271 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4274 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4275 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 4276 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 4277 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 4278 | u64 alignment, |
| 4279 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4280 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4281 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 4282 | struct i915_address_space *vm = &dev_priv->ggtt.base; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4283 | struct i915_vma *vma; |
| 4284 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 4285 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4286 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4287 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 4288 | if (flags & PIN_MAPPABLE && |
| 4289 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4290 | /* If the required space is larger than the available |
| 4291 | * aperture, we will not able to find a slot for the |
| 4292 | * object and unbinding the object now will be in |
| 4293 | * vain. Worse, doing so may cause us to ping-pong |
| 4294 | * the object in and out of the Global GTT and |
| 4295 | * waste a lot of cycles under the mutex. |
| 4296 | */ |
| 4297 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 4298 | return ERR_PTR(-E2BIG); |
| 4299 | |
| 4300 | /* If NONBLOCK is set the caller is optimistically |
| 4301 | * trying to cache the full object within the mappable |
| 4302 | * aperture, and *must* have a fallback in place for |
| 4303 | * situations where we cannot bind the object. We |
| 4304 | * can be a little more lax here and use the fallback |
| 4305 | * more often to avoid costly migrations of ourselves |
| 4306 | * and other objects within the aperture. |
| 4307 | * |
| 4308 | * Half-the-aperture is used as a simple heuristic. |
| 4309 | * More interesting would to do search for a free |
| 4310 | * block prior to making the commitment to unbind. |
| 4311 | * That caters for the self-harm case, and with a |
| 4312 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 4313 | * we could try to minimise harm to others. |
| 4314 | */ |
| 4315 | if (flags & PIN_NONBLOCK && |
| 4316 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 4317 | return ERR_PTR(-ENOSPC); |
| 4318 | } |
| 4319 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 4320 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 4321 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4322 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4323 | |
| 4324 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4325 | if (flags & PIN_NONBLOCK) { |
| 4326 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 4327 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4328 | |
Chris Wilson | 43ae70d | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4329 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 4330 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4331 | return ERR_PTR(-ENOSPC); |
| 4332 | } |
| 4333 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4334 | WARN(i915_vma_is_pinned(vma), |
| 4335 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4336 | " offset=%08x, req.alignment=%llx," |
| 4337 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 4338 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4339 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4340 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4341 | ret = i915_vma_unbind(vma); |
| 4342 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4343 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4344 | } |
| 4345 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4346 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 4347 | if (ret) |
| 4348 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4349 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4350 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4351 | } |
| 4352 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4353 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4354 | { |
| 4355 | /* Note that we could alias engines in the execbuf API, but |
| 4356 | * that would be very unwise as it prevents userspace from |
| 4357 | * fine control over engine selection. Ahem. |
| 4358 | * |
| 4359 | * This should be something like EXEC_MAX_ENGINE instead of |
| 4360 | * I915_NUM_ENGINES. |
| 4361 | */ |
| 4362 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 4363 | return 0x10000 << id; |
| 4364 | } |
| 4365 | |
| 4366 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 4367 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 4368 | /* The uABI guarantees an active writer is also amongst the read |
| 4369 | * engines. This would be true if we accessed the activity tracking |
| 4370 | * under the lock, but as we perform the lookup of the object and |
| 4371 | * its activity locklessly we can not guarantee that the last_write |
| 4372 | * being active implies that we have set the same engine flag from |
| 4373 | * last_read - hence we always set both read and write busy for |
| 4374 | * last_write. |
| 4375 | */ |
| 4376 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4379 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4380 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4381 | unsigned int (*flag)(unsigned int id)) |
| 4382 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4383 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4384 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4385 | /* We have to check the current hw status of the fence as the uABI |
| 4386 | * guarantees forward progress. We could rely on the idle worker |
| 4387 | * to eventually flush us, but to minimise latency just ask the |
| 4388 | * hardware. |
| 4389 | * |
| 4390 | * Note we only report on the status of native fences. |
| 4391 | */ |
| 4392 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4393 | return 0; |
| 4394 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4395 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4396 | rq = container_of(fence, struct i915_request, fence); |
| 4397 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4398 | return 0; |
| 4399 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4400 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4401 | } |
| 4402 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4403 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4404 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4405 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4406 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4407 | } |
| 4408 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4409 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4410 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4411 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4412 | if (!fence) |
| 4413 | return 0; |
| 4414 | |
| 4415 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4418 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4419 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4420 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4421 | { |
| 4422 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4423 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4424 | struct reservation_object_list *list; |
| 4425 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4426 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4427 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4428 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4429 | rcu_read_lock(); |
| 4430 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4431 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4432 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4433 | |
| 4434 | /* A discrepancy here is that we do not report the status of |
| 4435 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4436 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4437 | * This also means that wait-ioctl may report an object as busy, |
| 4438 | * where busy-ioctl considers it idle. |
| 4439 | * |
| 4440 | * We trade the ability to warn of foreign fences to report on which |
| 4441 | * i915 engines are active for the object. |
| 4442 | * |
| 4443 | * Alternatively, we can trade that extra information on read/write |
| 4444 | * activity with |
| 4445 | * args->busy = |
| 4446 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4447 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4448 | * |
| 4449 | */ |
| 4450 | retry: |
| 4451 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4452 | |
| 4453 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4454 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4455 | |
| 4456 | /* Translate shared fences to READ set of engines */ |
| 4457 | list = rcu_dereference(obj->resv->fence); |
| 4458 | if (list) { |
| 4459 | unsigned int shared_count = list->shared_count, i; |
| 4460 | |
| 4461 | for (i = 0; i < shared_count; ++i) { |
| 4462 | struct dma_fence *fence = |
| 4463 | rcu_dereference(list->shared[i]); |
| 4464 | |
| 4465 | args->busy |= busy_check_reader(fence); |
| 4466 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4467 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4468 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4469 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4470 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4471 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4472 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4473 | out: |
| 4474 | rcu_read_unlock(); |
| 4475 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | int |
| 4479 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4480 | struct drm_file *file_priv) |
| 4481 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4482 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4483 | } |
| 4484 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4485 | int |
| 4486 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4487 | struct drm_file *file_priv) |
| 4488 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4489 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4490 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4491 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4492 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4493 | |
| 4494 | switch (args->madv) { |
| 4495 | case I915_MADV_DONTNEED: |
| 4496 | case I915_MADV_WILLNEED: |
| 4497 | break; |
| 4498 | default: |
| 4499 | return -EINVAL; |
| 4500 | } |
| 4501 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4502 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4503 | if (!obj) |
| 4504 | return -ENOENT; |
| 4505 | |
| 4506 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4507 | if (err) |
| 4508 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4509 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4510 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4511 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4512 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4513 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4514 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4515 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4516 | obj->mm.quirked = false; |
| 4517 | } |
| 4518 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4519 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4520 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4521 | obj->mm.quirked = true; |
| 4522 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4523 | } |
| 4524 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4525 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4526 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4527 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4528 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4529 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4530 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4531 | i915_gem_object_truncate(obj); |
| 4532 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4533 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4534 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4535 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4536 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4537 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4538 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4539 | } |
| 4540 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4541 | static void |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4542 | frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4543 | { |
| 4544 | struct drm_i915_gem_object *obj = |
| 4545 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4546 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4547 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4550 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4551 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4552 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4553 | mutex_init(&obj->mm.lock); |
| 4554 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4555 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4556 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4557 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4558 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4559 | obj->ops = ops; |
| 4560 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4561 | reservation_object_init(&obj->__builtin_resv); |
| 4562 | obj->resv = &obj->__builtin_resv; |
| 4563 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4564 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4565 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4566 | |
| 4567 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4568 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4569 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4570 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4571 | 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] | 4572 | } |
| 4573 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4574 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4575 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4576 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4577 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4578 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4579 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4580 | |
| 4581 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4582 | }; |
| 4583 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4584 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4585 | struct drm_gem_object *obj, |
| 4586 | size_t size) |
| 4587 | { |
| 4588 | struct drm_i915_private *i915 = to_i915(dev); |
| 4589 | unsigned long flags = VM_NORESERVE; |
| 4590 | struct file *filp; |
| 4591 | |
| 4592 | drm_gem_private_object_init(dev, obj, size); |
| 4593 | |
| 4594 | if (i915->mm.gemfs) |
| 4595 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4596 | flags); |
| 4597 | else |
| 4598 | filp = shmem_file_setup("i915", size, flags); |
| 4599 | |
| 4600 | if (IS_ERR(filp)) |
| 4601 | return PTR_ERR(filp); |
| 4602 | |
| 4603 | obj->filp = filp; |
| 4604 | |
| 4605 | return 0; |
| 4606 | } |
| 4607 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4608 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4609 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4610 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4611 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4612 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4613 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4614 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4615 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4616 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4617 | /* There is a prevalence of the assumption that we fit the object's |
| 4618 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4619 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4620 | * such a local variable, please consider fixing! |
| 4621 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4622 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4623 | return ERR_PTR(-E2BIG); |
| 4624 | |
| 4625 | if (overflows_type(size, obj->base.size)) |
| 4626 | return ERR_PTR(-E2BIG); |
| 4627 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4628 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4629 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4630 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4631 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4632 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4633 | if (ret) |
| 4634 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4635 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4636 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4637 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4638 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4639 | mask &= ~__GFP_HIGHMEM; |
| 4640 | mask |= __GFP_DMA32; |
| 4641 | } |
| 4642 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4643 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4644 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4645 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4646 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4647 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4648 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4649 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4650 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4651 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4652 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4653 | /* 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] | 4654 | * cache) for about a 10% performance improvement |
| 4655 | * compared to uncached. Graphics requests other than |
| 4656 | * display scanout are coherent with the CPU in |
| 4657 | * accessing this cache. This means in this mode we |
| 4658 | * don't need to clflush on the CPU side, and on the |
| 4659 | * GPU side we only need to flush internal caches to |
| 4660 | * get data visible to the CPU. |
| 4661 | * |
| 4662 | * However, we maintain the display planes as UC, and so |
| 4663 | * need to rebind when first used as such. |
| 4664 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4665 | cache_level = I915_CACHE_LLC; |
| 4666 | else |
| 4667 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4668 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4669 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4670 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4671 | trace_i915_gem_object_create(obj); |
| 4672 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4673 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4674 | |
| 4675 | fail: |
| 4676 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4677 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4680 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4681 | { |
| 4682 | /* If we are the last user of the backing storage (be it shmemfs |
| 4683 | * pages or stolen etc), we know that the pages are going to be |
| 4684 | * immediately released. In this case, we can then skip copying |
| 4685 | * back the contents from the GPU. |
| 4686 | */ |
| 4687 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4688 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4689 | return false; |
| 4690 | |
| 4691 | if (obj->base.filp == NULL) |
| 4692 | return true; |
| 4693 | |
| 4694 | /* At first glance, this looks racy, but then again so would be |
| 4695 | * userspace racing mmap against close. However, the first external |
| 4696 | * reference to the filp can only be obtained through the |
| 4697 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4698 | * acquiring such a reference whilst we are in the middle of |
| 4699 | * freeing the object. |
| 4700 | */ |
| 4701 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4702 | } |
| 4703 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4704 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4705 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4706 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4707 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4708 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4709 | intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4710 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4711 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4712 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4713 | trace_i915_gem_object_destroy(obj); |
| 4714 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4715 | mutex_lock(&i915->drm.struct_mutex); |
| 4716 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4717 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
| 4718 | list_for_each_entry_safe(vma, vn, |
| 4719 | &obj->vma_list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4720 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4721 | vma->flags &= ~I915_VMA_PIN_MASK; |
| 4722 | i915_vma_close(vma); |
| 4723 | } |
Chris Wilson | db6c2b4 | 2016-11-01 11:54:00 +0000 | [diff] [blame] | 4724 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
| 4725 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4726 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4727 | /* This serializes freeing with the shrinker. Since the free |
| 4728 | * is delayed, first by RCU then by the workqueue, we want the |
| 4729 | * shrinker to be able to free pages of unreferenced objects, |
| 4730 | * or else we may oom whilst there are plenty of deferred |
| 4731 | * freed objects. |
| 4732 | */ |
| 4733 | if (i915_gem_object_has_pages(obj)) { |
| 4734 | spin_lock(&i915->mm.obj_lock); |
| 4735 | list_del_init(&obj->mm.link); |
| 4736 | spin_unlock(&i915->mm.obj_lock); |
| 4737 | } |
| 4738 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4739 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4740 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4741 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4742 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4743 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4744 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4745 | |
| 4746 | if (obj->ops->release) |
| 4747 | obj->ops->release(obj); |
| 4748 | |
| 4749 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4750 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4751 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4752 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4753 | |
| 4754 | if (obj->base.import_attach) |
| 4755 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4756 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4757 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4758 | drm_gem_object_release(&obj->base); |
| 4759 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4760 | |
| 4761 | kfree(obj->bit_17); |
| 4762 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4763 | |
Chris Wilson | c9c7047 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4764 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4765 | atomic_dec(&i915->mm.free_count); |
| 4766 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4767 | if (on) |
| 4768 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4769 | } |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4770 | intel_runtime_pm_put(i915); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4774 | { |
| 4775 | struct llist_node *freed; |
| 4776 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4777 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4778 | freed = NULL; |
| 4779 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4780 | /* Only one consumer of llist_del_first() allowed */ |
| 4781 | spin_lock(&i915->mm.free_lock); |
| 4782 | freed = llist_del_first(&i915->mm.free_list); |
| 4783 | spin_unlock(&i915->mm.free_lock); |
| 4784 | } |
| 4785 | if (unlikely(freed)) { |
| 4786 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4787 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4788 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4789 | } |
| 4790 | |
| 4791 | static void __i915_gem_free_work(struct work_struct *work) |
| 4792 | { |
| 4793 | struct drm_i915_private *i915 = |
| 4794 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4795 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4796 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4797 | /* |
| 4798 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4799 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4800 | * However, the object may also be bound into the global GTT (e.g. |
| 4801 | * older GPUs without per-process support, or for direct access through |
| 4802 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4803 | * unbound now. |
| 4804 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4805 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4806 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4807 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4808 | spin_unlock(&i915->mm.free_lock); |
| 4809 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4810 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4811 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4812 | return; |
| 4813 | |
| 4814 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4815 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4816 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4820 | { |
| 4821 | struct drm_i915_gem_object *obj = |
| 4822 | container_of(head, typeof(*obj), rcu); |
| 4823 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4824 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4825 | /* |
| 4826 | * Since we require blocking on struct_mutex to unbind the freed |
| 4827 | * object from the GPU before releasing resources back to the |
| 4828 | * system, we can not do that directly from the RCU callback (which may |
| 4829 | * be a softirq context), but must instead then defer that work onto a |
| 4830 | * kthread. We use the RCU callback rather than move the freed object |
| 4831 | * directly onto the work queue so that we can mix between using the |
| 4832 | * worker and performing frees directly from subsequent allocations for |
| 4833 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4834 | */ |
| 4835 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4836 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4837 | } |
| 4838 | |
| 4839 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4840 | { |
| 4841 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4842 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4843 | if (obj->mm.quirked) |
| 4844 | __i915_gem_object_unpin_pages(obj); |
| 4845 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4846 | if (discard_backing_storage(obj)) |
| 4847 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4848 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4849 | /* |
| 4850 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4851 | * read-side critical sections are complete, e.g. |
| 4852 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4853 | * lookup see i915_gem_object_lookup_rcu(). |
| 4854 | */ |
Chris Wilson | c9c7047 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4855 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4856 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4857 | } |
| 4858 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4859 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4860 | { |
| 4861 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4862 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4863 | if (!i915_gem_object_has_active_reference(obj) && |
| 4864 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4865 | i915_gem_object_set_active_reference(obj); |
| 4866 | else |
| 4867 | i915_gem_object_put(obj); |
| 4868 | } |
| 4869 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4870 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4871 | { |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4872 | struct i915_gem_context *kernel_context = i915->kernel_context; |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4873 | struct intel_engine_cs *engine; |
| 4874 | enum intel_engine_id id; |
| 4875 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4876 | for_each_engine(engine, i915, id) { |
| 4877 | GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request)); |
| 4878 | GEM_BUG_ON(engine->last_retired_context != kernel_context); |
| 4879 | } |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4880 | } |
| 4881 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4882 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4883 | { |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4884 | if (i915_terminally_wedged(&i915->gpu_error)) { |
| 4885 | mutex_lock(&i915->drm.struct_mutex); |
| 4886 | i915_gem_unset_wedged(i915); |
| 4887 | mutex_unlock(&i915->drm.struct_mutex); |
| 4888 | } |
| 4889 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4890 | /* |
| 4891 | * If we inherit context state from the BIOS or earlier occupants |
| 4892 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4893 | * try to take over. The only way to remove the earlier state |
| 4894 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4895 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4896 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4897 | */ |
Daniele Ceraolo Spurio | ce1599a | 2018-02-07 13:24:40 -0800 | [diff] [blame] | 4898 | if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915)) |
| 4899 | WARN_ON(intel_gpu_reset(i915, ALL_ENGINES)); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4902 | int i915_gem_suspend(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4903 | { |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4904 | struct drm_device *dev = &dev_priv->drm; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4905 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4906 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4907 | intel_runtime_pm_get(dev_priv); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4908 | intel_suspend_gt_powersave(dev_priv); |
| 4909 | |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4910 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4911 | |
| 4912 | /* We have to flush all the executing contexts to main memory so |
| 4913 | * that they can saved in the hibernation image. To ensure the last |
| 4914 | * context image is coherent, we have to switch away from it. That |
| 4915 | * leaves the dev_priv->kernel_context still active when |
| 4916 | * we actually suspend, and its image in memory may not match the GPU |
| 4917 | * state. Fortunately, the kernel_context is disposable and we do |
| 4918 | * not rely on its state. |
| 4919 | */ |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4920 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 4921 | ret = i915_gem_switch_to_kernel_context(dev_priv); |
| 4922 | if (ret) |
| 4923 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4924 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4925 | ret = i915_gem_wait_for_idle(dev_priv, |
| 4926 | I915_WAIT_INTERRUPTIBLE | |
| 4927 | I915_WAIT_LOCKED); |
| 4928 | if (ret && ret != -EIO) |
| 4929 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4930 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4931 | assert_kernel_context_is_current(dev_priv); |
| 4932 | } |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4933 | i915_gem_contexts_lost(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4934 | mutex_unlock(&dev->struct_mutex); |
| 4935 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 4936 | intel_uc_suspend(dev_priv); |
Sagar Arun Kamble | 63987bf | 2017-04-05 15:51:50 +0530 | [diff] [blame] | 4937 | |
Chris Wilson | 737b150 | 2015-01-26 18:03:03 +0200 | [diff] [blame] | 4938 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 4939 | cancel_delayed_work_sync(&dev_priv->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4940 | |
| 4941 | /* As the idle_work is rearming if it detects a race, play safe and |
| 4942 | * repeat the flush until it is definitely idle. |
| 4943 | */ |
Chris Wilson | 7c26240 | 2017-10-06 11:40:38 +0100 | [diff] [blame] | 4944 | drain_delayed_work(&dev_priv->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4945 | |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4946 | /* Assert that we sucessfully flushed all the work and |
| 4947 | * reset the GPU back to its idle, low power state. |
| 4948 | */ |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 4949 | WARN_ON(dev_priv->gt.awake); |
Chris Wilson | fc692bd | 2017-08-26 12:09:35 +0100 | [diff] [blame] | 4950 | if (WARN_ON(!intel_engines_are_idle(dev_priv))) |
| 4951 | i915_gem_set_wedged(dev_priv); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4952 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4953 | /* |
| 4954 | * Neither the BIOS, ourselves or any other kernel |
| 4955 | * expects the system to be in execlists mode on startup, |
| 4956 | * so we need to reset the GPU back to legacy mode. And the only |
| 4957 | * known way to disable logical contexts is through a GPU reset. |
| 4958 | * |
| 4959 | * So in order to leave the system in a known default configuration, |
| 4960 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4961 | * clean up the GEM state tracking, flushing off the requests and |
| 4962 | * leaving the system in a known idle state. |
| 4963 | * |
| 4964 | * Note that is of the upmost importance that the GPU is idle and |
| 4965 | * all stray writes are flushed *before* we dismantle the backing |
| 4966 | * storage for the pinned objects. |
| 4967 | * |
| 4968 | * However, since we are uncertain that resetting the GPU on older |
| 4969 | * machines is a good idea, we don't - just in case it leaves the |
| 4970 | * machine in an unusable condition. |
| 4971 | */ |
Michal Wajdeczko | c37d572 | 2018-03-12 13:03:07 +0000 | [diff] [blame] | 4972 | intel_uc_sanitize(dev_priv); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4973 | i915_gem_sanitize(dev_priv); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4974 | |
| 4975 | intel_runtime_pm_put(dev_priv); |
| 4976 | return 0; |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4977 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4978 | err_unlock: |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4979 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4980 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4981 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4982 | } |
| 4983 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4984 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4985 | { |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4986 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4987 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4988 | mutex_lock(&i915->drm.struct_mutex); |
| 4989 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4990 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4991 | i915_gem_restore_gtt_mappings(i915); |
| 4992 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4993 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4994 | /* |
| 4995 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4996 | * guarantee that the context image is complete. So let's just reset |
| 4997 | * it and start again. |
| 4998 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4999 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5000 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5001 | if (i915_gem_init_hw(i915)) |
| 5002 | goto err_wedged; |
| 5003 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 5004 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 5005 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5006 | /* Always reload a context for powersaving. */ |
| 5007 | if (i915_gem_switch_to_kernel_context(i915)) |
| 5008 | goto err_wedged; |
| 5009 | |
| 5010 | out_unlock: |
| 5011 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 5012 | mutex_unlock(&i915->drm.struct_mutex); |
| 5013 | return; |
| 5014 | |
| 5015 | err_wedged: |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5016 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 5017 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 5018 | i915_gem_set_wedged(i915); |
| 5019 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5020 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5021 | } |
| 5022 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5023 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5024 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5025 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5026 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 5027 | return; |
| 5028 | |
| 5029 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 5030 | DISP_TILE_SURFACE_SWIZZLING); |
| 5031 | |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5032 | if (IS_GEN5(dev_priv)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 5033 | return; |
| 5034 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5035 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5036 | if (IS_GEN6(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5037 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5038 | else if (IS_GEN7(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5039 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5040 | else if (IS_GEN8(dev_priv)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 5041 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 5042 | else |
| 5043 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5044 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 5045 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5046 | 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] | 5047 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5048 | I915_WRITE(RING_CTL(base), 0); |
| 5049 | I915_WRITE(RING_HEAD(base), 0); |
| 5050 | I915_WRITE(RING_TAIL(base), 0); |
| 5051 | I915_WRITE(RING_START(base), 0); |
| 5052 | } |
| 5053 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5054 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5055 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5056 | if (IS_I830(dev_priv)) { |
| 5057 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5058 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5059 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5060 | init_unused_ring(dev_priv, SRB2_BASE); |
| 5061 | init_unused_ring(dev_priv, SRB3_BASE); |
| 5062 | } else if (IS_GEN2(dev_priv)) { |
| 5063 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5064 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5065 | } else if (IS_GEN3(dev_priv)) { |
| 5066 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5067 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5068 | } |
| 5069 | } |
| 5070 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5071 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5072 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5073 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5074 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5075 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5076 | int err; |
| 5077 | |
| 5078 | for_each_engine(engine, i915, id) { |
| 5079 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5080 | if (err) { |
| 5081 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 5082 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5083 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5084 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | return 0; |
| 5088 | } |
| 5089 | |
| 5090 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 5091 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 5092 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5093 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 5094 | dev_priv->gt.last_init_time = ktime_get(); |
| 5095 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5096 | /* Double layer security blanket, see i915_gem_init() */ |
| 5097 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5098 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 5099 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 5100 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5101 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 5102 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5103 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 5104 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 5105 | |
Tvrtko Ursulin | 6e26695 | 2016-10-13 11:02:53 +0100 | [diff] [blame] | 5106 | if (HAS_PCH_NOP(dev_priv)) { |
Tvrtko Ursulin | fd6b8f4 | 2016-10-14 10:13:06 +0100 | [diff] [blame] | 5107 | if (IS_IVYBRIDGE(dev_priv)) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 5108 | u32 temp = I915_READ(GEN7_MSG_CTL); |
| 5109 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); |
| 5110 | I915_WRITE(GEN7_MSG_CTL, temp); |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5111 | } else if (INTEL_GEN(dev_priv) >= 7) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 5112 | u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); |
| 5113 | temp &= ~RESET_PCH_HANDSHAKE_ENABLE; |
| 5114 | I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); |
| 5115 | } |
Ben Widawsky | 88a2b2a | 2013-04-05 13:12:43 -0700 | [diff] [blame] | 5116 | } |
| 5117 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5118 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5119 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5120 | /* |
| 5121 | * At least 830 can leave some of the unused rings |
| 5122 | * "active" (ie. head != tail) after resume which |
| 5123 | * will prevent c3 entry. Makes sure all unused rings |
| 5124 | * are totally idle. |
| 5125 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5126 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5127 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 5128 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5129 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5130 | ret = -EIO; |
| 5131 | goto out; |
| 5132 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 5133 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5134 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5135 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5136 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5137 | goto out; |
| 5138 | } |
| 5139 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5140 | /* We can't enable contexts until all firmware is loaded */ |
| 5141 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5142 | if (ret) { |
| 5143 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5144 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5145 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5146 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5147 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 5148 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 5149 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 5150 | ret = __i915_gem_restart_engines(dev_priv); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5151 | out: |
| 5152 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5153 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5154 | } |
| 5155 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5156 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 5157 | { |
| 5158 | struct i915_gem_context *ctx; |
| 5159 | struct intel_engine_cs *engine; |
| 5160 | enum intel_engine_id id; |
| 5161 | int err; |
| 5162 | |
| 5163 | /* |
| 5164 | * As we reset the gpu during very early sanitisation, the current |
| 5165 | * register state on the GPU should reflect its defaults values. |
| 5166 | * We load a context onto the hw (with restore-inhibit), then switch |
| 5167 | * over to a second context to save that default register state. We |
| 5168 | * can then prime every new context with that state so they all start |
| 5169 | * from the same default HW values. |
| 5170 | */ |
| 5171 | |
| 5172 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 5173 | if (IS_ERR(ctx)) |
| 5174 | return PTR_ERR(ctx); |
| 5175 | |
| 5176 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5177 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5178 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5179 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5180 | if (IS_ERR(rq)) { |
| 5181 | err = PTR_ERR(rq); |
| 5182 | goto out_ctx; |
| 5183 | } |
| 5184 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 5185 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5186 | if (engine->init_context) |
| 5187 | err = engine->init_context(rq); |
| 5188 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5189 | __i915_request_add(rq, true); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5190 | if (err) |
| 5191 | goto err_active; |
| 5192 | } |
| 5193 | |
| 5194 | err = i915_gem_switch_to_kernel_context(i915); |
| 5195 | if (err) |
| 5196 | goto err_active; |
| 5197 | |
| 5198 | err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED); |
| 5199 | if (err) |
| 5200 | goto err_active; |
| 5201 | |
| 5202 | assert_kernel_context_is_current(i915); |
| 5203 | |
| 5204 | for_each_engine(engine, i915, id) { |
| 5205 | struct i915_vma *state; |
| 5206 | |
| 5207 | state = ctx->engine[id].state; |
| 5208 | if (!state) |
| 5209 | continue; |
| 5210 | |
| 5211 | /* |
| 5212 | * As we will hold a reference to the logical state, it will |
| 5213 | * not be torn down with the context, and importantly the |
| 5214 | * object will hold onto its vma (making it possible for a |
| 5215 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 5216 | * from the GTT to prevent such accidents and reclaim the |
| 5217 | * space. |
| 5218 | */ |
| 5219 | err = i915_vma_unbind(state); |
| 5220 | if (err) |
| 5221 | goto err_active; |
| 5222 | |
| 5223 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 5224 | if (err) |
| 5225 | goto err_active; |
| 5226 | |
| 5227 | engine->default_state = i915_gem_object_get(state->obj); |
| 5228 | } |
| 5229 | |
| 5230 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 5231 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 5232 | |
| 5233 | /* |
| 5234 | * Make sure that classes with multiple engine instances all |
| 5235 | * share the same basic configuration. |
| 5236 | */ |
| 5237 | for_each_engine(engine, i915, id) { |
| 5238 | unsigned int bit = BIT(engine->uabi_class); |
| 5239 | unsigned int expected = engine->default_state ? bit : 0; |
| 5240 | |
| 5241 | if ((found & bit) != expected) { |
| 5242 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 5243 | engine->uabi_class, engine->name); |
| 5244 | } |
| 5245 | } |
| 5246 | } |
| 5247 | |
| 5248 | out_ctx: |
| 5249 | i915_gem_context_set_closed(ctx); |
| 5250 | i915_gem_context_put(ctx); |
| 5251 | return err; |
| 5252 | |
| 5253 | err_active: |
| 5254 | /* |
| 5255 | * If we have to abandon now, we expect the engines to be idle |
| 5256 | * and ready to be torn-down. First try to flush any remaining |
| 5257 | * request, ensure we are pointing at the kernel context and |
| 5258 | * then remove it. |
| 5259 | */ |
| 5260 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 5261 | goto out_ctx; |
| 5262 | |
| 5263 | if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED))) |
| 5264 | goto out_ctx; |
| 5265 | |
| 5266 | i915_gem_contexts_lost(i915); |
| 5267 | goto out_ctx; |
| 5268 | } |
| 5269 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5270 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5271 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5272 | int ret; |
| 5273 | |
Matthew Auld | da9fe3f3 | 2017-10-06 23:18:31 +0100 | [diff] [blame] | 5274 | /* |
| 5275 | * We need to fallback to 4K pages since gvt gtt handling doesn't |
| 5276 | * support huge page entries - we will need to check either hypervisor |
| 5277 | * mm can support huge guest page or just do emulation in gvt. |
| 5278 | */ |
| 5279 | if (intel_vgpu_active(dev_priv)) |
| 5280 | mkwrite_device_info(dev_priv)->page_sizes = |
| 5281 | I915_GTT_PAGE_SIZE_4K; |
| 5282 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 5283 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 5284 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5285 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 5286 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5287 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5288 | } else { |
| 5289 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5290 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5291 | } |
| 5292 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5293 | ret = i915_gem_init_userptr(dev_priv); |
| 5294 | if (ret) |
| 5295 | return ret; |
| 5296 | |
Jackie Li | 6b0478f | 2018-03-13 17:32:50 -0700 | [diff] [blame] | 5297 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 5298 | if (ret) |
| 5299 | return ret; |
| 5300 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5301 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 5302 | if (ret) |
| 5303 | return ret; |
| 5304 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5305 | /* This is just a security blanket to placate dragons. |
| 5306 | * On some systems, we very sporadically observe that the first TLBs |
| 5307 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5308 | * we hold the forcewake during initialisation these problems |
| 5309 | * just magically go away. |
| 5310 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5311 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5312 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5313 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5314 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5315 | if (ret) { |
| 5316 | GEM_BUG_ON(ret == -EIO); |
| 5317 | goto err_unlock; |
| 5318 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5319 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5320 | ret = i915_gem_contexts_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5321 | if (ret) { |
| 5322 | GEM_BUG_ON(ret == -EIO); |
| 5323 | goto err_ggtt; |
| 5324 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5325 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5326 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5327 | if (ret) { |
| 5328 | GEM_BUG_ON(ret == -EIO); |
| 5329 | goto err_context; |
| 5330 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5331 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5332 | intel_init_gt_powersave(dev_priv); |
| 5333 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5334 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5335 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5336 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5337 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5338 | ret = i915_gem_init_hw(dev_priv); |
| 5339 | if (ret) |
| 5340 | goto err_uc_init; |
| 5341 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5342 | /* |
| 5343 | * Despite its name intel_init_clock_gating applies both display |
| 5344 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5345 | * GT power context workaround. Worse, sometimes it includes a context |
| 5346 | * register workaround which we need to apply before we record the |
| 5347 | * default HW state for all contexts. |
| 5348 | * |
| 5349 | * FIXME: break up the workarounds and apply them at the right time! |
| 5350 | */ |
| 5351 | intel_init_clock_gating(dev_priv); |
| 5352 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5353 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5354 | if (ret) |
| 5355 | goto err_init_hw; |
| 5356 | |
| 5357 | if (i915_inject_load_failure()) { |
| 5358 | ret = -ENODEV; |
| 5359 | goto err_init_hw; |
| 5360 | } |
| 5361 | |
| 5362 | if (i915_inject_load_failure()) { |
| 5363 | ret = -EIO; |
| 5364 | goto err_init_hw; |
| 5365 | } |
| 5366 | |
| 5367 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5368 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5369 | |
| 5370 | return 0; |
| 5371 | |
| 5372 | /* |
| 5373 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5374 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5375 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5376 | * driver doesn't explode during runtime. |
| 5377 | */ |
| 5378 | err_init_hw: |
| 5379 | i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED); |
| 5380 | i915_gem_contexts_lost(dev_priv); |
| 5381 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5382 | err_uc_init: |
| 5383 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5384 | err_pm: |
| 5385 | if (ret != -EIO) { |
| 5386 | intel_cleanup_gt_powersave(dev_priv); |
| 5387 | i915_gem_cleanup_engines(dev_priv); |
| 5388 | } |
| 5389 | err_context: |
| 5390 | if (ret != -EIO) |
| 5391 | i915_gem_contexts_fini(dev_priv); |
| 5392 | err_ggtt: |
| 5393 | err_unlock: |
| 5394 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5395 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5396 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5397 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5398 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5399 | if (ret != -EIO) |
| 5400 | i915_gem_cleanup_userptr(dev_priv); |
| 5401 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5402 | if (ret == -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5403 | /* |
| 5404 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5405 | * wedged. But we only want to do this where the GPU is angry, |
| 5406 | * for all other failure, such as an allocation failure, bail. |
| 5407 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5408 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5409 | DRM_ERROR("Failed to initialize GPU, declaring it wedged\n"); |
| 5410 | i915_gem_set_wedged(dev_priv); |
| 5411 | } |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5412 | ret = 0; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5413 | } |
| 5414 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5415 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5416 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5417 | } |
| 5418 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5419 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5420 | { |
| 5421 | i915_gem_sanitize(i915); |
| 5422 | } |
| 5423 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5424 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5425 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5426 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5427 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5428 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5429 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5430 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5431 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5432 | } |
| 5433 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5434 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5435 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5436 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5437 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5438 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5439 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5440 | !IS_CHERRYVIEW(dev_priv)) |
| 5441 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5442 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5443 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5444 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5445 | dev_priv->num_fence_regs = 16; |
| 5446 | else |
| 5447 | dev_priv->num_fence_regs = 8; |
| 5448 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5449 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5450 | dev_priv->num_fence_regs = |
| 5451 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5452 | |
| 5453 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5454 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5455 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5456 | |
| 5457 | fence->i915 = dev_priv; |
| 5458 | fence->id = i; |
| 5459 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5460 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5461 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5462 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5463 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5464 | } |
| 5465 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5466 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5467 | { |
| 5468 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5469 | spin_lock_init(&i915->mm.obj_lock); |
| 5470 | spin_lock_init(&i915->mm.free_lock); |
| 5471 | |
| 5472 | init_llist_head(&i915->mm.free_list); |
| 5473 | |
| 5474 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5475 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5476 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5477 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5478 | |
| 5479 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5480 | } |
| 5481 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5482 | int |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5483 | i915_gem_load_init(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5484 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5485 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5486 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5487 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5488 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5489 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5490 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5491 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5492 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5493 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5494 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5495 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5496 | if (!dev_priv->luts) |
| 5497 | goto err_vmas; |
| 5498 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5499 | dev_priv->requests = KMEM_CACHE(i915_request, |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5500 | SLAB_HWCACHE_ALIGN | |
| 5501 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5502 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5503 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5504 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5505 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5506 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5507 | SLAB_HWCACHE_ALIGN | |
| 5508 | SLAB_RECLAIM_ACCOUNT); |
| 5509 | if (!dev_priv->dependencies) |
| 5510 | goto err_requests; |
| 5511 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5512 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5513 | if (!dev_priv->priorities) |
| 5514 | goto err_dependencies; |
| 5515 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5516 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5517 | INIT_LIST_HEAD(&dev_priv->gt.timelines); |
Chris Wilson | bb89485 | 2016-11-14 20:40:57 +0000 | [diff] [blame] | 5518 | err = i915_gem_timeline_init__global(dev_priv); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5519 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5520 | if (err) |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5521 | goto err_priorities; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5522 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5523 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5524 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5525 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5526 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5527 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5528 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5529 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5530 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5531 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5532 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5533 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5534 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5535 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5536 | err = i915_gemfs_init(dev_priv); |
| 5537 | if (err) |
| 5538 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5539 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5540 | return 0; |
| 5541 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5542 | err_priorities: |
| 5543 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5544 | err_dependencies: |
| 5545 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5546 | err_requests: |
| 5547 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5548 | err_luts: |
| 5549 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5550 | err_vmas: |
| 5551 | kmem_cache_destroy(dev_priv->vmas); |
| 5552 | err_objects: |
| 5553 | kmem_cache_destroy(dev_priv->objects); |
| 5554 | err_out: |
| 5555 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5556 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5557 | |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5558 | void i915_gem_load_cleanup(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5559 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5560 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c7047 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5561 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5562 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5563 | WARN_ON(dev_priv->mm.object_count); |
Chris Wilson | 7d5d59e | 2016-11-01 08:48:41 +0000 | [diff] [blame] | 5564 | |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5565 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5566 | i915_gem_timeline_fini(&dev_priv->gt.global_timeline); |
| 5567 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); |
| 5568 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5569 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5570 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5571 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5572 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5573 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5574 | kmem_cache_destroy(dev_priv->vmas); |
| 5575 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5576 | |
| 5577 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5578 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5579 | |
| 5580 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5581 | } |
| 5582 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5583 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5584 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5585 | /* Discard all purgeable objects, let userspace recover those as |
| 5586 | * required after resuming. |
| 5587 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5588 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5589 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5590 | return 0; |
| 5591 | } |
| 5592 | |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5593 | int i915_gem_freeze_late(struct drm_i915_private *dev_priv) |
| 5594 | { |
| 5595 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5596 | struct list_head *phases[] = { |
| 5597 | &dev_priv->mm.unbound_list, |
| 5598 | &dev_priv->mm.bound_list, |
| 5599 | NULL |
| 5600 | }, **p; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5601 | |
| 5602 | /* Called just before we write the hibernation image. |
| 5603 | * |
| 5604 | * We need to update the domain tracking to reflect that the CPU |
| 5605 | * will be accessing all the pages to create and restore from the |
| 5606 | * hibernation, and so upon restoration those pages will be in the |
| 5607 | * CPU domain. |
| 5608 | * |
| 5609 | * To make sure the hibernation image contains the latest state, |
| 5610 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5611 | * |
| 5612 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5613 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5614 | */ |
| 5615 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 5616 | i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND); |
Chris Wilson | 17b93c4 | 2017-04-07 11:25:50 +0100 | [diff] [blame] | 5617 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5618 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5619 | spin_lock(&dev_priv->mm.obj_lock); |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5620 | for (p = phases; *p; p++) { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5621 | list_for_each_entry(obj, *p, mm.link) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 5622 | __start_cpu_write(obj); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5623 | } |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5624 | spin_unlock(&dev_priv->mm.obj_lock); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5625 | |
| 5626 | return 0; |
| 5627 | } |
| 5628 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5629 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5630 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5631 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5632 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5633 | |
| 5634 | /* Clean up our request list when the client is going away, so that |
| 5635 | * later retire_requests won't dereference our soon-to-be-gone |
| 5636 | * file_priv. |
| 5637 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5638 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5639 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5640 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5641 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5642 | } |
| 5643 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5644 | 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] | 5645 | { |
| 5646 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5647 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5648 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5649 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5650 | |
| 5651 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5652 | if (!file_priv) |
| 5653 | return -ENOMEM; |
| 5654 | |
| 5655 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5656 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5657 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5658 | |
| 5659 | spin_lock_init(&file_priv->mm.lock); |
| 5660 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5661 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5662 | file_priv->bsd_engine = -1; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5663 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5664 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5665 | if (ret) |
| 5666 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5667 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5668 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5669 | } |
| 5670 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5671 | /** |
| 5672 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5673 | * @old: current GEM buffer for the frontbuffer slots |
| 5674 | * @new: new GEM buffer for the frontbuffer slots |
| 5675 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5676 | * |
| 5677 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5678 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5679 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5680 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5681 | struct drm_i915_gem_object *new, |
| 5682 | unsigned frontbuffer_bits) |
| 5683 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5684 | /* Control of individual bits within the mask are guarded by |
| 5685 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5686 | * manipulation of individual bits. But since the bitfield as a whole |
| 5687 | * is updated using RMW, we need to use atomics in order to update |
| 5688 | * the bits. |
| 5689 | */ |
| 5690 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
| 5691 | sizeof(atomic_t) * BITS_PER_BYTE); |
| 5692 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5693 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5694 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5695 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5699 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5700 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5701 | } |
| 5702 | } |
| 5703 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5704 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5705 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5706 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5707 | const void *data, size_t size) |
| 5708 | { |
| 5709 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5710 | struct file *file; |
| 5711 | size_t offset; |
| 5712 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5713 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5714 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5715 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5716 | return obj; |
| 5717 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5718 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5719 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5720 | file = obj->base.filp; |
| 5721 | offset = 0; |
| 5722 | do { |
| 5723 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5724 | struct page *page; |
| 5725 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5726 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5727 | err = pagecache_write_begin(file, file->f_mapping, |
| 5728 | offset, len, 0, |
| 5729 | &page, &pgdata); |
| 5730 | if (err < 0) |
| 5731 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5732 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5733 | vaddr = kmap(page); |
| 5734 | memcpy(vaddr, data, len); |
| 5735 | kunmap(page); |
| 5736 | |
| 5737 | err = pagecache_write_end(file, file->f_mapping, |
| 5738 | offset, len, len, |
| 5739 | page, pgdata); |
| 5740 | if (err < 0) |
| 5741 | goto fail; |
| 5742 | |
| 5743 | size -= len; |
| 5744 | data += len; |
| 5745 | offset += len; |
| 5746 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5747 | |
| 5748 | return obj; |
| 5749 | |
| 5750 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5751 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5752 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5753 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5754 | |
| 5755 | struct scatterlist * |
| 5756 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5757 | unsigned int n, |
| 5758 | unsigned int *offset) |
| 5759 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5760 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5761 | struct scatterlist *sg; |
| 5762 | unsigned int idx, count; |
| 5763 | |
| 5764 | might_sleep(); |
| 5765 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5766 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5767 | |
| 5768 | /* As we iterate forward through the sg, we record each entry in a |
| 5769 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5770 | * this index previously, we will have an entry for it. |
| 5771 | * |
| 5772 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5773 | * sequential page access (where each new request is consecutive |
| 5774 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5775 | * i.e. O(1) with a large constant! |
| 5776 | */ |
| 5777 | if (n < READ_ONCE(iter->sg_idx)) |
| 5778 | goto lookup; |
| 5779 | |
| 5780 | mutex_lock(&iter->lock); |
| 5781 | |
| 5782 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5783 | * (or the subsequent) sg are fast - comparing against the last |
| 5784 | * sg is faster than going through the radixtree. |
| 5785 | */ |
| 5786 | |
| 5787 | sg = iter->sg_pos; |
| 5788 | idx = iter->sg_idx; |
| 5789 | count = __sg_page_count(sg); |
| 5790 | |
| 5791 | while (idx + count <= n) { |
| 5792 | unsigned long exception, i; |
| 5793 | int ret; |
| 5794 | |
| 5795 | /* If we cannot allocate and insert this entry, or the |
| 5796 | * individual pages from this range, cancel updating the |
| 5797 | * sg_idx so that on this lookup we are forced to linearly |
| 5798 | * scan onwards, but on future lookups we will try the |
| 5799 | * insertion again (in which case we need to be careful of |
| 5800 | * the error return reporting that we have already inserted |
| 5801 | * this index). |
| 5802 | */ |
| 5803 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5804 | if (ret && ret != -EEXIST) |
| 5805 | goto scan; |
| 5806 | |
| 5807 | exception = |
| 5808 | RADIX_TREE_EXCEPTIONAL_ENTRY | |
| 5809 | idx << RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5810 | for (i = 1; i < count; i++) { |
| 5811 | ret = radix_tree_insert(&iter->radix, idx + i, |
| 5812 | (void *)exception); |
| 5813 | if (ret && ret != -EEXIST) |
| 5814 | goto scan; |
| 5815 | } |
| 5816 | |
| 5817 | idx += count; |
| 5818 | sg = ____sg_next(sg); |
| 5819 | count = __sg_page_count(sg); |
| 5820 | } |
| 5821 | |
| 5822 | scan: |
| 5823 | iter->sg_pos = sg; |
| 5824 | iter->sg_idx = idx; |
| 5825 | |
| 5826 | mutex_unlock(&iter->lock); |
| 5827 | |
| 5828 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5829 | goto lookup; |
| 5830 | |
| 5831 | /* In case we failed to insert the entry into the radixtree, we need |
| 5832 | * to look beyond the current sg. |
| 5833 | */ |
| 5834 | while (idx + count <= n) { |
| 5835 | idx += count; |
| 5836 | sg = ____sg_next(sg); |
| 5837 | count = __sg_page_count(sg); |
| 5838 | } |
| 5839 | |
| 5840 | *offset = n - idx; |
| 5841 | return sg; |
| 5842 | |
| 5843 | lookup: |
| 5844 | rcu_read_lock(); |
| 5845 | |
| 5846 | sg = radix_tree_lookup(&iter->radix, n); |
| 5847 | GEM_BUG_ON(!sg); |
| 5848 | |
| 5849 | /* If this index is in the middle of multi-page sg entry, |
| 5850 | * the radixtree will contain an exceptional entry that points |
| 5851 | * to the start of that range. We will return the pointer to |
| 5852 | * the base page and the offset of this page within the |
| 5853 | * sg entry's range. |
| 5854 | */ |
| 5855 | *offset = 0; |
| 5856 | if (unlikely(radix_tree_exception(sg))) { |
| 5857 | unsigned long base = |
| 5858 | (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5859 | |
| 5860 | sg = radix_tree_lookup(&iter->radix, base); |
| 5861 | GEM_BUG_ON(!sg); |
| 5862 | |
| 5863 | *offset = n - base; |
| 5864 | } |
| 5865 | |
| 5866 | rcu_read_unlock(); |
| 5867 | |
| 5868 | return sg; |
| 5869 | } |
| 5870 | |
| 5871 | struct page * |
| 5872 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5873 | { |
| 5874 | struct scatterlist *sg; |
| 5875 | unsigned int offset; |
| 5876 | |
| 5877 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5878 | |
| 5879 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5880 | return nth_page(sg_page(sg), offset); |
| 5881 | } |
| 5882 | |
| 5883 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5884 | struct page * |
| 5885 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5886 | unsigned int n) |
| 5887 | { |
| 5888 | struct page *page; |
| 5889 | |
| 5890 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5891 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5892 | set_page_dirty(page); |
| 5893 | |
| 5894 | return page; |
| 5895 | } |
| 5896 | |
| 5897 | dma_addr_t |
| 5898 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5899 | unsigned long n) |
| 5900 | { |
| 5901 | struct scatterlist *sg; |
| 5902 | unsigned int offset; |
| 5903 | |
| 5904 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5905 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5906 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5907 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5908 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5909 | { |
| 5910 | struct sg_table *pages; |
| 5911 | int err; |
| 5912 | |
| 5913 | if (align > obj->base.size) |
| 5914 | return -EINVAL; |
| 5915 | |
| 5916 | if (obj->ops == &i915_gem_phys_ops) |
| 5917 | return 0; |
| 5918 | |
| 5919 | if (obj->ops != &i915_gem_object_ops) |
| 5920 | return -EINVAL; |
| 5921 | |
| 5922 | err = i915_gem_object_unbind(obj); |
| 5923 | if (err) |
| 5924 | return err; |
| 5925 | |
| 5926 | mutex_lock(&obj->mm.lock); |
| 5927 | |
| 5928 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5929 | err = -EFAULT; |
| 5930 | goto err_unlock; |
| 5931 | } |
| 5932 | |
| 5933 | if (obj->mm.quirked) { |
| 5934 | err = -EFAULT; |
| 5935 | goto err_unlock; |
| 5936 | } |
| 5937 | |
| 5938 | if (obj->mm.mapping) { |
| 5939 | err = -EBUSY; |
| 5940 | goto err_unlock; |
| 5941 | } |
| 5942 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5943 | pages = fetch_and_zero(&obj->mm.pages); |
| 5944 | if (pages) { |
| 5945 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 5946 | |
| 5947 | __i915_gem_object_reset_page_iter(obj); |
| 5948 | |
| 5949 | spin_lock(&i915->mm.obj_lock); |
| 5950 | list_del(&obj->mm.link); |
| 5951 | spin_unlock(&i915->mm.obj_lock); |
| 5952 | } |
| 5953 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5954 | obj->ops = &i915_gem_phys_ops; |
| 5955 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5956 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5957 | if (err) |
| 5958 | goto err_xfer; |
| 5959 | |
| 5960 | /* Perma-pin (until release) the physical set of pages */ |
| 5961 | __i915_gem_object_pin_pages(obj); |
| 5962 | |
| 5963 | if (!IS_ERR_OR_NULL(pages)) |
| 5964 | i915_gem_object_ops.put_pages(obj, pages); |
| 5965 | mutex_unlock(&obj->mm.lock); |
| 5966 | return 0; |
| 5967 | |
| 5968 | err_xfer: |
| 5969 | obj->ops = &i915_gem_object_ops; |
| 5970 | obj->mm.pages = pages; |
| 5971 | err_unlock: |
| 5972 | mutex_unlock(&obj->mm.lock); |
| 5973 | return err; |
| 5974 | } |
| 5975 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5976 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5977 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5978 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5979 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5980 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5981 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5982 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5983 | #endif |