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 | { |
| 243 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 244 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 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 && |
| 260 | (obj->base.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 | |
| 372 | /* This client is about to stall waiting for the GPU. In many cases |
| 373 | * this is undesirable and limits the throughput of the system, as |
| 374 | * many clients cannot continue processing user input/output whilst |
| 375 | * blocked. RPS autotuning may take tens of milliseconds to respond |
| 376 | * to the GPU load and thus incurs additional latency for the client. |
| 377 | * We can circumvent that by promoting the GPU frequency to maximum |
| 378 | * before we wait. This makes the GPU throttle up much more quickly |
| 379 | * (good for benchmarks and user experience, e.g. window animations), |
| 380 | * but at a cost of spending more power processing the workload |
| 381 | * (bad for battery). Not all clients even want their results |
| 382 | * immediately and for them we should just let the GPU select its own |
| 383 | * frequency to maximise efficiency. To prevent a single client from |
| 384 | * forcing the clocks too high for the whole system, we only allow |
| 385 | * each client to waitboost once in a busy period. |
| 386 | */ |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 387 | if (rps_client) { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 388 | if (INTEL_GEN(rq->i915) >= 6) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 389 | gen6_rps_boost(rq, rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 390 | else |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 391 | rps_client = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | timeout = i915_wait_request(rq, flags, timeout); |
| 395 | |
| 396 | out: |
| 397 | if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq)) |
| 398 | i915_gem_request_retire_upto(rq); |
| 399 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 400 | return timeout; |
| 401 | } |
| 402 | |
| 403 | static long |
| 404 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 405 | unsigned int flags, |
| 406 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 407 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 408 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 409 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 410 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 411 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 412 | |
| 413 | if (flags & I915_WAIT_ALL) { |
| 414 | struct dma_fence **shared; |
| 415 | unsigned int count, i; |
| 416 | int ret; |
| 417 | |
| 418 | ret = reservation_object_get_fences_rcu(resv, |
| 419 | &excl, &count, &shared); |
| 420 | if (ret) |
| 421 | return ret; |
| 422 | |
| 423 | for (i = 0; i < count; i++) { |
| 424 | timeout = i915_gem_object_wait_fence(shared[i], |
| 425 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 426 | rps_client); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 427 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 428 | break; |
| 429 | |
| 430 | dma_fence_put(shared[i]); |
| 431 | } |
| 432 | |
| 433 | for (; i < count; i++) |
| 434 | dma_fence_put(shared[i]); |
| 435 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 436 | |
| 437 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 438 | } else { |
| 439 | excl = reservation_object_get_excl_rcu(resv); |
| 440 | } |
| 441 | |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 442 | if (excl && timeout >= 0) { |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 443 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
| 444 | rps_client); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 445 | prune_fences = timeout >= 0; |
| 446 | } |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 447 | |
| 448 | dma_fence_put(excl); |
| 449 | |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 450 | /* Oportunistically prune the fences iff we know they have *all* been |
| 451 | * signaled and that the reservation object has not been changed (i.e. |
| 452 | * no new fences have been added). |
| 453 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 454 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 455 | if (reservation_object_trylock(resv)) { |
| 456 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 457 | reservation_object_add_excl_fence(resv, NULL); |
| 458 | reservation_object_unlock(resv); |
| 459 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 462 | return timeout; |
| 463 | } |
| 464 | |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 465 | static void __fence_set_priority(struct dma_fence *fence, int prio) |
| 466 | { |
| 467 | struct drm_i915_gem_request *rq; |
| 468 | struct intel_engine_cs *engine; |
| 469 | |
| 470 | if (!dma_fence_is_i915(fence)) |
| 471 | return; |
| 472 | |
| 473 | rq = to_request(fence); |
| 474 | engine = rq->engine; |
| 475 | if (!engine->schedule) |
| 476 | return; |
| 477 | |
| 478 | engine->schedule(rq, prio); |
| 479 | } |
| 480 | |
| 481 | static void fence_set_priority(struct dma_fence *fence, int prio) |
| 482 | { |
| 483 | /* Recurse once into a fence-array */ |
| 484 | if (dma_fence_is_array(fence)) { |
| 485 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 486 | int i; |
| 487 | |
| 488 | for (i = 0; i < array->num_fences; i++) |
| 489 | __fence_set_priority(array->fences[i], prio); |
| 490 | } else { |
| 491 | __fence_set_priority(fence, prio); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | int |
| 496 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 497 | unsigned int flags, |
| 498 | int prio) |
| 499 | { |
| 500 | struct dma_fence *excl; |
| 501 | |
| 502 | if (flags & I915_WAIT_ALL) { |
| 503 | struct dma_fence **shared; |
| 504 | unsigned int count, i; |
| 505 | int ret; |
| 506 | |
| 507 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 508 | &excl, &count, &shared); |
| 509 | if (ret) |
| 510 | return ret; |
| 511 | |
| 512 | for (i = 0; i < count; i++) { |
| 513 | fence_set_priority(shared[i], prio); |
| 514 | dma_fence_put(shared[i]); |
| 515 | } |
| 516 | |
| 517 | kfree(shared); |
| 518 | } else { |
| 519 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 520 | } |
| 521 | |
| 522 | if (excl) { |
| 523 | fence_set_priority(excl, prio); |
| 524 | dma_fence_put(excl); |
| 525 | } |
| 526 | return 0; |
| 527 | } |
| 528 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 529 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 530 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 531 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 532 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 533 | * @timeout: how long to wait |
Chris Wilson | a0a8b1c | 2017-11-09 14:06:44 +0000 | [diff] [blame] | 534 | * @rps_client: client (user process) to charge for any waitboosting |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 535 | */ |
| 536 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 537 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 538 | unsigned int flags, |
| 539 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 540 | struct intel_rps_client *rps_client) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 541 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 542 | might_sleep(); |
| 543 | #if IS_ENABLED(CONFIG_LOCKDEP) |
| 544 | GEM_BUG_ON(debug_locks && |
| 545 | !!lockdep_is_held(&obj->base.dev->struct_mutex) != |
| 546 | !!(flags & I915_WAIT_LOCKED)); |
| 547 | #endif |
| 548 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 549 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 550 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 551 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 552 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 553 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 557 | { |
| 558 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 559 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 560 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 563 | static int |
| 564 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 565 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 566 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 567 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 568 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 569 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 570 | |
| 571 | /* We manually control the domain here and pretend that it |
| 572 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 573 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 574 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 575 | if (copy_from_user(vaddr, user_data, args->size)) |
| 576 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 577 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 578 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 579 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 580 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 581 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 582 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 585 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 586 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 587 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 591 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 592 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 593 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 596 | static int |
| 597 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 598 | struct drm_i915_private *dev_priv, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 599 | uint64_t size, |
| 600 | uint32_t *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 601 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 602 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 603 | int ret; |
| 604 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 605 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 606 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 607 | if (size == 0) |
| 608 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 609 | |
| 610 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 611 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 612 | if (IS_ERR(obj)) |
| 613 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 614 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 615 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 616 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 617 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 618 | if (ret) |
| 619 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 620 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 621 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 625 | int |
| 626 | i915_gem_dumb_create(struct drm_file *file, |
| 627 | struct drm_device *dev, |
| 628 | struct drm_mode_create_dumb *args) |
| 629 | { |
| 630 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 631 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 632 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 633 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 634 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 635 | } |
| 636 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 637 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 638 | { |
| 639 | return !(obj->cache_level == I915_CACHE_NONE || |
| 640 | obj->cache_level == I915_CACHE_WT); |
| 641 | } |
| 642 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 643 | /** |
| 644 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 645 | * @dev: drm device pointer |
| 646 | * @data: ioctl data blob |
| 647 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 648 | */ |
| 649 | int |
| 650 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 651 | struct drm_file *file) |
| 652 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 653 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 654 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 655 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 656 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 657 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 658 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 659 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 660 | } |
| 661 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 662 | static inline enum fb_op_origin |
| 663 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 664 | { |
| 665 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 666 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 667 | } |
| 668 | |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 669 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 670 | { |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 671 | /* |
| 672 | * No actual flushing is required for the GTT write domain for reads |
| 673 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 674 | * as far as we know, so there's no chipset flush. It also doesn't |
| 675 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 676 | * |
| 677 | * However, we do have to enforce the order so that all writes through |
| 678 | * the GTT land before any writes to the device, such as updates to |
| 679 | * the GATT itself. |
| 680 | * |
| 681 | * We also have to wait a bit for the writes to land from the GTT. |
| 682 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 683 | * timing. This issue has only been observed when switching quickly |
| 684 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 685 | * and it appears to only affect discrete GTT blocks (i.e. on LLC |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 686 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 687 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 688 | */ |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 689 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 690 | wmb(); |
| 691 | |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 692 | intel_runtime_pm_get(dev_priv); |
| 693 | spin_lock_irq(&dev_priv->uncore.lock); |
| 694 | |
| 695 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
| 696 | |
| 697 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 698 | intel_runtime_pm_put(dev_priv); |
| 699 | } |
| 700 | |
| 701 | static void |
| 702 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 703 | { |
| 704 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 705 | struct i915_vma *vma; |
| 706 | |
| 707 | if (!(obj->base.write_domain & flush_domains)) |
| 708 | return; |
| 709 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 710 | switch (obj->base.write_domain) { |
| 711 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 712 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 713 | |
| 714 | intel_fb_obj_flush(obj, |
| 715 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 716 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame^] | 717 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 718 | if (vma->iomap) |
| 719 | continue; |
| 720 | |
| 721 | i915_vma_unset_ggtt_write(vma); |
| 722 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 723 | break; |
| 724 | |
| 725 | case I915_GEM_DOMAIN_CPU: |
| 726 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 727 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 728 | |
| 729 | case I915_GEM_DOMAIN_RENDER: |
| 730 | if (gpu_write_needs_clflush(obj)) |
| 731 | obj->cache_dirty = true; |
| 732 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | obj->base.write_domain = 0; |
| 736 | } |
| 737 | |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 738 | static inline int |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 739 | __copy_to_user_swizzled(char __user *cpu_vaddr, |
| 740 | const char *gpu_vaddr, int gpu_offset, |
| 741 | int length) |
| 742 | { |
| 743 | int ret, cpu_offset = 0; |
| 744 | |
| 745 | while (length > 0) { |
| 746 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 747 | int this_length = min(cacheline_end - gpu_offset, length); |
| 748 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 749 | |
| 750 | ret = __copy_to_user(cpu_vaddr + cpu_offset, |
| 751 | gpu_vaddr + swizzled_gpu_offset, |
| 752 | this_length); |
| 753 | if (ret) |
| 754 | return ret + length; |
| 755 | |
| 756 | cpu_offset += this_length; |
| 757 | gpu_offset += this_length; |
| 758 | length -= this_length; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | static inline int |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 765 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
| 766 | const char __user *cpu_vaddr, |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 767 | int length) |
| 768 | { |
| 769 | int ret, cpu_offset = 0; |
| 770 | |
| 771 | while (length > 0) { |
| 772 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 773 | int this_length = min(cacheline_end - gpu_offset, length); |
| 774 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 775 | |
| 776 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, |
| 777 | cpu_vaddr + cpu_offset, |
| 778 | this_length); |
| 779 | if (ret) |
| 780 | return ret + length; |
| 781 | |
| 782 | cpu_offset += this_length; |
| 783 | gpu_offset += this_length; |
| 784 | length -= this_length; |
| 785 | } |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 790 | /* |
| 791 | * Pins the specified object's pages and synchronizes the object with |
| 792 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 793 | * flush the object from the CPU cache. |
| 794 | */ |
| 795 | 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] | 796 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 797 | { |
| 798 | int ret; |
| 799 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 800 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 801 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 802 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 803 | if (!i915_gem_object_has_struct_page(obj)) |
| 804 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 805 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 806 | ret = i915_gem_object_wait(obj, |
| 807 | I915_WAIT_INTERRUPTIBLE | |
| 808 | I915_WAIT_LOCKED, |
| 809 | MAX_SCHEDULE_TIMEOUT, |
| 810 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 811 | if (ret) |
| 812 | return ret; |
| 813 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 814 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 815 | if (ret) |
| 816 | return ret; |
| 817 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 818 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 819 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 820 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 821 | if (ret) |
| 822 | goto err_unpin; |
| 823 | else |
| 824 | goto out; |
| 825 | } |
| 826 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 827 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 828 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 829 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 830 | * read domain and manually flush cachelines (if required). This |
| 831 | * optimizes for the case when the gpu will dirty the data |
| 832 | * anyway again before the next pread happens. |
| 833 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 834 | if (!obj->cache_dirty && |
| 835 | !(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 836 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 837 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 838 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 839 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 840 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 841 | |
| 842 | err_unpin: |
| 843 | i915_gem_object_unpin_pages(obj); |
| 844 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 848 | unsigned int *needs_clflush) |
| 849 | { |
| 850 | int ret; |
| 851 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 852 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 853 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 854 | *needs_clflush = 0; |
| 855 | if (!i915_gem_object_has_struct_page(obj)) |
| 856 | return -ENODEV; |
| 857 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 858 | ret = i915_gem_object_wait(obj, |
| 859 | I915_WAIT_INTERRUPTIBLE | |
| 860 | I915_WAIT_LOCKED | |
| 861 | I915_WAIT_ALL, |
| 862 | MAX_SCHEDULE_TIMEOUT, |
| 863 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 864 | if (ret) |
| 865 | return ret; |
| 866 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 867 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 868 | if (ret) |
| 869 | return ret; |
| 870 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 871 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 872 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 873 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 874 | if (ret) |
| 875 | goto err_unpin; |
| 876 | else |
| 877 | goto out; |
| 878 | } |
| 879 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 880 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 881 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 882 | /* If we're not in the cpu write domain, set ourself into the |
| 883 | * gtt write domain and manually flush cachelines (as required). |
| 884 | * This optimizes for the case when the gpu will use the data |
| 885 | * right away and we therefore have to clflush anyway. |
| 886 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 887 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 888 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 889 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 890 | /* |
| 891 | * Same trick applies to invalidate partially written |
| 892 | * cachelines read before writing. |
| 893 | */ |
| 894 | if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) |
| 895 | *needs_clflush |= CLFLUSH_BEFORE; |
| 896 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 897 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 898 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 899 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 900 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 901 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 902 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 903 | |
| 904 | err_unpin: |
| 905 | i915_gem_object_unpin_pages(obj); |
| 906 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 909 | static void |
| 910 | shmem_clflush_swizzled_range(char *addr, unsigned long length, |
| 911 | bool swizzled) |
| 912 | { |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 913 | if (unlikely(swizzled)) { |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 914 | unsigned long start = (unsigned long) addr; |
| 915 | unsigned long end = (unsigned long) addr + length; |
| 916 | |
| 917 | /* For swizzling simply ensure that we always flush both |
| 918 | * channels. Lame, but simple and it works. Swizzled |
| 919 | * pwrite/pread is far from a hotpath - current userspace |
| 920 | * doesn't use it at all. */ |
| 921 | start = round_down(start, 128); |
| 922 | end = round_up(end, 128); |
| 923 | |
| 924 | drm_clflush_virt_range((void *)start, end - start); |
| 925 | } else { |
| 926 | drm_clflush_virt_range(addr, length); |
| 927 | } |
| 928 | |
| 929 | } |
| 930 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 931 | /* Only difference to the fast-path function is that this can handle bit17 |
| 932 | * and uses non-atomic copy and kmap functions. */ |
| 933 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 934 | shmem_pread_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 935 | char __user *user_data, |
| 936 | bool page_do_bit17_swizzling, bool needs_clflush) |
| 937 | { |
| 938 | char *vaddr; |
| 939 | int ret; |
| 940 | |
| 941 | vaddr = kmap(page); |
| 942 | if (needs_clflush) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 943 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 944 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 945 | |
| 946 | if (page_do_bit17_swizzling) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 947 | ret = __copy_to_user_swizzled(user_data, vaddr, offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 948 | else |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 949 | ret = __copy_to_user(user_data, vaddr + offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 950 | kunmap(page); |
| 951 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 952 | return ret ? - EFAULT : 0; |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 955 | static int |
| 956 | shmem_pread(struct page *page, int offset, int length, char __user *user_data, |
| 957 | bool page_do_bit17_swizzling, bool needs_clflush) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 958 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 959 | int ret; |
| 960 | |
| 961 | ret = -ENODEV; |
| 962 | if (!page_do_bit17_swizzling) { |
| 963 | char *vaddr = kmap_atomic(page); |
| 964 | |
| 965 | if (needs_clflush) |
| 966 | drm_clflush_virt_range(vaddr + offset, length); |
| 967 | ret = __copy_to_user_inatomic(user_data, vaddr + offset, length); |
| 968 | kunmap_atomic(vaddr); |
| 969 | } |
| 970 | if (ret == 0) |
| 971 | return 0; |
| 972 | |
| 973 | return shmem_pread_slow(page, offset, length, user_data, |
| 974 | page_do_bit17_swizzling, needs_clflush); |
| 975 | } |
| 976 | |
| 977 | static int |
| 978 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 979 | struct drm_i915_gem_pread *args) |
| 980 | { |
| 981 | char __user *user_data; |
| 982 | u64 remain; |
| 983 | unsigned int obj_do_bit17_swizzling; |
| 984 | unsigned int needs_clflush; |
| 985 | unsigned int idx, offset; |
| 986 | int ret; |
| 987 | |
| 988 | obj_do_bit17_swizzling = 0; |
| 989 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 990 | obj_do_bit17_swizzling = BIT(17); |
| 991 | |
| 992 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 993 | if (ret) |
| 994 | return ret; |
| 995 | |
| 996 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 997 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 998 | if (ret) |
| 999 | return ret; |
| 1000 | |
| 1001 | remain = args->size; |
| 1002 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1003 | offset = offset_in_page(args->offset); |
| 1004 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1005 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1006 | int length; |
| 1007 | |
| 1008 | length = remain; |
| 1009 | if (offset + length > PAGE_SIZE) |
| 1010 | length = PAGE_SIZE - offset; |
| 1011 | |
| 1012 | ret = shmem_pread(page, offset, length, user_data, |
| 1013 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1014 | needs_clflush); |
| 1015 | if (ret) |
| 1016 | break; |
| 1017 | |
| 1018 | remain -= length; |
| 1019 | user_data += length; |
| 1020 | offset = 0; |
| 1021 | } |
| 1022 | |
| 1023 | i915_gem_obj_finish_shmem_access(obj); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | static inline bool |
| 1028 | gtt_user_read(struct io_mapping *mapping, |
| 1029 | loff_t base, int offset, |
| 1030 | char __user *user_data, int length) |
| 1031 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1032 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1033 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1034 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1035 | /* 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] | 1036 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1037 | unwritten = __copy_to_user_inatomic(user_data, |
| 1038 | (void __force *)vaddr + offset, |
| 1039 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1040 | io_mapping_unmap_atomic(vaddr); |
| 1041 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1042 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1043 | unwritten = copy_to_user(user_data, |
| 1044 | (void __force *)vaddr + offset, |
| 1045 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1046 | io_mapping_unmap(vaddr); |
| 1047 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1048 | return unwritten; |
| 1049 | } |
| 1050 | |
| 1051 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1052 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1053 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1054 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1055 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1056 | struct i915_ggtt *ggtt = &i915->ggtt; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1057 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1058 | struct i915_vma *vma; |
| 1059 | void __user *user_data; |
| 1060 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1061 | int ret; |
| 1062 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1063 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1064 | if (ret) |
| 1065 | return ret; |
| 1066 | |
| 1067 | intel_runtime_pm_get(i915); |
| 1068 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1069 | PIN_MAPPABLE | |
| 1070 | PIN_NONFAULT | |
| 1071 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1072 | if (!IS_ERR(vma)) { |
| 1073 | node.start = i915_ggtt_offset(vma); |
| 1074 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1075 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1076 | if (ret) { |
| 1077 | i915_vma_unpin(vma); |
| 1078 | vma = ERR_PTR(ret); |
| 1079 | } |
| 1080 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1081 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1082 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1083 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1084 | goto out_unlock; |
| 1085 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1089 | if (ret) |
| 1090 | goto out_unpin; |
| 1091 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1092 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1093 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1094 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1095 | remain = args->size; |
| 1096 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1097 | |
| 1098 | while (remain > 0) { |
| 1099 | /* Operation in this page |
| 1100 | * |
| 1101 | * page_base = page offset within aperture |
| 1102 | * page_offset = offset within page |
| 1103 | * page_length = bytes to copy for this page |
| 1104 | */ |
| 1105 | u32 page_base = node.start; |
| 1106 | unsigned page_offset = offset_in_page(offset); |
| 1107 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1108 | page_length = remain < page_length ? remain : page_length; |
| 1109 | if (node.allocated) { |
| 1110 | wmb(); |
| 1111 | ggtt->base.insert_page(&ggtt->base, |
| 1112 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1113 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1114 | wmb(); |
| 1115 | } else { |
| 1116 | page_base += offset & PAGE_MASK; |
| 1117 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1118 | |
| 1119 | if (gtt_user_read(&ggtt->mappable, page_base, page_offset, |
| 1120 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1121 | ret = -EFAULT; |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | remain -= page_length; |
| 1126 | user_data += page_length; |
| 1127 | offset += page_length; |
| 1128 | } |
| 1129 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1130 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1131 | out_unpin: |
| 1132 | if (node.allocated) { |
| 1133 | wmb(); |
| 1134 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1135 | node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1136 | remove_mappable_node(&node); |
| 1137 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1138 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1139 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1140 | out_unlock: |
| 1141 | intel_runtime_pm_put(i915); |
| 1142 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1143 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1144 | return ret; |
| 1145 | } |
| 1146 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1147 | /** |
| 1148 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1149 | * @dev: drm device pointer |
| 1150 | * @data: ioctl data blob |
| 1151 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1152 | * |
| 1153 | * On error, the contents of *data are undefined. |
| 1154 | */ |
| 1155 | int |
| 1156 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1157 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1158 | { |
| 1159 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1160 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1161 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1162 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1163 | if (args->size == 0) |
| 1164 | return 0; |
| 1165 | |
| 1166 | if (!access_ok(VERIFY_WRITE, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1167 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1168 | args->size)) |
| 1169 | return -EFAULT; |
| 1170 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1171 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1172 | if (!obj) |
| 1173 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1174 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1175 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1176 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1177 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1178 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1179 | } |
| 1180 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1181 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1182 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1183 | ret = i915_gem_object_wait(obj, |
| 1184 | I915_WAIT_INTERRUPTIBLE, |
| 1185 | MAX_SCHEDULE_TIMEOUT, |
| 1186 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1187 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1188 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1189 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1190 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1191 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1192 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1193 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1194 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1195 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1196 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1197 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1198 | i915_gem_object_unpin_pages(obj); |
| 1199 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1200 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1201 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1204 | /* This is the fast write path which cannot handle |
| 1205 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1206 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1207 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1208 | static inline bool |
| 1209 | ggtt_write(struct io_mapping *mapping, |
| 1210 | loff_t base, int offset, |
| 1211 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1212 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1213 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1214 | unsigned long unwritten; |
| 1215 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1216 | /* 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] | 1217 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1218 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1219 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1220 | io_mapping_unmap_atomic(vaddr); |
| 1221 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1222 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1223 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1224 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1225 | io_mapping_unmap(vaddr); |
| 1226 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1227 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1228 | return unwritten; |
| 1229 | } |
| 1230 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1231 | /** |
| 1232 | * This is the fast pwrite path, where we copy the data directly from the |
| 1233 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1234 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1235 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1236 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1237 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1238 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1239 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1240 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1241 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1242 | struct i915_ggtt *ggtt = &i915->ggtt; |
| 1243 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1244 | struct i915_vma *vma; |
| 1245 | u64 remain, offset; |
| 1246 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1247 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1248 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1249 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1250 | if (ret) |
| 1251 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1252 | |
Chris Wilson | 8bd8181 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1253 | if (i915_gem_object_has_struct_page(obj)) { |
| 1254 | /* |
| 1255 | * Avoid waking the device up if we can fallback, as |
| 1256 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1257 | * depending on PCI sleeps and our own resume time). |
| 1258 | * This easily dwarfs any performance advantage from |
| 1259 | * using the cache bypass of indirect GGTT access. |
| 1260 | */ |
| 1261 | if (!intel_runtime_pm_get_if_in_use(i915)) { |
| 1262 | ret = -EFAULT; |
| 1263 | goto out_unlock; |
| 1264 | } |
| 1265 | } else { |
| 1266 | /* No backing pages, no fallback, we must force GGTT access */ |
| 1267 | intel_runtime_pm_get(i915); |
| 1268 | } |
| 1269 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1270 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1271 | PIN_MAPPABLE | |
| 1272 | PIN_NONFAULT | |
| 1273 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1274 | if (!IS_ERR(vma)) { |
| 1275 | node.start = i915_ggtt_offset(vma); |
| 1276 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1277 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1278 | if (ret) { |
| 1279 | i915_vma_unpin(vma); |
| 1280 | vma = ERR_PTR(ret); |
| 1281 | } |
| 1282 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1283 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1284 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1285 | if (ret) |
Chris Wilson | 8bd8181 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1286 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1287 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1288 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1289 | |
| 1290 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1291 | if (ret) |
| 1292 | goto out_unpin; |
| 1293 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1294 | mutex_unlock(&i915->drm.struct_mutex); |
| 1295 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1296 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1297 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1298 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1299 | offset = args->offset; |
| 1300 | remain = args->size; |
| 1301 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1302 | /* Operation in this page |
| 1303 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1304 | * page_base = page offset within aperture |
| 1305 | * page_offset = offset within page |
| 1306 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1307 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1308 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1309 | unsigned int page_offset = offset_in_page(offset); |
| 1310 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1311 | page_length = remain < page_length ? remain : page_length; |
| 1312 | if (node.allocated) { |
| 1313 | wmb(); /* flush the write before we modify the GGTT */ |
| 1314 | ggtt->base.insert_page(&ggtt->base, |
| 1315 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1316 | node.start, I915_CACHE_NONE, 0); |
| 1317 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1318 | } else { |
| 1319 | page_base += offset & PAGE_MASK; |
| 1320 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1321 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1322 | * source page isn't available. Return the error and we'll |
| 1323 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1324 | * If the object is non-shmem backed, we retry again with the |
| 1325 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1326 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1327 | if (ggtt_write(&ggtt->mappable, page_base, page_offset, |
| 1328 | user_data, page_length)) { |
| 1329 | ret = -EFAULT; |
| 1330 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1331 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1332 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1333 | remain -= page_length; |
| 1334 | user_data += page_length; |
| 1335 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1336 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1337 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1338 | |
| 1339 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1340 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1341 | if (node.allocated) { |
| 1342 | wmb(); |
| 1343 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1344 | node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1345 | remove_mappable_node(&node); |
| 1346 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1347 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1348 | } |
Chris Wilson | 8bd8181 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1349 | out_rpm: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1350 | intel_runtime_pm_put(i915); |
Chris Wilson | 8bd8181 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1351 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1352 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1353 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1356 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1357 | shmem_pwrite_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1358 | char __user *user_data, |
| 1359 | bool page_do_bit17_swizzling, |
| 1360 | bool needs_clflush_before, |
| 1361 | bool needs_clflush_after) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1362 | { |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1363 | char *vaddr; |
| 1364 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1365 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1366 | vaddr = kmap(page); |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 1367 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1368 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1369 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1370 | if (page_do_bit17_swizzling) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1371 | ret = __copy_from_user_swizzled(vaddr, offset, user_data, |
| 1372 | length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1373 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1374 | ret = __copy_from_user(vaddr + offset, user_data, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1375 | if (needs_clflush_after) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1376 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1377 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1378 | kunmap(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1379 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 1380 | return ret ? -EFAULT : 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1383 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1384 | * Flushes invalid cachelines before writing to the target if |
| 1385 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1386 | * writing if needs_clflush is set. |
| 1387 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1388 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1389 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
| 1390 | bool page_do_bit17_swizzling, |
| 1391 | bool needs_clflush_before, |
| 1392 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1393 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1394 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1395 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1396 | ret = -ENODEV; |
| 1397 | if (!page_do_bit17_swizzling) { |
| 1398 | char *vaddr = kmap_atomic(page); |
| 1399 | |
| 1400 | if (needs_clflush_before) |
| 1401 | drm_clflush_virt_range(vaddr + offset, len); |
| 1402 | ret = __copy_from_user_inatomic(vaddr + offset, user_data, len); |
| 1403 | if (needs_clflush_after) |
| 1404 | drm_clflush_virt_range(vaddr + offset, len); |
| 1405 | |
| 1406 | kunmap_atomic(vaddr); |
| 1407 | } |
| 1408 | if (ret == 0) |
| 1409 | return ret; |
| 1410 | |
| 1411 | return shmem_pwrite_slow(page, offset, len, user_data, |
| 1412 | page_do_bit17_swizzling, |
| 1413 | needs_clflush_before, |
| 1414 | needs_clflush_after); |
| 1415 | } |
| 1416 | |
| 1417 | static int |
| 1418 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1419 | const struct drm_i915_gem_pwrite *args) |
| 1420 | { |
| 1421 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1422 | void __user *user_data; |
| 1423 | u64 remain; |
| 1424 | unsigned int obj_do_bit17_swizzling; |
| 1425 | unsigned int partial_cacheline_write; |
| 1426 | unsigned int needs_clflush; |
| 1427 | unsigned int offset, idx; |
| 1428 | int ret; |
| 1429 | |
| 1430 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1431 | if (ret) |
| 1432 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1433 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1434 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1435 | mutex_unlock(&i915->drm.struct_mutex); |
| 1436 | if (ret) |
| 1437 | return ret; |
| 1438 | |
| 1439 | obj_do_bit17_swizzling = 0; |
| 1440 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 1441 | obj_do_bit17_swizzling = BIT(17); |
| 1442 | |
| 1443 | /* If we don't overwrite a cacheline completely we need to be |
| 1444 | * careful to have up-to-date data by first clflushing. Don't |
| 1445 | * overcomplicate things and flush the entire patch. |
| 1446 | */ |
| 1447 | partial_cacheline_write = 0; |
| 1448 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1449 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1450 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1451 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1452 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1453 | offset = offset_in_page(args->offset); |
| 1454 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1455 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1456 | int length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1457 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1458 | length = remain; |
| 1459 | if (offset + length > PAGE_SIZE) |
| 1460 | length = PAGE_SIZE - offset; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1461 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1462 | ret = shmem_pwrite(page, offset, length, user_data, |
| 1463 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1464 | (offset | length) & partial_cacheline_write, |
| 1465 | needs_clflush & CLFLUSH_AFTER); |
| 1466 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1467 | break; |
| 1468 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1469 | remain -= length; |
| 1470 | user_data += length; |
| 1471 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1474 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1475 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1476 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1481 | * @dev: drm device |
| 1482 | * @data: ioctl data blob |
| 1483 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1484 | * |
| 1485 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1486 | */ |
| 1487 | int |
| 1488 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1489 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1490 | { |
| 1491 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1492 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1493 | int ret; |
| 1494 | |
| 1495 | if (args->size == 0) |
| 1496 | return 0; |
| 1497 | |
| 1498 | if (!access_ok(VERIFY_READ, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1499 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1500 | args->size)) |
| 1501 | return -EFAULT; |
| 1502 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1503 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1504 | if (!obj) |
| 1505 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1506 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1507 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1508 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1509 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1510 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1511 | } |
| 1512 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1513 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1514 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1515 | ret = -ENODEV; |
| 1516 | if (obj->ops->pwrite) |
| 1517 | ret = obj->ops->pwrite(obj, args); |
| 1518 | if (ret != -ENODEV) |
| 1519 | goto err; |
| 1520 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1521 | ret = i915_gem_object_wait(obj, |
| 1522 | I915_WAIT_INTERRUPTIBLE | |
| 1523 | I915_WAIT_ALL, |
| 1524 | MAX_SCHEDULE_TIMEOUT, |
| 1525 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1526 | if (ret) |
| 1527 | goto err; |
| 1528 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1529 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1530 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1531 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1532 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1533 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1534 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1535 | * it would end up going through the fenced access, and we'll get |
| 1536 | * different detiling behavior between reading and writing. |
| 1537 | * pread/pwrite currently are reading and writing from the CPU |
| 1538 | * perspective, requiring manual detiling by the client. |
| 1539 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1540 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1541 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1542 | /* Note that the gtt paths might fail with non-page-backed user |
| 1543 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1544 | * textures). Fallback to the shmem path in that case. |
| 1545 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1546 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1547 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1548 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1549 | if (obj->phys_handle) |
| 1550 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1551 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1552 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1553 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1554 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1555 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1556 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1557 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1558 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1561 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1562 | { |
| 1563 | struct drm_i915_private *i915; |
| 1564 | struct list_head *list; |
| 1565 | struct i915_vma *vma; |
| 1566 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1567 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1568 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame^] | 1569 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1570 | if (i915_vma_is_active(vma)) |
| 1571 | continue; |
| 1572 | |
| 1573 | if (!drm_mm_node_allocated(&vma->node)) |
| 1574 | continue; |
| 1575 | |
| 1576 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 1577 | } |
| 1578 | |
| 1579 | i915 = to_i915(obj->base.dev); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1580 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1581 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1582 | list_move_tail(&obj->mm.link, list); |
| 1583 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1584 | } |
| 1585 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1586 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1587 | * Called when user space prepares to use an object with the CPU, either |
| 1588 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1589 | * @dev: drm device |
| 1590 | * @data: ioctl data blob |
| 1591 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1592 | */ |
| 1593 | int |
| 1594 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1595 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1596 | { |
| 1597 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1598 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1599 | uint32_t read_domains = args->read_domains; |
| 1600 | uint32_t write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1601 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1602 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1603 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1604 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1605 | return -EINVAL; |
| 1606 | |
| 1607 | /* Having something in the write domain implies it's in the read |
| 1608 | * domain, and only that read domain. Enforce that in the request. |
| 1609 | */ |
| 1610 | if (write_domain != 0 && read_domains != write_domain) |
| 1611 | return -EINVAL; |
| 1612 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1613 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1614 | if (!obj) |
| 1615 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1616 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1617 | /* Try to flush the object off the GPU without holding the lock. |
| 1618 | * We will repeat the flush holding the lock in the normal manner |
| 1619 | * to catch cases where we are gazumped. |
| 1620 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1621 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1622 | I915_WAIT_INTERRUPTIBLE | |
| 1623 | (write_domain ? I915_WAIT_ALL : 0), |
| 1624 | MAX_SCHEDULE_TIMEOUT, |
| 1625 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1626 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1627 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1628 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1629 | /* |
| 1630 | * Proxy objects do not control access to the backing storage, ergo |
| 1631 | * they cannot be used as a means to manipulate the cache domain |
| 1632 | * tracking for that backing storage. The proxy object is always |
| 1633 | * considered to be outside of any cache domain. |
| 1634 | */ |
| 1635 | if (i915_gem_object_is_proxy(obj)) { |
| 1636 | err = -ENXIO; |
| 1637 | goto out; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1642 | * direct access in memory with previous cached writes through |
| 1643 | * shmemfs and that our cache domain tracking remains valid. |
| 1644 | * For example, if the obj->filp was moved to swap without us |
| 1645 | * being notified and releasing the pages, we would mistakenly |
| 1646 | * continue to assume that the obj remained out of the CPU cached |
| 1647 | * domain. |
| 1648 | */ |
| 1649 | err = i915_gem_object_pin_pages(obj); |
| 1650 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1651 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1652 | |
| 1653 | err = i915_mutex_lock_interruptible(dev); |
| 1654 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1655 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1656 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1657 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1658 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1659 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1660 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1661 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1662 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1663 | |
| 1664 | /* And bump the LRU for this access */ |
| 1665 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1666 | |
| 1667 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1668 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1669 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1670 | intel_fb_obj_invalidate(obj, |
| 1671 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1672 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1673 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1674 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1675 | out: |
| 1676 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1677 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | /** |
| 1681 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1682 | * @dev: drm device |
| 1683 | * @data: ioctl data blob |
| 1684 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1685 | */ |
| 1686 | int |
| 1687 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1688 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1689 | { |
| 1690 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1691 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1692 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1693 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1694 | if (!obj) |
| 1695 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1696 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1697 | /* |
| 1698 | * Proxy objects are barred from CPU access, so there is no |
| 1699 | * need to ban sw_finish as it is a nop. |
| 1700 | */ |
| 1701 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1702 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1703 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1704 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1705 | |
| 1706 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1710 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1711 | * it is mapped to. |
| 1712 | * @dev: drm device |
| 1713 | * @data: ioctl data blob |
| 1714 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1715 | * |
| 1716 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1717 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1718 | * |
| 1719 | * IMPORTANT: |
| 1720 | * |
| 1721 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1722 | * mmap support, please don't implement mmap support like here. The modern way |
| 1723 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1724 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1725 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1726 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1727 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1728 | */ |
| 1729 | int |
| 1730 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1731 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1732 | { |
| 1733 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1734 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1735 | unsigned long addr; |
| 1736 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1737 | if (args->flags & ~(I915_MMAP_WC)) |
| 1738 | return -EINVAL; |
| 1739 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1740 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1741 | return -ENODEV; |
| 1742 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1743 | obj = i915_gem_object_lookup(file, args->handle); |
| 1744 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1745 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1746 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1747 | /* prime objects have no backing filp to GEM mmap |
| 1748 | * pages from. |
| 1749 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1750 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1751 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1752 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1753 | } |
| 1754 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1755 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1756 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1757 | args->offset); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1758 | if (args->flags & I915_MMAP_WC) { |
| 1759 | struct mm_struct *mm = current->mm; |
| 1760 | struct vm_area_struct *vma; |
| 1761 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1762 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1763 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1764 | return -EINTR; |
| 1765 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1766 | vma = find_vma(mm, addr); |
| 1767 | if (vma) |
| 1768 | vma->vm_page_prot = |
| 1769 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1770 | else |
| 1771 | addr = -ENOMEM; |
| 1772 | up_write(&mm->mmap_sem); |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1773 | |
| 1774 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1775 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1776 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1777 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1778 | if (IS_ERR((void *)addr)) |
| 1779 | return addr; |
| 1780 | |
| 1781 | args->addr_ptr = (uint64_t) addr; |
| 1782 | |
| 1783 | return 0; |
| 1784 | } |
| 1785 | |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1786 | static unsigned int tile_row_pages(struct drm_i915_gem_object *obj) |
| 1787 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1788 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1789 | } |
| 1790 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1791 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1792 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1793 | * |
| 1794 | * A history of the GTT mmap interface: |
| 1795 | * |
| 1796 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1797 | * aligned and suitable for fencing, and still fit into the available |
| 1798 | * mappable space left by the pinned display objects. A classic problem |
| 1799 | * we called the page-fault-of-doom where we would ping-pong between |
| 1800 | * two objects that could not fit inside the GTT and so the memcpy |
| 1801 | * would page one object in at the expense of the other between every |
| 1802 | * single byte. |
| 1803 | * |
| 1804 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1805 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1806 | * object is too large for the available space (or simply too large |
| 1807 | * for the mappable aperture!), a view is created instead and faulted |
| 1808 | * into userspace. (This view is aligned and sized appropriately for |
| 1809 | * fenced access.) |
| 1810 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1811 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1812 | * delayed writes via GTT before performing direct access via WC. |
| 1813 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1814 | * Restrictions: |
| 1815 | * |
| 1816 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1817 | * hangs on some architectures, corruption on others. An attempt to service |
| 1818 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1819 | * |
| 1820 | * * the object must be able to fit into RAM (physical memory, though no |
| 1821 | * limited to the mappable aperture). |
| 1822 | * |
| 1823 | * |
| 1824 | * Caveats: |
| 1825 | * |
| 1826 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1827 | * all data to system memory. Subsequent access will not be synchronized. |
| 1828 | * |
| 1829 | * * all mappings are revoked on runtime device suspend. |
| 1830 | * |
| 1831 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1832 | * (older machines require fence register for display and blitter access |
| 1833 | * as well). Contention of the fence registers will cause the previous users |
| 1834 | * to be unmapped and any new access will generate new page faults. |
| 1835 | * |
| 1836 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1837 | * rather than the expected SIGSEGV. |
| 1838 | */ |
| 1839 | int i915_gem_mmap_gtt_version(void) |
| 1840 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1841 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1842 | } |
| 1843 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1844 | static inline struct i915_ggtt_view |
| 1845 | compute_partial_view(struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1846 | pgoff_t page_offset, |
| 1847 | unsigned int chunk) |
| 1848 | { |
| 1849 | struct i915_ggtt_view view; |
| 1850 | |
| 1851 | if (i915_gem_object_is_tiled(obj)) |
| 1852 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1853 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1854 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1855 | view.partial.offset = rounddown(page_offset, chunk); |
| 1856 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1857 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1858 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1859 | |
| 1860 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1861 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1862 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1863 | |
| 1864 | return view; |
| 1865 | } |
| 1866 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1867 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1868 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1869 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1870 | * |
| 1871 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1872 | * from userspace. The fault handler takes care of binding the object to |
| 1873 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1874 | * only if needed based on whether the old reg is still valid or the object |
| 1875 | * is tiled) and inserting a new PTE into the faulting process. |
| 1876 | * |
| 1877 | * Note that the faulting process may involve evicting existing objects |
| 1878 | * from the GTT and/or fence registers to make room. So performance may |
| 1879 | * suffer if the GTT working set is large or there are few fence registers |
| 1880 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1881 | * |
| 1882 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1883 | * 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] | 1884 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1885 | int i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1886 | { |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1887 | #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1888 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1889 | 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] | 1890 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1891 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1892 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1893 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1894 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1895 | pgoff_t page_offset; |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1896 | unsigned int flags; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1897 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1898 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1899 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1900 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1901 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1902 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1903 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1904 | /* 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] | 1905 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1906 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1907 | * where we are gazumped. |
| 1908 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1909 | ret = i915_gem_object_wait(obj, |
| 1910 | I915_WAIT_INTERRUPTIBLE, |
| 1911 | MAX_SCHEDULE_TIMEOUT, |
| 1912 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1913 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1914 | goto err; |
| 1915 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1916 | ret = i915_gem_object_pin_pages(obj); |
| 1917 | if (ret) |
| 1918 | goto err; |
| 1919 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1920 | intel_runtime_pm_get(dev_priv); |
| 1921 | |
| 1922 | ret = i915_mutex_lock_interruptible(dev); |
| 1923 | if (ret) |
| 1924 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1925 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1926 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1927 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1928 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1929 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1932 | /* If the object is smaller than a couple of partial vma, it is |
| 1933 | * not worth only creating a single partial vma - we may as well |
| 1934 | * clear enough space for the full object. |
| 1935 | */ |
| 1936 | flags = PIN_MAPPABLE; |
| 1937 | if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT) |
| 1938 | flags |= PIN_NONBLOCK | PIN_NONFAULT; |
| 1939 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1940 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1941 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1942 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1943 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1944 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1945 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1946 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1947 | /* Userspace is now writing through an untracked VMA, abandon |
| 1948 | * all hope that the hardware is able to track future writes. |
| 1949 | */ |
| 1950 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1951 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1952 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); |
| 1953 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1954 | if (IS_ERR(vma)) { |
| 1955 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1956 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1957 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1958 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1959 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1960 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1961 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1962 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1963 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1964 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1965 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1966 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1967 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1968 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1969 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1970 | (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT, |
| 1971 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
| 1972 | &ggtt->mappable); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1973 | if (ret) |
| 1974 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1975 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1976 | /* Mark as being mmapped into userspace for later revocation */ |
| 1977 | assert_rpm_wakelock_held(dev_priv); |
| 1978 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1979 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1980 | GEM_BUG_ON(!obj->userfault_count); |
| 1981 | |
Chris Wilson | 7125397 | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1982 | i915_vma_set_ggtt_write(vma); |
| 1983 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1984 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1985 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1986 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1987 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1988 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1989 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1990 | err_rpm: |
| 1991 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1992 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1993 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1994 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1995 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1996 | /* |
| 1997 | * We eat errors when the gpu is terminally wedged to avoid |
| 1998 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1999 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 2000 | * and so needs to be reported. |
| 2001 | */ |
| 2002 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2003 | ret = VM_FAULT_SIGBUS; |
| 2004 | break; |
| 2005 | } |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 2006 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 2007 | /* |
| 2008 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 2009 | * handler to reset everything when re-faulting in |
| 2010 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2011 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 2012 | case 0: |
| 2013 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 2014 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 2015 | case -EBUSY: |
| 2016 | /* |
| 2017 | * EBUSY is ok: this just means that another thread |
| 2018 | * already did the job. |
| 2019 | */ |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2020 | ret = VM_FAULT_NOPAGE; |
| 2021 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2022 | case -ENOMEM: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2023 | ret = VM_FAULT_OOM; |
| 2024 | break; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2025 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2026 | case -EFAULT: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2027 | ret = VM_FAULT_SIGBUS; |
| 2028 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2029 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2030 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2031 | ret = VM_FAULT_SIGBUS; |
| 2032 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2033 | } |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2034 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2035 | } |
| 2036 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2037 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2038 | { |
| 2039 | struct i915_vma *vma; |
| 2040 | |
| 2041 | GEM_BUG_ON(!obj->userfault_count); |
| 2042 | |
| 2043 | obj->userfault_count = 0; |
| 2044 | list_del(&obj->userfault_link); |
| 2045 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2046 | obj->base.dev->anon_inode->i_mapping); |
| 2047 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame^] | 2048 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2049 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2050 | } |
| 2051 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2052 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2053 | * i915_gem_release_mmap - remove physical page mappings |
| 2054 | * @obj: obj in question |
| 2055 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2056 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2057 | * relinquish ownership of the pages back to the system. |
| 2058 | * |
| 2059 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2060 | * object through the GTT and then lose the fence register due to |
| 2061 | * resource pressure. Similarly if the object has been moved out of the |
| 2062 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2063 | * mapping will then trigger a page fault on the next user access, allowing |
| 2064 | * fixup by i915_gem_fault(). |
| 2065 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2066 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2067 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2068 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2069 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2070 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2071 | /* Serialisation between user GTT access and our code depends upon |
| 2072 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2073 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2074 | * |
| 2075 | * Note that RPM complicates somewhat by adding an additional |
| 2076 | * requirement that operations to the GGTT be made holding the RPM |
| 2077 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2078 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2079 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2080 | intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2081 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2082 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2083 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2084 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2085 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2086 | |
| 2087 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2088 | * memory transactions from userspace before we return. The TLB |
| 2089 | * flushing implied above by changing the PTE above *should* be |
| 2090 | * sufficient, an extra barrier here just provides us with a bit |
| 2091 | * of paranoid documentation about our requirement to serialise |
| 2092 | * memory writes before touching registers / GSM. |
| 2093 | */ |
| 2094 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2095 | |
| 2096 | out: |
| 2097 | intel_runtime_pm_put(i915); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2100 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2101 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2102 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2103 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2104 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2105 | /* |
| 2106 | * Only called during RPM suspend. All users of the userfault_list |
| 2107 | * must be holding an RPM wakeref to ensure that this can not |
| 2108 | * run concurrently with themselves (and use the struct_mutex for |
| 2109 | * protection between themselves). |
| 2110 | */ |
| 2111 | |
| 2112 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2113 | &dev_priv->mm.userfault_list, userfault_link) |
| 2114 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2115 | |
| 2116 | /* The fence will be lost when the device powers down. If any were |
| 2117 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2118 | * down! All other fences will be reacquired by the user upon waking. |
| 2119 | */ |
| 2120 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2121 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2122 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2123 | /* Ideally we want to assert that the fence register is not |
| 2124 | * live at this point (i.e. that no piece of code will be |
| 2125 | * trying to write through fence + GTT, as that both violates |
| 2126 | * our tracking of activity and associated locking/barriers, |
| 2127 | * but also is illegal given that the hw is powered down). |
| 2128 | * |
| 2129 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2130 | * That is not sufficient, and we need a more fine-grained |
| 2131 | * tool if we want to have a sanity check here. |
| 2132 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2133 | |
| 2134 | if (!reg->vma) |
| 2135 | continue; |
| 2136 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2137 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2138 | reg->dirty = true; |
| 2139 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2142 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2143 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2144 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2145 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2146 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2147 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2148 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2149 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2150 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2151 | /* Attempt to reap some mmap space from dead objects */ |
| 2152 | do { |
| 2153 | err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE); |
| 2154 | if (err) |
| 2155 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2156 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2157 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2158 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2159 | if (!err) |
| 2160 | break; |
| 2161 | |
| 2162 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2163 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2164 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2168 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2169 | drm_gem_free_mmap_offset(&obj->base); |
| 2170 | } |
| 2171 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2172 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2173 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2174 | struct drm_device *dev, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2175 | uint32_t handle, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2176 | uint64_t *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2177 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2178 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2179 | int ret; |
| 2180 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2181 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2182 | if (!obj) |
| 2183 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2184 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2185 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2186 | if (ret == 0) |
| 2187 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2188 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2189 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2190 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2191 | } |
| 2192 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2193 | /** |
| 2194 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2195 | * @dev: DRM device |
| 2196 | * @data: GTT mapping ioctl data |
| 2197 | * @file: GEM object info |
| 2198 | * |
| 2199 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2200 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2201 | * up so we can get faults in the handler above. |
| 2202 | * |
| 2203 | * The fault handler will take care of binding the object into the GTT |
| 2204 | * (since it may have been evicted to make room for something), allocating |
| 2205 | * a fence register, and mapping the appropriate aperture address into |
| 2206 | * userspace. |
| 2207 | */ |
| 2208 | int |
| 2209 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2210 | struct drm_file *file) |
| 2211 | { |
| 2212 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2213 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2214 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2215 | } |
| 2216 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2217 | /* Immediately discard the backing storage */ |
| 2218 | static void |
| 2219 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2220 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2221 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2222 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2223 | if (obj->base.filp == NULL) |
| 2224 | return; |
| 2225 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2226 | /* Our goal here is to return as much of the memory as |
| 2227 | * is possible back to the system as we are called from OOM. |
| 2228 | * To do this we must instruct the shmfs to drop all of its |
| 2229 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2230 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2231 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2232 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2233 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2234 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2235 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2236 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2237 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2238 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2239 | struct address_space *mapping; |
| 2240 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2241 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2242 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2243 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2244 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2245 | case I915_MADV_DONTNEED: |
| 2246 | i915_gem_object_truncate(obj); |
| 2247 | case __I915_MADV_PURGED: |
| 2248 | return; |
| 2249 | } |
| 2250 | |
| 2251 | if (obj->base.filp == NULL) |
| 2252 | return; |
| 2253 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2254 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2255 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2256 | } |
| 2257 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2258 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2259 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2260 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2261 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2262 | struct sgt_iter sgt_iter; |
| 2263 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2264 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2265 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2266 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2267 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2268 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2269 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2270 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2271 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2272 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2273 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2274 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2275 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2276 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2277 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2278 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2279 | put_page(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2280 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2281 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2282 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2283 | sg_free_table(pages); |
| 2284 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2285 | } |
| 2286 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2287 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2288 | { |
| 2289 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2290 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2291 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2292 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2293 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2294 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2295 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2296 | } |
| 2297 | |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2298 | void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2299 | enum i915_mm_subclass subclass) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2300 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2301 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2302 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2303 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2304 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2305 | return; |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 2306 | |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 2307 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2308 | if (!i915_gem_object_has_pages(obj)) |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2309 | return; |
| 2310 | |
| 2311 | /* May be called by shrinker from within get_pages() (on another bo) */ |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2312 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2313 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) |
| 2314 | goto unlock; |
Ben Widawsky | 3e12302 | 2013-07-31 17:00:04 -0700 | [diff] [blame] | 2315 | |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2316 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
| 2317 | * array, hence protect them from being reaped by removing them from gtt |
| 2318 | * lists early. */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2319 | pages = fetch_and_zero(&obj->mm.pages); |
| 2320 | GEM_BUG_ON(!pages); |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2321 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2322 | spin_lock(&i915->mm.obj_lock); |
| 2323 | list_del(&obj->mm.link); |
| 2324 | spin_unlock(&i915->mm.obj_lock); |
| 2325 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2326 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2327 | void *ptr; |
| 2328 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2329 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2330 | if (is_vmalloc_addr(ptr)) |
| 2331 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2332 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2333 | kunmap(kmap_to_page(ptr)); |
| 2334 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2335 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2338 | __i915_gem_object_reset_page_iter(obj); |
| 2339 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2340 | if (!IS_ERR(pages)) |
| 2341 | obj->ops->put_pages(obj, pages); |
| 2342 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2343 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
| 2344 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2345 | unlock: |
| 2346 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2347 | } |
| 2348 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2349 | static bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2350 | { |
| 2351 | struct sg_table new_st; |
| 2352 | struct scatterlist *sg, *new_sg; |
| 2353 | unsigned int i; |
| 2354 | |
| 2355 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2356 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2357 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2358 | 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] | 2359 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2360 | |
| 2361 | new_sg = new_st.sgl; |
| 2362 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2363 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
| 2364 | /* called before being DMA mapped, no need to copy sg->dma_* */ |
| 2365 | new_sg = sg_next(new_sg); |
| 2366 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2367 | 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] | 2368 | |
| 2369 | sg_free_table(orig_st); |
| 2370 | |
| 2371 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2372 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2375 | 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] | 2376 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2377 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2378 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2379 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2380 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2381 | struct sg_table *st; |
| 2382 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2383 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2384 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2385 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2386 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2387 | unsigned int sg_page_sizes; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2388 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2389 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2390 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2391 | /* Assert that the object is not currently in any GPU domain. As it |
| 2392 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2393 | * a GPU cache |
| 2394 | */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2395 | GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); |
| 2396 | GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2397 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2398 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2399 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2400 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2401 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2402 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2403 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2404 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2405 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | /* Get the list of pages out of our struct file. They'll be pinned |
| 2409 | * at this point until we release them. |
| 2410 | * |
| 2411 | * Fail silently without starting the shrinker |
| 2412 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2413 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2414 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2415 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2416 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2417 | sg = st->sgl; |
| 2418 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2419 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2420 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2421 | const unsigned int shrink[] = { |
| 2422 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2423 | 0, |
| 2424 | }, *s = shrink; |
| 2425 | gfp_t gfp = noreclaim; |
| 2426 | |
| 2427 | do { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2428 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2429 | if (likely(!IS_ERR(page))) |
| 2430 | break; |
| 2431 | |
| 2432 | if (!*s) { |
| 2433 | ret = PTR_ERR(page); |
| 2434 | goto err_sg; |
| 2435 | } |
| 2436 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2437 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2438 | cond_resched(); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2439 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2440 | /* We've tried hard to allocate the memory by reaping |
| 2441 | * our own buffer, now let the real VM do its job and |
| 2442 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2443 | * |
| 2444 | * However, since graphics tend to be disposable, |
| 2445 | * defer the oom here by reporting the ENOMEM back |
| 2446 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2447 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2448 | if (!*s) { |
| 2449 | /* reclaim and warn, but no oom */ |
| 2450 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2451 | |
| 2452 | /* Our bo are always dirty and so we require |
| 2453 | * kswapd to reclaim our pages (direct reclaim |
| 2454 | * does not effectively begin pageout of our |
| 2455 | * buffers on its own). However, direct reclaim |
| 2456 | * only waits for kswapd when under allocation |
| 2457 | * congestion. So as a result __GFP_RECLAIM is |
| 2458 | * unreliable and fails to actually reclaim our |
| 2459 | * dirty pages -- unless you try over and over |
| 2460 | * again with !__GFP_NORETRY. However, we still |
| 2461 | * want to fail this allocation rather than |
| 2462 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2463 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2464 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2465 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2466 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2467 | } while (1); |
| 2468 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2469 | if (!i || |
| 2470 | sg->length >= max_segment || |
| 2471 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2472 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2473 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2474 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2475 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2476 | st->nents++; |
| 2477 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2478 | } else { |
| 2479 | sg->length += PAGE_SIZE; |
| 2480 | } |
| 2481 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2482 | |
| 2483 | /* Check that the i965g/gm workaround works. */ |
| 2484 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2485 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2486 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2487 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2488 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2489 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2490 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2491 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2492 | i915_sg_trim(st); |
| 2493 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2494 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2495 | if (ret) { |
| 2496 | /* DMA remapping failed? One possible cause is that |
| 2497 | * it could not reserve enough large entries, asking |
| 2498 | * for PAGE_SIZE chunks instead may be helpful. |
| 2499 | */ |
| 2500 | if (max_segment > PAGE_SIZE) { |
| 2501 | for_each_sgt_page(page, sgt_iter, st) |
| 2502 | put_page(page); |
| 2503 | sg_free_table(st); |
| 2504 | |
| 2505 | max_segment = PAGE_SIZE; |
| 2506 | goto rebuild_st; |
| 2507 | } else { |
| 2508 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2509 | "Failed to DMA remap %lu pages\n", |
| 2510 | page_count); |
| 2511 | goto err_pages; |
| 2512 | } |
| 2513 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2514 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2515 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2516 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2517 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2518 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2519 | |
| 2520 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2521 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2522 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2523 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2524 | err_pages: |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2525 | for_each_sgt_page(page, sgt_iter, st) |
| 2526 | put_page(page); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2527 | sg_free_table(st); |
| 2528 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2529 | |
| 2530 | /* shmemfs first checks if there is enough memory to allocate the page |
| 2531 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2532 | * ENOMEM for a genuine allocation failure. |
| 2533 | * |
| 2534 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2535 | * space and so want to translate the error from shmemfs back to our |
| 2536 | * usual understanding of ENOMEM. |
| 2537 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2538 | if (ret == -ENOSPC) |
| 2539 | ret = -ENOMEM; |
| 2540 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2541 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2545 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2546 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2547 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2548 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2549 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2550 | int i; |
| 2551 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2552 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2553 | |
| 2554 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2555 | obj->mm.get_page.sg_idx = 0; |
| 2556 | |
| 2557 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2558 | |
| 2559 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2560 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2561 | GEM_BUG_ON(obj->mm.quirked); |
| 2562 | __i915_gem_object_pin_pages(obj); |
| 2563 | obj->mm.quirked = true; |
| 2564 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2565 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2566 | GEM_BUG_ON(!sg_page_sizes); |
| 2567 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2568 | |
| 2569 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2570 | * Calculate the supported page-sizes which fit into the given |
| 2571 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2572 | * to use opportunistically when later inserting into the GTT. For |
| 2573 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2574 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2575 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2576 | */ |
| 2577 | obj->mm.page_sizes.sg = 0; |
| 2578 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2579 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2580 | obj->mm.page_sizes.sg |= BIT(i); |
| 2581 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2582 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2583 | |
| 2584 | spin_lock(&i915->mm.obj_lock); |
| 2585 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2586 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2590 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2591 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2592 | |
| 2593 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2594 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2595 | return -EFAULT; |
| 2596 | } |
| 2597 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2598 | err = obj->ops->get_pages(obj); |
| 2599 | GEM_BUG_ON(!err && IS_ERR_OR_NULL(obj->mm.pages)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2600 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2601 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2604 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2605 | * and pinned into our object. i915_gem_object_pin_pages() may be called |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2606 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2607 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2608 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2609 | * or as the object is itself released. |
| 2610 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2611 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2612 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2613 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2614 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2615 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2616 | if (err) |
| 2617 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2618 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2619 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2620 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2621 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2622 | err = ____i915_gem_object_get_pages(obj); |
| 2623 | if (err) |
| 2624 | goto unlock; |
| 2625 | |
| 2626 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2627 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2628 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2629 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2630 | unlock: |
| 2631 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2632 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2635 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2636 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2637 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2638 | { |
| 2639 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2640 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2641 | struct sgt_iter sgt_iter; |
| 2642 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2643 | struct page *stack_pages[32]; |
| 2644 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2645 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2646 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2647 | void *addr; |
| 2648 | |
| 2649 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2650 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2651 | return kmap(sg_page(sgt->sgl)); |
| 2652 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2653 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2654 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2655 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2656 | if (!pages) |
| 2657 | return NULL; |
| 2658 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2659 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2660 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2661 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2662 | |
| 2663 | /* Check that we have the expected number of pages */ |
| 2664 | GEM_BUG_ON(i != n_pages); |
| 2665 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2666 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2667 | default: |
| 2668 | MISSING_CASE(type); |
| 2669 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2670 | case I915_MAP_WB: |
| 2671 | pgprot = PAGE_KERNEL; |
| 2672 | break; |
| 2673 | case I915_MAP_WC: |
| 2674 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2675 | break; |
| 2676 | } |
| 2677 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2678 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2679 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2680 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2681 | |
| 2682 | return addr; |
| 2683 | } |
| 2684 | |
| 2685 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2686 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2687 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2688 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2689 | enum i915_map_type has_type; |
| 2690 | bool pinned; |
| 2691 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2692 | int ret; |
| 2693 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2694 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2695 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2696 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2697 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2698 | if (ret) |
| 2699 | return ERR_PTR(ret); |
| 2700 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2701 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2702 | type &= ~I915_MAP_OVERRIDE; |
| 2703 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2704 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2705 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2706 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2707 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2708 | ret = ____i915_gem_object_get_pages(obj); |
| 2709 | if (ret) |
| 2710 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2711 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2712 | smp_mb__before_atomic(); |
| 2713 | } |
| 2714 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2715 | pinned = false; |
| 2716 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2717 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2718 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2719 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2720 | if (ptr && has_type != type) { |
| 2721 | if (pinned) { |
| 2722 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2723 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2724 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2725 | |
| 2726 | if (is_vmalloc_addr(ptr)) |
| 2727 | vunmap(ptr); |
| 2728 | else |
| 2729 | kunmap(kmap_to_page(ptr)); |
| 2730 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2731 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2732 | } |
| 2733 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2734 | if (!ptr) { |
| 2735 | ptr = i915_gem_object_map(obj, type); |
| 2736 | if (!ptr) { |
| 2737 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2738 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2739 | } |
| 2740 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2741 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2744 | out_unlock: |
| 2745 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2746 | return ptr; |
| 2747 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2748 | err_unpin: |
| 2749 | atomic_dec(&obj->mm.pages_pin_count); |
| 2750 | err_unlock: |
| 2751 | ptr = ERR_PTR(ret); |
| 2752 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2753 | } |
| 2754 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2755 | static int |
| 2756 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2757 | const struct drm_i915_gem_pwrite *arg) |
| 2758 | { |
| 2759 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2760 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2761 | u64 remain, offset; |
| 2762 | unsigned int pg; |
| 2763 | |
| 2764 | /* Before we instantiate/pin the backing store for our use, we |
| 2765 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2766 | * the pagecache. We avoid the penalty of instantiating all the |
| 2767 | * pages, important if the user is just writing to a few and never |
| 2768 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2769 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2770 | * or clearing-before-use) before it is overwritten. |
| 2771 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2772 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2773 | return -ENODEV; |
| 2774 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2775 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2776 | return -EFAULT; |
| 2777 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2778 | /* Before the pages are instantiated the object is treated as being |
| 2779 | * in the CPU domain. The pages will be clflushed as required before |
| 2780 | * use, and we can freely write into the pages directly. If userspace |
| 2781 | * races pwrite with any other operation; corruption will ensue - |
| 2782 | * that is userspace's prerogative! |
| 2783 | */ |
| 2784 | |
| 2785 | remain = arg->size; |
| 2786 | offset = arg->offset; |
| 2787 | pg = offset_in_page(offset); |
| 2788 | |
| 2789 | do { |
| 2790 | unsigned int len, unwritten; |
| 2791 | struct page *page; |
| 2792 | void *data, *vaddr; |
| 2793 | int err; |
| 2794 | |
| 2795 | len = PAGE_SIZE - pg; |
| 2796 | if (len > remain) |
| 2797 | len = remain; |
| 2798 | |
| 2799 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2800 | offset, len, 0, |
| 2801 | &page, &data); |
| 2802 | if (err < 0) |
| 2803 | return err; |
| 2804 | |
| 2805 | vaddr = kmap(page); |
| 2806 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2807 | kunmap(page); |
| 2808 | |
| 2809 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2810 | offset, len, len - unwritten, |
| 2811 | page, data); |
| 2812 | if (err < 0) |
| 2813 | return err; |
| 2814 | |
| 2815 | if (unwritten) |
| 2816 | return -EFAULT; |
| 2817 | |
| 2818 | remain -= len; |
| 2819 | user_data += len; |
| 2820 | offset += len; |
| 2821 | pg = 0; |
| 2822 | } while (remain); |
| 2823 | |
| 2824 | return 0; |
| 2825 | } |
| 2826 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2827 | static bool ban_context(const struct i915_gem_context *ctx, |
| 2828 | unsigned int score) |
Mika Kuoppala | be62acb | 2013-08-30 16:19:28 +0300 | [diff] [blame] | 2829 | { |
Chris Wilson | 6095868 | 2016-12-31 11:20:11 +0000 | [diff] [blame] | 2830 | return (i915_gem_context_is_bannable(ctx) && |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2831 | score >= CONTEXT_SCORE_BAN_THRESHOLD); |
Mika Kuoppala | be62acb | 2013-08-30 16:19:28 +0300 | [diff] [blame] | 2832 | } |
| 2833 | |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2834 | static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2835 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2836 | unsigned int score; |
| 2837 | bool banned; |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2838 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2839 | atomic_inc(&ctx->guilty_count); |
| 2840 | |
| 2841 | score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score); |
| 2842 | banned = ban_context(ctx, score); |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2843 | DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n", |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2844 | ctx->name, score, yesno(banned)); |
| 2845 | if (!banned) |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2846 | return; |
| 2847 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2848 | i915_gem_context_set_banned(ctx); |
| 2849 | if (!IS_ERR_OR_NULL(ctx->file_priv)) { |
| 2850 | atomic_inc(&ctx->file_priv->context_bans); |
| 2851 | DRM_DEBUG_DRIVER("client %s has had %d context banned\n", |
| 2852 | ctx->name, atomic_read(&ctx->file_priv->context_bans)); |
| 2853 | } |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) |
| 2857 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2858 | atomic_inc(&ctx->active_count); |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2859 | } |
| 2860 | |
Chris Wilson | 8d9fc7f | 2014-02-25 17:11:23 +0200 | [diff] [blame] | 2861 | struct drm_i915_gem_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2862 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2863 | { |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2864 | struct drm_i915_gem_request *request, *active = NULL; |
| 2865 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2866 | |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2867 | /* We are called by the error capture and reset at a random |
| 2868 | * point in time. In particular, note that neither is crucially |
| 2869 | * ordered with an interrupt. After a hang, the GPU is dead and we |
| 2870 | * assume that no more writes can happen (we waited long enough for |
| 2871 | * all writes that were in transaction to be flushed) - adding an |
| 2872 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2873 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
| 2874 | */ |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2875 | spin_lock_irqsave(&engine->timeline->lock, flags); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 2876 | list_for_each_entry(request, &engine->timeline->requests, link) { |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2877 | if (__i915_gem_request_completed(request, |
| 2878 | request->global_seqno)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2879 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2880 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2881 | GEM_BUG_ON(request->engine != engine); |
Chris Wilson | c00122f3 | 2017-02-12 17:19:58 +0000 | [diff] [blame] | 2882 | GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 2883 | &request->fence.flags)); |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2884 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2885 | active = request; |
| 2886 | break; |
| 2887 | } |
| 2888 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 2889 | |
| 2890 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2891 | } |
| 2892 | |
Mika Kuoppala | bf2f043 | 2017-01-17 17:59:04 +0200 | [diff] [blame] | 2893 | static bool engine_stalled(struct intel_engine_cs *engine) |
| 2894 | { |
| 2895 | if (!engine->hangcheck.stalled) |
| 2896 | return false; |
| 2897 | |
| 2898 | /* Check for possible seqno movement after hang declaration */ |
| 2899 | if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) { |
| 2900 | DRM_DEBUG_DRIVER("%s pardoned\n", engine->name); |
| 2901 | return false; |
| 2902 | } |
| 2903 | |
| 2904 | return true; |
| 2905 | } |
| 2906 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2907 | /* |
| 2908 | * Ensure irq handler finishes, and not run again. |
| 2909 | * Also return the active request so that we only search for it once. |
| 2910 | */ |
| 2911 | struct drm_i915_gem_request * |
| 2912 | i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) |
| 2913 | { |
| 2914 | struct drm_i915_gem_request *request = NULL; |
| 2915 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 2916 | /* |
| 2917 | * During the reset sequence, we must prevent the engine from |
| 2918 | * entering RC6. As the context state is undefined until we restart |
| 2919 | * the engine, if it does enter RC6 during the reset, the state |
| 2920 | * written to the powercontext is undefined and so we may lose |
| 2921 | * GPU state upon resume, i.e. fail to restart after a reset. |
| 2922 | */ |
| 2923 | intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); |
| 2924 | |
| 2925 | /* |
| 2926 | * Prevent the signaler thread from updating the request |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2927 | * state (by calling dma_fence_signal) as we are processing |
| 2928 | * the reset. The write from the GPU of the seqno is |
| 2929 | * asynchronous and the signaler thread may see a different |
| 2930 | * value to us and declare the request complete, even though |
| 2931 | * the reset routine have picked that request as the active |
| 2932 | * (incomplete) request. This conflict is not handled |
| 2933 | * gracefully! |
| 2934 | */ |
| 2935 | kthread_park(engine->breadcrumbs.signaler); |
| 2936 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 2937 | /* |
| 2938 | * Prevent request submission to the hardware until we have |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2939 | * completed the reset in i915_gem_reset_finish(). If a request |
| 2940 | * is completed by one engine, it may then queue a request |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2941 | * to a second via its execlists->tasklet *just* as we are |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2942 | * calling engine->init_hw() and also writing the ELSP. |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2943 | * Turning off the execlists->tasklet until the reset is over |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2944 | * prevents the race. |
| 2945 | */ |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 2946 | tasklet_kill(&engine->execlists.tasklet); |
| 2947 | tasklet_disable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2948 | |
Michał Winiarski | c41937f | 2017-10-26 15:35:58 +0200 | [diff] [blame] | 2949 | /* |
| 2950 | * We're using worker to queue preemption requests from the tasklet in |
| 2951 | * GuC submission mode. |
| 2952 | * Even though tasklet was disabled, we may still have a worker queued. |
| 2953 | * Let's make sure that all workers scheduled before disabling the |
| 2954 | * tasklet are completed before continuing with the reset. |
| 2955 | */ |
| 2956 | if (engine->i915->guc.preempt_wq) |
| 2957 | flush_workqueue(engine->i915->guc.preempt_wq); |
| 2958 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2959 | if (engine->irq_seqno_barrier) |
| 2960 | engine->irq_seqno_barrier(engine); |
| 2961 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 2962 | request = i915_gem_find_active_request(engine); |
| 2963 | if (request && request->fence.error == -EIO) |
| 2964 | request = ERR_PTR(-EIO); /* Previous reset failed! */ |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2965 | |
| 2966 | return request; |
| 2967 | } |
| 2968 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2969 | int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2970 | { |
| 2971 | struct intel_engine_cs *engine; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2972 | struct drm_i915_gem_request *request; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2973 | enum intel_engine_id id; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2974 | int err = 0; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2975 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2976 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2977 | request = i915_gem_reset_prepare_engine(engine); |
| 2978 | if (IS_ERR(request)) { |
| 2979 | err = PTR_ERR(request); |
| 2980 | continue; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2981 | } |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 2982 | |
| 2983 | engine->hangcheck.active_request = request; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2984 | } |
| 2985 | |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2986 | i915_gem_revoke_fences(dev_priv); |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2987 | |
| 2988 | return err; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2989 | } |
| 2990 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2991 | static void skip_request(struct drm_i915_gem_request *request) |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2992 | { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 2993 | void *vaddr = request->ring->vaddr; |
| 2994 | u32 head; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2995 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 2996 | /* As this request likely depends on state from the lost |
| 2997 | * context, clear out all the user operations leaving the |
| 2998 | * breadcrumb at the end (so we get the fence notifications). |
| 2999 | */ |
| 3000 | head = request->head; |
| 3001 | if (request->postfix < head) { |
| 3002 | memset(vaddr + head, 0, request->ring->size - head); |
| 3003 | head = 0; |
| 3004 | } |
| 3005 | memset(vaddr + head, 0, request->postfix - head); |
Chris Wilson | c0d5f32 | 2017-01-10 17:22:43 +0000 | [diff] [blame] | 3006 | |
| 3007 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3010 | static void engine_skip_context(struct drm_i915_gem_request *request) |
| 3011 | { |
| 3012 | struct intel_engine_cs *engine = request->engine; |
| 3013 | struct i915_gem_context *hung_ctx = request->ctx; |
| 3014 | struct intel_timeline *timeline; |
| 3015 | unsigned long flags; |
| 3016 | |
| 3017 | timeline = i915_gem_context_lookup_timeline(hung_ctx, engine); |
| 3018 | |
| 3019 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3020 | spin_lock(&timeline->lock); |
| 3021 | |
| 3022 | list_for_each_entry_continue(request, &engine->timeline->requests, link) |
| 3023 | if (request->ctx == hung_ctx) |
| 3024 | skip_request(request); |
| 3025 | |
| 3026 | list_for_each_entry(request, &timeline->requests, link) |
| 3027 | skip_request(request); |
| 3028 | |
| 3029 | spin_unlock(&timeline->lock); |
| 3030 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 3031 | } |
| 3032 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3033 | /* Returns the request if it was guilty of the hang */ |
| 3034 | static struct drm_i915_gem_request * |
| 3035 | i915_gem_reset_request(struct intel_engine_cs *engine, |
| 3036 | struct drm_i915_gem_request *request) |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3037 | { |
Mika Kuoppala | 71895a0 | 2017-01-17 17:59:07 +0200 | [diff] [blame] | 3038 | /* The guilty request will get skipped on a hung engine. |
| 3039 | * |
| 3040 | * Users of client default contexts do not rely on logical |
| 3041 | * state preserved between batches so it is safe to execute |
| 3042 | * queued requests following the hang. Non default contexts |
| 3043 | * rely on preserved state, so skipping a batch loses the |
| 3044 | * evolution of the state and it needs to be considered corrupted. |
| 3045 | * Executing more queued batches on top of corrupted state is |
| 3046 | * risky. But we take the risk by trying to advance through |
| 3047 | * the queued requests in order to make the client behaviour |
| 3048 | * more predictable around resets, by not throwing away random |
| 3049 | * amount of batches it has prepared for execution. Sophisticated |
| 3050 | * clients can use gem_reset_stats_ioctl and dma fence status |
| 3051 | * (exported via sync_file info ioctl on explicit fences) to observe |
| 3052 | * when it loses the context state and should rebuild accordingly. |
| 3053 | * |
| 3054 | * The context ban, and ultimately the client ban, mechanism are safety |
| 3055 | * valves if client submission ends up resulting in nothing more than |
| 3056 | * subsequent hangs. |
| 3057 | */ |
| 3058 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3059 | if (engine_stalled(engine)) { |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3060 | i915_gem_context_mark_guilty(request->ctx); |
| 3061 | skip_request(request); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3062 | |
| 3063 | /* If this context is now banned, skip all pending requests. */ |
| 3064 | if (i915_gem_context_is_banned(request->ctx)) |
| 3065 | engine_skip_context(request); |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3066 | } else { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3067 | /* |
| 3068 | * Since this is not the hung engine, it may have advanced |
| 3069 | * since the hang declaration. Double check by refinding |
| 3070 | * the active request at the time of the reset. |
| 3071 | */ |
| 3072 | request = i915_gem_find_active_request(engine); |
| 3073 | if (request) { |
| 3074 | i915_gem_context_mark_innocent(request->ctx); |
| 3075 | dma_fence_set_error(&request->fence, -EAGAIN); |
| 3076 | |
| 3077 | /* Rewind the engine to replay the incomplete rq */ |
| 3078 | spin_lock_irq(&engine->timeline->lock); |
| 3079 | request = list_prev_entry(request, link); |
| 3080 | if (&request->link == &engine->timeline->requests) |
| 3081 | request = NULL; |
| 3082 | spin_unlock_irq(&engine->timeline->lock); |
| 3083 | } |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3084 | } |
| 3085 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3086 | return request; |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3087 | } |
| 3088 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3089 | void i915_gem_reset_engine(struct intel_engine_cs *engine, |
| 3090 | struct drm_i915_gem_request *request) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3091 | { |
Chris Wilson | ed454f2 | 2017-07-21 13:32:29 +0100 | [diff] [blame] | 3092 | engine->irq_posted = 0; |
| 3093 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3094 | if (request) |
| 3095 | request = i915_gem_reset_request(engine, request); |
| 3096 | |
| 3097 | if (request) { |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3098 | DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n", |
| 3099 | engine->name, request->global_seqno); |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3100 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3101 | |
| 3102 | /* Setup the CS to resume from the breadcrumb of the hung request */ |
| 3103 | engine->reset_hw(engine, request); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3104 | } |
| 3105 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3106 | void i915_gem_reset(struct drm_i915_private *dev_priv) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3107 | { |
| 3108 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3109 | enum intel_engine_id id; |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3110 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3111 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 3112 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3113 | i915_gem_retire_requests(dev_priv); |
| 3114 | |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3115 | for_each_engine(engine, dev_priv, id) { |
| 3116 | struct i915_gem_context *ctx; |
| 3117 | |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3118 | i915_gem_reset_engine(engine, engine->hangcheck.active_request); |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3119 | ctx = fetch_and_zero(&engine->last_retired_context); |
| 3120 | if (ctx) |
| 3121 | engine->context_unpin(engine, ctx); |
| 3122 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3123 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 3124 | i915_gem_restore_fences(dev_priv); |
Chris Wilson | f2a91d1 | 2016-09-21 14:51:06 +0100 | [diff] [blame] | 3125 | |
| 3126 | if (dev_priv->gt.awake) { |
| 3127 | intel_sanitize_gt_powersave(dev_priv); |
| 3128 | intel_enable_gt_powersave(dev_priv); |
| 3129 | if (INTEL_GEN(dev_priv) >= 6) |
| 3130 | gen6_rps_busy(dev_priv); |
| 3131 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3132 | } |
| 3133 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3134 | void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) |
| 3135 | { |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3136 | tasklet_enable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3137 | kthread_unpark(engine->breadcrumbs.signaler); |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3138 | |
| 3139 | intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3140 | } |
| 3141 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3142 | void i915_gem_reset_finish(struct drm_i915_private *dev_priv) |
| 3143 | { |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3144 | struct intel_engine_cs *engine; |
| 3145 | enum intel_engine_id id; |
| 3146 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3147 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3148 | |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3149 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3150 | engine->hangcheck.active_request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3151 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3152 | } |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3155 | static void nop_submit_request(struct drm_i915_gem_request *request) |
| 3156 | { |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3157 | dma_fence_set_error(&request->fence, -EIO); |
| 3158 | |
| 3159 | i915_gem_request_submit(request); |
| 3160 | } |
| 3161 | |
| 3162 | static void nop_complete_submit_request(struct drm_i915_gem_request *request) |
| 3163 | { |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3164 | unsigned long flags; |
| 3165 | |
Chris Wilson | 3cd9442 | 2017-01-10 17:22:45 +0000 | [diff] [blame] | 3166 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3167 | |
| 3168 | spin_lock_irqsave(&request->engine->timeline->lock, flags); |
| 3169 | __i915_gem_request_submit(request); |
Chris Wilson | 3dcf93f | 2016-11-22 14:41:20 +0000 | [diff] [blame] | 3170 | intel_engine_init_global_seqno(request->engine, request->global_seqno); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3171 | spin_unlock_irqrestore(&request->engine->timeline->lock, flags); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3172 | } |
| 3173 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3174 | void i915_gem_set_wedged(struct drm_i915_private *i915) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3175 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 3176 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3177 | enum intel_engine_id id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3178 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3179 | /* |
| 3180 | * First, stop submission to hw, but do not yet complete requests by |
| 3181 | * rolling the global seqno forward (since this would complete requests |
| 3182 | * for which we haven't set the fence error to EIO yet). |
| 3183 | */ |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3184 | for_each_engine(engine, i915, id) |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3185 | engine->submit_request = nop_submit_request; |
| 3186 | |
| 3187 | /* |
| 3188 | * Make sure no one is running the old callback before we proceed with |
| 3189 | * cancelling requests and resetting the completion tracking. Otherwise |
| 3190 | * we might submit a request to the hardware which never completes. |
| 3191 | */ |
| 3192 | synchronize_rcu(); |
| 3193 | |
| 3194 | for_each_engine(engine, i915, id) { |
| 3195 | /* Mark all executing requests as skipped */ |
| 3196 | engine->cancel_requests(engine); |
| 3197 | |
| 3198 | /* |
| 3199 | * Only once we've force-cancelled all in-flight requests can we |
| 3200 | * start to complete all requests. |
| 3201 | */ |
| 3202 | engine->submit_request = nop_complete_submit_request; |
| 3203 | } |
| 3204 | |
| 3205 | /* |
| 3206 | * Make sure no request can slip through without getting completed by |
| 3207 | * either this call here to intel_engine_init_global_seqno, or the one |
| 3208 | * in nop_complete_submit_request. |
| 3209 | */ |
| 3210 | synchronize_rcu(); |
| 3211 | |
| 3212 | for_each_engine(engine, i915, id) { |
| 3213 | unsigned long flags; |
| 3214 | |
| 3215 | /* Mark all pending requests as complete so that any concurrent |
| 3216 | * (lockless) lookup doesn't try and wait upon the request as we |
| 3217 | * reset it. |
| 3218 | */ |
| 3219 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3220 | intel_engine_init_global_seqno(engine, |
| 3221 | intel_engine_last_submit(engine)); |
| 3222 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 3223 | } |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3224 | |
Chris Wilson | 3d7adbb | 2017-07-21 13:32:27 +0100 | [diff] [blame] | 3225 | set_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3226 | wake_up_all(&i915->gpu_error.reset_queue); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3227 | } |
| 3228 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3229 | bool i915_gem_unset_wedged(struct drm_i915_private *i915) |
| 3230 | { |
| 3231 | struct i915_gem_timeline *tl; |
| 3232 | int i; |
| 3233 | |
| 3234 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3235 | if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3236 | return true; |
| 3237 | |
| 3238 | /* Before unwedging, make sure that all pending operations |
| 3239 | * are flushed and errored out - we may have requests waiting upon |
| 3240 | * third party fences. We marked all inflight requests as EIO, and |
| 3241 | * every execbuf since returned EIO, for consistency we want all |
| 3242 | * the currently pending requests to also be marked as EIO, which |
| 3243 | * is done inside our nop_submit_request - and so we must wait. |
| 3244 | * |
| 3245 | * No more can be submitted until we reset the wedged bit. |
| 3246 | */ |
| 3247 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3248 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
| 3249 | struct drm_i915_gem_request *rq; |
| 3250 | |
| 3251 | rq = i915_gem_active_peek(&tl->engine[i].last_request, |
| 3252 | &i915->drm.struct_mutex); |
| 3253 | if (!rq) |
| 3254 | continue; |
| 3255 | |
| 3256 | /* We can't use our normal waiter as we want to |
| 3257 | * avoid recursively trying to handle the current |
| 3258 | * reset. The basic dma_fence_default_wait() installs |
| 3259 | * a callback for dma_fence_signal(), which is |
| 3260 | * triggered by our nop handler (indirectly, the |
| 3261 | * callback enables the signaler thread which is |
| 3262 | * woken by the nop_submit_request() advancing the seqno |
| 3263 | * and when the seqno passes the fence, the signaler |
| 3264 | * then signals the fence waking us up). |
| 3265 | */ |
| 3266 | if (dma_fence_default_wait(&rq->fence, true, |
| 3267 | MAX_SCHEDULE_TIMEOUT) < 0) |
| 3268 | return false; |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | /* Undo nop_submit_request. We prevent all new i915 requests from |
| 3273 | * being queued (by disallowing execbuf whilst wedged) so having |
| 3274 | * waited for all active requests above, we know the system is idle |
| 3275 | * and do not have to worry about a thread being inside |
| 3276 | * engine->submit_request() as we swap over. So unlike installing |
| 3277 | * the nop_submit_request on reset, we can do this from normal |
| 3278 | * context and do not require stop_machine(). |
| 3279 | */ |
| 3280 | intel_engines_reset_default_submission(i915); |
Chris Wilson | 36703e7 | 2017-06-22 11:56:25 +0100 | [diff] [blame] | 3281 | i915_gem_contexts_lost(i915); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3282 | |
| 3283 | smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ |
| 3284 | clear_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3285 | |
| 3286 | return true; |
| 3287 | } |
| 3288 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 3289 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3290 | i915_gem_retire_work_handler(struct work_struct *work) |
| 3291 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3292 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3293 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 3294 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3295 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3296 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3297 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3298 | i915_gem_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3299 | mutex_unlock(&dev->struct_mutex); |
| 3300 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3301 | |
| 3302 | /* Keep the retire handler running until we are finally idle. |
| 3303 | * We do not need to do this test under locking as in the worst-case |
| 3304 | * we queue the retire worker once too often. |
| 3305 | */ |
Chris Wilson | c961561 | 2016-07-09 10:12:06 +0100 | [diff] [blame] | 3306 | if (READ_ONCE(dev_priv->gt.awake)) { |
| 3307 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3308 | queue_delayed_work(dev_priv->wq, |
| 3309 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 3310 | round_jiffies_up_relative(HZ)); |
Chris Wilson | c961561 | 2016-07-09 10:12:06 +0100 | [diff] [blame] | 3311 | } |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3312 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3313 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3314 | static inline bool |
| 3315 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 3316 | { |
| 3317 | return (READ_ONCE(i915->gt.active_requests) || |
| 3318 | work_pending(&i915->gt.idle_work.work)); |
| 3319 | } |
| 3320 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3321 | static void |
| 3322 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3323 | { |
| 3324 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3325 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3326 | bool rearm_hangcheck; |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3327 | ktime_t end; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3328 | |
| 3329 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3330 | return; |
| 3331 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3332 | /* |
| 3333 | * Wait for last execlists context complete, but bail out in case a |
| 3334 | * new request is submitted. |
| 3335 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3336 | end = ktime_add_ms(ktime_get(), 200); |
| 3337 | do { |
| 3338 | if (new_requests_since_last_retire(dev_priv)) |
| 3339 | return; |
| 3340 | |
| 3341 | if (intel_engines_are_idle(dev_priv)) |
| 3342 | break; |
| 3343 | |
| 3344 | usleep_range(100, 500); |
| 3345 | } while (ktime_before(ktime_get(), end)); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3346 | |
| 3347 | rearm_hangcheck = |
| 3348 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3349 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3350 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3351 | /* Currently busy, come back later */ |
| 3352 | mod_delayed_work(dev_priv->wq, |
| 3353 | &dev_priv->gt.idle_work, |
| 3354 | msecs_to_jiffies(50)); |
| 3355 | goto out_rearm; |
| 3356 | } |
| 3357 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3358 | /* |
| 3359 | * New request retired after this work handler started, extend active |
| 3360 | * period until next instance of the work. |
| 3361 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3362 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3363 | goto out_unlock; |
| 3364 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3365 | /* |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3366 | * Be paranoid and flush a concurrent interrupt to make sure |
| 3367 | * we don't reactivate any irq tasklets after parking. |
| 3368 | * |
| 3369 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 3370 | * there may still be an in-flight interrupt even though the CSB |
| 3371 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 3372 | * is completed before we continue, but it doesn't prevent the HW from |
| 3373 | * raising a spurious interrupt later. To complete the shield we should |
| 3374 | * coordinate disabling the CS irq with flushing the interrupts. |
| 3375 | */ |
| 3376 | synchronize_irq(dev_priv->drm.irq); |
| 3377 | |
Chris Wilson | aba5e27 | 2017-10-25 15:39:41 +0100 | [diff] [blame] | 3378 | intel_engines_park(dev_priv); |
Chris Wilson | d02a1d8 | 2017-11-27 12:30:54 +0000 | [diff] [blame] | 3379 | i915_gem_timelines_park(dev_priv); |
| 3380 | |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 3381 | i915_pmu_gt_parked(dev_priv); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3382 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3383 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3384 | dev_priv->gt.awake = false; |
| 3385 | rearm_hangcheck = false; |
Daniel Vetter | 30ecad7 | 2015-12-09 09:29:36 +0100 | [diff] [blame] | 3386 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3387 | if (INTEL_GEN(dev_priv) >= 6) |
| 3388 | gen6_rps_idle(dev_priv); |
| 3389 | intel_runtime_pm_put(dev_priv); |
| 3390 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3391 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3392 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3393 | out_rearm: |
| 3394 | if (rearm_hangcheck) { |
| 3395 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3396 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3397 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3398 | } |
| 3399 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3400 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3401 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3402 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3403 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3404 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3405 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3406 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3407 | mutex_lock(&i915->drm.struct_mutex); |
| 3408 | |
| 3409 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3410 | struct i915_gem_context *ctx = lut->ctx; |
| 3411 | struct i915_vma *vma; |
| 3412 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3413 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3414 | if (ctx->file_priv != fpriv) |
| 3415 | continue; |
| 3416 | |
| 3417 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3418 | GEM_BUG_ON(vma->obj != obj); |
| 3419 | |
| 3420 | /* We allow the process to have multiple handles to the same |
| 3421 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3422 | */ |
| 3423 | GEM_BUG_ON(!vma->open_count); |
| 3424 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3425 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3426 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3427 | list_del(&lut->obj_link); |
| 3428 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3429 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3430 | kmem_cache_free(i915->luts, lut); |
| 3431 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3432 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3433 | |
| 3434 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3435 | } |
| 3436 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3437 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3438 | { |
| 3439 | if (timeout_ns < 0) |
| 3440 | return MAX_SCHEDULE_TIMEOUT; |
| 3441 | |
| 3442 | if (timeout_ns == 0) |
| 3443 | return 0; |
| 3444 | |
| 3445 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3446 | } |
| 3447 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3448 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3449 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3450 | * @dev: drm device pointer |
| 3451 | * @data: ioctl data blob |
| 3452 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3453 | * |
| 3454 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3455 | * the timeout parameter. |
| 3456 | * -ETIME: object is still busy after timeout |
| 3457 | * -ERESTARTSYS: signal interrupted the wait |
| 3458 | * -ENONENT: object doesn't exist |
| 3459 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3460 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3461 | * -ENOMEM: damn |
| 3462 | * -ENODEV: Internal IRQ fail |
| 3463 | * -E?: The add request failed |
| 3464 | * |
| 3465 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3466 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3467 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3468 | * without holding struct_mutex the object may become re-busied before this |
| 3469 | * function completes. A similar but shorter * race condition exists in the busy |
| 3470 | * ioctl |
| 3471 | */ |
| 3472 | int |
| 3473 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3474 | { |
| 3475 | struct drm_i915_gem_wait *args = data; |
| 3476 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3477 | ktime_t start; |
| 3478 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3479 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3480 | if (args->flags != 0) |
| 3481 | return -EINVAL; |
| 3482 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3483 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3484 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3485 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3486 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3487 | start = ktime_get(); |
| 3488 | |
| 3489 | ret = i915_gem_object_wait(obj, |
| 3490 | I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL, |
| 3491 | to_wait_timeout(args->timeout_ns), |
| 3492 | to_rps_client(file)); |
| 3493 | |
| 3494 | if (args->timeout_ns > 0) { |
| 3495 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3496 | if (args->timeout_ns < 0) |
| 3497 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3498 | |
| 3499 | /* |
| 3500 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3501 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3502 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3503 | * |
| 3504 | * This is a regression from the timespec->ktime conversion. |
| 3505 | */ |
| 3506 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3507 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3508 | |
| 3509 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3510 | if (ret == -ETIME && args->timeout_ns) |
| 3511 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3512 | } |
| 3513 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3514 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3515 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3516 | } |
| 3517 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3518 | 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] | 3519 | { |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3520 | int ret, i; |
| 3521 | |
| 3522 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
| 3523 | ret = i915_gem_active_wait(&tl->engine[i].last_request, flags); |
| 3524 | if (ret) |
| 3525 | return ret; |
| 3526 | } |
| 3527 | |
| 3528 | return 0; |
| 3529 | } |
| 3530 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3531 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3532 | { |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3533 | if (wait_for(intel_engines_are_idle(i915), 50)) { |
| 3534 | DRM_ERROR("Failed to idle engines, declaring wedged!\n"); |
| 3535 | i915_gem_set_wedged(i915); |
| 3536 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | return 0; |
| 3540 | } |
| 3541 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3542 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags) |
| 3543 | { |
Dave Gordon | b4ac5af | 2016-03-24 11:20:38 +0000 | [diff] [blame] | 3544 | int ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3545 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3546 | /* If the device is asleep, we have no requests outstanding */ |
| 3547 | if (!READ_ONCE(i915->gt.awake)) |
| 3548 | return 0; |
| 3549 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3550 | if (flags & I915_WAIT_LOCKED) { |
| 3551 | struct i915_gem_timeline *tl; |
| 3552 | |
| 3553 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3554 | |
| 3555 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3556 | ret = wait_for_timeline(tl, flags); |
| 3557 | if (ret) |
| 3558 | return ret; |
| 3559 | } |
Chris Wilson | 72022a7 | 2017-03-30 15:50:38 +0100 | [diff] [blame] | 3560 | |
| 3561 | i915_gem_retire_requests(i915); |
| 3562 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3563 | |
| 3564 | ret = wait_for_engines(i915); |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3565 | } else { |
| 3566 | ret = wait_for_timeline(&i915->gt.global_timeline, flags); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 3567 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3568 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3569 | return ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3570 | } |
| 3571 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3572 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3573 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3574 | /* |
| 3575 | * We manually flush the CPU domain so that we can override and |
| 3576 | * force the flush for the display, and perform it asyncrhonously. |
| 3577 | */ |
| 3578 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3579 | if (obj->cache_dirty) |
| 3580 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3581 | obj->base.write_domain = 0; |
| 3582 | } |
| 3583 | |
| 3584 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3585 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3586 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3587 | return; |
| 3588 | |
| 3589 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3590 | __i915_gem_object_flush_for_display(obj); |
| 3591 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3592 | } |
| 3593 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3594 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3595 | * Moves a single object to the WC read, and possibly write domain. |
| 3596 | * @obj: object to act on |
| 3597 | * @write: ask for write access or read only |
| 3598 | * |
| 3599 | * This function returns when the move is complete, including waiting on |
| 3600 | * flushes to occur. |
| 3601 | */ |
| 3602 | int |
| 3603 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3604 | { |
| 3605 | int ret; |
| 3606 | |
| 3607 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3608 | |
| 3609 | ret = i915_gem_object_wait(obj, |
| 3610 | I915_WAIT_INTERRUPTIBLE | |
| 3611 | I915_WAIT_LOCKED | |
| 3612 | (write ? I915_WAIT_ALL : 0), |
| 3613 | MAX_SCHEDULE_TIMEOUT, |
| 3614 | NULL); |
| 3615 | if (ret) |
| 3616 | return ret; |
| 3617 | |
| 3618 | if (obj->base.write_domain == I915_GEM_DOMAIN_WC) |
| 3619 | return 0; |
| 3620 | |
| 3621 | /* Flush and acquire obj->pages so that we are coherent through |
| 3622 | * direct access in memory with previous cached writes through |
| 3623 | * shmemfs and that our cache domain tracking remains valid. |
| 3624 | * For example, if the obj->filp was moved to swap without us |
| 3625 | * being notified and releasing the pages, we would mistakenly |
| 3626 | * continue to assume that the obj remained out of the CPU cached |
| 3627 | * domain. |
| 3628 | */ |
| 3629 | ret = i915_gem_object_pin_pages(obj); |
| 3630 | if (ret) |
| 3631 | return ret; |
| 3632 | |
| 3633 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3634 | |
| 3635 | /* Serialise direct access to this object with the barriers for |
| 3636 | * coherent writes from the GPU, by effectively invalidating the |
| 3637 | * WC domain upon first access. |
| 3638 | */ |
| 3639 | if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0) |
| 3640 | mb(); |
| 3641 | |
| 3642 | /* It should now be out of any other write domains, and we can update |
| 3643 | * the domain values for our changes. |
| 3644 | */ |
| 3645 | GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3646 | obj->base.read_domains |= I915_GEM_DOMAIN_WC; |
| 3647 | if (write) { |
| 3648 | obj->base.read_domains = I915_GEM_DOMAIN_WC; |
| 3649 | obj->base.write_domain = I915_GEM_DOMAIN_WC; |
| 3650 | obj->mm.dirty = true; |
| 3651 | } |
| 3652 | |
| 3653 | i915_gem_object_unpin_pages(obj); |
| 3654 | return 0; |
| 3655 | } |
| 3656 | |
| 3657 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3658 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3659 | * @obj: object to act on |
| 3660 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3661 | * |
| 3662 | * This function returns when the move is complete, including waiting on |
| 3663 | * flushes to occur. |
| 3664 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3665 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3666 | 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] | 3667 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3668 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3669 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3670 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3671 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3672 | ret = i915_gem_object_wait(obj, |
| 3673 | I915_WAIT_INTERRUPTIBLE | |
| 3674 | I915_WAIT_LOCKED | |
| 3675 | (write ? I915_WAIT_ALL : 0), |
| 3676 | MAX_SCHEDULE_TIMEOUT, |
| 3677 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3678 | if (ret) |
| 3679 | return ret; |
| 3680 | |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3681 | if (obj->base.write_domain == I915_GEM_DOMAIN_GTT) |
| 3682 | return 0; |
| 3683 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3684 | /* Flush and acquire obj->pages so that we are coherent through |
| 3685 | * direct access in memory with previous cached writes through |
| 3686 | * shmemfs and that our cache domain tracking remains valid. |
| 3687 | * For example, if the obj->filp was moved to swap without us |
| 3688 | * being notified and releasing the pages, we would mistakenly |
| 3689 | * continue to assume that the obj remained out of the CPU cached |
| 3690 | * domain. |
| 3691 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3692 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3693 | if (ret) |
| 3694 | return ret; |
| 3695 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3696 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3697 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3698 | /* Serialise direct access to this object with the barriers for |
| 3699 | * coherent writes from the GPU, by effectively invalidating the |
| 3700 | * GTT domain upon first access. |
| 3701 | */ |
| 3702 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) |
| 3703 | mb(); |
| 3704 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3705 | /* It should now be out of any other write domains, and we can update |
| 3706 | * the domain values for our changes. |
| 3707 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 3708 | GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3709 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3710 | if (write) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3711 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
| 3712 | obj->base.write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3713 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3714 | } |
| 3715 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3716 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3717 | return 0; |
| 3718 | } |
| 3719 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3720 | /** |
| 3721 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3722 | * @obj: object to act on |
| 3723 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3724 | * |
| 3725 | * After this function returns, the object will be in the new cache-level |
| 3726 | * across all GTT and the contents of the backing storage will be coherent, |
| 3727 | * with respect to the new cache-level. In order to keep the backing storage |
| 3728 | * coherent for all users, we only allow a single cache level to be set |
| 3729 | * globally on the object and prevent it from being changed whilst the |
| 3730 | * hardware is reading from the object. That is if the object is currently |
| 3731 | * on the scanout it will be set to uncached (or equivalent display |
| 3732 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3733 | * that all direct access to the scanout remains coherent. |
| 3734 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3735 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3736 | enum i915_cache_level cache_level) |
| 3737 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3738 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3739 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3740 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3741 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3742 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3743 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3744 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3745 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3746 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3747 | * be invalid given the new cache-level. This is principally to |
| 3748 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3749 | * reading an invalid PTE on older architectures. |
| 3750 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3751 | restart: |
| 3752 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3753 | if (!drm_mm_node_allocated(&vma->node)) |
| 3754 | continue; |
| 3755 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3756 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3757 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3758 | return -EBUSY; |
| 3759 | } |
| 3760 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3761 | if (!i915_vma_is_closed(vma) && |
| 3762 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3763 | continue; |
| 3764 | |
| 3765 | ret = i915_vma_unbind(vma); |
| 3766 | if (ret) |
| 3767 | return ret; |
| 3768 | |
| 3769 | /* As unbinding may affect other elements in the |
| 3770 | * obj->vma_list (due to side-effects from retiring |
| 3771 | * an active vma), play safe and restart the iterator. |
| 3772 | */ |
| 3773 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3774 | } |
| 3775 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3776 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3777 | * cache-level on the PTE. We could simply unbind them all and |
| 3778 | * rebind with the correct cache-level on next use. However since |
| 3779 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3780 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3781 | * state and so involves less work. |
| 3782 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3783 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3784 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3785 | * If we wait upon the object, we know that all the bound |
| 3786 | * VMA are no longer active. |
| 3787 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3788 | ret = i915_gem_object_wait(obj, |
| 3789 | I915_WAIT_INTERRUPTIBLE | |
| 3790 | I915_WAIT_LOCKED | |
| 3791 | I915_WAIT_ALL, |
| 3792 | MAX_SCHEDULE_TIMEOUT, |
| 3793 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3794 | if (ret) |
| 3795 | return ret; |
| 3796 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3797 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3798 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3799 | /* Access to snoopable pages through the GTT is |
| 3800 | * incoherent and on some machines causes a hard |
| 3801 | * lockup. Relinquish the CPU mmaping to force |
| 3802 | * userspace to refault in the pages and we can |
| 3803 | * then double check if the GTT mapping is still |
| 3804 | * valid for that pointer access. |
| 3805 | */ |
| 3806 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3807 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3808 | /* As we no longer need a fence for GTT access, |
| 3809 | * we can relinquish it now (and so prevent having |
| 3810 | * to steal a fence from someone else on the next |
| 3811 | * fence request). Note GPU activity would have |
| 3812 | * dropped the fence as all snoopable access is |
| 3813 | * supposed to be linear. |
| 3814 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame^] | 3815 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3816 | ret = i915_vma_put_fence(vma); |
| 3817 | if (ret) |
| 3818 | return ret; |
| 3819 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3820 | } else { |
| 3821 | /* We either have incoherent backing store and |
| 3822 | * so no GTT access or the architecture is fully |
| 3823 | * coherent. In such cases, existing GTT mmaps |
| 3824 | * ignore the cache bit in the PTE and we can |
| 3825 | * rewrite it without confusing the GPU or having |
| 3826 | * to force userspace to fault back in its mmaps. |
| 3827 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3828 | } |
| 3829 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3830 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3831 | if (!drm_mm_node_allocated(&vma->node)) |
| 3832 | continue; |
| 3833 | |
| 3834 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3835 | if (ret) |
| 3836 | return ret; |
| 3837 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3838 | } |
| 3839 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3840 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3841 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3842 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3843 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3844 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3845 | return 0; |
| 3846 | } |
| 3847 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3848 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3849 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3850 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3851 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3852 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3853 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3854 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3855 | rcu_read_lock(); |
| 3856 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3857 | if (!obj) { |
| 3858 | err = -ENOENT; |
| 3859 | goto out; |
| 3860 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3861 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3862 | switch (obj->cache_level) { |
| 3863 | case I915_CACHE_LLC: |
| 3864 | case I915_CACHE_L3_LLC: |
| 3865 | args->caching = I915_CACHING_CACHED; |
| 3866 | break; |
| 3867 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3868 | case I915_CACHE_WT: |
| 3869 | args->caching = I915_CACHING_DISPLAY; |
| 3870 | break; |
| 3871 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3872 | default: |
| 3873 | args->caching = I915_CACHING_NONE; |
| 3874 | break; |
| 3875 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3876 | out: |
| 3877 | rcu_read_unlock(); |
| 3878 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3879 | } |
| 3880 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3881 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3882 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3883 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3884 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3885 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3886 | struct drm_i915_gem_object *obj; |
| 3887 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3888 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3889 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3890 | switch (args->caching) { |
| 3891 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3892 | level = I915_CACHE_NONE; |
| 3893 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3894 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3895 | /* |
| 3896 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3897 | * snooped mapping may leave stale data in a corresponding CPU |
| 3898 | * cacheline, whereas normally such cachelines would get |
| 3899 | * invalidated. |
| 3900 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3901 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3902 | return -ENODEV; |
| 3903 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3904 | level = I915_CACHE_LLC; |
| 3905 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3906 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3907 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3908 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3909 | default: |
| 3910 | return -EINVAL; |
| 3911 | } |
| 3912 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3913 | obj = i915_gem_object_lookup(file, args->handle); |
| 3914 | if (!obj) |
| 3915 | return -ENOENT; |
| 3916 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3917 | /* |
| 3918 | * The caching mode of proxy object is handled by its generator, and |
| 3919 | * not allowed to be changed by userspace. |
| 3920 | */ |
| 3921 | if (i915_gem_object_is_proxy(obj)) { |
| 3922 | ret = -ENXIO; |
| 3923 | goto out; |
| 3924 | } |
| 3925 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3926 | if (obj->cache_level == level) |
| 3927 | goto out; |
| 3928 | |
| 3929 | ret = i915_gem_object_wait(obj, |
| 3930 | I915_WAIT_INTERRUPTIBLE, |
| 3931 | MAX_SCHEDULE_TIMEOUT, |
| 3932 | to_rps_client(file)); |
| 3933 | if (ret) |
| 3934 | goto out; |
| 3935 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3936 | ret = i915_mutex_lock_interruptible(dev); |
| 3937 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3938 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3939 | |
| 3940 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3941 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3942 | |
| 3943 | out: |
| 3944 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3945 | return ret; |
| 3946 | } |
| 3947 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3948 | /* |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3949 | * Prepare buffer for display plane (scanout, cursors, etc). |
| 3950 | * Can be called from an uninterruptible phase (modesetting) and allows |
| 3951 | * any flushes to be pipelined (for pageflips). |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3952 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3953 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3954 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3955 | u32 alignment, |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3956 | const struct i915_ggtt_view *view) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3957 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3958 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3959 | int ret; |
| 3960 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3961 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3962 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3963 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3964 | * display coherency whilst setting up the cache domains. |
| 3965 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3966 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3967 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3968 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3969 | * a result, we make sure that the pinning that is about to occur is |
| 3970 | * done with uncached PTEs. This is lowest common denominator for all |
| 3971 | * chipsets. |
| 3972 | * |
| 3973 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3974 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3975 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3976 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3977 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3978 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3979 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3980 | if (ret) { |
| 3981 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3982 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3983 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3984 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3985 | /* As the user may map the buffer once pinned in the display plane |
| 3986 | * (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] | 3987 | * always use map_and_fenceable for all scanout buffers. However, |
| 3988 | * it may simply be too big to fit into mappable, in which case |
| 3989 | * put it anyway and hope that userspace can cope (but always first |
| 3990 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3991 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3992 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 47a8e3f | 2017-01-14 00:28:27 +0000 | [diff] [blame] | 3993 | if (!view || view->type == I915_GGTT_VIEW_NORMAL) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3994 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
| 3995 | PIN_MAPPABLE | PIN_NONBLOCK); |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3996 | if (IS_ERR(vma)) { |
| 3997 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 3998 | unsigned int flags; |
| 3999 | |
| 4000 | /* Valleyview is definitely limited to scanning out the first |
| 4001 | * 512MiB. Lets presume this behaviour was inherited from the |
| 4002 | * g4x display engine and that all earlier gen are similarly |
| 4003 | * limited. Testing suggests that it is a little more |
| 4004 | * complicated than this. For example, Cherryview appears quite |
| 4005 | * happy to scanout from anywhere within its global aperture. |
| 4006 | */ |
| 4007 | flags = 0; |
| 4008 | if (HAS_GMCH_DISPLAY(i915)) |
| 4009 | flags = PIN_MAPPABLE; |
| 4010 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
| 4011 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4012 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4013 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4014 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 4015 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 4016 | |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 4017 | /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 4018 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4019 | intel_fb_obj_flush(obj, ORIGIN_DIRTYFB); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 4020 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4021 | /* It should now be out of any other write domains, and we can update |
| 4022 | * the domain values for our changes. |
| 4023 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4024 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4025 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4026 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4027 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4028 | err_unpin_global: |
| 4029 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4030 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4031 | } |
| 4032 | |
| 4033 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4034 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4035 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 4036 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4037 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4038 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 4039 | return; |
| 4040 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4041 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 4042 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 4043 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4044 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 4045 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4046 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4047 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4048 | } |
| 4049 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4050 | /** |
| 4051 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 4052 | * @obj: object to act on |
| 4053 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4054 | * |
| 4055 | * This function returns when the move is complete, including waiting on |
| 4056 | * flushes to occur. |
| 4057 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 4058 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 4059 | 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] | 4060 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4061 | int ret; |
| 4062 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4063 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4064 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4065 | ret = i915_gem_object_wait(obj, |
| 4066 | I915_WAIT_INTERRUPTIBLE | |
| 4067 | I915_WAIT_LOCKED | |
| 4068 | (write ? I915_WAIT_ALL : 0), |
| 4069 | MAX_SCHEDULE_TIMEOUT, |
| 4070 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 4071 | if (ret) |
| 4072 | return ret; |
| 4073 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 4074 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4075 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4076 | /* Flush the CPU cache if it's still invalid. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4077 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4078 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4079 | obj->base.read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | /* It should now be out of any other write domains, and we can update |
| 4083 | * the domain values for our changes. |
| 4084 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4085 | GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4086 | |
| 4087 | /* If we're writing through the CPU, then the GPU read domains will |
| 4088 | * need to be invalidated at next use. |
| 4089 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4090 | if (write) |
| 4091 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 4092 | |
| 4093 | return 0; |
| 4094 | } |
| 4095 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4096 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 4097 | * emitted over 20 msec ago. |
| 4098 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4099 | * Note that if we were to use the current jiffies each time around the loop, |
| 4100 | * we wouldn't escape the function with any frames outstanding if the time to |
| 4101 | * render a frame was over 20ms. |
| 4102 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4103 | * This should get us reasonable parallelism between CPU and GPU but also |
| 4104 | * relatively low latency when blocking on a particular request to finish. |
| 4105 | */ |
| 4106 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4107 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4108 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4109 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4110 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 4111 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4112 | struct drm_i915_gem_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4113 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4114 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 4115 | /* ABI: return -EIO if already wedged */ |
| 4116 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 4117 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 4118 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4119 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4120 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4121 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 4122 | break; |
| 4123 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4124 | if (target) { |
| 4125 | list_del(&target->client_link); |
| 4126 | target->file_priv = NULL; |
| 4127 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 4128 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4129 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4130 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4131 | if (target) |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 4132 | i915_gem_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4133 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4134 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4135 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4136 | return 0; |
| 4137 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4138 | ret = i915_wait_request(target, |
| 4139 | I915_WAIT_INTERRUPTIBLE, |
| 4140 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e8a261e | 2016-07-20 13:31:49 +0100 | [diff] [blame] | 4141 | i915_gem_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4142 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4143 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4144 | } |
| 4145 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4146 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4147 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 4148 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 4149 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 4150 | u64 alignment, |
| 4151 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4152 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4153 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 4154 | struct i915_address_space *vm = &dev_priv->ggtt.base; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4155 | struct i915_vma *vma; |
| 4156 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 4157 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4158 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4159 | |
Chris Wilson | 43ae70d | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4160 | if (!view && flags & PIN_MAPPABLE) { |
| 4161 | /* If the required space is larger than the available |
| 4162 | * aperture, we will not able to find a slot for the |
| 4163 | * object and unbinding the object now will be in |
| 4164 | * vain. Worse, doing so may cause us to ping-pong |
| 4165 | * the object in and out of the Global GTT and |
| 4166 | * waste a lot of cycles under the mutex. |
| 4167 | */ |
| 4168 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 4169 | return ERR_PTR(-E2BIG); |
| 4170 | |
| 4171 | /* If NONBLOCK is set the caller is optimistically |
| 4172 | * trying to cache the full object within the mappable |
| 4173 | * aperture, and *must* have a fallback in place for |
| 4174 | * situations where we cannot bind the object. We |
| 4175 | * can be a little more lax here and use the fallback |
| 4176 | * more often to avoid costly migrations of ourselves |
| 4177 | * and other objects within the aperture. |
| 4178 | * |
| 4179 | * Half-the-aperture is used as a simple heuristic. |
| 4180 | * More interesting would to do search for a free |
| 4181 | * block prior to making the commitment to unbind. |
| 4182 | * That caters for the self-harm case, and with a |
| 4183 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 4184 | * we could try to minimise harm to others. |
| 4185 | */ |
| 4186 | if (flags & PIN_NONBLOCK && |
| 4187 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 4188 | return ERR_PTR(-ENOSPC); |
| 4189 | } |
| 4190 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 4191 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 4192 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4193 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4194 | |
| 4195 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4196 | if (flags & PIN_NONBLOCK) { |
| 4197 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 4198 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4199 | |
Chris Wilson | 43ae70d | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4200 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 4201 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4202 | return ERR_PTR(-ENOSPC); |
| 4203 | } |
| 4204 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4205 | WARN(i915_vma_is_pinned(vma), |
| 4206 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4207 | " offset=%08x, req.alignment=%llx," |
| 4208 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 4209 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4210 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4211 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4212 | ret = i915_vma_unbind(vma); |
| 4213 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4214 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4215 | } |
| 4216 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4217 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 4218 | if (ret) |
| 4219 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4220 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4221 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4222 | } |
| 4223 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4224 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4225 | { |
| 4226 | /* Note that we could alias engines in the execbuf API, but |
| 4227 | * that would be very unwise as it prevents userspace from |
| 4228 | * fine control over engine selection. Ahem. |
| 4229 | * |
| 4230 | * This should be something like EXEC_MAX_ENGINE instead of |
| 4231 | * I915_NUM_ENGINES. |
| 4232 | */ |
| 4233 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 4234 | return 0x10000 << id; |
| 4235 | } |
| 4236 | |
| 4237 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 4238 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 4239 | /* The uABI guarantees an active writer is also amongst the read |
| 4240 | * engines. This would be true if we accessed the activity tracking |
| 4241 | * under the lock, but as we perform the lookup of the object and |
| 4242 | * its activity locklessly we can not guarantee that the last_write |
| 4243 | * being active implies that we have set the same engine flag from |
| 4244 | * last_read - hence we always set both read and write busy for |
| 4245 | * last_write. |
| 4246 | */ |
| 4247 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4248 | } |
| 4249 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4250 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4251 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4252 | unsigned int (*flag)(unsigned int id)) |
| 4253 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4254 | struct drm_i915_gem_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4255 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4256 | /* We have to check the current hw status of the fence as the uABI |
| 4257 | * guarantees forward progress. We could rely on the idle worker |
| 4258 | * to eventually flush us, but to minimise latency just ask the |
| 4259 | * hardware. |
| 4260 | * |
| 4261 | * Note we only report on the status of native fences. |
| 4262 | */ |
| 4263 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4264 | return 0; |
| 4265 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4266 | /* opencode to_request() in order to avoid const warnings */ |
| 4267 | rq = container_of(fence, struct drm_i915_gem_request, fence); |
| 4268 | if (i915_gem_request_completed(rq)) |
| 4269 | return 0; |
| 4270 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4271 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4272 | } |
| 4273 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4274 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4275 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4276 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4277 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4278 | } |
| 4279 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4280 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4281 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4282 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4283 | if (!fence) |
| 4284 | return 0; |
| 4285 | |
| 4286 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4287 | } |
| 4288 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4289 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4290 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4291 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4292 | { |
| 4293 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4294 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4295 | struct reservation_object_list *list; |
| 4296 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4297 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4298 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4299 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4300 | rcu_read_lock(); |
| 4301 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4302 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4303 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4304 | |
| 4305 | /* A discrepancy here is that we do not report the status of |
| 4306 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4307 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4308 | * This also means that wait-ioctl may report an object as busy, |
| 4309 | * where busy-ioctl considers it idle. |
| 4310 | * |
| 4311 | * We trade the ability to warn of foreign fences to report on which |
| 4312 | * i915 engines are active for the object. |
| 4313 | * |
| 4314 | * Alternatively, we can trade that extra information on read/write |
| 4315 | * activity with |
| 4316 | * args->busy = |
| 4317 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4318 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4319 | * |
| 4320 | */ |
| 4321 | retry: |
| 4322 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4323 | |
| 4324 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4325 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4326 | |
| 4327 | /* Translate shared fences to READ set of engines */ |
| 4328 | list = rcu_dereference(obj->resv->fence); |
| 4329 | if (list) { |
| 4330 | unsigned int shared_count = list->shared_count, i; |
| 4331 | |
| 4332 | for (i = 0; i < shared_count; ++i) { |
| 4333 | struct dma_fence *fence = |
| 4334 | rcu_dereference(list->shared[i]); |
| 4335 | |
| 4336 | args->busy |= busy_check_reader(fence); |
| 4337 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4338 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4339 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4340 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4341 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4342 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4343 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4344 | out: |
| 4345 | rcu_read_unlock(); |
| 4346 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | int |
| 4350 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4351 | struct drm_file *file_priv) |
| 4352 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4353 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4354 | } |
| 4355 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4356 | int |
| 4357 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4358 | struct drm_file *file_priv) |
| 4359 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4360 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4361 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4362 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4363 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4364 | |
| 4365 | switch (args->madv) { |
| 4366 | case I915_MADV_DONTNEED: |
| 4367 | case I915_MADV_WILLNEED: |
| 4368 | break; |
| 4369 | default: |
| 4370 | return -EINVAL; |
| 4371 | } |
| 4372 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4373 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4374 | if (!obj) |
| 4375 | return -ENOENT; |
| 4376 | |
| 4377 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4378 | if (err) |
| 4379 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4380 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4381 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4382 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4383 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4384 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4385 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4386 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4387 | obj->mm.quirked = false; |
| 4388 | } |
| 4389 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4390 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4391 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4392 | obj->mm.quirked = true; |
| 4393 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4396 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4397 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4398 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4399 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4400 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4401 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4402 | i915_gem_object_truncate(obj); |
| 4403 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4404 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4405 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4406 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4407 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4408 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4409 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4410 | } |
| 4411 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4412 | static void |
| 4413 | frontbuffer_retire(struct i915_gem_active *active, |
| 4414 | struct drm_i915_gem_request *request) |
| 4415 | { |
| 4416 | struct drm_i915_gem_object *obj = |
| 4417 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4418 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4419 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4420 | } |
| 4421 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4422 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4423 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4424 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4425 | mutex_init(&obj->mm.lock); |
| 4426 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4427 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4428 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4429 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4430 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4431 | obj->ops = ops; |
| 4432 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4433 | reservation_object_init(&obj->__builtin_resv); |
| 4434 | obj->resv = &obj->__builtin_resv; |
| 4435 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4436 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4437 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4438 | |
| 4439 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4440 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4441 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4442 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4443 | 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] | 4444 | } |
| 4445 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4446 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4447 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4448 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4449 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4450 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4451 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4452 | |
| 4453 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4454 | }; |
| 4455 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4456 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4457 | struct drm_gem_object *obj, |
| 4458 | size_t size) |
| 4459 | { |
| 4460 | struct drm_i915_private *i915 = to_i915(dev); |
| 4461 | unsigned long flags = VM_NORESERVE; |
| 4462 | struct file *filp; |
| 4463 | |
| 4464 | drm_gem_private_object_init(dev, obj, size); |
| 4465 | |
| 4466 | if (i915->mm.gemfs) |
| 4467 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4468 | flags); |
| 4469 | else |
| 4470 | filp = shmem_file_setup("i915", size, flags); |
| 4471 | |
| 4472 | if (IS_ERR(filp)) |
| 4473 | return PTR_ERR(filp); |
| 4474 | |
| 4475 | obj->filp = filp; |
| 4476 | |
| 4477 | return 0; |
| 4478 | } |
| 4479 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4480 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4481 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4482 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4483 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4484 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4485 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4486 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4487 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4488 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4489 | /* There is a prevalence of the assumption that we fit the object's |
| 4490 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4491 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4492 | * such a local variable, please consider fixing! |
| 4493 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4494 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4495 | return ERR_PTR(-E2BIG); |
| 4496 | |
| 4497 | if (overflows_type(size, obj->base.size)) |
| 4498 | return ERR_PTR(-E2BIG); |
| 4499 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4500 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4501 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4502 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4503 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4504 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4505 | if (ret) |
| 4506 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4507 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4508 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4509 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4510 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4511 | mask &= ~__GFP_HIGHMEM; |
| 4512 | mask |= __GFP_DMA32; |
| 4513 | } |
| 4514 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4515 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4516 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4517 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4518 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4519 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4520 | |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4521 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 4522 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 4523 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4524 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4525 | /* 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] | 4526 | * cache) for about a 10% performance improvement |
| 4527 | * compared to uncached. Graphics requests other than |
| 4528 | * display scanout are coherent with the CPU in |
| 4529 | * accessing this cache. This means in this mode we |
| 4530 | * don't need to clflush on the CPU side, and on the |
| 4531 | * GPU side we only need to flush internal caches to |
| 4532 | * get data visible to the CPU. |
| 4533 | * |
| 4534 | * However, we maintain the display planes as UC, and so |
| 4535 | * need to rebind when first used as such. |
| 4536 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4537 | cache_level = I915_CACHE_LLC; |
| 4538 | else |
| 4539 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4540 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4541 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4542 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4543 | trace_i915_gem_object_create(obj); |
| 4544 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4545 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4546 | |
| 4547 | fail: |
| 4548 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4549 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4552 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4553 | { |
| 4554 | /* If we are the last user of the backing storage (be it shmemfs |
| 4555 | * pages or stolen etc), we know that the pages are going to be |
| 4556 | * immediately released. In this case, we can then skip copying |
| 4557 | * back the contents from the GPU. |
| 4558 | */ |
| 4559 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4560 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4561 | return false; |
| 4562 | |
| 4563 | if (obj->base.filp == NULL) |
| 4564 | return true; |
| 4565 | |
| 4566 | /* At first glance, this looks racy, but then again so would be |
| 4567 | * userspace racing mmap against close. However, the first external |
| 4568 | * reference to the filp can only be obtained through the |
| 4569 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4570 | * acquiring such a reference whilst we are in the middle of |
| 4571 | * freeing the object. |
| 4572 | */ |
| 4573 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4574 | } |
| 4575 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4576 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4577 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4578 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4579 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4580 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4581 | intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4582 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4583 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4584 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4585 | trace_i915_gem_object_destroy(obj); |
| 4586 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4587 | mutex_lock(&i915->drm.struct_mutex); |
| 4588 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4589 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
| 4590 | list_for_each_entry_safe(vma, vn, |
| 4591 | &obj->vma_list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4592 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4593 | vma->flags &= ~I915_VMA_PIN_MASK; |
| 4594 | i915_vma_close(vma); |
| 4595 | } |
Chris Wilson | db6c2b4 | 2016-11-01 11:54:00 +0000 | [diff] [blame] | 4596 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
| 4597 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4598 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4599 | /* This serializes freeing with the shrinker. Since the free |
| 4600 | * is delayed, first by RCU then by the workqueue, we want the |
| 4601 | * shrinker to be able to free pages of unreferenced objects, |
| 4602 | * or else we may oom whilst there are plenty of deferred |
| 4603 | * freed objects. |
| 4604 | */ |
| 4605 | if (i915_gem_object_has_pages(obj)) { |
| 4606 | spin_lock(&i915->mm.obj_lock); |
| 4607 | list_del_init(&obj->mm.link); |
| 4608 | spin_unlock(&i915->mm.obj_lock); |
| 4609 | } |
| 4610 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4611 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4612 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4613 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4614 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4615 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4616 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4617 | |
| 4618 | if (obj->ops->release) |
| 4619 | obj->ops->release(obj); |
| 4620 | |
| 4621 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4622 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4623 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4624 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4625 | |
| 4626 | if (obj->base.import_attach) |
| 4627 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4628 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4629 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4630 | drm_gem_object_release(&obj->base); |
| 4631 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4632 | |
| 4633 | kfree(obj->bit_17); |
| 4634 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4635 | |
| 4636 | if (on) |
| 4637 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4638 | } |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4639 | intel_runtime_pm_put(i915); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4640 | } |
| 4641 | |
| 4642 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4643 | { |
| 4644 | struct llist_node *freed; |
| 4645 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4646 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4647 | freed = NULL; |
| 4648 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4649 | /* Only one consumer of llist_del_first() allowed */ |
| 4650 | spin_lock(&i915->mm.free_lock); |
| 4651 | freed = llist_del_first(&i915->mm.free_list); |
| 4652 | spin_unlock(&i915->mm.free_lock); |
| 4653 | } |
| 4654 | if (unlikely(freed)) { |
| 4655 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4656 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4657 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | static void __i915_gem_free_work(struct work_struct *work) |
| 4661 | { |
| 4662 | struct drm_i915_private *i915 = |
| 4663 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4664 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4665 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4666 | /* All file-owned VMA should have been released by this point through |
| 4667 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4668 | * However, the object may also be bound into the global GTT (e.g. |
| 4669 | * older GPUs without per-process support, or for direct access through |
| 4670 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4671 | * unbound now. |
| 4672 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4673 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4674 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4675 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4676 | spin_unlock(&i915->mm.free_lock); |
| 4677 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4678 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4679 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4680 | return; |
| 4681 | |
| 4682 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4683 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4684 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4688 | { |
| 4689 | struct drm_i915_gem_object *obj = |
| 4690 | container_of(head, typeof(*obj), rcu); |
| 4691 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4692 | |
| 4693 | /* We can't simply use call_rcu() from i915_gem_free_object() |
| 4694 | * as we need to block whilst unbinding, and the call_rcu |
| 4695 | * task may be called from softirq context. So we take a |
| 4696 | * detour through a worker. |
| 4697 | */ |
| 4698 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
| 4699 | schedule_work(&i915->mm.free_work); |
| 4700 | } |
| 4701 | |
| 4702 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4703 | { |
| 4704 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4705 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4706 | if (obj->mm.quirked) |
| 4707 | __i915_gem_object_unpin_pages(obj); |
| 4708 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4709 | if (discard_backing_storage(obj)) |
| 4710 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4711 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4712 | /* Before we free the object, make sure any pure RCU-only |
| 4713 | * read-side critical sections are complete, e.g. |
| 4714 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4715 | * lookup see i915_gem_object_lookup_rcu(). |
| 4716 | */ |
| 4717 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4718 | } |
| 4719 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4720 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4721 | { |
| 4722 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4723 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4724 | if (!i915_gem_object_has_active_reference(obj) && |
| 4725 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4726 | i915_gem_object_set_active_reference(obj); |
| 4727 | else |
| 4728 | i915_gem_object_put(obj); |
| 4729 | } |
| 4730 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4731 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4732 | { |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4733 | struct i915_gem_context *kernel_context = i915->kernel_context; |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4734 | struct intel_engine_cs *engine; |
| 4735 | enum intel_engine_id id; |
| 4736 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4737 | for_each_engine(engine, i915, id) { |
| 4738 | GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request)); |
| 4739 | GEM_BUG_ON(engine->last_retired_context != kernel_context); |
| 4740 | } |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4741 | } |
| 4742 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4743 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4744 | { |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4745 | if (i915_terminally_wedged(&i915->gpu_error)) { |
| 4746 | mutex_lock(&i915->drm.struct_mutex); |
| 4747 | i915_gem_unset_wedged(i915); |
| 4748 | mutex_unlock(&i915->drm.struct_mutex); |
| 4749 | } |
| 4750 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4751 | /* |
| 4752 | * If we inherit context state from the BIOS or earlier occupants |
| 4753 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4754 | * try to take over. The only way to remove the earlier state |
| 4755 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4756 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4757 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4758 | */ |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4759 | if (INTEL_GEN(i915) >= 5) { |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4760 | int reset = intel_gpu_reset(i915, ALL_ENGINES); |
| 4761 | WARN_ON(reset && reset != -ENODEV); |
| 4762 | } |
| 4763 | } |
| 4764 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4765 | int i915_gem_suspend(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4766 | { |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4767 | struct drm_device *dev = &dev_priv->drm; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4768 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4769 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4770 | intel_runtime_pm_get(dev_priv); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4771 | intel_suspend_gt_powersave(dev_priv); |
| 4772 | |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4773 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4774 | |
| 4775 | /* We have to flush all the executing contexts to main memory so |
| 4776 | * that they can saved in the hibernation image. To ensure the last |
| 4777 | * context image is coherent, we have to switch away from it. That |
| 4778 | * leaves the dev_priv->kernel_context still active when |
| 4779 | * we actually suspend, and its image in memory may not match the GPU |
| 4780 | * state. Fortunately, the kernel_context is disposable and we do |
| 4781 | * not rely on its state. |
| 4782 | */ |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4783 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 4784 | ret = i915_gem_switch_to_kernel_context(dev_priv); |
| 4785 | if (ret) |
| 4786 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4787 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4788 | ret = i915_gem_wait_for_idle(dev_priv, |
| 4789 | I915_WAIT_INTERRUPTIBLE | |
| 4790 | I915_WAIT_LOCKED); |
| 4791 | if (ret && ret != -EIO) |
| 4792 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4793 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4794 | assert_kernel_context_is_current(dev_priv); |
| 4795 | } |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4796 | i915_gem_contexts_lost(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4797 | mutex_unlock(&dev->struct_mutex); |
| 4798 | |
Sagar Arun Kamble | 63987bf | 2017-04-05 15:51:50 +0530 | [diff] [blame] | 4799 | intel_guc_suspend(dev_priv); |
| 4800 | |
Chris Wilson | 737b150 | 2015-01-26 18:03:03 +0200 | [diff] [blame] | 4801 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 4802 | cancel_delayed_work_sync(&dev_priv->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4803 | |
| 4804 | /* As the idle_work is rearming if it detects a race, play safe and |
| 4805 | * repeat the flush until it is definitely idle. |
| 4806 | */ |
Chris Wilson | 7c26240 | 2017-10-06 11:40:38 +0100 | [diff] [blame] | 4807 | drain_delayed_work(&dev_priv->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4808 | |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4809 | /* Assert that we sucessfully flushed all the work and |
| 4810 | * reset the GPU back to its idle, low power state. |
| 4811 | */ |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 4812 | WARN_ON(dev_priv->gt.awake); |
Chris Wilson | fc692bd | 2017-08-26 12:09:35 +0100 | [diff] [blame] | 4813 | if (WARN_ON(!intel_engines_are_idle(dev_priv))) |
| 4814 | i915_gem_set_wedged(dev_priv); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4815 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4816 | /* |
| 4817 | * Neither the BIOS, ourselves or any other kernel |
| 4818 | * expects the system to be in execlists mode on startup, |
| 4819 | * so we need to reset the GPU back to legacy mode. And the only |
| 4820 | * known way to disable logical contexts is through a GPU reset. |
| 4821 | * |
| 4822 | * So in order to leave the system in a known default configuration, |
| 4823 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4824 | * clean up the GEM state tracking, flushing off the requests and |
| 4825 | * leaving the system in a known idle state. |
| 4826 | * |
| 4827 | * Note that is of the upmost importance that the GPU is idle and |
| 4828 | * all stray writes are flushed *before* we dismantle the backing |
| 4829 | * storage for the pinned objects. |
| 4830 | * |
| 4831 | * However, since we are uncertain that resetting the GPU on older |
| 4832 | * machines is a good idea, we don't - just in case it leaves the |
| 4833 | * machine in an unusable condition. |
| 4834 | */ |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4835 | i915_gem_sanitize(dev_priv); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4836 | |
| 4837 | intel_runtime_pm_put(dev_priv); |
| 4838 | return 0; |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4839 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4840 | err_unlock: |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4841 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4842 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4843 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4844 | } |
| 4845 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4846 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4847 | { |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4848 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4849 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4850 | mutex_lock(&i915->drm.struct_mutex); |
| 4851 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4852 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4853 | i915_gem_restore_gtt_mappings(i915); |
| 4854 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4855 | |
| 4856 | /* As we didn't flush the kernel context before suspend, we cannot |
| 4857 | * guarantee that the context image is complete. So let's just reset |
| 4858 | * it and start again. |
| 4859 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4860 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4861 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4862 | if (i915_gem_init_hw(i915)) |
| 4863 | goto err_wedged; |
| 4864 | |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 4865 | intel_guc_resume(i915); |
| 4866 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4867 | /* Always reload a context for powersaving. */ |
| 4868 | if (i915_gem_switch_to_kernel_context(i915)) |
| 4869 | goto err_wedged; |
| 4870 | |
| 4871 | out_unlock: |
| 4872 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 4873 | mutex_unlock(&i915->drm.struct_mutex); |
| 4874 | return; |
| 4875 | |
| 4876 | err_wedged: |
| 4877 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 4878 | i915_gem_set_wedged(i915); |
| 4879 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4880 | } |
| 4881 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4882 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4883 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4884 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4885 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4886 | return; |
| 4887 | |
| 4888 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4889 | DISP_TILE_SURFACE_SWIZZLING); |
| 4890 | |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 4891 | if (IS_GEN5(dev_priv)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4892 | return; |
| 4893 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4894 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 4895 | if (IS_GEN6(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4896 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 4897 | else if (IS_GEN7(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4898 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 4899 | else if (IS_GEN8(dev_priv)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4900 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4901 | else |
| 4902 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4903 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4904 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4905 | 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] | 4906 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4907 | I915_WRITE(RING_CTL(base), 0); |
| 4908 | I915_WRITE(RING_HEAD(base), 0); |
| 4909 | I915_WRITE(RING_TAIL(base), 0); |
| 4910 | I915_WRITE(RING_START(base), 0); |
| 4911 | } |
| 4912 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4913 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4914 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4915 | if (IS_I830(dev_priv)) { |
| 4916 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4917 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4918 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4919 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4920 | init_unused_ring(dev_priv, SRB3_BASE); |
| 4921 | } else if (IS_GEN2(dev_priv)) { |
| 4922 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4923 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4924 | } else if (IS_GEN3(dev_priv)) { |
| 4925 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4926 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4927 | } |
| 4928 | } |
| 4929 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4930 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4931 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4932 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4933 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4934 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4935 | int err; |
| 4936 | |
| 4937 | for_each_engine(engine, i915, id) { |
| 4938 | err = engine->init_hw(engine); |
| 4939 | if (err) |
| 4940 | return err; |
| 4941 | } |
| 4942 | |
| 4943 | return 0; |
| 4944 | } |
| 4945 | |
| 4946 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4947 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4948 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4949 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4950 | dev_priv->gt.last_init_time = ktime_get(); |
| 4951 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4952 | /* Double layer security blanket, see i915_gem_init() */ |
| 4953 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 4954 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4955 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4956 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4957 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4958 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4959 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4960 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4961 | |
Tvrtko Ursulin | 6e26695 | 2016-10-13 11:02:53 +0100 | [diff] [blame] | 4962 | if (HAS_PCH_NOP(dev_priv)) { |
Tvrtko Ursulin | fd6b8f4 | 2016-10-14 10:13:06 +0100 | [diff] [blame] | 4963 | if (IS_IVYBRIDGE(dev_priv)) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 4964 | u32 temp = I915_READ(GEN7_MSG_CTL); |
| 4965 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); |
| 4966 | I915_WRITE(GEN7_MSG_CTL, temp); |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4967 | } else if (INTEL_GEN(dev_priv) >= 7) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 4968 | u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); |
| 4969 | temp &= ~RESET_PCH_HANDSHAKE_ENABLE; |
| 4970 | I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); |
| 4971 | } |
Ben Widawsky | 88a2b2a | 2013-04-05 13:12:43 -0700 | [diff] [blame] | 4972 | } |
| 4973 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4974 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4975 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4976 | /* |
| 4977 | * At least 830 can leave some of the unused rings |
| 4978 | * "active" (ie. head != tail) after resume which |
| 4979 | * will prevent c3 entry. Makes sure all unused rings |
| 4980 | * are totally idle. |
| 4981 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4982 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4983 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4984 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4985 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 4986 | ret = -EIO; |
| 4987 | goto out; |
| 4988 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4989 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4990 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4991 | if (ret) { |
| 4992 | DRM_ERROR("PPGTT enable HW failed %d\n", ret); |
| 4993 | goto out; |
| 4994 | } |
| 4995 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4996 | /* We can't enable contexts until all firmware is loaded */ |
| 4997 | ret = intel_uc_init_hw(dev_priv); |
| 4998 | if (ret) |
| 4999 | goto out; |
| 5000 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5001 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 5002 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 5003 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 5004 | ret = __i915_gem_restart_engines(dev_priv); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5005 | out: |
| 5006 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5007 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5008 | } |
| 5009 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5010 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 5011 | { |
| 5012 | struct i915_gem_context *ctx; |
| 5013 | struct intel_engine_cs *engine; |
| 5014 | enum intel_engine_id id; |
| 5015 | int err; |
| 5016 | |
| 5017 | /* |
| 5018 | * As we reset the gpu during very early sanitisation, the current |
| 5019 | * register state on the GPU should reflect its defaults values. |
| 5020 | * We load a context onto the hw (with restore-inhibit), then switch |
| 5021 | * over to a second context to save that default register state. We |
| 5022 | * can then prime every new context with that state so they all start |
| 5023 | * from the same default HW values. |
| 5024 | */ |
| 5025 | |
| 5026 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 5027 | if (IS_ERR(ctx)) |
| 5028 | return PTR_ERR(ctx); |
| 5029 | |
| 5030 | for_each_engine(engine, i915, id) { |
| 5031 | struct drm_i915_gem_request *rq; |
| 5032 | |
| 5033 | rq = i915_gem_request_alloc(engine, ctx); |
| 5034 | if (IS_ERR(rq)) { |
| 5035 | err = PTR_ERR(rq); |
| 5036 | goto out_ctx; |
| 5037 | } |
| 5038 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 5039 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5040 | if (engine->init_context) |
| 5041 | err = engine->init_context(rq); |
| 5042 | |
| 5043 | __i915_add_request(rq, true); |
| 5044 | if (err) |
| 5045 | goto err_active; |
| 5046 | } |
| 5047 | |
| 5048 | err = i915_gem_switch_to_kernel_context(i915); |
| 5049 | if (err) |
| 5050 | goto err_active; |
| 5051 | |
| 5052 | err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED); |
| 5053 | if (err) |
| 5054 | goto err_active; |
| 5055 | |
| 5056 | assert_kernel_context_is_current(i915); |
| 5057 | |
| 5058 | for_each_engine(engine, i915, id) { |
| 5059 | struct i915_vma *state; |
| 5060 | |
| 5061 | state = ctx->engine[id].state; |
| 5062 | if (!state) |
| 5063 | continue; |
| 5064 | |
| 5065 | /* |
| 5066 | * As we will hold a reference to the logical state, it will |
| 5067 | * not be torn down with the context, and importantly the |
| 5068 | * object will hold onto its vma (making it possible for a |
| 5069 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 5070 | * from the GTT to prevent such accidents and reclaim the |
| 5071 | * space. |
| 5072 | */ |
| 5073 | err = i915_vma_unbind(state); |
| 5074 | if (err) |
| 5075 | goto err_active; |
| 5076 | |
| 5077 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 5078 | if (err) |
| 5079 | goto err_active; |
| 5080 | |
| 5081 | engine->default_state = i915_gem_object_get(state->obj); |
| 5082 | } |
| 5083 | |
| 5084 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 5085 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 5086 | |
| 5087 | /* |
| 5088 | * Make sure that classes with multiple engine instances all |
| 5089 | * share the same basic configuration. |
| 5090 | */ |
| 5091 | for_each_engine(engine, i915, id) { |
| 5092 | unsigned int bit = BIT(engine->uabi_class); |
| 5093 | unsigned int expected = engine->default_state ? bit : 0; |
| 5094 | |
| 5095 | if ((found & bit) != expected) { |
| 5096 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 5097 | engine->uabi_class, engine->name); |
| 5098 | } |
| 5099 | } |
| 5100 | } |
| 5101 | |
| 5102 | out_ctx: |
| 5103 | i915_gem_context_set_closed(ctx); |
| 5104 | i915_gem_context_put(ctx); |
| 5105 | return err; |
| 5106 | |
| 5107 | err_active: |
| 5108 | /* |
| 5109 | * If we have to abandon now, we expect the engines to be idle |
| 5110 | * and ready to be torn-down. First try to flush any remaining |
| 5111 | * request, ensure we are pointing at the kernel context and |
| 5112 | * then remove it. |
| 5113 | */ |
| 5114 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 5115 | goto out_ctx; |
| 5116 | |
| 5117 | if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED))) |
| 5118 | goto out_ctx; |
| 5119 | |
| 5120 | i915_gem_contexts_lost(i915); |
| 5121 | goto out_ctx; |
| 5122 | } |
| 5123 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5124 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5125 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5126 | int ret; |
| 5127 | |
Matthew Auld | da9fe3f3 | 2017-10-06 23:18:31 +0100 | [diff] [blame] | 5128 | /* |
| 5129 | * We need to fallback to 4K pages since gvt gtt handling doesn't |
| 5130 | * support huge page entries - we will need to check either hypervisor |
| 5131 | * mm can support huge guest page or just do emulation in gvt. |
| 5132 | */ |
| 5133 | if (intel_vgpu_active(dev_priv)) |
| 5134 | mkwrite_device_info(dev_priv)->page_sizes = |
| 5135 | I915_GTT_PAGE_SIZE_4K; |
| 5136 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 5137 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 5138 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5139 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 5140 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5141 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5142 | } else { |
| 5143 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5144 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5145 | } |
| 5146 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5147 | ret = i915_gem_init_userptr(dev_priv); |
| 5148 | if (ret) |
| 5149 | return ret; |
| 5150 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5151 | /* This is just a security blanket to placate dragons. |
| 5152 | * On some systems, we very sporadically observe that the first TLBs |
| 5153 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5154 | * we hold the forcewake during initialisation these problems |
| 5155 | * just magically go away. |
| 5156 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5157 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5158 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5159 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5160 | ret = i915_gem_init_ggtt(dev_priv); |
| 5161 | if (ret) |
| 5162 | goto out_unlock; |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5163 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5164 | ret = i915_gem_contexts_init(dev_priv); |
Jani Nikula | 7bcc377 | 2014-12-05 14:17:42 +0200 | [diff] [blame] | 5165 | if (ret) |
| 5166 | goto out_unlock; |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5167 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5168 | ret = intel_engines_init(dev_priv); |
Daniel Vetter | 35a57ff | 2014-11-20 00:33:07 +0100 | [diff] [blame] | 5169 | if (ret) |
Jani Nikula | 7bcc377 | 2014-12-05 14:17:42 +0200 | [diff] [blame] | 5170 | goto out_unlock; |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5171 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5172 | intel_init_gt_powersave(dev_priv); |
| 5173 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5174 | ret = i915_gem_init_hw(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5175 | if (ret) |
| 5176 | goto out_unlock; |
| 5177 | |
| 5178 | /* |
| 5179 | * Despite its name intel_init_clock_gating applies both display |
| 5180 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5181 | * GT power context workaround. Worse, sometimes it includes a context |
| 5182 | * register workaround which we need to apply before we record the |
| 5183 | * default HW state for all contexts. |
| 5184 | * |
| 5185 | * FIXME: break up the workarounds and apply them at the right time! |
| 5186 | */ |
| 5187 | intel_init_clock_gating(dev_priv); |
| 5188 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5189 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5190 | out_unlock: |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5191 | if (ret == -EIO) { |
Chris Wilson | 7e21d64 | 2016-07-27 09:07:29 +0100 | [diff] [blame] | 5192 | /* Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5193 | * wedged. But we only want to do this where the GPU is angry, |
| 5194 | * for all other failure, such as an allocation failure, bail. |
| 5195 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5196 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5197 | DRM_ERROR("Failed to initialize GPU, declaring it wedged\n"); |
| 5198 | i915_gem_set_wedged(dev_priv); |
| 5199 | } |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5200 | ret = 0; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5201 | } |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5202 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5203 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5204 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5205 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5206 | } |
| 5207 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5208 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5209 | { |
| 5210 | i915_gem_sanitize(i915); |
| 5211 | } |
| 5212 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5213 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5214 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5215 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5216 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5217 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5218 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5219 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5220 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5221 | } |
| 5222 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5223 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5224 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5225 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5226 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5227 | |
| 5228 | if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) && |
| 5229 | !IS_CHERRYVIEW(dev_priv)) |
| 5230 | dev_priv->num_fence_regs = 32; |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5231 | else if (INTEL_INFO(dev_priv)->gen >= 4 || |
| 5232 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5233 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5234 | dev_priv->num_fence_regs = 16; |
| 5235 | else |
| 5236 | dev_priv->num_fence_regs = 8; |
| 5237 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5238 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5239 | dev_priv->num_fence_regs = |
| 5240 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5241 | |
| 5242 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5243 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5244 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5245 | |
| 5246 | fence->i915 = dev_priv; |
| 5247 | fence->id = i; |
| 5248 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5249 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5250 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5251 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5252 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5253 | } |
| 5254 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5255 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5256 | { |
| 5257 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5258 | spin_lock_init(&i915->mm.obj_lock); |
| 5259 | spin_lock_init(&i915->mm.free_lock); |
| 5260 | |
| 5261 | init_llist_head(&i915->mm.free_list); |
| 5262 | |
| 5263 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5264 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5265 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5266 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5267 | |
| 5268 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5269 | } |
| 5270 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5271 | int |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5272 | i915_gem_load_init(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5273 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5274 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5275 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5276 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5277 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5278 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5279 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5280 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5281 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5282 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5283 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5284 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5285 | if (!dev_priv->luts) |
| 5286 | goto err_vmas; |
| 5287 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5288 | dev_priv->requests = KMEM_CACHE(drm_i915_gem_request, |
| 5289 | SLAB_HWCACHE_ALIGN | |
| 5290 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5291 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5292 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5293 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5294 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5295 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5296 | SLAB_HWCACHE_ALIGN | |
| 5297 | SLAB_RECLAIM_ACCOUNT); |
| 5298 | if (!dev_priv->dependencies) |
| 5299 | goto err_requests; |
| 5300 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5301 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5302 | if (!dev_priv->priorities) |
| 5303 | goto err_dependencies; |
| 5304 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5305 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5306 | INIT_LIST_HEAD(&dev_priv->gt.timelines); |
Chris Wilson | bb89485 | 2016-11-14 20:40:57 +0000 | [diff] [blame] | 5307 | err = i915_gem_timeline_init__global(dev_priv); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5308 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5309 | if (err) |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5310 | goto err_priorities; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5311 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5312 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5313 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5314 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5315 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5316 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5317 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5318 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5319 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5320 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5321 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5322 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5323 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5324 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5325 | err = i915_gemfs_init(dev_priv); |
| 5326 | if (err) |
| 5327 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5328 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5329 | return 0; |
| 5330 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5331 | err_priorities: |
| 5332 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5333 | err_dependencies: |
| 5334 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5335 | err_requests: |
| 5336 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5337 | err_luts: |
| 5338 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5339 | err_vmas: |
| 5340 | kmem_cache_destroy(dev_priv->vmas); |
| 5341 | err_objects: |
| 5342 | kmem_cache_destroy(dev_priv->objects); |
| 5343 | err_out: |
| 5344 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5345 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5346 | |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5347 | void i915_gem_load_cleanup(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5348 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5349 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 7d5d59e | 2016-11-01 08:48:41 +0000 | [diff] [blame] | 5350 | WARN_ON(!llist_empty(&dev_priv->mm.free_list)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5351 | WARN_ON(dev_priv->mm.object_count); |
Chris Wilson | 7d5d59e | 2016-11-01 08:48:41 +0000 | [diff] [blame] | 5352 | |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5353 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5354 | i915_gem_timeline_fini(&dev_priv->gt.global_timeline); |
| 5355 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); |
| 5356 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5357 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5358 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5359 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5360 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5361 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5362 | kmem_cache_destroy(dev_priv->vmas); |
| 5363 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5364 | |
| 5365 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5366 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5367 | |
| 5368 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5369 | } |
| 5370 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5371 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5372 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5373 | /* Discard all purgeable objects, let userspace recover those as |
| 5374 | * required after resuming. |
| 5375 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5376 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5377 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5378 | return 0; |
| 5379 | } |
| 5380 | |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5381 | int i915_gem_freeze_late(struct drm_i915_private *dev_priv) |
| 5382 | { |
| 5383 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5384 | struct list_head *phases[] = { |
| 5385 | &dev_priv->mm.unbound_list, |
| 5386 | &dev_priv->mm.bound_list, |
| 5387 | NULL |
| 5388 | }, **p; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5389 | |
| 5390 | /* Called just before we write the hibernation image. |
| 5391 | * |
| 5392 | * We need to update the domain tracking to reflect that the CPU |
| 5393 | * will be accessing all the pages to create and restore from the |
| 5394 | * hibernation, and so upon restoration those pages will be in the |
| 5395 | * CPU domain. |
| 5396 | * |
| 5397 | * To make sure the hibernation image contains the latest state, |
| 5398 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5399 | * |
| 5400 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5401 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5402 | */ |
| 5403 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 5404 | i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND); |
Chris Wilson | 17b93c4 | 2017-04-07 11:25:50 +0100 | [diff] [blame] | 5405 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5406 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5407 | spin_lock(&dev_priv->mm.obj_lock); |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5408 | for (p = phases; *p; p++) { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5409 | list_for_each_entry(obj, *p, mm.link) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 5410 | __start_cpu_write(obj); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5411 | } |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5412 | spin_unlock(&dev_priv->mm.obj_lock); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5413 | |
| 5414 | return 0; |
| 5415 | } |
| 5416 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5417 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5418 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5419 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | 15f7bbc | 2016-07-26 12:01:52 +0100 | [diff] [blame] | 5420 | struct drm_i915_gem_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5421 | |
| 5422 | /* Clean up our request list when the client is going away, so that |
| 5423 | * later retire_requests won't dereference our soon-to-be-gone |
| 5424 | * file_priv. |
| 5425 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5426 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5427 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5428 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5429 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5430 | } |
| 5431 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5432 | 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] | 5433 | { |
| 5434 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5435 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5436 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5437 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5438 | |
| 5439 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5440 | if (!file_priv) |
| 5441 | return -ENOMEM; |
| 5442 | |
| 5443 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5444 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5445 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5446 | |
| 5447 | spin_lock_init(&file_priv->mm.lock); |
| 5448 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5449 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5450 | file_priv->bsd_engine = -1; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5451 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5452 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5453 | if (ret) |
| 5454 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5455 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5456 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5457 | } |
| 5458 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5459 | /** |
| 5460 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5461 | * @old: current GEM buffer for the frontbuffer slots |
| 5462 | * @new: new GEM buffer for the frontbuffer slots |
| 5463 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5464 | * |
| 5465 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5466 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5467 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5468 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5469 | struct drm_i915_gem_object *new, |
| 5470 | unsigned frontbuffer_bits) |
| 5471 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5472 | /* Control of individual bits within the mask are guarded by |
| 5473 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5474 | * manipulation of individual bits. But since the bitfield as a whole |
| 5475 | * is updated using RMW, we need to use atomics in order to update |
| 5476 | * the bits. |
| 5477 | */ |
| 5478 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
| 5479 | sizeof(atomic_t) * BITS_PER_BYTE); |
| 5480 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5481 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5482 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5483 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5487 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5488 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5489 | } |
| 5490 | } |
| 5491 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5492 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5493 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5494 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5495 | const void *data, size_t size) |
| 5496 | { |
| 5497 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5498 | struct file *file; |
| 5499 | size_t offset; |
| 5500 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5501 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5502 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5503 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5504 | return obj; |
| 5505 | |
Chris Wilson | ce8ff09 | 2017-03-17 19:46:47 +0000 | [diff] [blame] | 5506 | GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5507 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5508 | file = obj->base.filp; |
| 5509 | offset = 0; |
| 5510 | do { |
| 5511 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5512 | struct page *page; |
| 5513 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5514 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5515 | err = pagecache_write_begin(file, file->f_mapping, |
| 5516 | offset, len, 0, |
| 5517 | &page, &pgdata); |
| 5518 | if (err < 0) |
| 5519 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5520 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5521 | vaddr = kmap(page); |
| 5522 | memcpy(vaddr, data, len); |
| 5523 | kunmap(page); |
| 5524 | |
| 5525 | err = pagecache_write_end(file, file->f_mapping, |
| 5526 | offset, len, len, |
| 5527 | page, pgdata); |
| 5528 | if (err < 0) |
| 5529 | goto fail; |
| 5530 | |
| 5531 | size -= len; |
| 5532 | data += len; |
| 5533 | offset += len; |
| 5534 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5535 | |
| 5536 | return obj; |
| 5537 | |
| 5538 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5539 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5540 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5541 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5542 | |
| 5543 | struct scatterlist * |
| 5544 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5545 | unsigned int n, |
| 5546 | unsigned int *offset) |
| 5547 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5548 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5549 | struct scatterlist *sg; |
| 5550 | unsigned int idx, count; |
| 5551 | |
| 5552 | might_sleep(); |
| 5553 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5554 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5555 | |
| 5556 | /* As we iterate forward through the sg, we record each entry in a |
| 5557 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5558 | * this index previously, we will have an entry for it. |
| 5559 | * |
| 5560 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5561 | * sequential page access (where each new request is consecutive |
| 5562 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5563 | * i.e. O(1) with a large constant! |
| 5564 | */ |
| 5565 | if (n < READ_ONCE(iter->sg_idx)) |
| 5566 | goto lookup; |
| 5567 | |
| 5568 | mutex_lock(&iter->lock); |
| 5569 | |
| 5570 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5571 | * (or the subsequent) sg are fast - comparing against the last |
| 5572 | * sg is faster than going through the radixtree. |
| 5573 | */ |
| 5574 | |
| 5575 | sg = iter->sg_pos; |
| 5576 | idx = iter->sg_idx; |
| 5577 | count = __sg_page_count(sg); |
| 5578 | |
| 5579 | while (idx + count <= n) { |
| 5580 | unsigned long exception, i; |
| 5581 | int ret; |
| 5582 | |
| 5583 | /* If we cannot allocate and insert this entry, or the |
| 5584 | * individual pages from this range, cancel updating the |
| 5585 | * sg_idx so that on this lookup we are forced to linearly |
| 5586 | * scan onwards, but on future lookups we will try the |
| 5587 | * insertion again (in which case we need to be careful of |
| 5588 | * the error return reporting that we have already inserted |
| 5589 | * this index). |
| 5590 | */ |
| 5591 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5592 | if (ret && ret != -EEXIST) |
| 5593 | goto scan; |
| 5594 | |
| 5595 | exception = |
| 5596 | RADIX_TREE_EXCEPTIONAL_ENTRY | |
| 5597 | idx << RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5598 | for (i = 1; i < count; i++) { |
| 5599 | ret = radix_tree_insert(&iter->radix, idx + i, |
| 5600 | (void *)exception); |
| 5601 | if (ret && ret != -EEXIST) |
| 5602 | goto scan; |
| 5603 | } |
| 5604 | |
| 5605 | idx += count; |
| 5606 | sg = ____sg_next(sg); |
| 5607 | count = __sg_page_count(sg); |
| 5608 | } |
| 5609 | |
| 5610 | scan: |
| 5611 | iter->sg_pos = sg; |
| 5612 | iter->sg_idx = idx; |
| 5613 | |
| 5614 | mutex_unlock(&iter->lock); |
| 5615 | |
| 5616 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5617 | goto lookup; |
| 5618 | |
| 5619 | /* In case we failed to insert the entry into the radixtree, we need |
| 5620 | * to look beyond the current sg. |
| 5621 | */ |
| 5622 | while (idx + count <= n) { |
| 5623 | idx += count; |
| 5624 | sg = ____sg_next(sg); |
| 5625 | count = __sg_page_count(sg); |
| 5626 | } |
| 5627 | |
| 5628 | *offset = n - idx; |
| 5629 | return sg; |
| 5630 | |
| 5631 | lookup: |
| 5632 | rcu_read_lock(); |
| 5633 | |
| 5634 | sg = radix_tree_lookup(&iter->radix, n); |
| 5635 | GEM_BUG_ON(!sg); |
| 5636 | |
| 5637 | /* If this index is in the middle of multi-page sg entry, |
| 5638 | * the radixtree will contain an exceptional entry that points |
| 5639 | * to the start of that range. We will return the pointer to |
| 5640 | * the base page and the offset of this page within the |
| 5641 | * sg entry's range. |
| 5642 | */ |
| 5643 | *offset = 0; |
| 5644 | if (unlikely(radix_tree_exception(sg))) { |
| 5645 | unsigned long base = |
| 5646 | (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5647 | |
| 5648 | sg = radix_tree_lookup(&iter->radix, base); |
| 5649 | GEM_BUG_ON(!sg); |
| 5650 | |
| 5651 | *offset = n - base; |
| 5652 | } |
| 5653 | |
| 5654 | rcu_read_unlock(); |
| 5655 | |
| 5656 | return sg; |
| 5657 | } |
| 5658 | |
| 5659 | struct page * |
| 5660 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5661 | { |
| 5662 | struct scatterlist *sg; |
| 5663 | unsigned int offset; |
| 5664 | |
| 5665 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5666 | |
| 5667 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5668 | return nth_page(sg_page(sg), offset); |
| 5669 | } |
| 5670 | |
| 5671 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5672 | struct page * |
| 5673 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5674 | unsigned int n) |
| 5675 | { |
| 5676 | struct page *page; |
| 5677 | |
| 5678 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5679 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5680 | set_page_dirty(page); |
| 5681 | |
| 5682 | return page; |
| 5683 | } |
| 5684 | |
| 5685 | dma_addr_t |
| 5686 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5687 | unsigned long n) |
| 5688 | { |
| 5689 | struct scatterlist *sg; |
| 5690 | unsigned int offset; |
| 5691 | |
| 5692 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5693 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5694 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5695 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5696 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5697 | { |
| 5698 | struct sg_table *pages; |
| 5699 | int err; |
| 5700 | |
| 5701 | if (align > obj->base.size) |
| 5702 | return -EINVAL; |
| 5703 | |
| 5704 | if (obj->ops == &i915_gem_phys_ops) |
| 5705 | return 0; |
| 5706 | |
| 5707 | if (obj->ops != &i915_gem_object_ops) |
| 5708 | return -EINVAL; |
| 5709 | |
| 5710 | err = i915_gem_object_unbind(obj); |
| 5711 | if (err) |
| 5712 | return err; |
| 5713 | |
| 5714 | mutex_lock(&obj->mm.lock); |
| 5715 | |
| 5716 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5717 | err = -EFAULT; |
| 5718 | goto err_unlock; |
| 5719 | } |
| 5720 | |
| 5721 | if (obj->mm.quirked) { |
| 5722 | err = -EFAULT; |
| 5723 | goto err_unlock; |
| 5724 | } |
| 5725 | |
| 5726 | if (obj->mm.mapping) { |
| 5727 | err = -EBUSY; |
| 5728 | goto err_unlock; |
| 5729 | } |
| 5730 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5731 | pages = fetch_and_zero(&obj->mm.pages); |
| 5732 | if (pages) { |
| 5733 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 5734 | |
| 5735 | __i915_gem_object_reset_page_iter(obj); |
| 5736 | |
| 5737 | spin_lock(&i915->mm.obj_lock); |
| 5738 | list_del(&obj->mm.link); |
| 5739 | spin_unlock(&i915->mm.obj_lock); |
| 5740 | } |
| 5741 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5742 | obj->ops = &i915_gem_phys_ops; |
| 5743 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5744 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5745 | if (err) |
| 5746 | goto err_xfer; |
| 5747 | |
| 5748 | /* Perma-pin (until release) the physical set of pages */ |
| 5749 | __i915_gem_object_pin_pages(obj); |
| 5750 | |
| 5751 | if (!IS_ERR_OR_NULL(pages)) |
| 5752 | i915_gem_object_ops.put_pages(obj, pages); |
| 5753 | mutex_unlock(&obj->mm.lock); |
| 5754 | return 0; |
| 5755 | |
| 5756 | err_xfer: |
| 5757 | obj->ops = &i915_gem_object_ops; |
| 5758 | obj->mm.pages = pages; |
| 5759 | err_unlock: |
| 5760 | mutex_unlock(&obj->mm.lock); |
| 5761 | return err; |
| 5762 | } |
| 5763 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5764 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5765 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5766 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5767 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5768 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5769 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5770 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5771 | #endif |