Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
Daniel Vetter | be6a037 | 2015-03-18 10:46:04 +0100 | [diff] [blame] | 2 | * Copyright © 2008-2015 Intel Corporation |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 28 | #include <drm/drm_vma_manager.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 29 | #include <drm/drm_pci.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/i915_drm.h> |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 31 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 32 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 33 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 34 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 36 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 37 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 38 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 39 | #include <linux/dma-buf.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 40 | #include <linux/mman.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 41 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 42 | #include "gt/intel_engine_pm.h" |
| 43 | #include "gt/intel_gt_pm.h" |
Chris Wilson | 112ed2d | 2019-04-24 18:48:39 +0100 | [diff] [blame] | 44 | #include "gt/intel_mocs.h" |
| 45 | #include "gt/intel_reset.h" |
| 46 | #include "gt/intel_workarounds.h" |
| 47 | |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 48 | #include "i915_drv.h" |
| 49 | #include "i915_gem_clflush.h" |
| 50 | #include "i915_gemfs.h" |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 51 | #include "i915_gem_pm.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 52 | #include "i915_trace.h" |
| 53 | #include "i915_vgpu.h" |
| 54 | |
| 55 | #include "intel_drv.h" |
| 56 | #include "intel_frontbuffer.h" |
Jani Nikula | 696173b | 2019-04-05 14:00:15 +0300 | [diff] [blame] | 57 | #include "intel_pm.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 58 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 59 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 60 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 61 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 62 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 63 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 64 | return false; |
| 65 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 66 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 67 | return true; |
| 68 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 69 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 72 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 73 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 74 | struct drm_mm_node *node, u32 size) |
| 75 | { |
| 76 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 77 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 78 | size, 0, I915_COLOR_UNEVICTABLE, |
| 79 | 0, ggtt->mappable_end, |
| 80 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void |
| 84 | remove_mappable_node(struct drm_mm_node *node) |
| 85 | { |
| 86 | drm_mm_remove_node(node); |
| 87 | } |
| 88 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 89 | /* some bookkeeping */ |
| 90 | 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] | 91 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 92 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 93 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 94 | dev_priv->mm.object_count++; |
| 95 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 96 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | 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] | 100 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 101 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 102 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 103 | dev_priv->mm.object_count--; |
| 104 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 105 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 108 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 109 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 110 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 111 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 112 | struct i915_ggtt *ggtt = &to_i915(dev)->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 113 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 114 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 115 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 116 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 117 | mutex_lock(&ggtt->vm.mutex); |
| 118 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 119 | pinned = ggtt->vm.reserved; |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 120 | list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 121 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 122 | pinned += vma->node.size; |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 123 | |
| 124 | mutex_unlock(&ggtt->vm.mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 125 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 126 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 127 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 128 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 132 | 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] | 133 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 134 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 135 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 136 | struct sg_table *st; |
| 137 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 138 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 139 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 140 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 141 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 142 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 143 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 144 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 145 | /* Always aligning to the object size, allows a single allocation |
| 146 | * to handle all possible callers, and given typical object sizes, |
| 147 | * the alignment of the buddy allocation will naturally match. |
| 148 | */ |
| 149 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 150 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 151 | roundup_pow_of_two(obj->base.size)); |
| 152 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 153 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 154 | |
| 155 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 156 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 157 | struct page *page; |
| 158 | char *src; |
| 159 | |
| 160 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 161 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 162 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 163 | goto err_phys; |
| 164 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 165 | |
| 166 | src = kmap_atomic(page); |
| 167 | memcpy(vaddr, src, PAGE_SIZE); |
| 168 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 169 | kunmap_atomic(src); |
| 170 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 171 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 172 | vaddr += PAGE_SIZE; |
| 173 | } |
| 174 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 175 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 176 | |
| 177 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 178 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 179 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 180 | goto err_phys; |
| 181 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 182 | |
| 183 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 184 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 185 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 186 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | sg = st->sgl; |
| 190 | sg->offset = 0; |
| 191 | sg->length = obj->base.size; |
| 192 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 193 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 194 | sg_dma_len(sg) = obj->base.size; |
| 195 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 196 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 197 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 198 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 199 | |
| 200 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 201 | |
| 202 | err_phys: |
| 203 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 204 | |
| 205 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 208 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 209 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 210 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 211 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 212 | if (cpu_write_needs_clflush(obj)) |
| 213 | obj->cache_dirty = true; |
| 214 | } |
| 215 | |
Chris Wilson | ee8efa8 | 2019-03-31 10:46:20 +0100 | [diff] [blame] | 216 | void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 217 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 218 | struct sg_table *pages, |
| 219 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 220 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 221 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 222 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 223 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 224 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 225 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 226 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 227 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 228 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 229 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 230 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 231 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void |
| 235 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 236 | struct sg_table *pages) |
| 237 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 238 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 239 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 240 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 241 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 242 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 243 | int i; |
| 244 | |
| 245 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 246 | struct page *page; |
| 247 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 248 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 249 | page = shmem_read_mapping_page(mapping, i); |
| 250 | if (IS_ERR(page)) |
| 251 | continue; |
| 252 | |
| 253 | dst = kmap_atomic(page); |
| 254 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 255 | memcpy(dst, vaddr, PAGE_SIZE); |
| 256 | kunmap_atomic(dst); |
| 257 | |
| 258 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 259 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 260 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 261 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 262 | vaddr += PAGE_SIZE; |
| 263 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 264 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 267 | sg_free_table(pages); |
| 268 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 269 | |
| 270 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void |
| 274 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 275 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 276 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 280 | .get_pages = i915_gem_object_get_pages_phys, |
| 281 | .put_pages = i915_gem_object_put_pages_phys, |
| 282 | .release = i915_gem_object_release_phys, |
| 283 | }; |
| 284 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 285 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 286 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 287 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 288 | { |
| 289 | struct i915_vma *vma; |
| 290 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 291 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 292 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 293 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 294 | |
| 295 | /* Closed vma are removed from the obj->vma_list - but they may |
| 296 | * still have an active binding on the object. To remove those we |
| 297 | * must wait for all rendering to complete to the object (as unbinding |
| 298 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 299 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 300 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 301 | if (ret) |
| 302 | return ret; |
| 303 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 304 | spin_lock(&obj->vma.lock); |
| 305 | while (!ret && (vma = list_first_entry_or_null(&obj->vma.list, |
| 306 | struct i915_vma, |
| 307 | obj_link))) { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 308 | list_move_tail(&vma->obj_link, &still_in_list); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 309 | spin_unlock(&obj->vma.lock); |
| 310 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 311 | ret = i915_vma_unbind(vma); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 312 | |
| 313 | spin_lock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 314 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 315 | list_splice(&still_in_list, &obj->vma.list); |
| 316 | spin_unlock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 321 | static long |
| 322 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 323 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 324 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 325 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 326 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 327 | |
| 328 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 329 | |
| 330 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 331 | return timeout; |
| 332 | |
| 333 | if (!dma_fence_is_i915(fence)) |
| 334 | return dma_fence_wait_timeout(fence, |
| 335 | flags & I915_WAIT_INTERRUPTIBLE, |
| 336 | timeout); |
| 337 | |
| 338 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 339 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 340 | goto out; |
| 341 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 342 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 343 | |
| 344 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 345 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 346 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 347 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 348 | return timeout; |
| 349 | } |
| 350 | |
| 351 | static long |
| 352 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 353 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 354 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 355 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 356 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 357 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 358 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 359 | |
| 360 | if (flags & I915_WAIT_ALL) { |
| 361 | struct dma_fence **shared; |
| 362 | unsigned int count, i; |
| 363 | int ret; |
| 364 | |
| 365 | ret = reservation_object_get_fences_rcu(resv, |
| 366 | &excl, &count, &shared); |
| 367 | if (ret) |
| 368 | return ret; |
| 369 | |
| 370 | for (i = 0; i < count; i++) { |
| 371 | timeout = i915_gem_object_wait_fence(shared[i], |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 372 | flags, timeout); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 373 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 374 | break; |
| 375 | |
| 376 | dma_fence_put(shared[i]); |
| 377 | } |
| 378 | |
| 379 | for (; i < count; i++) |
| 380 | dma_fence_put(shared[i]); |
| 381 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 382 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 383 | /* |
| 384 | * If both shared fences and an exclusive fence exist, |
| 385 | * then by construction the shared fences must be later |
| 386 | * than the exclusive fence. If we successfully wait for |
| 387 | * all the shared fences, we know that the exclusive fence |
| 388 | * must all be signaled. If all the shared fences are |
| 389 | * signaled, we can prune the array and recover the |
| 390 | * floating references on the fences/requests. |
| 391 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 392 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 393 | } else { |
| 394 | excl = reservation_object_get_excl_rcu(resv); |
| 395 | } |
| 396 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 397 | if (excl && timeout >= 0) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 398 | timeout = i915_gem_object_wait_fence(excl, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 399 | |
| 400 | dma_fence_put(excl); |
| 401 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 402 | /* |
| 403 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 404 | * signaled and that the reservation object has not been changed (i.e. |
| 405 | * no new fences have been added). |
| 406 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 407 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 408 | if (reservation_object_trylock(resv)) { |
| 409 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 410 | reservation_object_add_excl_fence(resv, NULL); |
| 411 | reservation_object_unlock(resv); |
| 412 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 415 | return timeout; |
| 416 | } |
| 417 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 418 | static void __fence_set_priority(struct dma_fence *fence, |
| 419 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 420 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 421 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 422 | struct intel_engine_cs *engine; |
| 423 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 424 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 425 | return; |
| 426 | |
| 427 | rq = to_request(fence); |
| 428 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 429 | |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 430 | local_bh_disable(); |
| 431 | rcu_read_lock(); /* RCU serialisation for set-wedged protection */ |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 432 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 433 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 434 | rcu_read_unlock(); |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 435 | local_bh_enable(); /* kick the tasklets if queues were reprioritised */ |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 438 | static void fence_set_priority(struct dma_fence *fence, |
| 439 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 440 | { |
| 441 | /* Recurse once into a fence-array */ |
| 442 | if (dma_fence_is_array(fence)) { |
| 443 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 444 | int i; |
| 445 | |
| 446 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 447 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 448 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 449 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | int |
| 454 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 455 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 456 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 457 | { |
| 458 | struct dma_fence *excl; |
| 459 | |
| 460 | if (flags & I915_WAIT_ALL) { |
| 461 | struct dma_fence **shared; |
| 462 | unsigned int count, i; |
| 463 | int ret; |
| 464 | |
| 465 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 466 | &excl, &count, &shared); |
| 467 | if (ret) |
| 468 | return ret; |
| 469 | |
| 470 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 471 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 472 | dma_fence_put(shared[i]); |
| 473 | } |
| 474 | |
| 475 | kfree(shared); |
| 476 | } else { |
| 477 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 478 | } |
| 479 | |
| 480 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 481 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 482 | dma_fence_put(excl); |
| 483 | } |
| 484 | return 0; |
| 485 | } |
| 486 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 487 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 488 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 489 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 490 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 491 | * @timeout: how long to wait |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 492 | */ |
| 493 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 494 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 495 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 496 | long timeout) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 497 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 498 | might_sleep(); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 499 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 500 | |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 501 | timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 502 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 505 | static int |
| 506 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 507 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 508 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 509 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 510 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 511 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 512 | |
| 513 | /* We manually control the domain here and pretend that it |
| 514 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 515 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 516 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 517 | if (copy_from_user(vaddr, user_data, args->size)) |
| 518 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 519 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 520 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 521 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 522 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 523 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 524 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 527 | static int |
| 528 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 529 | struct drm_i915_private *dev_priv, |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 530 | u64 *size_p, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 531 | u32 *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 532 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 533 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 534 | u32 handle; |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 535 | u64 size; |
| 536 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 537 | |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 538 | size = round_up(*size_p, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 539 | if (size == 0) |
| 540 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 541 | |
| 542 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 543 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 544 | if (IS_ERR(obj)) |
| 545 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 546 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 547 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 548 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 549 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 550 | if (ret) |
| 551 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 552 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 553 | *handle_p = handle; |
Chris Wilson | 9953402 | 2019-04-17 14:25:07 +0100 | [diff] [blame] | 554 | *size_p = size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 558 | int |
| 559 | i915_gem_dumb_create(struct drm_file *file, |
| 560 | struct drm_device *dev, |
| 561 | struct drm_mode_create_dumb *args) |
| 562 | { |
| 563 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 564 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 565 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 566 | return i915_gem_create(file, to_i915(dev), |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 567 | &args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 568 | } |
| 569 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 570 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 571 | { |
| 572 | return !(obj->cache_level == I915_CACHE_NONE || |
| 573 | obj->cache_level == I915_CACHE_WT); |
| 574 | } |
| 575 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 576 | /** |
| 577 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 578 | * @dev: drm device pointer |
| 579 | * @data: ioctl data blob |
| 580 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 581 | */ |
| 582 | int |
| 583 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 584 | struct drm_file *file) |
| 585 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 586 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 587 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 588 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 589 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 590 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 591 | return i915_gem_create(file, dev_priv, |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 592 | &args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 593 | } |
| 594 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 595 | static inline enum fb_op_origin |
| 596 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 597 | { |
| 598 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 599 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 600 | } |
| 601 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 602 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 603 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 604 | intel_wakeref_t wakeref; |
| 605 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 606 | /* |
| 607 | * No actual flushing is required for the GTT write domain for reads |
| 608 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 609 | * as far as we know, so there's no chipset flush. It also doesn't |
| 610 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 611 | * |
| 612 | * However, we do have to enforce the order so that all writes through |
| 613 | * the GTT land before any writes to the device, such as updates to |
| 614 | * the GATT itself. |
| 615 | * |
| 616 | * We also have to wait a bit for the writes to land from the GTT. |
| 617 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 618 | * timing. This issue has only been observed when switching quickly |
| 619 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 620 | * and it appears to only affect discrete GTT blocks (i.e. on LLC |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 621 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 622 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 623 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 624 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 625 | wmb(); |
| 626 | |
| 627 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 628 | return; |
| 629 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 630 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 631 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 632 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 633 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 634 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 635 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 636 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 637 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 638 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static void |
| 642 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 643 | { |
| 644 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 645 | struct i915_vma *vma; |
| 646 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 647 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 648 | return; |
| 649 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 650 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 651 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 652 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 653 | |
| 654 | intel_fb_obj_flush(obj, |
| 655 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 656 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 657 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 658 | if (vma->iomap) |
| 659 | continue; |
| 660 | |
| 661 | i915_vma_unset_ggtt_write(vma); |
| 662 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 663 | break; |
| 664 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 665 | case I915_GEM_DOMAIN_WC: |
| 666 | wmb(); |
| 667 | break; |
| 668 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 669 | case I915_GEM_DOMAIN_CPU: |
| 670 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 671 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 672 | |
| 673 | case I915_GEM_DOMAIN_RENDER: |
| 674 | if (gpu_write_needs_clflush(obj)) |
| 675 | obj->cache_dirty = true; |
| 676 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 679 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 682 | /* |
| 683 | * Pins the specified object's pages and synchronizes the object with |
| 684 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 685 | * flush the object from the CPU cache. |
| 686 | */ |
| 687 | 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] | 688 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 689 | { |
| 690 | int ret; |
| 691 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 692 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 693 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 694 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 695 | if (!i915_gem_object_has_struct_page(obj)) |
| 696 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 697 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 698 | ret = i915_gem_object_wait(obj, |
| 699 | I915_WAIT_INTERRUPTIBLE | |
| 700 | I915_WAIT_LOCKED, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 701 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 702 | if (ret) |
| 703 | return ret; |
| 704 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 705 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 706 | if (ret) |
| 707 | return ret; |
| 708 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 709 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 710 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 711 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 712 | if (ret) |
| 713 | goto err_unpin; |
| 714 | else |
| 715 | goto out; |
| 716 | } |
| 717 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 718 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 719 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 720 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 721 | * read domain and manually flush cachelines (if required). This |
| 722 | * optimizes for the case when the gpu will dirty the data |
| 723 | * anyway again before the next pread happens. |
| 724 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 725 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 726 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 727 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 728 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 729 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 730 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 731 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 732 | |
| 733 | err_unpin: |
| 734 | i915_gem_object_unpin_pages(obj); |
| 735 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 739 | unsigned int *needs_clflush) |
| 740 | { |
| 741 | int ret; |
| 742 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 743 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 744 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 745 | *needs_clflush = 0; |
| 746 | if (!i915_gem_object_has_struct_page(obj)) |
| 747 | return -ENODEV; |
| 748 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 749 | ret = i915_gem_object_wait(obj, |
| 750 | I915_WAIT_INTERRUPTIBLE | |
| 751 | I915_WAIT_LOCKED | |
| 752 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 753 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 754 | if (ret) |
| 755 | return ret; |
| 756 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 757 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 758 | if (ret) |
| 759 | return ret; |
| 760 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 761 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 762 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 763 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 764 | if (ret) |
| 765 | goto err_unpin; |
| 766 | else |
| 767 | goto out; |
| 768 | } |
| 769 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 770 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 771 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 772 | /* If we're not in the cpu write domain, set ourself into the |
| 773 | * gtt write domain and manually flush cachelines (as required). |
| 774 | * This optimizes for the case when the gpu will use the data |
| 775 | * right away and we therefore have to clflush anyway. |
| 776 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 777 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 778 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 779 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 780 | /* |
| 781 | * Same trick applies to invalidate partially written |
| 782 | * cachelines read before writing. |
| 783 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 784 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 785 | *needs_clflush |= CLFLUSH_BEFORE; |
| 786 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 787 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 788 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 789 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 790 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 791 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 792 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 793 | |
| 794 | err_unpin: |
| 795 | i915_gem_object_unpin_pages(obj); |
| 796 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 797 | } |
| 798 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 799 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 800 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 801 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 802 | { |
| 803 | char *vaddr; |
| 804 | int ret; |
| 805 | |
| 806 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 807 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 808 | if (needs_clflush) |
| 809 | drm_clflush_virt_range(vaddr + offset, len); |
| 810 | |
| 811 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 812 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 813 | kunmap(page); |
| 814 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 815 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static int |
| 819 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 820 | struct drm_i915_gem_pread *args) |
| 821 | { |
| 822 | char __user *user_data; |
| 823 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 824 | unsigned int needs_clflush; |
| 825 | unsigned int idx, offset; |
| 826 | int ret; |
| 827 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 828 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 829 | if (ret) |
| 830 | return ret; |
| 831 | |
| 832 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 833 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 834 | if (ret) |
| 835 | return ret; |
| 836 | |
| 837 | remain = args->size; |
| 838 | user_data = u64_to_user_ptr(args->data_ptr); |
| 839 | offset = offset_in_page(args->offset); |
| 840 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 841 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 842 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 843 | |
| 844 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 845 | needs_clflush); |
| 846 | if (ret) |
| 847 | break; |
| 848 | |
| 849 | remain -= length; |
| 850 | user_data += length; |
| 851 | offset = 0; |
| 852 | } |
| 853 | |
| 854 | i915_gem_obj_finish_shmem_access(obj); |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | static inline bool |
| 859 | gtt_user_read(struct io_mapping *mapping, |
| 860 | loff_t base, int offset, |
| 861 | char __user *user_data, int length) |
| 862 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 863 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 864 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 865 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 866 | /* 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] | 867 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 868 | unwritten = __copy_to_user_inatomic(user_data, |
| 869 | (void __force *)vaddr + offset, |
| 870 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 871 | io_mapping_unmap_atomic(vaddr); |
| 872 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 873 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 874 | unwritten = copy_to_user(user_data, |
| 875 | (void __force *)vaddr + offset, |
| 876 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 877 | io_mapping_unmap(vaddr); |
| 878 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 879 | return unwritten; |
| 880 | } |
| 881 | |
| 882 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 883 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 884 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 885 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 886 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 887 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 888 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 889 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 890 | struct i915_vma *vma; |
| 891 | void __user *user_data; |
| 892 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 893 | int ret; |
| 894 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 895 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 896 | if (ret) |
| 897 | return ret; |
| 898 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 899 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 900 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 901 | PIN_MAPPABLE | |
| 902 | PIN_NONFAULT | |
| 903 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 904 | if (!IS_ERR(vma)) { |
| 905 | node.start = i915_ggtt_offset(vma); |
| 906 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 907 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 908 | if (ret) { |
| 909 | i915_vma_unpin(vma); |
| 910 | vma = ERR_PTR(ret); |
| 911 | } |
| 912 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 913 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 914 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 915 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 916 | goto out_unlock; |
| 917 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 921 | if (ret) |
| 922 | goto out_unpin; |
| 923 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 924 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 925 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 926 | user_data = u64_to_user_ptr(args->data_ptr); |
| 927 | remain = args->size; |
| 928 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 929 | |
| 930 | while (remain > 0) { |
| 931 | /* Operation in this page |
| 932 | * |
| 933 | * page_base = page offset within aperture |
| 934 | * page_offset = offset within page |
| 935 | * page_length = bytes to copy for this page |
| 936 | */ |
| 937 | u32 page_base = node.start; |
| 938 | unsigned page_offset = offset_in_page(offset); |
| 939 | unsigned page_length = PAGE_SIZE - page_offset; |
| 940 | page_length = remain < page_length ? remain : page_length; |
| 941 | if (node.allocated) { |
| 942 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 943 | ggtt->vm.insert_page(&ggtt->vm, |
| 944 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 945 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 946 | wmb(); |
| 947 | } else { |
| 948 | page_base += offset & PAGE_MASK; |
| 949 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 950 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 951 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 952 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 953 | ret = -EFAULT; |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | remain -= page_length; |
| 958 | user_data += page_length; |
| 959 | offset += page_length; |
| 960 | } |
| 961 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 962 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 963 | out_unpin: |
| 964 | if (node.allocated) { |
| 965 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 966 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 967 | remove_mappable_node(&node); |
| 968 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 969 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 970 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 971 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 972 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 973 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 974 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 975 | return ret; |
| 976 | } |
| 977 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 978 | /** |
| 979 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 980 | * @dev: drm device pointer |
| 981 | * @data: ioctl data blob |
| 982 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 983 | * |
| 984 | * On error, the contents of *data are undefined. |
| 985 | */ |
| 986 | int |
| 987 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 988 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 989 | { |
| 990 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 991 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 992 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 993 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 994 | if (args->size == 0) |
| 995 | return 0; |
| 996 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 997 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 998 | args->size)) |
| 999 | return -EFAULT; |
| 1000 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1001 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1002 | if (!obj) |
| 1003 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1004 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1005 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1006 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1007 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1008 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1011 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1012 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1013 | ret = i915_gem_object_wait(obj, |
| 1014 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1015 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1016 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1017 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1018 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1019 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1020 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1021 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1022 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1023 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1024 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1025 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1026 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1027 | i915_gem_object_unpin_pages(obj); |
| 1028 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1029 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1030 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1033 | /* This is the fast write path which cannot handle |
| 1034 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1035 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1036 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1037 | static inline bool |
| 1038 | ggtt_write(struct io_mapping *mapping, |
| 1039 | loff_t base, int offset, |
| 1040 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1041 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1042 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1043 | unsigned long unwritten; |
| 1044 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1045 | /* 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] | 1046 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1047 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1048 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1049 | io_mapping_unmap_atomic(vaddr); |
| 1050 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1051 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1052 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1053 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1054 | io_mapping_unmap(vaddr); |
| 1055 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1056 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1057 | return unwritten; |
| 1058 | } |
| 1059 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1060 | /** |
| 1061 | * This is the fast pwrite path, where we copy the data directly from the |
| 1062 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1063 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1064 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1065 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1066 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1067 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1068 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1069 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1070 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1071 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1072 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1073 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1074 | struct i915_vma *vma; |
| 1075 | u64 remain, offset; |
| 1076 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1077 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1078 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1079 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1080 | if (ret) |
| 1081 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1082 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1083 | if (i915_gem_object_has_struct_page(obj)) { |
| 1084 | /* |
| 1085 | * Avoid waking the device up if we can fallback, as |
| 1086 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1087 | * depending on PCI sleeps and our own resume time). |
| 1088 | * This easily dwarfs any performance advantage from |
| 1089 | * using the cache bypass of indirect GGTT access. |
| 1090 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1091 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1092 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1093 | ret = -EFAULT; |
| 1094 | goto out_unlock; |
| 1095 | } |
| 1096 | } else { |
| 1097 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1098 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1101 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1102 | PIN_MAPPABLE | |
| 1103 | PIN_NONFAULT | |
| 1104 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1105 | if (!IS_ERR(vma)) { |
| 1106 | node.start = i915_ggtt_offset(vma); |
| 1107 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1108 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1109 | if (ret) { |
| 1110 | i915_vma_unpin(vma); |
| 1111 | vma = ERR_PTR(ret); |
| 1112 | } |
| 1113 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1114 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1115 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1116 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1117 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1118 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1119 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1120 | |
| 1121 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1122 | if (ret) |
| 1123 | goto out_unpin; |
| 1124 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1125 | mutex_unlock(&i915->drm.struct_mutex); |
| 1126 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1127 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1128 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1129 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1130 | offset = args->offset; |
| 1131 | remain = args->size; |
| 1132 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1133 | /* Operation in this page |
| 1134 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1135 | * page_base = page offset within aperture |
| 1136 | * page_offset = offset within page |
| 1137 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1138 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1139 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1140 | unsigned int page_offset = offset_in_page(offset); |
| 1141 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1142 | page_length = remain < page_length ? remain : page_length; |
| 1143 | if (node.allocated) { |
| 1144 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1145 | ggtt->vm.insert_page(&ggtt->vm, |
| 1146 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1147 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1148 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1149 | } else { |
| 1150 | page_base += offset & PAGE_MASK; |
| 1151 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1152 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1153 | * source page isn't available. Return the error and we'll |
| 1154 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1155 | * If the object is non-shmem backed, we retry again with the |
| 1156 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1157 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1158 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1159 | user_data, page_length)) { |
| 1160 | ret = -EFAULT; |
| 1161 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1162 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1163 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1164 | remain -= page_length; |
| 1165 | user_data += page_length; |
| 1166 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1167 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1168 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1169 | |
| 1170 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1171 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1172 | if (node.allocated) { |
| 1173 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1174 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1175 | remove_mappable_node(&node); |
| 1176 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1177 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1178 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1179 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1180 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1181 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1182 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1183 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1186 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1187 | * Flushes invalid cachelines before writing to the target if |
| 1188 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1189 | * writing if needs_clflush is set. |
| 1190 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1191 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1192 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1193 | bool needs_clflush_before, |
| 1194 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1195 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1196 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1197 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1198 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1199 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1200 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1201 | if (needs_clflush_before) |
| 1202 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1203 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1204 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1205 | if (!ret && needs_clflush_after) |
| 1206 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1207 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1208 | kunmap(page); |
| 1209 | |
| 1210 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | static int |
| 1214 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1215 | const struct drm_i915_gem_pwrite *args) |
| 1216 | { |
| 1217 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1218 | void __user *user_data; |
| 1219 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1220 | unsigned int partial_cacheline_write; |
| 1221 | unsigned int needs_clflush; |
| 1222 | unsigned int offset, idx; |
| 1223 | int ret; |
| 1224 | |
| 1225 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1226 | if (ret) |
| 1227 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1228 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1229 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1230 | mutex_unlock(&i915->drm.struct_mutex); |
| 1231 | if (ret) |
| 1232 | return ret; |
| 1233 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1234 | /* If we don't overwrite a cacheline completely we need to be |
| 1235 | * careful to have up-to-date data by first clflushing. Don't |
| 1236 | * overcomplicate things and flush the entire patch. |
| 1237 | */ |
| 1238 | partial_cacheline_write = 0; |
| 1239 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1240 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1241 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1242 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1243 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1244 | offset = offset_in_page(args->offset); |
| 1245 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1246 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1247 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1248 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1249 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1250 | (offset | length) & partial_cacheline_write, |
| 1251 | needs_clflush & CLFLUSH_AFTER); |
| 1252 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1253 | break; |
| 1254 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1255 | remain -= length; |
| 1256 | user_data += length; |
| 1257 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1260 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1261 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1262 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /** |
| 1266 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1267 | * @dev: drm device |
| 1268 | * @data: ioctl data blob |
| 1269 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1270 | * |
| 1271 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1272 | */ |
| 1273 | int |
| 1274 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1275 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1276 | { |
| 1277 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1278 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1279 | int ret; |
| 1280 | |
| 1281 | if (args->size == 0) |
| 1282 | return 0; |
| 1283 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1284 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1285 | return -EFAULT; |
| 1286 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1287 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1288 | if (!obj) |
| 1289 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1290 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1291 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1292 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1293 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1294 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1295 | } |
| 1296 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1297 | /* Writes not allowed into this read-only object */ |
| 1298 | if (i915_gem_object_is_readonly(obj)) { |
| 1299 | ret = -EINVAL; |
| 1300 | goto err; |
| 1301 | } |
| 1302 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1303 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1304 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1305 | ret = -ENODEV; |
| 1306 | if (obj->ops->pwrite) |
| 1307 | ret = obj->ops->pwrite(obj, args); |
| 1308 | if (ret != -ENODEV) |
| 1309 | goto err; |
| 1310 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1311 | ret = i915_gem_object_wait(obj, |
| 1312 | I915_WAIT_INTERRUPTIBLE | |
| 1313 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1314 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1315 | if (ret) |
| 1316 | goto err; |
| 1317 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1318 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1319 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1320 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1321 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1322 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1323 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1324 | * it would end up going through the fenced access, and we'll get |
| 1325 | * different detiling behavior between reading and writing. |
| 1326 | * pread/pwrite currently are reading and writing from the CPU |
| 1327 | * perspective, requiring manual detiling by the client. |
| 1328 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1329 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1330 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1331 | /* Note that the gtt paths might fail with non-page-backed user |
| 1332 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1333 | * textures). Fallback to the shmem path in that case. |
| 1334 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1335 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1336 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1337 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1338 | if (obj->phys_handle) |
| 1339 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1340 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1341 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1342 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1343 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1344 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1345 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1346 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1347 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1350 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1351 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1352 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1353 | struct list_head *list; |
| 1354 | struct i915_vma *vma; |
| 1355 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1356 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1357 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1358 | mutex_lock(&i915->ggtt.vm.mutex); |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1359 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1360 | if (!drm_mm_node_allocated(&vma->node)) |
| 1361 | continue; |
| 1362 | |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 1363 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1364 | } |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1365 | mutex_unlock(&i915->ggtt.vm.mutex); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1366 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1367 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1368 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1369 | list_move_tail(&obj->mm.link, list); |
| 1370 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1373 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1374 | * Called when user space prepares to use an object with the CPU, either |
| 1375 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1376 | * @dev: drm device |
| 1377 | * @data: ioctl data blob |
| 1378 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1379 | */ |
| 1380 | int |
| 1381 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1382 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1383 | { |
| 1384 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1385 | struct drm_i915_gem_object *obj; |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1386 | u32 read_domains = args->read_domains; |
| 1387 | u32 write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1388 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1389 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1390 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1391 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1392 | return -EINVAL; |
| 1393 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1394 | /* |
| 1395 | * Having something in the write domain implies it's in the read |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1396 | * domain, and only that read domain. Enforce that in the request. |
| 1397 | */ |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1398 | if (write_domain && read_domains != write_domain) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1399 | return -EINVAL; |
| 1400 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1401 | if (!read_domains) |
| 1402 | return 0; |
| 1403 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1404 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1405 | if (!obj) |
| 1406 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1407 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1408 | /* |
| 1409 | * Already in the desired write domain? Nothing for us to do! |
| 1410 | * |
| 1411 | * We apply a little bit of cunning here to catch a broader set of |
| 1412 | * no-ops. If obj->write_domain is set, we must be in the same |
| 1413 | * obj->read_domains, and only that domain. Therefore, if that |
| 1414 | * obj->write_domain matches the request read_domains, we are |
| 1415 | * already in the same read/write domain and can skip the operation, |
| 1416 | * without having to further check the requested write_domain. |
| 1417 | */ |
| 1418 | if (READ_ONCE(obj->write_domain) == read_domains) { |
| 1419 | err = 0; |
| 1420 | goto out; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * Try to flush the object off the GPU without holding the lock. |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1425 | * We will repeat the flush holding the lock in the normal manner |
| 1426 | * to catch cases where we are gazumped. |
| 1427 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1428 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1429 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1430 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1431 | (write_domain ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1432 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1433 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1434 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1435 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1436 | /* |
| 1437 | * Proxy objects do not control access to the backing storage, ergo |
| 1438 | * they cannot be used as a means to manipulate the cache domain |
| 1439 | * tracking for that backing storage. The proxy object is always |
| 1440 | * considered to be outside of any cache domain. |
| 1441 | */ |
| 1442 | if (i915_gem_object_is_proxy(obj)) { |
| 1443 | err = -ENXIO; |
| 1444 | goto out; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1449 | * direct access in memory with previous cached writes through |
| 1450 | * shmemfs and that our cache domain tracking remains valid. |
| 1451 | * For example, if the obj->filp was moved to swap without us |
| 1452 | * being notified and releasing the pages, we would mistakenly |
| 1453 | * continue to assume that the obj remained out of the CPU cached |
| 1454 | * domain. |
| 1455 | */ |
| 1456 | err = i915_gem_object_pin_pages(obj); |
| 1457 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1458 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1459 | |
| 1460 | err = i915_mutex_lock_interruptible(dev); |
| 1461 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1462 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1463 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1464 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1465 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1466 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1467 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1468 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1469 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1470 | |
| 1471 | /* And bump the LRU for this access */ |
| 1472 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1473 | |
| 1474 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1475 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1476 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1477 | intel_fb_obj_invalidate(obj, |
| 1478 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1479 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1480 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1481 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1482 | out: |
| 1483 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1484 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1489 | * @dev: drm device |
| 1490 | * @data: ioctl data blob |
| 1491 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1492 | */ |
| 1493 | int |
| 1494 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1495 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1496 | { |
| 1497 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1498 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1499 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1500 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1501 | if (!obj) |
| 1502 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1503 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1504 | /* |
| 1505 | * Proxy objects are barred from CPU access, so there is no |
| 1506 | * need to ban sw_finish as it is a nop. |
| 1507 | */ |
| 1508 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1509 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1510 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1511 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1512 | |
| 1513 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1516 | static inline bool |
| 1517 | __vma_matches(struct vm_area_struct *vma, struct file *filp, |
| 1518 | unsigned long addr, unsigned long size) |
| 1519 | { |
| 1520 | if (vma->vm_file != filp) |
| 1521 | return false; |
| 1522 | |
Tvrtko Ursulin | a90e194 | 2019-03-05 11:04:08 +0000 | [diff] [blame] | 1523 | return vma->vm_start == addr && |
| 1524 | (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1525 | } |
| 1526 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1527 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1528 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1529 | * it is mapped to. |
| 1530 | * @dev: drm device |
| 1531 | * @data: ioctl data blob |
| 1532 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1533 | * |
| 1534 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1535 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1536 | * |
| 1537 | * IMPORTANT: |
| 1538 | * |
| 1539 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1540 | * mmap support, please don't implement mmap support like here. The modern way |
| 1541 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1542 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1543 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1544 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1545 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1546 | */ |
| 1547 | int |
| 1548 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1549 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1550 | { |
| 1551 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1552 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1553 | unsigned long addr; |
| 1554 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1555 | if (args->flags & ~(I915_MMAP_WC)) |
| 1556 | return -EINVAL; |
| 1557 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1558 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1559 | return -ENODEV; |
| 1560 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1561 | obj = i915_gem_object_lookup(file, args->handle); |
| 1562 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1563 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1564 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1565 | /* prime objects have no backing filp to GEM mmap |
| 1566 | * pages from. |
| 1567 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1568 | if (!obj->base.filp) { |
Chris Wilson | 794a11c | 2019-03-14 07:58:29 +0000 | [diff] [blame] | 1569 | addr = -ENXIO; |
| 1570 | goto err; |
| 1571 | } |
| 1572 | |
| 1573 | if (range_overflows(args->offset, args->size, (u64)obj->base.size)) { |
| 1574 | addr = -EINVAL; |
| 1575 | goto err; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1576 | } |
| 1577 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1578 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1579 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1580 | args->offset); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1581 | if (IS_ERR_VALUE(addr)) |
| 1582 | goto err; |
| 1583 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1584 | if (args->flags & I915_MMAP_WC) { |
| 1585 | struct mm_struct *mm = current->mm; |
| 1586 | struct vm_area_struct *vma; |
| 1587 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1588 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | 794a11c | 2019-03-14 07:58:29 +0000 | [diff] [blame] | 1589 | addr = -EINTR; |
| 1590 | goto err; |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1591 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1592 | vma = find_vma(mm, addr); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1593 | if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1594 | vma->vm_page_prot = |
| 1595 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1596 | else |
| 1597 | addr = -ENOMEM; |
| 1598 | up_write(&mm->mmap_sem); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1599 | if (IS_ERR_VALUE(addr)) |
| 1600 | goto err; |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1601 | |
| 1602 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1603 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1604 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1605 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1606 | |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1607 | args->addr_ptr = (u64)addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1608 | return 0; |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1609 | |
| 1610 | err: |
| 1611 | i915_gem_object_put(obj); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1612 | return addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1615 | static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj) |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1616 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1617 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1618 | } |
| 1619 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1620 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1621 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1622 | * |
| 1623 | * A history of the GTT mmap interface: |
| 1624 | * |
| 1625 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1626 | * aligned and suitable for fencing, and still fit into the available |
| 1627 | * mappable space left by the pinned display objects. A classic problem |
| 1628 | * we called the page-fault-of-doom where we would ping-pong between |
| 1629 | * two objects that could not fit inside the GTT and so the memcpy |
| 1630 | * would page one object in at the expense of the other between every |
| 1631 | * single byte. |
| 1632 | * |
| 1633 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1634 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1635 | * object is too large for the available space (or simply too large |
| 1636 | * for the mappable aperture!), a view is created instead and faulted |
| 1637 | * into userspace. (This view is aligned and sized appropriately for |
| 1638 | * fenced access.) |
| 1639 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1640 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1641 | * delayed writes via GTT before performing direct access via WC. |
| 1642 | * |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 1643 | * 3 - Remove implicit set-domain(GTT) and synchronisation on initial |
| 1644 | * pagefault; swapin remains transparent. |
| 1645 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1646 | * Restrictions: |
| 1647 | * |
| 1648 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1649 | * hangs on some architectures, corruption on others. An attempt to service |
| 1650 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1651 | * |
| 1652 | * * the object must be able to fit into RAM (physical memory, though no |
| 1653 | * limited to the mappable aperture). |
| 1654 | * |
| 1655 | * |
| 1656 | * Caveats: |
| 1657 | * |
| 1658 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1659 | * all data to system memory. Subsequent access will not be synchronized. |
| 1660 | * |
| 1661 | * * all mappings are revoked on runtime device suspend. |
| 1662 | * |
| 1663 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1664 | * (older machines require fence register for display and blitter access |
| 1665 | * as well). Contention of the fence registers will cause the previous users |
| 1666 | * to be unmapped and any new access will generate new page faults. |
| 1667 | * |
| 1668 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1669 | * rather than the expected SIGSEGV. |
| 1670 | */ |
| 1671 | int i915_gem_mmap_gtt_version(void) |
| 1672 | { |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 1673 | return 3; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1674 | } |
| 1675 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1676 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1677 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1678 | pgoff_t page_offset, |
| 1679 | unsigned int chunk) |
| 1680 | { |
| 1681 | struct i915_ggtt_view view; |
| 1682 | |
| 1683 | if (i915_gem_object_is_tiled(obj)) |
| 1684 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1685 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1686 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1687 | view.partial.offset = rounddown(page_offset, chunk); |
| 1688 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1689 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1690 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1691 | |
| 1692 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1693 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1694 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1695 | |
| 1696 | return view; |
| 1697 | } |
| 1698 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1699 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1700 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1701 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1702 | * |
| 1703 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1704 | * from userspace. The fault handler takes care of binding the object to |
| 1705 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1706 | * only if needed based on whether the old reg is still valid or the object |
| 1707 | * is tiled) and inserting a new PTE into the faulting process. |
| 1708 | * |
| 1709 | * Note that the faulting process may involve evicting existing objects |
| 1710 | * from the GTT and/or fence registers to make room. So performance may |
| 1711 | * suffer if the GTT working set is large or there are few fence registers |
| 1712 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1713 | * |
| 1714 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1715 | * 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] | 1716 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1717 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1718 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1719 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1720 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1721 | 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] | 1722 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1723 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1724 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1725 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1726 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1727 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1728 | pgoff_t page_offset; |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 1729 | int srcu; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1730 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1731 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1732 | /* Sanity check that we allow writing into this object */ |
| 1733 | if (i915_gem_object_is_readonly(obj) && write) |
| 1734 | return VM_FAULT_SIGBUS; |
| 1735 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1736 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1737 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1738 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1739 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1740 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1741 | ret = i915_gem_object_pin_pages(obj); |
| 1742 | if (ret) |
| 1743 | goto err; |
| 1744 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1745 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1746 | |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1747 | srcu = i915_reset_trylock(dev_priv); |
| 1748 | if (srcu < 0) { |
| 1749 | ret = srcu; |
| 1750 | goto err_rpm; |
| 1751 | } |
| 1752 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1753 | ret = i915_mutex_lock_interruptible(dev); |
| 1754 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1755 | goto err_reset; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1756 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1757 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1758 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1759 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1760 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1763 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1764 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1765 | PIN_MAPPABLE | |
| 1766 | PIN_NONBLOCK | |
| 1767 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1768 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1769 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1770 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1771 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1772 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1773 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1774 | flags = PIN_MAPPABLE; |
| 1775 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1776 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1777 | |
| 1778 | /* |
| 1779 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1780 | * all hope that the hardware is able to track future writes. |
| 1781 | */ |
| 1782 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1783 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1784 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1785 | if (IS_ERR(vma) && !view.type) { |
| 1786 | flags = PIN_MAPPABLE; |
| 1787 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1788 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1789 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1790 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1791 | if (IS_ERR(vma)) { |
| 1792 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1793 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1794 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1795 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1796 | ret = i915_vma_pin_fence(vma); |
| 1797 | if (ret) |
| 1798 | goto err_unpin; |
| 1799 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1800 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1801 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1802 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1803 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1804 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1805 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1806 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1807 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1808 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1809 | /* Mark as being mmapped into userspace for later revocation */ |
| 1810 | assert_rpm_wakelock_held(dev_priv); |
| 1811 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1812 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1813 | GEM_BUG_ON(!obj->userfault_count); |
| 1814 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1815 | i915_vma_set_ggtt_write(vma); |
| 1816 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1817 | err_fence: |
| 1818 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1819 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1820 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1821 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1822 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1823 | err_reset: |
| 1824 | i915_reset_unlock(dev_priv, srcu); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1825 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1826 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1827 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1828 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1829 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1830 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1831 | /* |
| 1832 | * We eat errors when the gpu is terminally wedged to avoid |
| 1833 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1834 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1835 | * and so needs to be reported. |
| 1836 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1837 | if (!i915_terminally_wedged(dev_priv)) |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1838 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1839 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1840 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1841 | /* |
| 1842 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1843 | * handler to reset everything when re-faulting in |
| 1844 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1845 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1846 | case 0: |
| 1847 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1848 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1849 | case -EBUSY: |
| 1850 | /* |
| 1851 | * EBUSY is ok: this just means that another thread |
| 1852 | * already did the job. |
| 1853 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1854 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1855 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1856 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1857 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 1858 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1859 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1860 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1861 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1862 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1863 | } |
| 1864 | } |
| 1865 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1866 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 1867 | { |
| 1868 | struct i915_vma *vma; |
| 1869 | |
| 1870 | GEM_BUG_ON(!obj->userfault_count); |
| 1871 | |
| 1872 | obj->userfault_count = 0; |
| 1873 | list_del(&obj->userfault_link); |
| 1874 | drm_vma_node_unmap(&obj->base.vma_node, |
| 1875 | obj->base.dev->anon_inode->i_mapping); |
| 1876 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1877 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1878 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1881 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1882 | * i915_gem_release_mmap - remove physical page mappings |
| 1883 | * @obj: obj in question |
| 1884 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1885 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1886 | * relinquish ownership of the pages back to the system. |
| 1887 | * |
| 1888 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1889 | * object through the GTT and then lose the fence register due to |
| 1890 | * resource pressure. Similarly if the object has been moved out of the |
| 1891 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1892 | * mapping will then trigger a page fault on the next user access, allowing |
| 1893 | * fixup by i915_gem_fault(). |
| 1894 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1895 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1896 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1897 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1898 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1899 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1900 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1901 | /* Serialisation between user GTT access and our code depends upon |
| 1902 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 1903 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1904 | * |
| 1905 | * Note that RPM complicates somewhat by adding an additional |
| 1906 | * requirement that operations to the GGTT be made holding the RPM |
| 1907 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1908 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1909 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1910 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1911 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1912 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1913 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1914 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1915 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1916 | |
| 1917 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 1918 | * memory transactions from userspace before we return. The TLB |
| 1919 | * flushing implied above by changing the PTE above *should* be |
| 1920 | * sufficient, an extra barrier here just provides us with a bit |
| 1921 | * of paranoid documentation about our requirement to serialise |
| 1922 | * memory writes before touching registers / GSM. |
| 1923 | */ |
| 1924 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1925 | |
| 1926 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1927 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1928 | } |
| 1929 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1930 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 1931 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 1932 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1933 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 1934 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 1935 | /* |
| 1936 | * Only called during RPM suspend. All users of the userfault_list |
| 1937 | * must be holding an RPM wakeref to ensure that this can not |
| 1938 | * run concurrently with themselves (and use the struct_mutex for |
| 1939 | * protection between themselves). |
| 1940 | */ |
| 1941 | |
| 1942 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1943 | &dev_priv->mm.userfault_list, userfault_link) |
| 1944 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1945 | |
| 1946 | /* The fence will be lost when the device powers down. If any were |
| 1947 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 1948 | * down! All other fences will be reacquired by the user upon waking. |
| 1949 | */ |
| 1950 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 1951 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 1952 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 1953 | /* Ideally we want to assert that the fence register is not |
| 1954 | * live at this point (i.e. that no piece of code will be |
| 1955 | * trying to write through fence + GTT, as that both violates |
| 1956 | * our tracking of activity and associated locking/barriers, |
| 1957 | * but also is illegal given that the hw is powered down). |
| 1958 | * |
| 1959 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 1960 | * That is not sufficient, and we need a more fine-grained |
| 1961 | * tool if we want to have a sanity check here. |
| 1962 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1963 | |
| 1964 | if (!reg->vma) |
| 1965 | continue; |
| 1966 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1967 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1968 | reg->dirty = true; |
| 1969 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 1970 | } |
| 1971 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1972 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 1973 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 1974 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 1975 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1976 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 1977 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 1978 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 1979 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 1980 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 1981 | /* Attempt to reap some mmap space from dead objects */ |
| 1982 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 1983 | err = i915_gem_wait_for_idle(dev_priv, |
| 1984 | I915_WAIT_INTERRUPTIBLE, |
| 1985 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 1986 | if (err) |
| 1987 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1988 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 1989 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 1990 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 1991 | if (!err) |
| 1992 | break; |
| 1993 | |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 1994 | } while (flush_delayed_work(&dev_priv->gem.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 1995 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 1996 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2000 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2001 | drm_gem_free_mmap_offset(&obj->base); |
| 2002 | } |
| 2003 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2004 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2005 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2006 | struct drm_device *dev, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 2007 | u32 handle, |
| 2008 | u64 *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2009 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2010 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2011 | int ret; |
| 2012 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2013 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2014 | if (!obj) |
| 2015 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2016 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2017 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2018 | if (ret == 0) |
| 2019 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2020 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2021 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2022 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2023 | } |
| 2024 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2025 | /** |
| 2026 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2027 | * @dev: DRM device |
| 2028 | * @data: GTT mapping ioctl data |
| 2029 | * @file: GEM object info |
| 2030 | * |
| 2031 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2032 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2033 | * up so we can get faults in the handler above. |
| 2034 | * |
| 2035 | * The fault handler will take care of binding the object into the GTT |
| 2036 | * (since it may have been evicted to make room for something), allocating |
| 2037 | * a fence register, and mapping the appropriate aperture address into |
| 2038 | * userspace. |
| 2039 | */ |
| 2040 | int |
| 2041 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2042 | struct drm_file *file) |
| 2043 | { |
| 2044 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2045 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2046 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2047 | } |
| 2048 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2049 | /* Immediately discard the backing storage */ |
Chris Wilson | 2d6692e | 2019-04-20 12:55:39 +0100 | [diff] [blame] | 2050 | void __i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2051 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2052 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2053 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2054 | if (obj->base.filp == NULL) |
| 2055 | return; |
| 2056 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2057 | /* Our goal here is to return as much of the memory as |
| 2058 | * is possible back to the system as we are called from OOM. |
| 2059 | * To do this we must instruct the shmfs to drop all of its |
| 2060 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2061 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2062 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2063 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2064 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2065 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2066 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2067 | /* |
| 2068 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2069 | * ref count of those pages. |
| 2070 | */ |
| 2071 | static void check_release_pagevec(struct pagevec *pvec) |
| 2072 | { |
| 2073 | check_move_unevictable_pages(pvec); |
| 2074 | __pagevec_release(pvec); |
| 2075 | cond_resched(); |
| 2076 | } |
| 2077 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2078 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2079 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2080 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2081 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2082 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2083 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2084 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2085 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2086 | __i915_gem_object_release_shmem(obj, pages, true); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2087 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2088 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2089 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2090 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2091 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2092 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2093 | |
| 2094 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2095 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2096 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2097 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2098 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2099 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2100 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2101 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2102 | if (!pagevec_add(&pvec, page)) |
| 2103 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2104 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2105 | if (pagevec_count(&pvec)) |
| 2106 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2107 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2108 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2109 | sg_free_table(pages); |
| 2110 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2111 | } |
| 2112 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2113 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2114 | { |
| 2115 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2116 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2117 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2118 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2119 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2120 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2121 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2122 | } |
| 2123 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2124 | static struct sg_table * |
| 2125 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2126 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2127 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2128 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2129 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2130 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2131 | if (IS_ERR_OR_NULL(pages)) |
| 2132 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2133 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2134 | spin_lock(&i915->mm.obj_lock); |
| 2135 | list_del(&obj->mm.link); |
| 2136 | spin_unlock(&i915->mm.obj_lock); |
| 2137 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2138 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2139 | void *ptr; |
| 2140 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2141 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2142 | if (is_vmalloc_addr(ptr)) |
| 2143 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2144 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2145 | kunmap(kmap_to_page(ptr)); |
| 2146 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2147 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2150 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2151 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2152 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2153 | return pages; |
| 2154 | } |
| 2155 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2156 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2157 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2158 | { |
| 2159 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2160 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2161 | |
| 2162 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2163 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2164 | |
| 2165 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2166 | |
| 2167 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2168 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2169 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2170 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2171 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2172 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2173 | |
| 2174 | /* |
| 2175 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2176 | * array, hence protect them from being reaped by removing them from gtt |
| 2177 | * lists early. |
| 2178 | */ |
| 2179 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2180 | |
| 2181 | /* |
| 2182 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2183 | * NULL pages. In the future, when we have more asynchronous |
| 2184 | * get_pages backends we should be better able to handle the |
| 2185 | * cancellation of the async task in a more uniform manner. |
| 2186 | */ |
| 2187 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2188 | pages = ERR_PTR(-EINVAL); |
| 2189 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2190 | if (!IS_ERR(pages)) |
| 2191 | obj->ops->put_pages(obj, pages); |
| 2192 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2193 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2194 | unlock: |
| 2195 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2196 | |
| 2197 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2200 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2201 | { |
| 2202 | struct sg_table new_st; |
| 2203 | struct scatterlist *sg, *new_sg; |
| 2204 | unsigned int i; |
| 2205 | |
| 2206 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2207 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2208 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2209 | 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] | 2210 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2211 | |
| 2212 | new_sg = new_st.sgl; |
| 2213 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2214 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2215 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2216 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2217 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2218 | new_sg = sg_next(new_sg); |
| 2219 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2220 | 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] | 2221 | |
| 2222 | sg_free_table(orig_st); |
| 2223 | |
| 2224 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2225 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2228 | 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] | 2229 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2230 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2231 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2232 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2233 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2234 | struct sg_table *st; |
| 2235 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2236 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2237 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2238 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2239 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2240 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2241 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2242 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2243 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2244 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2245 | /* |
| 2246 | * Assert that the object is not currently in any GPU domain. As it |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2247 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2248 | * a GPU cache |
| 2249 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2250 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2251 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2252 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2253 | /* |
| 2254 | * If there's no chance of allocating enough pages for the whole |
| 2255 | * object, bail early. |
| 2256 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2257 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2258 | return -ENOMEM; |
| 2259 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2260 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2261 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2262 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2263 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2264 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2265 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2266 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2267 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2268 | } |
| 2269 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2270 | /* |
| 2271 | * Get the list of pages out of our struct file. They'll be pinned |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2272 | * at this point until we release them. |
| 2273 | * |
| 2274 | * Fail silently without starting the shrinker |
| 2275 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2276 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2277 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2278 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2279 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2280 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2281 | sg = st->sgl; |
| 2282 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2283 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2284 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2285 | const unsigned int shrink[] = { |
| 2286 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2287 | 0, |
| 2288 | }, *s = shrink; |
| 2289 | gfp_t gfp = noreclaim; |
| 2290 | |
| 2291 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2292 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2293 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 2294 | if (!IS_ERR(page)) |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2295 | break; |
| 2296 | |
| 2297 | if (!*s) { |
| 2298 | ret = PTR_ERR(page); |
| 2299 | goto err_sg; |
| 2300 | } |
| 2301 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2302 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2303 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2304 | /* |
| 2305 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2306 | * our own buffer, now let the real VM do its job and |
| 2307 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2308 | * |
| 2309 | * However, since graphics tend to be disposable, |
| 2310 | * defer the oom here by reporting the ENOMEM back |
| 2311 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2312 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2313 | if (!*s) { |
| 2314 | /* reclaim and warn, but no oom */ |
| 2315 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2316 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2317 | /* |
| 2318 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2319 | * kswapd to reclaim our pages (direct reclaim |
| 2320 | * does not effectively begin pageout of our |
| 2321 | * buffers on its own). However, direct reclaim |
| 2322 | * only waits for kswapd when under allocation |
| 2323 | * congestion. So as a result __GFP_RECLAIM is |
| 2324 | * unreliable and fails to actually reclaim our |
| 2325 | * dirty pages -- unless you try over and over |
| 2326 | * again with !__GFP_NORETRY. However, we still |
| 2327 | * want to fail this allocation rather than |
| 2328 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2329 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2330 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2331 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2332 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2333 | } while (1); |
| 2334 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2335 | if (!i || |
| 2336 | sg->length >= max_segment || |
| 2337 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2338 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2339 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2340 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2341 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2342 | st->nents++; |
| 2343 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2344 | } else { |
| 2345 | sg->length += PAGE_SIZE; |
| 2346 | } |
| 2347 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2348 | |
| 2349 | /* Check that the i965g/gm workaround works. */ |
| 2350 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2351 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2352 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2353 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2354 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2355 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2356 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2357 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2358 | i915_sg_trim(st); |
| 2359 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2360 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2361 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2362 | /* |
| 2363 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2364 | * it could not reserve enough large entries, asking |
| 2365 | * for PAGE_SIZE chunks instead may be helpful. |
| 2366 | */ |
| 2367 | if (max_segment > PAGE_SIZE) { |
| 2368 | for_each_sgt_page(page, sgt_iter, st) |
| 2369 | put_page(page); |
| 2370 | sg_free_table(st); |
| 2371 | |
| 2372 | max_segment = PAGE_SIZE; |
| 2373 | goto rebuild_st; |
| 2374 | } else { |
| 2375 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2376 | "Failed to DMA remap %lu pages\n", |
| 2377 | page_count); |
| 2378 | goto err_pages; |
| 2379 | } |
| 2380 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2381 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2382 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2383 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2384 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2385 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2386 | |
| 2387 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2388 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2389 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2390 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2391 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2392 | mapping_clear_unevictable(mapping); |
| 2393 | pagevec_init(&pvec); |
| 2394 | for_each_sgt_page(page, sgt_iter, st) { |
| 2395 | if (!pagevec_add(&pvec, page)) |
| 2396 | check_release_pagevec(&pvec); |
| 2397 | } |
| 2398 | if (pagevec_count(&pvec)) |
| 2399 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2400 | sg_free_table(st); |
| 2401 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2402 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2403 | /* |
| 2404 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2405 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2406 | * ENOMEM for a genuine allocation failure. |
| 2407 | * |
| 2408 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2409 | * space and so want to translate the error from shmemfs back to our |
| 2410 | * usual understanding of ENOMEM. |
| 2411 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2412 | if (ret == -ENOSPC) |
| 2413 | ret = -ENOMEM; |
| 2414 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2415 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2419 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2420 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2421 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2422 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2423 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2424 | int i; |
| 2425 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2426 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2427 | |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 2428 | /* Make the pages coherent with the GPU (flushing any swapin). */ |
| 2429 | if (obj->cache_dirty) { |
| 2430 | obj->write_domain = 0; |
| 2431 | if (i915_gem_object_has_struct_page(obj)) |
| 2432 | drm_clflush_sg(pages); |
| 2433 | obj->cache_dirty = false; |
| 2434 | } |
| 2435 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2436 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2437 | obj->mm.get_page.sg_idx = 0; |
| 2438 | |
| 2439 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2440 | |
| 2441 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2442 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2443 | GEM_BUG_ON(obj->mm.quirked); |
| 2444 | __i915_gem_object_pin_pages(obj); |
| 2445 | obj->mm.quirked = true; |
| 2446 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2447 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2448 | GEM_BUG_ON(!sg_page_sizes); |
| 2449 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2450 | |
| 2451 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2452 | * Calculate the supported page-sizes which fit into the given |
| 2453 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2454 | * to use opportunistically when later inserting into the GTT. For |
| 2455 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2456 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2457 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2458 | */ |
| 2459 | obj->mm.page_sizes.sg = 0; |
| 2460 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2461 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2462 | obj->mm.page_sizes.sg |= BIT(i); |
| 2463 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2464 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2465 | |
| 2466 | spin_lock(&i915->mm.obj_lock); |
| 2467 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2468 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2472 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2473 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2474 | |
| 2475 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2476 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2477 | return -EFAULT; |
| 2478 | } |
| 2479 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2480 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2481 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2482 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2483 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2486 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2487 | * 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] | 2488 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2489 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2490 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2491 | * or as the object is itself released. |
| 2492 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2493 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2494 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2495 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2496 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2497 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2498 | if (err) |
| 2499 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2500 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2501 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2502 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2503 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2504 | err = ____i915_gem_object_get_pages(obj); |
| 2505 | if (err) |
| 2506 | goto unlock; |
| 2507 | |
| 2508 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2509 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2510 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2511 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2512 | unlock: |
| 2513 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2514 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2517 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2518 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2519 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2520 | { |
| 2521 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2522 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2523 | struct sgt_iter sgt_iter; |
| 2524 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2525 | struct page *stack_pages[32]; |
| 2526 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2527 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2528 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2529 | void *addr; |
| 2530 | |
| 2531 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2532 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2533 | return kmap(sg_page(sgt->sgl)); |
| 2534 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2535 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2536 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2537 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2538 | if (!pages) |
| 2539 | return NULL; |
| 2540 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2541 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2542 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2543 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2544 | |
| 2545 | /* Check that we have the expected number of pages */ |
| 2546 | GEM_BUG_ON(i != n_pages); |
| 2547 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2548 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2549 | default: |
| 2550 | MISSING_CASE(type); |
| 2551 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2552 | case I915_MAP_WB: |
| 2553 | pgprot = PAGE_KERNEL; |
| 2554 | break; |
| 2555 | case I915_MAP_WC: |
| 2556 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2557 | break; |
| 2558 | } |
| 2559 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2560 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2561 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2562 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2563 | |
| 2564 | return addr; |
| 2565 | } |
| 2566 | |
| 2567 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2568 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2569 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2570 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2571 | enum i915_map_type has_type; |
| 2572 | bool pinned; |
| 2573 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2574 | int ret; |
| 2575 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2576 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2577 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2578 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2579 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2580 | if (ret) |
| 2581 | return ERR_PTR(ret); |
| 2582 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2583 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2584 | type &= ~I915_MAP_OVERRIDE; |
| 2585 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2586 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2587 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2588 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2589 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2590 | ret = ____i915_gem_object_get_pages(obj); |
| 2591 | if (ret) |
| 2592 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2593 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2594 | smp_mb__before_atomic(); |
| 2595 | } |
| 2596 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2597 | pinned = false; |
| 2598 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2599 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2600 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2601 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2602 | if (ptr && has_type != type) { |
| 2603 | if (pinned) { |
| 2604 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2605 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2606 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2607 | |
| 2608 | if (is_vmalloc_addr(ptr)) |
| 2609 | vunmap(ptr); |
| 2610 | else |
| 2611 | kunmap(kmap_to_page(ptr)); |
| 2612 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2613 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2614 | } |
| 2615 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2616 | if (!ptr) { |
| 2617 | ptr = i915_gem_object_map(obj, type); |
| 2618 | if (!ptr) { |
| 2619 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2620 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2621 | } |
| 2622 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2623 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2624 | } |
| 2625 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2626 | out_unlock: |
| 2627 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2628 | return ptr; |
| 2629 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2630 | err_unpin: |
| 2631 | atomic_dec(&obj->mm.pages_pin_count); |
| 2632 | err_unlock: |
| 2633 | ptr = ERR_PTR(ret); |
| 2634 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2635 | } |
| 2636 | |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 2637 | void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj, |
| 2638 | unsigned long offset, |
| 2639 | unsigned long size) |
| 2640 | { |
| 2641 | enum i915_map_type has_type; |
| 2642 | void *ptr; |
| 2643 | |
| 2644 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 2645 | GEM_BUG_ON(range_overflows_t(typeof(obj->base.size), |
| 2646 | offset, size, obj->base.size)); |
| 2647 | |
| 2648 | obj->mm.dirty = true; |
| 2649 | |
| 2650 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) |
| 2651 | return; |
| 2652 | |
| 2653 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
| 2654 | if (has_type == I915_MAP_WC) |
| 2655 | return; |
| 2656 | |
| 2657 | drm_clflush_virt_range(ptr + offset, size); |
| 2658 | if (size == obj->base.size) { |
| 2659 | obj->write_domain &= ~I915_GEM_DOMAIN_CPU; |
| 2660 | obj->cache_dirty = false; |
| 2661 | } |
| 2662 | } |
| 2663 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2664 | static int |
| 2665 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2666 | const struct drm_i915_gem_pwrite *arg) |
| 2667 | { |
| 2668 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2669 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2670 | u64 remain, offset; |
| 2671 | unsigned int pg; |
| 2672 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2673 | /* Caller already validated user args */ |
| 2674 | GEM_BUG_ON(!access_ok(user_data, arg->size)); |
| 2675 | |
| 2676 | /* |
| 2677 | * Before we instantiate/pin the backing store for our use, we |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2678 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2679 | * the pagecache. We avoid the penalty of instantiating all the |
| 2680 | * pages, important if the user is just writing to a few and never |
| 2681 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2682 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2683 | * or clearing-before-use) before it is overwritten. |
| 2684 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2685 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2686 | return -ENODEV; |
| 2687 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2688 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2689 | return -EFAULT; |
| 2690 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2691 | /* |
| 2692 | * Before the pages are instantiated the object is treated as being |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2693 | * in the CPU domain. The pages will be clflushed as required before |
| 2694 | * use, and we can freely write into the pages directly. If userspace |
| 2695 | * races pwrite with any other operation; corruption will ensue - |
| 2696 | * that is userspace's prerogative! |
| 2697 | */ |
| 2698 | |
| 2699 | remain = arg->size; |
| 2700 | offset = arg->offset; |
| 2701 | pg = offset_in_page(offset); |
| 2702 | |
| 2703 | do { |
| 2704 | unsigned int len, unwritten; |
| 2705 | struct page *page; |
| 2706 | void *data, *vaddr; |
| 2707 | int err; |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2708 | char c; |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2709 | |
| 2710 | len = PAGE_SIZE - pg; |
| 2711 | if (len > remain) |
| 2712 | len = remain; |
| 2713 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2714 | /* Prefault the user page to reduce potential recursion */ |
| 2715 | err = __get_user(c, user_data); |
| 2716 | if (err) |
| 2717 | return err; |
| 2718 | |
| 2719 | err = __get_user(c, user_data + len - 1); |
| 2720 | if (err) |
| 2721 | return err; |
| 2722 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2723 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2724 | offset, len, 0, |
| 2725 | &page, &data); |
| 2726 | if (err < 0) |
| 2727 | return err; |
| 2728 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2729 | vaddr = kmap_atomic(page); |
| 2730 | unwritten = __copy_from_user_inatomic(vaddr + pg, |
| 2731 | user_data, |
| 2732 | len); |
| 2733 | kunmap_atomic(vaddr); |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2734 | |
| 2735 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2736 | offset, len, len - unwritten, |
| 2737 | page, data); |
| 2738 | if (err < 0) |
| 2739 | return err; |
| 2740 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2741 | /* We don't handle -EFAULT, leave it to the caller to check */ |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2742 | if (unwritten) |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2743 | return -ENODEV; |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2744 | |
| 2745 | remain -= len; |
| 2746 | user_data += len; |
| 2747 | offset += len; |
| 2748 | pg = 0; |
| 2749 | } while (remain); |
| 2750 | |
| 2751 | return 0; |
| 2752 | } |
| 2753 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2754 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 2755 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2756 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2757 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 2758 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2759 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2760 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2761 | mutex_lock(&i915->drm.struct_mutex); |
| 2762 | |
| 2763 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 2764 | struct i915_gem_context *ctx = lut->ctx; |
| 2765 | struct i915_vma *vma; |
| 2766 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 2767 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2768 | if (ctx->file_priv != fpriv) |
| 2769 | continue; |
| 2770 | |
| 2771 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 2772 | GEM_BUG_ON(vma->obj != obj); |
| 2773 | |
| 2774 | /* We allow the process to have multiple handles to the same |
| 2775 | * vma, in the same fd namespace, by virtue of flink/open. |
| 2776 | */ |
| 2777 | GEM_BUG_ON(!vma->open_count); |
| 2778 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2779 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 2780 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2781 | list_del(&lut->obj_link); |
| 2782 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 2783 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 2784 | i915_lut_handle_free(lut); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2785 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 2786 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2787 | |
| 2788 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2789 | } |
| 2790 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2791 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 2792 | { |
| 2793 | if (timeout_ns < 0) |
| 2794 | return MAX_SCHEDULE_TIMEOUT; |
| 2795 | |
| 2796 | if (timeout_ns == 0) |
| 2797 | return 0; |
| 2798 | |
| 2799 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 2800 | } |
| 2801 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 2802 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2803 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 2804 | * @dev: drm device pointer |
| 2805 | * @data: ioctl data blob |
| 2806 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2807 | * |
| 2808 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 2809 | * the timeout parameter. |
| 2810 | * -ETIME: object is still busy after timeout |
| 2811 | * -ERESTARTSYS: signal interrupted the wait |
| 2812 | * -ENONENT: object doesn't exist |
| 2813 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 2814 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2815 | * -ENOMEM: damn |
| 2816 | * -ENODEV: Internal IRQ fail |
| 2817 | * -E?: The add request failed |
| 2818 | * |
| 2819 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 2820 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 2821 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 2822 | * without holding struct_mutex the object may become re-busied before this |
| 2823 | * function completes. A similar but shorter * race condition exists in the busy |
| 2824 | * ioctl |
| 2825 | */ |
| 2826 | int |
| 2827 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 2828 | { |
| 2829 | struct drm_i915_gem_wait *args = data; |
| 2830 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2831 | ktime_t start; |
| 2832 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2833 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 2834 | if (args->flags != 0) |
| 2835 | return -EINVAL; |
| 2836 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2837 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 2838 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2839 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 2840 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2841 | start = ktime_get(); |
| 2842 | |
| 2843 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 2844 | I915_WAIT_INTERRUPTIBLE | |
| 2845 | I915_WAIT_PRIORITY | |
| 2846 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 2847 | to_wait_timeout(args->timeout_ns)); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2848 | |
| 2849 | if (args->timeout_ns > 0) { |
| 2850 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2851 | if (args->timeout_ns < 0) |
| 2852 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 2853 | |
| 2854 | /* |
| 2855 | * Apparently ktime isn't accurate enough and occasionally has a |
| 2856 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 2857 | * things up to make the test happy. We allow up to 1 jiffy. |
| 2858 | * |
| 2859 | * This is a regression from the timespec->ktime conversion. |
| 2860 | */ |
| 2861 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 2862 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 2863 | |
| 2864 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 2865 | if (ret == -ETIME && args->timeout_ns) |
| 2866 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2867 | } |
| 2868 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2869 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 2870 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 2873 | static int wait_for_engines(struct drm_i915_private *i915) |
| 2874 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 2875 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 2876 | dev_err(i915->drm.dev, |
| 2877 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 2878 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 2879 | i915_gem_set_wedged(i915); |
| 2880 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | return 0; |
| 2884 | } |
| 2885 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2886 | static long |
| 2887 | wait_for_timelines(struct drm_i915_private *i915, |
| 2888 | unsigned int flags, long timeout) |
| 2889 | { |
| 2890 | struct i915_gt_timelines *gt = &i915->gt.timelines; |
| 2891 | struct i915_timeline *tl; |
| 2892 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2893 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 2894 | list_for_each_entry(tl, >->active_list, link) { |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2895 | struct i915_request *rq; |
| 2896 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 2897 | rq = i915_active_request_get_unlocked(&tl->last_request); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2898 | if (!rq) |
| 2899 | continue; |
| 2900 | |
| 2901 | mutex_unlock(>->mutex); |
| 2902 | |
| 2903 | /* |
| 2904 | * "Race-to-idle". |
| 2905 | * |
| 2906 | * Switching to the kernel context is often used a synchronous |
| 2907 | * step prior to idling, e.g. in suspend for flushing all |
| 2908 | * current operations to memory before sleeping. These we |
| 2909 | * want to complete as quickly as possible to avoid prolonged |
| 2910 | * stalls, so allow the gpu to boost to maximum clocks. |
| 2911 | */ |
| 2912 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 2913 | gen6_rps_boost(rq); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2914 | |
| 2915 | timeout = i915_request_wait(rq, flags, timeout); |
| 2916 | i915_request_put(rq); |
| 2917 | if (timeout < 0) |
| 2918 | return timeout; |
| 2919 | |
| 2920 | /* restart after reacquiring the lock */ |
| 2921 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 2922 | tl = list_entry(>->active_list, typeof(*tl), link); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2923 | } |
| 2924 | mutex_unlock(>->mutex); |
| 2925 | |
| 2926 | return timeout; |
| 2927 | } |
| 2928 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2929 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 2930 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 2931 | { |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 2932 | GEM_TRACE("flags=%x (%s), timeout=%ld%s, awake?=%s\n", |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2933 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 2934 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "", |
| 2935 | yesno(i915->gt.awake)); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 2936 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 2937 | /* If the device is asleep, we have no requests outstanding */ |
| 2938 | if (!READ_ONCE(i915->gt.awake)) |
| 2939 | return 0; |
| 2940 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2941 | timeout = wait_for_timelines(i915, flags, timeout); |
| 2942 | if (timeout < 0) |
| 2943 | return timeout; |
| 2944 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 2945 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2946 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 2947 | |
| 2948 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 2949 | |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 2950 | err = wait_for_engines(i915); |
| 2951 | if (err) |
| 2952 | return err; |
| 2953 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2954 | i915_retire_requests(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2955 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 2956 | |
| 2957 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2958 | } |
| 2959 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 2960 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 2961 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 2962 | /* |
| 2963 | * We manually flush the CPU domain so that we can override and |
| 2964 | * force the flush for the display, and perform it asyncrhonously. |
| 2965 | */ |
| 2966 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 2967 | if (obj->cache_dirty) |
| 2968 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2969 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 2973 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 2974 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 2975 | return; |
| 2976 | |
| 2977 | mutex_lock(&obj->base.dev->struct_mutex); |
| 2978 | __i915_gem_object_flush_for_display(obj); |
| 2979 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 2980 | } |
| 2981 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2982 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 2983 | * Moves a single object to the WC read, and possibly write domain. |
| 2984 | * @obj: object to act on |
| 2985 | * @write: ask for write access or read only |
| 2986 | * |
| 2987 | * This function returns when the move is complete, including waiting on |
| 2988 | * flushes to occur. |
| 2989 | */ |
| 2990 | int |
| 2991 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 2992 | { |
| 2993 | int ret; |
| 2994 | |
| 2995 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 2996 | |
| 2997 | ret = i915_gem_object_wait(obj, |
| 2998 | I915_WAIT_INTERRUPTIBLE | |
| 2999 | I915_WAIT_LOCKED | |
| 3000 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3001 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3002 | if (ret) |
| 3003 | return ret; |
| 3004 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3005 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3006 | return 0; |
| 3007 | |
| 3008 | /* Flush and acquire obj->pages so that we are coherent through |
| 3009 | * direct access in memory with previous cached writes through |
| 3010 | * shmemfs and that our cache domain tracking remains valid. |
| 3011 | * For example, if the obj->filp was moved to swap without us |
| 3012 | * being notified and releasing the pages, we would mistakenly |
| 3013 | * continue to assume that the obj remained out of the CPU cached |
| 3014 | * domain. |
| 3015 | */ |
| 3016 | ret = i915_gem_object_pin_pages(obj); |
| 3017 | if (ret) |
| 3018 | return ret; |
| 3019 | |
| 3020 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3021 | |
| 3022 | /* Serialise direct access to this object with the barriers for |
| 3023 | * coherent writes from the GPU, by effectively invalidating the |
| 3024 | * WC domain upon first access. |
| 3025 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3026 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3027 | mb(); |
| 3028 | |
| 3029 | /* It should now be out of any other write domains, and we can update |
| 3030 | * the domain values for our changes. |
| 3031 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3032 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3033 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3034 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3035 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3036 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3037 | obj->mm.dirty = true; |
| 3038 | } |
| 3039 | |
| 3040 | i915_gem_object_unpin_pages(obj); |
| 3041 | return 0; |
| 3042 | } |
| 3043 | |
| 3044 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3045 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3046 | * @obj: object to act on |
| 3047 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3048 | * |
| 3049 | * This function returns when the move is complete, including waiting on |
| 3050 | * flushes to occur. |
| 3051 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3052 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3053 | 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] | 3054 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3055 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3056 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3057 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3058 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3059 | ret = i915_gem_object_wait(obj, |
| 3060 | I915_WAIT_INTERRUPTIBLE | |
| 3061 | I915_WAIT_LOCKED | |
| 3062 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3063 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3064 | if (ret) |
| 3065 | return ret; |
| 3066 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3067 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3068 | return 0; |
| 3069 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3070 | /* Flush and acquire obj->pages so that we are coherent through |
| 3071 | * direct access in memory with previous cached writes through |
| 3072 | * shmemfs and that our cache domain tracking remains valid. |
| 3073 | * For example, if the obj->filp was moved to swap without us |
| 3074 | * being notified and releasing the pages, we would mistakenly |
| 3075 | * continue to assume that the obj remained out of the CPU cached |
| 3076 | * domain. |
| 3077 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3078 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3079 | if (ret) |
| 3080 | return ret; |
| 3081 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3082 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3083 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3084 | /* Serialise direct access to this object with the barriers for |
| 3085 | * coherent writes from the GPU, by effectively invalidating the |
| 3086 | * GTT domain upon first access. |
| 3087 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3088 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3089 | mb(); |
| 3090 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3091 | /* It should now be out of any other write domains, and we can update |
| 3092 | * the domain values for our changes. |
| 3093 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3094 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3095 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3096 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3097 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3098 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3099 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3102 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3103 | return 0; |
| 3104 | } |
| 3105 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3106 | /** |
| 3107 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3108 | * @obj: object to act on |
| 3109 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3110 | * |
| 3111 | * After this function returns, the object will be in the new cache-level |
| 3112 | * across all GTT and the contents of the backing storage will be coherent, |
| 3113 | * with respect to the new cache-level. In order to keep the backing storage |
| 3114 | * coherent for all users, we only allow a single cache level to be set |
| 3115 | * globally on the object and prevent it from being changed whilst the |
| 3116 | * hardware is reading from the object. That is if the object is currently |
| 3117 | * on the scanout it will be set to uncached (or equivalent display |
| 3118 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3119 | * that all direct access to the scanout remains coherent. |
| 3120 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3121 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3122 | enum i915_cache_level cache_level) |
| 3123 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3124 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3125 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3126 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3127 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3128 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3129 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3130 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3131 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3132 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3133 | * be invalid given the new cache-level. This is principally to |
| 3134 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3135 | * reading an invalid PTE on older architectures. |
| 3136 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3137 | restart: |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3138 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3139 | if (!drm_mm_node_allocated(&vma->node)) |
| 3140 | continue; |
| 3141 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3142 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3143 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3144 | return -EBUSY; |
| 3145 | } |
| 3146 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3147 | if (!i915_vma_is_closed(vma) && |
| 3148 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3149 | continue; |
| 3150 | |
| 3151 | ret = i915_vma_unbind(vma); |
| 3152 | if (ret) |
| 3153 | return ret; |
| 3154 | |
| 3155 | /* As unbinding may affect other elements in the |
| 3156 | * obj->vma_list (due to side-effects from retiring |
| 3157 | * an active vma), play safe and restart the iterator. |
| 3158 | */ |
| 3159 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3160 | } |
| 3161 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3162 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3163 | * cache-level on the PTE. We could simply unbind them all and |
| 3164 | * rebind with the correct cache-level on next use. However since |
| 3165 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3166 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3167 | * state and so involves less work. |
| 3168 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3169 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3170 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3171 | * If we wait upon the object, we know that all the bound |
| 3172 | * VMA are no longer active. |
| 3173 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3174 | ret = i915_gem_object_wait(obj, |
| 3175 | I915_WAIT_INTERRUPTIBLE | |
| 3176 | I915_WAIT_LOCKED | |
| 3177 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3178 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3179 | if (ret) |
| 3180 | return ret; |
| 3181 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3182 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3183 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3184 | /* Access to snoopable pages through the GTT is |
| 3185 | * incoherent and on some machines causes a hard |
| 3186 | * lockup. Relinquish the CPU mmaping to force |
| 3187 | * userspace to refault in the pages and we can |
| 3188 | * then double check if the GTT mapping is still |
| 3189 | * valid for that pointer access. |
| 3190 | */ |
| 3191 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3192 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3193 | /* As we no longer need a fence for GTT access, |
| 3194 | * we can relinquish it now (and so prevent having |
| 3195 | * to steal a fence from someone else on the next |
| 3196 | * fence request). Note GPU activity would have |
| 3197 | * dropped the fence as all snoopable access is |
| 3198 | * supposed to be linear. |
| 3199 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3200 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3201 | ret = i915_vma_put_fence(vma); |
| 3202 | if (ret) |
| 3203 | return ret; |
| 3204 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3205 | } else { |
| 3206 | /* We either have incoherent backing store and |
| 3207 | * so no GTT access or the architecture is fully |
| 3208 | * coherent. In such cases, existing GTT mmaps |
| 3209 | * ignore the cache bit in the PTE and we can |
| 3210 | * rewrite it without confusing the GPU or having |
| 3211 | * to force userspace to fault back in its mmaps. |
| 3212 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3213 | } |
| 3214 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3215 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3216 | if (!drm_mm_node_allocated(&vma->node)) |
| 3217 | continue; |
| 3218 | |
| 3219 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3220 | if (ret) |
| 3221 | return ret; |
| 3222 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3223 | } |
| 3224 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3225 | list_for_each_entry(vma, &obj->vma.list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3226 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3227 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3228 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3229 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3230 | return 0; |
| 3231 | } |
| 3232 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3233 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3234 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3235 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3236 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3237 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3238 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3239 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3240 | rcu_read_lock(); |
| 3241 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3242 | if (!obj) { |
| 3243 | err = -ENOENT; |
| 3244 | goto out; |
| 3245 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3246 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3247 | switch (obj->cache_level) { |
| 3248 | case I915_CACHE_LLC: |
| 3249 | case I915_CACHE_L3_LLC: |
| 3250 | args->caching = I915_CACHING_CACHED; |
| 3251 | break; |
| 3252 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3253 | case I915_CACHE_WT: |
| 3254 | args->caching = I915_CACHING_DISPLAY; |
| 3255 | break; |
| 3256 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3257 | default: |
| 3258 | args->caching = I915_CACHING_NONE; |
| 3259 | break; |
| 3260 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3261 | out: |
| 3262 | rcu_read_unlock(); |
| 3263 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3264 | } |
| 3265 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3266 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3267 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3268 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3269 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3270 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3271 | struct drm_i915_gem_object *obj; |
| 3272 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3273 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3274 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3275 | switch (args->caching) { |
| 3276 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3277 | level = I915_CACHE_NONE; |
| 3278 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3279 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3280 | /* |
| 3281 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3282 | * snooped mapping may leave stale data in a corresponding CPU |
| 3283 | * cacheline, whereas normally such cachelines would get |
| 3284 | * invalidated. |
| 3285 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3286 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3287 | return -ENODEV; |
| 3288 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3289 | level = I915_CACHE_LLC; |
| 3290 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3291 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3292 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3293 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3294 | default: |
| 3295 | return -EINVAL; |
| 3296 | } |
| 3297 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3298 | obj = i915_gem_object_lookup(file, args->handle); |
| 3299 | if (!obj) |
| 3300 | return -ENOENT; |
| 3301 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3302 | /* |
| 3303 | * The caching mode of proxy object is handled by its generator, and |
| 3304 | * not allowed to be changed by userspace. |
| 3305 | */ |
| 3306 | if (i915_gem_object_is_proxy(obj)) { |
| 3307 | ret = -ENXIO; |
| 3308 | goto out; |
| 3309 | } |
| 3310 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3311 | if (obj->cache_level == level) |
| 3312 | goto out; |
| 3313 | |
| 3314 | ret = i915_gem_object_wait(obj, |
| 3315 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3316 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3317 | if (ret) |
| 3318 | goto out; |
| 3319 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3320 | ret = i915_mutex_lock_interruptible(dev); |
| 3321 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3322 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3323 | |
| 3324 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3325 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3326 | |
| 3327 | out: |
| 3328 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3329 | return ret; |
| 3330 | } |
| 3331 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3332 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 3333 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 3334 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 3335 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 3336 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3337 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3338 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3339 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3340 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3341 | const struct i915_ggtt_view *view, |
| 3342 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3343 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3344 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3345 | int ret; |
| 3346 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3347 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3348 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3349 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3350 | * display coherency whilst setting up the cache domains. |
| 3351 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3352 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3353 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3354 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3355 | * a result, we make sure that the pinning that is about to occur is |
| 3356 | * done with uncached PTEs. This is lowest common denominator for all |
| 3357 | * chipsets. |
| 3358 | * |
| 3359 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3360 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3361 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3362 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3363 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3364 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3365 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3366 | if (ret) { |
| 3367 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3368 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3369 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3370 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3371 | /* As the user may map the buffer once pinned in the display plane |
| 3372 | * (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] | 3373 | * always use map_and_fenceable for all scanout buffers. However, |
| 3374 | * it may simply be too big to fit into mappable, in which case |
| 3375 | * put it anyway and hope that userspace can cope (but always first |
| 3376 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3377 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3378 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3379 | if ((flags & PIN_MAPPABLE) == 0 && |
| 3380 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3381 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3382 | flags | |
| 3383 | PIN_MAPPABLE | |
| 3384 | PIN_NONBLOCK); |
| 3385 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3386 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3387 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3388 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3389 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 3390 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 3391 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3392 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3393 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3394 | /* It should now be out of any other write domains, and we can update |
| 3395 | * the domain values for our changes. |
| 3396 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3397 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3398 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3399 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3400 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3401 | err_unpin_global: |
| 3402 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3403 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3407 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3408 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 3409 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3410 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3411 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 3412 | return; |
| 3413 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3414 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 3415 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3416 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3417 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 3418 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3419 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3420 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3421 | } |
| 3422 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3423 | /** |
| 3424 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3425 | * @obj: object to act on |
| 3426 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3427 | * |
| 3428 | * This function returns when the move is complete, including waiting on |
| 3429 | * flushes to occur. |
| 3430 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3431 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3432 | 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] | 3433 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3434 | int ret; |
| 3435 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3436 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3437 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3438 | ret = i915_gem_object_wait(obj, |
| 3439 | I915_WAIT_INTERRUPTIBLE | |
| 3440 | I915_WAIT_LOCKED | |
| 3441 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3442 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3443 | if (ret) |
| 3444 | return ret; |
| 3445 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3446 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3447 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3448 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3449 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 3450 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3451 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | /* It should now be out of any other write domains, and we can update |
| 3455 | * the domain values for our changes. |
| 3456 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3457 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3458 | |
| 3459 | /* If we're writing through the CPU, then the GPU read domains will |
| 3460 | * need to be invalidated at next use. |
| 3461 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3462 | if (write) |
| 3463 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3464 | |
| 3465 | return 0; |
| 3466 | } |
| 3467 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3468 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3469 | * emitted over 20 msec ago. |
| 3470 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3471 | * Note that if we were to use the current jiffies each time around the loop, |
| 3472 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3473 | * render a frame was over 20ms. |
| 3474 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3475 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3476 | * relatively low latency when blocking on a particular request to finish. |
| 3477 | */ |
| 3478 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3479 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3480 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3481 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3482 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 3483 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3484 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3485 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3486 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 3487 | /* ABI: return -EIO if already wedged */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 3488 | ret = i915_terminally_wedged(dev_priv); |
| 3489 | if (ret) |
| 3490 | return ret; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3491 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3492 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3493 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3494 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3495 | break; |
| 3496 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3497 | if (target) { |
| 3498 | list_del(&target->client_link); |
| 3499 | target->file_priv = NULL; |
| 3500 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 3501 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3502 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3503 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3504 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3505 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3506 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3507 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3508 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3509 | return 0; |
| 3510 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3511 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3512 | I915_WAIT_INTERRUPTIBLE, |
| 3513 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3514 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3515 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3516 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3519 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3520 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 3521 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 3522 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 3523 | u64 alignment, |
| 3524 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3525 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3526 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 3527 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3528 | struct i915_vma *vma; |
| 3529 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 3530 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3531 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3532 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 3533 | if (flags & PIN_MAPPABLE && |
| 3534 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3535 | /* If the required space is larger than the available |
| 3536 | * aperture, we will not able to find a slot for the |
| 3537 | * object and unbinding the object now will be in |
| 3538 | * vain. Worse, doing so may cause us to ping-pong |
| 3539 | * the object in and out of the Global GTT and |
| 3540 | * waste a lot of cycles under the mutex. |
| 3541 | */ |
| 3542 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 3543 | return ERR_PTR(-E2BIG); |
| 3544 | |
| 3545 | /* If NONBLOCK is set the caller is optimistically |
| 3546 | * trying to cache the full object within the mappable |
| 3547 | * aperture, and *must* have a fallback in place for |
| 3548 | * situations where we cannot bind the object. We |
| 3549 | * can be a little more lax here and use the fallback |
| 3550 | * more often to avoid costly migrations of ourselves |
| 3551 | * and other objects within the aperture. |
| 3552 | * |
| 3553 | * Half-the-aperture is used as a simple heuristic. |
| 3554 | * More interesting would to do search for a free |
| 3555 | * block prior to making the commitment to unbind. |
| 3556 | * That caters for the self-harm case, and with a |
| 3557 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 3558 | * we could try to minimise harm to others. |
| 3559 | */ |
| 3560 | if (flags & PIN_NONBLOCK && |
| 3561 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 3562 | return ERR_PTR(-ENOSPC); |
| 3563 | } |
| 3564 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 3565 | vma = i915_vma_instance(obj, vm, view); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 3566 | if (IS_ERR(vma)) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3567 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3568 | |
| 3569 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3570 | if (flags & PIN_NONBLOCK) { |
| 3571 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 3572 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3573 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3574 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 3575 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3576 | return ERR_PTR(-ENOSPC); |
| 3577 | } |
| 3578 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3579 | WARN(i915_vma_is_pinned(vma), |
| 3580 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3581 | " offset=%08x, req.alignment=%llx," |
| 3582 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 3583 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3584 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3585 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3586 | ret = i915_vma_unbind(vma); |
| 3587 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3588 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3589 | } |
| 3590 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3591 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 3592 | if (ret) |
| 3593 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3594 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3595 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3596 | } |
| 3597 | |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3598 | static __always_inline u32 __busy_read_flag(u8 id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3599 | { |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3600 | if (id == (u8)I915_ENGINE_CLASS_INVALID) |
| 3601 | return 0xffff0000u; |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3602 | |
| 3603 | GEM_BUG_ON(id >= 16); |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3604 | return 0x10000u << id; |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3605 | } |
| 3606 | |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3607 | static __always_inline u32 __busy_write_id(u8 id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3608 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3609 | /* |
| 3610 | * The uABI guarantees an active writer is also amongst the read |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 3611 | * engines. This would be true if we accessed the activity tracking |
| 3612 | * under the lock, but as we perform the lookup of the object and |
| 3613 | * its activity locklessly we can not guarantee that the last_write |
| 3614 | * being active implies that we have set the same engine flag from |
| 3615 | * last_read - hence we always set both read and write busy for |
| 3616 | * last_write. |
| 3617 | */ |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3618 | if (id == (u8)I915_ENGINE_CLASS_INVALID) |
| 3619 | return 0xffffffffu; |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3620 | |
| 3621 | return (id + 1) | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3622 | } |
| 3623 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3624 | static __always_inline unsigned int |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3625 | __busy_set_if_active(const struct dma_fence *fence, u32 (*flag)(u8 id)) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3626 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3627 | const struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3628 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3629 | /* |
| 3630 | * We have to check the current hw status of the fence as the uABI |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3631 | * guarantees forward progress. We could rely on the idle worker |
| 3632 | * to eventually flush us, but to minimise latency just ask the |
| 3633 | * hardware. |
| 3634 | * |
| 3635 | * Note we only report on the status of native fences. |
| 3636 | */ |
| 3637 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3638 | return 0; |
| 3639 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3640 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3641 | rq = container_of(fence, const struct i915_request, fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3642 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3643 | return 0; |
| 3644 | |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3645 | /* Beware type-expansion follies! */ |
| 3646 | BUILD_BUG_ON(!typecheck(u8, rq->engine->uabi_class)); |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3647 | return flag(rq->engine->uabi_class); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3648 | } |
| 3649 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3650 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3651 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3652 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3653 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3654 | } |
| 3655 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3656 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3657 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3658 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3659 | if (!fence) |
| 3660 | return 0; |
| 3661 | |
| 3662 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3663 | } |
| 3664 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3665 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3666 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3667 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3668 | { |
| 3669 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3670 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3671 | struct reservation_object_list *list; |
| 3672 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3673 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3674 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3675 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3676 | rcu_read_lock(); |
| 3677 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3678 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3679 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3680 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3681 | /* |
| 3682 | * A discrepancy here is that we do not report the status of |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3683 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 3684 | * a call to set-domain may still stall waiting for foreign rendering. |
| 3685 | * This also means that wait-ioctl may report an object as busy, |
| 3686 | * where busy-ioctl considers it idle. |
| 3687 | * |
| 3688 | * We trade the ability to warn of foreign fences to report on which |
| 3689 | * i915 engines are active for the object. |
| 3690 | * |
| 3691 | * Alternatively, we can trade that extra information on read/write |
| 3692 | * activity with |
| 3693 | * args->busy = |
| 3694 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 3695 | * to report the overall busyness. This is what the wait-ioctl does. |
| 3696 | * |
| 3697 | */ |
| 3698 | retry: |
| 3699 | seq = raw_read_seqcount(&obj->resv->seq); |
| 3700 | |
| 3701 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 3702 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 3703 | |
| 3704 | /* Translate shared fences to READ set of engines */ |
| 3705 | list = rcu_dereference(obj->resv->fence); |
| 3706 | if (list) { |
| 3707 | unsigned int shared_count = list->shared_count, i; |
| 3708 | |
| 3709 | for (i = 0; i < shared_count; ++i) { |
| 3710 | struct dma_fence *fence = |
| 3711 | rcu_dereference(list->shared[i]); |
| 3712 | |
| 3713 | args->busy |= busy_check_reader(fence); |
| 3714 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3715 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3716 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3717 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 3718 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 3719 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3720 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3721 | out: |
| 3722 | rcu_read_unlock(); |
| 3723 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | int |
| 3727 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 3728 | struct drm_file *file_priv) |
| 3729 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 3730 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3733 | int |
| 3734 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 3735 | struct drm_file *file_priv) |
| 3736 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3737 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3738 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3739 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3740 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3741 | |
| 3742 | switch (args->madv) { |
| 3743 | case I915_MADV_DONTNEED: |
| 3744 | case I915_MADV_WILLNEED: |
| 3745 | break; |
| 3746 | default: |
| 3747 | return -EINVAL; |
| 3748 | } |
| 3749 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3750 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3751 | if (!obj) |
| 3752 | return -ENOENT; |
| 3753 | |
| 3754 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 3755 | if (err) |
| 3756 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3757 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 3758 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 3759 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 3760 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3761 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 3762 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3763 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3764 | obj->mm.quirked = false; |
| 3765 | } |
| 3766 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 3767 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3768 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3769 | obj->mm.quirked = true; |
| 3770 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 3771 | } |
| 3772 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3773 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 3774 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3775 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 3776 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 3777 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 3778 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d6692e | 2019-04-20 12:55:39 +0100 | [diff] [blame] | 3779 | __i915_gem_object_truncate(obj); |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 3780 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3781 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3782 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 3783 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3784 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 3785 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3786 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3787 | } |
| 3788 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3789 | static void |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3790 | frontbuffer_retire(struct i915_active_request *active, |
| 3791 | struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3792 | { |
| 3793 | struct drm_i915_gem_object *obj = |
| 3794 | container_of(active, typeof(*obj), frontbuffer_write); |
| 3795 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 3796 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3799 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 3800 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3801 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3802 | mutex_init(&obj->mm.lock); |
| 3803 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3804 | spin_lock_init(&obj->vma.lock); |
| 3805 | INIT_LIST_HEAD(&obj->vma.list); |
| 3806 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3807 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 3808 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3809 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 3810 | init_rcu_head(&obj->rcu); |
| 3811 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3812 | obj->ops = ops; |
| 3813 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3814 | reservation_object_init(&obj->__builtin_resv); |
| 3815 | obj->resv = &obj->__builtin_resv; |
| 3816 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 3817 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3818 | i915_active_request_init(&obj->frontbuffer_write, |
| 3819 | NULL, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3820 | |
| 3821 | obj->mm.madv = I915_MADV_WILLNEED; |
| 3822 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 3823 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3824 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 3825 | 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] | 3826 | } |
| 3827 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3828 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 3829 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 3830 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 3831 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3832 | .get_pages = i915_gem_object_get_pages_gtt, |
| 3833 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 3834 | |
| 3835 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3836 | }; |
| 3837 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 3838 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 3839 | struct drm_gem_object *obj, |
| 3840 | size_t size) |
| 3841 | { |
| 3842 | struct drm_i915_private *i915 = to_i915(dev); |
| 3843 | unsigned long flags = VM_NORESERVE; |
| 3844 | struct file *filp; |
| 3845 | |
| 3846 | drm_gem_private_object_init(dev, obj, size); |
| 3847 | |
| 3848 | if (i915->mm.gemfs) |
| 3849 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 3850 | flags); |
| 3851 | else |
| 3852 | filp = shmem_file_setup("i915", size, flags); |
| 3853 | |
| 3854 | if (IS_ERR(filp)) |
| 3855 | return PTR_ERR(filp); |
| 3856 | |
| 3857 | obj->filp = filp; |
| 3858 | |
| 3859 | return 0; |
| 3860 | } |
| 3861 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 3862 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 3863 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 3864 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3865 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 3866 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3867 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 3868 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3869 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3870 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 3871 | /* There is a prevalence of the assumption that we fit the object's |
| 3872 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 3873 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 3874 | * such a local variable, please consider fixing! |
| 3875 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 3876 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 3877 | return ERR_PTR(-E2BIG); |
| 3878 | |
| 3879 | if (overflows_type(size, obj->base.size)) |
| 3880 | return ERR_PTR(-E2BIG); |
| 3881 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 3882 | obj = i915_gem_object_alloc(); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3883 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3884 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3885 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 3886 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3887 | if (ret) |
| 3888 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3889 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 3890 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 3891 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 3892 | /* 965gm cannot relocate objects above 4GiB. */ |
| 3893 | mask &= ~__GFP_HIGHMEM; |
| 3894 | mask |= __GFP_DMA32; |
| 3895 | } |
| 3896 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 3897 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 3898 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 3899 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 3900 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3901 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 3902 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3903 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 3904 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3905 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3906 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 3907 | /* 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] | 3908 | * cache) for about a 10% performance improvement |
| 3909 | * compared to uncached. Graphics requests other than |
| 3910 | * display scanout are coherent with the CPU in |
| 3911 | * accessing this cache. This means in this mode we |
| 3912 | * don't need to clflush on the CPU side, and on the |
| 3913 | * GPU side we only need to flush internal caches to |
| 3914 | * get data visible to the CPU. |
| 3915 | * |
| 3916 | * However, we maintain the display planes as UC, and so |
| 3917 | * need to rebind when first used as such. |
| 3918 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3919 | cache_level = I915_CACHE_LLC; |
| 3920 | else |
| 3921 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 3922 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3923 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3924 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 3925 | trace_i915_gem_object_create(obj); |
| 3926 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3927 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3928 | |
| 3929 | fail: |
| 3930 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3931 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 3934 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 3935 | { |
| 3936 | /* If we are the last user of the backing storage (be it shmemfs |
| 3937 | * pages or stolen etc), we know that the pages are going to be |
| 3938 | * immediately released. In this case, we can then skip copying |
| 3939 | * back the contents from the GPU. |
| 3940 | */ |
| 3941 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3942 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 3943 | return false; |
| 3944 | |
| 3945 | if (obj->base.filp == NULL) |
| 3946 | return true; |
| 3947 | |
| 3948 | /* At first glance, this looks racy, but then again so would be |
| 3949 | * userspace racing mmap against close. However, the first external |
| 3950 | * reference to the filp can only be obtained through the |
| 3951 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 3952 | * acquiring such a reference whilst we are in the middle of |
| 3953 | * freeing the object. |
| 3954 | */ |
| 3955 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 3956 | } |
| 3957 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3958 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 3959 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 3960 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3961 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 3962 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 3963 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 3964 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 3965 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3966 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 3967 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3968 | trace_i915_gem_object_destroy(obj); |
| 3969 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 3970 | mutex_lock(&i915->drm.struct_mutex); |
| 3971 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3972 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3973 | list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3974 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 3975 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 3976 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3977 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3978 | GEM_BUG_ON(!list_empty(&obj->vma.list)); |
| 3979 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3980 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 3981 | /* This serializes freeing with the shrinker. Since the free |
| 3982 | * is delayed, first by RCU then by the workqueue, we want the |
| 3983 | * shrinker to be able to free pages of unreferenced objects, |
| 3984 | * or else we may oom whilst there are plenty of deferred |
| 3985 | * freed objects. |
| 3986 | */ |
| 3987 | if (i915_gem_object_has_pages(obj)) { |
| 3988 | spin_lock(&i915->mm.obj_lock); |
| 3989 | list_del_init(&obj->mm.link); |
| 3990 | spin_unlock(&i915->mm.obj_lock); |
| 3991 | } |
| 3992 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 3993 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3994 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3995 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 3996 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3997 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 3998 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3999 | |
| 4000 | if (obj->ops->release) |
| 4001 | obj->ops->release(obj); |
| 4002 | |
| 4003 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4004 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4005 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4006 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4007 | |
| 4008 | if (obj->base.import_attach) |
| 4009 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4010 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4011 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4012 | drm_gem_object_release(&obj->base); |
| 4013 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4014 | |
Andy Shevchenko | 6e514e3 | 2019-03-04 11:29:08 +0200 | [diff] [blame] | 4015 | bitmap_free(obj->bit_17); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4016 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4017 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4018 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4019 | atomic_dec(&i915->mm.free_count); |
| 4020 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4021 | if (on) |
| 4022 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4023 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4024 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4028 | { |
| 4029 | struct llist_node *freed; |
| 4030 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4031 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4032 | freed = NULL; |
| 4033 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4034 | /* Only one consumer of llist_del_first() allowed */ |
| 4035 | spin_lock(&i915->mm.free_lock); |
| 4036 | freed = llist_del_first(&i915->mm.free_list); |
| 4037 | spin_unlock(&i915->mm.free_lock); |
| 4038 | } |
| 4039 | if (unlikely(freed)) { |
| 4040 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4041 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4042 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | static void __i915_gem_free_work(struct work_struct *work) |
| 4046 | { |
| 4047 | struct drm_i915_private *i915 = |
| 4048 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4049 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4050 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4051 | /* |
| 4052 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4053 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4054 | * However, the object may also be bound into the global GTT (e.g. |
| 4055 | * older GPUs without per-process support, or for direct access through |
| 4056 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4057 | * unbound now. |
| 4058 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4059 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4060 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4061 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4062 | spin_unlock(&i915->mm.free_lock); |
| 4063 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4064 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4065 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4066 | return; |
| 4067 | |
| 4068 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4069 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4070 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4074 | { |
| 4075 | struct drm_i915_gem_object *obj = |
| 4076 | container_of(head, typeof(*obj), rcu); |
| 4077 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4078 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4079 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4080 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4081 | * it like a rcu_head from this point forwards. And we expect all |
| 4082 | * objects to be freed via this path. |
| 4083 | */ |
| 4084 | destroy_rcu_head(&obj->rcu); |
| 4085 | |
| 4086 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4087 | * Since we require blocking on struct_mutex to unbind the freed |
| 4088 | * object from the GPU before releasing resources back to the |
| 4089 | * system, we can not do that directly from the RCU callback (which may |
| 4090 | * be a softirq context), but must instead then defer that work onto a |
| 4091 | * kthread. We use the RCU callback rather than move the freed object |
| 4092 | * directly onto the work queue so that we can mix between using the |
| 4093 | * worker and performing frees directly from subsequent allocations for |
| 4094 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4095 | */ |
| 4096 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4097 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4101 | { |
| 4102 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4103 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4104 | if (obj->mm.quirked) |
| 4105 | __i915_gem_object_unpin_pages(obj); |
| 4106 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4107 | if (discard_backing_storage(obj)) |
| 4108 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4109 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4110 | /* |
| 4111 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4112 | * read-side critical sections are complete, e.g. |
| 4113 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4114 | * lookup see i915_gem_object_lookup_rcu(). |
| 4115 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4116 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4117 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4118 | } |
| 4119 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4120 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4121 | { |
| 4122 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4123 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4124 | if (!i915_gem_object_has_active_reference(obj) && |
| 4125 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4126 | i915_gem_object_set_active_reference(obj); |
| 4127 | else |
| 4128 | i915_gem_object_put(obj); |
| 4129 | } |
| 4130 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4131 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4132 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4133 | intel_wakeref_t wakeref; |
| 4134 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4135 | GEM_TRACE("\n"); |
| 4136 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4137 | wakeref = intel_runtime_pm_get(i915); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4138 | intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4139 | |
| 4140 | /* |
| 4141 | * As we have just resumed the machine and woken the device up from |
| 4142 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4143 | * back to defaults, recovering from whatever wedged state we left it |
| 4144 | * in and so worth trying to use the device once more. |
| 4145 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4146 | if (i915_terminally_wedged(i915)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4147 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4148 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4149 | /* |
| 4150 | * If we inherit context state from the BIOS or earlier occupants |
| 4151 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4152 | * try to take over. The only way to remove the earlier state |
| 4153 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4154 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4155 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4156 | */ |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 4157 | intel_gt_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4158 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4159 | intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4160 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4161 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4162 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4163 | i915_gem_contexts_lost(i915); |
| 4164 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4167 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4168 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4169 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4170 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4171 | return; |
| 4172 | |
| 4173 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4174 | DISP_TILE_SURFACE_SWIZZLING); |
| 4175 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4176 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4177 | return; |
| 4178 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4179 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4180 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4181 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4182 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4183 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4184 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4185 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4186 | else |
| 4187 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4188 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4189 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4190 | 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] | 4191 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4192 | I915_WRITE(RING_CTL(base), 0); |
| 4193 | I915_WRITE(RING_HEAD(base), 0); |
| 4194 | I915_WRITE(RING_TAIL(base), 0); |
| 4195 | I915_WRITE(RING_START(base), 0); |
| 4196 | } |
| 4197 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4198 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4199 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4200 | if (IS_I830(dev_priv)) { |
| 4201 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4202 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4203 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4204 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4205 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4206 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4207 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4208 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4209 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4210 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4211 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4212 | } |
| 4213 | } |
| 4214 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4215 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4216 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4217 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4218 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4219 | dev_priv->gt.last_init_time = ktime_get(); |
| 4220 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4221 | /* Double layer security blanket, see i915_gem_init() */ |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4222 | intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4223 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4224 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4225 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4226 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4227 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4228 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4229 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4230 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4231 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4232 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4233 | /* ...and determine whether they are sticking. */ |
| 4234 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 4235 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4236 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4237 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4238 | /* |
| 4239 | * At least 830 can leave some of the unused rings |
| 4240 | * "active" (ie. head != tail) after resume which |
| 4241 | * will prevent c3 entry. Makes sure all unused rings |
| 4242 | * are totally idle. |
| 4243 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4244 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4245 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4246 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4247 | ret = i915_terminally_wedged(dev_priv); |
| 4248 | if (ret) |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4249 | goto out; |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4250 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4251 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4252 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4253 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4254 | goto out; |
| 4255 | } |
| 4256 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 4257 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 4258 | if (ret) { |
| 4259 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 4260 | goto out; |
| 4261 | } |
| 4262 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4263 | /* We can't enable contexts until all firmware is loaded */ |
| 4264 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4265 | if (ret) { |
| 4266 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4267 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4268 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4269 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4270 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 4271 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 4272 | /* Only when the HW is re-initialised, can we replay the requests */ |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 4273 | ret = intel_engines_resume(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4274 | if (ret) |
| 4275 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4276 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4277 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4278 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 4279 | intel_engines_set_scheduler_caps(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4280 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4281 | |
| 4282 | cleanup_uc: |
| 4283 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4284 | out: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4285 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4286 | |
| 4287 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4288 | } |
| 4289 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4290 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 4291 | { |
| 4292 | struct i915_gem_context *ctx; |
| 4293 | struct intel_engine_cs *engine; |
| 4294 | enum intel_engine_id id; |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4295 | int err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4296 | |
| 4297 | /* |
| 4298 | * As we reset the gpu during very early sanitisation, the current |
| 4299 | * register state on the GPU should reflect its defaults values. |
| 4300 | * We load a context onto the hw (with restore-inhibit), then switch |
| 4301 | * over to a second context to save that default register state. We |
| 4302 | * can then prime every new context with that state so they all start |
| 4303 | * from the same default HW values. |
| 4304 | */ |
| 4305 | |
| 4306 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 4307 | if (IS_ERR(ctx)) |
| 4308 | return PTR_ERR(ctx); |
| 4309 | |
| 4310 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4311 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4312 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4313 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4314 | if (IS_ERR(rq)) { |
| 4315 | err = PTR_ERR(rq); |
| 4316 | goto out_ctx; |
| 4317 | } |
| 4318 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 4319 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4320 | if (engine->init_context) |
| 4321 | err = engine->init_context(rq); |
| 4322 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 4323 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4324 | if (err) |
| 4325 | goto err_active; |
| 4326 | } |
| 4327 | |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4328 | /* Flush the default context image to memory, and enable powersaving. */ |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 4329 | if (!i915_gem_load_power_context(i915)) { |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4330 | err = -EIO; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4331 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4332 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4333 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4334 | for_each_engine(engine, i915, id) { |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4335 | struct intel_context *ce; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4336 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4337 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4338 | |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4339 | ce = intel_context_lookup(ctx, engine); |
| 4340 | if (!ce) |
| 4341 | continue; |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4342 | |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4343 | state = ce->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4344 | if (!state) |
| 4345 | continue; |
| 4346 | |
Chris Wilson | 0881954 | 2019-03-08 13:25:22 +0000 | [diff] [blame] | 4347 | GEM_BUG_ON(intel_context_is_pinned(ce)); |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4348 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4349 | /* |
| 4350 | * As we will hold a reference to the logical state, it will |
| 4351 | * not be torn down with the context, and importantly the |
| 4352 | * object will hold onto its vma (making it possible for a |
| 4353 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 4354 | * from the GTT to prevent such accidents and reclaim the |
| 4355 | * space. |
| 4356 | */ |
| 4357 | err = i915_vma_unbind(state); |
| 4358 | if (err) |
| 4359 | goto err_active; |
| 4360 | |
| 4361 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 4362 | if (err) |
| 4363 | goto err_active; |
| 4364 | |
| 4365 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 4366 | i915_gem_object_set_cache_coherency(engine->default_state, |
| 4367 | I915_CACHE_LLC); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4368 | |
| 4369 | /* Check we can acquire the image of the context state */ |
| 4370 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4371 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4372 | if (IS_ERR(vaddr)) { |
| 4373 | err = PTR_ERR(vaddr); |
| 4374 | goto err_active; |
| 4375 | } |
| 4376 | |
| 4377 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 4381 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 4382 | |
| 4383 | /* |
| 4384 | * Make sure that classes with multiple engine instances all |
| 4385 | * share the same basic configuration. |
| 4386 | */ |
| 4387 | for_each_engine(engine, i915, id) { |
| 4388 | unsigned int bit = BIT(engine->uabi_class); |
| 4389 | unsigned int expected = engine->default_state ? bit : 0; |
| 4390 | |
| 4391 | if ((found & bit) != expected) { |
| 4392 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 4393 | engine->uabi_class, engine->name); |
| 4394 | } |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | out_ctx: |
| 4399 | i915_gem_context_set_closed(ctx); |
| 4400 | i915_gem_context_put(ctx); |
| 4401 | return err; |
| 4402 | |
| 4403 | err_active: |
| 4404 | /* |
| 4405 | * If we have to abandon now, we expect the engines to be idle |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4406 | * and ready to be torn-down. The quickest way we can accomplish |
| 4407 | * this is by declaring ourselves wedged. |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4408 | */ |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4409 | i915_gem_set_wedged(i915); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4410 | goto out_ctx; |
| 4411 | } |
| 4412 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4413 | static int |
| 4414 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 4415 | { |
| 4416 | struct drm_i915_gem_object *obj; |
| 4417 | struct i915_vma *vma; |
| 4418 | int ret; |
| 4419 | |
| 4420 | obj = i915_gem_object_create_stolen(i915, size); |
| 4421 | if (!obj) |
| 4422 | obj = i915_gem_object_create_internal(i915, size); |
| 4423 | if (IS_ERR(obj)) { |
| 4424 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 4425 | return PTR_ERR(obj); |
| 4426 | } |
| 4427 | |
| 4428 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 4429 | if (IS_ERR(vma)) { |
| 4430 | ret = PTR_ERR(vma); |
| 4431 | goto err_unref; |
| 4432 | } |
| 4433 | |
| 4434 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 4435 | if (ret) |
| 4436 | goto err_unref; |
| 4437 | |
| 4438 | i915->gt.scratch = vma; |
| 4439 | return 0; |
| 4440 | |
| 4441 | err_unref: |
| 4442 | i915_gem_object_put(obj); |
| 4443 | return ret; |
| 4444 | } |
| 4445 | |
| 4446 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 4447 | { |
| 4448 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 4449 | } |
| 4450 | |
Chris Wilson | 254e118 | 2019-04-17 08:56:28 +0100 | [diff] [blame] | 4451 | static int intel_engines_verify_workarounds(struct drm_i915_private *i915) |
| 4452 | { |
| 4453 | struct intel_engine_cs *engine; |
| 4454 | enum intel_engine_id id; |
| 4455 | int err = 0; |
| 4456 | |
| 4457 | if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) |
| 4458 | return 0; |
| 4459 | |
| 4460 | for_each_engine(engine, i915, id) { |
| 4461 | if (intel_engine_verify_workarounds(engine, "load")) |
| 4462 | err = -EIO; |
| 4463 | } |
| 4464 | |
| 4465 | return err; |
| 4466 | } |
| 4467 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4468 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4469 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4470 | int ret; |
| 4471 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 4472 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 4473 | if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv)) |
Matthew Auld | da9fe3f3 | 2017-10-06 23:18:31 +0100 | [diff] [blame] | 4474 | mkwrite_device_info(dev_priv)->page_sizes = |
| 4475 | I915_GTT_PAGE_SIZE_4K; |
| 4476 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 4477 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4478 | |
Chris Wilson | 9726920 | 2019-04-10 20:01:20 +0100 | [diff] [blame] | 4479 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 4480 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | 9726920 | 2019-04-10 20:01:20 +0100 | [diff] [blame] | 4481 | else |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4482 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 4483 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4484 | i915_timelines_init(dev_priv); |
| 4485 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4486 | ret = i915_gem_init_userptr(dev_priv); |
| 4487 | if (ret) |
| 4488 | return ret; |
| 4489 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4490 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 4491 | if (ret) |
| 4492 | return ret; |
| 4493 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4494 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 4495 | if (ret) |
| 4496 | goto err_uc_misc; |
| 4497 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4498 | /* This is just a security blanket to placate dragons. |
| 4499 | * On some systems, we very sporadically observe that the first TLBs |
| 4500 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 4501 | * we hold the forcewake during initialisation these problems |
| 4502 | * just magically go away. |
| 4503 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4504 | mutex_lock(&dev_priv->drm.struct_mutex); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4505 | intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4506 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 4507 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4508 | if (ret) { |
| 4509 | GEM_BUG_ON(ret == -EIO); |
| 4510 | goto err_unlock; |
| 4511 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 4512 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4513 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4514 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4515 | if (ret) { |
| 4516 | GEM_BUG_ON(ret == -EIO); |
| 4517 | goto err_ggtt; |
| 4518 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 4519 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4520 | ret = i915_gem_contexts_init(dev_priv); |
| 4521 | if (ret) { |
| 4522 | GEM_BUG_ON(ret == -EIO); |
| 4523 | goto err_scratch; |
| 4524 | } |
| 4525 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4526 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4527 | if (ret) { |
| 4528 | GEM_BUG_ON(ret == -EIO); |
| 4529 | goto err_context; |
| 4530 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 4531 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 4532 | intel_init_gt_powersave(dev_priv); |
| 4533 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4534 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4535 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4536 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4537 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4538 | ret = i915_gem_init_hw(dev_priv); |
| 4539 | if (ret) |
| 4540 | goto err_uc_init; |
| 4541 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4542 | /* |
| 4543 | * Despite its name intel_init_clock_gating applies both display |
| 4544 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 4545 | * GT power context workaround. Worse, sometimes it includes a context |
| 4546 | * register workaround which we need to apply before we record the |
| 4547 | * default HW state for all contexts. |
| 4548 | * |
| 4549 | * FIXME: break up the workarounds and apply them at the right time! |
| 4550 | */ |
| 4551 | intel_init_clock_gating(dev_priv); |
| 4552 | |
Chris Wilson | 254e118 | 2019-04-17 08:56:28 +0100 | [diff] [blame] | 4553 | ret = intel_engines_verify_workarounds(dev_priv); |
| 4554 | if (ret) |
| 4555 | goto err_init_hw; |
| 4556 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4557 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4558 | if (ret) |
| 4559 | goto err_init_hw; |
| 4560 | |
| 4561 | if (i915_inject_load_failure()) { |
| 4562 | ret = -ENODEV; |
| 4563 | goto err_init_hw; |
| 4564 | } |
| 4565 | |
| 4566 | if (i915_inject_load_failure()) { |
| 4567 | ret = -EIO; |
| 4568 | goto err_init_hw; |
| 4569 | } |
| 4570 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4571 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4572 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4573 | |
| 4574 | return 0; |
| 4575 | |
| 4576 | /* |
| 4577 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 4578 | * disable GPU submission but keep KMS alive. We want to mark the |
| 4579 | * HW as irrevisibly wedged, but keep enough state around that the |
| 4580 | * driver doesn't explode during runtime. |
| 4581 | */ |
| 4582 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4583 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4584 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 4585 | i915_gem_set_wedged(dev_priv); |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 4586 | i915_gem_suspend(dev_priv); |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4587 | i915_gem_suspend_late(dev_priv); |
| 4588 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 4589 | i915_gem_drain_workqueue(dev_priv); |
| 4590 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4591 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4592 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4593 | err_uc_init: |
| 4594 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4595 | err_pm: |
| 4596 | if (ret != -EIO) { |
| 4597 | intel_cleanup_gt_powersave(dev_priv); |
| 4598 | i915_gem_cleanup_engines(dev_priv); |
| 4599 | } |
| 4600 | err_context: |
| 4601 | if (ret != -EIO) |
| 4602 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4603 | err_scratch: |
| 4604 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4605 | err_ggtt: |
| 4606 | err_unlock: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4607 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4608 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4609 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4610 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4611 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 4612 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4613 | if (ret != -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4614 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4615 | i915_timelines_fini(dev_priv); |
| 4616 | } |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4617 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4618 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 4619 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 4620 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4621 | /* |
| 4622 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4623 | * wedged. But we only want to do this where the GPU is angry, |
| 4624 | * for all other failure, such as an allocation failure, bail. |
| 4625 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4626 | if (!i915_reset_failed(dev_priv)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 4627 | i915_load_error(dev_priv, |
| 4628 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4629 | i915_gem_set_wedged(dev_priv); |
| 4630 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 4631 | |
| 4632 | /* Minimal basic recovery for KMS */ |
| 4633 | ret = i915_ggtt_enable_hw(dev_priv); |
| 4634 | i915_gem_restore_gtt_mappings(dev_priv); |
| 4635 | i915_gem_restore_fences(dev_priv); |
| 4636 | intel_init_clock_gating(dev_priv); |
| 4637 | |
| 4638 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4639 | } |
| 4640 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4641 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4642 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4643 | } |
| 4644 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4645 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 4646 | { |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 4647 | GEM_BUG_ON(dev_priv->gt.awake); |
| 4648 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4649 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 4650 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4651 | |
| 4652 | /* Flush any outstanding unpin_work. */ |
| 4653 | i915_gem_drain_workqueue(dev_priv); |
| 4654 | |
| 4655 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 4656 | intel_uc_fini_hw(dev_priv); |
| 4657 | intel_uc_fini(dev_priv); |
| 4658 | i915_gem_cleanup_engines(dev_priv); |
| 4659 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4660 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4661 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4662 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4663 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 4664 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 4665 | intel_cleanup_gt_powersave(dev_priv); |
| 4666 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4667 | intel_uc_fini_misc(dev_priv); |
| 4668 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4669 | i915_timelines_fini(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4670 | |
| 4671 | i915_gem_drain_freed_objects(dev_priv); |
| 4672 | |
| 4673 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 4674 | } |
| 4675 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4676 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 4677 | { |
| 4678 | i915_gem_sanitize(i915); |
| 4679 | } |
| 4680 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4681 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 4682 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4683 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4684 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4685 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4686 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4687 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 4688 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4689 | } |
| 4690 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4691 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4692 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 4693 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 4694 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4695 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 4696 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4697 | !IS_CHERRYVIEW(dev_priv)) |
| 4698 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 4699 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 4700 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 4701 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4702 | dev_priv->num_fence_regs = 16; |
| 4703 | else |
| 4704 | dev_priv->num_fence_regs = 8; |
| 4705 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 4706 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4707 | dev_priv->num_fence_regs = |
| 4708 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 4709 | |
| 4710 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 4711 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 4712 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 4713 | |
| 4714 | fence->i915 = dev_priv; |
| 4715 | fence->id = i; |
| 4716 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 4717 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 4718 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4719 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 4720 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4721 | } |
| 4722 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 4723 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 4724 | { |
| 4725 | spin_lock_init(&i915->mm.object_stat_lock); |
| 4726 | spin_lock_init(&i915->mm.obj_lock); |
| 4727 | spin_lock_init(&i915->mm.free_lock); |
| 4728 | |
| 4729 | init_llist_head(&i915->mm.free_list); |
| 4730 | |
| 4731 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 4732 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 4733 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 4734 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 4735 | |
| 4736 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 4737 | } |
| 4738 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 4739 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4740 | { |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 4741 | int err; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4742 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame^] | 4743 | intel_gt_pm_init(dev_priv); |
| 4744 | |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 4745 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4746 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 4747 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 4748 | i915_gem_init__mm(dev_priv); |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 4749 | i915_gem_init__pm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4750 | |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 4751 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 4752 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 4753 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 4754 | init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4755 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 4756 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 4757 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 4758 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 4759 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4760 | err = i915_gemfs_init(dev_priv); |
| 4761 | if (err) |
| 4762 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 4763 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 4764 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4765 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4766 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 4767 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 4768 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 4769 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4770 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 4771 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 4772 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 4773 | |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 4774 | cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
| 4775 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4776 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 4777 | } |
| 4778 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 4779 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 4780 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 4781 | /* Discard all purgeable objects, let userspace recover those as |
| 4782 | * required after resuming. |
| 4783 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 4784 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 4785 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 4786 | return 0; |
| 4787 | } |
| 4788 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4789 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4790 | { |
| 4791 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 4792 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4793 | &i915->mm.unbound_list, |
| 4794 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 4795 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4796 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4797 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4798 | /* |
| 4799 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4800 | * |
| 4801 | * We need to update the domain tracking to reflect that the CPU |
| 4802 | * will be accessing all the pages to create and restore from the |
| 4803 | * hibernation, and so upon restoration those pages will be in the |
| 4804 | * CPU domain. |
| 4805 | * |
| 4806 | * To make sure the hibernation image contains the latest state, |
| 4807 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 4808 | * |
| 4809 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 4810 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4811 | */ |
| 4812 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4813 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 4814 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4815 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4816 | mutex_lock(&i915->drm.struct_mutex); |
| 4817 | for (phase = phases; *phase; phase++) { |
| 4818 | list_for_each_entry(obj, *phase, mm.link) |
| 4819 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4820 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4821 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4822 | |
| 4823 | return 0; |
| 4824 | } |
| 4825 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4826 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4827 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4828 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4829 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4830 | |
| 4831 | /* Clean up our request list when the client is going away, so that |
| 4832 | * later retire_requests won't dereference our soon-to-be-gone |
| 4833 | * file_priv. |
| 4834 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4835 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4836 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4837 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4838 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4839 | } |
| 4840 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4841 | 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] | 4842 | { |
| 4843 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 4844 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4845 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 4846 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4847 | |
| 4848 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 4849 | if (!file_priv) |
| 4850 | return -ENOMEM; |
| 4851 | |
| 4852 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4853 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 4854 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4855 | |
| 4856 | spin_lock_init(&file_priv->mm.lock); |
| 4857 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4858 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 4859 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 4860 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 4861 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4862 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 4863 | if (ret) |
| 4864 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4865 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 4866 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4867 | } |
| 4868 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 4869 | /** |
| 4870 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 4871 | * @old: current GEM buffer for the frontbuffer slots |
| 4872 | * @new: new GEM buffer for the frontbuffer slots |
| 4873 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 4874 | * |
| 4875 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 4876 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 4877 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4878 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 4879 | struct drm_i915_gem_object *new, |
| 4880 | unsigned frontbuffer_bits) |
| 4881 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 4882 | /* Control of individual bits within the mask are guarded by |
| 4883 | * the owning plane->mutex, i.e. we can never see concurrent |
| 4884 | * manipulation of individual bits. But since the bitfield as a whole |
| 4885 | * is updated using RMW, we need to use atomics in order to update |
| 4886 | * the bits. |
| 4887 | */ |
| 4888 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 4889 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 4890 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4891 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 4892 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 4893 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 4897 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 4898 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4899 | } |
| 4900 | } |
| 4901 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4902 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 4903 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4904 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4905 | const void *data, size_t size) |
| 4906 | { |
| 4907 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4908 | struct file *file; |
| 4909 | size_t offset; |
| 4910 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4911 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4912 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4913 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4914 | return obj; |
| 4915 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4916 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4917 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4918 | file = obj->base.filp; |
| 4919 | offset = 0; |
| 4920 | do { |
| 4921 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 4922 | struct page *page; |
| 4923 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4924 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4925 | err = pagecache_write_begin(file, file->f_mapping, |
| 4926 | offset, len, 0, |
| 4927 | &page, &pgdata); |
| 4928 | if (err < 0) |
| 4929 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4930 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4931 | vaddr = kmap(page); |
| 4932 | memcpy(vaddr, data, len); |
| 4933 | kunmap(page); |
| 4934 | |
| 4935 | err = pagecache_write_end(file, file->f_mapping, |
| 4936 | offset, len, len, |
| 4937 | page, pgdata); |
| 4938 | if (err < 0) |
| 4939 | goto fail; |
| 4940 | |
| 4941 | size -= len; |
| 4942 | data += len; |
| 4943 | offset += len; |
| 4944 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4945 | |
| 4946 | return obj; |
| 4947 | |
| 4948 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4949 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4950 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4951 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 4952 | |
| 4953 | struct scatterlist * |
| 4954 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 4955 | unsigned int n, |
| 4956 | unsigned int *offset) |
| 4957 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4958 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 4959 | struct scatterlist *sg; |
| 4960 | unsigned int idx, count; |
| 4961 | |
| 4962 | might_sleep(); |
| 4963 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4964 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 4965 | |
| 4966 | /* As we iterate forward through the sg, we record each entry in a |
| 4967 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 4968 | * this index previously, we will have an entry for it. |
| 4969 | * |
| 4970 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 4971 | * sequential page access (where each new request is consecutive |
| 4972 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 4973 | * i.e. O(1) with a large constant! |
| 4974 | */ |
| 4975 | if (n < READ_ONCE(iter->sg_idx)) |
| 4976 | goto lookup; |
| 4977 | |
| 4978 | mutex_lock(&iter->lock); |
| 4979 | |
| 4980 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 4981 | * (or the subsequent) sg are fast - comparing against the last |
| 4982 | * sg is faster than going through the radixtree. |
| 4983 | */ |
| 4984 | |
| 4985 | sg = iter->sg_pos; |
| 4986 | idx = iter->sg_idx; |
| 4987 | count = __sg_page_count(sg); |
| 4988 | |
| 4989 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 4990 | void *entry; |
| 4991 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 4992 | int ret; |
| 4993 | |
| 4994 | /* If we cannot allocate and insert this entry, or the |
| 4995 | * individual pages from this range, cancel updating the |
| 4996 | * sg_idx so that on this lookup we are forced to linearly |
| 4997 | * scan onwards, but on future lookups we will try the |
| 4998 | * insertion again (in which case we need to be careful of |
| 4999 | * the error return reporting that we have already inserted |
| 5000 | * this index). |
| 5001 | */ |
| 5002 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5003 | if (ret && ret != -EEXIST) |
| 5004 | goto scan; |
| 5005 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5006 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5007 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5008 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5009 | if (ret && ret != -EEXIST) |
| 5010 | goto scan; |
| 5011 | } |
| 5012 | |
| 5013 | idx += count; |
| 5014 | sg = ____sg_next(sg); |
| 5015 | count = __sg_page_count(sg); |
| 5016 | } |
| 5017 | |
| 5018 | scan: |
| 5019 | iter->sg_pos = sg; |
| 5020 | iter->sg_idx = idx; |
| 5021 | |
| 5022 | mutex_unlock(&iter->lock); |
| 5023 | |
| 5024 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5025 | goto lookup; |
| 5026 | |
| 5027 | /* In case we failed to insert the entry into the radixtree, we need |
| 5028 | * to look beyond the current sg. |
| 5029 | */ |
| 5030 | while (idx + count <= n) { |
| 5031 | idx += count; |
| 5032 | sg = ____sg_next(sg); |
| 5033 | count = __sg_page_count(sg); |
| 5034 | } |
| 5035 | |
| 5036 | *offset = n - idx; |
| 5037 | return sg; |
| 5038 | |
| 5039 | lookup: |
| 5040 | rcu_read_lock(); |
| 5041 | |
| 5042 | sg = radix_tree_lookup(&iter->radix, n); |
| 5043 | GEM_BUG_ON(!sg); |
| 5044 | |
| 5045 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5046 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5047 | * to the start of that range. We will return the pointer to |
| 5048 | * the base page and the offset of this page within the |
| 5049 | * sg entry's range. |
| 5050 | */ |
| 5051 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5052 | if (unlikely(xa_is_value(sg))) { |
| 5053 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5054 | |
| 5055 | sg = radix_tree_lookup(&iter->radix, base); |
| 5056 | GEM_BUG_ON(!sg); |
| 5057 | |
| 5058 | *offset = n - base; |
| 5059 | } |
| 5060 | |
| 5061 | rcu_read_unlock(); |
| 5062 | |
| 5063 | return sg; |
| 5064 | } |
| 5065 | |
| 5066 | struct page * |
| 5067 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5068 | { |
| 5069 | struct scatterlist *sg; |
| 5070 | unsigned int offset; |
| 5071 | |
| 5072 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5073 | |
| 5074 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5075 | return nth_page(sg_page(sg), offset); |
| 5076 | } |
| 5077 | |
| 5078 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5079 | struct page * |
| 5080 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5081 | unsigned int n) |
| 5082 | { |
| 5083 | struct page *page; |
| 5084 | |
| 5085 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5086 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5087 | set_page_dirty(page); |
| 5088 | |
| 5089 | return page; |
| 5090 | } |
| 5091 | |
| 5092 | dma_addr_t |
| 5093 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5094 | unsigned long n) |
| 5095 | { |
| 5096 | struct scatterlist *sg; |
| 5097 | unsigned int offset; |
| 5098 | |
| 5099 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5100 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5101 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5102 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5103 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5104 | { |
| 5105 | struct sg_table *pages; |
| 5106 | int err; |
| 5107 | |
| 5108 | if (align > obj->base.size) |
| 5109 | return -EINVAL; |
| 5110 | |
| 5111 | if (obj->ops == &i915_gem_phys_ops) |
| 5112 | return 0; |
| 5113 | |
| 5114 | if (obj->ops != &i915_gem_object_ops) |
| 5115 | return -EINVAL; |
| 5116 | |
| 5117 | err = i915_gem_object_unbind(obj); |
| 5118 | if (err) |
| 5119 | return err; |
| 5120 | |
| 5121 | mutex_lock(&obj->mm.lock); |
| 5122 | |
| 5123 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5124 | err = -EFAULT; |
| 5125 | goto err_unlock; |
| 5126 | } |
| 5127 | |
| 5128 | if (obj->mm.quirked) { |
| 5129 | err = -EFAULT; |
| 5130 | goto err_unlock; |
| 5131 | } |
| 5132 | |
| 5133 | if (obj->mm.mapping) { |
| 5134 | err = -EBUSY; |
| 5135 | goto err_unlock; |
| 5136 | } |
| 5137 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5138 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5139 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5140 | obj->ops = &i915_gem_phys_ops; |
| 5141 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5142 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5143 | if (err) |
| 5144 | goto err_xfer; |
| 5145 | |
| 5146 | /* Perma-pin (until release) the physical set of pages */ |
| 5147 | __i915_gem_object_pin_pages(obj); |
| 5148 | |
| 5149 | if (!IS_ERR_OR_NULL(pages)) |
| 5150 | i915_gem_object_ops.put_pages(obj, pages); |
| 5151 | mutex_unlock(&obj->mm.lock); |
| 5152 | return 0; |
| 5153 | |
| 5154 | err_xfer: |
| 5155 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5156 | if (!IS_ERR_OR_NULL(pages)) { |
| 5157 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 5158 | |
| 5159 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 5160 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5161 | err_unlock: |
| 5162 | mutex_unlock(&obj->mm.lock); |
| 5163 | return err; |
| 5164 | } |
| 5165 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5166 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5167 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5168 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5169 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5170 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5171 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5172 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 5173 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5174 | #endif |