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 | { |
| 356 | struct drm_i915_gem_request *rq; |
| 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); |
| 369 | if (i915_gem_request_completed(rq)) |
| 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 | e9af4ea | 2018-01-18 13:16:09 +0000 | [diff] [blame] | 388 | if (rps_client && !i915_gem_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 | |
| 393 | timeout = i915_wait_request(rq, flags, timeout); |
| 394 | |
| 395 | out: |
| 396 | if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq)) |
| 397 | i915_gem_request_retire_upto(rq); |
| 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 | |
| 436 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 437 | } else { |
| 438 | excl = reservation_object_get_excl_rcu(resv); |
| 439 | } |
| 440 | |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 441 | if (excl && timeout >= 0) { |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 442 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
| 443 | rps_client); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 444 | prune_fences = timeout >= 0; |
| 445 | } |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 446 | |
| 447 | dma_fence_put(excl); |
| 448 | |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 449 | /* Oportunistically prune the fences iff we know they have *all* been |
| 450 | * signaled and that the reservation object has not been changed (i.e. |
| 451 | * no new fences have been added). |
| 452 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 453 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 454 | if (reservation_object_trylock(resv)) { |
| 455 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 456 | reservation_object_add_excl_fence(resv, NULL); |
| 457 | reservation_object_unlock(resv); |
| 458 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 461 | return timeout; |
| 462 | } |
| 463 | |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 464 | static void __fence_set_priority(struct dma_fence *fence, int prio) |
| 465 | { |
| 466 | struct drm_i915_gem_request *rq; |
| 467 | struct intel_engine_cs *engine; |
| 468 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 469 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 470 | return; |
| 471 | |
| 472 | rq = to_request(fence); |
| 473 | engine = rq->engine; |
| 474 | if (!engine->schedule) |
| 475 | return; |
| 476 | |
| 477 | engine->schedule(rq, prio); |
| 478 | } |
| 479 | |
| 480 | static void fence_set_priority(struct dma_fence *fence, int prio) |
| 481 | { |
| 482 | /* Recurse once into a fence-array */ |
| 483 | if (dma_fence_is_array(fence)) { |
| 484 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 485 | int i; |
| 486 | |
| 487 | for (i = 0; i < array->num_fences; i++) |
| 488 | __fence_set_priority(array->fences[i], prio); |
| 489 | } else { |
| 490 | __fence_set_priority(fence, prio); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | int |
| 495 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 496 | unsigned int flags, |
| 497 | int prio) |
| 498 | { |
| 499 | struct dma_fence *excl; |
| 500 | |
| 501 | if (flags & I915_WAIT_ALL) { |
| 502 | struct dma_fence **shared; |
| 503 | unsigned int count, i; |
| 504 | int ret; |
| 505 | |
| 506 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 507 | &excl, &count, &shared); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | |
| 511 | for (i = 0; i < count; i++) { |
| 512 | fence_set_priority(shared[i], prio); |
| 513 | dma_fence_put(shared[i]); |
| 514 | } |
| 515 | |
| 516 | kfree(shared); |
| 517 | } else { |
| 518 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 519 | } |
| 520 | |
| 521 | if (excl) { |
| 522 | fence_set_priority(excl, prio); |
| 523 | dma_fence_put(excl); |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 528 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 529 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 530 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 531 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 532 | * @timeout: how long to wait |
Chris Wilson | a0a8b1c | 2017-11-09 14:06:44 +0000 | [diff] [blame] | 533 | * @rps_client: client (user process) to charge for any waitboosting |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 534 | */ |
| 535 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 536 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 537 | unsigned int flags, |
| 538 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 539 | struct intel_rps_client *rps_client) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 540 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 541 | might_sleep(); |
| 542 | #if IS_ENABLED(CONFIG_LOCKDEP) |
| 543 | GEM_BUG_ON(debug_locks && |
| 544 | !!lockdep_is_held(&obj->base.dev->struct_mutex) != |
| 545 | !!(flags & I915_WAIT_LOCKED)); |
| 546 | #endif |
| 547 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 548 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 549 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 550 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 551 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 552 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 556 | { |
| 557 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 558 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 559 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 562 | static int |
| 563 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 564 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 565 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 566 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 567 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 568 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 569 | |
| 570 | /* We manually control the domain here and pretend that it |
| 571 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 572 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 573 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 574 | if (copy_from_user(vaddr, user_data, args->size)) |
| 575 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 576 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 577 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 578 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 579 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 580 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 581 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 584 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 585 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 586 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 590 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 591 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 592 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 595 | static int |
| 596 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 597 | struct drm_i915_private *dev_priv, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 598 | uint64_t size, |
| 599 | uint32_t *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 600 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 601 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 602 | int ret; |
| 603 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 604 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 605 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 606 | if (size == 0) |
| 607 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 608 | |
| 609 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 610 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 611 | if (IS_ERR(obj)) |
| 612 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 613 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 614 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 615 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 616 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 617 | if (ret) |
| 618 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 619 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 620 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 624 | int |
| 625 | i915_gem_dumb_create(struct drm_file *file, |
| 626 | struct drm_device *dev, |
| 627 | struct drm_mode_create_dumb *args) |
| 628 | { |
| 629 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 630 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 631 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 632 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 633 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 634 | } |
| 635 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 636 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 637 | { |
| 638 | return !(obj->cache_level == I915_CACHE_NONE || |
| 639 | obj->cache_level == I915_CACHE_WT); |
| 640 | } |
| 641 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 642 | /** |
| 643 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 644 | * @dev: drm device pointer |
| 645 | * @data: ioctl data blob |
| 646 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 647 | */ |
| 648 | int |
| 649 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 650 | struct drm_file *file) |
| 651 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 652 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 653 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 654 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 655 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 656 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 657 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 658 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 659 | } |
| 660 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 661 | static inline enum fb_op_origin |
| 662 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 663 | { |
| 664 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 665 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 666 | } |
| 667 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 668 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 669 | { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 670 | /* |
| 671 | * No actual flushing is required for the GTT write domain for reads |
| 672 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 673 | * as far as we know, so there's no chipset flush. It also doesn't |
| 674 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 675 | * |
| 676 | * However, we do have to enforce the order so that all writes through |
| 677 | * the GTT land before any writes to the device, such as updates to |
| 678 | * the GATT itself. |
| 679 | * |
| 680 | * We also have to wait a bit for the writes to land from the GTT. |
| 681 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 682 | * timing. This issue has only been observed when switching quickly |
| 683 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 684 | * and it appears to only affect discrete GTT blocks (i.e. on LLC |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 685 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 686 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 687 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 688 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 689 | wmb(); |
| 690 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 691 | intel_runtime_pm_get(dev_priv); |
| 692 | spin_lock_irq(&dev_priv->uncore.lock); |
| 693 | |
| 694 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
| 695 | |
| 696 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 697 | intel_runtime_pm_put(dev_priv); |
| 698 | } |
| 699 | |
| 700 | static void |
| 701 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 702 | { |
| 703 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 704 | struct i915_vma *vma; |
| 705 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 706 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 707 | return; |
| 708 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 709 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 710 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 711 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 712 | |
| 713 | intel_fb_obj_flush(obj, |
| 714 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 715 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 716 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 717 | if (vma->iomap) |
| 718 | continue; |
| 719 | |
| 720 | i915_vma_unset_ggtt_write(vma); |
| 721 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 722 | break; |
| 723 | |
| 724 | case I915_GEM_DOMAIN_CPU: |
| 725 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 726 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 727 | |
| 728 | case I915_GEM_DOMAIN_RENDER: |
| 729 | if (gpu_write_needs_clflush(obj)) |
| 730 | obj->cache_dirty = true; |
| 731 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 734 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 735 | } |
| 736 | |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 737 | static inline int |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 738 | __copy_to_user_swizzled(char __user *cpu_vaddr, |
| 739 | const char *gpu_vaddr, int gpu_offset, |
| 740 | int length) |
| 741 | { |
| 742 | int ret, cpu_offset = 0; |
| 743 | |
| 744 | while (length > 0) { |
| 745 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 746 | int this_length = min(cacheline_end - gpu_offset, length); |
| 747 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 748 | |
| 749 | ret = __copy_to_user(cpu_vaddr + cpu_offset, |
| 750 | gpu_vaddr + swizzled_gpu_offset, |
| 751 | this_length); |
| 752 | if (ret) |
| 753 | return ret + length; |
| 754 | |
| 755 | cpu_offset += this_length; |
| 756 | gpu_offset += this_length; |
| 757 | length -= this_length; |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static inline int |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 764 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
| 765 | const char __user *cpu_vaddr, |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 766 | int length) |
| 767 | { |
| 768 | int ret, cpu_offset = 0; |
| 769 | |
| 770 | while (length > 0) { |
| 771 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 772 | int this_length = min(cacheline_end - gpu_offset, length); |
| 773 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 774 | |
| 775 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, |
| 776 | cpu_vaddr + cpu_offset, |
| 777 | this_length); |
| 778 | if (ret) |
| 779 | return ret + length; |
| 780 | |
| 781 | cpu_offset += this_length; |
| 782 | gpu_offset += this_length; |
| 783 | length -= this_length; |
| 784 | } |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 789 | /* |
| 790 | * Pins the specified object's pages and synchronizes the object with |
| 791 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 792 | * flush the object from the CPU cache. |
| 793 | */ |
| 794 | 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] | 795 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 796 | { |
| 797 | int ret; |
| 798 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 799 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 800 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 801 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 802 | if (!i915_gem_object_has_struct_page(obj)) |
| 803 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 804 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 805 | ret = i915_gem_object_wait(obj, |
| 806 | I915_WAIT_INTERRUPTIBLE | |
| 807 | I915_WAIT_LOCKED, |
| 808 | MAX_SCHEDULE_TIMEOUT, |
| 809 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 810 | if (ret) |
| 811 | return ret; |
| 812 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 813 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 814 | if (ret) |
| 815 | return ret; |
| 816 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 817 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 818 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 819 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 820 | if (ret) |
| 821 | goto err_unpin; |
| 822 | else |
| 823 | goto out; |
| 824 | } |
| 825 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 826 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 827 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 828 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 829 | * read domain and manually flush cachelines (if required). This |
| 830 | * optimizes for the case when the gpu will dirty the data |
| 831 | * anyway again before the next pread happens. |
| 832 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 833 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 834 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 835 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 836 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 837 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 838 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 839 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 840 | |
| 841 | err_unpin: |
| 842 | i915_gem_object_unpin_pages(obj); |
| 843 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 847 | unsigned int *needs_clflush) |
| 848 | { |
| 849 | int ret; |
| 850 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 851 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 852 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 853 | *needs_clflush = 0; |
| 854 | if (!i915_gem_object_has_struct_page(obj)) |
| 855 | return -ENODEV; |
| 856 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 857 | ret = i915_gem_object_wait(obj, |
| 858 | I915_WAIT_INTERRUPTIBLE | |
| 859 | I915_WAIT_LOCKED | |
| 860 | I915_WAIT_ALL, |
| 861 | MAX_SCHEDULE_TIMEOUT, |
| 862 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 863 | if (ret) |
| 864 | return ret; |
| 865 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 866 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 867 | if (ret) |
| 868 | return ret; |
| 869 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 870 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 871 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 872 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 873 | if (ret) |
| 874 | goto err_unpin; |
| 875 | else |
| 876 | goto out; |
| 877 | } |
| 878 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 879 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 880 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 881 | /* If we're not in the cpu write domain, set ourself into the |
| 882 | * gtt write domain and manually flush cachelines (as required). |
| 883 | * This optimizes for the case when the gpu will use the data |
| 884 | * right away and we therefore have to clflush anyway. |
| 885 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 886 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 887 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 888 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 889 | /* |
| 890 | * Same trick applies to invalidate partially written |
| 891 | * cachelines read before writing. |
| 892 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 893 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 894 | *needs_clflush |= CLFLUSH_BEFORE; |
| 895 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 896 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 897 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 898 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 899 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 900 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 901 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 902 | |
| 903 | err_unpin: |
| 904 | i915_gem_object_unpin_pages(obj); |
| 905 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 908 | static void |
| 909 | shmem_clflush_swizzled_range(char *addr, unsigned long length, |
| 910 | bool swizzled) |
| 911 | { |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 912 | if (unlikely(swizzled)) { |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 913 | unsigned long start = (unsigned long) addr; |
| 914 | unsigned long end = (unsigned long) addr + length; |
| 915 | |
| 916 | /* For swizzling simply ensure that we always flush both |
| 917 | * channels. Lame, but simple and it works. Swizzled |
| 918 | * pwrite/pread is far from a hotpath - current userspace |
| 919 | * doesn't use it at all. */ |
| 920 | start = round_down(start, 128); |
| 921 | end = round_up(end, 128); |
| 922 | |
| 923 | drm_clflush_virt_range((void *)start, end - start); |
| 924 | } else { |
| 925 | drm_clflush_virt_range(addr, length); |
| 926 | } |
| 927 | |
| 928 | } |
| 929 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 930 | /* Only difference to the fast-path function is that this can handle bit17 |
| 931 | * and uses non-atomic copy and kmap functions. */ |
| 932 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 933 | shmem_pread_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 934 | char __user *user_data, |
| 935 | bool page_do_bit17_swizzling, bool needs_clflush) |
| 936 | { |
| 937 | char *vaddr; |
| 938 | int ret; |
| 939 | |
| 940 | vaddr = kmap(page); |
| 941 | if (needs_clflush) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 942 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 943 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 944 | |
| 945 | if (page_do_bit17_swizzling) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 946 | ret = __copy_to_user_swizzled(user_data, vaddr, offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 947 | else |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 948 | ret = __copy_to_user(user_data, vaddr + offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 949 | kunmap(page); |
| 950 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 951 | return ret ? - EFAULT : 0; |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 952 | } |
| 953 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 954 | static int |
| 955 | shmem_pread(struct page *page, int offset, int length, char __user *user_data, |
| 956 | bool page_do_bit17_swizzling, bool needs_clflush) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 957 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 958 | int ret; |
| 959 | |
| 960 | ret = -ENODEV; |
| 961 | if (!page_do_bit17_swizzling) { |
| 962 | char *vaddr = kmap_atomic(page); |
| 963 | |
| 964 | if (needs_clflush) |
| 965 | drm_clflush_virt_range(vaddr + offset, length); |
| 966 | ret = __copy_to_user_inatomic(user_data, vaddr + offset, length); |
| 967 | kunmap_atomic(vaddr); |
| 968 | } |
| 969 | if (ret == 0) |
| 970 | return 0; |
| 971 | |
| 972 | return shmem_pread_slow(page, offset, length, user_data, |
| 973 | page_do_bit17_swizzling, needs_clflush); |
| 974 | } |
| 975 | |
| 976 | static int |
| 977 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 978 | struct drm_i915_gem_pread *args) |
| 979 | { |
| 980 | char __user *user_data; |
| 981 | u64 remain; |
| 982 | unsigned int obj_do_bit17_swizzling; |
| 983 | unsigned int needs_clflush; |
| 984 | unsigned int idx, offset; |
| 985 | int ret; |
| 986 | |
| 987 | obj_do_bit17_swizzling = 0; |
| 988 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 989 | obj_do_bit17_swizzling = BIT(17); |
| 990 | |
| 991 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 992 | if (ret) |
| 993 | return ret; |
| 994 | |
| 995 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 996 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 997 | if (ret) |
| 998 | return ret; |
| 999 | |
| 1000 | remain = args->size; |
| 1001 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1002 | offset = offset_in_page(args->offset); |
| 1003 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1004 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1005 | int length; |
| 1006 | |
| 1007 | length = remain; |
| 1008 | if (offset + length > PAGE_SIZE) |
| 1009 | length = PAGE_SIZE - offset; |
| 1010 | |
| 1011 | ret = shmem_pread(page, offset, length, user_data, |
| 1012 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1013 | needs_clflush); |
| 1014 | if (ret) |
| 1015 | break; |
| 1016 | |
| 1017 | remain -= length; |
| 1018 | user_data += length; |
| 1019 | offset = 0; |
| 1020 | } |
| 1021 | |
| 1022 | i915_gem_obj_finish_shmem_access(obj); |
| 1023 | return ret; |
| 1024 | } |
| 1025 | |
| 1026 | static inline bool |
| 1027 | gtt_user_read(struct io_mapping *mapping, |
| 1028 | loff_t base, int offset, |
| 1029 | char __user *user_data, int length) |
| 1030 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1031 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1032 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1033 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1034 | /* 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] | 1035 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1036 | unwritten = __copy_to_user_inatomic(user_data, |
| 1037 | (void __force *)vaddr + offset, |
| 1038 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1039 | io_mapping_unmap_atomic(vaddr); |
| 1040 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1041 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1042 | unwritten = copy_to_user(user_data, |
| 1043 | (void __force *)vaddr + offset, |
| 1044 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1045 | io_mapping_unmap(vaddr); |
| 1046 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1047 | return unwritten; |
| 1048 | } |
| 1049 | |
| 1050 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1051 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1052 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1053 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1054 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1055 | struct i915_ggtt *ggtt = &i915->ggtt; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1056 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1057 | struct i915_vma *vma; |
| 1058 | void __user *user_data; |
| 1059 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1060 | int ret; |
| 1061 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1062 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1063 | if (ret) |
| 1064 | return ret; |
| 1065 | |
| 1066 | intel_runtime_pm_get(i915); |
| 1067 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1068 | PIN_MAPPABLE | |
| 1069 | PIN_NONFAULT | |
| 1070 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1071 | if (!IS_ERR(vma)) { |
| 1072 | node.start = i915_ggtt_offset(vma); |
| 1073 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1074 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1075 | if (ret) { |
| 1076 | i915_vma_unpin(vma); |
| 1077 | vma = ERR_PTR(ret); |
| 1078 | } |
| 1079 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1080 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1081 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1082 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1083 | goto out_unlock; |
| 1084 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1088 | if (ret) |
| 1089 | goto out_unpin; |
| 1090 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1091 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1092 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1093 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1094 | remain = args->size; |
| 1095 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1096 | |
| 1097 | while (remain > 0) { |
| 1098 | /* Operation in this page |
| 1099 | * |
| 1100 | * page_base = page offset within aperture |
| 1101 | * page_offset = offset within page |
| 1102 | * page_length = bytes to copy for this page |
| 1103 | */ |
| 1104 | u32 page_base = node.start; |
| 1105 | unsigned page_offset = offset_in_page(offset); |
| 1106 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1107 | page_length = remain < page_length ? remain : page_length; |
| 1108 | if (node.allocated) { |
| 1109 | wmb(); |
| 1110 | ggtt->base.insert_page(&ggtt->base, |
| 1111 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1112 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1113 | wmb(); |
| 1114 | } else { |
| 1115 | page_base += offset & PAGE_MASK; |
| 1116 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1117 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1118 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1119 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1120 | ret = -EFAULT; |
| 1121 | break; |
| 1122 | } |
| 1123 | |
| 1124 | remain -= page_length; |
| 1125 | user_data += page_length; |
| 1126 | offset += page_length; |
| 1127 | } |
| 1128 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1129 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1130 | out_unpin: |
| 1131 | if (node.allocated) { |
| 1132 | wmb(); |
| 1133 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1134 | node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1135 | remove_mappable_node(&node); |
| 1136 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1137 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1138 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1139 | out_unlock: |
| 1140 | intel_runtime_pm_put(i915); |
| 1141 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1142 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1143 | return ret; |
| 1144 | } |
| 1145 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1146 | /** |
| 1147 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1148 | * @dev: drm device pointer |
| 1149 | * @data: ioctl data blob |
| 1150 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1151 | * |
| 1152 | * On error, the contents of *data are undefined. |
| 1153 | */ |
| 1154 | int |
| 1155 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1156 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1157 | { |
| 1158 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1159 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1160 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1161 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1162 | if (args->size == 0) |
| 1163 | return 0; |
| 1164 | |
| 1165 | if (!access_ok(VERIFY_WRITE, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1166 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1167 | args->size)) |
| 1168 | return -EFAULT; |
| 1169 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1170 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1171 | if (!obj) |
| 1172 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1173 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1174 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1175 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1176 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1177 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1178 | } |
| 1179 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1180 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1181 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1182 | ret = i915_gem_object_wait(obj, |
| 1183 | I915_WAIT_INTERRUPTIBLE, |
| 1184 | MAX_SCHEDULE_TIMEOUT, |
| 1185 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1186 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1187 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1188 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1189 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1190 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1191 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1192 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1193 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1194 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1195 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1196 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1197 | i915_gem_object_unpin_pages(obj); |
| 1198 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1199 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1200 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1203 | /* This is the fast write path which cannot handle |
| 1204 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1205 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1206 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1207 | static inline bool |
| 1208 | ggtt_write(struct io_mapping *mapping, |
| 1209 | loff_t base, int offset, |
| 1210 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1211 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1212 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1213 | unsigned long unwritten; |
| 1214 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1215 | /* 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] | 1216 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1217 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1218 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1219 | io_mapping_unmap_atomic(vaddr); |
| 1220 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1221 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1222 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1223 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1224 | io_mapping_unmap(vaddr); |
| 1225 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1226 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1227 | return unwritten; |
| 1228 | } |
| 1229 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1230 | /** |
| 1231 | * This is the fast pwrite path, where we copy the data directly from the |
| 1232 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1233 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1234 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1235 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1236 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1237 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1238 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1239 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1240 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1241 | struct i915_ggtt *ggtt = &i915->ggtt; |
| 1242 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1243 | struct i915_vma *vma; |
| 1244 | u64 remain, offset; |
| 1245 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1246 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1247 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1248 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1249 | if (ret) |
| 1250 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1251 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1252 | if (i915_gem_object_has_struct_page(obj)) { |
| 1253 | /* |
| 1254 | * Avoid waking the device up if we can fallback, as |
| 1255 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1256 | * depending on PCI sleeps and our own resume time). |
| 1257 | * This easily dwarfs any performance advantage from |
| 1258 | * using the cache bypass of indirect GGTT access. |
| 1259 | */ |
| 1260 | if (!intel_runtime_pm_get_if_in_use(i915)) { |
| 1261 | ret = -EFAULT; |
| 1262 | goto out_unlock; |
| 1263 | } |
| 1264 | } else { |
| 1265 | /* No backing pages, no fallback, we must force GGTT access */ |
| 1266 | intel_runtime_pm_get(i915); |
| 1267 | } |
| 1268 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1269 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1270 | PIN_MAPPABLE | |
| 1271 | PIN_NONFAULT | |
| 1272 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1273 | if (!IS_ERR(vma)) { |
| 1274 | node.start = i915_ggtt_offset(vma); |
| 1275 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1276 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1277 | if (ret) { |
| 1278 | i915_vma_unpin(vma); |
| 1279 | vma = ERR_PTR(ret); |
| 1280 | } |
| 1281 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1282 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1283 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1284 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1285 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1286 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1287 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1288 | |
| 1289 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1290 | if (ret) |
| 1291 | goto out_unpin; |
| 1292 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1293 | mutex_unlock(&i915->drm.struct_mutex); |
| 1294 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1295 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1296 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1297 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1298 | offset = args->offset; |
| 1299 | remain = args->size; |
| 1300 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1301 | /* Operation in this page |
| 1302 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1303 | * page_base = page offset within aperture |
| 1304 | * page_offset = offset within page |
| 1305 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1306 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1307 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1308 | unsigned int page_offset = offset_in_page(offset); |
| 1309 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1310 | page_length = remain < page_length ? remain : page_length; |
| 1311 | if (node.allocated) { |
| 1312 | wmb(); /* flush the write before we modify the GGTT */ |
| 1313 | ggtt->base.insert_page(&ggtt->base, |
| 1314 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1315 | node.start, I915_CACHE_NONE, 0); |
| 1316 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1317 | } else { |
| 1318 | page_base += offset & PAGE_MASK; |
| 1319 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1320 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1321 | * source page isn't available. Return the error and we'll |
| 1322 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1323 | * If the object is non-shmem backed, we retry again with the |
| 1324 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1325 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1326 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1327 | user_data, page_length)) { |
| 1328 | ret = -EFAULT; |
| 1329 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1330 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1331 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1332 | remain -= page_length; |
| 1333 | user_data += page_length; |
| 1334 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1335 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1336 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1337 | |
| 1338 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1339 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1340 | if (node.allocated) { |
| 1341 | wmb(); |
| 1342 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1343 | node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1344 | remove_mappable_node(&node); |
| 1345 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1346 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1347 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1348 | out_rpm: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1349 | intel_runtime_pm_put(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1350 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1351 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1352 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1355 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1356 | shmem_pwrite_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1357 | char __user *user_data, |
| 1358 | bool page_do_bit17_swizzling, |
| 1359 | bool needs_clflush_before, |
| 1360 | bool needs_clflush_after) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1361 | { |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1362 | char *vaddr; |
| 1363 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1364 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1365 | vaddr = kmap(page); |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 1366 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1367 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1368 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1369 | if (page_do_bit17_swizzling) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1370 | ret = __copy_from_user_swizzled(vaddr, offset, user_data, |
| 1371 | length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1372 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1373 | ret = __copy_from_user(vaddr + offset, user_data, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1374 | if (needs_clflush_after) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1375 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1376 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1377 | kunmap(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1378 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 1379 | return ret ? -EFAULT : 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1382 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1383 | * Flushes invalid cachelines before writing to the target if |
| 1384 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1385 | * writing if needs_clflush is set. |
| 1386 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1387 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1388 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
| 1389 | bool page_do_bit17_swizzling, |
| 1390 | bool needs_clflush_before, |
| 1391 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1392 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1393 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1394 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1395 | ret = -ENODEV; |
| 1396 | if (!page_do_bit17_swizzling) { |
| 1397 | char *vaddr = kmap_atomic(page); |
| 1398 | |
| 1399 | if (needs_clflush_before) |
| 1400 | drm_clflush_virt_range(vaddr + offset, len); |
| 1401 | ret = __copy_from_user_inatomic(vaddr + offset, user_data, len); |
| 1402 | if (needs_clflush_after) |
| 1403 | drm_clflush_virt_range(vaddr + offset, len); |
| 1404 | |
| 1405 | kunmap_atomic(vaddr); |
| 1406 | } |
| 1407 | if (ret == 0) |
| 1408 | return ret; |
| 1409 | |
| 1410 | return shmem_pwrite_slow(page, offset, len, user_data, |
| 1411 | page_do_bit17_swizzling, |
| 1412 | needs_clflush_before, |
| 1413 | needs_clflush_after); |
| 1414 | } |
| 1415 | |
| 1416 | static int |
| 1417 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1418 | const struct drm_i915_gem_pwrite *args) |
| 1419 | { |
| 1420 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1421 | void __user *user_data; |
| 1422 | u64 remain; |
| 1423 | unsigned int obj_do_bit17_swizzling; |
| 1424 | unsigned int partial_cacheline_write; |
| 1425 | unsigned int needs_clflush; |
| 1426 | unsigned int offset, idx; |
| 1427 | int ret; |
| 1428 | |
| 1429 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1430 | if (ret) |
| 1431 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1432 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1433 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1434 | mutex_unlock(&i915->drm.struct_mutex); |
| 1435 | if (ret) |
| 1436 | return ret; |
| 1437 | |
| 1438 | obj_do_bit17_swizzling = 0; |
| 1439 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 1440 | obj_do_bit17_swizzling = BIT(17); |
| 1441 | |
| 1442 | /* If we don't overwrite a cacheline completely we need to be |
| 1443 | * careful to have up-to-date data by first clflushing. Don't |
| 1444 | * overcomplicate things and flush the entire patch. |
| 1445 | */ |
| 1446 | partial_cacheline_write = 0; |
| 1447 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1448 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1449 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1450 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1451 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1452 | offset = offset_in_page(args->offset); |
| 1453 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1454 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1455 | int length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1456 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1457 | length = remain; |
| 1458 | if (offset + length > PAGE_SIZE) |
| 1459 | length = PAGE_SIZE - offset; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1460 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1461 | ret = shmem_pwrite(page, offset, length, user_data, |
| 1462 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1463 | (offset | length) & partial_cacheline_write, |
| 1464 | needs_clflush & CLFLUSH_AFTER); |
| 1465 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1466 | break; |
| 1467 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1468 | remain -= length; |
| 1469 | user_data += length; |
| 1470 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1473 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1474 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1475 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /** |
| 1479 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1480 | * @dev: drm device |
| 1481 | * @data: ioctl data blob |
| 1482 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1483 | * |
| 1484 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1485 | */ |
| 1486 | int |
| 1487 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1488 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1489 | { |
| 1490 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1491 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1492 | int ret; |
| 1493 | |
| 1494 | if (args->size == 0) |
| 1495 | return 0; |
| 1496 | |
| 1497 | if (!access_ok(VERIFY_READ, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1498 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1499 | args->size)) |
| 1500 | return -EFAULT; |
| 1501 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1502 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1503 | if (!obj) |
| 1504 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1505 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1506 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1507 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1508 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1509 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1512 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1513 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1514 | ret = -ENODEV; |
| 1515 | if (obj->ops->pwrite) |
| 1516 | ret = obj->ops->pwrite(obj, args); |
| 1517 | if (ret != -ENODEV) |
| 1518 | goto err; |
| 1519 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1520 | ret = i915_gem_object_wait(obj, |
| 1521 | I915_WAIT_INTERRUPTIBLE | |
| 1522 | I915_WAIT_ALL, |
| 1523 | MAX_SCHEDULE_TIMEOUT, |
| 1524 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1525 | if (ret) |
| 1526 | goto err; |
| 1527 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1528 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1529 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1530 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1531 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1532 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1533 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1534 | * it would end up going through the fenced access, and we'll get |
| 1535 | * different detiling behavior between reading and writing. |
| 1536 | * pread/pwrite currently are reading and writing from the CPU |
| 1537 | * perspective, requiring manual detiling by the client. |
| 1538 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1539 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1540 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1541 | /* Note that the gtt paths might fail with non-page-backed user |
| 1542 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1543 | * textures). Fallback to the shmem path in that case. |
| 1544 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1545 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1546 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1547 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1548 | if (obj->phys_handle) |
| 1549 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1550 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1551 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1552 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1553 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1554 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1555 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1556 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1557 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1560 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1561 | { |
| 1562 | struct drm_i915_private *i915; |
| 1563 | struct list_head *list; |
| 1564 | struct i915_vma *vma; |
| 1565 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1566 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1567 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1568 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1569 | if (i915_vma_is_active(vma)) |
| 1570 | continue; |
| 1571 | |
| 1572 | if (!drm_mm_node_allocated(&vma->node)) |
| 1573 | continue; |
| 1574 | |
| 1575 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 1576 | } |
| 1577 | |
| 1578 | i915 = to_i915(obj->base.dev); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1579 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1580 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1581 | list_move_tail(&obj->mm.link, list); |
| 1582 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1585 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1586 | * Called when user space prepares to use an object with the CPU, either |
| 1587 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1588 | * @dev: drm device |
| 1589 | * @data: ioctl data blob |
| 1590 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1591 | */ |
| 1592 | int |
| 1593 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1594 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1595 | { |
| 1596 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1597 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1598 | uint32_t read_domains = args->read_domains; |
| 1599 | uint32_t write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1600 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1601 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1602 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1603 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1604 | return -EINVAL; |
| 1605 | |
| 1606 | /* Having something in the write domain implies it's in the read |
| 1607 | * domain, and only that read domain. Enforce that in the request. |
| 1608 | */ |
| 1609 | if (write_domain != 0 && read_domains != write_domain) |
| 1610 | return -EINVAL; |
| 1611 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1612 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1613 | if (!obj) |
| 1614 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1615 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1616 | /* Try to flush the object off the GPU without holding the lock. |
| 1617 | * We will repeat the flush holding the lock in the normal manner |
| 1618 | * to catch cases where we are gazumped. |
| 1619 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1620 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1621 | I915_WAIT_INTERRUPTIBLE | |
| 1622 | (write_domain ? I915_WAIT_ALL : 0), |
| 1623 | MAX_SCHEDULE_TIMEOUT, |
| 1624 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1625 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1626 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1627 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1628 | /* |
| 1629 | * Proxy objects do not control access to the backing storage, ergo |
| 1630 | * they cannot be used as a means to manipulate the cache domain |
| 1631 | * tracking for that backing storage. The proxy object is always |
| 1632 | * considered to be outside of any cache domain. |
| 1633 | */ |
| 1634 | if (i915_gem_object_is_proxy(obj)) { |
| 1635 | err = -ENXIO; |
| 1636 | goto out; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1641 | * direct access in memory with previous cached writes through |
| 1642 | * shmemfs and that our cache domain tracking remains valid. |
| 1643 | * For example, if the obj->filp was moved to swap without us |
| 1644 | * being notified and releasing the pages, we would mistakenly |
| 1645 | * continue to assume that the obj remained out of the CPU cached |
| 1646 | * domain. |
| 1647 | */ |
| 1648 | err = i915_gem_object_pin_pages(obj); |
| 1649 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1650 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1651 | |
| 1652 | err = i915_mutex_lock_interruptible(dev); |
| 1653 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1654 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1655 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1656 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1657 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1658 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1659 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1660 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1661 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1662 | |
| 1663 | /* And bump the LRU for this access */ |
| 1664 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1665 | |
| 1666 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1667 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1668 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1669 | intel_fb_obj_invalidate(obj, |
| 1670 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1671 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1672 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1673 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1674 | out: |
| 1675 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1676 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1681 | * @dev: drm device |
| 1682 | * @data: ioctl data blob |
| 1683 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1684 | */ |
| 1685 | int |
| 1686 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1687 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1688 | { |
| 1689 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1690 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1691 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1692 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1693 | if (!obj) |
| 1694 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1695 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1696 | /* |
| 1697 | * Proxy objects are barred from CPU access, so there is no |
| 1698 | * need to ban sw_finish as it is a nop. |
| 1699 | */ |
| 1700 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1701 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1702 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1703 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1704 | |
| 1705 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1709 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1710 | * it is mapped to. |
| 1711 | * @dev: drm device |
| 1712 | * @data: ioctl data blob |
| 1713 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1714 | * |
| 1715 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1716 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1717 | * |
| 1718 | * IMPORTANT: |
| 1719 | * |
| 1720 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1721 | * mmap support, please don't implement mmap support like here. The modern way |
| 1722 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1723 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1724 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1725 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1726 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1727 | */ |
| 1728 | int |
| 1729 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1730 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1731 | { |
| 1732 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1733 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1734 | unsigned long addr; |
| 1735 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1736 | if (args->flags & ~(I915_MMAP_WC)) |
| 1737 | return -EINVAL; |
| 1738 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1739 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1740 | return -ENODEV; |
| 1741 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1742 | obj = i915_gem_object_lookup(file, args->handle); |
| 1743 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1744 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1745 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1746 | /* prime objects have no backing filp to GEM mmap |
| 1747 | * pages from. |
| 1748 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1749 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1750 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1751 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1752 | } |
| 1753 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1754 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1755 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1756 | args->offset); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1757 | if (args->flags & I915_MMAP_WC) { |
| 1758 | struct mm_struct *mm = current->mm; |
| 1759 | struct vm_area_struct *vma; |
| 1760 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1761 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1762 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1763 | return -EINTR; |
| 1764 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1765 | vma = find_vma(mm, addr); |
| 1766 | if (vma) |
| 1767 | vma->vm_page_prot = |
| 1768 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1769 | else |
| 1770 | addr = -ENOMEM; |
| 1771 | up_write(&mm->mmap_sem); |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1772 | |
| 1773 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1774 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1775 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1776 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1777 | if (IS_ERR((void *)addr)) |
| 1778 | return addr; |
| 1779 | |
| 1780 | args->addr_ptr = (uint64_t) addr; |
| 1781 | |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1785 | static unsigned int tile_row_pages(struct drm_i915_gem_object *obj) |
| 1786 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1787 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1788 | } |
| 1789 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1790 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1791 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1792 | * |
| 1793 | * A history of the GTT mmap interface: |
| 1794 | * |
| 1795 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1796 | * aligned and suitable for fencing, and still fit into the available |
| 1797 | * mappable space left by the pinned display objects. A classic problem |
| 1798 | * we called the page-fault-of-doom where we would ping-pong between |
| 1799 | * two objects that could not fit inside the GTT and so the memcpy |
| 1800 | * would page one object in at the expense of the other between every |
| 1801 | * single byte. |
| 1802 | * |
| 1803 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1804 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1805 | * object is too large for the available space (or simply too large |
| 1806 | * for the mappable aperture!), a view is created instead and faulted |
| 1807 | * into userspace. (This view is aligned and sized appropriately for |
| 1808 | * fenced access.) |
| 1809 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1810 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1811 | * delayed writes via GTT before performing direct access via WC. |
| 1812 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1813 | * Restrictions: |
| 1814 | * |
| 1815 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1816 | * hangs on some architectures, corruption on others. An attempt to service |
| 1817 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1818 | * |
| 1819 | * * the object must be able to fit into RAM (physical memory, though no |
| 1820 | * limited to the mappable aperture). |
| 1821 | * |
| 1822 | * |
| 1823 | * Caveats: |
| 1824 | * |
| 1825 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1826 | * all data to system memory. Subsequent access will not be synchronized. |
| 1827 | * |
| 1828 | * * all mappings are revoked on runtime device suspend. |
| 1829 | * |
| 1830 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1831 | * (older machines require fence register for display and blitter access |
| 1832 | * as well). Contention of the fence registers will cause the previous users |
| 1833 | * to be unmapped and any new access will generate new page faults. |
| 1834 | * |
| 1835 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1836 | * rather than the expected SIGSEGV. |
| 1837 | */ |
| 1838 | int i915_gem_mmap_gtt_version(void) |
| 1839 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1840 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1841 | } |
| 1842 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1843 | static inline struct i915_ggtt_view |
| 1844 | compute_partial_view(struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1845 | pgoff_t page_offset, |
| 1846 | unsigned int chunk) |
| 1847 | { |
| 1848 | struct i915_ggtt_view view; |
| 1849 | |
| 1850 | if (i915_gem_object_is_tiled(obj)) |
| 1851 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1852 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1853 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1854 | view.partial.offset = rounddown(page_offset, chunk); |
| 1855 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1856 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1857 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1858 | |
| 1859 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1860 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1861 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1862 | |
| 1863 | return view; |
| 1864 | } |
| 1865 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1866 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1867 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1868 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1869 | * |
| 1870 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1871 | * from userspace. The fault handler takes care of binding the object to |
| 1872 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1873 | * only if needed based on whether the old reg is still valid or the object |
| 1874 | * is tiled) and inserting a new PTE into the faulting process. |
| 1875 | * |
| 1876 | * Note that the faulting process may involve evicting existing objects |
| 1877 | * from the GTT and/or fence registers to make room. So performance may |
| 1878 | * suffer if the GTT working set is large or there are few fence registers |
| 1879 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1880 | * |
| 1881 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1882 | * 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] | 1883 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1884 | int i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1885 | { |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1886 | #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1887 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1888 | 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] | 1889 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1890 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1891 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1892 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1893 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1894 | pgoff_t page_offset; |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1895 | unsigned int flags; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1896 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1897 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1898 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1899 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1900 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1901 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1902 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1903 | /* 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] | 1904 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1905 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1906 | * where we are gazumped. |
| 1907 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1908 | ret = i915_gem_object_wait(obj, |
| 1909 | I915_WAIT_INTERRUPTIBLE, |
| 1910 | MAX_SCHEDULE_TIMEOUT, |
| 1911 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1912 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1913 | goto err; |
| 1914 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1915 | ret = i915_gem_object_pin_pages(obj); |
| 1916 | if (ret) |
| 1917 | goto err; |
| 1918 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1919 | intel_runtime_pm_get(dev_priv); |
| 1920 | |
| 1921 | ret = i915_mutex_lock_interruptible(dev); |
| 1922 | if (ret) |
| 1923 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1924 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1925 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1926 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1927 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1928 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1931 | /* If the object is smaller than a couple of partial vma, it is |
| 1932 | * not worth only creating a single partial vma - we may as well |
| 1933 | * clear enough space for the full object. |
| 1934 | */ |
| 1935 | flags = PIN_MAPPABLE; |
| 1936 | if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT) |
| 1937 | flags |= PIN_NONBLOCK | PIN_NONFAULT; |
| 1938 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1939 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1940 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1941 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1942 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1943 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1944 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1945 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1946 | /* Userspace is now writing through an untracked VMA, abandon |
| 1947 | * all hope that the hardware is able to track future writes. |
| 1948 | */ |
| 1949 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1950 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1951 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); |
| 1952 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1953 | if (IS_ERR(vma)) { |
| 1954 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1955 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1956 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1957 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1958 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1959 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1960 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1961 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1962 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1963 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1964 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1965 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1966 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1967 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1968 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1969 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1970 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1971 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1972 | if (ret) |
| 1973 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1974 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1975 | /* Mark as being mmapped into userspace for later revocation */ |
| 1976 | assert_rpm_wakelock_held(dev_priv); |
| 1977 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1978 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1979 | GEM_BUG_ON(!obj->userfault_count); |
| 1980 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1981 | i915_vma_set_ggtt_write(vma); |
| 1982 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1983 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1984 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1985 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1986 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1987 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1988 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1989 | err_rpm: |
| 1990 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1991 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1992 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1993 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1994 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1995 | /* |
| 1996 | * We eat errors when the gpu is terminally wedged to avoid |
| 1997 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1998 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1999 | * and so needs to be reported. |
| 2000 | */ |
| 2001 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2002 | ret = VM_FAULT_SIGBUS; |
| 2003 | break; |
| 2004 | } |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 2005 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 2006 | /* |
| 2007 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 2008 | * handler to reset everything when re-faulting in |
| 2009 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2010 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 2011 | case 0: |
| 2012 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 2013 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 2014 | case -EBUSY: |
| 2015 | /* |
| 2016 | * EBUSY is ok: this just means that another thread |
| 2017 | * already did the job. |
| 2018 | */ |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2019 | ret = VM_FAULT_NOPAGE; |
| 2020 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2021 | case -ENOMEM: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2022 | ret = VM_FAULT_OOM; |
| 2023 | break; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2024 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2025 | case -EFAULT: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2026 | ret = VM_FAULT_SIGBUS; |
| 2027 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2028 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2029 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2030 | ret = VM_FAULT_SIGBUS; |
| 2031 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2032 | } |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2033 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2034 | } |
| 2035 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2036 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2037 | { |
| 2038 | struct i915_vma *vma; |
| 2039 | |
| 2040 | GEM_BUG_ON(!obj->userfault_count); |
| 2041 | |
| 2042 | obj->userfault_count = 0; |
| 2043 | list_del(&obj->userfault_link); |
| 2044 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2045 | obj->base.dev->anon_inode->i_mapping); |
| 2046 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 2047 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2048 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2049 | } |
| 2050 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2051 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2052 | * i915_gem_release_mmap - remove physical page mappings |
| 2053 | * @obj: obj in question |
| 2054 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2055 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2056 | * relinquish ownership of the pages back to the system. |
| 2057 | * |
| 2058 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2059 | * object through the GTT and then lose the fence register due to |
| 2060 | * resource pressure. Similarly if the object has been moved out of the |
| 2061 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2062 | * mapping will then trigger a page fault on the next user access, allowing |
| 2063 | * fixup by i915_gem_fault(). |
| 2064 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2065 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2066 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2067 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2068 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2069 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2070 | /* Serialisation between user GTT access and our code depends upon |
| 2071 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2072 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2073 | * |
| 2074 | * Note that RPM complicates somewhat by adding an additional |
| 2075 | * requirement that operations to the GGTT be made holding the RPM |
| 2076 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2077 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2078 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2079 | intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2080 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2081 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2082 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2083 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2084 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2085 | |
| 2086 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2087 | * memory transactions from userspace before we return. The TLB |
| 2088 | * flushing implied above by changing the PTE above *should* be |
| 2089 | * sufficient, an extra barrier here just provides us with a bit |
| 2090 | * of paranoid documentation about our requirement to serialise |
| 2091 | * memory writes before touching registers / GSM. |
| 2092 | */ |
| 2093 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2094 | |
| 2095 | out: |
| 2096 | intel_runtime_pm_put(i915); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2097 | } |
| 2098 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2099 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2100 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2101 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2102 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2103 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2104 | /* |
| 2105 | * Only called during RPM suspend. All users of the userfault_list |
| 2106 | * must be holding an RPM wakeref to ensure that this can not |
| 2107 | * run concurrently with themselves (and use the struct_mutex for |
| 2108 | * protection between themselves). |
| 2109 | */ |
| 2110 | |
| 2111 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2112 | &dev_priv->mm.userfault_list, userfault_link) |
| 2113 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2114 | |
| 2115 | /* The fence will be lost when the device powers down. If any were |
| 2116 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2117 | * down! All other fences will be reacquired by the user upon waking. |
| 2118 | */ |
| 2119 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2120 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2121 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2122 | /* Ideally we want to assert that the fence register is not |
| 2123 | * live at this point (i.e. that no piece of code will be |
| 2124 | * trying to write through fence + GTT, as that both violates |
| 2125 | * our tracking of activity and associated locking/barriers, |
| 2126 | * but also is illegal given that the hw is powered down). |
| 2127 | * |
| 2128 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2129 | * That is not sufficient, and we need a more fine-grained |
| 2130 | * tool if we want to have a sanity check here. |
| 2131 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2132 | |
| 2133 | if (!reg->vma) |
| 2134 | continue; |
| 2135 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2136 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2137 | reg->dirty = true; |
| 2138 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2139 | } |
| 2140 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2141 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2142 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2143 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2144 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2145 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2146 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2147 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2148 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2149 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2150 | /* Attempt to reap some mmap space from dead objects */ |
| 2151 | do { |
| 2152 | err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE); |
| 2153 | if (err) |
| 2154 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2155 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2156 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2157 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2158 | if (!err) |
| 2159 | break; |
| 2160 | |
| 2161 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2162 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2163 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2167 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2168 | drm_gem_free_mmap_offset(&obj->base); |
| 2169 | } |
| 2170 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2171 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2172 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2173 | struct drm_device *dev, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2174 | uint32_t handle, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2175 | uint64_t *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2176 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2177 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2178 | int ret; |
| 2179 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2180 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2181 | if (!obj) |
| 2182 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2183 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2184 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2185 | if (ret == 0) |
| 2186 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2187 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2188 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2189 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2190 | } |
| 2191 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2192 | /** |
| 2193 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2194 | * @dev: DRM device |
| 2195 | * @data: GTT mapping ioctl data |
| 2196 | * @file: GEM object info |
| 2197 | * |
| 2198 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2199 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2200 | * up so we can get faults in the handler above. |
| 2201 | * |
| 2202 | * The fault handler will take care of binding the object into the GTT |
| 2203 | * (since it may have been evicted to make room for something), allocating |
| 2204 | * a fence register, and mapping the appropriate aperture address into |
| 2205 | * userspace. |
| 2206 | */ |
| 2207 | int |
| 2208 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2209 | struct drm_file *file) |
| 2210 | { |
| 2211 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2212 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2213 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2214 | } |
| 2215 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2216 | /* Immediately discard the backing storage */ |
| 2217 | static void |
| 2218 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2219 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2220 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2221 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2222 | if (obj->base.filp == NULL) |
| 2223 | return; |
| 2224 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2225 | /* Our goal here is to return as much of the memory as |
| 2226 | * is possible back to the system as we are called from OOM. |
| 2227 | * To do this we must instruct the shmfs to drop all of its |
| 2228 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2229 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2230 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2231 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2232 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2233 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2234 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2235 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2236 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2237 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2238 | struct address_space *mapping; |
| 2239 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2240 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2241 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2242 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2243 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2244 | case I915_MADV_DONTNEED: |
| 2245 | i915_gem_object_truncate(obj); |
| 2246 | case __I915_MADV_PURGED: |
| 2247 | return; |
| 2248 | } |
| 2249 | |
| 2250 | if (obj->base.filp == NULL) |
| 2251 | return; |
| 2252 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2253 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2254 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2257 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2258 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2259 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2260 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2261 | struct sgt_iter sgt_iter; |
| 2262 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2263 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2264 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2265 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2266 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2267 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2268 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2269 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2270 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2271 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2272 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2273 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2274 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2275 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2276 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2277 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2278 | put_page(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2279 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2280 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2281 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2282 | sg_free_table(pages); |
| 2283 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2284 | } |
| 2285 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2286 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2287 | { |
| 2288 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2289 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2290 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2291 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2292 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2293 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2294 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2295 | } |
| 2296 | |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2297 | void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2298 | enum i915_mm_subclass subclass) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2299 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2300 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2301 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2302 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2303 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2304 | return; |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 2305 | |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 2306 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2307 | if (!i915_gem_object_has_pages(obj)) |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2308 | return; |
| 2309 | |
| 2310 | /* May be called by shrinker from within get_pages() (on another bo) */ |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2311 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2312 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) |
| 2313 | goto unlock; |
Ben Widawsky | 3e12302 | 2013-07-31 17:00:04 -0700 | [diff] [blame] | 2314 | |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2315 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
| 2316 | * array, hence protect them from being reaped by removing them from gtt |
| 2317 | * lists early. */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2318 | pages = fetch_and_zero(&obj->mm.pages); |
| 2319 | GEM_BUG_ON(!pages); |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2320 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2321 | spin_lock(&i915->mm.obj_lock); |
| 2322 | list_del(&obj->mm.link); |
| 2323 | spin_unlock(&i915->mm.obj_lock); |
| 2324 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2325 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2326 | void *ptr; |
| 2327 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2328 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2329 | if (is_vmalloc_addr(ptr)) |
| 2330 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2331 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2332 | kunmap(kmap_to_page(ptr)); |
| 2333 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2334 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2335 | } |
| 2336 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2337 | __i915_gem_object_reset_page_iter(obj); |
| 2338 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2339 | if (!IS_ERR(pages)) |
| 2340 | obj->ops->put_pages(obj, pages); |
| 2341 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2342 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
| 2343 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2344 | unlock: |
| 2345 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2346 | } |
| 2347 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2348 | static bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2349 | { |
| 2350 | struct sg_table new_st; |
| 2351 | struct scatterlist *sg, *new_sg; |
| 2352 | unsigned int i; |
| 2353 | |
| 2354 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2355 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2356 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2357 | 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] | 2358 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2359 | |
| 2360 | new_sg = new_st.sgl; |
| 2361 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2362 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
| 2363 | /* called before being DMA mapped, no need to copy sg->dma_* */ |
| 2364 | new_sg = sg_next(new_sg); |
| 2365 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2366 | 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] | 2367 | |
| 2368 | sg_free_table(orig_st); |
| 2369 | |
| 2370 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2371 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2374 | 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] | 2375 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2376 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2377 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2378 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2379 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2380 | struct sg_table *st; |
| 2381 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2382 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2383 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2384 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2385 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2386 | unsigned int sg_page_sizes; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2387 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2388 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2389 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2390 | /* Assert that the object is not currently in any GPU domain. As it |
| 2391 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2392 | * a GPU cache |
| 2393 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2394 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2395 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2396 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2397 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2398 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2399 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2400 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2401 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2402 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2403 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2404 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | /* Get the list of pages out of our struct file. They'll be pinned |
| 2408 | * at this point until we release them. |
| 2409 | * |
| 2410 | * Fail silently without starting the shrinker |
| 2411 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2412 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2413 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2414 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2415 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2416 | sg = st->sgl; |
| 2417 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2418 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2419 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2420 | const unsigned int shrink[] = { |
| 2421 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2422 | 0, |
| 2423 | }, *s = shrink; |
| 2424 | gfp_t gfp = noreclaim; |
| 2425 | |
| 2426 | do { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2427 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2428 | if (likely(!IS_ERR(page))) |
| 2429 | break; |
| 2430 | |
| 2431 | if (!*s) { |
| 2432 | ret = PTR_ERR(page); |
| 2433 | goto err_sg; |
| 2434 | } |
| 2435 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2436 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2437 | cond_resched(); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2438 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2439 | /* We've tried hard to allocate the memory by reaping |
| 2440 | * our own buffer, now let the real VM do its job and |
| 2441 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2442 | * |
| 2443 | * However, since graphics tend to be disposable, |
| 2444 | * defer the oom here by reporting the ENOMEM back |
| 2445 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2446 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2447 | if (!*s) { |
| 2448 | /* reclaim and warn, but no oom */ |
| 2449 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2450 | |
| 2451 | /* Our bo are always dirty and so we require |
| 2452 | * kswapd to reclaim our pages (direct reclaim |
| 2453 | * does not effectively begin pageout of our |
| 2454 | * buffers on its own). However, direct reclaim |
| 2455 | * only waits for kswapd when under allocation |
| 2456 | * congestion. So as a result __GFP_RECLAIM is |
| 2457 | * unreliable and fails to actually reclaim our |
| 2458 | * dirty pages -- unless you try over and over |
| 2459 | * again with !__GFP_NORETRY. However, we still |
| 2460 | * want to fail this allocation rather than |
| 2461 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2462 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2463 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2464 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2465 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2466 | } while (1); |
| 2467 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2468 | if (!i || |
| 2469 | sg->length >= max_segment || |
| 2470 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2471 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2472 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2473 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2474 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2475 | st->nents++; |
| 2476 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2477 | } else { |
| 2478 | sg->length += PAGE_SIZE; |
| 2479 | } |
| 2480 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2481 | |
| 2482 | /* Check that the i965g/gm workaround works. */ |
| 2483 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2484 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2485 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2486 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2487 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2488 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2489 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2490 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2491 | i915_sg_trim(st); |
| 2492 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2493 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2494 | if (ret) { |
| 2495 | /* DMA remapping failed? One possible cause is that |
| 2496 | * it could not reserve enough large entries, asking |
| 2497 | * for PAGE_SIZE chunks instead may be helpful. |
| 2498 | */ |
| 2499 | if (max_segment > PAGE_SIZE) { |
| 2500 | for_each_sgt_page(page, sgt_iter, st) |
| 2501 | put_page(page); |
| 2502 | sg_free_table(st); |
| 2503 | |
| 2504 | max_segment = PAGE_SIZE; |
| 2505 | goto rebuild_st; |
| 2506 | } else { |
| 2507 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2508 | "Failed to DMA remap %lu pages\n", |
| 2509 | page_count); |
| 2510 | goto err_pages; |
| 2511 | } |
| 2512 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2513 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2514 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2515 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2516 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2517 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2518 | |
| 2519 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2520 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2521 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2522 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2523 | err_pages: |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2524 | for_each_sgt_page(page, sgt_iter, st) |
| 2525 | put_page(page); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2526 | sg_free_table(st); |
| 2527 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2528 | |
| 2529 | /* shmemfs first checks if there is enough memory to allocate the page |
| 2530 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2531 | * ENOMEM for a genuine allocation failure. |
| 2532 | * |
| 2533 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2534 | * space and so want to translate the error from shmemfs back to our |
| 2535 | * usual understanding of ENOMEM. |
| 2536 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2537 | if (ret == -ENOSPC) |
| 2538 | ret = -ENOMEM; |
| 2539 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2540 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2544 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2545 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2546 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2547 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2548 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2549 | int i; |
| 2550 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2551 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2552 | |
| 2553 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2554 | obj->mm.get_page.sg_idx = 0; |
| 2555 | |
| 2556 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2557 | |
| 2558 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2559 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2560 | GEM_BUG_ON(obj->mm.quirked); |
| 2561 | __i915_gem_object_pin_pages(obj); |
| 2562 | obj->mm.quirked = true; |
| 2563 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2564 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2565 | GEM_BUG_ON(!sg_page_sizes); |
| 2566 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2567 | |
| 2568 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2569 | * Calculate the supported page-sizes which fit into the given |
| 2570 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2571 | * to use opportunistically when later inserting into the GTT. For |
| 2572 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2573 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2574 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2575 | */ |
| 2576 | obj->mm.page_sizes.sg = 0; |
| 2577 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2578 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2579 | obj->mm.page_sizes.sg |= BIT(i); |
| 2580 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2581 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2582 | |
| 2583 | spin_lock(&i915->mm.obj_lock); |
| 2584 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2585 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2589 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2590 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2591 | |
| 2592 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2593 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2594 | return -EFAULT; |
| 2595 | } |
| 2596 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2597 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2598 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2599 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2600 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2601 | } |
| 2602 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2603 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2604 | * 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] | 2605 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2606 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2607 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2608 | * or as the object is itself released. |
| 2609 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2610 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2611 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2612 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2613 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2614 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2615 | if (err) |
| 2616 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2617 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2618 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2619 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2620 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2621 | err = ____i915_gem_object_get_pages(obj); |
| 2622 | if (err) |
| 2623 | goto unlock; |
| 2624 | |
| 2625 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2626 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2627 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2628 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2629 | unlock: |
| 2630 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2631 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2632 | } |
| 2633 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2634 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2635 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2636 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2637 | { |
| 2638 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2639 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2640 | struct sgt_iter sgt_iter; |
| 2641 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2642 | struct page *stack_pages[32]; |
| 2643 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2644 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2645 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2646 | void *addr; |
| 2647 | |
| 2648 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2649 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2650 | return kmap(sg_page(sgt->sgl)); |
| 2651 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2652 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2653 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2654 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2655 | if (!pages) |
| 2656 | return NULL; |
| 2657 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2658 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2659 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2660 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2661 | |
| 2662 | /* Check that we have the expected number of pages */ |
| 2663 | GEM_BUG_ON(i != n_pages); |
| 2664 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2665 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2666 | default: |
| 2667 | MISSING_CASE(type); |
| 2668 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2669 | case I915_MAP_WB: |
| 2670 | pgprot = PAGE_KERNEL; |
| 2671 | break; |
| 2672 | case I915_MAP_WC: |
| 2673 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2674 | break; |
| 2675 | } |
| 2676 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2677 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2678 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2679 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2680 | |
| 2681 | return addr; |
| 2682 | } |
| 2683 | |
| 2684 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2685 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2686 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2687 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2688 | enum i915_map_type has_type; |
| 2689 | bool pinned; |
| 2690 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2691 | int ret; |
| 2692 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2693 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2694 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2695 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2696 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2697 | if (ret) |
| 2698 | return ERR_PTR(ret); |
| 2699 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2700 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2701 | type &= ~I915_MAP_OVERRIDE; |
| 2702 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2703 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2704 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2705 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2706 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2707 | ret = ____i915_gem_object_get_pages(obj); |
| 2708 | if (ret) |
| 2709 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2710 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2711 | smp_mb__before_atomic(); |
| 2712 | } |
| 2713 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2714 | pinned = false; |
| 2715 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2716 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2717 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2718 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2719 | if (ptr && has_type != type) { |
| 2720 | if (pinned) { |
| 2721 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2722 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2723 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2724 | |
| 2725 | if (is_vmalloc_addr(ptr)) |
| 2726 | vunmap(ptr); |
| 2727 | else |
| 2728 | kunmap(kmap_to_page(ptr)); |
| 2729 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2730 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2731 | } |
| 2732 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2733 | if (!ptr) { |
| 2734 | ptr = i915_gem_object_map(obj, type); |
| 2735 | if (!ptr) { |
| 2736 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2737 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2738 | } |
| 2739 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2740 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2741 | } |
| 2742 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2743 | out_unlock: |
| 2744 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2745 | return ptr; |
| 2746 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2747 | err_unpin: |
| 2748 | atomic_dec(&obj->mm.pages_pin_count); |
| 2749 | err_unlock: |
| 2750 | ptr = ERR_PTR(ret); |
| 2751 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2752 | } |
| 2753 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2754 | static int |
| 2755 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2756 | const struct drm_i915_gem_pwrite *arg) |
| 2757 | { |
| 2758 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2759 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2760 | u64 remain, offset; |
| 2761 | unsigned int pg; |
| 2762 | |
| 2763 | /* Before we instantiate/pin the backing store for our use, we |
| 2764 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2765 | * the pagecache. We avoid the penalty of instantiating all the |
| 2766 | * pages, important if the user is just writing to a few and never |
| 2767 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2768 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2769 | * or clearing-before-use) before it is overwritten. |
| 2770 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2771 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2772 | return -ENODEV; |
| 2773 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2774 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2775 | return -EFAULT; |
| 2776 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2777 | /* Before the pages are instantiated the object is treated as being |
| 2778 | * in the CPU domain. The pages will be clflushed as required before |
| 2779 | * use, and we can freely write into the pages directly. If userspace |
| 2780 | * races pwrite with any other operation; corruption will ensue - |
| 2781 | * that is userspace's prerogative! |
| 2782 | */ |
| 2783 | |
| 2784 | remain = arg->size; |
| 2785 | offset = arg->offset; |
| 2786 | pg = offset_in_page(offset); |
| 2787 | |
| 2788 | do { |
| 2789 | unsigned int len, unwritten; |
| 2790 | struct page *page; |
| 2791 | void *data, *vaddr; |
| 2792 | int err; |
| 2793 | |
| 2794 | len = PAGE_SIZE - pg; |
| 2795 | if (len > remain) |
| 2796 | len = remain; |
| 2797 | |
| 2798 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2799 | offset, len, 0, |
| 2800 | &page, &data); |
| 2801 | if (err < 0) |
| 2802 | return err; |
| 2803 | |
| 2804 | vaddr = kmap(page); |
| 2805 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2806 | kunmap(page); |
| 2807 | |
| 2808 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2809 | offset, len, len - unwritten, |
| 2810 | page, data); |
| 2811 | if (err < 0) |
| 2812 | return err; |
| 2813 | |
| 2814 | if (unwritten) |
| 2815 | return -EFAULT; |
| 2816 | |
| 2817 | remain -= len; |
| 2818 | user_data += len; |
| 2819 | offset += len; |
| 2820 | pg = 0; |
| 2821 | } while (remain); |
| 2822 | |
| 2823 | return 0; |
| 2824 | } |
| 2825 | |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2826 | static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2827 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2828 | bool banned; |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2829 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2830 | atomic_inc(&ctx->guilty_count); |
| 2831 | |
Chris Wilson | 24eae08 | 2018-02-05 09:22:01 +0000 | [diff] [blame] | 2832 | banned = false; |
| 2833 | if (i915_gem_context_is_bannable(ctx)) { |
| 2834 | unsigned int score; |
| 2835 | |
| 2836 | score = atomic_add_return(CONTEXT_SCORE_GUILTY, |
| 2837 | &ctx->ban_score); |
| 2838 | banned = score >= CONTEXT_SCORE_BAN_THRESHOLD; |
| 2839 | |
| 2840 | DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n", |
| 2841 | ctx->name, score, yesno(banned)); |
| 2842 | } |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2843 | if (!banned) |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2844 | return; |
| 2845 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2846 | i915_gem_context_set_banned(ctx); |
| 2847 | if (!IS_ERR_OR_NULL(ctx->file_priv)) { |
| 2848 | atomic_inc(&ctx->file_priv->context_bans); |
| 2849 | DRM_DEBUG_DRIVER("client %s has had %d context banned\n", |
| 2850 | ctx->name, atomic_read(&ctx->file_priv->context_bans)); |
| 2851 | } |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) |
| 2855 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2856 | atomic_inc(&ctx->active_count); |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2857 | } |
| 2858 | |
Chris Wilson | 8d9fc7f | 2014-02-25 17:11:23 +0200 | [diff] [blame] | 2859 | struct drm_i915_gem_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2860 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2861 | { |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2862 | struct drm_i915_gem_request *request, *active = NULL; |
| 2863 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2864 | |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2865 | /* We are called by the error capture and reset at a random |
| 2866 | * point in time. In particular, note that neither is crucially |
| 2867 | * ordered with an interrupt. After a hang, the GPU is dead and we |
| 2868 | * assume that no more writes can happen (we waited long enough for |
| 2869 | * all writes that were in transaction to be flushed) - adding an |
| 2870 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2871 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
| 2872 | */ |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2873 | spin_lock_irqsave(&engine->timeline->lock, flags); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 2874 | list_for_each_entry(request, &engine->timeline->requests, link) { |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2875 | if (__i915_gem_request_completed(request, |
| 2876 | request->global_seqno)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2877 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2878 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2879 | GEM_BUG_ON(request->engine != engine); |
Chris Wilson | c00122f3 | 2017-02-12 17:19:58 +0000 | [diff] [blame] | 2880 | GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 2881 | &request->fence.flags)); |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2882 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2883 | active = request; |
| 2884 | break; |
| 2885 | } |
| 2886 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 2887 | |
| 2888 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2889 | } |
| 2890 | |
Mika Kuoppala | bf2f043 | 2017-01-17 17:59:04 +0200 | [diff] [blame] | 2891 | static bool engine_stalled(struct intel_engine_cs *engine) |
| 2892 | { |
| 2893 | if (!engine->hangcheck.stalled) |
| 2894 | return false; |
| 2895 | |
| 2896 | /* Check for possible seqno movement after hang declaration */ |
| 2897 | if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) { |
| 2898 | DRM_DEBUG_DRIVER("%s pardoned\n", engine->name); |
| 2899 | return false; |
| 2900 | } |
| 2901 | |
| 2902 | return true; |
| 2903 | } |
| 2904 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2905 | /* |
| 2906 | * Ensure irq handler finishes, and not run again. |
| 2907 | * Also return the active request so that we only search for it once. |
| 2908 | */ |
| 2909 | struct drm_i915_gem_request * |
| 2910 | i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) |
| 2911 | { |
| 2912 | struct drm_i915_gem_request *request = NULL; |
| 2913 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 2914 | /* |
| 2915 | * During the reset sequence, we must prevent the engine from |
| 2916 | * entering RC6. As the context state is undefined until we restart |
| 2917 | * the engine, if it does enter RC6 during the reset, the state |
| 2918 | * written to the powercontext is undefined and so we may lose |
| 2919 | * GPU state upon resume, i.e. fail to restart after a reset. |
| 2920 | */ |
| 2921 | intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); |
| 2922 | |
| 2923 | /* |
| 2924 | * Prevent the signaler thread from updating the request |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2925 | * state (by calling dma_fence_signal) as we are processing |
| 2926 | * the reset. The write from the GPU of the seqno is |
| 2927 | * asynchronous and the signaler thread may see a different |
| 2928 | * value to us and declare the request complete, even though |
| 2929 | * the reset routine have picked that request as the active |
| 2930 | * (incomplete) request. This conflict is not handled |
| 2931 | * gracefully! |
| 2932 | */ |
| 2933 | kthread_park(engine->breadcrumbs.signaler); |
| 2934 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 2935 | /* |
| 2936 | * Prevent request submission to the hardware until we have |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2937 | * completed the reset in i915_gem_reset_finish(). If a request |
| 2938 | * is completed by one engine, it may then queue a request |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2939 | * to a second via its execlists->tasklet *just* as we are |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2940 | * calling engine->init_hw() and also writing the ELSP. |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2941 | * Turning off the execlists->tasklet until the reset is over |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2942 | * prevents the race. |
| 2943 | */ |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2944 | tasklet_kill(&engine->execlists.tasklet); |
| 2945 | tasklet_disable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2946 | |
Michał Winiarski | c41937f | 2017-10-26 15:35:58 +0200 | [diff] [blame] | 2947 | /* |
| 2948 | * We're using worker to queue preemption requests from the tasklet in |
| 2949 | * GuC submission mode. |
| 2950 | * Even though tasklet was disabled, we may still have a worker queued. |
| 2951 | * Let's make sure that all workers scheduled before disabling the |
| 2952 | * tasklet are completed before continuing with the reset. |
| 2953 | */ |
| 2954 | if (engine->i915->guc.preempt_wq) |
| 2955 | flush_workqueue(engine->i915->guc.preempt_wq); |
| 2956 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2957 | if (engine->irq_seqno_barrier) |
| 2958 | engine->irq_seqno_barrier(engine); |
| 2959 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 2960 | request = i915_gem_find_active_request(engine); |
| 2961 | if (request && request->fence.error == -EIO) |
| 2962 | request = ERR_PTR(-EIO); /* Previous reset failed! */ |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2963 | |
| 2964 | return request; |
| 2965 | } |
| 2966 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2967 | int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2968 | { |
| 2969 | struct intel_engine_cs *engine; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2970 | struct drm_i915_gem_request *request; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2971 | enum intel_engine_id id; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2972 | int err = 0; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2973 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2974 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2975 | request = i915_gem_reset_prepare_engine(engine); |
| 2976 | if (IS_ERR(request)) { |
| 2977 | err = PTR_ERR(request); |
| 2978 | continue; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2979 | } |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 2980 | |
| 2981 | engine->hangcheck.active_request = request; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2982 | } |
| 2983 | |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2984 | i915_gem_revoke_fences(dev_priv); |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2985 | |
| 2986 | return err; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2987 | } |
| 2988 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2989 | static void skip_request(struct drm_i915_gem_request *request) |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2990 | { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 2991 | void *vaddr = request->ring->vaddr; |
| 2992 | u32 head; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2993 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 2994 | /* As this request likely depends on state from the lost |
| 2995 | * context, clear out all the user operations leaving the |
| 2996 | * breadcrumb at the end (so we get the fence notifications). |
| 2997 | */ |
| 2998 | head = request->head; |
| 2999 | if (request->postfix < head) { |
| 3000 | memset(vaddr + head, 0, request->ring->size - head); |
| 3001 | head = 0; |
| 3002 | } |
| 3003 | memset(vaddr + head, 0, request->postfix - head); |
Chris Wilson | c0d5f32 | 2017-01-10 17:22:43 +0000 | [diff] [blame] | 3004 | |
| 3005 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3008 | static void engine_skip_context(struct drm_i915_gem_request *request) |
| 3009 | { |
| 3010 | struct intel_engine_cs *engine = request->engine; |
| 3011 | struct i915_gem_context *hung_ctx = request->ctx; |
| 3012 | struct intel_timeline *timeline; |
| 3013 | unsigned long flags; |
| 3014 | |
| 3015 | timeline = i915_gem_context_lookup_timeline(hung_ctx, engine); |
| 3016 | |
| 3017 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3018 | spin_lock(&timeline->lock); |
| 3019 | |
| 3020 | list_for_each_entry_continue(request, &engine->timeline->requests, link) |
| 3021 | if (request->ctx == hung_ctx) |
| 3022 | skip_request(request); |
| 3023 | |
| 3024 | list_for_each_entry(request, &timeline->requests, link) |
| 3025 | skip_request(request); |
| 3026 | |
| 3027 | spin_unlock(&timeline->lock); |
| 3028 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 3029 | } |
| 3030 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3031 | /* Returns the request if it was guilty of the hang */ |
| 3032 | static struct drm_i915_gem_request * |
| 3033 | i915_gem_reset_request(struct intel_engine_cs *engine, |
| 3034 | struct drm_i915_gem_request *request) |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3035 | { |
Mika Kuoppala | 71895a0 | 2017-01-17 17:59:07 +0200 | [diff] [blame] | 3036 | /* The guilty request will get skipped on a hung engine. |
| 3037 | * |
| 3038 | * Users of client default contexts do not rely on logical |
| 3039 | * state preserved between batches so it is safe to execute |
| 3040 | * queued requests following the hang. Non default contexts |
| 3041 | * rely on preserved state, so skipping a batch loses the |
| 3042 | * evolution of the state and it needs to be considered corrupted. |
| 3043 | * Executing more queued batches on top of corrupted state is |
| 3044 | * risky. But we take the risk by trying to advance through |
| 3045 | * the queued requests in order to make the client behaviour |
| 3046 | * more predictable around resets, by not throwing away random |
| 3047 | * amount of batches it has prepared for execution. Sophisticated |
| 3048 | * clients can use gem_reset_stats_ioctl and dma fence status |
| 3049 | * (exported via sync_file info ioctl on explicit fences) to observe |
| 3050 | * when it loses the context state and should rebuild accordingly. |
| 3051 | * |
| 3052 | * The context ban, and ultimately the client ban, mechanism are safety |
| 3053 | * valves if client submission ends up resulting in nothing more than |
| 3054 | * subsequent hangs. |
| 3055 | */ |
| 3056 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3057 | if (engine_stalled(engine)) { |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3058 | i915_gem_context_mark_guilty(request->ctx); |
| 3059 | skip_request(request); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3060 | |
| 3061 | /* If this context is now banned, skip all pending requests. */ |
| 3062 | if (i915_gem_context_is_banned(request->ctx)) |
| 3063 | engine_skip_context(request); |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3064 | } else { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3065 | /* |
| 3066 | * Since this is not the hung engine, it may have advanced |
| 3067 | * since the hang declaration. Double check by refinding |
| 3068 | * the active request at the time of the reset. |
| 3069 | */ |
| 3070 | request = i915_gem_find_active_request(engine); |
| 3071 | if (request) { |
| 3072 | i915_gem_context_mark_innocent(request->ctx); |
| 3073 | dma_fence_set_error(&request->fence, -EAGAIN); |
| 3074 | |
| 3075 | /* Rewind the engine to replay the incomplete rq */ |
| 3076 | spin_lock_irq(&engine->timeline->lock); |
| 3077 | request = list_prev_entry(request, link); |
| 3078 | if (&request->link == &engine->timeline->requests) |
| 3079 | request = NULL; |
| 3080 | spin_unlock_irq(&engine->timeline->lock); |
| 3081 | } |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3082 | } |
| 3083 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3084 | return request; |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3085 | } |
| 3086 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3087 | void i915_gem_reset_engine(struct intel_engine_cs *engine, |
| 3088 | struct drm_i915_gem_request *request) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3089 | { |
Chris Wilson | fcb1de5 | 2017-12-19 09:01:10 +0000 | [diff] [blame] | 3090 | /* |
| 3091 | * Make sure this write is visible before we re-enable the interrupt |
| 3092 | * handlers on another CPU, as tasklet_enable() resolves to just |
| 3093 | * a compiler barrier which is insufficient for our purpose here. |
| 3094 | */ |
| 3095 | smp_store_mb(engine->irq_posted, 0); |
Chris Wilson | ed454f2 | 2017-07-21 13:32:29 +0100 | [diff] [blame] | 3096 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3097 | if (request) |
| 3098 | request = i915_gem_reset_request(engine, request); |
| 3099 | |
| 3100 | if (request) { |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3101 | DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n", |
| 3102 | engine->name, request->global_seqno); |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3103 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3104 | |
| 3105 | /* Setup the CS to resume from the breadcrumb of the hung request */ |
| 3106 | engine->reset_hw(engine, request); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3109 | void i915_gem_reset(struct drm_i915_private *dev_priv) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3110 | { |
| 3111 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3112 | enum intel_engine_id id; |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3113 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3114 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 3115 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3116 | i915_gem_retire_requests(dev_priv); |
| 3117 | |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3118 | for_each_engine(engine, dev_priv, id) { |
| 3119 | struct i915_gem_context *ctx; |
| 3120 | |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3121 | i915_gem_reset_engine(engine, engine->hangcheck.active_request); |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3122 | ctx = fetch_and_zero(&engine->last_retired_context); |
| 3123 | if (ctx) |
| 3124 | engine->context_unpin(engine, ctx); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3125 | |
| 3126 | /* |
| 3127 | * Ostensibily, we always want a context loaded for powersaving, |
| 3128 | * so if the engine is idle after the reset, send a request |
| 3129 | * to load our scratch kernel_context. |
| 3130 | * |
| 3131 | * More mysteriously, if we leave the engine idle after a reset, |
| 3132 | * the next userspace batch may hang, with what appears to be |
| 3133 | * an incoherent read by the CS (presumably stale TLB). An |
| 3134 | * empty request appears sufficient to paper over the glitch. |
| 3135 | */ |
Chris Wilson | 01b8fdc | 2018-02-05 15:24:31 +0000 | [diff] [blame] | 3136 | if (intel_engine_is_idle(engine)) { |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3137 | struct drm_i915_gem_request *rq; |
| 3138 | |
| 3139 | rq = i915_gem_request_alloc(engine, |
| 3140 | dev_priv->kernel_context); |
| 3141 | if (!IS_ERR(rq)) |
| 3142 | __i915_add_request(rq, false); |
| 3143 | } |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3144 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3145 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 3146 | i915_gem_restore_fences(dev_priv); |
Chris Wilson | f2a91d1 | 2016-09-21 14:51:06 +0100 | [diff] [blame] | 3147 | |
| 3148 | if (dev_priv->gt.awake) { |
| 3149 | intel_sanitize_gt_powersave(dev_priv); |
| 3150 | intel_enable_gt_powersave(dev_priv); |
| 3151 | if (INTEL_GEN(dev_priv) >= 6) |
| 3152 | gen6_rps_busy(dev_priv); |
| 3153 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3154 | } |
| 3155 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3156 | void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) |
| 3157 | { |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3158 | tasklet_enable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3159 | kthread_unpark(engine->breadcrumbs.signaler); |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3160 | |
| 3161 | intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3162 | } |
| 3163 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3164 | void i915_gem_reset_finish(struct drm_i915_private *dev_priv) |
| 3165 | { |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3166 | struct intel_engine_cs *engine; |
| 3167 | enum intel_engine_id id; |
| 3168 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3169 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3170 | |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3171 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3172 | engine->hangcheck.active_request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3173 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3174 | } |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3177 | static void nop_submit_request(struct drm_i915_gem_request *request) |
| 3178 | { |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3179 | dma_fence_set_error(&request->fence, -EIO); |
| 3180 | |
| 3181 | i915_gem_request_submit(request); |
| 3182 | } |
| 3183 | |
| 3184 | static void nop_complete_submit_request(struct drm_i915_gem_request *request) |
| 3185 | { |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3186 | unsigned long flags; |
| 3187 | |
Chris Wilson | 3cd9442 | 2017-01-10 17:22:45 +0000 | [diff] [blame] | 3188 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3189 | |
| 3190 | spin_lock_irqsave(&request->engine->timeline->lock, flags); |
| 3191 | __i915_gem_request_submit(request); |
Chris Wilson | 3dcf93f7 | 2016-11-22 14:41:20 +0000 | [diff] [blame] | 3192 | intel_engine_init_global_seqno(request->engine, request->global_seqno); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3193 | spin_unlock_irqrestore(&request->engine->timeline->lock, flags); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3194 | } |
| 3195 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3196 | void i915_gem_set_wedged(struct drm_i915_private *i915) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3197 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 3198 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3199 | enum intel_engine_id id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3200 | |
Chris Wilson | 559e040 | 2018-02-05 09:21:59 +0000 | [diff] [blame] | 3201 | if (drm_debug & DRM_UT_DRIVER) { |
| 3202 | struct drm_printer p = drm_debug_printer(__func__); |
| 3203 | |
| 3204 | for_each_engine(engine, i915, id) |
| 3205 | intel_engine_dump(engine, &p, "%s\n", engine->name); |
| 3206 | } |
| 3207 | |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3208 | set_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3209 | smp_mb__after_atomic(); |
| 3210 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3211 | /* |
| 3212 | * First, stop submission to hw, but do not yet complete requests by |
| 3213 | * rolling the global seqno forward (since this would complete requests |
| 3214 | * for which we haven't set the fence error to EIO yet). |
| 3215 | */ |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3216 | for_each_engine(engine, i915, id) |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3217 | engine->submit_request = nop_submit_request; |
| 3218 | |
| 3219 | /* |
| 3220 | * Make sure no one is running the old callback before we proceed with |
| 3221 | * cancelling requests and resetting the completion tracking. Otherwise |
| 3222 | * we might submit a request to the hardware which never completes. |
| 3223 | */ |
| 3224 | synchronize_rcu(); |
| 3225 | |
| 3226 | for_each_engine(engine, i915, id) { |
| 3227 | /* Mark all executing requests as skipped */ |
| 3228 | engine->cancel_requests(engine); |
| 3229 | |
| 3230 | /* |
| 3231 | * Only once we've force-cancelled all in-flight requests can we |
| 3232 | * start to complete all requests. |
| 3233 | */ |
| 3234 | engine->submit_request = nop_complete_submit_request; |
Chris Wilson | 3fed180 | 2018-02-07 21:05:43 +0000 | [diff] [blame] | 3235 | engine->schedule = NULL; |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3236 | } |
| 3237 | |
Chris Wilson | 3fed180 | 2018-02-07 21:05:43 +0000 | [diff] [blame] | 3238 | i915->caps.scheduler = 0; |
| 3239 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3240 | /* |
| 3241 | * Make sure no request can slip through without getting completed by |
| 3242 | * either this call here to intel_engine_init_global_seqno, or the one |
| 3243 | * in nop_complete_submit_request. |
| 3244 | */ |
| 3245 | synchronize_rcu(); |
| 3246 | |
| 3247 | for_each_engine(engine, i915, id) { |
| 3248 | unsigned long flags; |
| 3249 | |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3250 | /* |
| 3251 | * Mark all pending requests as complete so that any concurrent |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3252 | * (lockless) lookup doesn't try and wait upon the request as we |
| 3253 | * reset it. |
| 3254 | */ |
| 3255 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3256 | intel_engine_init_global_seqno(engine, |
| 3257 | intel_engine_last_submit(engine)); |
| 3258 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 3259 | } |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3260 | |
Chris Wilson | 3d7adbb | 2017-07-21 13:32:27 +0100 | [diff] [blame] | 3261 | wake_up_all(&i915->gpu_error.reset_queue); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3262 | } |
| 3263 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3264 | bool i915_gem_unset_wedged(struct drm_i915_private *i915) |
| 3265 | { |
| 3266 | struct i915_gem_timeline *tl; |
| 3267 | int i; |
| 3268 | |
| 3269 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3270 | if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3271 | return true; |
| 3272 | |
| 3273 | /* Before unwedging, make sure that all pending operations |
| 3274 | * are flushed and errored out - we may have requests waiting upon |
| 3275 | * third party fences. We marked all inflight requests as EIO, and |
| 3276 | * every execbuf since returned EIO, for consistency we want all |
| 3277 | * the currently pending requests to also be marked as EIO, which |
| 3278 | * is done inside our nop_submit_request - and so we must wait. |
| 3279 | * |
| 3280 | * No more can be submitted until we reset the wedged bit. |
| 3281 | */ |
| 3282 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3283 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
| 3284 | struct drm_i915_gem_request *rq; |
| 3285 | |
| 3286 | rq = i915_gem_active_peek(&tl->engine[i].last_request, |
| 3287 | &i915->drm.struct_mutex); |
| 3288 | if (!rq) |
| 3289 | continue; |
| 3290 | |
| 3291 | /* We can't use our normal waiter as we want to |
| 3292 | * avoid recursively trying to handle the current |
| 3293 | * reset. The basic dma_fence_default_wait() installs |
| 3294 | * a callback for dma_fence_signal(), which is |
| 3295 | * triggered by our nop handler (indirectly, the |
| 3296 | * callback enables the signaler thread which is |
| 3297 | * woken by the nop_submit_request() advancing the seqno |
| 3298 | * and when the seqno passes the fence, the signaler |
| 3299 | * then signals the fence waking us up). |
| 3300 | */ |
| 3301 | if (dma_fence_default_wait(&rq->fence, true, |
| 3302 | MAX_SCHEDULE_TIMEOUT) < 0) |
| 3303 | return false; |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | /* Undo nop_submit_request. We prevent all new i915 requests from |
| 3308 | * being queued (by disallowing execbuf whilst wedged) so having |
| 3309 | * waited for all active requests above, we know the system is idle |
| 3310 | * and do not have to worry about a thread being inside |
| 3311 | * engine->submit_request() as we swap over. So unlike installing |
| 3312 | * the nop_submit_request on reset, we can do this from normal |
| 3313 | * context and do not require stop_machine(). |
| 3314 | */ |
| 3315 | intel_engines_reset_default_submission(i915); |
Chris Wilson | 36703e7 | 2017-06-22 11:56:25 +0100 | [diff] [blame] | 3316 | i915_gem_contexts_lost(i915); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3317 | |
| 3318 | smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ |
| 3319 | clear_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3320 | |
| 3321 | return true; |
| 3322 | } |
| 3323 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 3324 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3325 | i915_gem_retire_work_handler(struct work_struct *work) |
| 3326 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3327 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3328 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 3329 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3330 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3331 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3332 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3333 | i915_gem_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3334 | mutex_unlock(&dev->struct_mutex); |
| 3335 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3336 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3337 | /* |
| 3338 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3339 | * We do not need to do this test under locking as in the worst-case |
| 3340 | * we queue the retire worker once too often. |
| 3341 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3342 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3343 | queue_delayed_work(dev_priv->wq, |
| 3344 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 3345 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3346 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3347 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3348 | static void shrink_caches(struct drm_i915_private *i915) |
| 3349 | { |
| 3350 | /* |
| 3351 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 3352 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 3353 | * with the aim of reducing fragmentation. |
| 3354 | */ |
| 3355 | kmem_cache_shrink(i915->priorities); |
| 3356 | kmem_cache_shrink(i915->dependencies); |
| 3357 | kmem_cache_shrink(i915->requests); |
| 3358 | kmem_cache_shrink(i915->luts); |
| 3359 | kmem_cache_shrink(i915->vmas); |
| 3360 | kmem_cache_shrink(i915->objects); |
| 3361 | } |
| 3362 | |
| 3363 | struct sleep_rcu_work { |
| 3364 | union { |
| 3365 | struct rcu_head rcu; |
| 3366 | struct work_struct work; |
| 3367 | }; |
| 3368 | struct drm_i915_private *i915; |
| 3369 | unsigned int epoch; |
| 3370 | }; |
| 3371 | |
| 3372 | static inline bool |
| 3373 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 3374 | { |
| 3375 | /* |
| 3376 | * There is a small chance that the epoch wrapped since we started |
| 3377 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 3378 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 3379 | */ |
| 3380 | return epoch == READ_ONCE(i915->gt.epoch); |
| 3381 | } |
| 3382 | |
| 3383 | static void __sleep_work(struct work_struct *work) |
| 3384 | { |
| 3385 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 3386 | struct drm_i915_private *i915 = s->i915; |
| 3387 | unsigned int epoch = s->epoch; |
| 3388 | |
| 3389 | kfree(s); |
| 3390 | if (same_epoch(i915, epoch)) |
| 3391 | shrink_caches(i915); |
| 3392 | } |
| 3393 | |
| 3394 | static void __sleep_rcu(struct rcu_head *rcu) |
| 3395 | { |
| 3396 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 3397 | struct drm_i915_private *i915 = s->i915; |
| 3398 | |
| 3399 | if (same_epoch(i915, s->epoch)) { |
| 3400 | INIT_WORK(&s->work, __sleep_work); |
| 3401 | queue_work(i915->wq, &s->work); |
| 3402 | } else { |
| 3403 | kfree(s); |
| 3404 | } |
| 3405 | } |
| 3406 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3407 | static inline bool |
| 3408 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 3409 | { |
| 3410 | return (READ_ONCE(i915->gt.active_requests) || |
| 3411 | work_pending(&i915->gt.idle_work.work)); |
| 3412 | } |
| 3413 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3414 | static void |
| 3415 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3416 | { |
| 3417 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3418 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3419 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3420 | bool rearm_hangcheck; |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3421 | ktime_t end; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3422 | |
| 3423 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3424 | return; |
| 3425 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3426 | /* |
| 3427 | * Wait for last execlists context complete, but bail out in case a |
| 3428 | * new request is submitted. |
| 3429 | */ |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3430 | end = ktime_add_ms(ktime_get(), I915_IDLE_ENGINES_TIMEOUT); |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3431 | do { |
| 3432 | if (new_requests_since_last_retire(dev_priv)) |
| 3433 | return; |
| 3434 | |
| 3435 | if (intel_engines_are_idle(dev_priv)) |
| 3436 | break; |
| 3437 | |
| 3438 | usleep_range(100, 500); |
| 3439 | } while (ktime_before(ktime_get(), end)); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3440 | |
| 3441 | rearm_hangcheck = |
| 3442 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3443 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3444 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3445 | /* Currently busy, come back later */ |
| 3446 | mod_delayed_work(dev_priv->wq, |
| 3447 | &dev_priv->gt.idle_work, |
| 3448 | msecs_to_jiffies(50)); |
| 3449 | goto out_rearm; |
| 3450 | } |
| 3451 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3452 | /* |
| 3453 | * New request retired after this work handler started, extend active |
| 3454 | * period until next instance of the work. |
| 3455 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3456 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3457 | goto out_unlock; |
| 3458 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3459 | /* |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3460 | * Be paranoid and flush a concurrent interrupt to make sure |
| 3461 | * we don't reactivate any irq tasklets after parking. |
| 3462 | * |
| 3463 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 3464 | * there may still be an in-flight interrupt even though the CSB |
| 3465 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 3466 | * is completed before we continue, but it doesn't prevent the HW from |
| 3467 | * raising a spurious interrupt later. To complete the shield we should |
| 3468 | * coordinate disabling the CS irq with flushing the interrupts. |
| 3469 | */ |
| 3470 | synchronize_irq(dev_priv->drm.irq); |
| 3471 | |
Chris Wilson | aba5e27 | 2017-10-25 15:39:41 +0100 | [diff] [blame] | 3472 | intel_engines_park(dev_priv); |
Chris Wilson | d02a1d8 | 2017-11-27 12:30:54 +0000 | [diff] [blame] | 3473 | i915_gem_timelines_park(dev_priv); |
| 3474 | |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 3475 | i915_pmu_gt_parked(dev_priv); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3476 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3477 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3478 | dev_priv->gt.awake = false; |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3479 | epoch = dev_priv->gt.epoch; |
| 3480 | GEM_BUG_ON(epoch == I915_EPOCH_INVALID); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3481 | rearm_hangcheck = false; |
Daniel Vetter | 30ecad7 | 2015-12-09 09:29:36 +0100 | [diff] [blame] | 3482 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3483 | if (INTEL_GEN(dev_priv) >= 6) |
| 3484 | gen6_rps_idle(dev_priv); |
Tvrtko Ursulin | b687637 | 2017-12-05 13:28:54 +0000 | [diff] [blame] | 3485 | |
| 3486 | intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ); |
| 3487 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3488 | intel_runtime_pm_put(dev_priv); |
| 3489 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3490 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3491 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3492 | out_rearm: |
| 3493 | if (rearm_hangcheck) { |
| 3494 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3495 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3496 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3497 | |
| 3498 | /* |
| 3499 | * When we are idle, it is an opportune time to reap our caches. |
| 3500 | * However, we have many objects that utilise RCU and the ordered |
| 3501 | * i915->wq that this work is executing on. To try and flush any |
| 3502 | * pending frees now we are idle, we first wait for an RCU grace |
| 3503 | * period, and then queue a task (that will run last on the wq) to |
| 3504 | * shrink and re-optimize the caches. |
| 3505 | */ |
| 3506 | if (same_epoch(dev_priv, epoch)) { |
| 3507 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3508 | if (s) { |
| 3509 | s->i915 = dev_priv; |
| 3510 | s->epoch = epoch; |
| 3511 | call_rcu(&s->rcu, __sleep_rcu); |
| 3512 | } |
| 3513 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3516 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3517 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3518 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3519 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3520 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3521 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3522 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3523 | mutex_lock(&i915->drm.struct_mutex); |
| 3524 | |
| 3525 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3526 | struct i915_gem_context *ctx = lut->ctx; |
| 3527 | struct i915_vma *vma; |
| 3528 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3529 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3530 | if (ctx->file_priv != fpriv) |
| 3531 | continue; |
| 3532 | |
| 3533 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3534 | GEM_BUG_ON(vma->obj != obj); |
| 3535 | |
| 3536 | /* We allow the process to have multiple handles to the same |
| 3537 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3538 | */ |
| 3539 | GEM_BUG_ON(!vma->open_count); |
| 3540 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3541 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3542 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3543 | list_del(&lut->obj_link); |
| 3544 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3545 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3546 | kmem_cache_free(i915->luts, lut); |
| 3547 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3548 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3549 | |
| 3550 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3551 | } |
| 3552 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3553 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3554 | { |
| 3555 | if (timeout_ns < 0) |
| 3556 | return MAX_SCHEDULE_TIMEOUT; |
| 3557 | |
| 3558 | if (timeout_ns == 0) |
| 3559 | return 0; |
| 3560 | |
| 3561 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3562 | } |
| 3563 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3564 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3565 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3566 | * @dev: drm device pointer |
| 3567 | * @data: ioctl data blob |
| 3568 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3569 | * |
| 3570 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3571 | * the timeout parameter. |
| 3572 | * -ETIME: object is still busy after timeout |
| 3573 | * -ERESTARTSYS: signal interrupted the wait |
| 3574 | * -ENONENT: object doesn't exist |
| 3575 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3576 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3577 | * -ENOMEM: damn |
| 3578 | * -ENODEV: Internal IRQ fail |
| 3579 | * -E?: The add request failed |
| 3580 | * |
| 3581 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3582 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3583 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3584 | * without holding struct_mutex the object may become re-busied before this |
| 3585 | * function completes. A similar but shorter * race condition exists in the busy |
| 3586 | * ioctl |
| 3587 | */ |
| 3588 | int |
| 3589 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3590 | { |
| 3591 | struct drm_i915_gem_wait *args = data; |
| 3592 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3593 | ktime_t start; |
| 3594 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3595 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3596 | if (args->flags != 0) |
| 3597 | return -EINVAL; |
| 3598 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3599 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3600 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3601 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3602 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3603 | start = ktime_get(); |
| 3604 | |
| 3605 | ret = i915_gem_object_wait(obj, |
| 3606 | I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL, |
| 3607 | to_wait_timeout(args->timeout_ns), |
| 3608 | to_rps_client(file)); |
| 3609 | |
| 3610 | if (args->timeout_ns > 0) { |
| 3611 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3612 | if (args->timeout_ns < 0) |
| 3613 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3614 | |
| 3615 | /* |
| 3616 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3617 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3618 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3619 | * |
| 3620 | * This is a regression from the timespec->ktime conversion. |
| 3621 | */ |
| 3622 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3623 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3624 | |
| 3625 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3626 | if (ret == -ETIME && args->timeout_ns) |
| 3627 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3628 | } |
| 3629 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3630 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3631 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3634 | 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] | 3635 | { |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3636 | int ret, i; |
| 3637 | |
| 3638 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
| 3639 | ret = i915_gem_active_wait(&tl->engine[i].last_request, flags); |
| 3640 | if (ret) |
| 3641 | return ret; |
| 3642 | } |
| 3643 | |
| 3644 | return 0; |
| 3645 | } |
| 3646 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3647 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3648 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3649 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3650 | dev_err(i915->drm.dev, |
| 3651 | "Failed to idle engines, declaring wedged!\n"); |
| 3652 | if (drm_debug & DRM_UT_DRIVER) { |
| 3653 | struct drm_printer p = drm_debug_printer(__func__); |
| 3654 | struct intel_engine_cs *engine; |
| 3655 | enum intel_engine_id id; |
| 3656 | |
| 3657 | for_each_engine(engine, i915, id) |
| 3658 | intel_engine_dump(engine, &p, |
Chris Wilson | 9e519bc | 2018-02-05 10:06:18 +0000 | [diff] [blame] | 3659 | "%s\n", engine->name); |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3662 | i915_gem_set_wedged(i915); |
| 3663 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3669 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags) |
| 3670 | { |
Dave Gordon | b4ac5af | 2016-03-24 11:20:38 +0000 | [diff] [blame] | 3671 | int ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3672 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3673 | /* If the device is asleep, we have no requests outstanding */ |
| 3674 | if (!READ_ONCE(i915->gt.awake)) |
| 3675 | return 0; |
| 3676 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3677 | if (flags & I915_WAIT_LOCKED) { |
| 3678 | struct i915_gem_timeline *tl; |
| 3679 | |
| 3680 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3681 | |
| 3682 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3683 | ret = wait_for_timeline(tl, flags); |
| 3684 | if (ret) |
| 3685 | return ret; |
| 3686 | } |
Chris Wilson | 72022a7 | 2017-03-30 15:50:38 +0100 | [diff] [blame] | 3687 | i915_gem_retire_requests(i915); |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3688 | |
| 3689 | ret = wait_for_engines(i915); |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3690 | } else { |
| 3691 | ret = wait_for_timeline(&i915->gt.global_timeline, flags); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 3692 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3693 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3694 | return ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3695 | } |
| 3696 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3697 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3698 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3699 | /* |
| 3700 | * We manually flush the CPU domain so that we can override and |
| 3701 | * force the flush for the display, and perform it asyncrhonously. |
| 3702 | */ |
| 3703 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3704 | if (obj->cache_dirty) |
| 3705 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3706 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
| 3709 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3710 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3711 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3712 | return; |
| 3713 | |
| 3714 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3715 | __i915_gem_object_flush_for_display(obj); |
| 3716 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3717 | } |
| 3718 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3719 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3720 | * Moves a single object to the WC read, and possibly write domain. |
| 3721 | * @obj: object to act on |
| 3722 | * @write: ask for write access or read only |
| 3723 | * |
| 3724 | * This function returns when the move is complete, including waiting on |
| 3725 | * flushes to occur. |
| 3726 | */ |
| 3727 | int |
| 3728 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3729 | { |
| 3730 | int ret; |
| 3731 | |
| 3732 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3733 | |
| 3734 | ret = i915_gem_object_wait(obj, |
| 3735 | I915_WAIT_INTERRUPTIBLE | |
| 3736 | I915_WAIT_LOCKED | |
| 3737 | (write ? I915_WAIT_ALL : 0), |
| 3738 | MAX_SCHEDULE_TIMEOUT, |
| 3739 | NULL); |
| 3740 | if (ret) |
| 3741 | return ret; |
| 3742 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3743 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3744 | return 0; |
| 3745 | |
| 3746 | /* Flush and acquire obj->pages so that we are coherent through |
| 3747 | * direct access in memory with previous cached writes through |
| 3748 | * shmemfs and that our cache domain tracking remains valid. |
| 3749 | * For example, if the obj->filp was moved to swap without us |
| 3750 | * being notified and releasing the pages, we would mistakenly |
| 3751 | * continue to assume that the obj remained out of the CPU cached |
| 3752 | * domain. |
| 3753 | */ |
| 3754 | ret = i915_gem_object_pin_pages(obj); |
| 3755 | if (ret) |
| 3756 | return ret; |
| 3757 | |
| 3758 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3759 | |
| 3760 | /* Serialise direct access to this object with the barriers for |
| 3761 | * coherent writes from the GPU, by effectively invalidating the |
| 3762 | * WC domain upon first access. |
| 3763 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3764 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3765 | mb(); |
| 3766 | |
| 3767 | /* It should now be out of any other write domains, and we can update |
| 3768 | * the domain values for our changes. |
| 3769 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3770 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3771 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3772 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3773 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3774 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3775 | obj->mm.dirty = true; |
| 3776 | } |
| 3777 | |
| 3778 | i915_gem_object_unpin_pages(obj); |
| 3779 | return 0; |
| 3780 | } |
| 3781 | |
| 3782 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3783 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3784 | * @obj: object to act on |
| 3785 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3786 | * |
| 3787 | * This function returns when the move is complete, including waiting on |
| 3788 | * flushes to occur. |
| 3789 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3790 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3791 | 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] | 3792 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3793 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3794 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3795 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3796 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3797 | ret = i915_gem_object_wait(obj, |
| 3798 | I915_WAIT_INTERRUPTIBLE | |
| 3799 | I915_WAIT_LOCKED | |
| 3800 | (write ? I915_WAIT_ALL : 0), |
| 3801 | MAX_SCHEDULE_TIMEOUT, |
| 3802 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3803 | if (ret) |
| 3804 | return ret; |
| 3805 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3806 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3807 | return 0; |
| 3808 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3809 | /* Flush and acquire obj->pages so that we are coherent through |
| 3810 | * direct access in memory with previous cached writes through |
| 3811 | * shmemfs and that our cache domain tracking remains valid. |
| 3812 | * For example, if the obj->filp was moved to swap without us |
| 3813 | * being notified and releasing the pages, we would mistakenly |
| 3814 | * continue to assume that the obj remained out of the CPU cached |
| 3815 | * domain. |
| 3816 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3817 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3818 | if (ret) |
| 3819 | return ret; |
| 3820 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3821 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3822 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3823 | /* Serialise direct access to this object with the barriers for |
| 3824 | * coherent writes from the GPU, by effectively invalidating the |
| 3825 | * GTT domain upon first access. |
| 3826 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3827 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3828 | mb(); |
| 3829 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3830 | /* It should now be out of any other write domains, and we can update |
| 3831 | * the domain values for our changes. |
| 3832 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3833 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3834 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3835 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3836 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3837 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3838 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3839 | } |
| 3840 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3841 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3842 | return 0; |
| 3843 | } |
| 3844 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3845 | /** |
| 3846 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3847 | * @obj: object to act on |
| 3848 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3849 | * |
| 3850 | * After this function returns, the object will be in the new cache-level |
| 3851 | * across all GTT and the contents of the backing storage will be coherent, |
| 3852 | * with respect to the new cache-level. In order to keep the backing storage |
| 3853 | * coherent for all users, we only allow a single cache level to be set |
| 3854 | * globally on the object and prevent it from being changed whilst the |
| 3855 | * hardware is reading from the object. That is if the object is currently |
| 3856 | * on the scanout it will be set to uncached (or equivalent display |
| 3857 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3858 | * that all direct access to the scanout remains coherent. |
| 3859 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3860 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3861 | enum i915_cache_level cache_level) |
| 3862 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3863 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3864 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3865 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3866 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3867 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3868 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3869 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3870 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3871 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3872 | * be invalid given the new cache-level. This is principally to |
| 3873 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3874 | * reading an invalid PTE on older architectures. |
| 3875 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3876 | restart: |
| 3877 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3878 | if (!drm_mm_node_allocated(&vma->node)) |
| 3879 | continue; |
| 3880 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3881 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3882 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3883 | return -EBUSY; |
| 3884 | } |
| 3885 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3886 | if (!i915_vma_is_closed(vma) && |
| 3887 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3888 | continue; |
| 3889 | |
| 3890 | ret = i915_vma_unbind(vma); |
| 3891 | if (ret) |
| 3892 | return ret; |
| 3893 | |
| 3894 | /* As unbinding may affect other elements in the |
| 3895 | * obj->vma_list (due to side-effects from retiring |
| 3896 | * an active vma), play safe and restart the iterator. |
| 3897 | */ |
| 3898 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3899 | } |
| 3900 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3901 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3902 | * cache-level on the PTE. We could simply unbind them all and |
| 3903 | * rebind with the correct cache-level on next use. However since |
| 3904 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3905 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3906 | * state and so involves less work. |
| 3907 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3908 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3909 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3910 | * If we wait upon the object, we know that all the bound |
| 3911 | * VMA are no longer active. |
| 3912 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3913 | ret = i915_gem_object_wait(obj, |
| 3914 | I915_WAIT_INTERRUPTIBLE | |
| 3915 | I915_WAIT_LOCKED | |
| 3916 | I915_WAIT_ALL, |
| 3917 | MAX_SCHEDULE_TIMEOUT, |
| 3918 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3919 | if (ret) |
| 3920 | return ret; |
| 3921 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3922 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3923 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3924 | /* Access to snoopable pages through the GTT is |
| 3925 | * incoherent and on some machines causes a hard |
| 3926 | * lockup. Relinquish the CPU mmaping to force |
| 3927 | * userspace to refault in the pages and we can |
| 3928 | * then double check if the GTT mapping is still |
| 3929 | * valid for that pointer access. |
| 3930 | */ |
| 3931 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3932 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3933 | /* As we no longer need a fence for GTT access, |
| 3934 | * we can relinquish it now (and so prevent having |
| 3935 | * to steal a fence from someone else on the next |
| 3936 | * fence request). Note GPU activity would have |
| 3937 | * dropped the fence as all snoopable access is |
| 3938 | * supposed to be linear. |
| 3939 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3940 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3941 | ret = i915_vma_put_fence(vma); |
| 3942 | if (ret) |
| 3943 | return ret; |
| 3944 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3945 | } else { |
| 3946 | /* We either have incoherent backing store and |
| 3947 | * so no GTT access or the architecture is fully |
| 3948 | * coherent. In such cases, existing GTT mmaps |
| 3949 | * ignore the cache bit in the PTE and we can |
| 3950 | * rewrite it without confusing the GPU or having |
| 3951 | * to force userspace to fault back in its mmaps. |
| 3952 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3953 | } |
| 3954 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3955 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3956 | if (!drm_mm_node_allocated(&vma->node)) |
| 3957 | continue; |
| 3958 | |
| 3959 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3960 | if (ret) |
| 3961 | return ret; |
| 3962 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3963 | } |
| 3964 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3965 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3966 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3967 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3968 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3969 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3970 | return 0; |
| 3971 | } |
| 3972 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3973 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3974 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3975 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3976 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3977 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3978 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3979 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3980 | rcu_read_lock(); |
| 3981 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3982 | if (!obj) { |
| 3983 | err = -ENOENT; |
| 3984 | goto out; |
| 3985 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3986 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3987 | switch (obj->cache_level) { |
| 3988 | case I915_CACHE_LLC: |
| 3989 | case I915_CACHE_L3_LLC: |
| 3990 | args->caching = I915_CACHING_CACHED; |
| 3991 | break; |
| 3992 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3993 | case I915_CACHE_WT: |
| 3994 | args->caching = I915_CACHING_DISPLAY; |
| 3995 | break; |
| 3996 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3997 | default: |
| 3998 | args->caching = I915_CACHING_NONE; |
| 3999 | break; |
| 4000 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4001 | out: |
| 4002 | rcu_read_unlock(); |
| 4003 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4004 | } |
| 4005 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4006 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 4007 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4008 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4009 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4010 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4011 | struct drm_i915_gem_object *obj; |
| 4012 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4013 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4014 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4015 | switch (args->caching) { |
| 4016 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4017 | level = I915_CACHE_NONE; |
| 4018 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4019 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4020 | /* |
| 4021 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 4022 | * snooped mapping may leave stale data in a corresponding CPU |
| 4023 | * cacheline, whereas normally such cachelines would get |
| 4024 | * invalidated. |
| 4025 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4026 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4027 | return -ENODEV; |
| 4028 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4029 | level = I915_CACHE_LLC; |
| 4030 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4031 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4032 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4033 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4034 | default: |
| 4035 | return -EINVAL; |
| 4036 | } |
| 4037 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4038 | obj = i915_gem_object_lookup(file, args->handle); |
| 4039 | if (!obj) |
| 4040 | return -ENOENT; |
| 4041 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 4042 | /* |
| 4043 | * The caching mode of proxy object is handled by its generator, and |
| 4044 | * not allowed to be changed by userspace. |
| 4045 | */ |
| 4046 | if (i915_gem_object_is_proxy(obj)) { |
| 4047 | ret = -ENXIO; |
| 4048 | goto out; |
| 4049 | } |
| 4050 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4051 | if (obj->cache_level == level) |
| 4052 | goto out; |
| 4053 | |
| 4054 | ret = i915_gem_object_wait(obj, |
| 4055 | I915_WAIT_INTERRUPTIBLE, |
| 4056 | MAX_SCHEDULE_TIMEOUT, |
| 4057 | to_rps_client(file)); |
| 4058 | if (ret) |
| 4059 | goto out; |
| 4060 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 4061 | ret = i915_mutex_lock_interruptible(dev); |
| 4062 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4063 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4064 | |
| 4065 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4066 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4067 | |
| 4068 | out: |
| 4069 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4070 | return ret; |
| 4071 | } |
| 4072 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4073 | /* |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4074 | * Prepare buffer for display plane (scanout, cursors, etc). |
| 4075 | * Can be called from an uninterruptible phase (modesetting) and allows |
| 4076 | * any flushes to be pipelined (for pageflips). |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4077 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4078 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4079 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 4080 | u32 alignment, |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 4081 | const struct i915_ggtt_view *view) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4082 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4083 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4084 | int ret; |
| 4085 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4086 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4087 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4088 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4089 | * display coherency whilst setting up the cache domains. |
| 4090 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4091 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4092 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4093 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 4094 | * a result, we make sure that the pinning that is about to occur is |
| 4095 | * done with uncached PTEs. This is lowest common denominator for all |
| 4096 | * chipsets. |
| 4097 | * |
| 4098 | * However for gen6+, we could do better by using the GFDT bit instead |
| 4099 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 4100 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 4101 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4102 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 4103 | HAS_WT(to_i915(obj->base.dev)) ? |
| 4104 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4105 | if (ret) { |
| 4106 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4107 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4108 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4109 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4110 | /* As the user may map the buffer once pinned in the display plane |
| 4111 | * (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] | 4112 | * always use map_and_fenceable for all scanout buffers. However, |
| 4113 | * it may simply be too big to fit into mappable, in which case |
| 4114 | * put it anyway and hope that userspace can cope (but always first |
| 4115 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4116 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4117 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 47a8e3f | 2017-01-14 00:28:27 +0000 | [diff] [blame] | 4118 | if (!view || view->type == I915_GGTT_VIEW_NORMAL) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4119 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
| 4120 | PIN_MAPPABLE | PIN_NONBLOCK); |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 4121 | if (IS_ERR(vma)) { |
| 4122 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4123 | unsigned int flags; |
| 4124 | |
| 4125 | /* Valleyview is definitely limited to scanning out the first |
| 4126 | * 512MiB. Lets presume this behaviour was inherited from the |
| 4127 | * g4x display engine and that all earlier gen are similarly |
| 4128 | * limited. Testing suggests that it is a little more |
| 4129 | * complicated than this. For example, Cherryview appears quite |
| 4130 | * happy to scanout from anywhere within its global aperture. |
| 4131 | */ |
| 4132 | flags = 0; |
| 4133 | if (HAS_GMCH_DISPLAY(i915)) |
| 4134 | flags = PIN_MAPPABLE; |
| 4135 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
| 4136 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4137 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4138 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4139 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 4140 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 4141 | |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 4142 | /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 4143 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4144 | intel_fb_obj_flush(obj, ORIGIN_DIRTYFB); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 4145 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4146 | /* It should now be out of any other write domains, and we can update |
| 4147 | * the domain values for our changes. |
| 4148 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4149 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4150 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4151 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4152 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4153 | err_unpin_global: |
| 4154 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4155 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4159 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4160 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 4161 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4162 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4163 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 4164 | return; |
| 4165 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4166 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 4167 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 4168 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4169 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 4170 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4171 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4172 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4173 | } |
| 4174 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4175 | /** |
| 4176 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 4177 | * @obj: object to act on |
| 4178 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4179 | * |
| 4180 | * This function returns when the move is complete, including waiting on |
| 4181 | * flushes to occur. |
| 4182 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 4183 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 4184 | 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] | 4185 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4186 | int ret; |
| 4187 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4188 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4189 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4190 | ret = i915_gem_object_wait(obj, |
| 4191 | I915_WAIT_INTERRUPTIBLE | |
| 4192 | I915_WAIT_LOCKED | |
| 4193 | (write ? I915_WAIT_ALL : 0), |
| 4194 | MAX_SCHEDULE_TIMEOUT, |
| 4195 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 4196 | if (ret) |
| 4197 | return ret; |
| 4198 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 4199 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4200 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4201 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4202 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4203 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4204 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | /* It should now be out of any other write domains, and we can update |
| 4208 | * the domain values for our changes. |
| 4209 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4210 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4211 | |
| 4212 | /* If we're writing through the CPU, then the GPU read domains will |
| 4213 | * need to be invalidated at next use. |
| 4214 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4215 | if (write) |
| 4216 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 4217 | |
| 4218 | return 0; |
| 4219 | } |
| 4220 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4221 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 4222 | * emitted over 20 msec ago. |
| 4223 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4224 | * Note that if we were to use the current jiffies each time around the loop, |
| 4225 | * we wouldn't escape the function with any frames outstanding if the time to |
| 4226 | * render a frame was over 20ms. |
| 4227 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4228 | * This should get us reasonable parallelism between CPU and GPU but also |
| 4229 | * relatively low latency when blocking on a particular request to finish. |
| 4230 | */ |
| 4231 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4232 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4233 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4234 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4235 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 4236 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4237 | struct drm_i915_gem_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4238 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4239 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 4240 | /* ABI: return -EIO if already wedged */ |
| 4241 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 4242 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 4243 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4244 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4245 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4246 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 4247 | break; |
| 4248 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4249 | if (target) { |
| 4250 | list_del(&target->client_link); |
| 4251 | target->file_priv = NULL; |
| 4252 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 4253 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4254 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4255 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4256 | if (target) |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 4257 | i915_gem_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4258 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4259 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4260 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4261 | return 0; |
| 4262 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4263 | ret = i915_wait_request(target, |
| 4264 | I915_WAIT_INTERRUPTIBLE, |
| 4265 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 4266 | i915_gem_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4267 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4268 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4269 | } |
| 4270 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4271 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4272 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 4273 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 4274 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 4275 | u64 alignment, |
| 4276 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4277 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4278 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 4279 | struct i915_address_space *vm = &dev_priv->ggtt.base; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4280 | struct i915_vma *vma; |
| 4281 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 4282 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4283 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4284 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4285 | if (!view && flags & PIN_MAPPABLE) { |
| 4286 | /* If the required space is larger than the available |
| 4287 | * aperture, we will not able to find a slot for the |
| 4288 | * object and unbinding the object now will be in |
| 4289 | * vain. Worse, doing so may cause us to ping-pong |
| 4290 | * the object in and out of the Global GTT and |
| 4291 | * waste a lot of cycles under the mutex. |
| 4292 | */ |
| 4293 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 4294 | return ERR_PTR(-E2BIG); |
| 4295 | |
| 4296 | /* If NONBLOCK is set the caller is optimistically |
| 4297 | * trying to cache the full object within the mappable |
| 4298 | * aperture, and *must* have a fallback in place for |
| 4299 | * situations where we cannot bind the object. We |
| 4300 | * can be a little more lax here and use the fallback |
| 4301 | * more often to avoid costly migrations of ourselves |
| 4302 | * and other objects within the aperture. |
| 4303 | * |
| 4304 | * Half-the-aperture is used as a simple heuristic. |
| 4305 | * More interesting would to do search for a free |
| 4306 | * block prior to making the commitment to unbind. |
| 4307 | * That caters for the self-harm case, and with a |
| 4308 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 4309 | * we could try to minimise harm to others. |
| 4310 | */ |
| 4311 | if (flags & PIN_NONBLOCK && |
| 4312 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 4313 | return ERR_PTR(-ENOSPC); |
| 4314 | } |
| 4315 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 4316 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 4317 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4318 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4319 | |
| 4320 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4321 | if (flags & PIN_NONBLOCK) { |
| 4322 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 4323 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4324 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4325 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 4326 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4327 | return ERR_PTR(-ENOSPC); |
| 4328 | } |
| 4329 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4330 | WARN(i915_vma_is_pinned(vma), |
| 4331 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4332 | " offset=%08x, req.alignment=%llx," |
| 4333 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 4334 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4335 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4336 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4337 | ret = i915_vma_unbind(vma); |
| 4338 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4339 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4340 | } |
| 4341 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4342 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 4343 | if (ret) |
| 4344 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4345 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4346 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4347 | } |
| 4348 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4349 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4350 | { |
| 4351 | /* Note that we could alias engines in the execbuf API, but |
| 4352 | * that would be very unwise as it prevents userspace from |
| 4353 | * fine control over engine selection. Ahem. |
| 4354 | * |
| 4355 | * This should be something like EXEC_MAX_ENGINE instead of |
| 4356 | * I915_NUM_ENGINES. |
| 4357 | */ |
| 4358 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 4359 | return 0x10000 << id; |
| 4360 | } |
| 4361 | |
| 4362 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 4363 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 4364 | /* The uABI guarantees an active writer is also amongst the read |
| 4365 | * engines. This would be true if we accessed the activity tracking |
| 4366 | * under the lock, but as we perform the lookup of the object and |
| 4367 | * its activity locklessly we can not guarantee that the last_write |
| 4368 | * being active implies that we have set the same engine flag from |
| 4369 | * last_read - hence we always set both read and write busy for |
| 4370 | * last_write. |
| 4371 | */ |
| 4372 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4373 | } |
| 4374 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4375 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4376 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4377 | unsigned int (*flag)(unsigned int id)) |
| 4378 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4379 | struct drm_i915_gem_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4380 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4381 | /* We have to check the current hw status of the fence as the uABI |
| 4382 | * guarantees forward progress. We could rely on the idle worker |
| 4383 | * to eventually flush us, but to minimise latency just ask the |
| 4384 | * hardware. |
| 4385 | * |
| 4386 | * Note we only report on the status of native fences. |
| 4387 | */ |
| 4388 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4389 | return 0; |
| 4390 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4391 | /* opencode to_request() in order to avoid const warnings */ |
| 4392 | rq = container_of(fence, struct drm_i915_gem_request, fence); |
| 4393 | if (i915_gem_request_completed(rq)) |
| 4394 | return 0; |
| 4395 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4396 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4399 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4400 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4401 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4402 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4403 | } |
| 4404 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4405 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4406 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4407 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4408 | if (!fence) |
| 4409 | return 0; |
| 4410 | |
| 4411 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4412 | } |
| 4413 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4414 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4415 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4416 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4417 | { |
| 4418 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4419 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4420 | struct reservation_object_list *list; |
| 4421 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4422 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4423 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4424 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4425 | rcu_read_lock(); |
| 4426 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4427 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4428 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4429 | |
| 4430 | /* A discrepancy here is that we do not report the status of |
| 4431 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4432 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4433 | * This also means that wait-ioctl may report an object as busy, |
| 4434 | * where busy-ioctl considers it idle. |
| 4435 | * |
| 4436 | * We trade the ability to warn of foreign fences to report on which |
| 4437 | * i915 engines are active for the object. |
| 4438 | * |
| 4439 | * Alternatively, we can trade that extra information on read/write |
| 4440 | * activity with |
| 4441 | * args->busy = |
| 4442 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4443 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4444 | * |
| 4445 | */ |
| 4446 | retry: |
| 4447 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4448 | |
| 4449 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4450 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4451 | |
| 4452 | /* Translate shared fences to READ set of engines */ |
| 4453 | list = rcu_dereference(obj->resv->fence); |
| 4454 | if (list) { |
| 4455 | unsigned int shared_count = list->shared_count, i; |
| 4456 | |
| 4457 | for (i = 0; i < shared_count; ++i) { |
| 4458 | struct dma_fence *fence = |
| 4459 | rcu_dereference(list->shared[i]); |
| 4460 | |
| 4461 | args->busy |= busy_check_reader(fence); |
| 4462 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4463 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4464 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4465 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4466 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4467 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4468 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4469 | out: |
| 4470 | rcu_read_unlock(); |
| 4471 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | int |
| 4475 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4476 | struct drm_file *file_priv) |
| 4477 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4478 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4479 | } |
| 4480 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4481 | int |
| 4482 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4483 | struct drm_file *file_priv) |
| 4484 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4485 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4486 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4487 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4488 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4489 | |
| 4490 | switch (args->madv) { |
| 4491 | case I915_MADV_DONTNEED: |
| 4492 | case I915_MADV_WILLNEED: |
| 4493 | break; |
| 4494 | default: |
| 4495 | return -EINVAL; |
| 4496 | } |
| 4497 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4498 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4499 | if (!obj) |
| 4500 | return -ENOENT; |
| 4501 | |
| 4502 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4503 | if (err) |
| 4504 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4505 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4506 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4507 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4508 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4509 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4510 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4511 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4512 | obj->mm.quirked = false; |
| 4513 | } |
| 4514 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4515 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4516 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4517 | obj->mm.quirked = true; |
| 4518 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4519 | } |
| 4520 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4521 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4522 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4523 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4524 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4525 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4526 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4527 | i915_gem_object_truncate(obj); |
| 4528 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4529 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4530 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4531 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4532 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4533 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4534 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4535 | } |
| 4536 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4537 | static void |
| 4538 | frontbuffer_retire(struct i915_gem_active *active, |
| 4539 | struct drm_i915_gem_request *request) |
| 4540 | { |
| 4541 | struct drm_i915_gem_object *obj = |
| 4542 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4543 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4544 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4547 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4548 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4549 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4550 | mutex_init(&obj->mm.lock); |
| 4551 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4552 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4553 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4554 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4555 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4556 | obj->ops = ops; |
| 4557 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4558 | reservation_object_init(&obj->__builtin_resv); |
| 4559 | obj->resv = &obj->__builtin_resv; |
| 4560 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4561 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4562 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4563 | |
| 4564 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4565 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4566 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4567 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4568 | 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] | 4569 | } |
| 4570 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4571 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4572 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4573 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4574 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4575 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4576 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4577 | |
| 4578 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4579 | }; |
| 4580 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4581 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4582 | struct drm_gem_object *obj, |
| 4583 | size_t size) |
| 4584 | { |
| 4585 | struct drm_i915_private *i915 = to_i915(dev); |
| 4586 | unsigned long flags = VM_NORESERVE; |
| 4587 | struct file *filp; |
| 4588 | |
| 4589 | drm_gem_private_object_init(dev, obj, size); |
| 4590 | |
| 4591 | if (i915->mm.gemfs) |
| 4592 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4593 | flags); |
| 4594 | else |
| 4595 | filp = shmem_file_setup("i915", size, flags); |
| 4596 | |
| 4597 | if (IS_ERR(filp)) |
| 4598 | return PTR_ERR(filp); |
| 4599 | |
| 4600 | obj->filp = filp; |
| 4601 | |
| 4602 | return 0; |
| 4603 | } |
| 4604 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4605 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4606 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4607 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4608 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4609 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4610 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4611 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4612 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4613 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4614 | /* There is a prevalence of the assumption that we fit the object's |
| 4615 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4616 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4617 | * such a local variable, please consider fixing! |
| 4618 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4619 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4620 | return ERR_PTR(-E2BIG); |
| 4621 | |
| 4622 | if (overflows_type(size, obj->base.size)) |
| 4623 | return ERR_PTR(-E2BIG); |
| 4624 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4625 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4626 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4627 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4628 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4629 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4630 | if (ret) |
| 4631 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4632 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4633 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4634 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4635 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4636 | mask &= ~__GFP_HIGHMEM; |
| 4637 | mask |= __GFP_DMA32; |
| 4638 | } |
| 4639 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4640 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4641 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4642 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4643 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4644 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4645 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4646 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4647 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4648 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4649 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4650 | /* 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] | 4651 | * cache) for about a 10% performance improvement |
| 4652 | * compared to uncached. Graphics requests other than |
| 4653 | * display scanout are coherent with the CPU in |
| 4654 | * accessing this cache. This means in this mode we |
| 4655 | * don't need to clflush on the CPU side, and on the |
| 4656 | * GPU side we only need to flush internal caches to |
| 4657 | * get data visible to the CPU. |
| 4658 | * |
| 4659 | * However, we maintain the display planes as UC, and so |
| 4660 | * need to rebind when first used as such. |
| 4661 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4662 | cache_level = I915_CACHE_LLC; |
| 4663 | else |
| 4664 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4665 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4666 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4667 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4668 | trace_i915_gem_object_create(obj); |
| 4669 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4670 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4671 | |
| 4672 | fail: |
| 4673 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4674 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4677 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4678 | { |
| 4679 | /* If we are the last user of the backing storage (be it shmemfs |
| 4680 | * pages or stolen etc), we know that the pages are going to be |
| 4681 | * immediately released. In this case, we can then skip copying |
| 4682 | * back the contents from the GPU. |
| 4683 | */ |
| 4684 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4685 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4686 | return false; |
| 4687 | |
| 4688 | if (obj->base.filp == NULL) |
| 4689 | return true; |
| 4690 | |
| 4691 | /* At first glance, this looks racy, but then again so would be |
| 4692 | * userspace racing mmap against close. However, the first external |
| 4693 | * reference to the filp can only be obtained through the |
| 4694 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4695 | * acquiring such a reference whilst we are in the middle of |
| 4696 | * freeing the object. |
| 4697 | */ |
| 4698 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4699 | } |
| 4700 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4701 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4702 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4703 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4704 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4705 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4706 | intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4707 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4708 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4709 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4710 | trace_i915_gem_object_destroy(obj); |
| 4711 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4712 | mutex_lock(&i915->drm.struct_mutex); |
| 4713 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4714 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
| 4715 | list_for_each_entry_safe(vma, vn, |
| 4716 | &obj->vma_list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4717 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4718 | vma->flags &= ~I915_VMA_PIN_MASK; |
| 4719 | i915_vma_close(vma); |
| 4720 | } |
Chris Wilson | db6c2b4 | 2016-11-01 11:54:00 +0000 | [diff] [blame] | 4721 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
| 4722 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4723 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4724 | /* This serializes freeing with the shrinker. Since the free |
| 4725 | * is delayed, first by RCU then by the workqueue, we want the |
| 4726 | * shrinker to be able to free pages of unreferenced objects, |
| 4727 | * or else we may oom whilst there are plenty of deferred |
| 4728 | * freed objects. |
| 4729 | */ |
| 4730 | if (i915_gem_object_has_pages(obj)) { |
| 4731 | spin_lock(&i915->mm.obj_lock); |
| 4732 | list_del_init(&obj->mm.link); |
| 4733 | spin_unlock(&i915->mm.obj_lock); |
| 4734 | } |
| 4735 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4736 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4737 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4738 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4739 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4740 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4741 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4742 | |
| 4743 | if (obj->ops->release) |
| 4744 | obj->ops->release(obj); |
| 4745 | |
| 4746 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4747 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4748 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4749 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4750 | |
| 4751 | if (obj->base.import_attach) |
| 4752 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4753 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4754 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4755 | drm_gem_object_release(&obj->base); |
| 4756 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4757 | |
| 4758 | kfree(obj->bit_17); |
| 4759 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4760 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame^] | 4761 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4762 | atomic_dec(&i915->mm.free_count); |
| 4763 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4764 | if (on) |
| 4765 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4766 | } |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4767 | intel_runtime_pm_put(i915); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4771 | { |
| 4772 | struct llist_node *freed; |
| 4773 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4774 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4775 | freed = NULL; |
| 4776 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4777 | /* Only one consumer of llist_del_first() allowed */ |
| 4778 | spin_lock(&i915->mm.free_lock); |
| 4779 | freed = llist_del_first(&i915->mm.free_list); |
| 4780 | spin_unlock(&i915->mm.free_lock); |
| 4781 | } |
| 4782 | if (unlikely(freed)) { |
| 4783 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4784 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4785 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | static void __i915_gem_free_work(struct work_struct *work) |
| 4789 | { |
| 4790 | struct drm_i915_private *i915 = |
| 4791 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4792 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4793 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4794 | /* |
| 4795 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4796 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4797 | * However, the object may also be bound into the global GTT (e.g. |
| 4798 | * older GPUs without per-process support, or for direct access through |
| 4799 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4800 | * unbound now. |
| 4801 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4802 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4803 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4804 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4805 | spin_unlock(&i915->mm.free_lock); |
| 4806 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4807 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4808 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4809 | return; |
| 4810 | |
| 4811 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4812 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4813 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4817 | { |
| 4818 | struct drm_i915_gem_object *obj = |
| 4819 | container_of(head, typeof(*obj), rcu); |
| 4820 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4821 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4822 | /* |
| 4823 | * Since we require blocking on struct_mutex to unbind the freed |
| 4824 | * object from the GPU before releasing resources back to the |
| 4825 | * system, we can not do that directly from the RCU callback (which may |
| 4826 | * be a softirq context), but must instead then defer that work onto a |
| 4827 | * kthread. We use the RCU callback rather than move the freed object |
| 4828 | * directly onto the work queue so that we can mix between using the |
| 4829 | * worker and performing frees directly from subsequent allocations for |
| 4830 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4831 | */ |
| 4832 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4833 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4834 | } |
| 4835 | |
| 4836 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4837 | { |
| 4838 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4839 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4840 | if (obj->mm.quirked) |
| 4841 | __i915_gem_object_unpin_pages(obj); |
| 4842 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4843 | if (discard_backing_storage(obj)) |
| 4844 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4845 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4846 | /* |
| 4847 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4848 | * read-side critical sections are complete, e.g. |
| 4849 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4850 | * lookup see i915_gem_object_lookup_rcu(). |
| 4851 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame^] | 4852 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4853 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4854 | } |
| 4855 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4856 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4857 | { |
| 4858 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4859 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4860 | if (!i915_gem_object_has_active_reference(obj) && |
| 4861 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4862 | i915_gem_object_set_active_reference(obj); |
| 4863 | else |
| 4864 | i915_gem_object_put(obj); |
| 4865 | } |
| 4866 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4867 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4868 | { |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4869 | struct i915_gem_context *kernel_context = i915->kernel_context; |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4870 | struct intel_engine_cs *engine; |
| 4871 | enum intel_engine_id id; |
| 4872 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4873 | for_each_engine(engine, i915, id) { |
| 4874 | GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request)); |
| 4875 | GEM_BUG_ON(engine->last_retired_context != kernel_context); |
| 4876 | } |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4877 | } |
| 4878 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4879 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4880 | { |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4881 | if (i915_terminally_wedged(&i915->gpu_error)) { |
| 4882 | mutex_lock(&i915->drm.struct_mutex); |
| 4883 | i915_gem_unset_wedged(i915); |
| 4884 | mutex_unlock(&i915->drm.struct_mutex); |
| 4885 | } |
| 4886 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4887 | /* |
| 4888 | * If we inherit context state from the BIOS or earlier occupants |
| 4889 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4890 | * try to take over. The only way to remove the earlier state |
| 4891 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4892 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4893 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4894 | */ |
Daniele Ceraolo Spurio | ce1599a | 2018-02-07 13:24:40 -0800 | [diff] [blame] | 4895 | if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915)) |
| 4896 | WARN_ON(intel_gpu_reset(i915, ALL_ENGINES)); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4899 | int i915_gem_suspend(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4900 | { |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4901 | struct drm_device *dev = &dev_priv->drm; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4902 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4903 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4904 | intel_runtime_pm_get(dev_priv); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4905 | intel_suspend_gt_powersave(dev_priv); |
| 4906 | |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4907 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4908 | |
| 4909 | /* We have to flush all the executing contexts to main memory so |
| 4910 | * that they can saved in the hibernation image. To ensure the last |
| 4911 | * context image is coherent, we have to switch away from it. That |
| 4912 | * leaves the dev_priv->kernel_context still active when |
| 4913 | * we actually suspend, and its image in memory may not match the GPU |
| 4914 | * state. Fortunately, the kernel_context is disposable and we do |
| 4915 | * not rely on its state. |
| 4916 | */ |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4917 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 4918 | ret = i915_gem_switch_to_kernel_context(dev_priv); |
| 4919 | if (ret) |
| 4920 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4921 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4922 | ret = i915_gem_wait_for_idle(dev_priv, |
| 4923 | I915_WAIT_INTERRUPTIBLE | |
| 4924 | I915_WAIT_LOCKED); |
| 4925 | if (ret && ret != -EIO) |
| 4926 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4927 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4928 | assert_kernel_context_is_current(dev_priv); |
| 4929 | } |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4930 | i915_gem_contexts_lost(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4931 | mutex_unlock(&dev->struct_mutex); |
| 4932 | |
Sagar Arun Kamble | 63987bf | 2017-04-05 15:51:50 +0530 | [diff] [blame] | 4933 | intel_guc_suspend(dev_priv); |
| 4934 | |
Chris Wilson | 737b150 | 2015-01-26 18:03:03 +0200 | [diff] [blame] | 4935 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 4936 | cancel_delayed_work_sync(&dev_priv->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4937 | |
| 4938 | /* As the idle_work is rearming if it detects a race, play safe and |
| 4939 | * repeat the flush until it is definitely idle. |
| 4940 | */ |
Chris Wilson | 7c26240 | 2017-10-06 11:40:38 +0100 | [diff] [blame] | 4941 | drain_delayed_work(&dev_priv->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4942 | |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4943 | /* Assert that we sucessfully flushed all the work and |
| 4944 | * reset the GPU back to its idle, low power state. |
| 4945 | */ |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 4946 | WARN_ON(dev_priv->gt.awake); |
Chris Wilson | fc692bd | 2017-08-26 12:09:35 +0100 | [diff] [blame] | 4947 | if (WARN_ON(!intel_engines_are_idle(dev_priv))) |
| 4948 | i915_gem_set_wedged(dev_priv); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4949 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4950 | /* |
| 4951 | * Neither the BIOS, ourselves or any other kernel |
| 4952 | * expects the system to be in execlists mode on startup, |
| 4953 | * so we need to reset the GPU back to legacy mode. And the only |
| 4954 | * known way to disable logical contexts is through a GPU reset. |
| 4955 | * |
| 4956 | * So in order to leave the system in a known default configuration, |
| 4957 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4958 | * clean up the GEM state tracking, flushing off the requests and |
| 4959 | * leaving the system in a known idle state. |
| 4960 | * |
| 4961 | * Note that is of the upmost importance that the GPU is idle and |
| 4962 | * all stray writes are flushed *before* we dismantle the backing |
| 4963 | * storage for the pinned objects. |
| 4964 | * |
| 4965 | * However, since we are uncertain that resetting the GPU on older |
| 4966 | * machines is a good idea, we don't - just in case it leaves the |
| 4967 | * machine in an unusable condition. |
| 4968 | */ |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4969 | i915_gem_sanitize(dev_priv); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4970 | |
| 4971 | intel_runtime_pm_put(dev_priv); |
| 4972 | return 0; |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4973 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4974 | err_unlock: |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4975 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4976 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4977 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4978 | } |
| 4979 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4980 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4981 | { |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4982 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4983 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4984 | mutex_lock(&i915->drm.struct_mutex); |
| 4985 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4986 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4987 | i915_gem_restore_gtt_mappings(i915); |
| 4988 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4989 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4990 | /* |
| 4991 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4992 | * guarantee that the context image is complete. So let's just reset |
| 4993 | * it and start again. |
| 4994 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4995 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4996 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4997 | if (i915_gem_init_hw(i915)) |
| 4998 | goto err_wedged; |
| 4999 | |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 5000 | intel_guc_resume(i915); |
| 5001 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5002 | /* Always reload a context for powersaving. */ |
| 5003 | if (i915_gem_switch_to_kernel_context(i915)) |
| 5004 | goto err_wedged; |
| 5005 | |
| 5006 | out_unlock: |
| 5007 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 5008 | mutex_unlock(&i915->drm.struct_mutex); |
| 5009 | return; |
| 5010 | |
| 5011 | err_wedged: |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5012 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 5013 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 5014 | i915_gem_set_wedged(i915); |
| 5015 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5016 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5019 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5020 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5021 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5022 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 5023 | return; |
| 5024 | |
| 5025 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 5026 | DISP_TILE_SURFACE_SWIZZLING); |
| 5027 | |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5028 | if (IS_GEN5(dev_priv)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 5029 | return; |
| 5030 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5031 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5032 | if (IS_GEN6(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5033 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5034 | else if (IS_GEN7(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5035 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5036 | else if (IS_GEN8(dev_priv)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 5037 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 5038 | else |
| 5039 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5040 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 5041 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5042 | 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] | 5043 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5044 | I915_WRITE(RING_CTL(base), 0); |
| 5045 | I915_WRITE(RING_HEAD(base), 0); |
| 5046 | I915_WRITE(RING_TAIL(base), 0); |
| 5047 | I915_WRITE(RING_START(base), 0); |
| 5048 | } |
| 5049 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5050 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5051 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5052 | if (IS_I830(dev_priv)) { |
| 5053 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5054 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5055 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5056 | init_unused_ring(dev_priv, SRB2_BASE); |
| 5057 | init_unused_ring(dev_priv, SRB3_BASE); |
| 5058 | } else if (IS_GEN2(dev_priv)) { |
| 5059 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5060 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5061 | } else if (IS_GEN3(dev_priv)) { |
| 5062 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5063 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5064 | } |
| 5065 | } |
| 5066 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5067 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5068 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5069 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5070 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5071 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5072 | int err; |
| 5073 | |
| 5074 | for_each_engine(engine, i915, id) { |
| 5075 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5076 | if (err) { |
| 5077 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 5078 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5079 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5080 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | return 0; |
| 5084 | } |
| 5085 | |
| 5086 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 5087 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 5088 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5089 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 5090 | dev_priv->gt.last_init_time = ktime_get(); |
| 5091 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5092 | /* Double layer security blanket, see i915_gem_init() */ |
| 5093 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5094 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 5095 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 5096 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5097 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 5098 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5099 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 5100 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 5101 | |
Tvrtko Ursulin | 6e26695 | 2016-10-13 11:02:53 +0100 | [diff] [blame] | 5102 | if (HAS_PCH_NOP(dev_priv)) { |
Tvrtko Ursulin | fd6b8f4 | 2016-10-14 10:13:06 +0100 | [diff] [blame] | 5103 | if (IS_IVYBRIDGE(dev_priv)) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 5104 | u32 temp = I915_READ(GEN7_MSG_CTL); |
| 5105 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); |
| 5106 | I915_WRITE(GEN7_MSG_CTL, temp); |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5107 | } else if (INTEL_GEN(dev_priv) >= 7) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 5108 | u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); |
| 5109 | temp &= ~RESET_PCH_HANDSHAKE_ENABLE; |
| 5110 | I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); |
| 5111 | } |
Ben Widawsky | 88a2b2a | 2013-04-05 13:12:43 -0700 | [diff] [blame] | 5112 | } |
| 5113 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5114 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5115 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5116 | /* |
| 5117 | * At least 830 can leave some of the unused rings |
| 5118 | * "active" (ie. head != tail) after resume which |
| 5119 | * will prevent c3 entry. Makes sure all unused rings |
| 5120 | * are totally idle. |
| 5121 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5122 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5123 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 5124 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5125 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5126 | ret = -EIO; |
| 5127 | goto out; |
| 5128 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 5129 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5130 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5131 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5132 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5133 | goto out; |
| 5134 | } |
| 5135 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5136 | /* We can't enable contexts until all firmware is loaded */ |
| 5137 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5138 | if (ret) { |
| 5139 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5140 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5141 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5142 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5143 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 5144 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 5145 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 5146 | ret = __i915_gem_restart_engines(dev_priv); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5147 | out: |
| 5148 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5149 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5150 | } |
| 5151 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5152 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 5153 | { |
| 5154 | struct i915_gem_context *ctx; |
| 5155 | struct intel_engine_cs *engine; |
| 5156 | enum intel_engine_id id; |
| 5157 | int err; |
| 5158 | |
| 5159 | /* |
| 5160 | * As we reset the gpu during very early sanitisation, the current |
| 5161 | * register state on the GPU should reflect its defaults values. |
| 5162 | * We load a context onto the hw (with restore-inhibit), then switch |
| 5163 | * over to a second context to save that default register state. We |
| 5164 | * can then prime every new context with that state so they all start |
| 5165 | * from the same default HW values. |
| 5166 | */ |
| 5167 | |
| 5168 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 5169 | if (IS_ERR(ctx)) |
| 5170 | return PTR_ERR(ctx); |
| 5171 | |
| 5172 | for_each_engine(engine, i915, id) { |
| 5173 | struct drm_i915_gem_request *rq; |
| 5174 | |
| 5175 | rq = i915_gem_request_alloc(engine, ctx); |
| 5176 | if (IS_ERR(rq)) { |
| 5177 | err = PTR_ERR(rq); |
| 5178 | goto out_ctx; |
| 5179 | } |
| 5180 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 5181 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5182 | if (engine->init_context) |
| 5183 | err = engine->init_context(rq); |
| 5184 | |
| 5185 | __i915_add_request(rq, true); |
| 5186 | if (err) |
| 5187 | goto err_active; |
| 5188 | } |
| 5189 | |
| 5190 | err = i915_gem_switch_to_kernel_context(i915); |
| 5191 | if (err) |
| 5192 | goto err_active; |
| 5193 | |
| 5194 | err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED); |
| 5195 | if (err) |
| 5196 | goto err_active; |
| 5197 | |
| 5198 | assert_kernel_context_is_current(i915); |
| 5199 | |
| 5200 | for_each_engine(engine, i915, id) { |
| 5201 | struct i915_vma *state; |
| 5202 | |
| 5203 | state = ctx->engine[id].state; |
| 5204 | if (!state) |
| 5205 | continue; |
| 5206 | |
| 5207 | /* |
| 5208 | * As we will hold a reference to the logical state, it will |
| 5209 | * not be torn down with the context, and importantly the |
| 5210 | * object will hold onto its vma (making it possible for a |
| 5211 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 5212 | * from the GTT to prevent such accidents and reclaim the |
| 5213 | * space. |
| 5214 | */ |
| 5215 | err = i915_vma_unbind(state); |
| 5216 | if (err) |
| 5217 | goto err_active; |
| 5218 | |
| 5219 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 5220 | if (err) |
| 5221 | goto err_active; |
| 5222 | |
| 5223 | engine->default_state = i915_gem_object_get(state->obj); |
| 5224 | } |
| 5225 | |
| 5226 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 5227 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 5228 | |
| 5229 | /* |
| 5230 | * Make sure that classes with multiple engine instances all |
| 5231 | * share the same basic configuration. |
| 5232 | */ |
| 5233 | for_each_engine(engine, i915, id) { |
| 5234 | unsigned int bit = BIT(engine->uabi_class); |
| 5235 | unsigned int expected = engine->default_state ? bit : 0; |
| 5236 | |
| 5237 | if ((found & bit) != expected) { |
| 5238 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 5239 | engine->uabi_class, engine->name); |
| 5240 | } |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | out_ctx: |
| 5245 | i915_gem_context_set_closed(ctx); |
| 5246 | i915_gem_context_put(ctx); |
| 5247 | return err; |
| 5248 | |
| 5249 | err_active: |
| 5250 | /* |
| 5251 | * If we have to abandon now, we expect the engines to be idle |
| 5252 | * and ready to be torn-down. First try to flush any remaining |
| 5253 | * request, ensure we are pointing at the kernel context and |
| 5254 | * then remove it. |
| 5255 | */ |
| 5256 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 5257 | goto out_ctx; |
| 5258 | |
| 5259 | if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED))) |
| 5260 | goto out_ctx; |
| 5261 | |
| 5262 | i915_gem_contexts_lost(i915); |
| 5263 | goto out_ctx; |
| 5264 | } |
| 5265 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5266 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5267 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5268 | int ret; |
| 5269 | |
Matthew Auld | da9fe3f3 | 2017-10-06 23:18:31 +0100 | [diff] [blame] | 5270 | /* |
| 5271 | * We need to fallback to 4K pages since gvt gtt handling doesn't |
| 5272 | * support huge page entries - we will need to check either hypervisor |
| 5273 | * mm can support huge guest page or just do emulation in gvt. |
| 5274 | */ |
| 5275 | if (intel_vgpu_active(dev_priv)) |
| 5276 | mkwrite_device_info(dev_priv)->page_sizes = |
| 5277 | I915_GTT_PAGE_SIZE_4K; |
| 5278 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 5279 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 5280 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5281 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 5282 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5283 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5284 | } else { |
| 5285 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5286 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5287 | } |
| 5288 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5289 | ret = i915_gem_init_userptr(dev_priv); |
| 5290 | if (ret) |
| 5291 | return ret; |
| 5292 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5293 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 5294 | if (ret) |
| 5295 | return ret; |
| 5296 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5297 | /* This is just a security blanket to placate dragons. |
| 5298 | * On some systems, we very sporadically observe that the first TLBs |
| 5299 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5300 | * we hold the forcewake during initialisation these problems |
| 5301 | * just magically go away. |
| 5302 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5303 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5304 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5305 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5306 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5307 | if (ret) { |
| 5308 | GEM_BUG_ON(ret == -EIO); |
| 5309 | goto err_unlock; |
| 5310 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5311 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5312 | ret = i915_gem_contexts_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5313 | if (ret) { |
| 5314 | GEM_BUG_ON(ret == -EIO); |
| 5315 | goto err_ggtt; |
| 5316 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5317 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5318 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5319 | if (ret) { |
| 5320 | GEM_BUG_ON(ret == -EIO); |
| 5321 | goto err_context; |
| 5322 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5323 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5324 | intel_init_gt_powersave(dev_priv); |
| 5325 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5326 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5327 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5328 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5329 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5330 | ret = i915_gem_init_hw(dev_priv); |
| 5331 | if (ret) |
| 5332 | goto err_uc_init; |
| 5333 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5334 | /* |
| 5335 | * Despite its name intel_init_clock_gating applies both display |
| 5336 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5337 | * GT power context workaround. Worse, sometimes it includes a context |
| 5338 | * register workaround which we need to apply before we record the |
| 5339 | * default HW state for all contexts. |
| 5340 | * |
| 5341 | * FIXME: break up the workarounds and apply them at the right time! |
| 5342 | */ |
| 5343 | intel_init_clock_gating(dev_priv); |
| 5344 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5345 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5346 | if (ret) |
| 5347 | goto err_init_hw; |
| 5348 | |
| 5349 | if (i915_inject_load_failure()) { |
| 5350 | ret = -ENODEV; |
| 5351 | goto err_init_hw; |
| 5352 | } |
| 5353 | |
| 5354 | if (i915_inject_load_failure()) { |
| 5355 | ret = -EIO; |
| 5356 | goto err_init_hw; |
| 5357 | } |
| 5358 | |
| 5359 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5360 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5361 | |
| 5362 | return 0; |
| 5363 | |
| 5364 | /* |
| 5365 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5366 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5367 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5368 | * driver doesn't explode during runtime. |
| 5369 | */ |
| 5370 | err_init_hw: |
| 5371 | i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED); |
| 5372 | i915_gem_contexts_lost(dev_priv); |
| 5373 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5374 | err_uc_init: |
| 5375 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5376 | err_pm: |
| 5377 | if (ret != -EIO) { |
| 5378 | intel_cleanup_gt_powersave(dev_priv); |
| 5379 | i915_gem_cleanup_engines(dev_priv); |
| 5380 | } |
| 5381 | err_context: |
| 5382 | if (ret != -EIO) |
| 5383 | i915_gem_contexts_fini(dev_priv); |
| 5384 | err_ggtt: |
| 5385 | err_unlock: |
| 5386 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5387 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5388 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5389 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5390 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5391 | if (ret != -EIO) |
| 5392 | i915_gem_cleanup_userptr(dev_priv); |
| 5393 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5394 | if (ret == -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5395 | /* |
| 5396 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5397 | * wedged. But we only want to do this where the GPU is angry, |
| 5398 | * for all other failure, such as an allocation failure, bail. |
| 5399 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5400 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5401 | DRM_ERROR("Failed to initialize GPU, declaring it wedged\n"); |
| 5402 | i915_gem_set_wedged(dev_priv); |
| 5403 | } |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5404 | ret = 0; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5405 | } |
| 5406 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5407 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5408 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5409 | } |
| 5410 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5411 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5412 | { |
| 5413 | i915_gem_sanitize(i915); |
| 5414 | } |
| 5415 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5416 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5417 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5418 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5419 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5420 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5421 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5422 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5423 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5424 | } |
| 5425 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5426 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5427 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5428 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5429 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5430 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5431 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5432 | !IS_CHERRYVIEW(dev_priv)) |
| 5433 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5434 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5435 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5436 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5437 | dev_priv->num_fence_regs = 16; |
| 5438 | else |
| 5439 | dev_priv->num_fence_regs = 8; |
| 5440 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5441 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5442 | dev_priv->num_fence_regs = |
| 5443 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5444 | |
| 5445 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5446 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5447 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5448 | |
| 5449 | fence->i915 = dev_priv; |
| 5450 | fence->id = i; |
| 5451 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5452 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5453 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5454 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5455 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5456 | } |
| 5457 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5458 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5459 | { |
| 5460 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5461 | spin_lock_init(&i915->mm.obj_lock); |
| 5462 | spin_lock_init(&i915->mm.free_lock); |
| 5463 | |
| 5464 | init_llist_head(&i915->mm.free_list); |
| 5465 | |
| 5466 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5467 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5468 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5469 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5470 | |
| 5471 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5472 | } |
| 5473 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5474 | int |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5475 | i915_gem_load_init(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5476 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5477 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5478 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5479 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5480 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5481 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5482 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5483 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5484 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5485 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5486 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5487 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5488 | if (!dev_priv->luts) |
| 5489 | goto err_vmas; |
| 5490 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5491 | dev_priv->requests = KMEM_CACHE(drm_i915_gem_request, |
| 5492 | SLAB_HWCACHE_ALIGN | |
| 5493 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5494 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5495 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5496 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5497 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5498 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5499 | SLAB_HWCACHE_ALIGN | |
| 5500 | SLAB_RECLAIM_ACCOUNT); |
| 5501 | if (!dev_priv->dependencies) |
| 5502 | goto err_requests; |
| 5503 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5504 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5505 | if (!dev_priv->priorities) |
| 5506 | goto err_dependencies; |
| 5507 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5508 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5509 | INIT_LIST_HEAD(&dev_priv->gt.timelines); |
Chris Wilson | bb89485 | 2016-11-14 20:40:57 +0000 | [diff] [blame] | 5510 | err = i915_gem_timeline_init__global(dev_priv); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5511 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5512 | if (err) |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5513 | goto err_priorities; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5514 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5515 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5516 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5517 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5518 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5519 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5520 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5521 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5522 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5523 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5524 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5525 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5526 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5527 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5528 | err = i915_gemfs_init(dev_priv); |
| 5529 | if (err) |
| 5530 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5531 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5532 | return 0; |
| 5533 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5534 | err_priorities: |
| 5535 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5536 | err_dependencies: |
| 5537 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5538 | err_requests: |
| 5539 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5540 | err_luts: |
| 5541 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5542 | err_vmas: |
| 5543 | kmem_cache_destroy(dev_priv->vmas); |
| 5544 | err_objects: |
| 5545 | kmem_cache_destroy(dev_priv->objects); |
| 5546 | err_out: |
| 5547 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5548 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5549 | |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5550 | void i915_gem_load_cleanup(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5551 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5552 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame^] | 5553 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5554 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5555 | WARN_ON(dev_priv->mm.object_count); |
Chris Wilson | 7d5d59e | 2016-11-01 08:48:41 +0000 | [diff] [blame] | 5556 | |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5557 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5558 | i915_gem_timeline_fini(&dev_priv->gt.global_timeline); |
| 5559 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); |
| 5560 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5561 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5562 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5563 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5564 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5565 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5566 | kmem_cache_destroy(dev_priv->vmas); |
| 5567 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5568 | |
| 5569 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5570 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5571 | |
| 5572 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5573 | } |
| 5574 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5575 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5576 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5577 | /* Discard all purgeable objects, let userspace recover those as |
| 5578 | * required after resuming. |
| 5579 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5580 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5581 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5582 | return 0; |
| 5583 | } |
| 5584 | |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5585 | int i915_gem_freeze_late(struct drm_i915_private *dev_priv) |
| 5586 | { |
| 5587 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5588 | struct list_head *phases[] = { |
| 5589 | &dev_priv->mm.unbound_list, |
| 5590 | &dev_priv->mm.bound_list, |
| 5591 | NULL |
| 5592 | }, **p; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5593 | |
| 5594 | /* Called just before we write the hibernation image. |
| 5595 | * |
| 5596 | * We need to update the domain tracking to reflect that the CPU |
| 5597 | * will be accessing all the pages to create and restore from the |
| 5598 | * hibernation, and so upon restoration those pages will be in the |
| 5599 | * CPU domain. |
| 5600 | * |
| 5601 | * To make sure the hibernation image contains the latest state, |
| 5602 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5603 | * |
| 5604 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5605 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5606 | */ |
| 5607 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 5608 | i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND); |
Chris Wilson | 17b93c4 | 2017-04-07 11:25:50 +0100 | [diff] [blame] | 5609 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5610 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5611 | spin_lock(&dev_priv->mm.obj_lock); |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5612 | for (p = phases; *p; p++) { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5613 | list_for_each_entry(obj, *p, mm.link) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 5614 | __start_cpu_write(obj); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5615 | } |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5616 | spin_unlock(&dev_priv->mm.obj_lock); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5617 | |
| 5618 | return 0; |
| 5619 | } |
| 5620 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5621 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5622 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5623 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | 15f7bbc | 2016-07-26 12:01:52 +0100 | [diff] [blame] | 5624 | struct drm_i915_gem_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5625 | |
| 5626 | /* Clean up our request list when the client is going away, so that |
| 5627 | * later retire_requests won't dereference our soon-to-be-gone |
| 5628 | * file_priv. |
| 5629 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5630 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5631 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5632 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5633 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5634 | } |
| 5635 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5636 | 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] | 5637 | { |
| 5638 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5639 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5640 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5641 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5642 | |
| 5643 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5644 | if (!file_priv) |
| 5645 | return -ENOMEM; |
| 5646 | |
| 5647 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5648 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5649 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5650 | |
| 5651 | spin_lock_init(&file_priv->mm.lock); |
| 5652 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5653 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5654 | file_priv->bsd_engine = -1; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5655 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5656 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5657 | if (ret) |
| 5658 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5659 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5660 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5661 | } |
| 5662 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5663 | /** |
| 5664 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5665 | * @old: current GEM buffer for the frontbuffer slots |
| 5666 | * @new: new GEM buffer for the frontbuffer slots |
| 5667 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5668 | * |
| 5669 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5670 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5671 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5672 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5673 | struct drm_i915_gem_object *new, |
| 5674 | unsigned frontbuffer_bits) |
| 5675 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5676 | /* Control of individual bits within the mask are guarded by |
| 5677 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5678 | * manipulation of individual bits. But since the bitfield as a whole |
| 5679 | * is updated using RMW, we need to use atomics in order to update |
| 5680 | * the bits. |
| 5681 | */ |
| 5682 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
| 5683 | sizeof(atomic_t) * BITS_PER_BYTE); |
| 5684 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5685 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5686 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5687 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5691 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5692 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5693 | } |
| 5694 | } |
| 5695 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5696 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5697 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5698 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5699 | const void *data, size_t size) |
| 5700 | { |
| 5701 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5702 | struct file *file; |
| 5703 | size_t offset; |
| 5704 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5705 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5706 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5707 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5708 | return obj; |
| 5709 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5710 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5711 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5712 | file = obj->base.filp; |
| 5713 | offset = 0; |
| 5714 | do { |
| 5715 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5716 | struct page *page; |
| 5717 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5718 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5719 | err = pagecache_write_begin(file, file->f_mapping, |
| 5720 | offset, len, 0, |
| 5721 | &page, &pgdata); |
| 5722 | if (err < 0) |
| 5723 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5724 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5725 | vaddr = kmap(page); |
| 5726 | memcpy(vaddr, data, len); |
| 5727 | kunmap(page); |
| 5728 | |
| 5729 | err = pagecache_write_end(file, file->f_mapping, |
| 5730 | offset, len, len, |
| 5731 | page, pgdata); |
| 5732 | if (err < 0) |
| 5733 | goto fail; |
| 5734 | |
| 5735 | size -= len; |
| 5736 | data += len; |
| 5737 | offset += len; |
| 5738 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5739 | |
| 5740 | return obj; |
| 5741 | |
| 5742 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5743 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5744 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5745 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5746 | |
| 5747 | struct scatterlist * |
| 5748 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5749 | unsigned int n, |
| 5750 | unsigned int *offset) |
| 5751 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5752 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5753 | struct scatterlist *sg; |
| 5754 | unsigned int idx, count; |
| 5755 | |
| 5756 | might_sleep(); |
| 5757 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5758 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5759 | |
| 5760 | /* As we iterate forward through the sg, we record each entry in a |
| 5761 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5762 | * this index previously, we will have an entry for it. |
| 5763 | * |
| 5764 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5765 | * sequential page access (where each new request is consecutive |
| 5766 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5767 | * i.e. O(1) with a large constant! |
| 5768 | */ |
| 5769 | if (n < READ_ONCE(iter->sg_idx)) |
| 5770 | goto lookup; |
| 5771 | |
| 5772 | mutex_lock(&iter->lock); |
| 5773 | |
| 5774 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5775 | * (or the subsequent) sg are fast - comparing against the last |
| 5776 | * sg is faster than going through the radixtree. |
| 5777 | */ |
| 5778 | |
| 5779 | sg = iter->sg_pos; |
| 5780 | idx = iter->sg_idx; |
| 5781 | count = __sg_page_count(sg); |
| 5782 | |
| 5783 | while (idx + count <= n) { |
| 5784 | unsigned long exception, i; |
| 5785 | int ret; |
| 5786 | |
| 5787 | /* If we cannot allocate and insert this entry, or the |
| 5788 | * individual pages from this range, cancel updating the |
| 5789 | * sg_idx so that on this lookup we are forced to linearly |
| 5790 | * scan onwards, but on future lookups we will try the |
| 5791 | * insertion again (in which case we need to be careful of |
| 5792 | * the error return reporting that we have already inserted |
| 5793 | * this index). |
| 5794 | */ |
| 5795 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5796 | if (ret && ret != -EEXIST) |
| 5797 | goto scan; |
| 5798 | |
| 5799 | exception = |
| 5800 | RADIX_TREE_EXCEPTIONAL_ENTRY | |
| 5801 | idx << RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5802 | for (i = 1; i < count; i++) { |
| 5803 | ret = radix_tree_insert(&iter->radix, idx + i, |
| 5804 | (void *)exception); |
| 5805 | if (ret && ret != -EEXIST) |
| 5806 | goto scan; |
| 5807 | } |
| 5808 | |
| 5809 | idx += count; |
| 5810 | sg = ____sg_next(sg); |
| 5811 | count = __sg_page_count(sg); |
| 5812 | } |
| 5813 | |
| 5814 | scan: |
| 5815 | iter->sg_pos = sg; |
| 5816 | iter->sg_idx = idx; |
| 5817 | |
| 5818 | mutex_unlock(&iter->lock); |
| 5819 | |
| 5820 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5821 | goto lookup; |
| 5822 | |
| 5823 | /* In case we failed to insert the entry into the radixtree, we need |
| 5824 | * to look beyond the current sg. |
| 5825 | */ |
| 5826 | while (idx + count <= n) { |
| 5827 | idx += count; |
| 5828 | sg = ____sg_next(sg); |
| 5829 | count = __sg_page_count(sg); |
| 5830 | } |
| 5831 | |
| 5832 | *offset = n - idx; |
| 5833 | return sg; |
| 5834 | |
| 5835 | lookup: |
| 5836 | rcu_read_lock(); |
| 5837 | |
| 5838 | sg = radix_tree_lookup(&iter->radix, n); |
| 5839 | GEM_BUG_ON(!sg); |
| 5840 | |
| 5841 | /* If this index is in the middle of multi-page sg entry, |
| 5842 | * the radixtree will contain an exceptional entry that points |
| 5843 | * to the start of that range. We will return the pointer to |
| 5844 | * the base page and the offset of this page within the |
| 5845 | * sg entry's range. |
| 5846 | */ |
| 5847 | *offset = 0; |
| 5848 | if (unlikely(radix_tree_exception(sg))) { |
| 5849 | unsigned long base = |
| 5850 | (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5851 | |
| 5852 | sg = radix_tree_lookup(&iter->radix, base); |
| 5853 | GEM_BUG_ON(!sg); |
| 5854 | |
| 5855 | *offset = n - base; |
| 5856 | } |
| 5857 | |
| 5858 | rcu_read_unlock(); |
| 5859 | |
| 5860 | return sg; |
| 5861 | } |
| 5862 | |
| 5863 | struct page * |
| 5864 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5865 | { |
| 5866 | struct scatterlist *sg; |
| 5867 | unsigned int offset; |
| 5868 | |
| 5869 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5870 | |
| 5871 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5872 | return nth_page(sg_page(sg), offset); |
| 5873 | } |
| 5874 | |
| 5875 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5876 | struct page * |
| 5877 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5878 | unsigned int n) |
| 5879 | { |
| 5880 | struct page *page; |
| 5881 | |
| 5882 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5883 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5884 | set_page_dirty(page); |
| 5885 | |
| 5886 | return page; |
| 5887 | } |
| 5888 | |
| 5889 | dma_addr_t |
| 5890 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5891 | unsigned long n) |
| 5892 | { |
| 5893 | struct scatterlist *sg; |
| 5894 | unsigned int offset; |
| 5895 | |
| 5896 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5897 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5898 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5899 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5900 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5901 | { |
| 5902 | struct sg_table *pages; |
| 5903 | int err; |
| 5904 | |
| 5905 | if (align > obj->base.size) |
| 5906 | return -EINVAL; |
| 5907 | |
| 5908 | if (obj->ops == &i915_gem_phys_ops) |
| 5909 | return 0; |
| 5910 | |
| 5911 | if (obj->ops != &i915_gem_object_ops) |
| 5912 | return -EINVAL; |
| 5913 | |
| 5914 | err = i915_gem_object_unbind(obj); |
| 5915 | if (err) |
| 5916 | return err; |
| 5917 | |
| 5918 | mutex_lock(&obj->mm.lock); |
| 5919 | |
| 5920 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5921 | err = -EFAULT; |
| 5922 | goto err_unlock; |
| 5923 | } |
| 5924 | |
| 5925 | if (obj->mm.quirked) { |
| 5926 | err = -EFAULT; |
| 5927 | goto err_unlock; |
| 5928 | } |
| 5929 | |
| 5930 | if (obj->mm.mapping) { |
| 5931 | err = -EBUSY; |
| 5932 | goto err_unlock; |
| 5933 | } |
| 5934 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5935 | pages = fetch_and_zero(&obj->mm.pages); |
| 5936 | if (pages) { |
| 5937 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 5938 | |
| 5939 | __i915_gem_object_reset_page_iter(obj); |
| 5940 | |
| 5941 | spin_lock(&i915->mm.obj_lock); |
| 5942 | list_del(&obj->mm.link); |
| 5943 | spin_unlock(&i915->mm.obj_lock); |
| 5944 | } |
| 5945 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5946 | obj->ops = &i915_gem_phys_ops; |
| 5947 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5948 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5949 | if (err) |
| 5950 | goto err_xfer; |
| 5951 | |
| 5952 | /* Perma-pin (until release) the physical set of pages */ |
| 5953 | __i915_gem_object_pin_pages(obj); |
| 5954 | |
| 5955 | if (!IS_ERR_OR_NULL(pages)) |
| 5956 | i915_gem_object_ops.put_pages(obj, pages); |
| 5957 | mutex_unlock(&obj->mm.lock); |
| 5958 | return 0; |
| 5959 | |
| 5960 | err_xfer: |
| 5961 | obj->ops = &i915_gem_object_ops; |
| 5962 | obj->mm.pages = pages; |
| 5963 | err_unlock: |
| 5964 | mutex_unlock(&obj->mm.lock); |
| 5965 | return err; |
| 5966 | } |
| 5967 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5968 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5969 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5970 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5971 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5972 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5973 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5974 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5975 | #endif |