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> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/i915_drm.h> |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 30 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 31 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 32 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 33 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 35 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 36 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 37 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 38 | #include <linux/dma-buf.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 39 | #include <linux/mman.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 40 | |
Chris Wilson | 10be98a | 2019-05-28 10:29:49 +0100 | [diff] [blame] | 41 | #include "gem/i915_gem_clflush.h" |
| 42 | #include "gem/i915_gem_context.h" |
Chris Wilson | afa1308 | 2019-05-28 10:29:43 +0100 | [diff] [blame] | 43 | #include "gem/i915_gem_ioctls.h" |
Chris Wilson | 10be98a | 2019-05-28 10:29:49 +0100 | [diff] [blame] | 44 | #include "gem/i915_gem_pm.h" |
| 45 | #include "gem/i915_gemfs.h" |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 46 | #include "gt/intel_engine_pm.h" |
| 47 | #include "gt/intel_gt_pm.h" |
Chris Wilson | 112ed2d | 2019-04-24 18:48:39 +0100 | [diff] [blame] | 48 | #include "gt/intel_mocs.h" |
| 49 | #include "gt/intel_reset.h" |
| 50 | #include "gt/intel_workarounds.h" |
| 51 | |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 52 | #include "i915_drv.h" |
Chris Wilson | 37d63f8 | 2019-05-28 10:29:50 +0100 | [diff] [blame] | 53 | #include "i915_scatterlist.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 54 | #include "i915_trace.h" |
| 55 | #include "i915_vgpu.h" |
| 56 | |
Ville Syrjälä | aa5ca8b | 2019-05-09 15:21:57 +0300 | [diff] [blame] | 57 | #include "intel_display.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 58 | #include "intel_drv.h" |
| 59 | #include "intel_frontbuffer.h" |
Jani Nikula | 696173b | 2019-04-05 14:00:15 +0300 | [diff] [blame] | 60 | #include "intel_pm.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 61 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 62 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 63 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 64 | struct drm_mm_node *node, u32 size) |
| 65 | { |
| 66 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 67 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 68 | size, 0, I915_COLOR_UNEVICTABLE, |
| 69 | 0, ggtt->mappable_end, |
| 70 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void |
| 74 | remove_mappable_node(struct drm_mm_node *node) |
| 75 | { |
| 76 | drm_mm_remove_node(node); |
| 77 | } |
| 78 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 79 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 80 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 81 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 82 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 83 | struct i915_ggtt *ggtt = &to_i915(dev)->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 84 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 85 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 86 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 87 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 88 | mutex_lock(&ggtt->vm.mutex); |
| 89 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 90 | pinned = ggtt->vm.reserved; |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 91 | list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 92 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 93 | pinned += vma->node.size; |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 94 | |
| 95 | mutex_unlock(&ggtt->vm.mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 96 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 97 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 98 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 99 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 103 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 104 | { |
| 105 | struct i915_vma *vma; |
| 106 | LIST_HEAD(still_in_list); |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 107 | int ret = 0; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 108 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 109 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 110 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 111 | spin_lock(&obj->vma.lock); |
| 112 | while (!ret && (vma = list_first_entry_or_null(&obj->vma.list, |
| 113 | struct i915_vma, |
| 114 | obj_link))) { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 115 | list_move_tail(&vma->obj_link, &still_in_list); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 116 | spin_unlock(&obj->vma.lock); |
| 117 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 118 | ret = i915_vma_unbind(vma); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 119 | |
| 120 | spin_lock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 121 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 122 | list_splice(&still_in_list, &obj->vma.list); |
| 123 | spin_unlock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 128 | static int |
| 129 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 130 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 131 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 132 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 133 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 134 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 135 | |
| 136 | /* We manually control the domain here and pretend that it |
| 137 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 138 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 139 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 140 | if (copy_from_user(vaddr, user_data, args->size)) |
| 141 | return -EFAULT; |
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 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 144 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 145 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 146 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 147 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 150 | static int |
| 151 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 152 | struct drm_i915_private *dev_priv, |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 153 | u64 *size_p, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 154 | u32 *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 155 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 156 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 157 | u32 handle; |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 158 | u64 size; |
| 159 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 160 | |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 161 | size = round_up(*size_p, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 162 | if (size == 0) |
| 163 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 164 | |
| 165 | /* Allocate the new object */ |
Chris Wilson | 8475355 | 2019-05-28 10:29:45 +0100 | [diff] [blame] | 166 | obj = i915_gem_object_create_shmem(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 167 | if (IS_ERR(obj)) |
| 168 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 169 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 170 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 171 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 172 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 173 | if (ret) |
| 174 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 175 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 176 | *handle_p = handle; |
Chris Wilson | 9953402 | 2019-04-17 14:25:07 +0100 | [diff] [blame] | 177 | *size_p = size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 181 | int |
| 182 | i915_gem_dumb_create(struct drm_file *file, |
| 183 | struct drm_device *dev, |
| 184 | struct drm_mode_create_dumb *args) |
| 185 | { |
Ville Syrjälä | aa5ca8b | 2019-05-09 15:21:57 +0300 | [diff] [blame] | 186 | int cpp = DIV_ROUND_UP(args->bpp, 8); |
| 187 | u32 format; |
| 188 | |
| 189 | switch (cpp) { |
| 190 | case 1: |
| 191 | format = DRM_FORMAT_C8; |
| 192 | break; |
| 193 | case 2: |
| 194 | format = DRM_FORMAT_RGB565; |
| 195 | break; |
| 196 | case 4: |
| 197 | format = DRM_FORMAT_XRGB8888; |
| 198 | break; |
| 199 | default: |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 203 | /* have to work out size/pitch and return them */ |
Ville Syrjälä | aa5ca8b | 2019-05-09 15:21:57 +0300 | [diff] [blame] | 204 | args->pitch = ALIGN(args->width * cpp, 64); |
| 205 | |
| 206 | /* align stride to page size so that we can remap */ |
| 207 | if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format, |
| 208 | DRM_FORMAT_MOD_LINEAR)) |
| 209 | args->pitch = ALIGN(args->pitch, 4096); |
| 210 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 211 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 212 | return i915_gem_create(file, to_i915(dev), |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 213 | &args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 214 | } |
| 215 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 216 | /** |
| 217 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 218 | * @dev: drm device pointer |
| 219 | * @data: ioctl data blob |
| 220 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 221 | */ |
| 222 | int |
| 223 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 224 | struct drm_file *file) |
| 225 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 226 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 227 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 228 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 229 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 230 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 231 | return i915_gem_create(file, dev_priv, |
Michał Winiarski | e163484 | 2019-03-26 18:02:18 +0100 | [diff] [blame] | 232 | &args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 235 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 236 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 237 | intel_wakeref_t wakeref; |
| 238 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 239 | /* |
| 240 | * No actual flushing is required for the GTT write domain for reads |
| 241 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 242 | * as far as we know, so there's no chipset flush. It also doesn't |
| 243 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 244 | * |
| 245 | * However, we do have to enforce the order so that all writes through |
| 246 | * the GTT land before any writes to the device, such as updates to |
| 247 | * the GATT itself. |
| 248 | * |
| 249 | * We also have to wait a bit for the writes to land from the GTT. |
| 250 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 251 | * timing. This issue has only been observed when switching quickly |
| 252 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 253 | * 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] | 254 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 255 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 256 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 257 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 258 | wmb(); |
| 259 | |
| 260 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 261 | return; |
| 262 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 263 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 264 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 265 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 266 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 267 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 268 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 269 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 270 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 271 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 274 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 275 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 276 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 277 | { |
| 278 | char *vaddr; |
| 279 | int ret; |
| 280 | |
| 281 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 282 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 283 | if (needs_clflush) |
| 284 | drm_clflush_virt_range(vaddr + offset, len); |
| 285 | |
| 286 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 287 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 288 | kunmap(page); |
| 289 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 290 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static int |
| 294 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 295 | struct drm_i915_gem_pread *args) |
| 296 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 297 | unsigned int needs_clflush; |
| 298 | unsigned int idx, offset; |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 299 | struct dma_fence *fence; |
| 300 | char __user *user_data; |
| 301 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 302 | int ret; |
| 303 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 304 | ret = i915_gem_object_prepare_read(obj, &needs_clflush); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 305 | if (ret) |
| 306 | return ret; |
| 307 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 308 | fence = i915_gem_object_lock_fence(obj); |
| 309 | i915_gem_object_finish_access(obj); |
| 310 | if (!fence) |
| 311 | return -ENOMEM; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 312 | |
| 313 | remain = args->size; |
| 314 | user_data = u64_to_user_ptr(args->data_ptr); |
| 315 | offset = offset_in_page(args->offset); |
| 316 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 317 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 318 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 319 | |
| 320 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 321 | needs_clflush); |
| 322 | if (ret) |
| 323 | break; |
| 324 | |
| 325 | remain -= length; |
| 326 | user_data += length; |
| 327 | offset = 0; |
| 328 | } |
| 329 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 330 | i915_gem_object_unlock_fence(obj, fence); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static inline bool |
| 335 | gtt_user_read(struct io_mapping *mapping, |
| 336 | loff_t base, int offset, |
| 337 | char __user *user_data, int length) |
| 338 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 339 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 340 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 341 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 342 | /* 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] | 343 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 344 | unwritten = __copy_to_user_inatomic(user_data, |
| 345 | (void __force *)vaddr + offset, |
| 346 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 347 | io_mapping_unmap_atomic(vaddr); |
| 348 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 349 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 350 | unwritten = copy_to_user(user_data, |
| 351 | (void __force *)vaddr + offset, |
| 352 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 353 | io_mapping_unmap(vaddr); |
| 354 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 355 | return unwritten; |
| 356 | } |
| 357 | |
| 358 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 359 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 360 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 361 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 362 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 363 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 364 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 365 | struct drm_mm_node node; |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 366 | struct dma_fence *fence; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 367 | void __user *user_data; |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 368 | struct i915_vma *vma; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 369 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 370 | int ret; |
| 371 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 372 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 376 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 377 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 378 | PIN_MAPPABLE | |
| 379 | PIN_NONFAULT | |
| 380 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 381 | if (!IS_ERR(vma)) { |
| 382 | node.start = i915_ggtt_offset(vma); |
| 383 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 384 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 385 | if (ret) { |
| 386 | i915_vma_unpin(vma); |
| 387 | vma = ERR_PTR(ret); |
| 388 | } |
| 389 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 390 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 391 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 392 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 393 | goto out_unlock; |
| 394 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 395 | } |
| 396 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 397 | mutex_unlock(&i915->drm.struct_mutex); |
| 398 | |
| 399 | ret = i915_gem_object_lock_interruptible(obj); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 400 | if (ret) |
| 401 | goto out_unpin; |
| 402 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 403 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 404 | if (ret) { |
| 405 | i915_gem_object_unlock(obj); |
| 406 | goto out_unpin; |
| 407 | } |
| 408 | |
| 409 | fence = i915_gem_object_lock_fence(obj); |
| 410 | i915_gem_object_unlock(obj); |
| 411 | if (!fence) { |
| 412 | ret = -ENOMEM; |
| 413 | goto out_unpin; |
| 414 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 415 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 416 | user_data = u64_to_user_ptr(args->data_ptr); |
| 417 | remain = args->size; |
| 418 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 419 | |
| 420 | while (remain > 0) { |
| 421 | /* Operation in this page |
| 422 | * |
| 423 | * page_base = page offset within aperture |
| 424 | * page_offset = offset within page |
| 425 | * page_length = bytes to copy for this page |
| 426 | */ |
| 427 | u32 page_base = node.start; |
| 428 | unsigned page_offset = offset_in_page(offset); |
| 429 | unsigned page_length = PAGE_SIZE - page_offset; |
| 430 | page_length = remain < page_length ? remain : page_length; |
| 431 | if (node.allocated) { |
| 432 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 433 | ggtt->vm.insert_page(&ggtt->vm, |
| 434 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 435 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 436 | wmb(); |
| 437 | } else { |
| 438 | page_base += offset & PAGE_MASK; |
| 439 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 440 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 441 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 442 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 443 | ret = -EFAULT; |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | remain -= page_length; |
| 448 | user_data += page_length; |
| 449 | offset += page_length; |
| 450 | } |
| 451 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 452 | i915_gem_object_unlock_fence(obj, fence); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 453 | out_unpin: |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 454 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 455 | if (node.allocated) { |
| 456 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 457 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 458 | remove_mappable_node(&node); |
| 459 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 460 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 461 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 462 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 463 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 464 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 465 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 466 | return ret; |
| 467 | } |
| 468 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 469 | /** |
| 470 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 471 | * @dev: drm device pointer |
| 472 | * @data: ioctl data blob |
| 473 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 474 | * |
| 475 | * On error, the contents of *data are undefined. |
| 476 | */ |
| 477 | int |
| 478 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 479 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 480 | { |
| 481 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 482 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 483 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 484 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 485 | if (args->size == 0) |
| 486 | return 0; |
| 487 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 488 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 489 | args->size)) |
| 490 | return -EFAULT; |
| 491 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 492 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 493 | if (!obj) |
| 494 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 495 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 496 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 497 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 498 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 499 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 502 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 503 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 504 | ret = i915_gem_object_wait(obj, |
| 505 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 506 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 507 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 508 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 509 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 510 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 511 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 512 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 513 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 514 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 515 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 516 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 517 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 518 | i915_gem_object_unpin_pages(obj); |
| 519 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 520 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 521 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 524 | /* This is the fast write path which cannot handle |
| 525 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 526 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 527 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 528 | static inline bool |
| 529 | ggtt_write(struct io_mapping *mapping, |
| 530 | loff_t base, int offset, |
| 531 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 532 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 533 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 534 | unsigned long unwritten; |
| 535 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 536 | /* 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] | 537 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 538 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 539 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 540 | io_mapping_unmap_atomic(vaddr); |
| 541 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 542 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 543 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 544 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 545 | io_mapping_unmap(vaddr); |
| 546 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 547 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 548 | return unwritten; |
| 549 | } |
| 550 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 551 | /** |
| 552 | * This is the fast pwrite path, where we copy the data directly from the |
| 553 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 554 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 555 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 556 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 557 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 558 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 559 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 560 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 561 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 562 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 563 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 564 | struct drm_mm_node node; |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 565 | struct dma_fence *fence; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 566 | struct i915_vma *vma; |
| 567 | u64 remain, offset; |
| 568 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 569 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 570 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 571 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 572 | if (ret) |
| 573 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 574 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 575 | if (i915_gem_object_has_struct_page(obj)) { |
| 576 | /* |
| 577 | * Avoid waking the device up if we can fallback, as |
| 578 | * waking/resuming is very slow (worst-case 10-100 ms |
| 579 | * depending on PCI sleeps and our own resume time). |
| 580 | * This easily dwarfs any performance advantage from |
| 581 | * using the cache bypass of indirect GGTT access. |
| 582 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 583 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 584 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 585 | ret = -EFAULT; |
| 586 | goto out_unlock; |
| 587 | } |
| 588 | } else { |
| 589 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 590 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 593 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 594 | PIN_MAPPABLE | |
| 595 | PIN_NONFAULT | |
| 596 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 597 | if (!IS_ERR(vma)) { |
| 598 | node.start = i915_ggtt_offset(vma); |
| 599 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 600 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 601 | if (ret) { |
| 602 | i915_vma_unpin(vma); |
| 603 | vma = ERR_PTR(ret); |
| 604 | } |
| 605 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 606 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 607 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 608 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 609 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 610 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 611 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 612 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 613 | mutex_unlock(&i915->drm.struct_mutex); |
| 614 | |
| 615 | ret = i915_gem_object_lock_interruptible(obj); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 616 | if (ret) |
| 617 | goto out_unpin; |
| 618 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 619 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 620 | if (ret) { |
| 621 | i915_gem_object_unlock(obj); |
| 622 | goto out_unpin; |
| 623 | } |
| 624 | |
| 625 | fence = i915_gem_object_lock_fence(obj); |
| 626 | i915_gem_object_unlock(obj); |
| 627 | if (!fence) { |
| 628 | ret = -ENOMEM; |
| 629 | goto out_unpin; |
| 630 | } |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 631 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 632 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 633 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 634 | user_data = u64_to_user_ptr(args->data_ptr); |
| 635 | offset = args->offset; |
| 636 | remain = args->size; |
| 637 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 638 | /* Operation in this page |
| 639 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 640 | * page_base = page offset within aperture |
| 641 | * page_offset = offset within page |
| 642 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 643 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 644 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 645 | unsigned int page_offset = offset_in_page(offset); |
| 646 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 647 | page_length = remain < page_length ? remain : page_length; |
| 648 | if (node.allocated) { |
| 649 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 650 | ggtt->vm.insert_page(&ggtt->vm, |
| 651 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 652 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 653 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 654 | } else { |
| 655 | page_base += offset & PAGE_MASK; |
| 656 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 657 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 658 | * source page isn't available. Return the error and we'll |
| 659 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 660 | * If the object is non-shmem backed, we retry again with the |
| 661 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 662 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 663 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 664 | user_data, page_length)) { |
| 665 | ret = -EFAULT; |
| 666 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 667 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 668 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 669 | remain -= page_length; |
| 670 | user_data += page_length; |
| 671 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 672 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 673 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 674 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 675 | i915_gem_object_unlock_fence(obj, fence); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 676 | out_unpin: |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 677 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 678 | if (node.allocated) { |
| 679 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 680 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 681 | remove_mappable_node(&node); |
| 682 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 683 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 684 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 685 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 686 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 687 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 688 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 689 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 692 | /* Per-page copy function for the shmem pwrite fastpath. |
| 693 | * Flushes invalid cachelines before writing to the target if |
| 694 | * needs_clflush_before is set and flushes out any written cachelines after |
| 695 | * writing if needs_clflush is set. |
| 696 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 697 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 698 | 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] | 699 | bool needs_clflush_before, |
| 700 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 701 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 702 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 703 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 704 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 705 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 706 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 707 | if (needs_clflush_before) |
| 708 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 709 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 710 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 711 | if (!ret && needs_clflush_after) |
| 712 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 713 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 714 | kunmap(page); |
| 715 | |
| 716 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static int |
| 720 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 721 | const struct drm_i915_gem_pwrite *args) |
| 722 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 723 | unsigned int partial_cacheline_write; |
| 724 | unsigned int needs_clflush; |
| 725 | unsigned int offset, idx; |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 726 | struct dma_fence *fence; |
| 727 | void __user *user_data; |
| 728 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 729 | int ret; |
| 730 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 731 | ret = i915_gem_object_prepare_write(obj, &needs_clflush); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 732 | if (ret) |
| 733 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 734 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 735 | fence = i915_gem_object_lock_fence(obj); |
| 736 | i915_gem_object_finish_access(obj); |
| 737 | if (!fence) |
| 738 | return -ENOMEM; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 739 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 740 | /* If we don't overwrite a cacheline completely we need to be |
| 741 | * careful to have up-to-date data by first clflushing. Don't |
| 742 | * overcomplicate things and flush the entire patch. |
| 743 | */ |
| 744 | partial_cacheline_write = 0; |
| 745 | if (needs_clflush & CLFLUSH_BEFORE) |
| 746 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 747 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 748 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 749 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 750 | offset = offset_in_page(args->offset); |
| 751 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 752 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 753 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 754 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 755 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 756 | (offset | length) & partial_cacheline_write, |
| 757 | needs_clflush & CLFLUSH_AFTER); |
| 758 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 759 | break; |
| 760 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 761 | remain -= length; |
| 762 | user_data += length; |
| 763 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 766 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 767 | i915_gem_object_unlock_fence(obj, fence); |
| 768 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 769 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 774 | * @dev: drm device |
| 775 | * @data: ioctl data blob |
| 776 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 777 | * |
| 778 | * On error, the contents of the buffer that were to be modified are undefined. |
| 779 | */ |
| 780 | int |
| 781 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 782 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 783 | { |
| 784 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 785 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 786 | int ret; |
| 787 | |
| 788 | if (args->size == 0) |
| 789 | return 0; |
| 790 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 791 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 792 | return -EFAULT; |
| 793 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 794 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 795 | if (!obj) |
| 796 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 797 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 798 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 799 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 800 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 801 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 802 | } |
| 803 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 804 | /* Writes not allowed into this read-only object */ |
| 805 | if (i915_gem_object_is_readonly(obj)) { |
| 806 | ret = -EINVAL; |
| 807 | goto err; |
| 808 | } |
| 809 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 810 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 811 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 812 | ret = -ENODEV; |
| 813 | if (obj->ops->pwrite) |
| 814 | ret = obj->ops->pwrite(obj, args); |
| 815 | if (ret != -ENODEV) |
| 816 | goto err; |
| 817 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 818 | ret = i915_gem_object_wait(obj, |
| 819 | I915_WAIT_INTERRUPTIBLE | |
| 820 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 821 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 822 | if (ret) |
| 823 | goto err; |
| 824 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 825 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 826 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 827 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 828 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 829 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 830 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 831 | * it would end up going through the fenced access, and we'll get |
| 832 | * different detiling behavior between reading and writing. |
| 833 | * pread/pwrite currently are reading and writing from the CPU |
| 834 | * perspective, requiring manual detiling by the client. |
| 835 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 836 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 837 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 838 | /* Note that the gtt paths might fail with non-page-backed user |
| 839 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 840 | * textures). Fallback to the shmem path in that case. |
| 841 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 842 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 843 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 844 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 845 | if (obj->phys_handle) |
| 846 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 847 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 848 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 849 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 850 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 851 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 852 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 853 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 854 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 857 | /** |
| 858 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 859 | * @dev: drm device |
| 860 | * @data: ioctl data blob |
| 861 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 862 | */ |
| 863 | int |
| 864 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 865 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 866 | { |
| 867 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 868 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 869 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 870 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 871 | if (!obj) |
| 872 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 873 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 874 | /* |
| 875 | * Proxy objects are barred from CPU access, so there is no |
| 876 | * need to ban sw_finish as it is a nop. |
| 877 | */ |
| 878 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 879 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 880 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 881 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 882 | |
| 883 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 886 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 887 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 888 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 889 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 890 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 891 | /* |
| 892 | * Only called during RPM suspend. All users of the userfault_list |
| 893 | * must be holding an RPM wakeref to ensure that this can not |
| 894 | * run concurrently with themselves (and use the struct_mutex for |
| 895 | * protection between themselves). |
| 896 | */ |
| 897 | |
| 898 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 899 | &dev_priv->mm.userfault_list, userfault_link) |
| 900 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 901 | |
| 902 | /* The fence will be lost when the device powers down. If any were |
| 903 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 904 | * down! All other fences will be reacquired by the user upon waking. |
| 905 | */ |
| 906 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 907 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 908 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 909 | /* Ideally we want to assert that the fence register is not |
| 910 | * live at this point (i.e. that no piece of code will be |
| 911 | * trying to write through fence + GTT, as that both violates |
| 912 | * our tracking of activity and associated locking/barriers, |
| 913 | * but also is illegal given that the hw is powered down). |
| 914 | * |
| 915 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 916 | * That is not sufficient, and we need a more fine-grained |
| 917 | * tool if we want to have a sanity check here. |
| 918 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 919 | |
| 920 | if (!reg->vma) |
| 921 | continue; |
| 922 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 923 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 924 | reg->dirty = true; |
| 925 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 926 | } |
| 927 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 928 | static int wait_for_engines(struct drm_i915_private *i915) |
| 929 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 930 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 931 | dev_err(i915->drm.dev, |
| 932 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 933 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 934 | i915_gem_set_wedged(i915); |
| 935 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 941 | static long |
| 942 | wait_for_timelines(struct drm_i915_private *i915, |
| 943 | unsigned int flags, long timeout) |
| 944 | { |
| 945 | struct i915_gt_timelines *gt = &i915->gt.timelines; |
| 946 | struct i915_timeline *tl; |
| 947 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 948 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 949 | list_for_each_entry(tl, >->active_list, link) { |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 950 | struct i915_request *rq; |
| 951 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 952 | rq = i915_active_request_get_unlocked(&tl->last_request); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 953 | if (!rq) |
| 954 | continue; |
| 955 | |
| 956 | mutex_unlock(>->mutex); |
| 957 | |
| 958 | /* |
| 959 | * "Race-to-idle". |
| 960 | * |
| 961 | * Switching to the kernel context is often used a synchronous |
| 962 | * step prior to idling, e.g. in suspend for flushing all |
| 963 | * current operations to memory before sleeping. These we |
| 964 | * want to complete as quickly as possible to avoid prolonged |
| 965 | * stalls, so allow the gpu to boost to maximum clocks. |
| 966 | */ |
| 967 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 968 | gen6_rps_boost(rq); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 969 | |
| 970 | timeout = i915_request_wait(rq, flags, timeout); |
| 971 | i915_request_put(rq); |
| 972 | if (timeout < 0) |
| 973 | return timeout; |
| 974 | |
| 975 | /* restart after reacquiring the lock */ |
| 976 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 977 | tl = list_entry(>->active_list, typeof(*tl), link); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 978 | } |
| 979 | mutex_unlock(>->mutex); |
| 980 | |
| 981 | return timeout; |
| 982 | } |
| 983 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 984 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 985 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 986 | { |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 987 | GEM_TRACE("flags=%x (%s), timeout=%ld%s, awake?=%s\n", |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 988 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 989 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "", |
| 990 | yesno(i915->gt.awake)); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 991 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 992 | /* If the device is asleep, we have no requests outstanding */ |
| 993 | if (!READ_ONCE(i915->gt.awake)) |
| 994 | return 0; |
| 995 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 996 | timeout = wait_for_timelines(i915, flags, timeout); |
| 997 | if (timeout < 0) |
| 998 | return timeout; |
| 999 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 1000 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 1001 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 1002 | |
| 1003 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 1004 | |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 1005 | err = wait_for_engines(i915); |
| 1006 | if (err) |
| 1007 | return err; |
| 1008 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 1009 | i915_retire_requests(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 1010 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 1011 | |
| 1012 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1015 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 1016 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 1017 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 1018 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 1019 | u64 alignment, |
| 1020 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 1021 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 1022 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1023 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 1024 | struct i915_vma *vma; |
| 1025 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1026 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 1027 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 1028 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 1029 | if (flags & PIN_MAPPABLE && |
| 1030 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 1031 | /* If the required space is larger than the available |
| 1032 | * aperture, we will not able to find a slot for the |
| 1033 | * object and unbinding the object now will be in |
| 1034 | * vain. Worse, doing so may cause us to ping-pong |
| 1035 | * the object in and out of the Global GTT and |
| 1036 | * waste a lot of cycles under the mutex. |
| 1037 | */ |
| 1038 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 1039 | return ERR_PTR(-E2BIG); |
| 1040 | |
| 1041 | /* If NONBLOCK is set the caller is optimistically |
| 1042 | * trying to cache the full object within the mappable |
| 1043 | * aperture, and *must* have a fallback in place for |
| 1044 | * situations where we cannot bind the object. We |
| 1045 | * can be a little more lax here and use the fallback |
| 1046 | * more often to avoid costly migrations of ourselves |
| 1047 | * and other objects within the aperture. |
| 1048 | * |
| 1049 | * Half-the-aperture is used as a simple heuristic. |
| 1050 | * More interesting would to do search for a free |
| 1051 | * block prior to making the commitment to unbind. |
| 1052 | * That caters for the self-harm case, and with a |
| 1053 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 1054 | * we could try to minimise harm to others. |
| 1055 | */ |
| 1056 | if (flags & PIN_NONBLOCK && |
| 1057 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 1058 | return ERR_PTR(-ENOSPC); |
| 1059 | } |
| 1060 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 1061 | vma = i915_vma_instance(obj, vm, view); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 1062 | if (IS_ERR(vma)) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1063 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 1064 | |
| 1065 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 1066 | if (flags & PIN_NONBLOCK) { |
| 1067 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 1068 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 1069 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 1070 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 1071 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 1072 | return ERR_PTR(-ENOSPC); |
| 1073 | } |
| 1074 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 1075 | WARN(i915_vma_is_pinned(vma), |
| 1076 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 1077 | " offset=%08x, req.alignment=%llx," |
| 1078 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 1079 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 1080 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 1081 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 1082 | ret = i915_vma_unbind(vma); |
| 1083 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1084 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1087 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 1088 | if (ret) |
| 1089 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 1090 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1091 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1094 | int |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1095 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 1096 | struct drm_file *file_priv) |
| 1097 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 1098 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1099 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1100 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 1101 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1102 | |
| 1103 | switch (args->madv) { |
| 1104 | case I915_MADV_DONTNEED: |
| 1105 | case I915_MADV_WILLNEED: |
| 1106 | break; |
| 1107 | default: |
| 1108 | return -EINVAL; |
| 1109 | } |
| 1110 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1111 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 1112 | if (!obj) |
| 1113 | return -ENOENT; |
| 1114 | |
| 1115 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 1116 | if (err) |
| 1117 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1118 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 1119 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 1120 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 1121 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 1122 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 1123 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 1124 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 1125 | obj->mm.quirked = false; |
| 1126 | } |
| 1127 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 1128 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 1129 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 1130 | obj->mm.quirked = true; |
| 1131 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 1134 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 1135 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1136 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1137 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 1138 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 1139 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | f033428 | 2019-05-28 10:29:46 +0100 | [diff] [blame] | 1140 | i915_gem_object_truncate(obj); |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 1141 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 1142 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 1143 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1144 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 1145 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 1146 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 1147 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 1150 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 1151 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1152 | intel_wakeref_t wakeref; |
| 1153 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 1154 | GEM_TRACE("\n"); |
| 1155 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1156 | wakeref = intel_runtime_pm_get(i915); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 1157 | intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 1158 | |
| 1159 | /* |
| 1160 | * As we have just resumed the machine and woken the device up from |
| 1161 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 1162 | * back to defaults, recovering from whatever wedged state we left it |
| 1163 | * in and so worth trying to use the device once more. |
| 1164 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1165 | if (i915_terminally_wedged(i915)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 1166 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 1167 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 1168 | /* |
| 1169 | * If we inherit context state from the BIOS or earlier occupants |
| 1170 | * of the GPU, the GPU may be in an inconsistent state when we |
| 1171 | * try to take over. The only way to remove the earlier state |
| 1172 | * is by resetting. However, resetting on earlier gen is tricky as |
| 1173 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 1174 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 1175 | */ |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 1176 | intel_gt_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 1177 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 1178 | intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1179 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 1180 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 1181 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 1182 | i915_gem_contexts_lost(i915); |
| 1183 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 1186 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 1187 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 1188 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 1189 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 1190 | return; |
| 1191 | |
| 1192 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 1193 | DISP_TILE_SURFACE_SWIZZLING); |
| 1194 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 1195 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 1196 | return; |
| 1197 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 1198 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 1199 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 1200 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 1201 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 1202 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 1203 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 1204 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 1205 | else |
| 1206 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 1207 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 1208 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 1209 | 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] | 1210 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 1211 | I915_WRITE(RING_CTL(base), 0); |
| 1212 | I915_WRITE(RING_HEAD(base), 0); |
| 1213 | I915_WRITE(RING_TAIL(base), 0); |
| 1214 | I915_WRITE(RING_START(base), 0); |
| 1215 | } |
| 1216 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 1217 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 1218 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 1219 | if (IS_I830(dev_priv)) { |
| 1220 | init_unused_ring(dev_priv, PRB1_BASE); |
| 1221 | init_unused_ring(dev_priv, SRB0_BASE); |
| 1222 | init_unused_ring(dev_priv, SRB1_BASE); |
| 1223 | init_unused_ring(dev_priv, SRB2_BASE); |
| 1224 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 1225 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 1226 | init_unused_ring(dev_priv, SRB0_BASE); |
| 1227 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 1228 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 1229 | init_unused_ring(dev_priv, PRB1_BASE); |
| 1230 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 1234 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 1235 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 1236 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 1237 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 1238 | dev_priv->gt.last_init_time = ktime_get(); |
| 1239 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 1240 | /* Double layer security blanket, see i915_gem_init() */ |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 1241 | intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 1242 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1243 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 1244 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 1245 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 1246 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 1247 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 1248 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 1249 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 1250 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 1251 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 1252 | /* ...and determine whether they are sticking. */ |
| 1253 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 1254 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 1255 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 1256 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 1257 | /* |
| 1258 | * At least 830 can leave some of the unused rings |
| 1259 | * "active" (ie. head != tail) after resume which |
| 1260 | * will prevent c3 entry. Makes sure all unused rings |
| 1261 | * are totally idle. |
| 1262 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 1263 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 1264 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 1265 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1266 | ret = i915_terminally_wedged(dev_priv); |
| 1267 | if (ret) |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 1268 | goto out; |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 1269 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 1270 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 1271 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 1272 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 1273 | goto out; |
| 1274 | } |
| 1275 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 1276 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 1277 | if (ret) { |
| 1278 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 1279 | goto out; |
| 1280 | } |
| 1281 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 1282 | /* We can't enable contexts until all firmware is loaded */ |
| 1283 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 1284 | if (ret) { |
| 1285 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 1286 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 1287 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 1288 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 1289 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 1290 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 1291 | /* Only when the HW is re-initialised, can we replay the requests */ |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 1292 | ret = intel_engines_resume(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 1293 | if (ret) |
| 1294 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 1295 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 1296 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 1297 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 1298 | intel_engines_set_scheduler_caps(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 1299 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 1300 | |
| 1301 | cleanup_uc: |
| 1302 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 1303 | out: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 1304 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 1305 | |
| 1306 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1309 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 1310 | { |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1311 | struct intel_engine_cs *engine; |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 1312 | struct i915_gem_context *ctx; |
| 1313 | struct i915_gem_engines *e; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1314 | enum intel_engine_id id; |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 1315 | int err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1316 | |
| 1317 | /* |
| 1318 | * As we reset the gpu during very early sanitisation, the current |
| 1319 | * register state on the GPU should reflect its defaults values. |
| 1320 | * We load a context onto the hw (with restore-inhibit), then switch |
| 1321 | * over to a second context to save that default register state. We |
| 1322 | * can then prime every new context with that state so they all start |
| 1323 | * from the same default HW values. |
| 1324 | */ |
| 1325 | |
| 1326 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 1327 | if (IS_ERR(ctx)) |
| 1328 | return PTR_ERR(ctx); |
| 1329 | |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 1330 | e = i915_gem_context_lock_engines(ctx); |
| 1331 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1332 | for_each_engine(engine, i915, id) { |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 1333 | struct intel_context *ce = e->engines[id]; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 1334 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1335 | |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 1336 | rq = intel_context_create_request(ce); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1337 | if (IS_ERR(rq)) { |
| 1338 | err = PTR_ERR(rq); |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 1339 | goto err_active; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 1342 | err = 0; |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 1343 | if (rq->engine->init_context) |
| 1344 | err = rq->engine->init_context(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1345 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 1346 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1347 | if (err) |
| 1348 | goto err_active; |
| 1349 | } |
| 1350 | |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 1351 | /* Flush the default context image to memory, and enable powersaving. */ |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 1352 | if (!i915_gem_load_power_context(i915)) { |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 1353 | err = -EIO; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1354 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 1355 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1356 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1357 | for_each_engine(engine, i915, id) { |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 1358 | struct intel_context *ce = e->engines[id]; |
| 1359 | struct i915_vma *state = ce->state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 1360 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1361 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1362 | if (!state) |
| 1363 | continue; |
| 1364 | |
Chris Wilson | 0881954 | 2019-03-08 13:25:22 +0000 | [diff] [blame] | 1365 | GEM_BUG_ON(intel_context_is_pinned(ce)); |
Chris Wilson | c4d52fe | 2019-03-08 13:25:19 +0000 | [diff] [blame] | 1366 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1367 | /* |
| 1368 | * As we will hold a reference to the logical state, it will |
| 1369 | * not be torn down with the context, and importantly the |
| 1370 | * object will hold onto its vma (making it possible for a |
| 1371 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 1372 | * from the GTT to prevent such accidents and reclaim the |
| 1373 | * space. |
| 1374 | */ |
| 1375 | err = i915_vma_unbind(state); |
| 1376 | if (err) |
| 1377 | goto err_active; |
| 1378 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 1379 | i915_gem_object_lock(state->obj); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1380 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 1381 | i915_gem_object_unlock(state->obj); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1382 | if (err) |
| 1383 | goto err_active; |
| 1384 | |
| 1385 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | a679f58 | 2019-03-21 16:19:07 +0000 | [diff] [blame] | 1386 | i915_gem_object_set_cache_coherency(engine->default_state, |
| 1387 | I915_CACHE_LLC); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 1388 | |
| 1389 | /* Check we can acquire the image of the context state */ |
| 1390 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 1391 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 1392 | if (IS_ERR(vaddr)) { |
| 1393 | err = PTR_ERR(vaddr); |
| 1394 | goto err_active; |
| 1395 | } |
| 1396 | |
| 1397 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 1401 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 1402 | |
| 1403 | /* |
| 1404 | * Make sure that classes with multiple engine instances all |
| 1405 | * share the same basic configuration. |
| 1406 | */ |
| 1407 | for_each_engine(engine, i915, id) { |
| 1408 | unsigned int bit = BIT(engine->uabi_class); |
| 1409 | unsigned int expected = engine->default_state ? bit : 0; |
| 1410 | |
| 1411 | if ((found & bit) != expected) { |
| 1412 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 1413 | engine->uabi_class, engine->name); |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | out_ctx: |
Chris Wilson | 5e2a041 | 2019-04-26 17:33:34 +0100 | [diff] [blame] | 1419 | i915_gem_context_unlock_engines(ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1420 | i915_gem_context_set_closed(ctx); |
| 1421 | i915_gem_context_put(ctx); |
| 1422 | return err; |
| 1423 | |
| 1424 | err_active: |
| 1425 | /* |
| 1426 | * 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] | 1427 | * and ready to be torn-down. The quickest way we can accomplish |
| 1428 | * this is by declaring ourselves wedged. |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1429 | */ |
Chris Wilson | 604c37d | 2019-03-08 09:36:55 +0000 | [diff] [blame] | 1430 | i915_gem_set_wedged(i915); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1431 | goto out_ctx; |
| 1432 | } |
| 1433 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 1434 | static int |
| 1435 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 1436 | { |
| 1437 | struct drm_i915_gem_object *obj; |
| 1438 | struct i915_vma *vma; |
| 1439 | int ret; |
| 1440 | |
| 1441 | obj = i915_gem_object_create_stolen(i915, size); |
| 1442 | if (!obj) |
| 1443 | obj = i915_gem_object_create_internal(i915, size); |
| 1444 | if (IS_ERR(obj)) { |
| 1445 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 1446 | return PTR_ERR(obj); |
| 1447 | } |
| 1448 | |
| 1449 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 1450 | if (IS_ERR(vma)) { |
| 1451 | ret = PTR_ERR(vma); |
| 1452 | goto err_unref; |
| 1453 | } |
| 1454 | |
| 1455 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 1456 | if (ret) |
| 1457 | goto err_unref; |
| 1458 | |
| 1459 | i915->gt.scratch = vma; |
| 1460 | return 0; |
| 1461 | |
| 1462 | err_unref: |
| 1463 | i915_gem_object_put(obj); |
| 1464 | return ret; |
| 1465 | } |
| 1466 | |
| 1467 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 1468 | { |
| 1469 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 1470 | } |
| 1471 | |
Chris Wilson | 254e118 | 2019-04-17 08:56:28 +0100 | [diff] [blame] | 1472 | static int intel_engines_verify_workarounds(struct drm_i915_private *i915) |
| 1473 | { |
| 1474 | struct intel_engine_cs *engine; |
| 1475 | enum intel_engine_id id; |
| 1476 | int err = 0; |
| 1477 | |
| 1478 | if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) |
| 1479 | return 0; |
| 1480 | |
| 1481 | for_each_engine(engine, i915, id) { |
| 1482 | if (intel_engine_verify_workarounds(engine, "load")) |
| 1483 | err = -EIO; |
| 1484 | } |
| 1485 | |
| 1486 | return err; |
| 1487 | } |
| 1488 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 1489 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 1490 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 1491 | int ret; |
| 1492 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 1493 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 1494 | 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] | 1495 | mkwrite_device_info(dev_priv)->page_sizes = |
| 1496 | I915_GTT_PAGE_SIZE_4K; |
| 1497 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 1498 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 1499 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 1500 | i915_timelines_init(dev_priv); |
| 1501 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 1502 | ret = i915_gem_init_userptr(dev_priv); |
| 1503 | if (ret) |
| 1504 | return ret; |
| 1505 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 1506 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 1507 | if (ret) |
| 1508 | return ret; |
| 1509 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 1510 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 1511 | if (ret) |
| 1512 | goto err_uc_misc; |
| 1513 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 1514 | /* This is just a security blanket to placate dragons. |
| 1515 | * On some systems, we very sporadically observe that the first TLBs |
| 1516 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 1517 | * we hold the forcewake during initialisation these problems |
| 1518 | * just magically go away. |
| 1519 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 1520 | mutex_lock(&dev_priv->drm.struct_mutex); |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 1521 | intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 1522 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 1523 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1524 | if (ret) { |
| 1525 | GEM_BUG_ON(ret == -EIO); |
| 1526 | goto err_unlock; |
| 1527 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 1528 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 1529 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 1530 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1531 | if (ret) { |
| 1532 | GEM_BUG_ON(ret == -EIO); |
| 1533 | goto err_ggtt; |
| 1534 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 1535 | |
Chris Wilson | 11334c6 | 2019-04-26 17:33:33 +0100 | [diff] [blame] | 1536 | ret = intel_engines_setup(dev_priv); |
| 1537 | if (ret) { |
| 1538 | GEM_BUG_ON(ret == -EIO); |
| 1539 | goto err_unlock; |
| 1540 | } |
| 1541 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 1542 | ret = i915_gem_contexts_init(dev_priv); |
| 1543 | if (ret) { |
| 1544 | GEM_BUG_ON(ret == -EIO); |
| 1545 | goto err_scratch; |
| 1546 | } |
| 1547 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 1548 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1549 | if (ret) { |
| 1550 | GEM_BUG_ON(ret == -EIO); |
| 1551 | goto err_context; |
| 1552 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 1553 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 1554 | intel_init_gt_powersave(dev_priv); |
| 1555 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 1556 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 1557 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1558 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 1559 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 1560 | ret = i915_gem_init_hw(dev_priv); |
| 1561 | if (ret) |
| 1562 | goto err_uc_init; |
| 1563 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 1564 | /* |
| 1565 | * Despite its name intel_init_clock_gating applies both display |
| 1566 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 1567 | * GT power context workaround. Worse, sometimes it includes a context |
| 1568 | * register workaround which we need to apply before we record the |
| 1569 | * default HW state for all contexts. |
| 1570 | * |
| 1571 | * FIXME: break up the workarounds and apply them at the right time! |
| 1572 | */ |
| 1573 | intel_init_clock_gating(dev_priv); |
| 1574 | |
Chris Wilson | 254e118 | 2019-04-17 08:56:28 +0100 | [diff] [blame] | 1575 | ret = intel_engines_verify_workarounds(dev_priv); |
| 1576 | if (ret) |
| 1577 | goto err_init_hw; |
| 1578 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 1579 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1580 | if (ret) |
| 1581 | goto err_init_hw; |
| 1582 | |
| 1583 | if (i915_inject_load_failure()) { |
| 1584 | ret = -ENODEV; |
| 1585 | goto err_init_hw; |
| 1586 | } |
| 1587 | |
| 1588 | if (i915_inject_load_failure()) { |
| 1589 | ret = -EIO; |
| 1590 | goto err_init_hw; |
| 1591 | } |
| 1592 | |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 1593 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1594 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1595 | |
| 1596 | return 0; |
| 1597 | |
| 1598 | /* |
| 1599 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 1600 | * disable GPU submission but keep KMS alive. We want to mark the |
| 1601 | * HW as irrevisibly wedged, but keep enough state around that the |
| 1602 | * driver doesn't explode during runtime. |
| 1603 | */ |
| 1604 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 1605 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1606 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 1607 | i915_gem_set_wedged(dev_priv); |
Chris Wilson | 5861b01 | 2019-03-08 09:36:54 +0000 | [diff] [blame] | 1608 | i915_gem_suspend(dev_priv); |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 1609 | i915_gem_suspend_late(dev_priv); |
| 1610 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 1611 | i915_gem_drain_workqueue(dev_priv); |
| 1612 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 1613 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1614 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 1615 | err_uc_init: |
| 1616 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1617 | err_pm: |
| 1618 | if (ret != -EIO) { |
| 1619 | intel_cleanup_gt_powersave(dev_priv); |
Chris Wilson | 45b9c96 | 2019-05-01 11:32:04 +0100 | [diff] [blame] | 1620 | intel_engines_cleanup(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1621 | } |
| 1622 | err_context: |
| 1623 | if (ret != -EIO) |
| 1624 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 1625 | err_scratch: |
| 1626 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1627 | err_ggtt: |
| 1628 | err_unlock: |
Daniele Ceraolo Spurio | 3ceea6a | 2019-03-19 11:35:36 -0700 | [diff] [blame] | 1629 | intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1630 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1631 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 1632 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 1633 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 1634 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 1635 | if (ret != -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1636 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 1637 | i915_timelines_fini(dev_priv); |
| 1638 | } |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1639 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 1640 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 1641 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 1642 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1643 | /* |
| 1644 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 1645 | * wedged. But we only want to do this where the GPU is angry, |
| 1646 | * for all other failure, such as an allocation failure, bail. |
| 1647 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1648 | if (!i915_reset_failed(dev_priv)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 1649 | i915_load_error(dev_priv, |
| 1650 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 1651 | i915_gem_set_wedged(dev_priv); |
| 1652 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 1653 | |
| 1654 | /* Minimal basic recovery for KMS */ |
| 1655 | ret = i915_ggtt_enable_hw(dev_priv); |
| 1656 | i915_gem_restore_gtt_mappings(dev_priv); |
| 1657 | i915_gem_restore_fences(dev_priv); |
| 1658 | intel_init_clock_gating(dev_priv); |
| 1659 | |
| 1660 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 1663 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 1664 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 1665 | } |
| 1666 | |
Janusz Krzysztofik | 47bc28d | 2019-05-30 15:31:05 +0200 | [diff] [blame^] | 1667 | void i915_gem_fini_hw(struct drm_i915_private *dev_priv) |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 1668 | { |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 1669 | GEM_BUG_ON(dev_priv->gt.awake); |
| 1670 | |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame] | 1671 | intel_wakeref_auto_fini(&dev_priv->mm.userfault_wakeref); |
| 1672 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 1673 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 1674 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 1675 | |
| 1676 | /* Flush any outstanding unpin_work. */ |
| 1677 | i915_gem_drain_workqueue(dev_priv); |
| 1678 | |
| 1679 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 1680 | intel_uc_fini_hw(dev_priv); |
| 1681 | intel_uc_fini(dev_priv); |
Janusz Krzysztofik | 47bc28d | 2019-05-30 15:31:05 +0200 | [diff] [blame^] | 1682 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1683 | |
| 1684 | i915_gem_drain_freed_objects(dev_priv); |
| 1685 | } |
| 1686 | |
| 1687 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 1688 | { |
| 1689 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 45b9c96 | 2019-05-01 11:32:04 +0100 | [diff] [blame] | 1690 | intel_engines_cleanup(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 1691 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 1692 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 1693 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1694 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 1695 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 1696 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 1697 | intel_cleanup_gt_powersave(dev_priv); |
| 1698 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 1699 | intel_uc_fini_misc(dev_priv); |
| 1700 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 1701 | i915_timelines_fini(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 1702 | |
| 1703 | i915_gem_drain_freed_objects(dev_priv); |
| 1704 | |
| 1705 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 1706 | } |
| 1707 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 1708 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 1709 | { |
| 1710 | i915_gem_sanitize(i915); |
| 1711 | } |
| 1712 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 1713 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 1714 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 1715 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1716 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 1717 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 1718 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 1719 | !IS_CHERRYVIEW(dev_priv)) |
| 1720 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 1721 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 1722 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 1723 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 1724 | dev_priv->num_fence_regs = 16; |
| 1725 | else |
| 1726 | dev_priv->num_fence_regs = 8; |
| 1727 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 1728 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 1729 | dev_priv->num_fence_regs = |
| 1730 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 1731 | |
| 1732 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1733 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 1734 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 1735 | |
| 1736 | fence->i915 = dev_priv; |
| 1737 | fence->id = i; |
| 1738 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 1739 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 1740 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 1741 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 1742 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 1745 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 1746 | { |
| 1747 | spin_lock_init(&i915->mm.object_stat_lock); |
| 1748 | spin_lock_init(&i915->mm.obj_lock); |
| 1749 | spin_lock_init(&i915->mm.free_lock); |
| 1750 | |
| 1751 | init_llist_head(&i915->mm.free_list); |
| 1752 | |
| 1753 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 1754 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 1755 | INIT_LIST_HEAD(&i915->mm.fence_list); |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame] | 1756 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 1757 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame] | 1758 | intel_wakeref_auto_init(&i915->mm.userfault_wakeref, i915); |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 1759 | |
Chris Wilson | 8475355 | 2019-05-28 10:29:45 +0100 | [diff] [blame] | 1760 | i915_gem_init__objects(i915); |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 1763 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1764 | { |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 1765 | int err; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 1766 | |
Chris Wilson | 79ffac85 | 2019-04-24 21:07:17 +0100 | [diff] [blame] | 1767 | intel_gt_pm_init(dev_priv); |
| 1768 | |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 1769 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 1770 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 1771 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 1772 | i915_gem_init__mm(dev_priv); |
Chris Wilson | 23c3c3d | 2019-04-24 21:07:14 +0100 | [diff] [blame] | 1773 | i915_gem_init__pm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1774 | |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 1775 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 1776 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 1777 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 1778 | init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 1779 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 1780 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 1781 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 1782 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 1783 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 1784 | err = i915_gemfs_init(dev_priv); |
| 1785 | if (err) |
| 1786 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 1787 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 1788 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1789 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 1790 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 1791 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 1792 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 1793 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 1794 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 1795 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 1796 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 1797 | |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 1798 | cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
| 1799 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 1800 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 1801 | } |
| 1802 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 1803 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 1804 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 1805 | /* Discard all purgeable objects, let userspace recover those as |
| 1806 | * required after resuming. |
| 1807 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 1808 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 1809 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 1810 | return 0; |
| 1811 | } |
| 1812 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 1813 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 1814 | { |
| 1815 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 1816 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 1817 | &i915->mm.unbound_list, |
| 1818 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 1819 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 1820 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 1821 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 1822 | /* |
| 1823 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 1824 | * |
| 1825 | * We need to update the domain tracking to reflect that the CPU |
| 1826 | * will be accessing all the pages to create and restore from the |
| 1827 | * hibernation, and so upon restoration those pages will be in the |
| 1828 | * CPU domain. |
| 1829 | * |
| 1830 | * To make sure the hibernation image contains the latest state, |
| 1831 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 1832 | * |
| 1833 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 1834 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 1835 | */ |
| 1836 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 1837 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 1838 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 1839 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 1840 | for (phase = phases; *phase; phase++) { |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 1841 | list_for_each_entry(obj, *phase, mm.link) { |
| 1842 | i915_gem_object_lock(obj); |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 1843 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 1844 | i915_gem_object_unlock(obj); |
| 1845 | } |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | return 0; |
| 1849 | } |
| 1850 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1851 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1852 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1853 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 1854 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* Clean up our request list when the client is going away, so that |
| 1857 | * later retire_requests won't dereference our soon-to-be-gone |
| 1858 | * file_priv. |
| 1859 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1860 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 1861 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1862 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1863 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 1864 | } |
| 1865 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 1866 | 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] | 1867 | { |
| 1868 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 1869 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 1870 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 1871 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 1872 | |
| 1873 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 1874 | if (!file_priv) |
| 1875 | return -ENOMEM; |
| 1876 | |
| 1877 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 1878 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 1879 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 1880 | |
| 1881 | spin_lock_init(&file_priv->mm.lock); |
| 1882 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 1883 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 1884 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 1885 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1886 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 1887 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 1888 | if (ret) |
| 1889 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 1890 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 1891 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 1892 | } |
| 1893 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 1894 | /** |
| 1895 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1896 | * @old: current GEM buffer for the frontbuffer slots |
| 1897 | * @new: new GEM buffer for the frontbuffer slots |
| 1898 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 1899 | * |
| 1900 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 1901 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 1902 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 1903 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 1904 | struct drm_i915_gem_object *new, |
| 1905 | unsigned frontbuffer_bits) |
| 1906 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 1907 | /* Control of individual bits within the mask are guarded by |
| 1908 | * the owning plane->mutex, i.e. we can never see concurrent |
| 1909 | * manipulation of individual bits. But since the bitfield as a whole |
| 1910 | * is updated using RMW, we need to use atomics in order to update |
| 1911 | * the bits. |
| 1912 | */ |
| 1913 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 1914 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 1915 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 1916 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 1917 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 1918 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 1922 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 1923 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 1927 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 1928 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 1929 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 1930 | #endif |