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 | |
Ville Syrjälä | aa5ca8b | 2019-05-09 15:21:57 +0300 | [diff] [blame] | 55 | #include "intel_display.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 56 | #include "intel_drv.h" |
| 57 | #include "intel_frontbuffer.h" |
Jani Nikula | 696173b | 2019-04-05 14:00:15 +0300 | [diff] [blame] | 58 | #include "intel_pm.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 59 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 60 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 61 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 62 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 63 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 64 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 65 | return false; |
| 66 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 67 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 68 | return true; |
| 69 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 70 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 73 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 74 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 75 | struct drm_mm_node *node, u32 size) |
| 76 | { |
| 77 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 78 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 79 | size, 0, I915_COLOR_UNEVICTABLE, |
| 80 | 0, ggtt->mappable_end, |
| 81 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void |
| 85 | remove_mappable_node(struct drm_mm_node *node) |
| 86 | { |
| 87 | drm_mm_remove_node(node); |
| 88 | } |
| 89 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 90 | /* some bookkeeping */ |
| 91 | 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] | 92 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 93 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 94 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 95 | dev_priv->mm.object_count++; |
| 96 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 97 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | 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] | 101 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 102 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 103 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 104 | dev_priv->mm.object_count--; |
| 105 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 106 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 109 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 110 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 111 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 112 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 113 | struct i915_ggtt *ggtt = &to_i915(dev)->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 114 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 115 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 116 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 117 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 118 | mutex_lock(&ggtt->vm.mutex); |
| 119 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 120 | pinned = ggtt->vm.reserved; |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 121 | list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 122 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 123 | pinned += vma->node.size; |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 124 | |
| 125 | mutex_unlock(&ggtt->vm.mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 126 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 127 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 128 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 129 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 133 | 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] | 134 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 135 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 136 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 137 | struct sg_table *st; |
| 138 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 139 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 140 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 141 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 142 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 143 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 144 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 145 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 146 | /* Always aligning to the object size, allows a single allocation |
| 147 | * to handle all possible callers, and given typical object sizes, |
| 148 | * the alignment of the buddy allocation will naturally match. |
| 149 | */ |
| 150 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 151 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 152 | roundup_pow_of_two(obj->base.size)); |
| 153 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 154 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 155 | |
| 156 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 157 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 158 | struct page *page; |
| 159 | char *src; |
| 160 | |
| 161 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 162 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 163 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 164 | goto err_phys; |
| 165 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 166 | |
| 167 | src = kmap_atomic(page); |
| 168 | memcpy(vaddr, src, PAGE_SIZE); |
| 169 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 170 | kunmap_atomic(src); |
| 171 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 172 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 173 | vaddr += PAGE_SIZE; |
| 174 | } |
| 175 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 176 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 177 | |
| 178 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 179 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 180 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 181 | goto err_phys; |
| 182 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 183 | |
| 184 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 185 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 186 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 187 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | sg = st->sgl; |
| 191 | sg->offset = 0; |
| 192 | sg->length = obj->base.size; |
| 193 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 194 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 195 | sg_dma_len(sg) = obj->base.size; |
| 196 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 197 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 198 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 199 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 200 | |
| 201 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 202 | |
| 203 | err_phys: |
| 204 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 205 | |
| 206 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 209 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 210 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 211 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 212 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 213 | if (cpu_write_needs_clflush(obj)) |
| 214 | obj->cache_dirty = true; |
| 215 | } |
| 216 | |
Chris Wilson | ee8efa8 | 2019-03-31 10:46:20 +0100 | [diff] [blame] | 217 | void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 218 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 219 | struct sg_table *pages, |
| 220 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 221 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 222 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 223 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 224 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 225 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 226 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 227 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 228 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 229 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 230 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 231 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 232 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static void |
| 236 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 237 | struct sg_table *pages) |
| 238 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 239 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 240 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 241 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 242 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 243 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 244 | int i; |
| 245 | |
| 246 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 247 | struct page *page; |
| 248 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 249 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 250 | page = shmem_read_mapping_page(mapping, i); |
| 251 | if (IS_ERR(page)) |
| 252 | continue; |
| 253 | |
| 254 | dst = kmap_atomic(page); |
| 255 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 256 | memcpy(dst, vaddr, PAGE_SIZE); |
| 257 | kunmap_atomic(dst); |
| 258 | |
| 259 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 260 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 261 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 262 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 263 | vaddr += PAGE_SIZE; |
| 264 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 265 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 268 | sg_free_table(pages); |
| 269 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 270 | |
| 271 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static void |
| 275 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 276 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 277 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 281 | .get_pages = i915_gem_object_get_pages_phys, |
| 282 | .put_pages = i915_gem_object_put_pages_phys, |
| 283 | .release = i915_gem_object_release_phys, |
| 284 | }; |
| 285 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 286 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 287 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 288 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 289 | { |
| 290 | struct i915_vma *vma; |
| 291 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 292 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 293 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 294 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 295 | |
| 296 | /* Closed vma are removed from the obj->vma_list - but they may |
| 297 | * still have an active binding on the object. To remove those we |
| 298 | * must wait for all rendering to complete to the object (as unbinding |
| 299 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 300 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 301 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 302 | if (ret) |
| 303 | return ret; |
| 304 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 305 | spin_lock(&obj->vma.lock); |
| 306 | while (!ret && (vma = list_first_entry_or_null(&obj->vma.list, |
| 307 | struct i915_vma, |
| 308 | obj_link))) { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 309 | list_move_tail(&vma->obj_link, &still_in_list); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 310 | spin_unlock(&obj->vma.lock); |
| 311 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 312 | ret = i915_vma_unbind(vma); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 313 | |
| 314 | spin_lock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 315 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 316 | list_splice(&still_in_list, &obj->vma.list); |
| 317 | spin_unlock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 322 | static long |
| 323 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 324 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 325 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 326 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 327 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 328 | |
| 329 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 330 | |
| 331 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 332 | return timeout; |
| 333 | |
| 334 | if (!dma_fence_is_i915(fence)) |
| 335 | return dma_fence_wait_timeout(fence, |
| 336 | flags & I915_WAIT_INTERRUPTIBLE, |
| 337 | timeout); |
| 338 | |
| 339 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 340 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 341 | goto out; |
| 342 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 343 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 344 | |
| 345 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 346 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 347 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 348 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 349 | return timeout; |
| 350 | } |
| 351 | |
| 352 | static long |
| 353 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 354 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 355 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 356 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 357 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 358 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 359 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 360 | |
| 361 | if (flags & I915_WAIT_ALL) { |
| 362 | struct dma_fence **shared; |
| 363 | unsigned int count, i; |
| 364 | int ret; |
| 365 | |
| 366 | ret = reservation_object_get_fences_rcu(resv, |
| 367 | &excl, &count, &shared); |
| 368 | if (ret) |
| 369 | return ret; |
| 370 | |
| 371 | for (i = 0; i < count; i++) { |
| 372 | timeout = i915_gem_object_wait_fence(shared[i], |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 373 | flags, timeout); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 374 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 375 | break; |
| 376 | |
| 377 | dma_fence_put(shared[i]); |
| 378 | } |
| 379 | |
| 380 | for (; i < count; i++) |
| 381 | dma_fence_put(shared[i]); |
| 382 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 383 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 384 | /* |
| 385 | * If both shared fences and an exclusive fence exist, |
| 386 | * then by construction the shared fences must be later |
| 387 | * than the exclusive fence. If we successfully wait for |
| 388 | * all the shared fences, we know that the exclusive fence |
| 389 | * must all be signaled. If all the shared fences are |
| 390 | * signaled, we can prune the array and recover the |
| 391 | * floating references on the fences/requests. |
| 392 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 393 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 394 | } else { |
| 395 | excl = reservation_object_get_excl_rcu(resv); |
| 396 | } |
| 397 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 398 | if (excl && timeout >= 0) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 399 | timeout = i915_gem_object_wait_fence(excl, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 400 | |
| 401 | dma_fence_put(excl); |
| 402 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 403 | /* |
| 404 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 405 | * signaled and that the reservation object has not been changed (i.e. |
| 406 | * no new fences have been added). |
| 407 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 408 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 409 | if (reservation_object_trylock(resv)) { |
| 410 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 411 | reservation_object_add_excl_fence(resv, NULL); |
| 412 | reservation_object_unlock(resv); |
| 413 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 416 | return timeout; |
| 417 | } |
| 418 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 419 | static void __fence_set_priority(struct dma_fence *fence, |
| 420 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 421 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 422 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 423 | struct intel_engine_cs *engine; |
| 424 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 425 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 426 | return; |
| 427 | |
| 428 | rq = to_request(fence); |
| 429 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 430 | |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 431 | local_bh_disable(); |
| 432 | rcu_read_lock(); /* RCU serialisation for set-wedged protection */ |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 433 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 434 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 435 | rcu_read_unlock(); |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 436 | local_bh_enable(); /* kick the tasklets if queues were reprioritised */ |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 439 | static void fence_set_priority(struct dma_fence *fence, |
| 440 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 441 | { |
| 442 | /* Recurse once into a fence-array */ |
| 443 | if (dma_fence_is_array(fence)) { |
| 444 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 445 | int i; |
| 446 | |
| 447 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 448 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 449 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 450 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | int |
| 455 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 456 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 457 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 458 | { |
| 459 | struct dma_fence *excl; |
| 460 | |
| 461 | if (flags & I915_WAIT_ALL) { |
| 462 | struct dma_fence **shared; |
| 463 | unsigned int count, i; |
| 464 | int ret; |
| 465 | |
| 466 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 467 | &excl, &count, &shared); |
| 468 | if (ret) |
| 469 | return ret; |
| 470 | |
| 471 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 472 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 473 | dma_fence_put(shared[i]); |
| 474 | } |
| 475 | |
| 476 | kfree(shared); |
| 477 | } else { |
| 478 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 479 | } |
| 480 | |
| 481 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 482 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 483 | dma_fence_put(excl); |
| 484 | } |
| 485 | return 0; |
| 486 | } |
| 487 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 488 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 489 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 490 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 491 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 492 | * @timeout: how long to wait |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 493 | */ |
| 494 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 495 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 496 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 497 | long timeout) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 498 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 499 | might_sleep(); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 500 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 501 | |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 502 | timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 503 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 506 | static int |
| 507 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 508 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 509 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 510 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 511 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 512 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 513 | |
| 514 | /* We manually control the domain here and pretend that it |
| 515 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 516 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 517 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 518 | if (copy_from_user(vaddr, user_data, args->size)) |
| 519 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 520 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 521 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 522 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 523 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 524 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 525 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 528 | static int |
| 529 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 530 | struct drm_i915_private *dev_priv, |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 531 | u64 *size_p, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 532 | u32 *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 533 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 534 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 535 | u32 handle; |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 536 | u64 size; |
| 537 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 538 | |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 539 | size = round_up(*size_p, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 540 | if (size == 0) |
| 541 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 542 | |
| 543 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 544 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 545 | if (IS_ERR(obj)) |
| 546 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 547 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 548 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 549 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 550 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 551 | if (ret) |
| 552 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 553 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 554 | *handle_p = handle; |
Chris Wilson | 9953402 | 2019-04-17 14:25:07 +0100 | [diff] [blame] | 555 | *size_p = size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 559 | int |
| 560 | i915_gem_dumb_create(struct drm_file *file, |
| 561 | struct drm_device *dev, |
| 562 | struct drm_mode_create_dumb *args) |
| 563 | { |
Ville Syrjälä | aa5ca8b | 2019-05-09 15:21:57 +0300 | [diff] [blame] | 564 | int cpp = DIV_ROUND_UP(args->bpp, 8); |
| 565 | u32 format; |
| 566 | |
| 567 | switch (cpp) { |
| 568 | case 1: |
| 569 | format = DRM_FORMAT_C8; |
| 570 | break; |
| 571 | case 2: |
| 572 | format = DRM_FORMAT_RGB565; |
| 573 | break; |
| 574 | case 4: |
| 575 | format = DRM_FORMAT_XRGB8888; |
| 576 | break; |
| 577 | default: |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 581 | /* have to work out size/pitch and return them */ |
Ville Syrjälä | aa5ca8b | 2019-05-09 15:21:57 +0300 | [diff] [blame] | 582 | args->pitch = ALIGN(args->width * cpp, 64); |
| 583 | |
| 584 | /* align stride to page size so that we can remap */ |
| 585 | if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format, |
| 586 | DRM_FORMAT_MOD_LINEAR)) |
| 587 | args->pitch = ALIGN(args->pitch, 4096); |
| 588 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 589 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 590 | return i915_gem_create(file, to_i915(dev), |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 591 | &args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 592 | } |
| 593 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 594 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 595 | { |
| 596 | return !(obj->cache_level == I915_CACHE_NONE || |
| 597 | obj->cache_level == I915_CACHE_WT); |
| 598 | } |
| 599 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 600 | /** |
| 601 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 602 | * @dev: drm device pointer |
| 603 | * @data: ioctl data blob |
| 604 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 605 | */ |
| 606 | int |
| 607 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 608 | struct drm_file *file) |
| 609 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 610 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 611 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 612 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 613 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 614 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 615 | return i915_gem_create(file, dev_priv, |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 616 | &args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 617 | } |
| 618 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 619 | static inline enum fb_op_origin |
| 620 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 621 | { |
| 622 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 623 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 624 | } |
| 625 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 626 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 627 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 628 | intel_wakeref_t wakeref; |
| 629 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 630 | /* |
| 631 | * No actual flushing is required for the GTT write domain for reads |
| 632 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 633 | * as far as we know, so there's no chipset flush. It also doesn't |
| 634 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 635 | * |
| 636 | * However, we do have to enforce the order so that all writes through |
| 637 | * the GTT land before any writes to the device, such as updates to |
| 638 | * the GATT itself. |
| 639 | * |
| 640 | * We also have to wait a bit for the writes to land from the GTT. |
| 641 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 642 | * timing. This issue has only been observed when switching quickly |
| 643 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 644 | * 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] | 645 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 646 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 647 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 648 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 649 | wmb(); |
| 650 | |
| 651 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 652 | return; |
| 653 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 654 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 655 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 656 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 657 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 658 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 659 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 660 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 661 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 662 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | static void |
| 666 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 667 | { |
| 668 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 669 | struct i915_vma *vma; |
| 670 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 671 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 672 | return; |
| 673 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 674 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 675 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 676 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 677 | |
| 678 | intel_fb_obj_flush(obj, |
| 679 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 680 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 681 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 682 | if (vma->iomap) |
| 683 | continue; |
| 684 | |
| 685 | i915_vma_unset_ggtt_write(vma); |
| 686 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 687 | break; |
| 688 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 689 | case I915_GEM_DOMAIN_WC: |
| 690 | wmb(); |
| 691 | break; |
| 692 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 693 | case I915_GEM_DOMAIN_CPU: |
| 694 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 695 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 696 | |
| 697 | case I915_GEM_DOMAIN_RENDER: |
| 698 | if (gpu_write_needs_clflush(obj)) |
| 699 | obj->cache_dirty = true; |
| 700 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 703 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 706 | /* |
| 707 | * Pins the specified object's pages and synchronizes the object with |
| 708 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 709 | * flush the object from the CPU cache. |
| 710 | */ |
| 711 | 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] | 712 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 713 | { |
| 714 | int ret; |
| 715 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 716 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 717 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 718 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 719 | if (!i915_gem_object_has_struct_page(obj)) |
| 720 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 721 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 722 | ret = i915_gem_object_wait(obj, |
| 723 | I915_WAIT_INTERRUPTIBLE | |
| 724 | I915_WAIT_LOCKED, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 725 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 726 | if (ret) |
| 727 | return ret; |
| 728 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 729 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 730 | if (ret) |
| 731 | return ret; |
| 732 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 733 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 734 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 735 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 736 | if (ret) |
| 737 | goto err_unpin; |
| 738 | else |
| 739 | goto out; |
| 740 | } |
| 741 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 742 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 743 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 744 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 745 | * read domain and manually flush cachelines (if required). This |
| 746 | * optimizes for the case when the gpu will dirty the data |
| 747 | * anyway again before the next pread happens. |
| 748 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 749 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 750 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 751 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 752 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 753 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 754 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 755 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 756 | |
| 757 | err_unpin: |
| 758 | i915_gem_object_unpin_pages(obj); |
| 759 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 763 | unsigned int *needs_clflush) |
| 764 | { |
| 765 | int ret; |
| 766 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 767 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 768 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 769 | *needs_clflush = 0; |
| 770 | if (!i915_gem_object_has_struct_page(obj)) |
| 771 | return -ENODEV; |
| 772 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 773 | ret = i915_gem_object_wait(obj, |
| 774 | I915_WAIT_INTERRUPTIBLE | |
| 775 | I915_WAIT_LOCKED | |
| 776 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 777 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 778 | if (ret) |
| 779 | return ret; |
| 780 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 781 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 782 | if (ret) |
| 783 | return ret; |
| 784 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 785 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 786 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 787 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 788 | if (ret) |
| 789 | goto err_unpin; |
| 790 | else |
| 791 | goto out; |
| 792 | } |
| 793 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 794 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 795 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 796 | /* If we're not in the cpu write domain, set ourself into the |
| 797 | * gtt write domain and manually flush cachelines (as required). |
| 798 | * This optimizes for the case when the gpu will use the data |
| 799 | * right away and we therefore have to clflush anyway. |
| 800 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 801 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 802 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 803 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 804 | /* |
| 805 | * Same trick applies to invalidate partially written |
| 806 | * cachelines read before writing. |
| 807 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 808 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 809 | *needs_clflush |= CLFLUSH_BEFORE; |
| 810 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 811 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 812 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 813 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 814 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 815 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 816 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 817 | |
| 818 | err_unpin: |
| 819 | i915_gem_object_unpin_pages(obj); |
| 820 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 821 | } |
| 822 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 823 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 824 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 825 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 826 | { |
| 827 | char *vaddr; |
| 828 | int ret; |
| 829 | |
| 830 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 831 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 832 | if (needs_clflush) |
| 833 | drm_clflush_virt_range(vaddr + offset, len); |
| 834 | |
| 835 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 836 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 837 | kunmap(page); |
| 838 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 839 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static int |
| 843 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 844 | struct drm_i915_gem_pread *args) |
| 845 | { |
| 846 | char __user *user_data; |
| 847 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 848 | unsigned int needs_clflush; |
| 849 | unsigned int idx, offset; |
| 850 | int ret; |
| 851 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 852 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 853 | if (ret) |
| 854 | return ret; |
| 855 | |
| 856 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 857 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 858 | if (ret) |
| 859 | return ret; |
| 860 | |
| 861 | remain = args->size; |
| 862 | user_data = u64_to_user_ptr(args->data_ptr); |
| 863 | offset = offset_in_page(args->offset); |
| 864 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 865 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 866 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 867 | |
| 868 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 869 | needs_clflush); |
| 870 | if (ret) |
| 871 | break; |
| 872 | |
| 873 | remain -= length; |
| 874 | user_data += length; |
| 875 | offset = 0; |
| 876 | } |
| 877 | |
| 878 | i915_gem_obj_finish_shmem_access(obj); |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | static inline bool |
| 883 | gtt_user_read(struct io_mapping *mapping, |
| 884 | loff_t base, int offset, |
| 885 | char __user *user_data, int length) |
| 886 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 887 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 888 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 889 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 890 | /* 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] | 891 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 892 | unwritten = __copy_to_user_inatomic(user_data, |
| 893 | (void __force *)vaddr + offset, |
| 894 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 895 | io_mapping_unmap_atomic(vaddr); |
| 896 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 897 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 898 | unwritten = copy_to_user(user_data, |
| 899 | (void __force *)vaddr + offset, |
| 900 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 901 | io_mapping_unmap(vaddr); |
| 902 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 903 | return unwritten; |
| 904 | } |
| 905 | |
| 906 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 907 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 908 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 909 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 910 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 911 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 912 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 913 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 914 | struct i915_vma *vma; |
| 915 | void __user *user_data; |
| 916 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 917 | int ret; |
| 918 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 919 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 920 | if (ret) |
| 921 | return ret; |
| 922 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 923 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 924 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 925 | PIN_MAPPABLE | |
| 926 | PIN_NONFAULT | |
| 927 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 928 | if (!IS_ERR(vma)) { |
| 929 | node.start = i915_ggtt_offset(vma); |
| 930 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 931 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 932 | if (ret) { |
| 933 | i915_vma_unpin(vma); |
| 934 | vma = ERR_PTR(ret); |
| 935 | } |
| 936 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 937 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 938 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 939 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 940 | goto out_unlock; |
| 941 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 945 | if (ret) |
| 946 | goto out_unpin; |
| 947 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 948 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 949 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 950 | user_data = u64_to_user_ptr(args->data_ptr); |
| 951 | remain = args->size; |
| 952 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 953 | |
| 954 | while (remain > 0) { |
| 955 | /* Operation in this page |
| 956 | * |
| 957 | * page_base = page offset within aperture |
| 958 | * page_offset = offset within page |
| 959 | * page_length = bytes to copy for this page |
| 960 | */ |
| 961 | u32 page_base = node.start; |
| 962 | unsigned page_offset = offset_in_page(offset); |
| 963 | unsigned page_length = PAGE_SIZE - page_offset; |
| 964 | page_length = remain < page_length ? remain : page_length; |
| 965 | if (node.allocated) { |
| 966 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 967 | ggtt->vm.insert_page(&ggtt->vm, |
| 968 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 969 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 970 | wmb(); |
| 971 | } else { |
| 972 | page_base += offset & PAGE_MASK; |
| 973 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 974 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 975 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 976 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 977 | ret = -EFAULT; |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | remain -= page_length; |
| 982 | user_data += page_length; |
| 983 | offset += page_length; |
| 984 | } |
| 985 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 986 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 987 | out_unpin: |
| 988 | if (node.allocated) { |
| 989 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 990 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 991 | remove_mappable_node(&node); |
| 992 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 993 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 994 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 995 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 996 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 997 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 998 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 999 | return ret; |
| 1000 | } |
| 1001 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1002 | /** |
| 1003 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1004 | * @dev: drm device pointer |
| 1005 | * @data: ioctl data blob |
| 1006 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1007 | * |
| 1008 | * On error, the contents of *data are undefined. |
| 1009 | */ |
| 1010 | int |
| 1011 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1012 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1013 | { |
| 1014 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1015 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1016 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1017 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1018 | if (args->size == 0) |
| 1019 | return 0; |
| 1020 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1021 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1022 | args->size)) |
| 1023 | return -EFAULT; |
| 1024 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1025 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1026 | if (!obj) |
| 1027 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1028 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1029 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1030 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1031 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1032 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1035 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1036 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1037 | ret = i915_gem_object_wait(obj, |
| 1038 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1039 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1040 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1041 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1042 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1043 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1044 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1045 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1046 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1047 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1048 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1049 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1050 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1051 | i915_gem_object_unpin_pages(obj); |
| 1052 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1053 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1054 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1057 | /* This is the fast write path which cannot handle |
| 1058 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1059 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1060 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1061 | static inline bool |
| 1062 | ggtt_write(struct io_mapping *mapping, |
| 1063 | loff_t base, int offset, |
| 1064 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1065 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1066 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1067 | unsigned long unwritten; |
| 1068 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1069 | /* 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] | 1070 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1071 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1072 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1073 | io_mapping_unmap_atomic(vaddr); |
| 1074 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1075 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1076 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1077 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1078 | io_mapping_unmap(vaddr); |
| 1079 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1080 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1081 | return unwritten; |
| 1082 | } |
| 1083 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1084 | /** |
| 1085 | * This is the fast pwrite path, where we copy the data directly from the |
| 1086 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1087 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1088 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1089 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1090 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1091 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1092 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1093 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1094 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1095 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1096 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1097 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1098 | struct i915_vma *vma; |
| 1099 | u64 remain, offset; |
| 1100 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1101 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1102 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1103 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1104 | if (ret) |
| 1105 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1106 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1107 | if (i915_gem_object_has_struct_page(obj)) { |
| 1108 | /* |
| 1109 | * Avoid waking the device up if we can fallback, as |
| 1110 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1111 | * depending on PCI sleeps and our own resume time). |
| 1112 | * This easily dwarfs any performance advantage from |
| 1113 | * using the cache bypass of indirect GGTT access. |
| 1114 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1115 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1116 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1117 | ret = -EFAULT; |
| 1118 | goto out_unlock; |
| 1119 | } |
| 1120 | } else { |
| 1121 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1122 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1125 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1126 | PIN_MAPPABLE | |
| 1127 | PIN_NONFAULT | |
| 1128 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1129 | if (!IS_ERR(vma)) { |
| 1130 | node.start = i915_ggtt_offset(vma); |
| 1131 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1132 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1133 | if (ret) { |
| 1134 | i915_vma_unpin(vma); |
| 1135 | vma = ERR_PTR(ret); |
| 1136 | } |
| 1137 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1138 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1139 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1140 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1141 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1142 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1143 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1144 | |
| 1145 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1146 | if (ret) |
| 1147 | goto out_unpin; |
| 1148 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1149 | mutex_unlock(&i915->drm.struct_mutex); |
| 1150 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1151 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1152 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1153 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1154 | offset = args->offset; |
| 1155 | remain = args->size; |
| 1156 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1157 | /* Operation in this page |
| 1158 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1159 | * page_base = page offset within aperture |
| 1160 | * page_offset = offset within page |
| 1161 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1162 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1163 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1164 | unsigned int page_offset = offset_in_page(offset); |
| 1165 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1166 | page_length = remain < page_length ? remain : page_length; |
| 1167 | if (node.allocated) { |
| 1168 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1169 | ggtt->vm.insert_page(&ggtt->vm, |
| 1170 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1171 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1172 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1173 | } else { |
| 1174 | page_base += offset & PAGE_MASK; |
| 1175 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1176 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1177 | * source page isn't available. Return the error and we'll |
| 1178 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1179 | * If the object is non-shmem backed, we retry again with the |
| 1180 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1181 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1182 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1183 | user_data, page_length)) { |
| 1184 | ret = -EFAULT; |
| 1185 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1186 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1187 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1188 | remain -= page_length; |
| 1189 | user_data += page_length; |
| 1190 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1191 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1192 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1193 | |
| 1194 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1195 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1196 | if (node.allocated) { |
| 1197 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1198 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1199 | remove_mappable_node(&node); |
| 1200 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1201 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1202 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1203 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1204 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1205 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1206 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1207 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1210 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1211 | * Flushes invalid cachelines before writing to the target if |
| 1212 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1213 | * writing if needs_clflush is set. |
| 1214 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1215 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1216 | 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] | 1217 | bool needs_clflush_before, |
| 1218 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1219 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1220 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1221 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1222 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1223 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1224 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1225 | if (needs_clflush_before) |
| 1226 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1227 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1228 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1229 | if (!ret && needs_clflush_after) |
| 1230 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1231 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1232 | kunmap(page); |
| 1233 | |
| 1234 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | static int |
| 1238 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1239 | const struct drm_i915_gem_pwrite *args) |
| 1240 | { |
| 1241 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1242 | void __user *user_data; |
| 1243 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1244 | unsigned int partial_cacheline_write; |
| 1245 | unsigned int needs_clflush; |
| 1246 | unsigned int offset, idx; |
| 1247 | int ret; |
| 1248 | |
| 1249 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1250 | if (ret) |
| 1251 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1252 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1253 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1254 | mutex_unlock(&i915->drm.struct_mutex); |
| 1255 | if (ret) |
| 1256 | return ret; |
| 1257 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1258 | /* If we don't overwrite a cacheline completely we need to be |
| 1259 | * careful to have up-to-date data by first clflushing. Don't |
| 1260 | * overcomplicate things and flush the entire patch. |
| 1261 | */ |
| 1262 | partial_cacheline_write = 0; |
| 1263 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1264 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1265 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1266 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1267 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1268 | offset = offset_in_page(args->offset); |
| 1269 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1270 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1271 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1272 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1273 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1274 | (offset | length) & partial_cacheline_write, |
| 1275 | needs_clflush & CLFLUSH_AFTER); |
| 1276 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1277 | break; |
| 1278 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1279 | remain -= length; |
| 1280 | user_data += length; |
| 1281 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1284 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1285 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1286 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /** |
| 1290 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1291 | * @dev: drm device |
| 1292 | * @data: ioctl data blob |
| 1293 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1294 | * |
| 1295 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1296 | */ |
| 1297 | int |
| 1298 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1299 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1300 | { |
| 1301 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1302 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1303 | int ret; |
| 1304 | |
| 1305 | if (args->size == 0) |
| 1306 | return 0; |
| 1307 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1308 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1309 | return -EFAULT; |
| 1310 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1311 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1312 | if (!obj) |
| 1313 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1314 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1315 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1316 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1317 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1318 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1321 | /* Writes not allowed into this read-only object */ |
| 1322 | if (i915_gem_object_is_readonly(obj)) { |
| 1323 | ret = -EINVAL; |
| 1324 | goto err; |
| 1325 | } |
| 1326 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1327 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1328 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1329 | ret = -ENODEV; |
| 1330 | if (obj->ops->pwrite) |
| 1331 | ret = obj->ops->pwrite(obj, args); |
| 1332 | if (ret != -ENODEV) |
| 1333 | goto err; |
| 1334 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1335 | ret = i915_gem_object_wait(obj, |
| 1336 | I915_WAIT_INTERRUPTIBLE | |
| 1337 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1338 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1339 | if (ret) |
| 1340 | goto err; |
| 1341 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1342 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1343 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1344 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1345 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1346 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1347 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1348 | * it would end up going through the fenced access, and we'll get |
| 1349 | * different detiling behavior between reading and writing. |
| 1350 | * pread/pwrite currently are reading and writing from the CPU |
| 1351 | * perspective, requiring manual detiling by the client. |
| 1352 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1353 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1354 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1355 | /* Note that the gtt paths might fail with non-page-backed user |
| 1356 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1357 | * textures). Fallback to the shmem path in that case. |
| 1358 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1359 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1360 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1361 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1362 | if (obj->phys_handle) |
| 1363 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1364 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1365 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1366 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1367 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1368 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1369 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1370 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1371 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1374 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1375 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1376 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1377 | struct list_head *list; |
| 1378 | struct i915_vma *vma; |
| 1379 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1380 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1381 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1382 | mutex_lock(&i915->ggtt.vm.mutex); |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1383 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1384 | if (!drm_mm_node_allocated(&vma->node)) |
| 1385 | continue; |
| 1386 | |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 1387 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1388 | } |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1389 | mutex_unlock(&i915->ggtt.vm.mutex); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1390 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1391 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1392 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1393 | list_move_tail(&obj->mm.link, list); |
| 1394 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1397 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1398 | * Called when user space prepares to use an object with the CPU, either |
| 1399 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1400 | * @dev: drm device |
| 1401 | * @data: ioctl data blob |
| 1402 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1403 | */ |
| 1404 | int |
| 1405 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1406 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1407 | { |
| 1408 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1409 | struct drm_i915_gem_object *obj; |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1410 | u32 read_domains = args->read_domains; |
| 1411 | u32 write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1412 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1413 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1414 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1415 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1416 | return -EINVAL; |
| 1417 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1418 | /* |
| 1419 | * Having something in the write domain implies it's in the read |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1420 | * domain, and only that read domain. Enforce that in the request. |
| 1421 | */ |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1422 | if (write_domain && read_domains != write_domain) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1423 | return -EINVAL; |
| 1424 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1425 | if (!read_domains) |
| 1426 | return 0; |
| 1427 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1428 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1429 | if (!obj) |
| 1430 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1431 | |
Chris Wilson | 754a254 | 2019-03-21 16:19:08 +0000 | [diff] [blame] | 1432 | /* |
| 1433 | * Already in the desired write domain? Nothing for us to do! |
| 1434 | * |
| 1435 | * We apply a little bit of cunning here to catch a broader set of |
| 1436 | * no-ops. If obj->write_domain is set, we must be in the same |
| 1437 | * obj->read_domains, and only that domain. Therefore, if that |
| 1438 | * obj->write_domain matches the request read_domains, we are |
| 1439 | * already in the same read/write domain and can skip the operation, |
| 1440 | * without having to further check the requested write_domain. |
| 1441 | */ |
| 1442 | if (READ_ONCE(obj->write_domain) == read_domains) { |
| 1443 | err = 0; |
| 1444 | goto out; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * Try to flush the object off the GPU without holding the lock. |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1449 | * We will repeat the flush holding the lock in the normal manner |
| 1450 | * to catch cases where we are gazumped. |
| 1451 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1452 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1453 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1454 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1455 | (write_domain ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1456 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1457 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1458 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1459 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1460 | /* |
| 1461 | * Proxy objects do not control access to the backing storage, ergo |
| 1462 | * they cannot be used as a means to manipulate the cache domain |
| 1463 | * tracking for that backing storage. The proxy object is always |
| 1464 | * considered to be outside of any cache domain. |
| 1465 | */ |
| 1466 | if (i915_gem_object_is_proxy(obj)) { |
| 1467 | err = -ENXIO; |
| 1468 | goto out; |
| 1469 | } |
| 1470 | |
| 1471 | /* |
| 1472 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1473 | * direct access in memory with previous cached writes through |
| 1474 | * shmemfs and that our cache domain tracking remains valid. |
| 1475 | * For example, if the obj->filp was moved to swap without us |
| 1476 | * being notified and releasing the pages, we would mistakenly |
| 1477 | * continue to assume that the obj remained out of the CPU cached |
| 1478 | * domain. |
| 1479 | */ |
| 1480 | err = i915_gem_object_pin_pages(obj); |
| 1481 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1482 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1483 | |
| 1484 | err = i915_mutex_lock_interruptible(dev); |
| 1485 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1486 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1487 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1488 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1489 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1490 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1491 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1492 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1493 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1494 | |
| 1495 | /* And bump the LRU for this access */ |
| 1496 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1497 | |
| 1498 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1499 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1500 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1501 | intel_fb_obj_invalidate(obj, |
| 1502 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1503 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1504 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1505 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1506 | out: |
| 1507 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1508 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1513 | * @dev: drm device |
| 1514 | * @data: ioctl data blob |
| 1515 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1516 | */ |
| 1517 | int |
| 1518 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1519 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1520 | { |
| 1521 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1522 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1523 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1524 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1525 | if (!obj) |
| 1526 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1527 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1528 | /* |
| 1529 | * Proxy objects are barred from CPU access, so there is no |
| 1530 | * need to ban sw_finish as it is a nop. |
| 1531 | */ |
| 1532 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1533 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1534 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1535 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1536 | |
| 1537 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1540 | static inline bool |
| 1541 | __vma_matches(struct vm_area_struct *vma, struct file *filp, |
| 1542 | unsigned long addr, unsigned long size) |
| 1543 | { |
| 1544 | if (vma->vm_file != filp) |
| 1545 | return false; |
| 1546 | |
Tvrtko Ursulin | a90e194 | 2019-03-05 11:04:08 +0000 | [diff] [blame] | 1547 | return vma->vm_start == addr && |
| 1548 | (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1549 | } |
| 1550 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1551 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1552 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1553 | * it is mapped to. |
| 1554 | * @dev: drm device |
| 1555 | * @data: ioctl data blob |
| 1556 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1557 | * |
| 1558 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1559 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1560 | * |
| 1561 | * IMPORTANT: |
| 1562 | * |
| 1563 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1564 | * mmap support, please don't implement mmap support like here. The modern way |
| 1565 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1566 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1567 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1568 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1569 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1570 | */ |
| 1571 | int |
| 1572 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1573 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1574 | { |
| 1575 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1576 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1577 | unsigned long addr; |
| 1578 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1579 | if (args->flags & ~(I915_MMAP_WC)) |
| 1580 | return -EINVAL; |
| 1581 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1582 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1583 | return -ENODEV; |
| 1584 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1585 | obj = i915_gem_object_lookup(file, args->handle); |
| 1586 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1587 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1588 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1589 | /* prime objects have no backing filp to GEM mmap |
| 1590 | * pages from. |
| 1591 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1592 | if (!obj->base.filp) { |
Chris Wilson | 794a11c | 2019-03-14 07:58:29 +0000 | [diff] [blame] | 1593 | addr = -ENXIO; |
| 1594 | goto err; |
| 1595 | } |
| 1596 | |
| 1597 | if (range_overflows(args->offset, args->size, (u64)obj->base.size)) { |
| 1598 | addr = -EINVAL; |
| 1599 | goto err; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1602 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1603 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1604 | args->offset); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1605 | if (IS_ERR_VALUE(addr)) |
| 1606 | goto err; |
| 1607 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1608 | if (args->flags & I915_MMAP_WC) { |
| 1609 | struct mm_struct *mm = current->mm; |
| 1610 | struct vm_area_struct *vma; |
| 1611 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1612 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | 794a11c | 2019-03-14 07:58:29 +0000 | [diff] [blame] | 1613 | addr = -EINTR; |
| 1614 | goto err; |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1615 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1616 | vma = find_vma(mm, addr); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1617 | if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1618 | vma->vm_page_prot = |
| 1619 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1620 | else |
| 1621 | addr = -ENOMEM; |
| 1622 | up_write(&mm->mmap_sem); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1623 | if (IS_ERR_VALUE(addr)) |
| 1624 | goto err; |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1625 | |
| 1626 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1627 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1628 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1629 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1630 | |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1631 | args->addr_ptr = (u64)addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1632 | return 0; |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1633 | |
| 1634 | err: |
| 1635 | i915_gem_object_put(obj); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1636 | return addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1639 | 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] | 1640 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1641 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1642 | } |
| 1643 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1644 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1645 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1646 | * |
| 1647 | * A history of the GTT mmap interface: |
| 1648 | * |
| 1649 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1650 | * aligned and suitable for fencing, and still fit into the available |
| 1651 | * mappable space left by the pinned display objects. A classic problem |
| 1652 | * we called the page-fault-of-doom where we would ping-pong between |
| 1653 | * two objects that could not fit inside the GTT and so the memcpy |
| 1654 | * would page one object in at the expense of the other between every |
| 1655 | * single byte. |
| 1656 | * |
| 1657 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1658 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1659 | * object is too large for the available space (or simply too large |
| 1660 | * for the mappable aperture!), a view is created instead and faulted |
| 1661 | * into userspace. (This view is aligned and sized appropriately for |
| 1662 | * fenced access.) |
| 1663 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1664 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1665 | * delayed writes via GTT before performing direct access via WC. |
| 1666 | * |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 1667 | * 3 - Remove implicit set-domain(GTT) and synchronisation on initial |
| 1668 | * pagefault; swapin remains transparent. |
| 1669 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1670 | * Restrictions: |
| 1671 | * |
| 1672 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1673 | * hangs on some architectures, corruption on others. An attempt to service |
| 1674 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1675 | * |
| 1676 | * * the object must be able to fit into RAM (physical memory, though no |
| 1677 | * limited to the mappable aperture). |
| 1678 | * |
| 1679 | * |
| 1680 | * Caveats: |
| 1681 | * |
| 1682 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1683 | * all data to system memory. Subsequent access will not be synchronized. |
| 1684 | * |
| 1685 | * * all mappings are revoked on runtime device suspend. |
| 1686 | * |
| 1687 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1688 | * (older machines require fence register for display and blitter access |
| 1689 | * as well). Contention of the fence registers will cause the previous users |
| 1690 | * to be unmapped and any new access will generate new page faults. |
| 1691 | * |
| 1692 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1693 | * rather than the expected SIGSEGV. |
| 1694 | */ |
| 1695 | int i915_gem_mmap_gtt_version(void) |
| 1696 | { |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 1697 | return 3; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1698 | } |
| 1699 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1700 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1701 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1702 | pgoff_t page_offset, |
| 1703 | unsigned int chunk) |
| 1704 | { |
| 1705 | struct i915_ggtt_view view; |
| 1706 | |
| 1707 | if (i915_gem_object_is_tiled(obj)) |
| 1708 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1709 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1710 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1711 | view.partial.offset = rounddown(page_offset, chunk); |
| 1712 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1713 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1714 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1715 | |
| 1716 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1717 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1718 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1719 | |
| 1720 | return view; |
| 1721 | } |
| 1722 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1723 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1724 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1725 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1726 | * |
| 1727 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1728 | * from userspace. The fault handler takes care of binding the object to |
| 1729 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1730 | * only if needed based on whether the old reg is still valid or the object |
| 1731 | * is tiled) and inserting a new PTE into the faulting process. |
| 1732 | * |
| 1733 | * Note that the faulting process may involve evicting existing objects |
| 1734 | * from the GTT and/or fence registers to make room. So performance may |
| 1735 | * suffer if the GTT working set is large or there are few fence registers |
| 1736 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1737 | * |
| 1738 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1739 | * 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] | 1740 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1741 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1742 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1743 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1744 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1745 | 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] | 1746 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1747 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1748 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1749 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1750 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1751 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1752 | pgoff_t page_offset; |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 1753 | int srcu; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1754 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1755 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1756 | /* Sanity check that we allow writing into this object */ |
| 1757 | if (i915_gem_object_is_readonly(obj) && write) |
| 1758 | return VM_FAULT_SIGBUS; |
| 1759 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1760 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1761 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1762 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1763 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1764 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1765 | ret = i915_gem_object_pin_pages(obj); |
| 1766 | if (ret) |
| 1767 | goto err; |
| 1768 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1769 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1770 | |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1771 | srcu = i915_reset_trylock(dev_priv); |
| 1772 | if (srcu < 0) { |
| 1773 | ret = srcu; |
| 1774 | goto err_rpm; |
| 1775 | } |
| 1776 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1777 | ret = i915_mutex_lock_interruptible(dev); |
| 1778 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1779 | goto err_reset; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1780 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1781 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1782 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1783 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1784 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1787 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1788 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1789 | PIN_MAPPABLE | |
| 1790 | PIN_NONBLOCK | |
| 1791 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1792 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1793 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1794 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1795 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1796 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1797 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1798 | flags = PIN_MAPPABLE; |
| 1799 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1800 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1801 | |
| 1802 | /* |
| 1803 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1804 | * all hope that the hardware is able to track future writes. |
| 1805 | */ |
| 1806 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1807 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1808 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1809 | if (IS_ERR(vma) && !view.type) { |
| 1810 | flags = PIN_MAPPABLE; |
| 1811 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1812 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1813 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1814 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1815 | if (IS_ERR(vma)) { |
| 1816 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1817 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1818 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1819 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1820 | ret = i915_vma_pin_fence(vma); |
| 1821 | if (ret) |
| 1822 | goto err_unpin; |
| 1823 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1824 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1825 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1826 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1827 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1828 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1829 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1830 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1831 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1832 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1833 | /* Mark as being mmapped into userspace for later revocation */ |
| 1834 | assert_rpm_wakelock_held(dev_priv); |
| 1835 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1836 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame^] | 1837 | if (CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND) |
| 1838 | intel_wakeref_auto(&dev_priv->mm.userfault_wakeref, |
| 1839 | msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND)); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1840 | GEM_BUG_ON(!obj->userfault_count); |
| 1841 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1842 | i915_vma_set_ggtt_write(vma); |
| 1843 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1844 | err_fence: |
| 1845 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1846 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1847 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1848 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1849 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1850 | err_reset: |
| 1851 | i915_reset_unlock(dev_priv, srcu); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1852 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1853 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1854 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1855 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1856 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1857 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1858 | /* |
| 1859 | * We eat errors when the gpu is terminally wedged to avoid |
| 1860 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1861 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1862 | * and so needs to be reported. |
| 1863 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1864 | if (!i915_terminally_wedged(dev_priv)) |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1865 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1866 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1867 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1868 | /* |
| 1869 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1870 | * handler to reset everything when re-faulting in |
| 1871 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1872 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1873 | case 0: |
| 1874 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1875 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1876 | case -EBUSY: |
| 1877 | /* |
| 1878 | * EBUSY is ok: this just means that another thread |
| 1879 | * already did the job. |
| 1880 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1881 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1882 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1883 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1884 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 1885 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1886 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1887 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1888 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1889 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1893 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 1894 | { |
| 1895 | struct i915_vma *vma; |
| 1896 | |
| 1897 | GEM_BUG_ON(!obj->userfault_count); |
| 1898 | |
| 1899 | obj->userfault_count = 0; |
| 1900 | list_del(&obj->userfault_link); |
| 1901 | drm_vma_node_unmap(&obj->base.vma_node, |
| 1902 | obj->base.dev->anon_inode->i_mapping); |
| 1903 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1904 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1905 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1906 | } |
| 1907 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1908 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1909 | * i915_gem_release_mmap - remove physical page mappings |
| 1910 | * @obj: obj in question |
| 1911 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1912 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1913 | * relinquish ownership of the pages back to the system. |
| 1914 | * |
| 1915 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1916 | * object through the GTT and then lose the fence register due to |
| 1917 | * resource pressure. Similarly if the object has been moved out of the |
| 1918 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1919 | * mapping will then trigger a page fault on the next user access, allowing |
| 1920 | * fixup by i915_gem_fault(). |
| 1921 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1922 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1923 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1924 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1925 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1926 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1927 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1928 | /* Serialisation between user GTT access and our code depends upon |
| 1929 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 1930 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1931 | * |
| 1932 | * Note that RPM complicates somewhat by adding an additional |
| 1933 | * requirement that operations to the GGTT be made holding the RPM |
| 1934 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1935 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1936 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1937 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1938 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1939 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1940 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1941 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1942 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1943 | |
| 1944 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 1945 | * memory transactions from userspace before we return. The TLB |
| 1946 | * flushing implied above by changing the PTE above *should* be |
| 1947 | * sufficient, an extra barrier here just provides us with a bit |
| 1948 | * of paranoid documentation about our requirement to serialise |
| 1949 | * memory writes before touching registers / GSM. |
| 1950 | */ |
| 1951 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1952 | |
| 1953 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1954 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1957 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 1958 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 1959 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1960 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 1961 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 1962 | /* |
| 1963 | * Only called during RPM suspend. All users of the userfault_list |
| 1964 | * must be holding an RPM wakeref to ensure that this can not |
| 1965 | * run concurrently with themselves (and use the struct_mutex for |
| 1966 | * protection between themselves). |
| 1967 | */ |
| 1968 | |
| 1969 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1970 | &dev_priv->mm.userfault_list, userfault_link) |
| 1971 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1972 | |
| 1973 | /* The fence will be lost when the device powers down. If any were |
| 1974 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 1975 | * down! All other fences will be reacquired by the user upon waking. |
| 1976 | */ |
| 1977 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 1978 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 1979 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 1980 | /* Ideally we want to assert that the fence register is not |
| 1981 | * live at this point (i.e. that no piece of code will be |
| 1982 | * trying to write through fence + GTT, as that both violates |
| 1983 | * our tracking of activity and associated locking/barriers, |
| 1984 | * but also is illegal given that the hw is powered down). |
| 1985 | * |
| 1986 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 1987 | * That is not sufficient, and we need a more fine-grained |
| 1988 | * tool if we want to have a sanity check here. |
| 1989 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1990 | |
| 1991 | if (!reg->vma) |
| 1992 | continue; |
| 1993 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1994 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 1995 | reg->dirty = true; |
| 1996 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1999 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2000 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2001 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2002 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2003 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2004 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2005 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2006 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2007 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2008 | /* Attempt to reap some mmap space from dead objects */ |
| 2009 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2010 | err = i915_gem_wait_for_idle(dev_priv, |
| 2011 | I915_WAIT_INTERRUPTIBLE, |
| 2012 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2013 | if (err) |
| 2014 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2015 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2016 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2017 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2018 | if (!err) |
| 2019 | break; |
| 2020 | |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 2021 | } while (flush_delayed_work(&dev_priv->gem.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2022 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2023 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2027 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2028 | drm_gem_free_mmap_offset(&obj->base); |
| 2029 | } |
| 2030 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2031 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2032 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2033 | struct drm_device *dev, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 2034 | u32 handle, |
| 2035 | u64 *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2036 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2037 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2038 | int ret; |
| 2039 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2040 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2041 | if (!obj) |
| 2042 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2043 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2044 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2045 | if (ret == 0) |
| 2046 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2047 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2048 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2049 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2050 | } |
| 2051 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2052 | /** |
| 2053 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2054 | * @dev: DRM device |
| 2055 | * @data: GTT mapping ioctl data |
| 2056 | * @file: GEM object info |
| 2057 | * |
| 2058 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2059 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2060 | * up so we can get faults in the handler above. |
| 2061 | * |
| 2062 | * The fault handler will take care of binding the object into the GTT |
| 2063 | * (since it may have been evicted to make room for something), allocating |
| 2064 | * a fence register, and mapping the appropriate aperture address into |
| 2065 | * userspace. |
| 2066 | */ |
| 2067 | int |
| 2068 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2069 | struct drm_file *file) |
| 2070 | { |
| 2071 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2072 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2073 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2074 | } |
| 2075 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2076 | /* Immediately discard the backing storage */ |
Chris Wilson | 2d6692e | 2019-04-20 12:55:39 +0100 | [diff] [blame] | 2077 | void __i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2078 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2079 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2080 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2081 | if (obj->base.filp == NULL) |
| 2082 | return; |
| 2083 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2084 | /* Our goal here is to return as much of the memory as |
| 2085 | * is possible back to the system as we are called from OOM. |
| 2086 | * To do this we must instruct the shmfs to drop all of its |
| 2087 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2088 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2089 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2090 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2091 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2092 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2093 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2094 | /* |
| 2095 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2096 | * ref count of those pages. |
| 2097 | */ |
| 2098 | static void check_release_pagevec(struct pagevec *pvec) |
| 2099 | { |
| 2100 | check_move_unevictable_pages(pvec); |
| 2101 | __pagevec_release(pvec); |
| 2102 | cond_resched(); |
| 2103 | } |
| 2104 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2105 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2106 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2107 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2108 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2109 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2110 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2111 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2112 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2113 | __i915_gem_object_release_shmem(obj, pages, true); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2114 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2115 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2116 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2117 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2118 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2119 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2120 | |
| 2121 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2122 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2123 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2124 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2125 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2126 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2127 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2128 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2129 | if (!pagevec_add(&pvec, page)) |
| 2130 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2131 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2132 | if (pagevec_count(&pvec)) |
| 2133 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2134 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2135 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2136 | sg_free_table(pages); |
| 2137 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2138 | } |
| 2139 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2140 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2141 | { |
| 2142 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2143 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2144 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2145 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2146 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2147 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2148 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2149 | } |
| 2150 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2151 | static struct sg_table * |
| 2152 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2153 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2154 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2155 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2156 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2157 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2158 | if (IS_ERR_OR_NULL(pages)) |
| 2159 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2160 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2161 | spin_lock(&i915->mm.obj_lock); |
| 2162 | list_del(&obj->mm.link); |
| 2163 | spin_unlock(&i915->mm.obj_lock); |
| 2164 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2165 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2166 | void *ptr; |
| 2167 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2168 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2169 | if (is_vmalloc_addr(ptr)) |
| 2170 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2171 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2172 | kunmap(kmap_to_page(ptr)); |
| 2173 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2174 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2177 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2178 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2179 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2180 | return pages; |
| 2181 | } |
| 2182 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2183 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2184 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2185 | { |
| 2186 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2187 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2188 | |
| 2189 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2190 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2191 | |
| 2192 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2193 | |
| 2194 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2195 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2196 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2197 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2198 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2199 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2200 | |
| 2201 | /* |
| 2202 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2203 | * array, hence protect them from being reaped by removing them from gtt |
| 2204 | * lists early. |
| 2205 | */ |
| 2206 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2207 | |
| 2208 | /* |
| 2209 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2210 | * NULL pages. In the future, when we have more asynchronous |
| 2211 | * get_pages backends we should be better able to handle the |
| 2212 | * cancellation of the async task in a more uniform manner. |
| 2213 | */ |
| 2214 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2215 | pages = ERR_PTR(-EINVAL); |
| 2216 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2217 | if (!IS_ERR(pages)) |
| 2218 | obj->ops->put_pages(obj, pages); |
| 2219 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2220 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2221 | unlock: |
| 2222 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2223 | |
| 2224 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2225 | } |
| 2226 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2227 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2228 | { |
| 2229 | struct sg_table new_st; |
| 2230 | struct scatterlist *sg, *new_sg; |
| 2231 | unsigned int i; |
| 2232 | |
| 2233 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2234 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2235 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2236 | 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] | 2237 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2238 | |
| 2239 | new_sg = new_st.sgl; |
| 2240 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2241 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2242 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2243 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2244 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2245 | new_sg = sg_next(new_sg); |
| 2246 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2247 | 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] | 2248 | |
| 2249 | sg_free_table(orig_st); |
| 2250 | |
| 2251 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2252 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2255 | 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] | 2256 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2257 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2258 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2259 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2260 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2261 | struct sg_table *st; |
| 2262 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2263 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2264 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2265 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2266 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2267 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2268 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2269 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2270 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2271 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2272 | /* |
| 2273 | * 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] | 2274 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2275 | * a GPU cache |
| 2276 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2277 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2278 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2279 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2280 | /* |
| 2281 | * If there's no chance of allocating enough pages for the whole |
| 2282 | * object, bail early. |
| 2283 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2284 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2285 | return -ENOMEM; |
| 2286 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2287 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2288 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2289 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2290 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2291 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2292 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2293 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2294 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2295 | } |
| 2296 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2297 | /* |
| 2298 | * 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] | 2299 | * at this point until we release them. |
| 2300 | * |
| 2301 | * Fail silently without starting the shrinker |
| 2302 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2303 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2304 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2305 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2306 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2307 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2308 | sg = st->sgl; |
| 2309 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2310 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2311 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2312 | const unsigned int shrink[] = { |
| 2313 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2314 | 0, |
| 2315 | }, *s = shrink; |
| 2316 | gfp_t gfp = noreclaim; |
| 2317 | |
| 2318 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2319 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2320 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 2321 | if (!IS_ERR(page)) |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2322 | break; |
| 2323 | |
| 2324 | if (!*s) { |
| 2325 | ret = PTR_ERR(page); |
| 2326 | goto err_sg; |
| 2327 | } |
| 2328 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2329 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2330 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2331 | /* |
| 2332 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2333 | * our own buffer, now let the real VM do its job and |
| 2334 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2335 | * |
| 2336 | * However, since graphics tend to be disposable, |
| 2337 | * defer the oom here by reporting the ENOMEM back |
| 2338 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2339 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2340 | if (!*s) { |
| 2341 | /* reclaim and warn, but no oom */ |
| 2342 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2343 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2344 | /* |
| 2345 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2346 | * kswapd to reclaim our pages (direct reclaim |
| 2347 | * does not effectively begin pageout of our |
| 2348 | * buffers on its own). However, direct reclaim |
| 2349 | * only waits for kswapd when under allocation |
| 2350 | * congestion. So as a result __GFP_RECLAIM is |
| 2351 | * unreliable and fails to actually reclaim our |
| 2352 | * dirty pages -- unless you try over and over |
| 2353 | * again with !__GFP_NORETRY. However, we still |
| 2354 | * want to fail this allocation rather than |
| 2355 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2356 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2357 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2358 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2359 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2360 | } while (1); |
| 2361 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2362 | if (!i || |
| 2363 | sg->length >= max_segment || |
| 2364 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2365 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2366 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2367 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2368 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2369 | st->nents++; |
| 2370 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2371 | } else { |
| 2372 | sg->length += PAGE_SIZE; |
| 2373 | } |
| 2374 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2375 | |
| 2376 | /* Check that the i965g/gm workaround works. */ |
| 2377 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2378 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2379 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2380 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2381 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2382 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2383 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2384 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2385 | i915_sg_trim(st); |
| 2386 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2387 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2388 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2389 | /* |
| 2390 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2391 | * it could not reserve enough large entries, asking |
| 2392 | * for PAGE_SIZE chunks instead may be helpful. |
| 2393 | */ |
| 2394 | if (max_segment > PAGE_SIZE) { |
| 2395 | for_each_sgt_page(page, sgt_iter, st) |
| 2396 | put_page(page); |
| 2397 | sg_free_table(st); |
| 2398 | |
| 2399 | max_segment = PAGE_SIZE; |
| 2400 | goto rebuild_st; |
| 2401 | } else { |
| 2402 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2403 | "Failed to DMA remap %lu pages\n", |
| 2404 | page_count); |
| 2405 | goto err_pages; |
| 2406 | } |
| 2407 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2408 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2409 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2410 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2411 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2412 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2413 | |
| 2414 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2415 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2416 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2417 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2418 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2419 | mapping_clear_unevictable(mapping); |
| 2420 | pagevec_init(&pvec); |
| 2421 | for_each_sgt_page(page, sgt_iter, st) { |
| 2422 | if (!pagevec_add(&pvec, page)) |
| 2423 | check_release_pagevec(&pvec); |
| 2424 | } |
| 2425 | if (pagevec_count(&pvec)) |
| 2426 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2427 | sg_free_table(st); |
| 2428 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2429 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2430 | /* |
| 2431 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2432 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2433 | * ENOMEM for a genuine allocation failure. |
| 2434 | * |
| 2435 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2436 | * space and so want to translate the error from shmemfs back to our |
| 2437 | * usual understanding of ENOMEM. |
| 2438 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2439 | if (ret == -ENOSPC) |
| 2440 | ret = -ENOMEM; |
| 2441 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2442 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2446 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2447 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2448 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2449 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2450 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2451 | int i; |
| 2452 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2453 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2454 | |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 2455 | /* Make the pages coherent with the GPU (flushing any swapin). */ |
| 2456 | if (obj->cache_dirty) { |
| 2457 | obj->write_domain = 0; |
| 2458 | if (i915_gem_object_has_struct_page(obj)) |
| 2459 | drm_clflush_sg(pages); |
| 2460 | obj->cache_dirty = false; |
| 2461 | } |
| 2462 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2463 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2464 | obj->mm.get_page.sg_idx = 0; |
| 2465 | |
| 2466 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2467 | |
| 2468 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2469 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2470 | GEM_BUG_ON(obj->mm.quirked); |
| 2471 | __i915_gem_object_pin_pages(obj); |
| 2472 | obj->mm.quirked = true; |
| 2473 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2474 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2475 | GEM_BUG_ON(!sg_page_sizes); |
| 2476 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2477 | |
| 2478 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2479 | * Calculate the supported page-sizes which fit into the given |
| 2480 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2481 | * to use opportunistically when later inserting into the GTT. For |
| 2482 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2483 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2484 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2485 | */ |
| 2486 | obj->mm.page_sizes.sg = 0; |
| 2487 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2488 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2489 | obj->mm.page_sizes.sg |= BIT(i); |
| 2490 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2491 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2492 | |
| 2493 | spin_lock(&i915->mm.obj_lock); |
| 2494 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2495 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2499 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2500 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2501 | |
| 2502 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2503 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2504 | return -EFAULT; |
| 2505 | } |
| 2506 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2507 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2508 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2509 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2510 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2511 | } |
| 2512 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2513 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2514 | * 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] | 2515 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2516 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2517 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2518 | * or as the object is itself released. |
| 2519 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2520 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2521 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2522 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2523 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2524 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2525 | if (err) |
| 2526 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2527 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2528 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2529 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2530 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2531 | err = ____i915_gem_object_get_pages(obj); |
| 2532 | if (err) |
| 2533 | goto unlock; |
| 2534 | |
| 2535 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2536 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2537 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2538 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2539 | unlock: |
| 2540 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2541 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2544 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2545 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2546 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2547 | { |
| 2548 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2549 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2550 | struct sgt_iter sgt_iter; |
| 2551 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2552 | struct page *stack_pages[32]; |
| 2553 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2554 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2555 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2556 | void *addr; |
| 2557 | |
| 2558 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2559 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2560 | return kmap(sg_page(sgt->sgl)); |
| 2561 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2562 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2563 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2564 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2565 | if (!pages) |
| 2566 | return NULL; |
| 2567 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2568 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2569 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2570 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2571 | |
| 2572 | /* Check that we have the expected number of pages */ |
| 2573 | GEM_BUG_ON(i != n_pages); |
| 2574 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2575 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2576 | default: |
| 2577 | MISSING_CASE(type); |
| 2578 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2579 | case I915_MAP_WB: |
| 2580 | pgprot = PAGE_KERNEL; |
| 2581 | break; |
| 2582 | case I915_MAP_WC: |
| 2583 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2584 | break; |
| 2585 | } |
| 2586 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2587 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2588 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2589 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2590 | |
| 2591 | return addr; |
| 2592 | } |
| 2593 | |
| 2594 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2595 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2596 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2597 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2598 | enum i915_map_type has_type; |
| 2599 | bool pinned; |
| 2600 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2601 | int ret; |
| 2602 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2603 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2604 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2605 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2606 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2607 | if (ret) |
| 2608 | return ERR_PTR(ret); |
| 2609 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2610 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2611 | type &= ~I915_MAP_OVERRIDE; |
| 2612 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2613 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2614 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2615 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2616 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2617 | ret = ____i915_gem_object_get_pages(obj); |
| 2618 | if (ret) |
| 2619 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2620 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2621 | smp_mb__before_atomic(); |
| 2622 | } |
| 2623 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2624 | pinned = false; |
| 2625 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2626 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2627 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2628 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2629 | if (ptr && has_type != type) { |
| 2630 | if (pinned) { |
| 2631 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2632 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2633 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2634 | |
| 2635 | if (is_vmalloc_addr(ptr)) |
| 2636 | vunmap(ptr); |
| 2637 | else |
| 2638 | kunmap(kmap_to_page(ptr)); |
| 2639 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2640 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2641 | } |
| 2642 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2643 | if (!ptr) { |
| 2644 | ptr = i915_gem_object_map(obj, type); |
| 2645 | if (!ptr) { |
| 2646 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2647 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2648 | } |
| 2649 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2650 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2651 | } |
| 2652 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2653 | out_unlock: |
| 2654 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2655 | return ptr; |
| 2656 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2657 | err_unpin: |
| 2658 | atomic_dec(&obj->mm.pages_pin_count); |
| 2659 | err_unlock: |
| 2660 | ptr = ERR_PTR(ret); |
| 2661 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2662 | } |
| 2663 | |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 2664 | void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj, |
| 2665 | unsigned long offset, |
| 2666 | unsigned long size) |
| 2667 | { |
| 2668 | enum i915_map_type has_type; |
| 2669 | void *ptr; |
| 2670 | |
| 2671 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 2672 | GEM_BUG_ON(range_overflows_t(typeof(obj->base.size), |
| 2673 | offset, size, obj->base.size)); |
| 2674 | |
| 2675 | obj->mm.dirty = true; |
| 2676 | |
| 2677 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) |
| 2678 | return; |
| 2679 | |
| 2680 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
| 2681 | if (has_type == I915_MAP_WC) |
| 2682 | return; |
| 2683 | |
| 2684 | drm_clflush_virt_range(ptr + offset, size); |
| 2685 | if (size == obj->base.size) { |
| 2686 | obj->write_domain &= ~I915_GEM_DOMAIN_CPU; |
| 2687 | obj->cache_dirty = false; |
| 2688 | } |
| 2689 | } |
| 2690 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2691 | static int |
| 2692 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2693 | const struct drm_i915_gem_pwrite *arg) |
| 2694 | { |
| 2695 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2696 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2697 | u64 remain, offset; |
| 2698 | unsigned int pg; |
| 2699 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2700 | /* Caller already validated user args */ |
| 2701 | GEM_BUG_ON(!access_ok(user_data, arg->size)); |
| 2702 | |
| 2703 | /* |
| 2704 | * Before we instantiate/pin the backing store for our use, we |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2705 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2706 | * the pagecache. We avoid the penalty of instantiating all the |
| 2707 | * pages, important if the user is just writing to a few and never |
| 2708 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2709 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2710 | * or clearing-before-use) before it is overwritten. |
| 2711 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2712 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2713 | return -ENODEV; |
| 2714 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2715 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2716 | return -EFAULT; |
| 2717 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2718 | /* |
| 2719 | * Before the pages are instantiated the object is treated as being |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2720 | * in the CPU domain. The pages will be clflushed as required before |
| 2721 | * use, and we can freely write into the pages directly. If userspace |
| 2722 | * races pwrite with any other operation; corruption will ensue - |
| 2723 | * that is userspace's prerogative! |
| 2724 | */ |
| 2725 | |
| 2726 | remain = arg->size; |
| 2727 | offset = arg->offset; |
| 2728 | pg = offset_in_page(offset); |
| 2729 | |
| 2730 | do { |
| 2731 | unsigned int len, unwritten; |
| 2732 | struct page *page; |
| 2733 | void *data, *vaddr; |
| 2734 | int err; |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2735 | char c; |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2736 | |
| 2737 | len = PAGE_SIZE - pg; |
| 2738 | if (len > remain) |
| 2739 | len = remain; |
| 2740 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2741 | /* Prefault the user page to reduce potential recursion */ |
| 2742 | err = __get_user(c, user_data); |
| 2743 | if (err) |
| 2744 | return err; |
| 2745 | |
| 2746 | err = __get_user(c, user_data + len - 1); |
| 2747 | if (err) |
| 2748 | return err; |
| 2749 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2750 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2751 | offset, len, 0, |
| 2752 | &page, &data); |
| 2753 | if (err < 0) |
| 2754 | return err; |
| 2755 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2756 | vaddr = kmap_atomic(page); |
| 2757 | unwritten = __copy_from_user_inatomic(vaddr + pg, |
| 2758 | user_data, |
| 2759 | len); |
| 2760 | kunmap_atomic(vaddr); |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2761 | |
| 2762 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2763 | offset, len, len - unwritten, |
| 2764 | page, data); |
| 2765 | if (err < 0) |
| 2766 | return err; |
| 2767 | |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2768 | /* We don't handle -EFAULT, leave it to the caller to check */ |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2769 | if (unwritten) |
Chris Wilson | b01720b | 2019-04-01 14:39:09 +0100 | [diff] [blame] | 2770 | return -ENODEV; |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2771 | |
| 2772 | remain -= len; |
| 2773 | user_data += len; |
| 2774 | offset += len; |
| 2775 | pg = 0; |
| 2776 | } while (remain); |
| 2777 | |
| 2778 | return 0; |
| 2779 | } |
| 2780 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2781 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 2782 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2783 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2784 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 2785 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2786 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2787 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2788 | mutex_lock(&i915->drm.struct_mutex); |
| 2789 | |
| 2790 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 2791 | struct i915_gem_context *ctx = lut->ctx; |
| 2792 | struct i915_vma *vma; |
| 2793 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 2794 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2795 | if (ctx->file_priv != fpriv) |
| 2796 | continue; |
| 2797 | |
| 2798 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 2799 | GEM_BUG_ON(vma->obj != obj); |
| 2800 | |
| 2801 | /* We allow the process to have multiple handles to the same |
| 2802 | * vma, in the same fd namespace, by virtue of flink/open. |
| 2803 | */ |
| 2804 | GEM_BUG_ON(!vma->open_count); |
| 2805 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2806 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 2807 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2808 | list_del(&lut->obj_link); |
| 2809 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 2810 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 2811 | i915_lut_handle_free(lut); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2812 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 2813 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 2814 | |
| 2815 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 2816 | } |
| 2817 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2818 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 2819 | { |
| 2820 | if (timeout_ns < 0) |
| 2821 | return MAX_SCHEDULE_TIMEOUT; |
| 2822 | |
| 2823 | if (timeout_ns == 0) |
| 2824 | return 0; |
| 2825 | |
| 2826 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 2827 | } |
| 2828 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 2829 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2830 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 2831 | * @dev: drm device pointer |
| 2832 | * @data: ioctl data blob |
| 2833 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2834 | * |
| 2835 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 2836 | * the timeout parameter. |
| 2837 | * -ETIME: object is still busy after timeout |
| 2838 | * -ERESTARTSYS: signal interrupted the wait |
| 2839 | * -ENONENT: object doesn't exist |
| 2840 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 2841 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2842 | * -ENOMEM: damn |
| 2843 | * -ENODEV: Internal IRQ fail |
| 2844 | * -E?: The add request failed |
| 2845 | * |
| 2846 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 2847 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 2848 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 2849 | * without holding struct_mutex the object may become re-busied before this |
| 2850 | * function completes. A similar but shorter * race condition exists in the busy |
| 2851 | * ioctl |
| 2852 | */ |
| 2853 | int |
| 2854 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 2855 | { |
| 2856 | struct drm_i915_gem_wait *args = data; |
| 2857 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2858 | ktime_t start; |
| 2859 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2860 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 2861 | if (args->flags != 0) |
| 2862 | return -EINVAL; |
| 2863 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2864 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 2865 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2866 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 2867 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2868 | start = ktime_get(); |
| 2869 | |
| 2870 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 2871 | I915_WAIT_INTERRUPTIBLE | |
| 2872 | I915_WAIT_PRIORITY | |
| 2873 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 2874 | to_wait_timeout(args->timeout_ns)); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2875 | |
| 2876 | if (args->timeout_ns > 0) { |
| 2877 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2878 | if (args->timeout_ns < 0) |
| 2879 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 2880 | |
| 2881 | /* |
| 2882 | * Apparently ktime isn't accurate enough and occasionally has a |
| 2883 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 2884 | * things up to make the test happy. We allow up to 1 jiffy. |
| 2885 | * |
| 2886 | * This is a regression from the timespec->ktime conversion. |
| 2887 | */ |
| 2888 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 2889 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 2890 | |
| 2891 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 2892 | if (ret == -ETIME && args->timeout_ns) |
| 2893 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2894 | } |
| 2895 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2896 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 2897 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2898 | } |
| 2899 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 2900 | static int wait_for_engines(struct drm_i915_private *i915) |
| 2901 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 2902 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 2903 | dev_err(i915->drm.dev, |
| 2904 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 2905 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 2906 | i915_gem_set_wedged(i915); |
| 2907 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | return 0; |
| 2911 | } |
| 2912 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2913 | static long |
| 2914 | wait_for_timelines(struct drm_i915_private *i915, |
| 2915 | unsigned int flags, long timeout) |
| 2916 | { |
| 2917 | struct i915_gt_timelines *gt = &i915->gt.timelines; |
| 2918 | struct i915_timeline *tl; |
| 2919 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2920 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 2921 | list_for_each_entry(tl, >->active_list, link) { |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2922 | struct i915_request *rq; |
| 2923 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 2924 | rq = i915_active_request_get_unlocked(&tl->last_request); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2925 | if (!rq) |
| 2926 | continue; |
| 2927 | |
| 2928 | mutex_unlock(>->mutex); |
| 2929 | |
| 2930 | /* |
| 2931 | * "Race-to-idle". |
| 2932 | * |
| 2933 | * Switching to the kernel context is often used a synchronous |
| 2934 | * step prior to idling, e.g. in suspend for flushing all |
| 2935 | * current operations to memory before sleeping. These we |
| 2936 | * want to complete as quickly as possible to avoid prolonged |
| 2937 | * stalls, so allow the gpu to boost to maximum clocks. |
| 2938 | */ |
| 2939 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 2940 | gen6_rps_boost(rq); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2941 | |
| 2942 | timeout = i915_request_wait(rq, flags, timeout); |
| 2943 | i915_request_put(rq); |
| 2944 | if (timeout < 0) |
| 2945 | return timeout; |
| 2946 | |
| 2947 | /* restart after reacquiring the lock */ |
| 2948 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 2949 | tl = list_entry(>->active_list, typeof(*tl), link); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2950 | } |
| 2951 | mutex_unlock(>->mutex); |
| 2952 | |
| 2953 | return timeout; |
| 2954 | } |
| 2955 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2956 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 2957 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 2958 | { |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 2959 | GEM_TRACE("flags=%x (%s), timeout=%ld%s, awake?=%s\n", |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2960 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 2961 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "", |
| 2962 | yesno(i915->gt.awake)); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 2963 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 2964 | /* If the device is asleep, we have no requests outstanding */ |
| 2965 | if (!READ_ONCE(i915->gt.awake)) |
| 2966 | return 0; |
| 2967 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 2968 | timeout = wait_for_timelines(i915, flags, timeout); |
| 2969 | if (timeout < 0) |
| 2970 | return timeout; |
| 2971 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 2972 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2973 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 2974 | |
| 2975 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 2976 | |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 2977 | err = wait_for_engines(i915); |
| 2978 | if (err) |
| 2979 | return err; |
| 2980 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2981 | i915_retire_requests(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2982 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 2983 | |
| 2984 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2985 | } |
| 2986 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 2987 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 2988 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 2989 | /* |
| 2990 | * We manually flush the CPU domain so that we can override and |
| 2991 | * force the flush for the display, and perform it asyncrhonously. |
| 2992 | */ |
| 2993 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 2994 | if (obj->cache_dirty) |
| 2995 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2996 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3000 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3001 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3002 | return; |
| 3003 | |
| 3004 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3005 | __i915_gem_object_flush_for_display(obj); |
| 3006 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3007 | } |
| 3008 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3009 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3010 | * Moves a single object to the WC read, and possibly write domain. |
| 3011 | * @obj: object to act on |
| 3012 | * @write: ask for write access or read only |
| 3013 | * |
| 3014 | * This function returns when the move is complete, including waiting on |
| 3015 | * flushes to occur. |
| 3016 | */ |
| 3017 | int |
| 3018 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3019 | { |
| 3020 | int ret; |
| 3021 | |
| 3022 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3023 | |
| 3024 | ret = i915_gem_object_wait(obj, |
| 3025 | I915_WAIT_INTERRUPTIBLE | |
| 3026 | I915_WAIT_LOCKED | |
| 3027 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3028 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3029 | if (ret) |
| 3030 | return ret; |
| 3031 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3032 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3033 | return 0; |
| 3034 | |
| 3035 | /* Flush and acquire obj->pages so that we are coherent through |
| 3036 | * direct access in memory with previous cached writes through |
| 3037 | * shmemfs and that our cache domain tracking remains valid. |
| 3038 | * For example, if the obj->filp was moved to swap without us |
| 3039 | * being notified and releasing the pages, we would mistakenly |
| 3040 | * continue to assume that the obj remained out of the CPU cached |
| 3041 | * domain. |
| 3042 | */ |
| 3043 | ret = i915_gem_object_pin_pages(obj); |
| 3044 | if (ret) |
| 3045 | return ret; |
| 3046 | |
| 3047 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3048 | |
| 3049 | /* Serialise direct access to this object with the barriers for |
| 3050 | * coherent writes from the GPU, by effectively invalidating the |
| 3051 | * WC domain upon first access. |
| 3052 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3053 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3054 | mb(); |
| 3055 | |
| 3056 | /* It should now be out of any other write domains, and we can update |
| 3057 | * the domain values for our changes. |
| 3058 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3059 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3060 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3061 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3062 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3063 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3064 | obj->mm.dirty = true; |
| 3065 | } |
| 3066 | |
| 3067 | i915_gem_object_unpin_pages(obj); |
| 3068 | return 0; |
| 3069 | } |
| 3070 | |
| 3071 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3072 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3073 | * @obj: object to act on |
| 3074 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3075 | * |
| 3076 | * This function returns when the move is complete, including waiting on |
| 3077 | * flushes to occur. |
| 3078 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3079 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3080 | 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] | 3081 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3082 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3083 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3084 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3085 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3086 | ret = i915_gem_object_wait(obj, |
| 3087 | I915_WAIT_INTERRUPTIBLE | |
| 3088 | I915_WAIT_LOCKED | |
| 3089 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3090 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3091 | if (ret) |
| 3092 | return ret; |
| 3093 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3094 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3095 | return 0; |
| 3096 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3097 | /* Flush and acquire obj->pages so that we are coherent through |
| 3098 | * direct access in memory with previous cached writes through |
| 3099 | * shmemfs and that our cache domain tracking remains valid. |
| 3100 | * For example, if the obj->filp was moved to swap without us |
| 3101 | * being notified and releasing the pages, we would mistakenly |
| 3102 | * continue to assume that the obj remained out of the CPU cached |
| 3103 | * domain. |
| 3104 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3105 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3106 | if (ret) |
| 3107 | return ret; |
| 3108 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3109 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3110 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3111 | /* Serialise direct access to this object with the barriers for |
| 3112 | * coherent writes from the GPU, by effectively invalidating the |
| 3113 | * GTT domain upon first access. |
| 3114 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3115 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3116 | mb(); |
| 3117 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3118 | /* It should now be out of any other write domains, and we can update |
| 3119 | * the domain values for our changes. |
| 3120 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3121 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3122 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3123 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3124 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3125 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3126 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3127 | } |
| 3128 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3129 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3130 | return 0; |
| 3131 | } |
| 3132 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3133 | /** |
| 3134 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3135 | * @obj: object to act on |
| 3136 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3137 | * |
| 3138 | * After this function returns, the object will be in the new cache-level |
| 3139 | * across all GTT and the contents of the backing storage will be coherent, |
| 3140 | * with respect to the new cache-level. In order to keep the backing storage |
| 3141 | * coherent for all users, we only allow a single cache level to be set |
| 3142 | * globally on the object and prevent it from being changed whilst the |
| 3143 | * hardware is reading from the object. That is if the object is currently |
| 3144 | * on the scanout it will be set to uncached (or equivalent display |
| 3145 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3146 | * that all direct access to the scanout remains coherent. |
| 3147 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3148 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3149 | enum i915_cache_level cache_level) |
| 3150 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3151 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3152 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3153 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3154 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3155 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3156 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3157 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3158 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3159 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3160 | * be invalid given the new cache-level. This is principally to |
| 3161 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3162 | * reading an invalid PTE on older architectures. |
| 3163 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3164 | restart: |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3165 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3166 | if (!drm_mm_node_allocated(&vma->node)) |
| 3167 | continue; |
| 3168 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3169 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3170 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3171 | return -EBUSY; |
| 3172 | } |
| 3173 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3174 | if (!i915_vma_is_closed(vma) && |
| 3175 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3176 | continue; |
| 3177 | |
| 3178 | ret = i915_vma_unbind(vma); |
| 3179 | if (ret) |
| 3180 | return ret; |
| 3181 | |
| 3182 | /* As unbinding may affect other elements in the |
| 3183 | * obj->vma_list (due to side-effects from retiring |
| 3184 | * an active vma), play safe and restart the iterator. |
| 3185 | */ |
| 3186 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3187 | } |
| 3188 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3189 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3190 | * cache-level on the PTE. We could simply unbind them all and |
| 3191 | * rebind with the correct cache-level on next use. However since |
| 3192 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3193 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3194 | * state and so involves less work. |
| 3195 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3196 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3197 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3198 | * If we wait upon the object, we know that all the bound |
| 3199 | * VMA are no longer active. |
| 3200 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3201 | ret = i915_gem_object_wait(obj, |
| 3202 | I915_WAIT_INTERRUPTIBLE | |
| 3203 | I915_WAIT_LOCKED | |
| 3204 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3205 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3206 | if (ret) |
| 3207 | return ret; |
| 3208 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3209 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3210 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3211 | /* Access to snoopable pages through the GTT is |
| 3212 | * incoherent and on some machines causes a hard |
| 3213 | * lockup. Relinquish the CPU mmaping to force |
| 3214 | * userspace to refault in the pages and we can |
| 3215 | * then double check if the GTT mapping is still |
| 3216 | * valid for that pointer access. |
| 3217 | */ |
| 3218 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3219 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3220 | /* As we no longer need a fence for GTT access, |
| 3221 | * we can relinquish it now (and so prevent having |
| 3222 | * to steal a fence from someone else on the next |
| 3223 | * fence request). Note GPU activity would have |
| 3224 | * dropped the fence as all snoopable access is |
| 3225 | * supposed to be linear. |
| 3226 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3227 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3228 | ret = i915_vma_put_fence(vma); |
| 3229 | if (ret) |
| 3230 | return ret; |
| 3231 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3232 | } else { |
| 3233 | /* We either have incoherent backing store and |
| 3234 | * so no GTT access or the architecture is fully |
| 3235 | * coherent. In such cases, existing GTT mmaps |
| 3236 | * ignore the cache bit in the PTE and we can |
| 3237 | * rewrite it without confusing the GPU or having |
| 3238 | * to force userspace to fault back in its mmaps. |
| 3239 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3240 | } |
| 3241 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3242 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3243 | if (!drm_mm_node_allocated(&vma->node)) |
| 3244 | continue; |
| 3245 | |
| 3246 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3247 | if (ret) |
| 3248 | return ret; |
| 3249 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3250 | } |
| 3251 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3252 | list_for_each_entry(vma, &obj->vma.list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3253 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3254 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3255 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3256 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3257 | return 0; |
| 3258 | } |
| 3259 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3260 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3261 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3262 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3263 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3264 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3265 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3266 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3267 | rcu_read_lock(); |
| 3268 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3269 | if (!obj) { |
| 3270 | err = -ENOENT; |
| 3271 | goto out; |
| 3272 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3273 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3274 | switch (obj->cache_level) { |
| 3275 | case I915_CACHE_LLC: |
| 3276 | case I915_CACHE_L3_LLC: |
| 3277 | args->caching = I915_CACHING_CACHED; |
| 3278 | break; |
| 3279 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3280 | case I915_CACHE_WT: |
| 3281 | args->caching = I915_CACHING_DISPLAY; |
| 3282 | break; |
| 3283 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3284 | default: |
| 3285 | args->caching = I915_CACHING_NONE; |
| 3286 | break; |
| 3287 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3288 | out: |
| 3289 | rcu_read_unlock(); |
| 3290 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3291 | } |
| 3292 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3293 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3294 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3295 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3296 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3297 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3298 | struct drm_i915_gem_object *obj; |
| 3299 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3300 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3301 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3302 | switch (args->caching) { |
| 3303 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3304 | level = I915_CACHE_NONE; |
| 3305 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3306 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3307 | /* |
| 3308 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3309 | * snooped mapping may leave stale data in a corresponding CPU |
| 3310 | * cacheline, whereas normally such cachelines would get |
| 3311 | * invalidated. |
| 3312 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3313 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3314 | return -ENODEV; |
| 3315 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3316 | level = I915_CACHE_LLC; |
| 3317 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3318 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3319 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3320 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3321 | default: |
| 3322 | return -EINVAL; |
| 3323 | } |
| 3324 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3325 | obj = i915_gem_object_lookup(file, args->handle); |
| 3326 | if (!obj) |
| 3327 | return -ENOENT; |
| 3328 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3329 | /* |
| 3330 | * The caching mode of proxy object is handled by its generator, and |
| 3331 | * not allowed to be changed by userspace. |
| 3332 | */ |
| 3333 | if (i915_gem_object_is_proxy(obj)) { |
| 3334 | ret = -ENXIO; |
| 3335 | goto out; |
| 3336 | } |
| 3337 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3338 | if (obj->cache_level == level) |
| 3339 | goto out; |
| 3340 | |
| 3341 | ret = i915_gem_object_wait(obj, |
| 3342 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3343 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3344 | if (ret) |
| 3345 | goto out; |
| 3346 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3347 | ret = i915_mutex_lock_interruptible(dev); |
| 3348 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3349 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3350 | |
| 3351 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3352 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3353 | |
| 3354 | out: |
| 3355 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3356 | return ret; |
| 3357 | } |
| 3358 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3359 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 3360 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 3361 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 3362 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 3363 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3364 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3365 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3366 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3367 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3368 | const struct i915_ggtt_view *view, |
| 3369 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3370 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3371 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3372 | int ret; |
| 3373 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3374 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3375 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3376 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3377 | * display coherency whilst setting up the cache domains. |
| 3378 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3379 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3380 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3381 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3382 | * a result, we make sure that the pinning that is about to occur is |
| 3383 | * done with uncached PTEs. This is lowest common denominator for all |
| 3384 | * chipsets. |
| 3385 | * |
| 3386 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3387 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3388 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3389 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3390 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3391 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3392 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3393 | if (ret) { |
| 3394 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3395 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3396 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3397 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3398 | /* As the user may map the buffer once pinned in the display plane |
| 3399 | * (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] | 3400 | * always use map_and_fenceable for all scanout buffers. However, |
| 3401 | * it may simply be too big to fit into mappable, in which case |
| 3402 | * put it anyway and hope that userspace can cope (but always first |
| 3403 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3404 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3405 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3406 | if ((flags & PIN_MAPPABLE) == 0 && |
| 3407 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3408 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3409 | flags | |
| 3410 | PIN_MAPPABLE | |
| 3411 | PIN_NONBLOCK); |
| 3412 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3413 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3414 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3415 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3416 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 3417 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 3418 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3419 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3420 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3421 | /* It should now be out of any other write domains, and we can update |
| 3422 | * the domain values for our changes. |
| 3423 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3424 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3425 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3426 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3427 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3428 | err_unpin_global: |
| 3429 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3430 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3434 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3435 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 3436 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3437 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3438 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 3439 | return; |
| 3440 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3441 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 3442 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3443 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3444 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 3445 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3446 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3447 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3448 | } |
| 3449 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3450 | /** |
| 3451 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3452 | * @obj: object to act on |
| 3453 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3454 | * |
| 3455 | * This function returns when the move is complete, including waiting on |
| 3456 | * flushes to occur. |
| 3457 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3458 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3459 | 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] | 3460 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3461 | int ret; |
| 3462 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3463 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3464 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3465 | ret = i915_gem_object_wait(obj, |
| 3466 | I915_WAIT_INTERRUPTIBLE | |
| 3467 | I915_WAIT_LOCKED | |
| 3468 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3469 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3470 | if (ret) |
| 3471 | return ret; |
| 3472 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3473 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3474 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3475 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3476 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 3477 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3478 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | /* It should now be out of any other write domains, and we can update |
| 3482 | * the domain values for our changes. |
| 3483 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3484 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3485 | |
| 3486 | /* If we're writing through the CPU, then the GPU read domains will |
| 3487 | * need to be invalidated at next use. |
| 3488 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3489 | if (write) |
| 3490 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3491 | |
| 3492 | return 0; |
| 3493 | } |
| 3494 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3495 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3496 | * emitted over 20 msec ago. |
| 3497 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3498 | * Note that if we were to use the current jiffies each time around the loop, |
| 3499 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3500 | * render a frame was over 20ms. |
| 3501 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3502 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3503 | * relatively low latency when blocking on a particular request to finish. |
| 3504 | */ |
| 3505 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3506 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3507 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3508 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3509 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 3510 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3511 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3512 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3513 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 3514 | /* ABI: return -EIO if already wedged */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 3515 | ret = i915_terminally_wedged(dev_priv); |
| 3516 | if (ret) |
| 3517 | return ret; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3518 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3519 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3520 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3521 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3522 | break; |
| 3523 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3524 | if (target) { |
| 3525 | list_del(&target->client_link); |
| 3526 | target->file_priv = NULL; |
| 3527 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 3528 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3529 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3530 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3531 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3532 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3533 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3534 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3535 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3536 | return 0; |
| 3537 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3538 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3539 | I915_WAIT_INTERRUPTIBLE, |
| 3540 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3541 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3542 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3543 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3544 | } |
| 3545 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3546 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3547 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 3548 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 3549 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 3550 | u64 alignment, |
| 3551 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3552 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3553 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 3554 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3555 | struct i915_vma *vma; |
| 3556 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 3557 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3558 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3559 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 3560 | if (flags & PIN_MAPPABLE && |
| 3561 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3562 | /* If the required space is larger than the available |
| 3563 | * aperture, we will not able to find a slot for the |
| 3564 | * object and unbinding the object now will be in |
| 3565 | * vain. Worse, doing so may cause us to ping-pong |
| 3566 | * the object in and out of the Global GTT and |
| 3567 | * waste a lot of cycles under the mutex. |
| 3568 | */ |
| 3569 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 3570 | return ERR_PTR(-E2BIG); |
| 3571 | |
| 3572 | /* If NONBLOCK is set the caller is optimistically |
| 3573 | * trying to cache the full object within the mappable |
| 3574 | * aperture, and *must* have a fallback in place for |
| 3575 | * situations where we cannot bind the object. We |
| 3576 | * can be a little more lax here and use the fallback |
| 3577 | * more often to avoid costly migrations of ourselves |
| 3578 | * and other objects within the aperture. |
| 3579 | * |
| 3580 | * Half-the-aperture is used as a simple heuristic. |
| 3581 | * More interesting would to do search for a free |
| 3582 | * block prior to making the commitment to unbind. |
| 3583 | * That caters for the self-harm case, and with a |
| 3584 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 3585 | * we could try to minimise harm to others. |
| 3586 | */ |
| 3587 | if (flags & PIN_NONBLOCK && |
| 3588 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 3589 | return ERR_PTR(-ENOSPC); |
| 3590 | } |
| 3591 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 3592 | vma = i915_vma_instance(obj, vm, view); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 3593 | if (IS_ERR(vma)) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3594 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3595 | |
| 3596 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3597 | if (flags & PIN_NONBLOCK) { |
| 3598 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 3599 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3600 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3601 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 3602 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3603 | return ERR_PTR(-ENOSPC); |
| 3604 | } |
| 3605 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3606 | WARN(i915_vma_is_pinned(vma), |
| 3607 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3608 | " offset=%08x, req.alignment=%llx," |
| 3609 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 3610 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3611 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3612 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3613 | ret = i915_vma_unbind(vma); |
| 3614 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3615 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3616 | } |
| 3617 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3618 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 3619 | if (ret) |
| 3620 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3621 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3622 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3625 | static __always_inline u32 __busy_read_flag(u8 id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3626 | { |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3627 | if (id == (u8)I915_ENGINE_CLASS_INVALID) |
| 3628 | return 0xffff0000u; |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3629 | |
| 3630 | GEM_BUG_ON(id >= 16); |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3631 | return 0x10000u << id; |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3632 | } |
| 3633 | |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3634 | static __always_inline u32 __busy_write_id(u8 id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3635 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3636 | /* |
| 3637 | * The uABI guarantees an active writer is also amongst the read |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 3638 | * engines. This would be true if we accessed the activity tracking |
| 3639 | * under the lock, but as we perform the lookup of the object and |
| 3640 | * its activity locklessly we can not guarantee that the last_write |
| 3641 | * being active implies that we have set the same engine flag from |
| 3642 | * last_read - hence we always set both read and write busy for |
| 3643 | * last_write. |
| 3644 | */ |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3645 | if (id == (u8)I915_ENGINE_CLASS_INVALID) |
| 3646 | return 0xffffffffu; |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3647 | |
| 3648 | return (id + 1) | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3649 | } |
| 3650 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3651 | static __always_inline unsigned int |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3652 | __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] | 3653 | { |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3654 | const struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3655 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3656 | /* |
| 3657 | * 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] | 3658 | * guarantees forward progress. We could rely on the idle worker |
| 3659 | * to eventually flush us, but to minimise latency just ask the |
| 3660 | * hardware. |
| 3661 | * |
| 3662 | * Note we only report on the status of native fences. |
| 3663 | */ |
| 3664 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3665 | return 0; |
| 3666 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3667 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3668 | rq = container_of(fence, const struct i915_request, fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3669 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3670 | return 0; |
| 3671 | |
Chris Wilson | 6960d9c | 2019-04-04 11:19:14 +0100 | [diff] [blame] | 3672 | /* Beware type-expansion follies! */ |
| 3673 | BUILD_BUG_ON(!typecheck(u8, rq->engine->uabi_class)); |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3674 | return flag(rq->engine->uabi_class); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3675 | } |
| 3676 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3677 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3678 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3679 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3680 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3681 | } |
| 3682 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3683 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3684 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3685 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3686 | if (!fence) |
| 3687 | return 0; |
| 3688 | |
| 3689 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3690 | } |
| 3691 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3692 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3693 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3694 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3695 | { |
| 3696 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3697 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3698 | struct reservation_object_list *list; |
| 3699 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3700 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3701 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3702 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3703 | rcu_read_lock(); |
| 3704 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3705 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3706 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3707 | |
Chris Wilson | c8b5024 | 2019-03-05 16:26:43 +0000 | [diff] [blame] | 3708 | /* |
| 3709 | * A discrepancy here is that we do not report the status of |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3710 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 3711 | * a call to set-domain may still stall waiting for foreign rendering. |
| 3712 | * This also means that wait-ioctl may report an object as busy, |
| 3713 | * where busy-ioctl considers it idle. |
| 3714 | * |
| 3715 | * We trade the ability to warn of foreign fences to report on which |
| 3716 | * i915 engines are active for the object. |
| 3717 | * |
| 3718 | * Alternatively, we can trade that extra information on read/write |
| 3719 | * activity with |
| 3720 | * args->busy = |
| 3721 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 3722 | * to report the overall busyness. This is what the wait-ioctl does. |
| 3723 | * |
| 3724 | */ |
| 3725 | retry: |
| 3726 | seq = raw_read_seqcount(&obj->resv->seq); |
| 3727 | |
| 3728 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 3729 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 3730 | |
| 3731 | /* Translate shared fences to READ set of engines */ |
| 3732 | list = rcu_dereference(obj->resv->fence); |
| 3733 | if (list) { |
| 3734 | unsigned int shared_count = list->shared_count, i; |
| 3735 | |
| 3736 | for (i = 0; i < shared_count; ++i) { |
| 3737 | struct dma_fence *fence = |
| 3738 | rcu_dereference(list->shared[i]); |
| 3739 | |
| 3740 | args->busy |= busy_check_reader(fence); |
| 3741 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3742 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3743 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3744 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 3745 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 3746 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3747 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3748 | out: |
| 3749 | rcu_read_unlock(); |
| 3750 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3751 | } |
| 3752 | |
| 3753 | int |
| 3754 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 3755 | struct drm_file *file_priv) |
| 3756 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 3757 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3758 | } |
| 3759 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3760 | int |
| 3761 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 3762 | struct drm_file *file_priv) |
| 3763 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3764 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3765 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3766 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3767 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3768 | |
| 3769 | switch (args->madv) { |
| 3770 | case I915_MADV_DONTNEED: |
| 3771 | case I915_MADV_WILLNEED: |
| 3772 | break; |
| 3773 | default: |
| 3774 | return -EINVAL; |
| 3775 | } |
| 3776 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3777 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3778 | if (!obj) |
| 3779 | return -ENOENT; |
| 3780 | |
| 3781 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 3782 | if (err) |
| 3783 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3784 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 3785 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 3786 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 3787 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3788 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 3789 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3790 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3791 | obj->mm.quirked = false; |
| 3792 | } |
| 3793 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 3794 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3795 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 3796 | obj->mm.quirked = true; |
| 3797 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 3798 | } |
| 3799 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3800 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 3801 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3802 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 3803 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 3804 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 3805 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d6692e | 2019-04-20 12:55:39 +0100 | [diff] [blame] | 3806 | __i915_gem_object_truncate(obj); |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 3807 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3808 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3809 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 3810 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3811 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 3812 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3813 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3814 | } |
| 3815 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3816 | static void |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3817 | frontbuffer_retire(struct i915_active_request *active, |
| 3818 | struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3819 | { |
| 3820 | struct drm_i915_gem_object *obj = |
| 3821 | container_of(active, typeof(*obj), frontbuffer_write); |
| 3822 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 3823 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3826 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 3827 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3828 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 3829 | mutex_init(&obj->mm.lock); |
| 3830 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3831 | spin_lock_init(&obj->vma.lock); |
| 3832 | INIT_LIST_HEAD(&obj->vma.list); |
| 3833 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3834 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 3835 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3836 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 3837 | init_rcu_head(&obj->rcu); |
| 3838 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3839 | obj->ops = ops; |
| 3840 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3841 | reservation_object_init(&obj->__builtin_resv); |
| 3842 | obj->resv = &obj->__builtin_resv; |
| 3843 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 3844 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3845 | i915_active_request_init(&obj->frontbuffer_write, |
| 3846 | NULL, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3847 | |
| 3848 | obj->mm.madv = I915_MADV_WILLNEED; |
| 3849 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 3850 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3851 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 3852 | 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] | 3853 | } |
| 3854 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3855 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 3856 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 3857 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 3858 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3859 | .get_pages = i915_gem_object_get_pages_gtt, |
| 3860 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 3861 | |
| 3862 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3863 | }; |
| 3864 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 3865 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 3866 | struct drm_gem_object *obj, |
| 3867 | size_t size) |
| 3868 | { |
| 3869 | struct drm_i915_private *i915 = to_i915(dev); |
| 3870 | unsigned long flags = VM_NORESERVE; |
| 3871 | struct file *filp; |
| 3872 | |
| 3873 | drm_gem_private_object_init(dev, obj, size); |
| 3874 | |
| 3875 | if (i915->mm.gemfs) |
| 3876 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 3877 | flags); |
| 3878 | else |
| 3879 | filp = shmem_file_setup("i915", size, flags); |
| 3880 | |
| 3881 | if (IS_ERR(filp)) |
| 3882 | return PTR_ERR(filp); |
| 3883 | |
| 3884 | obj->filp = filp; |
| 3885 | |
| 3886 | return 0; |
| 3887 | } |
| 3888 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 3889 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 3890 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 3891 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3892 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 3893 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3894 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 3895 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3896 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3897 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 3898 | /* There is a prevalence of the assumption that we fit the object's |
| 3899 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 3900 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 3901 | * such a local variable, please consider fixing! |
| 3902 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 3903 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 3904 | return ERR_PTR(-E2BIG); |
| 3905 | |
| 3906 | if (overflows_type(size, obj->base.size)) |
| 3907 | return ERR_PTR(-E2BIG); |
| 3908 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 3909 | obj = i915_gem_object_alloc(); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3910 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3911 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3912 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 3913 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3914 | if (ret) |
| 3915 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3916 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 3917 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 3918 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 3919 | /* 965gm cannot relocate objects above 4GiB. */ |
| 3920 | mask &= ~__GFP_HIGHMEM; |
| 3921 | mask |= __GFP_DMA32; |
| 3922 | } |
| 3923 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 3924 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 3925 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 3926 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 3927 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3928 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 3929 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3930 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 3931 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3932 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3933 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 3934 | /* 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] | 3935 | * cache) for about a 10% performance improvement |
| 3936 | * compared to uncached. Graphics requests other than |
| 3937 | * display scanout are coherent with the CPU in |
| 3938 | * accessing this cache. This means in this mode we |
| 3939 | * don't need to clflush on the CPU side, and on the |
| 3940 | * GPU side we only need to flush internal caches to |
| 3941 | * get data visible to the CPU. |
| 3942 | * |
| 3943 | * However, we maintain the display planes as UC, and so |
| 3944 | * need to rebind when first used as such. |
| 3945 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3946 | cache_level = I915_CACHE_LLC; |
| 3947 | else |
| 3948 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 3949 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3950 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3951 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 3952 | trace_i915_gem_object_create(obj); |
| 3953 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3954 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3955 | |
| 3956 | fail: |
| 3957 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 3958 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 3961 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 3962 | { |
| 3963 | /* If we are the last user of the backing storage (be it shmemfs |
| 3964 | * pages or stolen etc), we know that the pages are going to be |
| 3965 | * immediately released. In this case, we can then skip copying |
| 3966 | * back the contents from the GPU. |
| 3967 | */ |
| 3968 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3969 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 3970 | return false; |
| 3971 | |
| 3972 | if (obj->base.filp == NULL) |
| 3973 | return true; |
| 3974 | |
| 3975 | /* At first glance, this looks racy, but then again so would be |
| 3976 | * userspace racing mmap against close. However, the first external |
| 3977 | * reference to the filp can only be obtained through the |
| 3978 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 3979 | * acquiring such a reference whilst we are in the middle of |
| 3980 | * freeing the object. |
| 3981 | */ |
| 3982 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 3983 | } |
| 3984 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3985 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 3986 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 3987 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3988 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 3989 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 3990 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 3991 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 3992 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3993 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 3994 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3995 | trace_i915_gem_object_destroy(obj); |
| 3996 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 3997 | mutex_lock(&i915->drm.struct_mutex); |
| 3998 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3999 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4000 | list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4001 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4002 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4003 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4004 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4005 | GEM_BUG_ON(!list_empty(&obj->vma.list)); |
| 4006 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4007 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4008 | /* This serializes freeing with the shrinker. Since the free |
| 4009 | * is delayed, first by RCU then by the workqueue, we want the |
| 4010 | * shrinker to be able to free pages of unreferenced objects, |
| 4011 | * or else we may oom whilst there are plenty of deferred |
| 4012 | * freed objects. |
| 4013 | */ |
| 4014 | if (i915_gem_object_has_pages(obj)) { |
| 4015 | spin_lock(&i915->mm.obj_lock); |
| 4016 | list_del_init(&obj->mm.link); |
| 4017 | spin_unlock(&i915->mm.obj_lock); |
| 4018 | } |
| 4019 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4020 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4021 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4022 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4023 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4024 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4025 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4026 | |
| 4027 | if (obj->ops->release) |
| 4028 | obj->ops->release(obj); |
| 4029 | |
| 4030 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4031 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4032 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4033 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4034 | |
| 4035 | if (obj->base.import_attach) |
| 4036 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4037 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4038 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4039 | drm_gem_object_release(&obj->base); |
| 4040 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4041 | |
Andy Shevchenko | 6e514e3 | 2019-03-04 11:29:08 +0200 | [diff] [blame] | 4042 | bitmap_free(obj->bit_17); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4043 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4044 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4045 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4046 | atomic_dec(&i915->mm.free_count); |
| 4047 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4048 | if (on) |
| 4049 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4050 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4051 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4055 | { |
| 4056 | struct llist_node *freed; |
| 4057 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4058 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4059 | freed = NULL; |
| 4060 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4061 | /* Only one consumer of llist_del_first() allowed */ |
| 4062 | spin_lock(&i915->mm.free_lock); |
| 4063 | freed = llist_del_first(&i915->mm.free_list); |
| 4064 | spin_unlock(&i915->mm.free_lock); |
| 4065 | } |
| 4066 | if (unlikely(freed)) { |
| 4067 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4068 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4069 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | static void __i915_gem_free_work(struct work_struct *work) |
| 4073 | { |
| 4074 | struct drm_i915_private *i915 = |
| 4075 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4076 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4077 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4078 | /* |
| 4079 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4080 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4081 | * However, the object may also be bound into the global GTT (e.g. |
| 4082 | * older GPUs without per-process support, or for direct access through |
| 4083 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4084 | * unbound now. |
| 4085 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4086 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4087 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4088 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4089 | spin_unlock(&i915->mm.free_lock); |
| 4090 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4091 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4092 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4093 | return; |
| 4094 | |
| 4095 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4096 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4097 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4101 | { |
| 4102 | struct drm_i915_gem_object *obj = |
| 4103 | container_of(head, typeof(*obj), rcu); |
| 4104 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4105 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4106 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4107 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4108 | * it like a rcu_head from this point forwards. And we expect all |
| 4109 | * objects to be freed via this path. |
| 4110 | */ |
| 4111 | destroy_rcu_head(&obj->rcu); |
| 4112 | |
| 4113 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4114 | * Since we require blocking on struct_mutex to unbind the freed |
| 4115 | * object from the GPU before releasing resources back to the |
| 4116 | * system, we can not do that directly from the RCU callback (which may |
| 4117 | * be a softirq context), but must instead then defer that work onto a |
| 4118 | * kthread. We use the RCU callback rather than move the freed object |
| 4119 | * directly onto the work queue so that we can mix between using the |
| 4120 | * worker and performing frees directly from subsequent allocations for |
| 4121 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4122 | */ |
| 4123 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4124 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4128 | { |
| 4129 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4130 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4131 | if (obj->mm.quirked) |
| 4132 | __i915_gem_object_unpin_pages(obj); |
| 4133 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4134 | if (discard_backing_storage(obj)) |
| 4135 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4136 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4137 | /* |
| 4138 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4139 | * read-side critical sections are complete, e.g. |
| 4140 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4141 | * lookup see i915_gem_object_lookup_rcu(). |
| 4142 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4143 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4144 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4145 | } |
| 4146 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4147 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4148 | { |
| 4149 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4150 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4151 | if (!i915_gem_object_has_active_reference(obj) && |
| 4152 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4153 | i915_gem_object_set_active_reference(obj); |
| 4154 | else |
| 4155 | i915_gem_object_put(obj); |
| 4156 | } |
| 4157 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4158 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4159 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4160 | intel_wakeref_t wakeref; |
| 4161 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4162 | GEM_TRACE("\n"); |
| 4163 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4164 | wakeref = intel_runtime_pm_get(i915); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4165 | intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4166 | |
| 4167 | /* |
| 4168 | * As we have just resumed the machine and woken the device up from |
| 4169 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4170 | * back to defaults, recovering from whatever wedged state we left it |
| 4171 | * in and so worth trying to use the device once more. |
| 4172 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4173 | if (i915_terminally_wedged(i915)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4174 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4175 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4176 | /* |
| 4177 | * If we inherit context state from the BIOS or earlier occupants |
| 4178 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4179 | * try to take over. The only way to remove the earlier state |
| 4180 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4181 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4182 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4183 | */ |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 4184 | intel_gt_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4185 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4186 | intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4187 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4188 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4189 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4190 | i915_gem_contexts_lost(i915); |
| 4191 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4194 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4195 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4196 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4197 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4198 | return; |
| 4199 | |
| 4200 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4201 | DISP_TILE_SURFACE_SWIZZLING); |
| 4202 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4203 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4204 | return; |
| 4205 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4206 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4207 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4208 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4209 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4210 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4211 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4212 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4213 | else |
| 4214 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4215 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4216 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4217 | 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] | 4218 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4219 | I915_WRITE(RING_CTL(base), 0); |
| 4220 | I915_WRITE(RING_HEAD(base), 0); |
| 4221 | I915_WRITE(RING_TAIL(base), 0); |
| 4222 | I915_WRITE(RING_START(base), 0); |
| 4223 | } |
| 4224 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4225 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4226 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4227 | if (IS_I830(dev_priv)) { |
| 4228 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4229 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4230 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4231 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4232 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4233 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4234 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4235 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4236 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4237 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4238 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4242 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4243 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4244 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4245 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4246 | dev_priv->gt.last_init_time = ktime_get(); |
| 4247 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4248 | /* Double layer security blanket, see i915_gem_init() */ |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4249 | intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4250 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4251 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4252 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4253 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4254 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4255 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4256 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4257 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4258 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4259 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4260 | /* ...and determine whether they are sticking. */ |
| 4261 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 4262 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4263 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4264 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4265 | /* |
| 4266 | * At least 830 can leave some of the unused rings |
| 4267 | * "active" (ie. head != tail) after resume which |
| 4268 | * will prevent c3 entry. Makes sure all unused rings |
| 4269 | * are totally idle. |
| 4270 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4271 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4272 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4273 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4274 | ret = i915_terminally_wedged(dev_priv); |
| 4275 | if (ret) |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4276 | goto out; |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4277 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4278 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4279 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4280 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4281 | goto out; |
| 4282 | } |
| 4283 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 4284 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 4285 | if (ret) { |
| 4286 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 4287 | goto out; |
| 4288 | } |
| 4289 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4290 | /* We can't enable contexts until all firmware is loaded */ |
| 4291 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4292 | if (ret) { |
| 4293 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4294 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4295 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4296 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4297 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 4298 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 4299 | /* Only when the HW is re-initialised, can we replay the requests */ |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 4300 | ret = intel_engines_resume(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4301 | if (ret) |
| 4302 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4303 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4304 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4305 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 4306 | intel_engines_set_scheduler_caps(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4307 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4308 | |
| 4309 | cleanup_uc: |
| 4310 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4311 | out: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4312 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4313 | |
| 4314 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4315 | } |
| 4316 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4317 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 4318 | { |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4319 | struct intel_engine_cs *engine; |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 4320 | struct i915_gem_context *ctx; |
| 4321 | struct i915_gem_engines *e; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4322 | enum intel_engine_id id; |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4323 | int err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4324 | |
| 4325 | /* |
| 4326 | * As we reset the gpu during very early sanitisation, the current |
| 4327 | * register state on the GPU should reflect its defaults values. |
| 4328 | * We load a context onto the hw (with restore-inhibit), then switch |
| 4329 | * over to a second context to save that default register state. We |
| 4330 | * can then prime every new context with that state so they all start |
| 4331 | * from the same default HW values. |
| 4332 | */ |
| 4333 | |
| 4334 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 4335 | if (IS_ERR(ctx)) |
| 4336 | return PTR_ERR(ctx); |
| 4337 | |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 4338 | e = i915_gem_context_lock_engines(ctx); |
| 4339 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4340 | for_each_engine(engine, i915, id) { |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 4341 | struct intel_context *ce = e->engines[id]; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4342 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4343 | |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 4344 | rq = intel_context_create_request(ce); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4345 | if (IS_ERR(rq)) { |
| 4346 | err = PTR_ERR(rq); |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 4347 | goto err_active; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 4350 | err = 0; |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 4351 | if (rq->engine->init_context) |
| 4352 | err = rq->engine->init_context(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4353 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 4354 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4355 | if (err) |
| 4356 | goto err_active; |
| 4357 | } |
| 4358 | |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4359 | /* Flush the default context image to memory, and enable powersaving. */ |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 4360 | if (!i915_gem_load_power_context(i915)) { |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4361 | err = -EIO; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4362 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4363 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4364 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4365 | for_each_engine(engine, i915, id) { |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 4366 | struct intel_context *ce = e->engines[id]; |
| 4367 | struct i915_vma *state = ce->state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4368 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4369 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4370 | if (!state) |
| 4371 | continue; |
| 4372 | |
Chris Wilson | 0881954 | 2019-03-08 13:25:22 +0000 | [diff] [blame] | 4373 | GEM_BUG_ON(intel_context_is_pinned(ce)); |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 4374 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4375 | /* |
| 4376 | * As we will hold a reference to the logical state, it will |
| 4377 | * not be torn down with the context, and importantly the |
| 4378 | * object will hold onto its vma (making it possible for a |
| 4379 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 4380 | * from the GTT to prevent such accidents and reclaim the |
| 4381 | * space. |
| 4382 | */ |
| 4383 | err = i915_vma_unbind(state); |
| 4384 | if (err) |
| 4385 | goto err_active; |
| 4386 | |
| 4387 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 4388 | if (err) |
| 4389 | goto err_active; |
| 4390 | |
| 4391 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 4392 | i915_gem_object_set_cache_coherency(engine->default_state, |
| 4393 | I915_CACHE_LLC); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4394 | |
| 4395 | /* Check we can acquire the image of the context state */ |
| 4396 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4397 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4398 | if (IS_ERR(vaddr)) { |
| 4399 | err = PTR_ERR(vaddr); |
| 4400 | goto err_active; |
| 4401 | } |
| 4402 | |
| 4403 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 4407 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 4408 | |
| 4409 | /* |
| 4410 | * Make sure that classes with multiple engine instances all |
| 4411 | * share the same basic configuration. |
| 4412 | */ |
| 4413 | for_each_engine(engine, i915, id) { |
| 4414 | unsigned int bit = BIT(engine->uabi_class); |
| 4415 | unsigned int expected = engine->default_state ? bit : 0; |
| 4416 | |
| 4417 | if ((found & bit) != expected) { |
| 4418 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 4419 | engine->uabi_class, engine->name); |
| 4420 | } |
| 4421 | } |
| 4422 | } |
| 4423 | |
| 4424 | out_ctx: |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 4425 | i915_gem_context_unlock_engines(ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4426 | i915_gem_context_set_closed(ctx); |
| 4427 | i915_gem_context_put(ctx); |
| 4428 | return err; |
| 4429 | |
| 4430 | err_active: |
| 4431 | /* |
| 4432 | * 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] | 4433 | * and ready to be torn-down. The quickest way we can accomplish |
| 4434 | * this is by declaring ourselves wedged. |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4435 | */ |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 4436 | i915_gem_set_wedged(i915); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4437 | goto out_ctx; |
| 4438 | } |
| 4439 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4440 | static int |
| 4441 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 4442 | { |
| 4443 | struct drm_i915_gem_object *obj; |
| 4444 | struct i915_vma *vma; |
| 4445 | int ret; |
| 4446 | |
| 4447 | obj = i915_gem_object_create_stolen(i915, size); |
| 4448 | if (!obj) |
| 4449 | obj = i915_gem_object_create_internal(i915, size); |
| 4450 | if (IS_ERR(obj)) { |
| 4451 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 4452 | return PTR_ERR(obj); |
| 4453 | } |
| 4454 | |
| 4455 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 4456 | if (IS_ERR(vma)) { |
| 4457 | ret = PTR_ERR(vma); |
| 4458 | goto err_unref; |
| 4459 | } |
| 4460 | |
| 4461 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 4462 | if (ret) |
| 4463 | goto err_unref; |
| 4464 | |
| 4465 | i915->gt.scratch = vma; |
| 4466 | return 0; |
| 4467 | |
| 4468 | err_unref: |
| 4469 | i915_gem_object_put(obj); |
| 4470 | return ret; |
| 4471 | } |
| 4472 | |
| 4473 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 4474 | { |
| 4475 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 4476 | } |
| 4477 | |
Chris Wilson | 254e118 | 2019-04-17 08:56:28 +0100 | [diff] [blame] | 4478 | static int intel_engines_verify_workarounds(struct drm_i915_private *i915) |
| 4479 | { |
| 4480 | struct intel_engine_cs *engine; |
| 4481 | enum intel_engine_id id; |
| 4482 | int err = 0; |
| 4483 | |
| 4484 | if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) |
| 4485 | return 0; |
| 4486 | |
| 4487 | for_each_engine(engine, i915, id) { |
| 4488 | if (intel_engine_verify_workarounds(engine, "load")) |
| 4489 | err = -EIO; |
| 4490 | } |
| 4491 | |
| 4492 | return err; |
| 4493 | } |
| 4494 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4495 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4496 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4497 | int ret; |
| 4498 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 4499 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 4500 | 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] | 4501 | mkwrite_device_info(dev_priv)->page_sizes = |
| 4502 | I915_GTT_PAGE_SIZE_4K; |
| 4503 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 4504 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4505 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4506 | i915_timelines_init(dev_priv); |
| 4507 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4508 | ret = i915_gem_init_userptr(dev_priv); |
| 4509 | if (ret) |
| 4510 | return ret; |
| 4511 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4512 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 4513 | if (ret) |
| 4514 | return ret; |
| 4515 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4516 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 4517 | if (ret) |
| 4518 | goto err_uc_misc; |
| 4519 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4520 | /* This is just a security blanket to placate dragons. |
| 4521 | * On some systems, we very sporadically observe that the first TLBs |
| 4522 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 4523 | * we hold the forcewake during initialisation these problems |
| 4524 | * just magically go away. |
| 4525 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4526 | mutex_lock(&dev_priv->drm.struct_mutex); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4527 | intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4528 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 4529 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4530 | if (ret) { |
| 4531 | GEM_BUG_ON(ret == -EIO); |
| 4532 | goto err_unlock; |
| 4533 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 4534 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4535 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4536 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4537 | if (ret) { |
| 4538 | GEM_BUG_ON(ret == -EIO); |
| 4539 | goto err_ggtt; |
| 4540 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 4541 | |
Chris Wilson | 11334c6 | 2019-04-26 17:33:33 +0100 | [diff] [blame] | 4542 | ret = intel_engines_setup(dev_priv); |
| 4543 | if (ret) { |
| 4544 | GEM_BUG_ON(ret == -EIO); |
| 4545 | goto err_unlock; |
| 4546 | } |
| 4547 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4548 | ret = i915_gem_contexts_init(dev_priv); |
| 4549 | if (ret) { |
| 4550 | GEM_BUG_ON(ret == -EIO); |
| 4551 | goto err_scratch; |
| 4552 | } |
| 4553 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4554 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4555 | if (ret) { |
| 4556 | GEM_BUG_ON(ret == -EIO); |
| 4557 | goto err_context; |
| 4558 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 4559 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 4560 | intel_init_gt_powersave(dev_priv); |
| 4561 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4562 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4563 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4564 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4565 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4566 | ret = i915_gem_init_hw(dev_priv); |
| 4567 | if (ret) |
| 4568 | goto err_uc_init; |
| 4569 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 4570 | /* |
| 4571 | * Despite its name intel_init_clock_gating applies both display |
| 4572 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 4573 | * GT power context workaround. Worse, sometimes it includes a context |
| 4574 | * register workaround which we need to apply before we record the |
| 4575 | * default HW state for all contexts. |
| 4576 | * |
| 4577 | * FIXME: break up the workarounds and apply them at the right time! |
| 4578 | */ |
| 4579 | intel_init_clock_gating(dev_priv); |
| 4580 | |
Chris Wilson | 254e118 | 2019-04-17 08:56:28 +0100 | [diff] [blame] | 4581 | ret = intel_engines_verify_workarounds(dev_priv); |
| 4582 | if (ret) |
| 4583 | goto err_init_hw; |
| 4584 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4585 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4586 | if (ret) |
| 4587 | goto err_init_hw; |
| 4588 | |
| 4589 | if (i915_inject_load_failure()) { |
| 4590 | ret = -ENODEV; |
| 4591 | goto err_init_hw; |
| 4592 | } |
| 4593 | |
| 4594 | if (i915_inject_load_failure()) { |
| 4595 | ret = -EIO; |
| 4596 | goto err_init_hw; |
| 4597 | } |
| 4598 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4599 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4600 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4601 | |
| 4602 | return 0; |
| 4603 | |
| 4604 | /* |
| 4605 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 4606 | * disable GPU submission but keep KMS alive. We want to mark the |
| 4607 | * HW as irrevisibly wedged, but keep enough state around that the |
| 4608 | * driver doesn't explode during runtime. |
| 4609 | */ |
| 4610 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4611 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4612 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 4613 | i915_gem_set_wedged(dev_priv); |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 4614 | i915_gem_suspend(dev_priv); |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4615 | i915_gem_suspend_late(dev_priv); |
| 4616 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 4617 | i915_gem_drain_workqueue(dev_priv); |
| 4618 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 4619 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4620 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 4621 | err_uc_init: |
| 4622 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4623 | err_pm: |
| 4624 | if (ret != -EIO) { |
| 4625 | intel_cleanup_gt_powersave(dev_priv); |
Chris Wilson | 45b9c96 | 2019-05-01 11:32:04 +0100 | [diff] [blame] | 4626 | intel_engines_cleanup(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4627 | } |
| 4628 | err_context: |
| 4629 | if (ret != -EIO) |
| 4630 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4631 | err_scratch: |
| 4632 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4633 | err_ggtt: |
| 4634 | err_unlock: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 4635 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4636 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4637 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4638 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4639 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 4640 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4641 | if (ret != -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4642 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4643 | i915_timelines_fini(dev_priv); |
| 4644 | } |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4645 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4646 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 4647 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 4648 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4649 | /* |
| 4650 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4651 | * wedged. But we only want to do this where the GPU is angry, |
| 4652 | * for all other failure, such as an allocation failure, bail. |
| 4653 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4654 | if (!i915_reset_failed(dev_priv)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 4655 | i915_load_error(dev_priv, |
| 4656 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4657 | i915_gem_set_wedged(dev_priv); |
| 4658 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 4659 | |
| 4660 | /* Minimal basic recovery for KMS */ |
| 4661 | ret = i915_ggtt_enable_hw(dev_priv); |
| 4662 | i915_gem_restore_gtt_mappings(dev_priv); |
| 4663 | i915_gem_restore_fences(dev_priv); |
| 4664 | intel_init_clock_gating(dev_priv); |
| 4665 | |
| 4666 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4667 | } |
| 4668 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4669 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 4670 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4671 | } |
| 4672 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4673 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 4674 | { |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 4675 | GEM_BUG_ON(dev_priv->gt.awake); |
| 4676 | |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame^] | 4677 | intel_wakeref_auto_fini(&dev_priv->mm.userfault_wakeref); |
| 4678 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4679 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 4680 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4681 | |
| 4682 | /* Flush any outstanding unpin_work. */ |
| 4683 | i915_gem_drain_workqueue(dev_priv); |
| 4684 | |
| 4685 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 4686 | intel_uc_fini_hw(dev_priv); |
| 4687 | intel_uc_fini(dev_priv); |
Chris Wilson | 45b9c96 | 2019-05-01 11:32:04 +0100 | [diff] [blame] | 4688 | intel_engines_cleanup(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4689 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4690 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4691 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 4692 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4693 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 4694 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 4695 | intel_cleanup_gt_powersave(dev_priv); |
| 4696 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4697 | intel_uc_fini_misc(dev_priv); |
| 4698 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4699 | i915_timelines_fini(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 4700 | |
| 4701 | i915_gem_drain_freed_objects(dev_priv); |
| 4702 | |
| 4703 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 4704 | } |
| 4705 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4706 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 4707 | { |
| 4708 | i915_gem_sanitize(i915); |
| 4709 | } |
| 4710 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4711 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4712 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 4713 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 4714 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4715 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 4716 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4717 | !IS_CHERRYVIEW(dev_priv)) |
| 4718 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 4719 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 4720 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 4721 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4722 | dev_priv->num_fence_regs = 16; |
| 4723 | else |
| 4724 | dev_priv->num_fence_regs = 8; |
| 4725 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 4726 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4727 | dev_priv->num_fence_regs = |
| 4728 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 4729 | |
| 4730 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 4731 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 4732 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 4733 | |
| 4734 | fence->i915 = dev_priv; |
| 4735 | fence->id = i; |
| 4736 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 4737 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 4738 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4739 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 4740 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 4741 | } |
| 4742 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 4743 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 4744 | { |
| 4745 | spin_lock_init(&i915->mm.object_stat_lock); |
| 4746 | spin_lock_init(&i915->mm.obj_lock); |
| 4747 | spin_lock_init(&i915->mm.free_lock); |
| 4748 | |
| 4749 | init_llist_head(&i915->mm.free_list); |
| 4750 | |
| 4751 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 4752 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 4753 | INIT_LIST_HEAD(&i915->mm.fence_list); |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame^] | 4754 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 4755 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame^] | 4756 | intel_wakeref_auto_init(&i915->mm.userfault_wakeref, i915); |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 4757 | |
| 4758 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 4759 | } |
| 4760 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 4761 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4762 | { |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 4763 | int err; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4764 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 4765 | intel_gt_pm_init(dev_priv); |
| 4766 | |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 4767 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4768 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 4769 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 4770 | i915_gem_init__mm(dev_priv); |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 4771 | i915_gem_init__pm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4772 | |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 4773 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 4774 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 4775 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 4776 | init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4777 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 4778 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 4779 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 4780 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 4781 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4782 | err = i915_gemfs_init(dev_priv); |
| 4783 | if (err) |
| 4784 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 4785 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 4786 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4787 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4788 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 4789 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 4790 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 4791 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4792 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 4793 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 4794 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 4795 | |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 4796 | cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
| 4797 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4798 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 4799 | } |
| 4800 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 4801 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 4802 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 4803 | /* Discard all purgeable objects, let userspace recover those as |
| 4804 | * required after resuming. |
| 4805 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 4806 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 4807 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 4808 | return 0; |
| 4809 | } |
| 4810 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4811 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4812 | { |
| 4813 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 4814 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4815 | &i915->mm.unbound_list, |
| 4816 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 4817 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4818 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4819 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4820 | /* |
| 4821 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4822 | * |
| 4823 | * We need to update the domain tracking to reflect that the CPU |
| 4824 | * will be accessing all the pages to create and restore from the |
| 4825 | * hibernation, and so upon restoration those pages will be in the |
| 4826 | * CPU domain. |
| 4827 | * |
| 4828 | * To make sure the hibernation image contains the latest state, |
| 4829 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 4830 | * |
| 4831 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 4832 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4833 | */ |
| 4834 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4835 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 4836 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4837 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4838 | mutex_lock(&i915->drm.struct_mutex); |
| 4839 | for (phase = phases; *phase; phase++) { |
| 4840 | list_for_each_entry(obj, *phase, mm.link) |
| 4841 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4842 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 4843 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 4844 | |
| 4845 | return 0; |
| 4846 | } |
| 4847 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4848 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4849 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4850 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4851 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4852 | |
| 4853 | /* Clean up our request list when the client is going away, so that |
| 4854 | * later retire_requests won't dereference our soon-to-be-gone |
| 4855 | * file_priv. |
| 4856 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4857 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4858 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4859 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4860 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4861 | } |
| 4862 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4863 | 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] | 4864 | { |
| 4865 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 4866 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4867 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 4868 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4869 | |
| 4870 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 4871 | if (!file_priv) |
| 4872 | return -ENOMEM; |
| 4873 | |
| 4874 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4875 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 4876 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4877 | |
| 4878 | spin_lock_init(&file_priv->mm.lock); |
| 4879 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4880 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 4881 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 4882 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 4883 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 4884 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 4885 | if (ret) |
| 4886 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4887 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 4888 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 4889 | } |
| 4890 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 4891 | /** |
| 4892 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 4893 | * @old: current GEM buffer for the frontbuffer slots |
| 4894 | * @new: new GEM buffer for the frontbuffer slots |
| 4895 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 4896 | * |
| 4897 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 4898 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 4899 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4900 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 4901 | struct drm_i915_gem_object *new, |
| 4902 | unsigned frontbuffer_bits) |
| 4903 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 4904 | /* Control of individual bits within the mask are guarded by |
| 4905 | * the owning plane->mutex, i.e. we can never see concurrent |
| 4906 | * manipulation of individual bits. But since the bitfield as a whole |
| 4907 | * is updated using RMW, we need to use atomics in order to update |
| 4908 | * the bits. |
| 4909 | */ |
| 4910 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 4911 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 4912 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4913 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 4914 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 4915 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 4919 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 4920 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4921 | } |
| 4922 | } |
| 4923 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4924 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 4925 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4926 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4927 | const void *data, size_t size) |
| 4928 | { |
| 4929 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4930 | struct file *file; |
| 4931 | size_t offset; |
| 4932 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4933 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4934 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4935 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4936 | return obj; |
| 4937 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4938 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4939 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4940 | file = obj->base.filp; |
| 4941 | offset = 0; |
| 4942 | do { |
| 4943 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 4944 | struct page *page; |
| 4945 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4946 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4947 | err = pagecache_write_begin(file, file->f_mapping, |
| 4948 | offset, len, 0, |
| 4949 | &page, &pgdata); |
| 4950 | if (err < 0) |
| 4951 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4952 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4953 | vaddr = kmap(page); |
| 4954 | memcpy(vaddr, data, len); |
| 4955 | kunmap(page); |
| 4956 | |
| 4957 | err = pagecache_write_end(file, file->f_mapping, |
| 4958 | offset, len, len, |
| 4959 | page, pgdata); |
| 4960 | if (err < 0) |
| 4961 | goto fail; |
| 4962 | |
| 4963 | size -= len; |
| 4964 | data += len; |
| 4965 | offset += len; |
| 4966 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4967 | |
| 4968 | return obj; |
| 4969 | |
| 4970 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4971 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 4972 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 4973 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 4974 | |
| 4975 | struct scatterlist * |
| 4976 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 4977 | unsigned int n, |
| 4978 | unsigned int *offset) |
| 4979 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4980 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 4981 | struct scatterlist *sg; |
| 4982 | unsigned int idx, count; |
| 4983 | |
| 4984 | might_sleep(); |
| 4985 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4986 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 4987 | |
| 4988 | /* As we iterate forward through the sg, we record each entry in a |
| 4989 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 4990 | * this index previously, we will have an entry for it. |
| 4991 | * |
| 4992 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 4993 | * sequential page access (where each new request is consecutive |
| 4994 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 4995 | * i.e. O(1) with a large constant! |
| 4996 | */ |
| 4997 | if (n < READ_ONCE(iter->sg_idx)) |
| 4998 | goto lookup; |
| 4999 | |
| 5000 | mutex_lock(&iter->lock); |
| 5001 | |
| 5002 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5003 | * (or the subsequent) sg are fast - comparing against the last |
| 5004 | * sg is faster than going through the radixtree. |
| 5005 | */ |
| 5006 | |
| 5007 | sg = iter->sg_pos; |
| 5008 | idx = iter->sg_idx; |
| 5009 | count = __sg_page_count(sg); |
| 5010 | |
| 5011 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5012 | void *entry; |
| 5013 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5014 | int ret; |
| 5015 | |
| 5016 | /* If we cannot allocate and insert this entry, or the |
| 5017 | * individual pages from this range, cancel updating the |
| 5018 | * sg_idx so that on this lookup we are forced to linearly |
| 5019 | * scan onwards, but on future lookups we will try the |
| 5020 | * insertion again (in which case we need to be careful of |
| 5021 | * the error return reporting that we have already inserted |
| 5022 | * this index). |
| 5023 | */ |
| 5024 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5025 | if (ret && ret != -EEXIST) |
| 5026 | goto scan; |
| 5027 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5028 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5029 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5030 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5031 | if (ret && ret != -EEXIST) |
| 5032 | goto scan; |
| 5033 | } |
| 5034 | |
| 5035 | idx += count; |
| 5036 | sg = ____sg_next(sg); |
| 5037 | count = __sg_page_count(sg); |
| 5038 | } |
| 5039 | |
| 5040 | scan: |
| 5041 | iter->sg_pos = sg; |
| 5042 | iter->sg_idx = idx; |
| 5043 | |
| 5044 | mutex_unlock(&iter->lock); |
| 5045 | |
| 5046 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5047 | goto lookup; |
| 5048 | |
| 5049 | /* In case we failed to insert the entry into the radixtree, we need |
| 5050 | * to look beyond the current sg. |
| 5051 | */ |
| 5052 | while (idx + count <= n) { |
| 5053 | idx += count; |
| 5054 | sg = ____sg_next(sg); |
| 5055 | count = __sg_page_count(sg); |
| 5056 | } |
| 5057 | |
| 5058 | *offset = n - idx; |
| 5059 | return sg; |
| 5060 | |
| 5061 | lookup: |
| 5062 | rcu_read_lock(); |
| 5063 | |
| 5064 | sg = radix_tree_lookup(&iter->radix, n); |
| 5065 | GEM_BUG_ON(!sg); |
| 5066 | |
| 5067 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5068 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5069 | * to the start of that range. We will return the pointer to |
| 5070 | * the base page and the offset of this page within the |
| 5071 | * sg entry's range. |
| 5072 | */ |
| 5073 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5074 | if (unlikely(xa_is_value(sg))) { |
| 5075 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5076 | |
| 5077 | sg = radix_tree_lookup(&iter->radix, base); |
| 5078 | GEM_BUG_ON(!sg); |
| 5079 | |
| 5080 | *offset = n - base; |
| 5081 | } |
| 5082 | |
| 5083 | rcu_read_unlock(); |
| 5084 | |
| 5085 | return sg; |
| 5086 | } |
| 5087 | |
| 5088 | struct page * |
| 5089 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5090 | { |
| 5091 | struct scatterlist *sg; |
| 5092 | unsigned int offset; |
| 5093 | |
| 5094 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5095 | |
| 5096 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5097 | return nth_page(sg_page(sg), offset); |
| 5098 | } |
| 5099 | |
| 5100 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5101 | struct page * |
| 5102 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5103 | unsigned int n) |
| 5104 | { |
| 5105 | struct page *page; |
| 5106 | |
| 5107 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5108 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5109 | set_page_dirty(page); |
| 5110 | |
| 5111 | return page; |
| 5112 | } |
| 5113 | |
| 5114 | dma_addr_t |
Ville Syrjälä | 1a74fc0 | 2019-05-09 15:21:52 +0300 | [diff] [blame] | 5115 | i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj, |
| 5116 | unsigned long n, |
| 5117 | unsigned int *len) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5118 | { |
| 5119 | struct scatterlist *sg; |
| 5120 | unsigned int offset; |
| 5121 | |
| 5122 | sg = i915_gem_object_get_sg(obj, n, &offset); |
Ville Syrjälä | 1a74fc0 | 2019-05-09 15:21:52 +0300 | [diff] [blame] | 5123 | |
| 5124 | if (len) |
| 5125 | *len = sg_dma_len(sg) - (offset << PAGE_SHIFT); |
| 5126 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5127 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5128 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5129 | |
Ville Syrjälä | 1a74fc0 | 2019-05-09 15:21:52 +0300 | [diff] [blame] | 5130 | dma_addr_t |
| 5131 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5132 | unsigned long n) |
| 5133 | { |
| 5134 | return i915_gem_object_get_dma_address_len(obj, n, NULL); |
| 5135 | } |
| 5136 | |
| 5137 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5138 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5139 | { |
| 5140 | struct sg_table *pages; |
| 5141 | int err; |
| 5142 | |
| 5143 | if (align > obj->base.size) |
| 5144 | return -EINVAL; |
| 5145 | |
| 5146 | if (obj->ops == &i915_gem_phys_ops) |
| 5147 | return 0; |
| 5148 | |
| 5149 | if (obj->ops != &i915_gem_object_ops) |
| 5150 | return -EINVAL; |
| 5151 | |
| 5152 | err = i915_gem_object_unbind(obj); |
| 5153 | if (err) |
| 5154 | return err; |
| 5155 | |
| 5156 | mutex_lock(&obj->mm.lock); |
| 5157 | |
| 5158 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5159 | err = -EFAULT; |
| 5160 | goto err_unlock; |
| 5161 | } |
| 5162 | |
| 5163 | if (obj->mm.quirked) { |
| 5164 | err = -EFAULT; |
| 5165 | goto err_unlock; |
| 5166 | } |
| 5167 | |
| 5168 | if (obj->mm.mapping) { |
| 5169 | err = -EBUSY; |
| 5170 | goto err_unlock; |
| 5171 | } |
| 5172 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5173 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5174 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5175 | obj->ops = &i915_gem_phys_ops; |
| 5176 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5177 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5178 | if (err) |
| 5179 | goto err_xfer; |
| 5180 | |
| 5181 | /* Perma-pin (until release) the physical set of pages */ |
| 5182 | __i915_gem_object_pin_pages(obj); |
| 5183 | |
| 5184 | if (!IS_ERR_OR_NULL(pages)) |
| 5185 | i915_gem_object_ops.put_pages(obj, pages); |
| 5186 | mutex_unlock(&obj->mm.lock); |
| 5187 | return 0; |
| 5188 | |
| 5189 | err_xfer: |
| 5190 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5191 | if (!IS_ERR_OR_NULL(pages)) { |
| 5192 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 5193 | |
| 5194 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 5195 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5196 | err_unlock: |
| 5197 | mutex_unlock(&obj->mm.lock); |
| 5198 | return err; |
| 5199 | } |
| 5200 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5201 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5202 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5203 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5204 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5205 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5206 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5207 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 5208 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5209 | #endif |