blob: 275e9e0799b9f97c2355213c3abf9cc5fc7b89e3 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
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 Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000038#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000039#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010040#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070041#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000043#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070044#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080045#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020046#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070047
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010048static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson05394f32010-11-08 19:18:58 +000049static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
Daniel Vettere62b59e2015-01-21 14:53:48 +010050static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson61050802012-04-17 15:31:31 +010051
Chris Wilson2c225692013-08-09 12:26:45 +010052static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
53{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053054 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
55 return false;
56
Chris Wilsone59dc172017-02-22 11:40:45 +000057 if (!i915_gem_object_is_coherent(obj))
Chris Wilson2c225692013-08-09 12:26:45 +010058 return true;
59
60 return obj->pin_display;
61}
62
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053063static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010064insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053065 struct drm_mm_node *node, u32 size)
66{
67 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000068 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
69 size, 0, I915_COLOR_UNEVICTABLE,
70 0, ggtt->mappable_end,
71 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053072}
73
74static void
75remove_mappable_node(struct drm_mm_node *node)
76{
77 drm_mm_remove_node(node);
78}
79
Chris Wilson73aa8082010-09-30 11:46:12 +010080/* some bookkeeping */
81static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010082 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010083{
Daniel Vetterc20e8352013-07-24 22:40:23 +020084 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010085 dev_priv->mm.object_count++;
86 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020087 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010088}
89
90static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010091 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010092{
Daniel Vetterc20e8352013-07-24 22:40:23 +020093 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010094 dev_priv->mm.object_count--;
95 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020096 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010097}
98
Chris Wilson21dd3732011-01-26 15:55:56 +000099static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100100i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100102 int ret;
103
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100104 might_sleep();
105
Chris Wilsond98c52c2016-04-13 17:35:05 +0100106 if (!i915_reset_in_progress(error))
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100107 return 0;
108
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200109 /*
110 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
111 * userspace. If it takes that long something really bad is going on and
112 * we should simply try to bail out and fail as gracefully as possible.
113 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100114 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilsond98c52c2016-04-13 17:35:05 +0100115 !i915_reset_in_progress(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100116 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200117 if (ret == 0) {
118 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
119 return -EIO;
120 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100122 } else {
123 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200124 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100125}
126
Chris Wilson54cf91d2010-11-25 18:00:26 +0000127int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100128{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100129 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100130 int ret;
131
Daniel Vetter33196de2012-11-14 17:14:05 +0100132 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100133 if (ret)
134 return ret;
135
136 ret = mutex_lock_interruptible(&dev->struct_mutex);
137 if (ret)
138 return ret;
139
Chris Wilson76c1dec2010-09-25 11:22:51 +0100140 return 0;
141}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100142
Eric Anholt673a3942008-07-30 12:06:12 -0700143int
Eric Anholt5a125c32008-10-22 21:40:13 -0700144i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000145 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700146{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300147 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200148 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300149 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100150 struct i915_vma *vma;
Chris Wilson6299f992010-11-24 12:23:44 +0000151 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700152
Chris Wilson6299f992010-11-24 12:23:44 +0000153 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100154 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000155 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100156 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100157 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000158 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100159 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100160 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100161 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700162
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300163 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400164 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000165
Eric Anholt5a125c32008-10-22 21:40:13 -0700166 return 0;
167}
168
Chris Wilson03ac84f2016-10-28 13:58:36 +0100169static struct sg_table *
Chris Wilson6a2c4232014-11-04 04:51:40 -0800170i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100171{
Al Viro93c76a32015-12-04 23:45:44 -0500172 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000173 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800174 struct sg_table *st;
175 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000176 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800177 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100178
Chris Wilson6a2c4232014-11-04 04:51:40 -0800179 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Chris Wilson03ac84f2016-10-28 13:58:36 +0100180 return ERR_PTR(-EINVAL);
Chris Wilson00731152014-05-21 12:42:56 +0100181
Chris Wilsondbb43512016-12-07 13:34:11 +0000182 /* Always aligning to the object size, allows a single allocation
183 * to handle all possible callers, and given typical object sizes,
184 * the alignment of the buddy allocation will naturally match.
185 */
186 phys = drm_pci_alloc(obj->base.dev,
187 obj->base.size,
188 roundup_pow_of_two(obj->base.size));
189 if (!phys)
190 return ERR_PTR(-ENOMEM);
191
192 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800193 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
194 struct page *page;
195 char *src;
196
197 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000198 if (IS_ERR(page)) {
199 st = ERR_CAST(page);
200 goto err_phys;
201 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800202
203 src = kmap_atomic(page);
204 memcpy(vaddr, src, PAGE_SIZE);
205 drm_clflush_virt_range(vaddr, PAGE_SIZE);
206 kunmap_atomic(src);
207
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300208 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800209 vaddr += PAGE_SIZE;
210 }
211
Chris Wilsonc0336662016-05-06 15:40:21 +0100212 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800213
214 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000215 if (!st) {
216 st = ERR_PTR(-ENOMEM);
217 goto err_phys;
218 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800219
220 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
221 kfree(st);
Chris Wilsondbb43512016-12-07 13:34:11 +0000222 st = ERR_PTR(-ENOMEM);
223 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800224 }
225
226 sg = st->sgl;
227 sg->offset = 0;
228 sg->length = obj->base.size;
229
Chris Wilsondbb43512016-12-07 13:34:11 +0000230 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800231 sg_dma_len(sg) = obj->base.size;
232
Chris Wilsondbb43512016-12-07 13:34:11 +0000233 obj->phys_handle = phys;
234 return st;
235
236err_phys:
237 drm_pci_free(obj->base.dev, phys);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100238 return st;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239}
240
241static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000242__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000243 struct sg_table *pages,
244 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800245{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100246 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800247
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100248 if (obj->mm.madv == I915_MADV_DONTNEED)
249 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800250
Chris Wilsone5facdf2016-12-23 14:57:57 +0000251 if (needs_clflush &&
252 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsone59dc172017-02-22 11:40:45 +0000253 !i915_gem_object_is_coherent(obj))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000254 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100255
256 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
257 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
258}
259
260static void
261i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
262 struct sg_table *pages)
263{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000264 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100265
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100266 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500267 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800268 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100269 int i;
270
271 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800272 struct page *page;
273 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100274
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 page = shmem_read_mapping_page(mapping, i);
276 if (IS_ERR(page))
277 continue;
278
279 dst = kmap_atomic(page);
280 drm_clflush_virt_range(vaddr, PAGE_SIZE);
281 memcpy(dst, vaddr, PAGE_SIZE);
282 kunmap_atomic(dst);
283
284 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100285 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100286 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300287 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100288 vaddr += PAGE_SIZE;
289 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100290 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100291 }
292
Chris Wilson03ac84f2016-10-28 13:58:36 +0100293 sg_free_table(pages);
294 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000295
296 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800297}
298
299static void
300i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
301{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100302 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800303}
304
305static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
306 .get_pages = i915_gem_object_get_pages_phys,
307 .put_pages = i915_gem_object_put_pages_phys,
308 .release = i915_gem_object_release_phys,
309};
310
Chris Wilson581ab1f2017-02-15 16:39:00 +0000311static const struct drm_i915_gem_object_ops i915_gem_object_ops;
312
Chris Wilson35a96112016-08-14 18:44:40 +0100313int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100314{
315 struct i915_vma *vma;
316 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100317 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100318
Chris Wilson02bef8f2016-08-14 18:44:41 +0100319 lockdep_assert_held(&obj->base.dev->struct_mutex);
320
321 /* Closed vma are removed from the obj->vma_list - but they may
322 * still have an active binding on the object. To remove those we
323 * must wait for all rendering to complete to the object (as unbinding
324 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100325 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100326 ret = i915_gem_object_wait(obj,
327 I915_WAIT_INTERRUPTIBLE |
328 I915_WAIT_LOCKED |
329 I915_WAIT_ALL,
330 MAX_SCHEDULE_TIMEOUT,
331 NULL);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100332 if (ret)
333 return ret;
334
335 i915_gem_retire_requests(to_i915(obj->base.dev));
336
Chris Wilsonaa653a62016-08-04 07:52:27 +0100337 while ((vma = list_first_entry_or_null(&obj->vma_list,
338 struct i915_vma,
339 obj_link))) {
340 list_move_tail(&vma->obj_link, &still_in_list);
341 ret = i915_vma_unbind(vma);
342 if (ret)
343 break;
344 }
345 list_splice(&still_in_list, &obj->vma_list);
346
347 return ret;
348}
349
Chris Wilsone95433c2016-10-28 13:58:27 +0100350static long
351i915_gem_object_wait_fence(struct dma_fence *fence,
352 unsigned int flags,
353 long timeout,
354 struct intel_rps_client *rps)
355{
356 struct drm_i915_gem_request *rq;
357
358 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
359
360 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
361 return timeout;
362
363 if (!dma_fence_is_i915(fence))
364 return dma_fence_wait_timeout(fence,
365 flags & I915_WAIT_INTERRUPTIBLE,
366 timeout);
367
368 rq = to_request(fence);
369 if (i915_gem_request_completed(rq))
370 goto out;
371
372 /* This client is about to stall waiting for the GPU. In many cases
373 * this is undesirable and limits the throughput of the system, as
374 * many clients cannot continue processing user input/output whilst
375 * blocked. RPS autotuning may take tens of milliseconds to respond
376 * to the GPU load and thus incurs additional latency for the client.
377 * We can circumvent that by promoting the GPU frequency to maximum
378 * before we wait. This makes the GPU throttle up much more quickly
379 * (good for benchmarks and user experience, e.g. window animations),
380 * but at a cost of spending more power processing the workload
381 * (bad for battery). Not all clients even want their results
382 * immediately and for them we should just let the GPU select its own
383 * frequency to maximise efficiency. To prevent a single client from
384 * forcing the clocks too high for the whole system, we only allow
385 * each client to waitboost once in a busy period.
386 */
387 if (rps) {
388 if (INTEL_GEN(rq->i915) >= 6)
389 gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
390 else
391 rps = NULL;
392 }
393
394 timeout = i915_wait_request(rq, flags, timeout);
395
396out:
397 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
398 i915_gem_request_retire_upto(rq);
399
Chris Wilson754c9fd2017-02-23 07:44:14 +0000400 if (rps && i915_gem_request_global_seqno(rq) == intel_engine_last_submit(rq->engine)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100401 /* The GPU is now idle and this client has stalled.
402 * Since no other client has submitted a request in the
403 * meantime, assume that this client is the only one
404 * supplying work to the GPU but is unable to keep that
405 * work supplied because it is waiting. Since the GPU is
406 * then never kept fully busy, RPS autoclocking will
407 * keep the clocks relatively low, causing further delays.
408 * Compensate by giving the synchronous client credit for
409 * a waitboost next time.
410 */
411 spin_lock(&rq->i915->rps.client_lock);
412 list_del_init(&rps->link);
413 spin_unlock(&rq->i915->rps.client_lock);
414 }
415
416 return timeout;
417}
418
419static long
420i915_gem_object_wait_reservation(struct reservation_object *resv,
421 unsigned int flags,
422 long timeout,
423 struct intel_rps_client *rps)
424{
Chris Wilsone54ca972017-02-17 15:13:04 +0000425 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100426 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000427 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100428
429 if (flags & I915_WAIT_ALL) {
430 struct dma_fence **shared;
431 unsigned int count, i;
432 int ret;
433
434 ret = reservation_object_get_fences_rcu(resv,
435 &excl, &count, &shared);
436 if (ret)
437 return ret;
438
439 for (i = 0; i < count; i++) {
440 timeout = i915_gem_object_wait_fence(shared[i],
441 flags, timeout,
442 rps);
Chris Wilsond892e932017-02-12 21:53:43 +0000443 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100444 break;
445
446 dma_fence_put(shared[i]);
447 }
448
449 for (; i < count; i++)
450 dma_fence_put(shared[i]);
451 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000452
453 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100454 } else {
455 excl = reservation_object_get_excl_rcu(resv);
456 }
457
Chris Wilsone54ca972017-02-17 15:13:04 +0000458 if (excl && timeout >= 0) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100459 timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
Chris Wilsone54ca972017-02-17 15:13:04 +0000460 prune_fences = timeout >= 0;
461 }
Chris Wilsone95433c2016-10-28 13:58:27 +0100462
463 dma_fence_put(excl);
464
Chris Wilsone54ca972017-02-17 15:13:04 +0000465 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
466 reservation_object_lock(resv, NULL);
467 if (!__read_seqcount_retry(&resv->seq, seq))
468 reservation_object_add_excl_fence(resv, NULL);
469 reservation_object_unlock(resv);
470 }
471
Chris Wilsone95433c2016-10-28 13:58:27 +0100472 return timeout;
473}
474
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000475static void __fence_set_priority(struct dma_fence *fence, int prio)
476{
477 struct drm_i915_gem_request *rq;
478 struct intel_engine_cs *engine;
479
480 if (!dma_fence_is_i915(fence))
481 return;
482
483 rq = to_request(fence);
484 engine = rq->engine;
485 if (!engine->schedule)
486 return;
487
488 engine->schedule(rq, prio);
489}
490
491static void fence_set_priority(struct dma_fence *fence, int prio)
492{
493 /* Recurse once into a fence-array */
494 if (dma_fence_is_array(fence)) {
495 struct dma_fence_array *array = to_dma_fence_array(fence);
496 int i;
497
498 for (i = 0; i < array->num_fences; i++)
499 __fence_set_priority(array->fences[i], prio);
500 } else {
501 __fence_set_priority(fence, prio);
502 }
503}
504
505int
506i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
507 unsigned int flags,
508 int prio)
509{
510 struct dma_fence *excl;
511
512 if (flags & I915_WAIT_ALL) {
513 struct dma_fence **shared;
514 unsigned int count, i;
515 int ret;
516
517 ret = reservation_object_get_fences_rcu(obj->resv,
518 &excl, &count, &shared);
519 if (ret)
520 return ret;
521
522 for (i = 0; i < count; i++) {
523 fence_set_priority(shared[i], prio);
524 dma_fence_put(shared[i]);
525 }
526
527 kfree(shared);
528 } else {
529 excl = reservation_object_get_excl_rcu(obj->resv);
530 }
531
532 if (excl) {
533 fence_set_priority(excl, prio);
534 dma_fence_put(excl);
535 }
536 return 0;
537}
538
Chris Wilson00e60f22016-08-04 16:32:40 +0100539/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100540 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100541 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100542 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
543 * @timeout: how long to wait
544 * @rps: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100545 */
546int
Chris Wilsone95433c2016-10-28 13:58:27 +0100547i915_gem_object_wait(struct drm_i915_gem_object *obj,
548 unsigned int flags,
549 long timeout,
550 struct intel_rps_client *rps)
Chris Wilson00e60f22016-08-04 16:32:40 +0100551{
Chris Wilsone95433c2016-10-28 13:58:27 +0100552 might_sleep();
553#if IS_ENABLED(CONFIG_LOCKDEP)
554 GEM_BUG_ON(debug_locks &&
555 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
556 !!(flags & I915_WAIT_LOCKED));
557#endif
558 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100559
Chris Wilsond07f0e52016-10-28 13:58:44 +0100560 timeout = i915_gem_object_wait_reservation(obj->resv,
561 flags, timeout,
562 rps);
Chris Wilsone95433c2016-10-28 13:58:27 +0100563 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100564}
565
566static struct intel_rps_client *to_rps_client(struct drm_file *file)
567{
568 struct drm_i915_file_private *fpriv = file->driver_priv;
569
570 return &fpriv->rps;
571}
572
Chris Wilson00731152014-05-21 12:42:56 +0100573int
574i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
575 int align)
576{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800577 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100578
Chris Wilsondbb43512016-12-07 13:34:11 +0000579 if (align > obj->base.size)
580 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100581
Chris Wilsondbb43512016-12-07 13:34:11 +0000582 if (obj->ops == &i915_gem_phys_ops)
Chris Wilson00731152014-05-21 12:42:56 +0100583 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100584
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100585 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100586 return -EFAULT;
587
588 if (obj->base.filp == NULL)
589 return -EINVAL;
590
Chris Wilson4717ca92016-08-04 07:52:28 +0100591 ret = i915_gem_object_unbind(obj);
592 if (ret)
593 return ret;
594
Chris Wilson548625e2016-11-01 12:11:34 +0000595 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100596 if (obj->mm.pages)
597 return -EBUSY;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800598
Chris Wilson581ab1f2017-02-15 16:39:00 +0000599 GEM_BUG_ON(obj->ops != &i915_gem_object_ops);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800600 obj->ops = &i915_gem_phys_ops;
601
Chris Wilson581ab1f2017-02-15 16:39:00 +0000602 ret = i915_gem_object_pin_pages(obj);
603 if (ret)
604 goto err_xfer;
605
606 return 0;
607
608err_xfer:
609 obj->ops = &i915_gem_object_ops;
610 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100611}
612
613static int
614i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
615 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100616 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100617{
Chris Wilson00731152014-05-21 12:42:56 +0100618 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300619 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800620
621 /* We manually control the domain here and pretend that it
622 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
623 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700624 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000625 if (copy_from_user(vaddr, user_data, args->size))
626 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100627
Chris Wilson6a2c4232014-11-04 04:51:40 -0800628 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000629 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200630
Chris Wilsond59b21e2017-02-22 11:40:49 +0000631 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000632 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100633}
634
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000635void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000636{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100637 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000638}
639
640void i915_gem_object_free(struct drm_i915_gem_object *obj)
641{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100642 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100643 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000644}
645
Dave Airlieff72145b2011-02-07 12:16:14 +1000646static int
647i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000648 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000649 uint64_t size,
650 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700651{
Chris Wilson05394f32010-11-08 19:18:58 +0000652 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300653 int ret;
654 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700655
Dave Airlieff72145b2011-02-07 12:16:14 +1000656 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200657 if (size == 0)
658 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700659
660 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000661 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100662 if (IS_ERR(obj))
663 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700664
Chris Wilson05394f32010-11-08 19:18:58 +0000665 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100666 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100667 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200668 if (ret)
669 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100670
Dave Airlieff72145b2011-02-07 12:16:14 +1000671 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700672 return 0;
673}
674
Dave Airlieff72145b2011-02-07 12:16:14 +1000675int
676i915_gem_dumb_create(struct drm_file *file,
677 struct drm_device *dev,
678 struct drm_mode_create_dumb *args)
679{
680 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300681 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000682 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000683 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000684 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000685}
686
Dave Airlieff72145b2011-02-07 12:16:14 +1000687/**
688 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100689 * @dev: drm device pointer
690 * @data: ioctl data blob
691 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000692 */
693int
694i915_gem_create_ioctl(struct drm_device *dev, void *data,
695 struct drm_file *file)
696{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000697 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000698 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200699
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000700 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100701
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000702 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000703 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000704}
705
Daniel Vetter8c599672011-12-14 13:57:31 +0100706static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100707__copy_to_user_swizzled(char __user *cpu_vaddr,
708 const char *gpu_vaddr, int gpu_offset,
709 int length)
710{
711 int ret, cpu_offset = 0;
712
713 while (length > 0) {
714 int cacheline_end = ALIGN(gpu_offset + 1, 64);
715 int this_length = min(cacheline_end - gpu_offset, length);
716 int swizzled_gpu_offset = gpu_offset ^ 64;
717
718 ret = __copy_to_user(cpu_vaddr + cpu_offset,
719 gpu_vaddr + swizzled_gpu_offset,
720 this_length);
721 if (ret)
722 return ret + length;
723
724 cpu_offset += this_length;
725 gpu_offset += this_length;
726 length -= this_length;
727 }
728
729 return 0;
730}
731
732static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700733__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
734 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100735 int length)
736{
737 int ret, cpu_offset = 0;
738
739 while (length > 0) {
740 int cacheline_end = ALIGN(gpu_offset + 1, 64);
741 int this_length = min(cacheline_end - gpu_offset, length);
742 int swizzled_gpu_offset = gpu_offset ^ 64;
743
744 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
745 cpu_vaddr + cpu_offset,
746 this_length);
747 if (ret)
748 return ret + length;
749
750 cpu_offset += this_length;
751 gpu_offset += this_length;
752 length -= this_length;
753 }
754
755 return 0;
756}
757
Brad Volkin4c914c02014-02-18 10:15:45 -0800758/*
759 * Pins the specified object's pages and synchronizes the object with
760 * GPU accesses. Sets needs_clflush to non-zero if the caller should
761 * flush the object from the CPU cache.
762 */
763int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100764 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800765{
766 int ret;
767
Chris Wilsone95433c2016-10-28 13:58:27 +0100768 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800769
Chris Wilsone95433c2016-10-28 13:58:27 +0100770 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100771 if (!i915_gem_object_has_struct_page(obj))
772 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800773
Chris Wilsone95433c2016-10-28 13:58:27 +0100774 ret = i915_gem_object_wait(obj,
775 I915_WAIT_INTERRUPTIBLE |
776 I915_WAIT_LOCKED,
777 MAX_SCHEDULE_TIMEOUT,
778 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100779 if (ret)
780 return ret;
781
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100782 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100783 if (ret)
784 return ret;
785
Chris Wilsona314d5c2016-08-18 17:16:48 +0100786 i915_gem_object_flush_gtt_write_domain(obj);
787
Chris Wilson43394c72016-08-18 17:16:47 +0100788 /* If we're not in the cpu read domain, set ourself into the gtt
789 * read domain and manually flush cachelines (if required). This
790 * optimizes for the case when the gpu will dirty the data
791 * anyway again before the next pread happens.
792 */
793 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone59dc172017-02-22 11:40:45 +0000794 *needs_clflush = !i915_gem_object_is_coherent(obj);
Brad Volkin4c914c02014-02-18 10:15:45 -0800795
Chris Wilson43394c72016-08-18 17:16:47 +0100796 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
797 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson97649512016-08-18 17:16:50 +0100798 if (ret)
799 goto err_unpin;
800
Chris Wilson43394c72016-08-18 17:16:47 +0100801 *needs_clflush = 0;
802 }
803
Chris Wilson97649512016-08-18 17:16:50 +0100804 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100805 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100806
807err_unpin:
808 i915_gem_object_unpin_pages(obj);
809 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100810}
811
812int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
813 unsigned int *needs_clflush)
814{
815 int ret;
816
Chris Wilsone95433c2016-10-28 13:58:27 +0100817 lockdep_assert_held(&obj->base.dev->struct_mutex);
818
Chris Wilson43394c72016-08-18 17:16:47 +0100819 *needs_clflush = 0;
820 if (!i915_gem_object_has_struct_page(obj))
821 return -ENODEV;
822
Chris Wilsone95433c2016-10-28 13:58:27 +0100823 ret = i915_gem_object_wait(obj,
824 I915_WAIT_INTERRUPTIBLE |
825 I915_WAIT_LOCKED |
826 I915_WAIT_ALL,
827 MAX_SCHEDULE_TIMEOUT,
828 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100829 if (ret)
830 return ret;
831
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100832 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100833 if (ret)
834 return ret;
835
Chris Wilsona314d5c2016-08-18 17:16:48 +0100836 i915_gem_object_flush_gtt_write_domain(obj);
837
Chris Wilson43394c72016-08-18 17:16:47 +0100838 /* If we're not in the cpu write domain, set ourself into the
839 * gtt write domain and manually flush cachelines (as required).
840 * This optimizes for the case when the gpu will use the data
841 * right away and we therefore have to clflush anyway.
842 */
843 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
844 *needs_clflush |= cpu_write_needs_clflush(obj) << 1;
845
846 /* Same trick applies to invalidate partially written cachelines read
847 * before writing.
848 */
849 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone59dc172017-02-22 11:40:45 +0000850 *needs_clflush |= !i915_gem_object_is_coherent(obj);
Chris Wilson43394c72016-08-18 17:16:47 +0100851
Chris Wilson43394c72016-08-18 17:16:47 +0100852 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
853 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilson97649512016-08-18 17:16:50 +0100854 if (ret)
855 goto err_unpin;
856
Chris Wilson43394c72016-08-18 17:16:47 +0100857 *needs_clflush = 0;
858 }
859
860 if ((*needs_clflush & CLFLUSH_AFTER) == 0)
861 obj->cache_dirty = true;
862
863 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100864 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100865 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100866 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100867
868err_unpin:
869 i915_gem_object_unpin_pages(obj);
870 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800871}
872
Daniel Vetter23c18c72012-03-25 19:47:42 +0200873static void
874shmem_clflush_swizzled_range(char *addr, unsigned long length,
875 bool swizzled)
876{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200877 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200878 unsigned long start = (unsigned long) addr;
879 unsigned long end = (unsigned long) addr + length;
880
881 /* For swizzling simply ensure that we always flush both
882 * channels. Lame, but simple and it works. Swizzled
883 * pwrite/pread is far from a hotpath - current userspace
884 * doesn't use it at all. */
885 start = round_down(start, 128);
886 end = round_up(end, 128);
887
888 drm_clflush_virt_range((void *)start, end - start);
889 } else {
890 drm_clflush_virt_range(addr, length);
891 }
892
893}
894
Daniel Vetterd174bd62012-03-25 19:47:40 +0200895/* Only difference to the fast-path function is that this can handle bit17
896 * and uses non-atomic copy and kmap functions. */
897static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100898shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200899 char __user *user_data,
900 bool page_do_bit17_swizzling, bool needs_clflush)
901{
902 char *vaddr;
903 int ret;
904
905 vaddr = kmap(page);
906 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100907 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200908 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200909
910 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100911 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200912 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100913 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200914 kunmap(page);
915
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100916 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200917}
918
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100919static int
920shmem_pread(struct page *page, int offset, int length, char __user *user_data,
921 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530922{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100923 int ret;
924
925 ret = -ENODEV;
926 if (!page_do_bit17_swizzling) {
927 char *vaddr = kmap_atomic(page);
928
929 if (needs_clflush)
930 drm_clflush_virt_range(vaddr + offset, length);
931 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
932 kunmap_atomic(vaddr);
933 }
934 if (ret == 0)
935 return 0;
936
937 return shmem_pread_slow(page, offset, length, user_data,
938 page_do_bit17_swizzling, needs_clflush);
939}
940
941static int
942i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
943 struct drm_i915_gem_pread *args)
944{
945 char __user *user_data;
946 u64 remain;
947 unsigned int obj_do_bit17_swizzling;
948 unsigned int needs_clflush;
949 unsigned int idx, offset;
950 int ret;
951
952 obj_do_bit17_swizzling = 0;
953 if (i915_gem_object_needs_bit17_swizzle(obj))
954 obj_do_bit17_swizzling = BIT(17);
955
956 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
957 if (ret)
958 return ret;
959
960 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
961 mutex_unlock(&obj->base.dev->struct_mutex);
962 if (ret)
963 return ret;
964
965 remain = args->size;
966 user_data = u64_to_user_ptr(args->data_ptr);
967 offset = offset_in_page(args->offset);
968 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
969 struct page *page = i915_gem_object_get_page(obj, idx);
970 int length;
971
972 length = remain;
973 if (offset + length > PAGE_SIZE)
974 length = PAGE_SIZE - offset;
975
976 ret = shmem_pread(page, offset, length, user_data,
977 page_to_phys(page) & obj_do_bit17_swizzling,
978 needs_clflush);
979 if (ret)
980 break;
981
982 remain -= length;
983 user_data += length;
984 offset = 0;
985 }
986
987 i915_gem_obj_finish_shmem_access(obj);
988 return ret;
989}
990
991static inline bool
992gtt_user_read(struct io_mapping *mapping,
993 loff_t base, int offset,
994 char __user *user_data, int length)
995{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530996 void *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100997 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530998
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530999 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001000 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1001 unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1002 io_mapping_unmap_atomic(vaddr);
1003 if (unwritten) {
1004 vaddr = (void __force *)
1005 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1006 unwritten = copy_to_user(user_data, vaddr + offset, length);
1007 io_mapping_unmap(vaddr);
1008 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301009 return unwritten;
1010}
1011
1012static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001013i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1014 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301015{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001016 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1017 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301018 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001019 struct i915_vma *vma;
1020 void __user *user_data;
1021 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301022 int ret;
1023
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001024 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1025 if (ret)
1026 return ret;
1027
1028 intel_runtime_pm_get(i915);
1029 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1030 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001031 if (!IS_ERR(vma)) {
1032 node.start = i915_ggtt_offset(vma);
1033 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001034 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001035 if (ret) {
1036 i915_vma_unpin(vma);
1037 vma = ERR_PTR(ret);
1038 }
1039 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001040 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001041 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301042 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001043 goto out_unlock;
1044 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301045 }
1046
1047 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1048 if (ret)
1049 goto out_unpin;
1050
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001051 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301052
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001053 user_data = u64_to_user_ptr(args->data_ptr);
1054 remain = args->size;
1055 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301056
1057 while (remain > 0) {
1058 /* Operation in this page
1059 *
1060 * page_base = page offset within aperture
1061 * page_offset = offset within page
1062 * page_length = bytes to copy for this page
1063 */
1064 u32 page_base = node.start;
1065 unsigned page_offset = offset_in_page(offset);
1066 unsigned page_length = PAGE_SIZE - page_offset;
1067 page_length = remain < page_length ? remain : page_length;
1068 if (node.allocated) {
1069 wmb();
1070 ggtt->base.insert_page(&ggtt->base,
1071 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001072 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301073 wmb();
1074 } else {
1075 page_base += offset & PAGE_MASK;
1076 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001077
1078 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1079 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301080 ret = -EFAULT;
1081 break;
1082 }
1083
1084 remain -= page_length;
1085 user_data += page_length;
1086 offset += page_length;
1087 }
1088
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001089 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301090out_unpin:
1091 if (node.allocated) {
1092 wmb();
1093 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001094 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301095 remove_mappable_node(&node);
1096 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001097 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301098 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001099out_unlock:
1100 intel_runtime_pm_put(i915);
1101 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001102
Eric Anholteb014592009-03-10 11:44:52 -07001103 return ret;
1104}
1105
Eric Anholt673a3942008-07-30 12:06:12 -07001106/**
1107 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001108 * @dev: drm device pointer
1109 * @data: ioctl data blob
1110 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001111 *
1112 * On error, the contents of *data are undefined.
1113 */
1114int
1115i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001116 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001117{
1118 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001119 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001120 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001121
Chris Wilson51311d02010-11-17 09:10:42 +00001122 if (args->size == 0)
1123 return 0;
1124
1125 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001126 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001127 args->size))
1128 return -EFAULT;
1129
Chris Wilson03ac0642016-07-20 13:31:51 +01001130 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001131 if (!obj)
1132 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001133
Chris Wilson7dcd2492010-09-26 20:21:44 +01001134 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001135 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001136 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001137 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001138 }
1139
Chris Wilsondb53a302011-02-03 11:57:46 +00001140 trace_i915_gem_object_pread(obj, args->offset, args->size);
1141
Chris Wilsone95433c2016-10-28 13:58:27 +01001142 ret = i915_gem_object_wait(obj,
1143 I915_WAIT_INTERRUPTIBLE,
1144 MAX_SCHEDULE_TIMEOUT,
1145 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001146 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001147 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001148
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001149 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001150 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001151 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001152
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001153 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001154 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001155 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301156
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001157 i915_gem_object_unpin_pages(obj);
1158out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001159 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001160 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001161}
1162
Keith Packard0839ccb2008-10-30 19:38:48 -07001163/* This is the fast write path which cannot handle
1164 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001165 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001166
Chris Wilsonfe115622016-10-28 13:58:40 +01001167static inline bool
1168ggtt_write(struct io_mapping *mapping,
1169 loff_t base, int offset,
1170 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001171{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001172 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001173 unsigned long unwritten;
1174
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001175 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonfe115622016-10-28 13:58:40 +01001176 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1177 unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001178 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001179 io_mapping_unmap_atomic(vaddr);
1180 if (unwritten) {
1181 vaddr = (void __force *)
1182 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1183 unwritten = copy_from_user(vaddr + offset, user_data, length);
1184 io_mapping_unmap(vaddr);
1185 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001186
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001187 return unwritten;
1188}
1189
Eric Anholt3de09aa2009-03-09 09:42:23 -07001190/**
1191 * This is the fast pwrite path, where we copy the data directly from the
1192 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001193 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001194 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001195 */
Eric Anholt673a3942008-07-30 12:06:12 -07001196static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001197i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1198 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001199{
Chris Wilsonfe115622016-10-28 13:58:40 +01001200 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301201 struct i915_ggtt *ggtt = &i915->ggtt;
1202 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001203 struct i915_vma *vma;
1204 u64 remain, offset;
1205 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301206 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301207
Chris Wilsonfe115622016-10-28 13:58:40 +01001208 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1209 if (ret)
1210 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001211
Chris Wilson9c870d02016-10-24 13:42:15 +01001212 intel_runtime_pm_get(i915);
Chris Wilson058d88c2016-08-15 10:49:06 +01001213 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsonde895082016-08-04 16:32:34 +01001214 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001215 if (!IS_ERR(vma)) {
1216 node.start = i915_ggtt_offset(vma);
1217 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001218 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001219 if (ret) {
1220 i915_vma_unpin(vma);
1221 vma = ERR_PTR(ret);
1222 }
1223 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001224 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001225 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301226 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001227 goto out_unlock;
1228 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301229 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001230
1231 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1232 if (ret)
1233 goto out_unpin;
1234
Chris Wilsonfe115622016-10-28 13:58:40 +01001235 mutex_unlock(&i915->drm.struct_mutex);
1236
Chris Wilsonb19482d2016-08-18 17:16:43 +01001237 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001238
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301239 user_data = u64_to_user_ptr(args->data_ptr);
1240 offset = args->offset;
1241 remain = args->size;
1242 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001243 /* Operation in this page
1244 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001245 * page_base = page offset within aperture
1246 * page_offset = offset within page
1247 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001248 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301249 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001250 unsigned int page_offset = offset_in_page(offset);
1251 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301252 page_length = remain < page_length ? remain : page_length;
1253 if (node.allocated) {
1254 wmb(); /* flush the write before we modify the GGTT */
1255 ggtt->base.insert_page(&ggtt->base,
1256 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1257 node.start, I915_CACHE_NONE, 0);
1258 wmb(); /* flush modifications to the GGTT (insert_page) */
1259 } else {
1260 page_base += offset & PAGE_MASK;
1261 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001262 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001263 * source page isn't available. Return the error and we'll
1264 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301265 * If the object is non-shmem backed, we retry again with the
1266 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001267 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001268 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1269 user_data, page_length)) {
1270 ret = -EFAULT;
1271 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001272 }
Eric Anholt673a3942008-07-30 12:06:12 -07001273
Keith Packard0839ccb2008-10-30 19:38:48 -07001274 remain -= page_length;
1275 user_data += page_length;
1276 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001277 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001278 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001279
1280 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001281out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301282 if (node.allocated) {
1283 wmb();
1284 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001285 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301286 remove_mappable_node(&node);
1287 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001288 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301289 }
Chris Wilsonfe115622016-10-28 13:58:40 +01001290out_unlock:
Chris Wilson9c870d02016-10-24 13:42:15 +01001291 intel_runtime_pm_put(i915);
Chris Wilsonfe115622016-10-28 13:58:40 +01001292 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001293 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001294}
1295
Eric Anholt673a3942008-07-30 12:06:12 -07001296static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001297shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001298 char __user *user_data,
1299 bool page_do_bit17_swizzling,
1300 bool needs_clflush_before,
1301 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001302{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001303 char *vaddr;
1304 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001305
Daniel Vetterd174bd62012-03-25 19:47:40 +02001306 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001307 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001308 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001309 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001310 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001311 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1312 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001313 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001314 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001315 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001316 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001317 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001318 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001319
Chris Wilson755d2212012-09-04 21:02:55 +01001320 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001321}
1322
Chris Wilsonfe115622016-10-28 13:58:40 +01001323/* Per-page copy function for the shmem pwrite fastpath.
1324 * Flushes invalid cachelines before writing to the target if
1325 * needs_clflush_before is set and flushes out any written cachelines after
1326 * writing if needs_clflush is set.
1327 */
Eric Anholt40123c12009-03-09 13:42:30 -07001328static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001329shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1330 bool page_do_bit17_swizzling,
1331 bool needs_clflush_before,
1332 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001333{
Chris Wilsonfe115622016-10-28 13:58:40 +01001334 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001335
Chris Wilsonfe115622016-10-28 13:58:40 +01001336 ret = -ENODEV;
1337 if (!page_do_bit17_swizzling) {
1338 char *vaddr = kmap_atomic(page);
1339
1340 if (needs_clflush_before)
1341 drm_clflush_virt_range(vaddr + offset, len);
1342 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1343 if (needs_clflush_after)
1344 drm_clflush_virt_range(vaddr + offset, len);
1345
1346 kunmap_atomic(vaddr);
1347 }
1348 if (ret == 0)
1349 return ret;
1350
1351 return shmem_pwrite_slow(page, offset, len, user_data,
1352 page_do_bit17_swizzling,
1353 needs_clflush_before,
1354 needs_clflush_after);
1355}
1356
1357static int
1358i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1359 const struct drm_i915_gem_pwrite *args)
1360{
1361 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1362 void __user *user_data;
1363 u64 remain;
1364 unsigned int obj_do_bit17_swizzling;
1365 unsigned int partial_cacheline_write;
1366 unsigned int needs_clflush;
1367 unsigned int offset, idx;
1368 int ret;
1369
1370 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001371 if (ret)
1372 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001373
Chris Wilsonfe115622016-10-28 13:58:40 +01001374 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1375 mutex_unlock(&i915->drm.struct_mutex);
1376 if (ret)
1377 return ret;
1378
1379 obj_do_bit17_swizzling = 0;
1380 if (i915_gem_object_needs_bit17_swizzle(obj))
1381 obj_do_bit17_swizzling = BIT(17);
1382
1383 /* If we don't overwrite a cacheline completely we need to be
1384 * careful to have up-to-date data by first clflushing. Don't
1385 * overcomplicate things and flush the entire patch.
1386 */
1387 partial_cacheline_write = 0;
1388 if (needs_clflush & CLFLUSH_BEFORE)
1389 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1390
Chris Wilson43394c72016-08-18 17:16:47 +01001391 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001392 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001393 offset = offset_in_page(args->offset);
1394 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1395 struct page *page = i915_gem_object_get_page(obj, idx);
1396 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001397
Chris Wilsonfe115622016-10-28 13:58:40 +01001398 length = remain;
1399 if (offset + length > PAGE_SIZE)
1400 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001401
Chris Wilsonfe115622016-10-28 13:58:40 +01001402 ret = shmem_pwrite(page, offset, length, user_data,
1403 page_to_phys(page) & obj_do_bit17_swizzling,
1404 (offset | length) & partial_cacheline_write,
1405 needs_clflush & CLFLUSH_AFTER);
1406 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001407 break;
1408
Chris Wilsonfe115622016-10-28 13:58:40 +01001409 remain -= length;
1410 user_data += length;
1411 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001412 }
1413
Chris Wilsond59b21e2017-02-22 11:40:49 +00001414 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001415 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001416 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001417}
1418
1419/**
1420 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001421 * @dev: drm device
1422 * @data: ioctl data blob
1423 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001424 *
1425 * On error, the contents of the buffer that were to be modified are undefined.
1426 */
1427int
1428i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001429 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001430{
1431 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001432 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001433 int ret;
1434
1435 if (args->size == 0)
1436 return 0;
1437
1438 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001439 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001440 args->size))
1441 return -EFAULT;
1442
Chris Wilson03ac0642016-07-20 13:31:51 +01001443 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001444 if (!obj)
1445 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001446
Chris Wilson7dcd2492010-09-26 20:21:44 +01001447 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001448 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001449 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001450 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001451 }
1452
Chris Wilsondb53a302011-02-03 11:57:46 +00001453 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1454
Chris Wilsone95433c2016-10-28 13:58:27 +01001455 ret = i915_gem_object_wait(obj,
1456 I915_WAIT_INTERRUPTIBLE |
1457 I915_WAIT_ALL,
1458 MAX_SCHEDULE_TIMEOUT,
1459 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001460 if (ret)
1461 goto err;
1462
Chris Wilsonfe115622016-10-28 13:58:40 +01001463 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001464 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001465 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001466
Daniel Vetter935aaa62012-03-25 19:47:35 +02001467 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001468 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1469 * it would end up going through the fenced access, and we'll get
1470 * different detiling behavior between reading and writing.
1471 * pread/pwrite currently are reading and writing from the CPU
1472 * perspective, requiring manual detiling by the client.
1473 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001474 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001475 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001476 /* Note that the gtt paths might fail with non-page-backed user
1477 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001478 * textures). Fallback to the shmem path in that case.
1479 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001480 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001481
Chris Wilsond1054ee2016-07-16 18:42:36 +01001482 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001483 if (obj->phys_handle)
1484 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301485 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001486 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001487 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001488
Chris Wilsonfe115622016-10-28 13:58:40 +01001489 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001490err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001491 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001492 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001493}
1494
Chris Wilsond243ad82016-08-18 17:16:44 +01001495static inline enum fb_op_origin
Chris Wilsonaeecc962016-06-17 14:46:39 -03001496write_origin(struct drm_i915_gem_object *obj, unsigned domain)
1497{
Chris Wilson50349242016-08-18 17:17:04 +01001498 return (domain == I915_GEM_DOMAIN_GTT ?
1499 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001500}
1501
Chris Wilson40e62d52016-10-28 13:58:41 +01001502static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1503{
1504 struct drm_i915_private *i915;
1505 struct list_head *list;
1506 struct i915_vma *vma;
1507
1508 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1509 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001510 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001511
1512 if (i915_vma_is_active(vma))
1513 continue;
1514
1515 if (!drm_mm_node_allocated(&vma->node))
1516 continue;
1517
1518 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1519 }
1520
1521 i915 = to_i915(obj->base.dev);
1522 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Joonas Lahtinen56cea322016-11-02 12:16:04 +02001523 list_move_tail(&obj->global_link, list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001524}
1525
Eric Anholt673a3942008-07-30 12:06:12 -07001526/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001527 * Called when user space prepares to use an object with the CPU, either
1528 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001529 * @dev: drm device
1530 * @data: ioctl data blob
1531 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001532 */
1533int
1534i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001535 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001536{
1537 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001538 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001539 uint32_t read_domains = args->read_domains;
1540 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001541 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001542
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001543 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001544 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001545 return -EINVAL;
1546
1547 /* Having something in the write domain implies it's in the read
1548 * domain, and only that read domain. Enforce that in the request.
1549 */
1550 if (write_domain != 0 && read_domains != write_domain)
1551 return -EINVAL;
1552
Chris Wilson03ac0642016-07-20 13:31:51 +01001553 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001554 if (!obj)
1555 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001556
Chris Wilson3236f572012-08-24 09:35:09 +01001557 /* Try to flush the object off the GPU without holding the lock.
1558 * We will repeat the flush holding the lock in the normal manner
1559 * to catch cases where we are gazumped.
1560 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001561 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001562 I915_WAIT_INTERRUPTIBLE |
1563 (write_domain ? I915_WAIT_ALL : 0),
1564 MAX_SCHEDULE_TIMEOUT,
1565 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001566 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001567 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001568
Chris Wilson40e62d52016-10-28 13:58:41 +01001569 /* Flush and acquire obj->pages so that we are coherent through
1570 * direct access in memory with previous cached writes through
1571 * shmemfs and that our cache domain tracking remains valid.
1572 * For example, if the obj->filp was moved to swap without us
1573 * being notified and releasing the pages, we would mistakenly
1574 * continue to assume that the obj remained out of the CPU cached
1575 * domain.
1576 */
1577 err = i915_gem_object_pin_pages(obj);
1578 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001579 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001580
1581 err = i915_mutex_lock_interruptible(dev);
1582 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001583 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001584
Chris Wilson43566de2015-01-02 16:29:29 +05301585 if (read_domains & I915_GEM_DOMAIN_GTT)
Chris Wilson40e62d52016-10-28 13:58:41 +01001586 err = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301587 else
Chris Wilson40e62d52016-10-28 13:58:41 +01001588 err = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1589
1590 /* And bump the LRU for this access */
1591 i915_gem_object_bump_inactive_ggtt(obj);
1592
1593 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001594
Daniel Vetter031b6982015-06-26 19:35:16 +02001595 if (write_domain != 0)
Chris Wilsonaeecc962016-06-17 14:46:39 -03001596 intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001597
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001598out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001599 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001600out:
1601 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001602 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001603}
1604
1605/**
1606 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001607 * @dev: drm device
1608 * @data: ioctl data blob
1609 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001610 */
1611int
1612i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001613 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001614{
1615 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001616 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001617
Chris Wilson03ac0642016-07-20 13:31:51 +01001618 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001619 if (!obj)
1620 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001621
Eric Anholt673a3942008-07-30 12:06:12 -07001622 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001623 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001624 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001625
1626 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001627}
1628
1629/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001630 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1631 * it is mapped to.
1632 * @dev: drm device
1633 * @data: ioctl data blob
1634 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001635 *
1636 * While the mapping holds a reference on the contents of the object, it doesn't
1637 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001638 *
1639 * IMPORTANT:
1640 *
1641 * DRM driver writers who look a this function as an example for how to do GEM
1642 * mmap support, please don't implement mmap support like here. The modern way
1643 * to implement DRM mmap support is with an mmap offset ioctl (like
1644 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1645 * That way debug tooling like valgrind will understand what's going on, hiding
1646 * the mmap call in a driver private ioctl will break that. The i915 driver only
1647 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001648 */
1649int
1650i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001651 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001652{
1653 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001654 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001655 unsigned long addr;
1656
Akash Goel1816f922015-01-02 16:29:30 +05301657 if (args->flags & ~(I915_MMAP_WC))
1658 return -EINVAL;
1659
Borislav Petkov568a58e2016-03-29 17:42:01 +02001660 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301661 return -ENODEV;
1662
Chris Wilson03ac0642016-07-20 13:31:51 +01001663 obj = i915_gem_object_lookup(file, args->handle);
1664 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001665 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001666
Daniel Vetter1286ff72012-05-10 15:25:09 +02001667 /* prime objects have no backing filp to GEM mmap
1668 * pages from.
1669 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001670 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001671 i915_gem_object_put(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001672 return -EINVAL;
1673 }
1674
Chris Wilson03ac0642016-07-20 13:31:51 +01001675 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001676 PROT_READ | PROT_WRITE, MAP_SHARED,
1677 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301678 if (args->flags & I915_MMAP_WC) {
1679 struct mm_struct *mm = current->mm;
1680 struct vm_area_struct *vma;
1681
Michal Hocko80a89a52016-05-23 16:26:11 -07001682 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001683 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001684 return -EINTR;
1685 }
Akash Goel1816f922015-01-02 16:29:30 +05301686 vma = find_vma(mm, addr);
1687 if (vma)
1688 vma->vm_page_prot =
1689 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1690 else
1691 addr = -ENOMEM;
1692 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001693
1694 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001695 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301696 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001697 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001698 if (IS_ERR((void *)addr))
1699 return addr;
1700
1701 args->addr_ptr = (uint64_t) addr;
1702
1703 return 0;
1704}
1705
Chris Wilson03af84f2016-08-18 17:17:01 +01001706static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1707{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001708 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001709}
1710
Jesse Barnesde151cf2008-11-12 10:03:55 -08001711/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001712 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1713 *
1714 * A history of the GTT mmap interface:
1715 *
1716 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1717 * aligned and suitable for fencing, and still fit into the available
1718 * mappable space left by the pinned display objects. A classic problem
1719 * we called the page-fault-of-doom where we would ping-pong between
1720 * two objects that could not fit inside the GTT and so the memcpy
1721 * would page one object in at the expense of the other between every
1722 * single byte.
1723 *
1724 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1725 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1726 * object is too large for the available space (or simply too large
1727 * for the mappable aperture!), a view is created instead and faulted
1728 * into userspace. (This view is aligned and sized appropriately for
1729 * fenced access.)
1730 *
1731 * Restrictions:
1732 *
1733 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1734 * hangs on some architectures, corruption on others. An attempt to service
1735 * a GTT page fault from a snoopable object will generate a SIGBUS.
1736 *
1737 * * the object must be able to fit into RAM (physical memory, though no
1738 * limited to the mappable aperture).
1739 *
1740 *
1741 * Caveats:
1742 *
1743 * * a new GTT page fault will synchronize rendering from the GPU and flush
1744 * all data to system memory. Subsequent access will not be synchronized.
1745 *
1746 * * all mappings are revoked on runtime device suspend.
1747 *
1748 * * there are only 8, 16 or 32 fence registers to share between all users
1749 * (older machines require fence register for display and blitter access
1750 * as well). Contention of the fence registers will cause the previous users
1751 * to be unmapped and any new access will generate new page faults.
1752 *
1753 * * running out of memory while servicing a fault may generate a SIGBUS,
1754 * rather than the expected SIGSEGV.
1755 */
1756int i915_gem_mmap_gtt_version(void)
1757{
1758 return 1;
1759}
1760
Chris Wilson2d4281b2017-01-10 09:56:32 +00001761static inline struct i915_ggtt_view
1762compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001763 pgoff_t page_offset,
1764 unsigned int chunk)
1765{
1766 struct i915_ggtt_view view;
1767
1768 if (i915_gem_object_is_tiled(obj))
1769 chunk = roundup(chunk, tile_row_pages(obj));
1770
Chris Wilson2d4281b2017-01-10 09:56:32 +00001771 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001772 view.partial.offset = rounddown(page_offset, chunk);
1773 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001774 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001775 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001776
1777 /* If the partial covers the entire object, just create a normal VMA. */
1778 if (chunk >= obj->base.size >> PAGE_SHIFT)
1779 view.type = I915_GGTT_VIEW_NORMAL;
1780
1781 return view;
1782}
1783
Chris Wilson4cc69072016-08-25 19:05:19 +01001784/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001785 * i915_gem_fault - fault a page into the GTT
Chris Wilson058d88c2016-08-15 10:49:06 +01001786 * @area: CPU VMA in question
Geliang Tangd9072a32015-09-15 05:58:44 -07001787 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001788 *
1789 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1790 * from userspace. The fault handler takes care of binding the object to
1791 * the GTT (if needed), allocating and programming a fence register (again,
1792 * only if needed based on whether the old reg is still valid or the object
1793 * is tiled) and inserting a new PTE into the faulting process.
1794 *
1795 * Note that the faulting process may involve evicting existing objects
1796 * from the GTT and/or fence registers to make room. So performance may
1797 * suffer if the GTT working set is large or there are few fence registers
1798 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001799 *
1800 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1801 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001802 */
Chris Wilson058d88c2016-08-15 10:49:06 +01001803int i915_gem_fault(struct vm_area_struct *area, struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001804{
Chris Wilson03af84f2016-08-18 17:17:01 +01001805#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Chris Wilson058d88c2016-08-15 10:49:06 +01001806 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001807 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001808 struct drm_i915_private *dev_priv = to_i915(dev);
1809 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001810 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001811 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001812 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001813 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001814 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001815
Jesse Barnesde151cf2008-11-12 10:03:55 -08001816 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001817 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001818
Chris Wilsondb53a302011-02-03 11:57:46 +00001819 trace_i915_gem_object_fault(obj, page_offset, true, write);
1820
Chris Wilson6e4930f2014-02-07 18:37:06 -02001821 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001822 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001823 * repeat the flush holding the lock in the normal manner to catch cases
1824 * where we are gazumped.
1825 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001826 ret = i915_gem_object_wait(obj,
1827 I915_WAIT_INTERRUPTIBLE,
1828 MAX_SCHEDULE_TIMEOUT,
1829 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001830 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001831 goto err;
1832
Chris Wilson40e62d52016-10-28 13:58:41 +01001833 ret = i915_gem_object_pin_pages(obj);
1834 if (ret)
1835 goto err;
1836
Chris Wilsonb8f90962016-08-05 10:14:07 +01001837 intel_runtime_pm_get(dev_priv);
1838
1839 ret = i915_mutex_lock_interruptible(dev);
1840 if (ret)
1841 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001842
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001843 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001844 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001845 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001846 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001847 }
1848
Chris Wilson82118872016-08-18 17:17:05 +01001849 /* If the object is smaller than a couple of partial vma, it is
1850 * not worth only creating a single partial vma - we may as well
1851 * clear enough space for the full object.
1852 */
1853 flags = PIN_MAPPABLE;
1854 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1855 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1856
Chris Wilsona61007a2016-08-18 17:17:02 +01001857 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001858 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001859 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001860 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001861 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001862 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001863
Chris Wilson50349242016-08-18 17:17:04 +01001864 /* Userspace is now writing through an untracked VMA, abandon
1865 * all hope that the hardware is able to track future writes.
1866 */
1867 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1868
Chris Wilsona61007a2016-08-18 17:17:02 +01001869 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1870 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001871 if (IS_ERR(vma)) {
1872 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001873 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001874 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001875
Chris Wilsonc9839302012-11-20 10:45:17 +00001876 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1877 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001878 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001879
Chris Wilson49ef5292016-08-18 17:17:00 +01001880 ret = i915_vma_get_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001881 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001882 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001883
Chris Wilson275f0392016-10-24 13:42:14 +01001884 /* Mark as being mmapped into userspace for later revocation */
Chris Wilson9c870d02016-10-24 13:42:15 +01001885 assert_rpm_wakelock_held(dev_priv);
Chris Wilson275f0392016-10-24 13:42:14 +01001886 if (list_empty(&obj->userfault_link))
1887 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
Chris Wilson275f0392016-10-24 13:42:14 +01001888
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001889 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001890 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001891 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001892 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1893 min_t(u64, vma->size, area->vm_end - area->vm_start),
1894 &ggtt->mappable);
Chris Wilsona61007a2016-08-18 17:17:02 +01001895
Chris Wilsonb8f90962016-08-05 10:14:07 +01001896err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001897 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001898err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001899 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001900err_rpm:
1901 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001902 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001903err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001904 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001905 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001906 /*
1907 * We eat errors when the gpu is terminally wedged to avoid
1908 * userspace unduly crashing (gl has no provisions for mmaps to
1909 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1910 * and so needs to be reported.
1911 */
1912 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001913 ret = VM_FAULT_SIGBUS;
1914 break;
1915 }
Chris Wilson045e7692010-11-07 09:18:22 +00001916 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001917 /*
1918 * EAGAIN means the gpu is hung and we'll wait for the error
1919 * handler to reset everything when re-faulting in
1920 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001921 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001922 case 0:
1923 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001924 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001925 case -EBUSY:
1926 /*
1927 * EBUSY is ok: this just means that another thread
1928 * already did the job.
1929 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001930 ret = VM_FAULT_NOPAGE;
1931 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001932 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001933 ret = VM_FAULT_OOM;
1934 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001935 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001936 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001937 ret = VM_FAULT_SIGBUS;
1938 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001939 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001940 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001941 ret = VM_FAULT_SIGBUS;
1942 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001943 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001944 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001945}
1946
1947/**
Chris Wilson901782b2009-07-10 08:18:50 +01001948 * i915_gem_release_mmap - remove physical page mappings
1949 * @obj: obj in question
1950 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001951 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001952 * relinquish ownership of the pages back to the system.
1953 *
1954 * It is vital that we remove the page mapping if we have mapped a tiled
1955 * object through the GTT and then lose the fence register due to
1956 * resource pressure. Similarly if the object has been moved out of the
1957 * aperture, than pages mapped into userspace must be revoked. Removing the
1958 * mapping will then trigger a page fault on the next user access, allowing
1959 * fixup by i915_gem_fault().
1960 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001961void
Chris Wilson05394f32010-11-08 19:18:58 +00001962i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001963{
Chris Wilson275f0392016-10-24 13:42:14 +01001964 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01001965
Chris Wilson349f2cc2016-04-13 17:35:12 +01001966 /* Serialisation between user GTT access and our code depends upon
1967 * revoking the CPU's PTE whilst the mutex is held. The next user
1968 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01001969 *
1970 * Note that RPM complicates somewhat by adding an additional
1971 * requirement that operations to the GGTT be made holding the RPM
1972 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01001973 */
Chris Wilson275f0392016-10-24 13:42:14 +01001974 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01001975 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001976
Chris Wilson3594a3e2016-10-24 13:42:16 +01001977 if (list_empty(&obj->userfault_link))
Chris Wilson9c870d02016-10-24 13:42:15 +01001978 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01001979
Chris Wilson3594a3e2016-10-24 13:42:16 +01001980 list_del_init(&obj->userfault_link);
David Herrmann6796cb12014-01-03 14:24:19 +01001981 drm_vma_node_unmap(&obj->base.vma_node,
1982 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001983
1984 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1985 * memory transactions from userspace before we return. The TLB
1986 * flushing implied above by changing the PTE above *should* be
1987 * sufficient, an extra barrier here just provides us with a bit
1988 * of paranoid documentation about our requirement to serialise
1989 * memory writes before touching registers / GSM.
1990 */
1991 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01001992
1993out:
1994 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01001995}
1996
Chris Wilson7c108fd2016-10-24 13:42:18 +01001997void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001998{
Chris Wilson3594a3e2016-10-24 13:42:16 +01001999 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002000 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002001
Chris Wilson3594a3e2016-10-24 13:42:16 +01002002 /*
2003 * Only called during RPM suspend. All users of the userfault_list
2004 * must be holding an RPM wakeref to ensure that this can not
2005 * run concurrently with themselves (and use the struct_mutex for
2006 * protection between themselves).
2007 */
2008
2009 list_for_each_entry_safe(obj, on,
2010 &dev_priv->mm.userfault_list, userfault_link) {
Chris Wilson275f0392016-10-24 13:42:14 +01002011 list_del_init(&obj->userfault_link);
Chris Wilson275f0392016-10-24 13:42:14 +01002012 drm_vma_node_unmap(&obj->base.vma_node,
2013 obj->base.dev->anon_inode->i_mapping);
Chris Wilson275f0392016-10-24 13:42:14 +01002014 }
Chris Wilson7c108fd2016-10-24 13:42:18 +01002015
2016 /* The fence will be lost when the device powers down. If any were
2017 * in use by hardware (i.e. they are pinned), we should not be powering
2018 * down! All other fences will be reacquired by the user upon waking.
2019 */
2020 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2021 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2022
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002023 /* Ideally we want to assert that the fence register is not
2024 * live at this point (i.e. that no piece of code will be
2025 * trying to write through fence + GTT, as that both violates
2026 * our tracking of activity and associated locking/barriers,
2027 * but also is illegal given that the hw is powered down).
2028 *
2029 * Previously we used reg->pin_count as a "liveness" indicator.
2030 * That is not sufficient, and we need a more fine-grained
2031 * tool if we want to have a sanity check here.
2032 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002033
2034 if (!reg->vma)
2035 continue;
2036
2037 GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
2038 reg->dirty = true;
2039 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002040}
2041
Chris Wilsond8cb5082012-08-11 15:41:03 +01002042static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2043{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002044 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002045 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002046
Chris Wilsonf3f61842016-08-05 10:14:14 +01002047 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002048 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002049 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002050
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002051 /* Attempt to reap some mmap space from dead objects */
2052 do {
2053 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2054 if (err)
2055 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002056
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002057 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002058 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002059 if (!err)
2060 break;
2061
2062 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002063
Chris Wilsonf3f61842016-08-05 10:14:14 +01002064 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002065}
2066
2067static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2068{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002069 drm_gem_free_mmap_offset(&obj->base);
2070}
2071
Dave Airlieda6b51d2014-12-24 13:11:17 +10002072int
Dave Airlieff72145b2011-02-07 12:16:14 +10002073i915_gem_mmap_gtt(struct drm_file *file,
2074 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002075 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002076 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002077{
Chris Wilson05394f32010-11-08 19:18:58 +00002078 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002079 int ret;
2080
Chris Wilson03ac0642016-07-20 13:31:51 +01002081 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002082 if (!obj)
2083 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002084
Chris Wilsond8cb5082012-08-11 15:41:03 +01002085 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002086 if (ret == 0)
2087 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002088
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002089 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002090 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002091}
2092
Dave Airlieff72145b2011-02-07 12:16:14 +10002093/**
2094 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2095 * @dev: DRM device
2096 * @data: GTT mapping ioctl data
2097 * @file: GEM object info
2098 *
2099 * Simply returns the fake offset to userspace so it can mmap it.
2100 * The mmap call will end up in drm_gem_mmap(), which will set things
2101 * up so we can get faults in the handler above.
2102 *
2103 * The fault handler will take care of binding the object into the GTT
2104 * (since it may have been evicted to make room for something), allocating
2105 * a fence register, and mapping the appropriate aperture address into
2106 * userspace.
2107 */
2108int
2109i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2110 struct drm_file *file)
2111{
2112 struct drm_i915_gem_mmap_gtt *args = data;
2113
Dave Airlieda6b51d2014-12-24 13:11:17 +10002114 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002115}
2116
Daniel Vetter225067e2012-08-20 10:23:20 +02002117/* Immediately discard the backing storage */
2118static void
2119i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002120{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002121 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002122
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002123 if (obj->base.filp == NULL)
2124 return;
2125
Daniel Vetter225067e2012-08-20 10:23:20 +02002126 /* Our goal here is to return as much of the memory as
2127 * is possible back to the system as we are called from OOM.
2128 * To do this we must instruct the shmfs to drop all of its
2129 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002130 */
Chris Wilson55372522014-03-25 13:23:06 +00002131 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002132 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01002133}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002134
Chris Wilson55372522014-03-25 13:23:06 +00002135/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002136void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002137{
Chris Wilson55372522014-03-25 13:23:06 +00002138 struct address_space *mapping;
2139
Chris Wilson1233e2d2016-10-28 13:58:37 +01002140 lockdep_assert_held(&obj->mm.lock);
2141 GEM_BUG_ON(obj->mm.pages);
2142
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002143 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002144 case I915_MADV_DONTNEED:
2145 i915_gem_object_truncate(obj);
2146 case __I915_MADV_PURGED:
2147 return;
2148 }
2149
2150 if (obj->base.filp == NULL)
2151 return;
2152
Al Viro93c76a32015-12-04 23:45:44 -05002153 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002154 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002155}
2156
Chris Wilson5cdf5882010-09-27 15:51:07 +01002157static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002158i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2159 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002160{
Dave Gordon85d12252016-05-20 11:54:06 +01002161 struct sgt_iter sgt_iter;
2162 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002163
Chris Wilsone5facdf2016-12-23 14:57:57 +00002164 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002165
Chris Wilson03ac84f2016-10-28 13:58:36 +01002166 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002167
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002168 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002169 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002170
Chris Wilson03ac84f2016-10-28 13:58:36 +01002171 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002172 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002173 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002174
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002175 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002176 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002177
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002178 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002179 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002180 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002181
Chris Wilson03ac84f2016-10-28 13:58:36 +01002182 sg_free_table(pages);
2183 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002184}
2185
Chris Wilson96d77632016-10-28 13:58:33 +01002186static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2187{
2188 struct radix_tree_iter iter;
2189 void **slot;
2190
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002191 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2192 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilson96d77632016-10-28 13:58:33 +01002193}
2194
Chris Wilson548625e2016-11-01 12:11:34 +00002195void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2196 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002197{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002198 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002199
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002200 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002201 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002202
Chris Wilson15717de2016-08-04 07:52:26 +01002203 GEM_BUG_ON(obj->bind_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002204 if (!READ_ONCE(obj->mm.pages))
2205 return;
2206
2207 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002208 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002209 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2210 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002211
Chris Wilsona2165e32012-12-03 11:49:00 +00002212 /* ->put_pages might need to allocate memory for the bit17 swizzle
2213 * array, hence protect them from being reaped by removing them from gtt
2214 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002215 pages = fetch_and_zero(&obj->mm.pages);
2216 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002217
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002218 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002219 void *ptr;
2220
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002221 ptr = ptr_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002222 if (is_vmalloc_addr(ptr))
2223 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002224 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002225 kunmap(kmap_to_page(ptr));
2226
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002227 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002228 }
2229
Chris Wilson96d77632016-10-28 13:58:33 +01002230 __i915_gem_object_reset_page_iter(obj);
2231
Chris Wilson03ac84f2016-10-28 13:58:36 +01002232 obj->ops->put_pages(obj, pages);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002233unlock:
2234 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002235}
2236
Chris Wilson935a2f72017-02-13 17:15:13 +00002237static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002238{
2239 struct sg_table new_st;
2240 struct scatterlist *sg, *new_sg;
2241 unsigned int i;
2242
2243 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002244 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002245
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002246 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002247 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002248
2249 new_sg = new_st.sgl;
2250 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2251 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2252 /* called before being DMA mapped, no need to copy sg->dma_* */
2253 new_sg = sg_next(new_sg);
2254 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002255 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002256
2257 sg_free_table(orig_st);
2258
2259 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002260 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002261}
2262
Chris Wilson03ac84f2016-10-28 13:58:36 +01002263static struct sg_table *
Chris Wilson6c085a72012-08-20 11:40:46 +02002264i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002265{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002266 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002267 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2268 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002269 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002270 struct sg_table *st;
2271 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002272 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002273 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002274 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson4ff340f02016-10-18 13:02:50 +01002275 unsigned int max_segment;
Imre Deake2273302015-07-09 12:59:05 +03002276 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002277 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002278
Chris Wilson6c085a72012-08-20 11:40:46 +02002279 /* Assert that the object is not currently in any GPU domain. As it
2280 * wasn't in the GTT, there shouldn't be any way it could have been in
2281 * a GPU cache
2282 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002283 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2284 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002285
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -05002286 max_segment = swiotlb_max_segment();
Chris Wilson871dfbd2016-10-11 09:20:21 +01002287 if (!max_segment)
Chris Wilson4ff340f02016-10-18 13:02:50 +01002288 max_segment = rounddown(UINT_MAX, PAGE_SIZE);
Chris Wilson871dfbd2016-10-11 09:20:21 +01002289
Chris Wilson9da3da62012-06-01 15:20:22 +01002290 st = kmalloc(sizeof(*st), GFP_KERNEL);
2291 if (st == NULL)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002292 return ERR_PTR(-ENOMEM);
Eric Anholt673a3942008-07-30 12:06:12 -07002293
Chris Wilsond766ef52016-12-19 12:43:45 +00002294rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002295 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002296 kfree(st);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002297 return ERR_PTR(-ENOMEM);
Chris Wilson9da3da62012-06-01 15:20:22 +01002298 }
2299
2300 /* Get the list of pages out of our struct file. They'll be pinned
2301 * at this point until we release them.
2302 *
2303 * Fail silently without starting the shrinker
2304 */
Al Viro93c76a32015-12-04 23:45:44 -05002305 mapping = obj->base.filp->f_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002306 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002307 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002308 sg = st->sgl;
2309 st->nents = 0;
2310 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002311 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2312 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002313 i915_gem_shrink(dev_priv,
2314 page_count,
2315 I915_SHRINK_BOUND |
2316 I915_SHRINK_UNBOUND |
2317 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002318 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2319 }
2320 if (IS_ERR(page)) {
2321 /* We've tried hard to allocate the memory by reaping
2322 * our own buffer, now let the real VM do its job and
2323 * go down in flames if truly OOM.
2324 */
David Herrmannf461d1b2014-05-25 14:34:10 +02002325 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002326 if (IS_ERR(page)) {
2327 ret = PTR_ERR(page);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002328 goto err_sg;
Imre Deake2273302015-07-09 12:59:05 +03002329 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002330 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002331 if (!i ||
2332 sg->length >= max_segment ||
2333 page_to_pfn(page) != last_pfn + 1) {
Imre Deak90797e62013-02-18 19:28:03 +02002334 if (i)
2335 sg = sg_next(sg);
2336 st->nents++;
2337 sg_set_page(sg, page, PAGE_SIZE, 0);
2338 } else {
2339 sg->length += PAGE_SIZE;
2340 }
2341 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002342
2343 /* Check that the i965g/gm workaround works. */
2344 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002345 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002346 if (sg) /* loop terminated early; short sg table */
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002347 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002348
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002349 /* Trim unused sg entries to avoid wasting memory. */
2350 i915_sg_trim(st);
2351
Chris Wilson03ac84f2016-10-28 13:58:36 +01002352 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002353 if (ret) {
2354 /* DMA remapping failed? One possible cause is that
2355 * it could not reserve enough large entries, asking
2356 * for PAGE_SIZE chunks instead may be helpful.
2357 */
2358 if (max_segment > PAGE_SIZE) {
2359 for_each_sgt_page(page, sgt_iter, st)
2360 put_page(page);
2361 sg_free_table(st);
2362
2363 max_segment = PAGE_SIZE;
2364 goto rebuild_st;
2365 } else {
2366 dev_warn(&dev_priv->drm.pdev->dev,
2367 "Failed to DMA remap %lu pages\n",
2368 page_count);
2369 goto err_pages;
2370 }
2371 }
Imre Deake2273302015-07-09 12:59:05 +03002372
Eric Anholt673a3942008-07-30 12:06:12 -07002373 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002374 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002375
Chris Wilson03ac84f2016-10-28 13:58:36 +01002376 return st;
Eric Anholt673a3942008-07-30 12:06:12 -07002377
Chris Wilsonb17993b2016-11-14 11:29:30 +00002378err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002379 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002380err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002381 for_each_sgt_page(page, sgt_iter, st)
2382 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002383 sg_free_table(st);
2384 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002385
2386 /* shmemfs first checks if there is enough memory to allocate the page
2387 * and reports ENOSPC should there be insufficient, along with the usual
2388 * ENOMEM for a genuine allocation failure.
2389 *
2390 * We use ENOSPC in our driver to mean that we have run out of aperture
2391 * space and so want to translate the error from shmemfs back to our
2392 * usual understanding of ENOMEM.
2393 */
Imre Deake2273302015-07-09 12:59:05 +03002394 if (ret == -ENOSPC)
2395 ret = -ENOMEM;
2396
Chris Wilson03ac84f2016-10-28 13:58:36 +01002397 return ERR_PTR(ret);
2398}
2399
2400void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
2401 struct sg_table *pages)
2402{
Chris Wilson1233e2d2016-10-28 13:58:37 +01002403 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002404
2405 obj->mm.get_page.sg_pos = pages->sgl;
2406 obj->mm.get_page.sg_idx = 0;
2407
2408 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002409
2410 if (i915_gem_object_is_tiled(obj) &&
2411 to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
2412 GEM_BUG_ON(obj->mm.quirked);
2413 __i915_gem_object_pin_pages(obj);
2414 obj->mm.quirked = true;
2415 }
Chris Wilson03ac84f2016-10-28 13:58:36 +01002416}
2417
2418static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2419{
2420 struct sg_table *pages;
2421
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002422 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2423
Chris Wilson03ac84f2016-10-28 13:58:36 +01002424 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2425 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2426 return -EFAULT;
2427 }
2428
2429 pages = obj->ops->get_pages(obj);
2430 if (unlikely(IS_ERR(pages)))
2431 return PTR_ERR(pages);
2432
2433 __i915_gem_object_set_pages(obj, pages);
2434 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002435}
2436
Chris Wilson37e680a2012-06-07 15:38:42 +01002437/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002438 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002439 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002440 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002441 * either as a result of memory pressure (reaping pages under the shrinker)
2442 * or as the object is itself released.
2443 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002444int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002445{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002446 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002447
Chris Wilson1233e2d2016-10-28 13:58:37 +01002448 err = mutex_lock_interruptible(&obj->mm.lock);
2449 if (err)
2450 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002451
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002452 if (unlikely(!obj->mm.pages)) {
2453 err = ____i915_gem_object_get_pages(obj);
2454 if (err)
2455 goto unlock;
2456
2457 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002458 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002459 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002460
Chris Wilson1233e2d2016-10-28 13:58:37 +01002461unlock:
2462 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002463 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002464}
2465
Dave Gordondd6034c2016-05-20 11:54:04 +01002466/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002467static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2468 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002469{
2470 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002471 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002472 struct sgt_iter sgt_iter;
2473 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002474 struct page *stack_pages[32];
2475 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002476 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002477 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002478 void *addr;
2479
2480 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002481 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002482 return kmap(sg_page(sgt->sgl));
2483
Dave Gordonb338fa42016-05-20 11:54:05 +01002484 if (n_pages > ARRAY_SIZE(stack_pages)) {
2485 /* Too big for stack -- allocate temporary array instead */
2486 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2487 if (!pages)
2488 return NULL;
2489 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002490
Dave Gordon85d12252016-05-20 11:54:06 +01002491 for_each_sgt_page(page, sgt_iter, sgt)
2492 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002493
2494 /* Check that we have the expected number of pages */
2495 GEM_BUG_ON(i != n_pages);
2496
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002497 switch (type) {
2498 case I915_MAP_WB:
2499 pgprot = PAGE_KERNEL;
2500 break;
2501 case I915_MAP_WC:
2502 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2503 break;
2504 }
2505 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002506
Dave Gordonb338fa42016-05-20 11:54:05 +01002507 if (pages != stack_pages)
2508 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002509
2510 return addr;
2511}
2512
2513/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002514void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2515 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002516{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002517 enum i915_map_type has_type;
2518 bool pinned;
2519 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002520 int ret;
2521
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002522 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002523
Chris Wilson1233e2d2016-10-28 13:58:37 +01002524 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002525 if (ret)
2526 return ERR_PTR(ret);
2527
Chris Wilson1233e2d2016-10-28 13:58:37 +01002528 pinned = true;
2529 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002530 if (unlikely(!obj->mm.pages)) {
2531 ret = ____i915_gem_object_get_pages(obj);
2532 if (ret)
2533 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002534
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002535 smp_mb__before_atomic();
2536 }
2537 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002538 pinned = false;
2539 }
2540 GEM_BUG_ON(!obj->mm.pages);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002541
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002542 ptr = ptr_unpack_bits(obj->mm.mapping, has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002543 if (ptr && has_type != type) {
2544 if (pinned) {
2545 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002546 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002547 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002548
2549 if (is_vmalloc_addr(ptr))
2550 vunmap(ptr);
2551 else
2552 kunmap(kmap_to_page(ptr));
2553
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002554 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002555 }
2556
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002557 if (!ptr) {
2558 ptr = i915_gem_object_map(obj, type);
2559 if (!ptr) {
2560 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002561 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002562 }
2563
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002564 obj->mm.mapping = ptr_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002565 }
2566
Chris Wilson1233e2d2016-10-28 13:58:37 +01002567out_unlock:
2568 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002569 return ptr;
2570
Chris Wilson1233e2d2016-10-28 13:58:37 +01002571err_unpin:
2572 atomic_dec(&obj->mm.pages_pin_count);
2573err_unlock:
2574 ptr = ERR_PTR(ret);
2575 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002576}
2577
Chris Wilson60958682016-12-31 11:20:11 +00002578static bool ban_context(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002579{
Chris Wilson60958682016-12-31 11:20:11 +00002580 return (i915_gem_context_is_bannable(ctx) &&
2581 ctx->ban_score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002582}
2583
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002584static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002585{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002586 ctx->guilty_count++;
Chris Wilson60958682016-12-31 11:20:11 +00002587 ctx->ban_score += CONTEXT_SCORE_GUILTY;
2588 if (ban_context(ctx))
2589 i915_gem_context_set_banned(ctx);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002590
2591 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002592 ctx->name, ctx->ban_score,
Chris Wilson60958682016-12-31 11:20:11 +00002593 yesno(i915_gem_context_is_banned(ctx)));
Mika Kuoppalab083a082016-11-18 15:10:47 +02002594
Chris Wilson60958682016-12-31 11:20:11 +00002595 if (!i915_gem_context_is_banned(ctx) || IS_ERR_OR_NULL(ctx->file_priv))
Mika Kuoppalab083a082016-11-18 15:10:47 +02002596 return;
2597
Chris Wilsond9e9da62016-11-22 14:41:18 +00002598 ctx->file_priv->context_bans++;
2599 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2600 ctx->name, ctx->file_priv->context_bans);
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002601}
2602
2603static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2604{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002605 ctx->active_count++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002606}
2607
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002608struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002609i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002610{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002611 struct drm_i915_gem_request *request, *active = NULL;
2612 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002613
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002614 /* We are called by the error capture and reset at a random
2615 * point in time. In particular, note that neither is crucially
2616 * ordered with an interrupt. After a hang, the GPU is dead and we
2617 * assume that no more writes can happen (we waited long enough for
2618 * all writes that were in transaction to be flushed) - adding an
2619 * extra delay for a recent interrupt is pointless. Hence, we do
2620 * not need an engine->irq_seqno_barrier() before the seqno reads.
2621 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002622 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002623 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002624 if (__i915_gem_request_completed(request,
2625 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002626 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002627
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002628 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002629 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2630 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002631
Chris Wilson754c9fd2017-02-23 07:44:14 +00002632 active = request;
2633 break;
2634 }
2635 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2636
2637 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002638}
2639
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002640static bool engine_stalled(struct intel_engine_cs *engine)
2641{
2642 if (!engine->hangcheck.stalled)
2643 return false;
2644
2645 /* Check for possible seqno movement after hang declaration */
2646 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2647 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2648 return false;
2649 }
2650
2651 return true;
2652}
2653
Chris Wilson0e178ae2017-01-17 17:59:06 +02002654int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002655{
2656 struct intel_engine_cs *engine;
2657 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002658 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002659
2660 /* Ensure irq handler finishes, and not run again. */
Chris Wilson0e178ae2017-01-17 17:59:06 +02002661 for_each_engine(engine, dev_priv, id) {
2662 struct drm_i915_gem_request *request;
2663
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002664 /* Prevent the signaler thread from updating the request
2665 * state (by calling dma_fence_signal) as we are processing
2666 * the reset. The write from the GPU of the seqno is
2667 * asynchronous and the signaler thread may see a different
2668 * value to us and declare the request complete, even though
2669 * the reset routine have picked that request as the active
2670 * (incomplete) request. This conflict is not handled
2671 * gracefully!
2672 */
2673 kthread_park(engine->breadcrumbs.signaler);
2674
Chris Wilson1f7b8472017-02-08 14:30:33 +00002675 /* Prevent request submission to the hardware until we have
2676 * completed the reset in i915_gem_reset_finish(). If a request
2677 * is completed by one engine, it may then queue a request
2678 * to a second via its engine->irq_tasklet *just* as we are
2679 * calling engine->init_hw() and also writing the ELSP.
2680 * Turning off the engine->irq_tasklet until the reset is over
2681 * prevents the race.
2682 */
Chris Wilson4c965542017-01-17 17:59:01 +02002683 tasklet_kill(&engine->irq_tasklet);
Chris Wilson1d309632017-02-12 17:20:00 +00002684 tasklet_disable(&engine->irq_tasklet);
Chris Wilson4c965542017-01-17 17:59:01 +02002685
Chris Wilson8c12d122017-02-10 18:52:14 +00002686 if (engine->irq_seqno_barrier)
2687 engine->irq_seqno_barrier(engine);
2688
Chris Wilson0e178ae2017-01-17 17:59:06 +02002689 if (engine_stalled(engine)) {
2690 request = i915_gem_find_active_request(engine);
2691 if (request && request->fence.error == -EIO)
2692 err = -EIO; /* Previous reset failed! */
2693 }
2694 }
2695
Chris Wilson4c965542017-01-17 17:59:01 +02002696 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002697
2698 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002699}
2700
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002701static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002702{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002703 void *vaddr = request->ring->vaddr;
2704 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002705
Chris Wilson821ed7d2016-09-09 14:11:53 +01002706 /* As this request likely depends on state from the lost
2707 * context, clear out all the user operations leaving the
2708 * breadcrumb at the end (so we get the fence notifications).
2709 */
2710 head = request->head;
2711 if (request->postfix < head) {
2712 memset(vaddr + head, 0, request->ring->size - head);
2713 head = 0;
2714 }
2715 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00002716
2717 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00002718}
2719
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002720static void engine_skip_context(struct drm_i915_gem_request *request)
2721{
2722 struct intel_engine_cs *engine = request->engine;
2723 struct i915_gem_context *hung_ctx = request->ctx;
2724 struct intel_timeline *timeline;
2725 unsigned long flags;
2726
2727 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
2728
2729 spin_lock_irqsave(&engine->timeline->lock, flags);
2730 spin_lock(&timeline->lock);
2731
2732 list_for_each_entry_continue(request, &engine->timeline->requests, link)
2733 if (request->ctx == hung_ctx)
2734 skip_request(request);
2735
2736 list_for_each_entry(request, &timeline->requests, link)
2737 skip_request(request);
2738
2739 spin_unlock(&timeline->lock);
2740 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2741}
2742
Mika Kuoppala61da5362017-01-17 17:59:05 +02002743/* Returns true if the request was guilty of hang */
2744static bool i915_gem_reset_request(struct drm_i915_gem_request *request)
2745{
2746 /* Read once and return the resolution */
2747 const bool guilty = engine_stalled(request->engine);
2748
Mika Kuoppala71895a02017-01-17 17:59:07 +02002749 /* The guilty request will get skipped on a hung engine.
2750 *
2751 * Users of client default contexts do not rely on logical
2752 * state preserved between batches so it is safe to execute
2753 * queued requests following the hang. Non default contexts
2754 * rely on preserved state, so skipping a batch loses the
2755 * evolution of the state and it needs to be considered corrupted.
2756 * Executing more queued batches on top of corrupted state is
2757 * risky. But we take the risk by trying to advance through
2758 * the queued requests in order to make the client behaviour
2759 * more predictable around resets, by not throwing away random
2760 * amount of batches it has prepared for execution. Sophisticated
2761 * clients can use gem_reset_stats_ioctl and dma fence status
2762 * (exported via sync_file info ioctl on explicit fences) to observe
2763 * when it loses the context state and should rebuild accordingly.
2764 *
2765 * The context ban, and ultimately the client ban, mechanism are safety
2766 * valves if client submission ends up resulting in nothing more than
2767 * subsequent hangs.
2768 */
2769
Mika Kuoppala61da5362017-01-17 17:59:05 +02002770 if (guilty) {
2771 i915_gem_context_mark_guilty(request->ctx);
2772 skip_request(request);
2773 } else {
2774 i915_gem_context_mark_innocent(request->ctx);
2775 dma_fence_set_error(&request->fence, -EAGAIN);
2776 }
2777
2778 return guilty;
2779}
2780
Chris Wilson821ed7d2016-09-09 14:11:53 +01002781static void i915_gem_reset_engine(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002782{
Chris Wilsondcff85c2016-08-05 10:14:11 +01002783 struct drm_i915_gem_request *request;
Chris Wilson608c1a52015-09-03 13:01:40 +01002784
Chris Wilson821ed7d2016-09-09 14:11:53 +01002785 request = i915_gem_find_active_request(engine);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002786 if (request && i915_gem_reset_request(request)) {
2787 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
2788 engine->name, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002789
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002790 /* If this context is now banned, skip all pending requests. */
2791 if (i915_gem_context_is_banned(request->ctx))
2792 engine_skip_context(request);
2793 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002794
2795 /* Setup the CS to resume from the breadcrumb of the hung request */
2796 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002797}
2798
Chris Wilsond8027092017-02-08 14:30:32 +00002799void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002800{
2801 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302802 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002803
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002804 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2805
Chris Wilson821ed7d2016-09-09 14:11:53 +01002806 i915_gem_retire_requests(dev_priv);
2807
Chris Wilson2ae55732017-02-12 17:20:02 +00002808 for_each_engine(engine, dev_priv, id) {
2809 struct i915_gem_context *ctx;
2810
Chris Wilson821ed7d2016-09-09 14:11:53 +01002811 i915_gem_reset_engine(engine);
Chris Wilson2ae55732017-02-12 17:20:02 +00002812 ctx = fetch_and_zero(&engine->last_retired_context);
2813 if (ctx)
2814 engine->context_unpin(engine, ctx);
2815 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002816
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00002817 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01002818
2819 if (dev_priv->gt.awake) {
2820 intel_sanitize_gt_powersave(dev_priv);
2821 intel_enable_gt_powersave(dev_priv);
2822 if (INTEL_GEN(dev_priv) >= 6)
2823 gen6_rps_busy(dev_priv);
2824 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002825}
2826
Chris Wilsond8027092017-02-08 14:30:32 +00002827void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
2828{
Chris Wilson1f7b8472017-02-08 14:30:33 +00002829 struct intel_engine_cs *engine;
2830 enum intel_engine_id id;
2831
Chris Wilsond8027092017-02-08 14:30:32 +00002832 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00002833
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002834 for_each_engine(engine, dev_priv, id) {
Chris Wilson1f7b8472017-02-08 14:30:33 +00002835 tasklet_enable(&engine->irq_tasklet);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002836 kthread_unpark(engine->breadcrumbs.signaler);
2837 }
Chris Wilsond8027092017-02-08 14:30:32 +00002838}
2839
Chris Wilson821ed7d2016-09-09 14:11:53 +01002840static void nop_submit_request(struct drm_i915_gem_request *request)
2841{
Chris Wilson3cd94422017-01-10 17:22:45 +00002842 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00002843 i915_gem_request_submit(request);
2844 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002845}
2846
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002847static void engine_set_wedged(struct intel_engine_cs *engine)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002848{
Chris Wilson3cd94422017-01-10 17:22:45 +00002849 struct drm_i915_gem_request *request;
2850 unsigned long flags;
2851
Chris Wilson20e49332016-11-22 14:41:21 +00002852 /* We need to be sure that no thread is running the old callback as
2853 * we install the nop handler (otherwise we would submit a request
2854 * to hardware that will never complete). In order to prevent this
2855 * race, we wait until the machine is idle before making the swap
2856 * (using stop_machine()).
2857 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01002858 engine->submit_request = nop_submit_request;
Chris Wilson70c2a242016-09-09 14:11:46 +01002859
Chris Wilson3cd94422017-01-10 17:22:45 +00002860 /* Mark all executing requests as skipped */
2861 spin_lock_irqsave(&engine->timeline->lock, flags);
2862 list_for_each_entry(request, &engine->timeline->requests, link)
2863 dma_fence_set_error(&request->fence, -EIO);
2864 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2865
Chris Wilsonc4b09302016-07-20 09:21:10 +01002866 /* Mark all pending requests as complete so that any concurrent
2867 * (lockless) lookup doesn't try and wait upon the request as we
2868 * reset it.
2869 */
Chris Wilson73cb9702016-10-28 13:58:46 +01002870 intel_engine_init_global_seqno(engine,
Chris Wilsoncb399ea2016-11-01 10:03:16 +00002871 intel_engine_last_submit(engine));
Chris Wilsonc4b09302016-07-20 09:21:10 +01002872
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002873 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002874 * Clear the execlists queue up before freeing the requests, as those
2875 * are the ones that keep the context and ringbuffer backing objects
2876 * pinned in place.
2877 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002878
Tomas Elf7de1691a2015-10-19 16:32:32 +01002879 if (i915.enable_execlists) {
Chris Wilson663f71e2016-11-14 20:41:00 +00002880 unsigned long flags;
2881
2882 spin_lock_irqsave(&engine->timeline->lock, flags);
2883
Chris Wilson70c2a242016-09-09 14:11:46 +01002884 i915_gem_request_put(engine->execlist_port[0].request);
2885 i915_gem_request_put(engine->execlist_port[1].request);
2886 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
Chris Wilson20311bd2016-11-14 20:41:03 +00002887 engine->execlist_queue = RB_ROOT;
2888 engine->execlist_first = NULL;
Chris Wilson663f71e2016-11-14 20:41:00 +00002889
2890 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002891 }
Eric Anholt673a3942008-07-30 12:06:12 -07002892}
2893
Chris Wilson20e49332016-11-22 14:41:21 +00002894static int __i915_gem_set_wedged_BKL(void *data)
Eric Anholt673a3942008-07-30 12:06:12 -07002895{
Chris Wilson20e49332016-11-22 14:41:21 +00002896 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002897 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302898 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07002899
Chris Wilson20e49332016-11-22 14:41:21 +00002900 for_each_engine(engine, i915, id)
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002901 engine_set_wedged(engine);
Chris Wilson20e49332016-11-22 14:41:21 +00002902
2903 return 0;
2904}
2905
2906void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
2907{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002908 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2909 set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
Chris Wilson4db080f2013-12-04 11:37:09 +00002910
Chris Wilson20e49332016-11-22 14:41:21 +00002911 stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
Chris Wilsondfaae392010-09-22 10:31:52 +01002912
Chris Wilson20e49332016-11-22 14:41:21 +00002913 i915_gem_context_lost(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002914 i915_gem_retire_requests(dev_priv);
Chris Wilson20e49332016-11-22 14:41:21 +00002915
2916 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002917}
2918
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002919static void
Eric Anholt673a3942008-07-30 12:06:12 -07002920i915_gem_retire_work_handler(struct work_struct *work)
2921{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002922 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002923 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002924 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002925
Chris Wilson891b48c2010-09-29 12:26:37 +01002926 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002927 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002928 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002929 mutex_unlock(&dev->struct_mutex);
2930 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002931
2932 /* Keep the retire handler running until we are finally idle.
2933 * We do not need to do this test under locking as in the worst-case
2934 * we queue the retire worker once too often.
2935 */
Chris Wilsonc9615612016-07-09 10:12:06 +01002936 if (READ_ONCE(dev_priv->gt.awake)) {
2937 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01002938 queue_delayed_work(dev_priv->wq,
2939 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002940 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01002941 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002942}
Chris Wilson891b48c2010-09-29 12:26:37 +01002943
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002944static void
2945i915_gem_idle_work_handler(struct work_struct *work)
2946{
2947 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002948 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002949 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002950 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302951 enum intel_engine_id id;
Chris Wilson67d97da2016-07-04 08:08:31 +01002952 bool rearm_hangcheck;
2953
2954 if (!READ_ONCE(dev_priv->gt.awake))
2955 return;
2956
Imre Deak0cb56702016-11-07 11:20:04 +02002957 /*
2958 * Wait for last execlists context complete, but bail out in case a
2959 * new request is submitted.
2960 */
2961 wait_for(READ_ONCE(dev_priv->gt.active_requests) ||
2962 intel_execlists_idle(dev_priv), 10);
2963
Chris Wilson28176ef2016-10-28 13:58:56 +01002964 if (READ_ONCE(dev_priv->gt.active_requests))
Chris Wilson67d97da2016-07-04 08:08:31 +01002965 return;
2966
2967 rearm_hangcheck =
2968 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
2969
2970 if (!mutex_trylock(&dev->struct_mutex)) {
2971 /* Currently busy, come back later */
2972 mod_delayed_work(dev_priv->wq,
2973 &dev_priv->gt.idle_work,
2974 msecs_to_jiffies(50));
2975 goto out_rearm;
2976 }
2977
Imre Deak93c97dc2016-11-07 11:20:03 +02002978 /*
2979 * New request retired after this work handler started, extend active
2980 * period until next instance of the work.
2981 */
2982 if (work_pending(work))
2983 goto out_unlock;
2984
Chris Wilson28176ef2016-10-28 13:58:56 +01002985 if (dev_priv->gt.active_requests)
Chris Wilson67d97da2016-07-04 08:08:31 +01002986 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002987
Imre Deak0cb56702016-11-07 11:20:04 +02002988 if (wait_for(intel_execlists_idle(dev_priv), 10))
2989 DRM_ERROR("Timeout waiting for engines to idle\n");
2990
Chris Wilson67b807a82017-02-27 20:58:50 +00002991 for_each_engine(engine, dev_priv, id) {
2992 intel_engine_disarm_breadcrumbs(engine);
Chris Wilson67d97da2016-07-04 08:08:31 +01002993 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson67b807a82017-02-27 20:58:50 +00002994 }
Zou Nan hai852835f2010-05-21 09:08:56 +08002995
Chris Wilson67d97da2016-07-04 08:08:31 +01002996 GEM_BUG_ON(!dev_priv->gt.awake);
2997 dev_priv->gt.awake = false;
2998 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01002999
Chris Wilson67d97da2016-07-04 08:08:31 +01003000 if (INTEL_GEN(dev_priv) >= 6)
3001 gen6_rps_idle(dev_priv);
3002 intel_runtime_pm_put(dev_priv);
3003out_unlock:
3004 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003005
Chris Wilson67d97da2016-07-04 08:08:31 +01003006out_rearm:
3007 if (rearm_hangcheck) {
3008 GEM_BUG_ON(!dev_priv->gt.awake);
3009 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003010 }
Eric Anholt673a3942008-07-30 12:06:12 -07003011}
3012
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003013void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3014{
3015 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3016 struct drm_i915_file_private *fpriv = file->driver_priv;
3017 struct i915_vma *vma, *vn;
3018
3019 mutex_lock(&obj->base.dev->struct_mutex);
3020 list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
3021 if (vma->vm->file == fpriv)
3022 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003023
3024 if (i915_gem_object_is_active(obj) &&
3025 !i915_gem_object_has_active_reference(obj)) {
3026 i915_gem_object_set_active_reference(obj);
3027 i915_gem_object_get(obj);
3028 }
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003029 mutex_unlock(&obj->base.dev->struct_mutex);
3030}
3031
Chris Wilsone95433c2016-10-28 13:58:27 +01003032static unsigned long to_wait_timeout(s64 timeout_ns)
3033{
3034 if (timeout_ns < 0)
3035 return MAX_SCHEDULE_TIMEOUT;
3036
3037 if (timeout_ns == 0)
3038 return 0;
3039
3040 return nsecs_to_jiffies_timeout(timeout_ns);
3041}
3042
Ben Widawsky5816d642012-04-11 11:18:19 -07003043/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003044 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003045 * @dev: drm device pointer
3046 * @data: ioctl data blob
3047 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003048 *
3049 * Returns 0 if successful, else an error is returned with the remaining time in
3050 * the timeout parameter.
3051 * -ETIME: object is still busy after timeout
3052 * -ERESTARTSYS: signal interrupted the wait
3053 * -ENONENT: object doesn't exist
3054 * Also possible, but rare:
3055 * -EAGAIN: GPU wedged
3056 * -ENOMEM: damn
3057 * -ENODEV: Internal IRQ fail
3058 * -E?: The add request failed
3059 *
3060 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3061 * non-zero timeout parameter the wait ioctl will wait for the given number of
3062 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3063 * without holding struct_mutex the object may become re-busied before this
3064 * function completes. A similar but shorter * race condition exists in the busy
3065 * ioctl
3066 */
3067int
3068i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3069{
3070 struct drm_i915_gem_wait *args = data;
3071 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003072 ktime_t start;
3073 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003074
Daniel Vetter11b5d512014-09-29 15:31:26 +02003075 if (args->flags != 0)
3076 return -EINVAL;
3077
Chris Wilson03ac0642016-07-20 13:31:51 +01003078 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003079 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003080 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003081
Chris Wilsone95433c2016-10-28 13:58:27 +01003082 start = ktime_get();
3083
3084 ret = i915_gem_object_wait(obj,
3085 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3086 to_wait_timeout(args->timeout_ns),
3087 to_rps_client(file));
3088
3089 if (args->timeout_ns > 0) {
3090 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3091 if (args->timeout_ns < 0)
3092 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003093
3094 /*
3095 * Apparently ktime isn't accurate enough and occasionally has a
3096 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3097 * things up to make the test happy. We allow up to 1 jiffy.
3098 *
3099 * This is a regression from the timespec->ktime conversion.
3100 */
3101 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3102 args->timeout_ns = 0;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003103 }
3104
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003105 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003106 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003107}
3108
Chris Wilson73cb9702016-10-28 13:58:46 +01003109static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003110{
Chris Wilson73cb9702016-10-28 13:58:46 +01003111 int ret, i;
3112
3113 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3114 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3115 if (ret)
3116 return ret;
3117 }
3118
3119 return 0;
3120}
3121
3122int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3123{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003124 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003125
Chris Wilson9caa34a2016-11-11 14:58:08 +00003126 if (flags & I915_WAIT_LOCKED) {
3127 struct i915_gem_timeline *tl;
3128
3129 lockdep_assert_held(&i915->drm.struct_mutex);
3130
3131 list_for_each_entry(tl, &i915->gt.timelines, link) {
3132 ret = wait_for_timeline(tl, flags);
3133 if (ret)
3134 return ret;
3135 }
3136 } else {
3137 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003138 if (ret)
3139 return ret;
3140 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003141
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003142 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003143}
3144
Eric Anholte47c68e2008-11-14 13:35:19 -08003145/** Flushes the GTT write domain for the object if it's dirty. */
3146static void
Chris Wilson05394f32010-11-08 19:18:58 +00003147i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003148{
Chris Wilson3b5724d2016-08-18 17:16:49 +01003149 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003150
Chris Wilson05394f32010-11-08 19:18:58 +00003151 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003152 return;
3153
Chris Wilson63256ec2011-01-04 18:42:07 +00003154 /* No actual flushing is required for the GTT write domain. Writes
Chris Wilson3b5724d2016-08-18 17:16:49 +01003155 * to it "immediately" go to main memory as far as we know, so there's
Eric Anholte47c68e2008-11-14 13:35:19 -08003156 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003157 *
3158 * However, we do have to enforce the order so that all writes through
3159 * the GTT land before any writes to the device, such as updates to
3160 * the GATT itself.
Chris Wilson3b5724d2016-08-18 17:16:49 +01003161 *
3162 * We also have to wait a bit for the writes to land from the GTT.
3163 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
3164 * timing. This issue has only been observed when switching quickly
3165 * between GTT writes and CPU reads from inside the kernel on recent hw,
3166 * and it appears to only affect discrete GTT blocks (i.e. on LLC
3167 * system agents we cannot reproduce this behaviour).
Eric Anholte47c68e2008-11-14 13:35:19 -08003168 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003169 wmb();
Chris Wilson3b5724d2016-08-18 17:16:49 +01003170 if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
Akash Goel3b3f1652016-10-13 22:44:48 +05303171 POSTING_READ(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
Chris Wilson63256ec2011-01-04 18:42:07 +00003172
Chris Wilsond59b21e2017-02-22 11:40:49 +00003173 intel_fb_obj_flush(obj, write_origin(obj, I915_GEM_DOMAIN_GTT));
Daniel Vetterf99d7062014-06-19 16:01:59 +02003174
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003175 obj->base.write_domain = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08003176}
3177
3178/** Flushes the CPU write domain for the object if it's dirty. */
3179static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003180i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003181{
Chris Wilson05394f32010-11-08 19:18:58 +00003182 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003183 return;
3184
Chris Wilson57822dc2017-02-22 11:40:48 +00003185 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003186 obj->base.write_domain = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08003187}
3188
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003189static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3190{
3191 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU && !obj->cache_dirty)
3192 return;
3193
Chris Wilson57822dc2017-02-22 11:40:48 +00003194 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003195 obj->base.write_domain = 0;
3196}
3197
3198void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3199{
3200 if (!READ_ONCE(obj->pin_display))
3201 return;
3202
3203 mutex_lock(&obj->base.dev->struct_mutex);
3204 __i915_gem_object_flush_for_display(obj);
3205 mutex_unlock(&obj->base.dev->struct_mutex);
3206}
3207
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003208/**
3209 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003210 * @obj: object to act on
3211 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003212 *
3213 * This function returns when the move is complete, including waiting on
3214 * flushes to occur.
3215 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003216int
Chris Wilson20217462010-11-23 15:26:33 +00003217i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003218{
Eric Anholte47c68e2008-11-14 13:35:19 -08003219 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003220
Chris Wilsone95433c2016-10-28 13:58:27 +01003221 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003222
Chris Wilsone95433c2016-10-28 13:58:27 +01003223 ret = i915_gem_object_wait(obj,
3224 I915_WAIT_INTERRUPTIBLE |
3225 I915_WAIT_LOCKED |
3226 (write ? I915_WAIT_ALL : 0),
3227 MAX_SCHEDULE_TIMEOUT,
3228 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003229 if (ret)
3230 return ret;
3231
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003232 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3233 return 0;
3234
Chris Wilson43566de2015-01-02 16:29:29 +05303235 /* Flush and acquire obj->pages so that we are coherent through
3236 * direct access in memory with previous cached writes through
3237 * shmemfs and that our cache domain tracking remains valid.
3238 * For example, if the obj->filp was moved to swap without us
3239 * being notified and releasing the pages, we would mistakenly
3240 * continue to assume that the obj remained out of the CPU cached
3241 * domain.
3242 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003243 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303244 if (ret)
3245 return ret;
3246
Daniel Vettere62b59e2015-01-21 14:53:48 +01003247 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003248
Chris Wilsond0a57782012-10-09 19:24:37 +01003249 /* Serialise direct access to this object with the barriers for
3250 * coherent writes from the GPU, by effectively invalidating the
3251 * GTT domain upon first access.
3252 */
3253 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3254 mb();
3255
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003256 /* It should now be out of any other write domains, and we can update
3257 * the domain values for our changes.
3258 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003259 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003260 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003261 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003262 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3263 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003264 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003265 }
3266
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003267 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003268 return 0;
3269}
3270
Chris Wilsonef55f922015-10-09 14:11:27 +01003271/**
3272 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003273 * @obj: object to act on
3274 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003275 *
3276 * After this function returns, the object will be in the new cache-level
3277 * across all GTT and the contents of the backing storage will be coherent,
3278 * with respect to the new cache-level. In order to keep the backing storage
3279 * coherent for all users, we only allow a single cache level to be set
3280 * globally on the object and prevent it from being changed whilst the
3281 * hardware is reading from the object. That is if the object is currently
3282 * on the scanout it will be set to uncached (or equivalent display
3283 * cache coherency) and all non-MOCS GPU access will also be uncached so
3284 * that all direct access to the scanout remains coherent.
3285 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003286int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3287 enum i915_cache_level cache_level)
3288{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003289 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003290 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003291
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003292 lockdep_assert_held(&obj->base.dev->struct_mutex);
3293
Chris Wilsone4ffd172011-04-04 09:44:39 +01003294 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003295 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003296
Chris Wilsonef55f922015-10-09 14:11:27 +01003297 /* Inspect the list of currently bound VMA and unbind any that would
3298 * be invalid given the new cache-level. This is principally to
3299 * catch the issue of the CS prefetch crossing page boundaries and
3300 * reading an invalid PTE on older architectures.
3301 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003302restart:
3303 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003304 if (!drm_mm_node_allocated(&vma->node))
3305 continue;
3306
Chris Wilson20dfbde2016-08-04 16:32:30 +01003307 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003308 DRM_DEBUG("can not change the cache level of pinned objects\n");
3309 return -EBUSY;
3310 }
3311
Chris Wilsonaa653a62016-08-04 07:52:27 +01003312 if (i915_gem_valid_gtt_space(vma, cache_level))
3313 continue;
3314
3315 ret = i915_vma_unbind(vma);
3316 if (ret)
3317 return ret;
3318
3319 /* As unbinding may affect other elements in the
3320 * obj->vma_list (due to side-effects from retiring
3321 * an active vma), play safe and restart the iterator.
3322 */
3323 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003324 }
3325
Chris Wilsonef55f922015-10-09 14:11:27 +01003326 /* We can reuse the existing drm_mm nodes but need to change the
3327 * cache-level on the PTE. We could simply unbind them all and
3328 * rebind with the correct cache-level on next use. However since
3329 * we already have a valid slot, dma mapping, pages etc, we may as
3330 * rewrite the PTE in the belief that doing so tramples upon less
3331 * state and so involves less work.
3332 */
Chris Wilson15717de2016-08-04 07:52:26 +01003333 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003334 /* Before we change the PTE, the GPU must not be accessing it.
3335 * If we wait upon the object, we know that all the bound
3336 * VMA are no longer active.
3337 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003338 ret = i915_gem_object_wait(obj,
3339 I915_WAIT_INTERRUPTIBLE |
3340 I915_WAIT_LOCKED |
3341 I915_WAIT_ALL,
3342 MAX_SCHEDULE_TIMEOUT,
3343 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003344 if (ret)
3345 return ret;
3346
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003347 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3348 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003349 /* Access to snoopable pages through the GTT is
3350 * incoherent and on some machines causes a hard
3351 * lockup. Relinquish the CPU mmaping to force
3352 * userspace to refault in the pages and we can
3353 * then double check if the GTT mapping is still
3354 * valid for that pointer access.
3355 */
3356 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003357
Chris Wilsonef55f922015-10-09 14:11:27 +01003358 /* As we no longer need a fence for GTT access,
3359 * we can relinquish it now (and so prevent having
3360 * to steal a fence from someone else on the next
3361 * fence request). Note GPU activity would have
3362 * dropped the fence as all snoopable access is
3363 * supposed to be linear.
3364 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003365 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3366 ret = i915_vma_put_fence(vma);
3367 if (ret)
3368 return ret;
3369 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003370 } else {
3371 /* We either have incoherent backing store and
3372 * so no GTT access or the architecture is fully
3373 * coherent. In such cases, existing GTT mmaps
3374 * ignore the cache bit in the PTE and we can
3375 * rewrite it without confusing the GPU or having
3376 * to force userspace to fault back in its mmaps.
3377 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003378 }
3379
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003380 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003381 if (!drm_mm_node_allocated(&vma->node))
3382 continue;
3383
3384 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3385 if (ret)
3386 return ret;
3387 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003388 }
3389
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003390 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU &&
Chris Wilsone59dc172017-02-22 11:40:45 +00003391 i915_gem_object_is_coherent(obj))
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003392 obj->cache_dirty = true;
3393
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003394 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003395 vma->node.color = cache_level;
3396 obj->cache_level = cache_level;
3397
Chris Wilsone4ffd172011-04-04 09:44:39 +01003398 return 0;
3399}
3400
Ben Widawsky199adf42012-09-21 17:01:20 -07003401int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3402 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003403{
Ben Widawsky199adf42012-09-21 17:01:20 -07003404 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003405 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003406 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003407
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003408 rcu_read_lock();
3409 obj = i915_gem_object_lookup_rcu(file, args->handle);
3410 if (!obj) {
3411 err = -ENOENT;
3412 goto out;
3413 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003414
Chris Wilson651d7942013-08-08 14:41:10 +01003415 switch (obj->cache_level) {
3416 case I915_CACHE_LLC:
3417 case I915_CACHE_L3_LLC:
3418 args->caching = I915_CACHING_CACHED;
3419 break;
3420
Chris Wilson4257d3b2013-08-08 14:41:11 +01003421 case I915_CACHE_WT:
3422 args->caching = I915_CACHING_DISPLAY;
3423 break;
3424
Chris Wilson651d7942013-08-08 14:41:10 +01003425 default:
3426 args->caching = I915_CACHING_NONE;
3427 break;
3428 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003429out:
3430 rcu_read_unlock();
3431 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003432}
3433
Ben Widawsky199adf42012-09-21 17:01:20 -07003434int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3435 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003436{
Chris Wilson9c870d02016-10-24 13:42:15 +01003437 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003438 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003439 struct drm_i915_gem_object *obj;
3440 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003441 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003442
Ben Widawsky199adf42012-09-21 17:01:20 -07003443 switch (args->caching) {
3444 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003445 level = I915_CACHE_NONE;
3446 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003447 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003448 /*
3449 * Due to a HW issue on BXT A stepping, GPU stores via a
3450 * snooped mapping may leave stale data in a corresponding CPU
3451 * cacheline, whereas normally such cachelines would get
3452 * invalidated.
3453 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003454 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003455 return -ENODEV;
3456
Chris Wilsone6994ae2012-07-10 10:27:08 +01003457 level = I915_CACHE_LLC;
3458 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003459 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003460 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003461 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003462 default:
3463 return -EINVAL;
3464 }
3465
Chris Wilsond65415d2017-01-19 08:22:10 +00003466 obj = i915_gem_object_lookup(file, args->handle);
3467 if (!obj)
3468 return -ENOENT;
3469
3470 if (obj->cache_level == level)
3471 goto out;
3472
3473 ret = i915_gem_object_wait(obj,
3474 I915_WAIT_INTERRUPTIBLE,
3475 MAX_SCHEDULE_TIMEOUT,
3476 to_rps_client(file));
3477 if (ret)
3478 goto out;
3479
Ben Widawsky3bc29132012-09-26 16:15:20 -07003480 ret = i915_mutex_lock_interruptible(dev);
3481 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003482 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003483
3484 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003485 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003486
3487out:
3488 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003489 return ret;
3490}
3491
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003492/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003493 * Prepare buffer for display plane (scanout, cursors, etc).
3494 * Can be called from an uninterruptible phase (modesetting) and allows
3495 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003496 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003497struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003498i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3499 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003500 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003501{
Chris Wilson058d88c2016-08-15 10:49:06 +01003502 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003503 int ret;
3504
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003505 lockdep_assert_held(&obj->base.dev->struct_mutex);
3506
Chris Wilsoncc98b412013-08-09 12:25:09 +01003507 /* Mark the pin_display early so that we account for the
3508 * display coherency whilst setting up the cache domains.
3509 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003510 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003511
Eric Anholta7ef0642011-03-29 16:59:54 -07003512 /* The display engine is not coherent with the LLC cache on gen6. As
3513 * a result, we make sure that the pinning that is about to occur is
3514 * done with uncached PTEs. This is lowest common denominator for all
3515 * chipsets.
3516 *
3517 * However for gen6+, we could do better by using the GFDT bit instead
3518 * of uncaching, which would allow us to flush all the LLC-cached data
3519 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3520 */
Chris Wilson651d7942013-08-08 14:41:10 +01003521 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003522 HAS_WT(to_i915(obj->base.dev)) ?
3523 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003524 if (ret) {
3525 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003526 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003527 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003528
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003529 /* As the user may map the buffer once pinned in the display plane
3530 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003531 * always use map_and_fenceable for all scanout buffers. However,
3532 * it may simply be too big to fit into mappable, in which case
3533 * put it anyway and hope that userspace can cope (but always first
3534 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003535 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003536 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003537 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003538 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3539 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003540 if (IS_ERR(vma)) {
3541 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3542 unsigned int flags;
3543
3544 /* Valleyview is definitely limited to scanning out the first
3545 * 512MiB. Lets presume this behaviour was inherited from the
3546 * g4x display engine and that all earlier gen are similarly
3547 * limited. Testing suggests that it is a little more
3548 * complicated than this. For example, Cherryview appears quite
3549 * happy to scanout from anywhere within its global aperture.
3550 */
3551 flags = 0;
3552 if (HAS_GMCH_DISPLAY(i915))
3553 flags = PIN_MAPPABLE;
3554 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
3555 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003556 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003557 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003558
Chris Wilsond8923dc2016-08-18 17:17:07 +01003559 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3560
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003561 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003562 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00003563 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003564
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003565 /* It should now be out of any other write domains, and we can update
3566 * the domain values for our changes.
3567 */
Chris Wilson05394f32010-11-08 19:18:58 +00003568 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003569
Chris Wilson058d88c2016-08-15 10:49:06 +01003570 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003571
3572err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003573 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003574 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003575}
3576
3577void
Chris Wilson058d88c2016-08-15 10:49:06 +01003578i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003579{
Chris Wilson49d73912016-11-29 09:50:08 +00003580 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003581
Chris Wilson058d88c2016-08-15 10:49:06 +01003582 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003583 return;
3584
Chris Wilsond8923dc2016-08-18 17:17:07 +01003585 if (--vma->obj->pin_display == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003586 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003587
Chris Wilson383d5822016-08-18 17:17:08 +01003588 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003589 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003590
Chris Wilson058d88c2016-08-15 10:49:06 +01003591 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003592}
3593
Eric Anholte47c68e2008-11-14 13:35:19 -08003594/**
3595 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003596 * @obj: object to act on
3597 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003598 *
3599 * This function returns when the move is complete, including waiting on
3600 * flushes to occur.
3601 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003602int
Chris Wilson919926a2010-11-12 13:42:53 +00003603i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003604{
Eric Anholte47c68e2008-11-14 13:35:19 -08003605 int ret;
3606
Chris Wilsone95433c2016-10-28 13:58:27 +01003607 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003608
Chris Wilsone95433c2016-10-28 13:58:27 +01003609 ret = i915_gem_object_wait(obj,
3610 I915_WAIT_INTERRUPTIBLE |
3611 I915_WAIT_LOCKED |
3612 (write ? I915_WAIT_ALL : 0),
3613 MAX_SCHEDULE_TIMEOUT,
3614 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003615 if (ret)
3616 return ret;
3617
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003618 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3619 return 0;
3620
Eric Anholte47c68e2008-11-14 13:35:19 -08003621 i915_gem_object_flush_gtt_write_domain(obj);
3622
Eric Anholte47c68e2008-11-14 13:35:19 -08003623 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003624 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003625 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00003626 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003627 }
3628
3629 /* It should now be out of any other write domains, and we can update
3630 * the domain values for our changes.
3631 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003632 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003633
3634 /* If we're writing through the CPU, then the GPU read domains will
3635 * need to be invalidated at next use.
3636 */
3637 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003638 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3639 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003640 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003641
3642 return 0;
3643}
3644
Eric Anholt673a3942008-07-30 12:06:12 -07003645/* Throttle our rendering by waiting until the ring has completed our requests
3646 * emitted over 20 msec ago.
3647 *
Eric Anholtb9624422009-06-03 07:27:35 +00003648 * Note that if we were to use the current jiffies each time around the loop,
3649 * we wouldn't escape the function with any frames outstanding if the time to
3650 * render a frame was over 20ms.
3651 *
Eric Anholt673a3942008-07-30 12:06:12 -07003652 * This should get us reasonable parallelism between CPU and GPU but also
3653 * relatively low latency when blocking on a particular request to finish.
3654 */
3655static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003656i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003657{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003658 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003659 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003660 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003661 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003662 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003663
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003664 /* ABI: return -EIO if already wedged */
3665 if (i915_terminally_wedged(&dev_priv->gpu_error))
3666 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003667
Chris Wilson1c255952010-09-26 11:03:27 +01003668 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003669 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003670 if (time_after_eq(request->emitted_jiffies, recent_enough))
3671 break;
3672
John Harrisonfcfa423c2015-05-29 17:44:12 +01003673 /*
3674 * Note that the request might not have been submitted yet.
3675 * In which case emitted_jiffies will be zero.
3676 */
3677 if (!request->emitted_jiffies)
3678 continue;
3679
John Harrison54fb2412014-11-24 18:49:27 +00003680 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003681 }
John Harrisonff865882014-11-24 18:49:28 +00003682 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003683 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003684 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003685
John Harrison54fb2412014-11-24 18:49:27 +00003686 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003687 return 0;
3688
Chris Wilsone95433c2016-10-28 13:58:27 +01003689 ret = i915_wait_request(target,
3690 I915_WAIT_INTERRUPTIBLE,
3691 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003692 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003693
Chris Wilsone95433c2016-10-28 13:58:27 +01003694 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003695}
3696
Chris Wilson058d88c2016-08-15 10:49:06 +01003697struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003698i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3699 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003700 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003701 u64 alignment,
3702 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003703{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003704 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
3705 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01003706 struct i915_vma *vma;
3707 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003708
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003709 lockdep_assert_held(&obj->base.dev->struct_mutex);
3710
Chris Wilson718659a2017-01-16 15:21:28 +00003711 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00003712 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003713 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003714
3715 if (i915_vma_misplaced(vma, size, alignment, flags)) {
3716 if (flags & PIN_NONBLOCK &&
3717 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003718 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003719
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003720 if (flags & PIN_MAPPABLE) {
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003721 /* If the required space is larger than the available
3722 * aperture, we will not able to find a slot for the
3723 * object and unbinding the object now will be in
3724 * vain. Worse, doing so may cause us to ping-pong
3725 * the object in and out of the Global GTT and
3726 * waste a lot of cycles under the mutex.
3727 */
Chris Wilson944397f2017-01-09 16:16:11 +00003728 if (vma->fence_size > dev_priv->ggtt.mappable_end)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003729 return ERR_PTR(-E2BIG);
3730
3731 /* If NONBLOCK is set the caller is optimistically
3732 * trying to cache the full object within the mappable
3733 * aperture, and *must* have a fallback in place for
3734 * situations where we cannot bind the object. We
3735 * can be a little more lax here and use the fallback
3736 * more often to avoid costly migrations of ourselves
3737 * and other objects within the aperture.
3738 *
3739 * Half-the-aperture is used as a simple heuristic.
3740 * More interesting would to do search for a free
3741 * block prior to making the commitment to unbind.
3742 * That caters for the self-harm case, and with a
3743 * little more heuristics (e.g. NOFAULT, NOEVICT)
3744 * we could try to minimise harm to others.
3745 */
3746 if (flags & PIN_NONBLOCK &&
Chris Wilson944397f2017-01-09 16:16:11 +00003747 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003748 return ERR_PTR(-ENOSPC);
3749 }
3750
Chris Wilson59bfa122016-08-04 16:32:31 +01003751 WARN(i915_vma_is_pinned(vma),
3752 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003753 " offset=%08x, req.alignment=%llx,"
3754 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3755 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003756 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003757 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003758 ret = i915_vma_unbind(vma);
3759 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003760 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003761 }
3762
Chris Wilson058d88c2016-08-15 10:49:06 +01003763 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3764 if (ret)
3765 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003766
Chris Wilson058d88c2016-08-15 10:49:06 +01003767 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003768}
3769
Chris Wilsonedf6b762016-08-09 09:23:33 +01003770static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003771{
3772 /* Note that we could alias engines in the execbuf API, but
3773 * that would be very unwise as it prevents userspace from
3774 * fine control over engine selection. Ahem.
3775 *
3776 * This should be something like EXEC_MAX_ENGINE instead of
3777 * I915_NUM_ENGINES.
3778 */
3779 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
3780 return 0x10000 << id;
3781}
3782
3783static __always_inline unsigned int __busy_write_id(unsigned int id)
3784{
Chris Wilson70cb4722016-08-09 18:08:25 +01003785 /* The uABI guarantees an active writer is also amongst the read
3786 * engines. This would be true if we accessed the activity tracking
3787 * under the lock, but as we perform the lookup of the object and
3788 * its activity locklessly we can not guarantee that the last_write
3789 * being active implies that we have set the same engine flag from
3790 * last_read - hence we always set both read and write busy for
3791 * last_write.
3792 */
3793 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003794}
3795
Chris Wilsonedf6b762016-08-09 09:23:33 +01003796static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003797__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003798 unsigned int (*flag)(unsigned int id))
3799{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003800 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003801
Chris Wilsond07f0e52016-10-28 13:58:44 +01003802 /* We have to check the current hw status of the fence as the uABI
3803 * guarantees forward progress. We could rely on the idle worker
3804 * to eventually flush us, but to minimise latency just ask the
3805 * hardware.
3806 *
3807 * Note we only report on the status of native fences.
3808 */
3809 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003810 return 0;
3811
Chris Wilsond07f0e52016-10-28 13:58:44 +01003812 /* opencode to_request() in order to avoid const warnings */
3813 rq = container_of(fence, struct drm_i915_gem_request, fence);
3814 if (i915_gem_request_completed(rq))
3815 return 0;
3816
3817 return flag(rq->engine->exec_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003818}
3819
Chris Wilsonedf6b762016-08-09 09:23:33 +01003820static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003821busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003822{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003823 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003824}
3825
Chris Wilsonedf6b762016-08-09 09:23:33 +01003826static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003827busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003828{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003829 if (!fence)
3830 return 0;
3831
3832 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003833}
3834
Eric Anholt673a3942008-07-30 12:06:12 -07003835int
Eric Anholt673a3942008-07-30 12:06:12 -07003836i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003837 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003838{
3839 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003840 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003841 struct reservation_object_list *list;
3842 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003843 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003844
Chris Wilsond07f0e52016-10-28 13:58:44 +01003845 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003846 rcu_read_lock();
3847 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003848 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003849 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003850
3851 /* A discrepancy here is that we do not report the status of
3852 * non-i915 fences, i.e. even though we may report the object as idle,
3853 * a call to set-domain may still stall waiting for foreign rendering.
3854 * This also means that wait-ioctl may report an object as busy,
3855 * where busy-ioctl considers it idle.
3856 *
3857 * We trade the ability to warn of foreign fences to report on which
3858 * i915 engines are active for the object.
3859 *
3860 * Alternatively, we can trade that extra information on read/write
3861 * activity with
3862 * args->busy =
3863 * !reservation_object_test_signaled_rcu(obj->resv, true);
3864 * to report the overall busyness. This is what the wait-ioctl does.
3865 *
3866 */
3867retry:
3868 seq = raw_read_seqcount(&obj->resv->seq);
3869
3870 /* Translate the exclusive fence to the READ *and* WRITE engine */
3871 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
3872
3873 /* Translate shared fences to READ set of engines */
3874 list = rcu_dereference(obj->resv->fence);
3875 if (list) {
3876 unsigned int shared_count = list->shared_count, i;
3877
3878 for (i = 0; i < shared_count; ++i) {
3879 struct dma_fence *fence =
3880 rcu_dereference(list->shared[i]);
3881
3882 args->busy |= busy_check_reader(fence);
3883 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003884 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003885
Chris Wilsond07f0e52016-10-28 13:58:44 +01003886 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
3887 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00003888
Chris Wilsond07f0e52016-10-28 13:58:44 +01003889 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003890out:
3891 rcu_read_unlock();
3892 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07003893}
3894
3895int
3896i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3897 struct drm_file *file_priv)
3898{
Akshay Joshi0206e352011-08-16 15:34:10 -04003899 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003900}
3901
Chris Wilson3ef94da2009-09-14 16:50:29 +01003902int
3903i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3904 struct drm_file *file_priv)
3905{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003906 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01003907 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003908 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003909 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003910
3911 switch (args->madv) {
3912 case I915_MADV_DONTNEED:
3913 case I915_MADV_WILLNEED:
3914 break;
3915 default:
3916 return -EINVAL;
3917 }
3918
Chris Wilson03ac0642016-07-20 13:31:51 +01003919 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003920 if (!obj)
3921 return -ENOENT;
3922
3923 err = mutex_lock_interruptible(&obj->mm.lock);
3924 if (err)
3925 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003926
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003927 if (obj->mm.pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01003928 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01003929 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003930 if (obj->mm.madv == I915_MADV_WILLNEED) {
3931 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003932 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003933 obj->mm.quirked = false;
3934 }
3935 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003936 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003937 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003938 obj->mm.quirked = true;
3939 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01003940 }
3941
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003942 if (obj->mm.madv != __I915_MADV_PURGED)
3943 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003944
Chris Wilson6c085a72012-08-20 11:40:46 +02003945 /* if the object is no longer attached, discard its backing storage */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003946 if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003947 i915_gem_object_truncate(obj);
3948
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003949 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003950 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003951
Chris Wilson1233e2d2016-10-28 13:58:37 +01003952out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003953 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003954 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003955}
3956
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003957static void
3958frontbuffer_retire(struct i915_gem_active *active,
3959 struct drm_i915_gem_request *request)
3960{
3961 struct drm_i915_gem_object *obj =
3962 container_of(active, typeof(*obj), frontbuffer_write);
3963
Chris Wilsond59b21e2017-02-22 11:40:49 +00003964 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003965}
3966
Chris Wilson37e680a2012-06-07 15:38:42 +01003967void i915_gem_object_init(struct drm_i915_gem_object *obj,
3968 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01003969{
Chris Wilson1233e2d2016-10-28 13:58:37 +01003970 mutex_init(&obj->mm.lock);
3971
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003972 INIT_LIST_HEAD(&obj->global_link);
Chris Wilson275f0392016-10-24 13:42:14 +01003973 INIT_LIST_HEAD(&obj->userfault_link);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02003974 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07003975 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01003976 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01003977
Chris Wilson37e680a2012-06-07 15:38:42 +01003978 obj->ops = ops;
3979
Chris Wilsond07f0e52016-10-28 13:58:44 +01003980 reservation_object_init(&obj->__builtin_resv);
3981 obj->resv = &obj->__builtin_resv;
3982
Chris Wilson50349242016-08-18 17:17:04 +01003983 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003984 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003985
3986 obj->mm.madv = I915_MADV_WILLNEED;
3987 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
3988 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01003989
Dave Gordonf19ec8c2016-07-04 11:34:37 +01003990 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01003991}
3992
Chris Wilson37e680a2012-06-07 15:38:42 +01003993static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00003994 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
3995 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson37e680a2012-06-07 15:38:42 +01003996 .get_pages = i915_gem_object_get_pages_gtt,
3997 .put_pages = i915_gem_object_put_pages_gtt,
3998};
3999
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004000struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004001i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004002{
Daniel Vetterc397b902010-04-09 19:05:07 +00004003 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004004 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004005 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004006 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004007
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004008 /* There is a prevalence of the assumption that we fit the object's
4009 * page count inside a 32bit _signed_ variable. Let's document this and
4010 * catch if we ever need to fix it. In the meantime, if you do spot
4011 * such a local variable, please consider fixing!
4012 */
4013 if (WARN_ON(size >> PAGE_SHIFT > INT_MAX))
4014 return ERR_PTR(-E2BIG);
4015
4016 if (overflows_type(size, obj->base.size))
4017 return ERR_PTR(-E2BIG);
4018
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004019 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004020 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004021 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004022
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004023 ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004024 if (ret)
4025 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004026
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004027 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004028 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004029 /* 965gm cannot relocate objects above 4GiB. */
4030 mask &= ~__GFP_HIGHMEM;
4031 mask |= __GFP_DMA32;
4032 }
4033
Al Viro93c76a32015-12-04 23:45:44 -05004034 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004035 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004036
Chris Wilson37e680a2012-06-07 15:38:42 +01004037 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004038
Daniel Vetterc397b902010-04-09 19:05:07 +00004039 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4040 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4041
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004042 if (HAS_LLC(dev_priv)) {
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004043 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004044 * cache) for about a 10% performance improvement
4045 * compared to uncached. Graphics requests other than
4046 * display scanout are coherent with the CPU in
4047 * accessing this cache. This means in this mode we
4048 * don't need to clflush on the CPU side, and on the
4049 * GPU side we only need to flush internal caches to
4050 * get data visible to the CPU.
4051 *
4052 * However, we maintain the display planes as UC, and so
4053 * need to rebind when first used as such.
4054 */
4055 obj->cache_level = I915_CACHE_LLC;
4056 } else
4057 obj->cache_level = I915_CACHE_NONE;
4058
Daniel Vetterd861e332013-07-24 23:25:03 +02004059 trace_i915_gem_object_create(obj);
4060
Chris Wilson05394f32010-11-08 19:18:58 +00004061 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004062
4063fail:
4064 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004065 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004066}
4067
Chris Wilson340fbd82014-05-22 09:16:52 +01004068static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4069{
4070 /* If we are the last user of the backing storage (be it shmemfs
4071 * pages or stolen etc), we know that the pages are going to be
4072 * immediately released. In this case, we can then skip copying
4073 * back the contents from the GPU.
4074 */
4075
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004076 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004077 return false;
4078
4079 if (obj->base.filp == NULL)
4080 return true;
4081
4082 /* At first glance, this looks racy, but then again so would be
4083 * userspace racing mmap against close. However, the first external
4084 * reference to the filp can only be obtained through the
4085 * i915_gem_mmap_ioctl() which safeguards us against the user
4086 * acquiring such a reference whilst we are in the middle of
4087 * freeing the object.
4088 */
4089 return atomic_long_read(&obj->base.filp->f_count) == 1;
4090}
4091
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004092static void __i915_gem_free_objects(struct drm_i915_private *i915,
4093 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004094{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004095 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004096
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004097 mutex_lock(&i915->drm.struct_mutex);
4098 intel_runtime_pm_get(i915);
4099 llist_for_each_entry(obj, freed, freed) {
4100 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004101
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004102 trace_i915_gem_object_destroy(obj);
4103
4104 GEM_BUG_ON(i915_gem_object_is_active(obj));
4105 list_for_each_entry_safe(vma, vn,
4106 &obj->vma_list, obj_link) {
4107 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
4108 GEM_BUG_ON(i915_vma_is_active(vma));
4109 vma->flags &= ~I915_VMA_PIN_MASK;
4110 i915_vma_close(vma);
4111 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004112 GEM_BUG_ON(!list_empty(&obj->vma_list));
4113 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004114
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004115 list_del(&obj->global_link);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004116 }
4117 intel_runtime_pm_put(i915);
4118 mutex_unlock(&i915->drm.struct_mutex);
4119
4120 llist_for_each_entry_safe(obj, on, freed, freed) {
4121 GEM_BUG_ON(obj->bind_count);
4122 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
4123
4124 if (obj->ops->release)
4125 obj->ops->release(obj);
4126
4127 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4128 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004129 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004130 GEM_BUG_ON(obj->mm.pages);
4131
4132 if (obj->base.import_attach)
4133 drm_prime_gem_destroy(&obj->base, NULL);
4134
Chris Wilsond07f0e52016-10-28 13:58:44 +01004135 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004136 drm_gem_object_release(&obj->base);
4137 i915_gem_info_remove_obj(i915, obj->base.size);
4138
4139 kfree(obj->bit_17);
4140 i915_gem_object_free(obj);
4141 }
4142}
4143
4144static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4145{
4146 struct llist_node *freed;
4147
4148 freed = llist_del_all(&i915->mm.free_list);
4149 if (unlikely(freed))
4150 __i915_gem_free_objects(i915, freed);
4151}
4152
4153static void __i915_gem_free_work(struct work_struct *work)
4154{
4155 struct drm_i915_private *i915 =
4156 container_of(work, struct drm_i915_private, mm.free_work);
4157 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004158
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004159 /* All file-owned VMA should have been released by this point through
4160 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4161 * However, the object may also be bound into the global GTT (e.g.
4162 * older GPUs without per-process support, or for direct access through
4163 * the GTT either for the user or for scanout). Those VMA still need to
4164 * unbound now.
4165 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004166
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004167 while ((freed = llist_del_all(&i915->mm.free_list)))
4168 __i915_gem_free_objects(i915, freed);
4169}
4170
4171static void __i915_gem_free_object_rcu(struct rcu_head *head)
4172{
4173 struct drm_i915_gem_object *obj =
4174 container_of(head, typeof(*obj), rcu);
4175 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4176
4177 /* We can't simply use call_rcu() from i915_gem_free_object()
4178 * as we need to block whilst unbinding, and the call_rcu
4179 * task may be called from softirq context. So we take a
4180 * detour through a worker.
4181 */
4182 if (llist_add(&obj->freed, &i915->mm.free_list))
4183 schedule_work(&i915->mm.free_work);
4184}
4185
4186void i915_gem_free_object(struct drm_gem_object *gem_obj)
4187{
4188 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4189
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004190 if (obj->mm.quirked)
4191 __i915_gem_object_unpin_pages(obj);
4192
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004193 if (discard_backing_storage(obj))
4194 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004195
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004196 /* Before we free the object, make sure any pure RCU-only
4197 * read-side critical sections are complete, e.g.
4198 * i915_gem_busy_ioctl(). For the corresponding synchronized
4199 * lookup see i915_gem_object_lookup_rcu().
4200 */
4201 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004202}
4203
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004204void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4205{
4206 lockdep_assert_held(&obj->base.dev->struct_mutex);
4207
4208 GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
4209 if (i915_gem_object_is_active(obj))
4210 i915_gem_object_set_active_reference(obj);
4211 else
4212 i915_gem_object_put(obj);
4213}
4214
Chris Wilson3033aca2016-10-28 13:58:47 +01004215static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
4216{
4217 struct intel_engine_cs *engine;
4218 enum intel_engine_id id;
4219
4220 for_each_engine(engine, dev_priv, id)
Chris Wilsonf131e352016-12-29 14:40:37 +00004221 GEM_BUG_ON(engine->last_retired_context &&
4222 !i915_gem_context_is_kernel(engine->last_retired_context));
Chris Wilson3033aca2016-10-28 13:58:47 +01004223}
4224
Chris Wilson24145512017-01-24 11:01:35 +00004225void i915_gem_sanitize(struct drm_i915_private *i915)
4226{
4227 /*
4228 * If we inherit context state from the BIOS or earlier occupants
4229 * of the GPU, the GPU may be in an inconsistent state when we
4230 * try to take over. The only way to remove the earlier state
4231 * is by resetting. However, resetting on earlier gen is tricky as
4232 * it may impact the display and we are uncertain about the stability
4233 * of the reset, so we only reset recent machines with logical
4234 * context support (that must be reset to remove any stray contexts).
4235 */
4236 if (HAS_HW_CONTEXTS(i915)) {
4237 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4238 WARN_ON(reset && reset != -ENODEV);
4239 }
4240}
4241
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004242int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004243{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004244 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004245 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004246
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004247 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004248 intel_suspend_gt_powersave(dev_priv);
4249
Chris Wilson45c5f202013-10-16 11:50:01 +01004250 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004251
4252 /* We have to flush all the executing contexts to main memory so
4253 * that they can saved in the hibernation image. To ensure the last
4254 * context image is coherent, we have to switch away from it. That
4255 * leaves the dev_priv->kernel_context still active when
4256 * we actually suspend, and its image in memory may not match the GPU
4257 * state. Fortunately, the kernel_context is disposable and we do
4258 * not rely on its state.
4259 */
4260 ret = i915_gem_switch_to_kernel_context(dev_priv);
4261 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004262 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004263
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004264 ret = i915_gem_wait_for_idle(dev_priv,
4265 I915_WAIT_INTERRUPTIBLE |
4266 I915_WAIT_LOCKED);
Chris Wilsonf7403342013-09-13 23:57:04 +01004267 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004268 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004269
Chris Wilsonc0336662016-05-06 15:40:21 +01004270 i915_gem_retire_requests(dev_priv);
Chris Wilson28176ef2016-10-28 13:58:56 +01004271 GEM_BUG_ON(dev_priv->gt.active_requests);
Eric Anholt673a3942008-07-30 12:06:12 -07004272
Chris Wilson3033aca2016-10-28 13:58:47 +01004273 assert_kernel_context_is_current(dev_priv);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004274 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004275 mutex_unlock(&dev->struct_mutex);
4276
Chris Wilson737b1502015-01-26 18:03:03 +02004277 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004278 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004279
4280 /* As the idle_work is rearming if it detects a race, play safe and
4281 * repeat the flush until it is definitely idle.
4282 */
4283 while (flush_delayed_work(&dev_priv->gt.idle_work))
4284 ;
4285
4286 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson29105cc2010-01-07 10:39:13 +00004287
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004288 /* Assert that we sucessfully flushed all the work and
4289 * reset the GPU back to its idle, low power state.
4290 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004291 WARN_ON(dev_priv->gt.awake);
Imre Deak31ab49a2016-11-07 11:20:05 +02004292 WARN_ON(!intel_execlists_idle(dev_priv));
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004293
Imre Deak1c777c52016-10-12 17:46:37 +03004294 /*
4295 * Neither the BIOS, ourselves or any other kernel
4296 * expects the system to be in execlists mode on startup,
4297 * so we need to reset the GPU back to legacy mode. And the only
4298 * known way to disable logical contexts is through a GPU reset.
4299 *
4300 * So in order to leave the system in a known default configuration,
4301 * always reset the GPU upon unload and suspend. Afterwards we then
4302 * clean up the GEM state tracking, flushing off the requests and
4303 * leaving the system in a known idle state.
4304 *
4305 * Note that is of the upmost importance that the GPU is idle and
4306 * all stray writes are flushed *before* we dismantle the backing
4307 * storage for the pinned objects.
4308 *
4309 * However, since we are uncertain that resetting the GPU on older
4310 * machines is a good idea, we don't - just in case it leaves the
4311 * machine in an unusable condition.
4312 */
Chris Wilson24145512017-01-24 11:01:35 +00004313 i915_gem_sanitize(dev_priv);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004314 goto out_rpm_put;
Imre Deak1c777c52016-10-12 17:46:37 +03004315
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004316err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004317 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004318out_rpm_put:
4319 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004320 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004321}
4322
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004323void i915_gem_resume(struct drm_i915_private *dev_priv)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004324{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004325 struct drm_device *dev = &dev_priv->drm;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004326
Imre Deak31ab49a2016-11-07 11:20:05 +02004327 WARN_ON(dev_priv->gt.awake);
4328
Chris Wilson5ab57c72016-07-15 14:56:20 +01004329 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00004330 i915_gem_restore_gtt_mappings(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004331
4332 /* As we didn't flush the kernel context before suspend, we cannot
4333 * guarantee that the context image is complete. So let's just reset
4334 * it and start again.
4335 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004336 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004337
4338 mutex_unlock(&dev->struct_mutex);
4339}
4340
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004341void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004342{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004343 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004344 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4345 return;
4346
4347 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4348 DISP_TILE_SURFACE_SWIZZLING);
4349
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004350 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004351 return;
4352
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004353 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004354 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004355 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004356 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004357 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004358 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004359 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004360 else
4361 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004362}
Daniel Vettere21af882012-02-09 20:53:27 +01004363
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004364static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004365{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004366 I915_WRITE(RING_CTL(base), 0);
4367 I915_WRITE(RING_HEAD(base), 0);
4368 I915_WRITE(RING_TAIL(base), 0);
4369 I915_WRITE(RING_START(base), 0);
4370}
4371
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004372static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004373{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004374 if (IS_I830(dev_priv)) {
4375 init_unused_ring(dev_priv, PRB1_BASE);
4376 init_unused_ring(dev_priv, SRB0_BASE);
4377 init_unused_ring(dev_priv, SRB1_BASE);
4378 init_unused_ring(dev_priv, SRB2_BASE);
4379 init_unused_ring(dev_priv, SRB3_BASE);
4380 } else if (IS_GEN2(dev_priv)) {
4381 init_unused_ring(dev_priv, SRB0_BASE);
4382 init_unused_ring(dev_priv, SRB1_BASE);
4383 } else if (IS_GEN3(dev_priv)) {
4384 init_unused_ring(dev_priv, PRB1_BASE);
4385 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004386 }
4387}
4388
Chris Wilson20a8a742017-02-08 14:30:31 +00004389static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004390{
Chris Wilson20a8a742017-02-08 14:30:31 +00004391 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004392 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304393 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004394 int err;
4395
4396 for_each_engine(engine, i915, id) {
4397 err = engine->init_hw(engine);
4398 if (err)
4399 return err;
4400 }
4401
4402 return 0;
4403}
4404
4405int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4406{
Chris Wilsond200cda2016-04-28 09:56:44 +01004407 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004408
Chris Wilsonde867c22016-10-25 13:16:02 +01004409 dev_priv->gt.last_init_time = ktime_get();
4410
Chris Wilson5e4f5182015-02-13 14:35:59 +00004411 /* Double layer security blanket, see i915_gem_init() */
4412 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4413
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004414 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004415 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004416
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004417 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004418 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004419 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004420
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004421 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004422 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004423 u32 temp = I915_READ(GEN7_MSG_CTL);
4424 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4425 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004426 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004427 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4428 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4429 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4430 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004431 }
4432
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004433 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004434
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004435 /*
4436 * At least 830 can leave some of the unused rings
4437 * "active" (ie. head != tail) after resume which
4438 * will prevent c3 entry. Makes sure all unused rings
4439 * are totally idle.
4440 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004441 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004442
Dave Gordoned54c1a2016-01-19 19:02:54 +00004443 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004444
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004445 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004446 if (ret) {
4447 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4448 goto out;
4449 }
4450
4451 /* Need to do basic initialisation of all rings first: */
Chris Wilson20a8a742017-02-08 14:30:31 +00004452 ret = __i915_gem_restart_engines(dev_priv);
4453 if (ret)
4454 goto out;
Mika Kuoppala99433932013-01-22 14:12:17 +02004455
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004456 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004457
Alex Dai33a732f2015-08-12 15:43:36 +01004458 /* We can't enable contexts until all firmware is loaded */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004459 ret = intel_guc_setup(dev_priv);
Dave Gordone556f7c2016-06-07 09:14:49 +01004460 if (ret)
4461 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004462
Chris Wilson5e4f5182015-02-13 14:35:59 +00004463out:
4464 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004465 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004466}
4467
Chris Wilson39df9192016-07-20 13:31:57 +01004468bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4469{
4470 if (INTEL_INFO(dev_priv)->gen < 6)
4471 return false;
4472
4473 /* TODO: make semaphores and Execlists play nicely together */
4474 if (i915.enable_execlists)
4475 return false;
4476
4477 if (value >= 0)
4478 return value;
4479
4480#ifdef CONFIG_INTEL_IOMMU
4481 /* Enable semaphores on SNB when IO remapping is off */
4482 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4483 return false;
4484#endif
4485
4486 return true;
4487}
4488
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004489int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004490{
Chris Wilson1070a422012-04-24 15:47:41 +01004491 int ret;
4492
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004493 mutex_lock(&dev_priv->drm.struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004494
Chris Wilson57822dc2017-02-22 11:40:48 +00004495 i915_gem_clflush_init(dev_priv);
4496
Oscar Mateoa83014d2014-07-24 17:04:21 +01004497 if (!i915.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004498 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004499 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004500 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004501 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004502 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004503 }
4504
Chris Wilson5e4f5182015-02-13 14:35:59 +00004505 /* This is just a security blanket to placate dragons.
4506 * On some systems, we very sporadically observe that the first TLBs
4507 * used by the CS may be stale, despite us poking the TLB reset. If
4508 * we hold the forcewake during initialisation these problems
4509 * just magically go away.
4510 */
4511 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4512
Chris Wilson72778cb2016-05-19 16:17:16 +01004513 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004514
4515 ret = i915_gem_init_ggtt(dev_priv);
4516 if (ret)
4517 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004518
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004519 ret = i915_gem_context_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004520 if (ret)
4521 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004522
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004523 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004524 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004525 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004526
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004527 ret = i915_gem_init_hw(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004528 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004529 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004530 * wedged. But we only want to do this where the GPU is angry,
4531 * for all other failure, such as an allocation failure, bail.
4532 */
4533 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004534 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004535 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004536 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004537
4538out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004539 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004540 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004541
Chris Wilson60990322014-04-09 09:19:42 +01004542 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004543}
4544
Chris Wilson24145512017-01-24 11:01:35 +00004545void i915_gem_init_mmio(struct drm_i915_private *i915)
4546{
4547 i915_gem_sanitize(i915);
4548}
4549
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004550void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004551i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004552{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004553 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304554 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004555
Akash Goel3b3f1652016-10-13 22:44:48 +05304556 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004557 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004558}
4559
Eric Anholt673a3942008-07-30 12:06:12 -07004560void
Imre Deak40ae4e12016-03-16 14:54:03 +02004561i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4562{
Chris Wilson49ef5292016-08-18 17:17:00 +01004563 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004564
4565 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4566 !IS_CHERRYVIEW(dev_priv))
4567 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02004568 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
4569 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4570 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004571 dev_priv->num_fence_regs = 16;
4572 else
4573 dev_priv->num_fence_regs = 8;
4574
Chris Wilsonc0336662016-05-06 15:40:21 +01004575 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004576 dev_priv->num_fence_regs =
4577 I915_READ(vgtif_reg(avail_rs.fence_num));
4578
4579 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004580 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4581 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4582
4583 fence->i915 = dev_priv;
4584 fence->id = i;
4585 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4586 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004587 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004588
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004589 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004590}
4591
Chris Wilson73cb9702016-10-28 13:58:46 +01004592int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004593i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004594{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004595 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004596
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004597 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
4598 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01004599 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01004600
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004601 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
4602 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01004603 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01004604
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004605 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
4606 SLAB_HWCACHE_ALIGN |
4607 SLAB_RECLAIM_ACCOUNT |
4608 SLAB_DESTROY_BY_RCU);
4609 if (!dev_priv->requests)
Chris Wilson73cb9702016-10-28 13:58:46 +01004610 goto err_vmas;
Chris Wilson73cb9702016-10-28 13:58:46 +01004611
Chris Wilson52e54202016-11-14 20:41:02 +00004612 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
4613 SLAB_HWCACHE_ALIGN |
4614 SLAB_RECLAIM_ACCOUNT);
4615 if (!dev_priv->dependencies)
4616 goto err_requests;
4617
Chris Wilson73cb9702016-10-28 13:58:46 +01004618 mutex_lock(&dev_priv->drm.struct_mutex);
4619 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00004620 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01004621 mutex_unlock(&dev_priv->drm.struct_mutex);
4622 if (err)
Chris Wilson52e54202016-11-14 20:41:02 +00004623 goto err_dependencies;
Eric Anholt673a3942008-07-30 12:06:12 -07004624
Ben Widawskya33afea2013-09-17 21:12:45 -07004625 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004626 INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
4627 init_llist_head(&dev_priv->mm.free_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004628 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4629 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004630 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004631 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004632 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004633 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004634 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004635 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004636 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004637 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004638
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004639 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004640
Chris Wilsonce453d82011-02-21 14:43:56 +00004641 dev_priv->mm.interruptible = true;
4642
Joonas Lahtinen6f633402016-09-01 14:58:21 +03004643 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
4644
Chris Wilsonb5add952016-08-04 16:32:36 +01004645 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01004646
4647 return 0;
4648
Chris Wilson52e54202016-11-14 20:41:02 +00004649err_dependencies:
4650 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01004651err_requests:
4652 kmem_cache_destroy(dev_priv->requests);
4653err_vmas:
4654 kmem_cache_destroy(dev_priv->vmas);
4655err_objects:
4656 kmem_cache_destroy(dev_priv->objects);
4657err_out:
4658 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004659}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004660
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004661void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02004662{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004663 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004664 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004665 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004666
Matthew Auldea84aa72016-11-17 21:04:11 +00004667 mutex_lock(&dev_priv->drm.struct_mutex);
4668 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
4669 WARN_ON(!list_empty(&dev_priv->gt.timelines));
4670 mutex_unlock(&dev_priv->drm.struct_mutex);
4671
Chris Wilson52e54202016-11-14 20:41:02 +00004672 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02004673 kmem_cache_destroy(dev_priv->requests);
4674 kmem_cache_destroy(dev_priv->vmas);
4675 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01004676
4677 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
4678 rcu_barrier();
Imre Deakd64aa092016-01-19 15:26:29 +02004679}
4680
Chris Wilson6a800ea2016-09-21 14:51:07 +01004681int i915_gem_freeze(struct drm_i915_private *dev_priv)
4682{
Chris Wilson6a800ea2016-09-21 14:51:07 +01004683 mutex_lock(&dev_priv->drm.struct_mutex);
4684 i915_gem_shrink_all(dev_priv);
4685 mutex_unlock(&dev_priv->drm.struct_mutex);
4686
Chris Wilson6a800ea2016-09-21 14:51:07 +01004687 return 0;
4688}
4689
Chris Wilson461fb992016-05-14 07:26:33 +01004690int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4691{
4692 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01004693 struct list_head *phases[] = {
4694 &dev_priv->mm.unbound_list,
4695 &dev_priv->mm.bound_list,
4696 NULL
4697 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01004698
4699 /* Called just before we write the hibernation image.
4700 *
4701 * We need to update the domain tracking to reflect that the CPU
4702 * will be accessing all the pages to create and restore from the
4703 * hibernation, and so upon restoration those pages will be in the
4704 * CPU domain.
4705 *
4706 * To make sure the hibernation image contains the latest state,
4707 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01004708 *
4709 * To try and reduce the hibernation image, we manually shrink
4710 * the objects as well.
Chris Wilson461fb992016-05-14 07:26:33 +01004711 */
4712
Chris Wilson6a800ea2016-09-21 14:51:07 +01004713 mutex_lock(&dev_priv->drm.struct_mutex);
4714 i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
Chris Wilson461fb992016-05-14 07:26:33 +01004715
Chris Wilson7aab2d52016-09-09 20:02:18 +01004716 for (p = phases; *p; p++) {
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004717 list_for_each_entry(obj, *p, global_link) {
Chris Wilson7aab2d52016-09-09 20:02:18 +01004718 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4719 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4720 }
Chris Wilson461fb992016-05-14 07:26:33 +01004721 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01004722 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01004723
4724 return 0;
4725}
4726
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004727void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004728{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004729 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004730 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004731
4732 /* Clean up our request list when the client is going away, so that
4733 * later retire_requests won't dereference our soon-to-be-gone
4734 * file_priv.
4735 */
Chris Wilson1c255952010-09-26 11:03:27 +01004736 spin_lock(&file_priv->mm.lock);
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004737 list_for_each_entry(request, &file_priv->mm.request_list, client_list)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004738 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004739 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004740
Chris Wilson2e1b8732015-04-27 13:41:22 +01004741 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004742 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004743 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004744 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004745 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004746}
4747
4748int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4749{
4750 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004751 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004752
Chris Wilsonc4c29d72016-11-09 10:45:07 +00004753 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004754
4755 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4756 if (!file_priv)
4757 return -ENOMEM;
4758
4759 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004760 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004761 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004762 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004763
4764 spin_lock_init(&file_priv->mm.lock);
4765 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004766
Chris Wilsonc80ff162016-07-27 09:07:27 +01004767 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004768
Ben Widawskye422b882013-12-06 14:10:58 -08004769 ret = i915_gem_context_open(dev, file);
4770 if (ret)
4771 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004772
Ben Widawskye422b882013-12-06 14:10:58 -08004773 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004774}
4775
Daniel Vetterb680c372014-09-19 18:27:27 +02004776/**
4777 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004778 * @old: current GEM buffer for the frontbuffer slots
4779 * @new: new GEM buffer for the frontbuffer slots
4780 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004781 *
4782 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4783 * from @old and setting them in @new. Both @old and @new can be NULL.
4784 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004785void i915_gem_track_fb(struct drm_i915_gem_object *old,
4786 struct drm_i915_gem_object *new,
4787 unsigned frontbuffer_bits)
4788{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004789 /* Control of individual bits within the mask are guarded by
4790 * the owning plane->mutex, i.e. we can never see concurrent
4791 * manipulation of individual bits. But since the bitfield as a whole
4792 * is updated using RMW, we need to use atomics in order to update
4793 * the bits.
4794 */
4795 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
4796 sizeof(atomic_t) * BITS_PER_BYTE);
4797
Daniel Vettera071fa02014-06-18 23:28:09 +02004798 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004799 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
4800 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004801 }
4802
4803 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004804 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
4805 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004806 }
4807}
4808
Dave Gordonea702992015-07-09 19:29:02 +01004809/* Allocate a new GEM object and fill it with the supplied data */
4810struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004811i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01004812 const void *data, size_t size)
4813{
4814 struct drm_i915_gem_object *obj;
4815 struct sg_table *sg;
4816 size_t bytes;
4817 int ret;
4818
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004819 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01004820 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01004821 return obj;
4822
4823 ret = i915_gem_object_set_to_cpu_domain(obj, true);
4824 if (ret)
4825 goto fail;
4826
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004827 ret = i915_gem_object_pin_pages(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004828 if (ret)
4829 goto fail;
4830
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004831 sg = obj->mm.pages;
Dave Gordonea702992015-07-09 19:29:02 +01004832 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004833 obj->mm.dirty = true; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01004834 i915_gem_object_unpin_pages(obj);
4835
4836 if (WARN_ON(bytes != size)) {
4837 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
4838 ret = -EFAULT;
4839 goto fail;
4840 }
4841
4842 return obj;
4843
4844fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004845 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004846 return ERR_PTR(ret);
4847}
Chris Wilson96d77632016-10-28 13:58:33 +01004848
4849struct scatterlist *
4850i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
4851 unsigned int n,
4852 unsigned int *offset)
4853{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004854 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01004855 struct scatterlist *sg;
4856 unsigned int idx, count;
4857
4858 might_sleep();
4859 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004860 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01004861
4862 /* As we iterate forward through the sg, we record each entry in a
4863 * radixtree for quick repeated (backwards) lookups. If we have seen
4864 * this index previously, we will have an entry for it.
4865 *
4866 * Initial lookup is O(N), but this is amortized to O(1) for
4867 * sequential page access (where each new request is consecutive
4868 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
4869 * i.e. O(1) with a large constant!
4870 */
4871 if (n < READ_ONCE(iter->sg_idx))
4872 goto lookup;
4873
4874 mutex_lock(&iter->lock);
4875
4876 /* We prefer to reuse the last sg so that repeated lookup of this
4877 * (or the subsequent) sg are fast - comparing against the last
4878 * sg is faster than going through the radixtree.
4879 */
4880
4881 sg = iter->sg_pos;
4882 idx = iter->sg_idx;
4883 count = __sg_page_count(sg);
4884
4885 while (idx + count <= n) {
4886 unsigned long exception, i;
4887 int ret;
4888
4889 /* If we cannot allocate and insert this entry, or the
4890 * individual pages from this range, cancel updating the
4891 * sg_idx so that on this lookup we are forced to linearly
4892 * scan onwards, but on future lookups we will try the
4893 * insertion again (in which case we need to be careful of
4894 * the error return reporting that we have already inserted
4895 * this index).
4896 */
4897 ret = radix_tree_insert(&iter->radix, idx, sg);
4898 if (ret && ret != -EEXIST)
4899 goto scan;
4900
4901 exception =
4902 RADIX_TREE_EXCEPTIONAL_ENTRY |
4903 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
4904 for (i = 1; i < count; i++) {
4905 ret = radix_tree_insert(&iter->radix, idx + i,
4906 (void *)exception);
4907 if (ret && ret != -EEXIST)
4908 goto scan;
4909 }
4910
4911 idx += count;
4912 sg = ____sg_next(sg);
4913 count = __sg_page_count(sg);
4914 }
4915
4916scan:
4917 iter->sg_pos = sg;
4918 iter->sg_idx = idx;
4919
4920 mutex_unlock(&iter->lock);
4921
4922 if (unlikely(n < idx)) /* insertion completed by another thread */
4923 goto lookup;
4924
4925 /* In case we failed to insert the entry into the radixtree, we need
4926 * to look beyond the current sg.
4927 */
4928 while (idx + count <= n) {
4929 idx += count;
4930 sg = ____sg_next(sg);
4931 count = __sg_page_count(sg);
4932 }
4933
4934 *offset = n - idx;
4935 return sg;
4936
4937lookup:
4938 rcu_read_lock();
4939
4940 sg = radix_tree_lookup(&iter->radix, n);
4941 GEM_BUG_ON(!sg);
4942
4943 /* If this index is in the middle of multi-page sg entry,
4944 * the radixtree will contain an exceptional entry that points
4945 * to the start of that range. We will return the pointer to
4946 * the base page and the offset of this page within the
4947 * sg entry's range.
4948 */
4949 *offset = 0;
4950 if (unlikely(radix_tree_exception(sg))) {
4951 unsigned long base =
4952 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
4953
4954 sg = radix_tree_lookup(&iter->radix, base);
4955 GEM_BUG_ON(!sg);
4956
4957 *offset = n - base;
4958 }
4959
4960 rcu_read_unlock();
4961
4962 return sg;
4963}
4964
4965struct page *
4966i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
4967{
4968 struct scatterlist *sg;
4969 unsigned int offset;
4970
4971 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
4972
4973 sg = i915_gem_object_get_sg(obj, n, &offset);
4974 return nth_page(sg_page(sg), offset);
4975}
4976
4977/* Like i915_gem_object_get_page(), but mark the returned page dirty */
4978struct page *
4979i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
4980 unsigned int n)
4981{
4982 struct page *page;
4983
4984 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004985 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01004986 set_page_dirty(page);
4987
4988 return page;
4989}
4990
4991dma_addr_t
4992i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
4993 unsigned long n)
4994{
4995 struct scatterlist *sg;
4996 unsigned int offset;
4997
4998 sg = i915_gem_object_get_sg(obj, n, &offset);
4999 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5000}
Chris Wilson935a2f72017-02-13 17:15:13 +00005001
5002#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5003#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005004#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005005#include "selftests/huge_gem_object.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005006#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005007#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005008#endif