blob: d87983ba536f9ba8d23a37cfc260104b347d1386 [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 Wilson03d1cac2017-03-08 13:26:28 +0000465 /* Oportunistically prune the fences iff we know they have *all* been
466 * signaled and that the reservation object has not been changed (i.e.
467 * no new fences have been added).
468 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000469 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000470 if (reservation_object_trylock(resv)) {
471 if (!__read_seqcount_retry(&resv->seq, seq))
472 reservation_object_add_excl_fence(resv, NULL);
473 reservation_object_unlock(resv);
474 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000475 }
476
Chris Wilsone95433c2016-10-28 13:58:27 +0100477 return timeout;
478}
479
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000480static void __fence_set_priority(struct dma_fence *fence, int prio)
481{
482 struct drm_i915_gem_request *rq;
483 struct intel_engine_cs *engine;
484
485 if (!dma_fence_is_i915(fence))
486 return;
487
488 rq = to_request(fence);
489 engine = rq->engine;
490 if (!engine->schedule)
491 return;
492
493 engine->schedule(rq, prio);
494}
495
496static void fence_set_priority(struct dma_fence *fence, int prio)
497{
498 /* Recurse once into a fence-array */
499 if (dma_fence_is_array(fence)) {
500 struct dma_fence_array *array = to_dma_fence_array(fence);
501 int i;
502
503 for (i = 0; i < array->num_fences; i++)
504 __fence_set_priority(array->fences[i], prio);
505 } else {
506 __fence_set_priority(fence, prio);
507 }
508}
509
510int
511i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
512 unsigned int flags,
513 int prio)
514{
515 struct dma_fence *excl;
516
517 if (flags & I915_WAIT_ALL) {
518 struct dma_fence **shared;
519 unsigned int count, i;
520 int ret;
521
522 ret = reservation_object_get_fences_rcu(obj->resv,
523 &excl, &count, &shared);
524 if (ret)
525 return ret;
526
527 for (i = 0; i < count; i++) {
528 fence_set_priority(shared[i], prio);
529 dma_fence_put(shared[i]);
530 }
531
532 kfree(shared);
533 } else {
534 excl = reservation_object_get_excl_rcu(obj->resv);
535 }
536
537 if (excl) {
538 fence_set_priority(excl, prio);
539 dma_fence_put(excl);
540 }
541 return 0;
542}
543
Chris Wilson00e60f22016-08-04 16:32:40 +0100544/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100545 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100546 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100547 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
548 * @timeout: how long to wait
549 * @rps: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100550 */
551int
Chris Wilsone95433c2016-10-28 13:58:27 +0100552i915_gem_object_wait(struct drm_i915_gem_object *obj,
553 unsigned int flags,
554 long timeout,
555 struct intel_rps_client *rps)
Chris Wilson00e60f22016-08-04 16:32:40 +0100556{
Chris Wilsone95433c2016-10-28 13:58:27 +0100557 might_sleep();
558#if IS_ENABLED(CONFIG_LOCKDEP)
559 GEM_BUG_ON(debug_locks &&
560 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
561 !!(flags & I915_WAIT_LOCKED));
562#endif
563 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100564
Chris Wilsond07f0e52016-10-28 13:58:44 +0100565 timeout = i915_gem_object_wait_reservation(obj->resv,
566 flags, timeout,
567 rps);
Chris Wilsone95433c2016-10-28 13:58:27 +0100568 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100569}
570
571static struct intel_rps_client *to_rps_client(struct drm_file *file)
572{
573 struct drm_i915_file_private *fpriv = file->driver_priv;
574
575 return &fpriv->rps;
576}
577
Chris Wilson00731152014-05-21 12:42:56 +0100578int
579i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
580 int align)
581{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800582 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100583
Chris Wilsondbb43512016-12-07 13:34:11 +0000584 if (align > obj->base.size)
585 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100586
Chris Wilsondbb43512016-12-07 13:34:11 +0000587 if (obj->ops == &i915_gem_phys_ops)
Chris Wilson00731152014-05-21 12:42:56 +0100588 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100589
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100590 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100591 return -EFAULT;
592
593 if (obj->base.filp == NULL)
594 return -EINVAL;
595
Chris Wilson4717ca92016-08-04 07:52:28 +0100596 ret = i915_gem_object_unbind(obj);
597 if (ret)
598 return ret;
599
Chris Wilson548625e2016-11-01 12:11:34 +0000600 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100601 if (obj->mm.pages)
602 return -EBUSY;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800603
Chris Wilson581ab1f2017-02-15 16:39:00 +0000604 GEM_BUG_ON(obj->ops != &i915_gem_object_ops);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800605 obj->ops = &i915_gem_phys_ops;
606
Chris Wilson581ab1f2017-02-15 16:39:00 +0000607 ret = i915_gem_object_pin_pages(obj);
608 if (ret)
609 goto err_xfer;
610
611 return 0;
612
613err_xfer:
614 obj->ops = &i915_gem_object_ops;
615 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100616}
617
618static int
619i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
620 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100621 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100622{
Chris Wilson00731152014-05-21 12:42:56 +0100623 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300624 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800625
626 /* We manually control the domain here and pretend that it
627 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
628 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700629 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000630 if (copy_from_user(vaddr, user_data, args->size))
631 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100632
Chris Wilson6a2c4232014-11-04 04:51:40 -0800633 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000634 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200635
Chris Wilsond59b21e2017-02-22 11:40:49 +0000636 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000637 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100638}
639
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000640void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000641{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100642 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000643}
644
645void i915_gem_object_free(struct drm_i915_gem_object *obj)
646{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100647 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100648 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000649}
650
Dave Airlieff72145b2011-02-07 12:16:14 +1000651static int
652i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000653 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000654 uint64_t size,
655 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700656{
Chris Wilson05394f32010-11-08 19:18:58 +0000657 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300658 int ret;
659 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700660
Dave Airlieff72145b2011-02-07 12:16:14 +1000661 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200662 if (size == 0)
663 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700664
665 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000666 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100667 if (IS_ERR(obj))
668 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700669
Chris Wilson05394f32010-11-08 19:18:58 +0000670 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100671 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100672 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200673 if (ret)
674 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100675
Dave Airlieff72145b2011-02-07 12:16:14 +1000676 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700677 return 0;
678}
679
Dave Airlieff72145b2011-02-07 12:16:14 +1000680int
681i915_gem_dumb_create(struct drm_file *file,
682 struct drm_device *dev,
683 struct drm_mode_create_dumb *args)
684{
685 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300686 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000687 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000688 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000689 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000690}
691
Dave Airlieff72145b2011-02-07 12:16:14 +1000692/**
693 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100694 * @dev: drm device pointer
695 * @data: ioctl data blob
696 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000697 */
698int
699i915_gem_create_ioctl(struct drm_device *dev, void *data,
700 struct drm_file *file)
701{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000702 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000703 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200704
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000705 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100706
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000707 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000708 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000709}
710
Daniel Vetter8c599672011-12-14 13:57:31 +0100711static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100712__copy_to_user_swizzled(char __user *cpu_vaddr,
713 const char *gpu_vaddr, int gpu_offset,
714 int length)
715{
716 int ret, cpu_offset = 0;
717
718 while (length > 0) {
719 int cacheline_end = ALIGN(gpu_offset + 1, 64);
720 int this_length = min(cacheline_end - gpu_offset, length);
721 int swizzled_gpu_offset = gpu_offset ^ 64;
722
723 ret = __copy_to_user(cpu_vaddr + cpu_offset,
724 gpu_vaddr + swizzled_gpu_offset,
725 this_length);
726 if (ret)
727 return ret + length;
728
729 cpu_offset += this_length;
730 gpu_offset += this_length;
731 length -= this_length;
732 }
733
734 return 0;
735}
736
737static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700738__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
739 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100740 int length)
741{
742 int ret, cpu_offset = 0;
743
744 while (length > 0) {
745 int cacheline_end = ALIGN(gpu_offset + 1, 64);
746 int this_length = min(cacheline_end - gpu_offset, length);
747 int swizzled_gpu_offset = gpu_offset ^ 64;
748
749 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
750 cpu_vaddr + cpu_offset,
751 this_length);
752 if (ret)
753 return ret + length;
754
755 cpu_offset += this_length;
756 gpu_offset += this_length;
757 length -= this_length;
758 }
759
760 return 0;
761}
762
Brad Volkin4c914c02014-02-18 10:15:45 -0800763/*
764 * Pins the specified object's pages and synchronizes the object with
765 * GPU accesses. Sets needs_clflush to non-zero if the caller should
766 * flush the object from the CPU cache.
767 */
768int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100769 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800770{
771 int ret;
772
Chris Wilsone95433c2016-10-28 13:58:27 +0100773 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800774
Chris Wilsone95433c2016-10-28 13:58:27 +0100775 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100776 if (!i915_gem_object_has_struct_page(obj))
777 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800778
Chris Wilsone95433c2016-10-28 13:58:27 +0100779 ret = i915_gem_object_wait(obj,
780 I915_WAIT_INTERRUPTIBLE |
781 I915_WAIT_LOCKED,
782 MAX_SCHEDULE_TIMEOUT,
783 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100784 if (ret)
785 return ret;
786
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100787 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100788 if (ret)
789 return ret;
790
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000791 if (i915_gem_object_is_coherent(obj) ||
792 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
793 ret = i915_gem_object_set_to_cpu_domain(obj, false);
794 if (ret)
795 goto err_unpin;
796 else
797 goto out;
798 }
799
Chris Wilsona314d5c2016-08-18 17:16:48 +0100800 i915_gem_object_flush_gtt_write_domain(obj);
801
Chris Wilson43394c72016-08-18 17:16:47 +0100802 /* If we're not in the cpu read domain, set ourself into the gtt
803 * read domain and manually flush cachelines (if required). This
804 * optimizes for the case when the gpu will dirty the data
805 * anyway again before the next pread happens.
806 */
807 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000808 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800809
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000810out:
Chris Wilson97649512016-08-18 17:16:50 +0100811 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100812 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100813
814err_unpin:
815 i915_gem_object_unpin_pages(obj);
816 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100817}
818
819int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
820 unsigned int *needs_clflush)
821{
822 int ret;
823
Chris Wilsone95433c2016-10-28 13:58:27 +0100824 lockdep_assert_held(&obj->base.dev->struct_mutex);
825
Chris Wilson43394c72016-08-18 17:16:47 +0100826 *needs_clflush = 0;
827 if (!i915_gem_object_has_struct_page(obj))
828 return -ENODEV;
829
Chris Wilsone95433c2016-10-28 13:58:27 +0100830 ret = i915_gem_object_wait(obj,
831 I915_WAIT_INTERRUPTIBLE |
832 I915_WAIT_LOCKED |
833 I915_WAIT_ALL,
834 MAX_SCHEDULE_TIMEOUT,
835 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100836 if (ret)
837 return ret;
838
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100839 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100840 if (ret)
841 return ret;
842
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000843 if (i915_gem_object_is_coherent(obj) ||
844 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
845 ret = i915_gem_object_set_to_cpu_domain(obj, true);
846 if (ret)
847 goto err_unpin;
848 else
849 goto out;
850 }
851
Chris Wilsona314d5c2016-08-18 17:16:48 +0100852 i915_gem_object_flush_gtt_write_domain(obj);
853
Chris Wilson43394c72016-08-18 17:16:47 +0100854 /* If we're not in the cpu write domain, set ourself into the
855 * gtt write domain and manually flush cachelines (as required).
856 * This optimizes for the case when the gpu will use the data
857 * right away and we therefore have to clflush anyway.
858 */
859 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000860 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100861
862 /* Same trick applies to invalidate partially written cachelines read
863 * before writing.
864 */
865 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000866 *needs_clflush |= CLFLUSH_BEFORE;
Chris Wilson43394c72016-08-18 17:16:47 +0100867
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000868out:
Chris Wilson43394c72016-08-18 17:16:47 +0100869 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100870 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100871 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100872 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100873
874err_unpin:
875 i915_gem_object_unpin_pages(obj);
876 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800877}
878
Daniel Vetter23c18c72012-03-25 19:47:42 +0200879static void
880shmem_clflush_swizzled_range(char *addr, unsigned long length,
881 bool swizzled)
882{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200883 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200884 unsigned long start = (unsigned long) addr;
885 unsigned long end = (unsigned long) addr + length;
886
887 /* For swizzling simply ensure that we always flush both
888 * channels. Lame, but simple and it works. Swizzled
889 * pwrite/pread is far from a hotpath - current userspace
890 * doesn't use it at all. */
891 start = round_down(start, 128);
892 end = round_up(end, 128);
893
894 drm_clflush_virt_range((void *)start, end - start);
895 } else {
896 drm_clflush_virt_range(addr, length);
897 }
898
899}
900
Daniel Vetterd174bd62012-03-25 19:47:40 +0200901/* Only difference to the fast-path function is that this can handle bit17
902 * and uses non-atomic copy and kmap functions. */
903static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100904shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200905 char __user *user_data,
906 bool page_do_bit17_swizzling, bool needs_clflush)
907{
908 char *vaddr;
909 int ret;
910
911 vaddr = kmap(page);
912 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100913 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200914 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200915
916 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100917 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200918 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100919 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200920 kunmap(page);
921
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100922 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200923}
924
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100925static int
926shmem_pread(struct page *page, int offset, int length, char __user *user_data,
927 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530928{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100929 int ret;
930
931 ret = -ENODEV;
932 if (!page_do_bit17_swizzling) {
933 char *vaddr = kmap_atomic(page);
934
935 if (needs_clflush)
936 drm_clflush_virt_range(vaddr + offset, length);
937 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
938 kunmap_atomic(vaddr);
939 }
940 if (ret == 0)
941 return 0;
942
943 return shmem_pread_slow(page, offset, length, user_data,
944 page_do_bit17_swizzling, needs_clflush);
945}
946
947static int
948i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
949 struct drm_i915_gem_pread *args)
950{
951 char __user *user_data;
952 u64 remain;
953 unsigned int obj_do_bit17_swizzling;
954 unsigned int needs_clflush;
955 unsigned int idx, offset;
956 int ret;
957
958 obj_do_bit17_swizzling = 0;
959 if (i915_gem_object_needs_bit17_swizzle(obj))
960 obj_do_bit17_swizzling = BIT(17);
961
962 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
963 if (ret)
964 return ret;
965
966 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
967 mutex_unlock(&obj->base.dev->struct_mutex);
968 if (ret)
969 return ret;
970
971 remain = args->size;
972 user_data = u64_to_user_ptr(args->data_ptr);
973 offset = offset_in_page(args->offset);
974 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
975 struct page *page = i915_gem_object_get_page(obj, idx);
976 int length;
977
978 length = remain;
979 if (offset + length > PAGE_SIZE)
980 length = PAGE_SIZE - offset;
981
982 ret = shmem_pread(page, offset, length, user_data,
983 page_to_phys(page) & obj_do_bit17_swizzling,
984 needs_clflush);
985 if (ret)
986 break;
987
988 remain -= length;
989 user_data += length;
990 offset = 0;
991 }
992
993 i915_gem_obj_finish_shmem_access(obj);
994 return ret;
995}
996
997static inline bool
998gtt_user_read(struct io_mapping *mapping,
999 loff_t base, int offset,
1000 char __user *user_data, int length)
1001{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301002 void *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001003 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301004
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301005 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001006 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1007 unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1008 io_mapping_unmap_atomic(vaddr);
1009 if (unwritten) {
1010 vaddr = (void __force *)
1011 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1012 unwritten = copy_to_user(user_data, vaddr + offset, length);
1013 io_mapping_unmap(vaddr);
1014 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301015 return unwritten;
1016}
1017
1018static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001019i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1020 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301021{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001022 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1023 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301024 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001025 struct i915_vma *vma;
1026 void __user *user_data;
1027 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301028 int ret;
1029
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001030 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1031 if (ret)
1032 return ret;
1033
1034 intel_runtime_pm_get(i915);
1035 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1036 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001037 if (!IS_ERR(vma)) {
1038 node.start = i915_ggtt_offset(vma);
1039 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001040 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001041 if (ret) {
1042 i915_vma_unpin(vma);
1043 vma = ERR_PTR(ret);
1044 }
1045 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001046 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001047 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301048 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001049 goto out_unlock;
1050 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301051 }
1052
1053 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1054 if (ret)
1055 goto out_unpin;
1056
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001057 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301058
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001059 user_data = u64_to_user_ptr(args->data_ptr);
1060 remain = args->size;
1061 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301062
1063 while (remain > 0) {
1064 /* Operation in this page
1065 *
1066 * page_base = page offset within aperture
1067 * page_offset = offset within page
1068 * page_length = bytes to copy for this page
1069 */
1070 u32 page_base = node.start;
1071 unsigned page_offset = offset_in_page(offset);
1072 unsigned page_length = PAGE_SIZE - page_offset;
1073 page_length = remain < page_length ? remain : page_length;
1074 if (node.allocated) {
1075 wmb();
1076 ggtt->base.insert_page(&ggtt->base,
1077 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001078 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301079 wmb();
1080 } else {
1081 page_base += offset & PAGE_MASK;
1082 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001083
1084 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1085 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301086 ret = -EFAULT;
1087 break;
1088 }
1089
1090 remain -= page_length;
1091 user_data += page_length;
1092 offset += page_length;
1093 }
1094
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001095 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301096out_unpin:
1097 if (node.allocated) {
1098 wmb();
1099 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001100 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301101 remove_mappable_node(&node);
1102 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001103 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301104 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001105out_unlock:
1106 intel_runtime_pm_put(i915);
1107 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001108
Eric Anholteb014592009-03-10 11:44:52 -07001109 return ret;
1110}
1111
Eric Anholt673a3942008-07-30 12:06:12 -07001112/**
1113 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001114 * @dev: drm device pointer
1115 * @data: ioctl data blob
1116 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001117 *
1118 * On error, the contents of *data are undefined.
1119 */
1120int
1121i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001122 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001123{
1124 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001125 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001126 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001127
Chris Wilson51311d02010-11-17 09:10:42 +00001128 if (args->size == 0)
1129 return 0;
1130
1131 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001132 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001133 args->size))
1134 return -EFAULT;
1135
Chris Wilson03ac0642016-07-20 13:31:51 +01001136 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001137 if (!obj)
1138 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001139
Chris Wilson7dcd2492010-09-26 20:21:44 +01001140 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001141 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001142 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001143 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001144 }
1145
Chris Wilsondb53a302011-02-03 11:57:46 +00001146 trace_i915_gem_object_pread(obj, args->offset, args->size);
1147
Chris Wilsone95433c2016-10-28 13:58:27 +01001148 ret = i915_gem_object_wait(obj,
1149 I915_WAIT_INTERRUPTIBLE,
1150 MAX_SCHEDULE_TIMEOUT,
1151 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001152 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001153 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001154
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001155 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001156 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001157 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001158
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001159 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001160 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001161 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301162
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001163 i915_gem_object_unpin_pages(obj);
1164out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001165 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001166 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001167}
1168
Keith Packard0839ccb2008-10-30 19:38:48 -07001169/* This is the fast write path which cannot handle
1170 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001171 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001172
Chris Wilsonfe115622016-10-28 13:58:40 +01001173static inline bool
1174ggtt_write(struct io_mapping *mapping,
1175 loff_t base, int offset,
1176 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001177{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001178 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001179 unsigned long unwritten;
1180
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001181 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonfe115622016-10-28 13:58:40 +01001182 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1183 unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001184 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001185 io_mapping_unmap_atomic(vaddr);
1186 if (unwritten) {
1187 vaddr = (void __force *)
1188 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1189 unwritten = copy_from_user(vaddr + offset, user_data, length);
1190 io_mapping_unmap(vaddr);
1191 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001192
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001193 return unwritten;
1194}
1195
Eric Anholt3de09aa2009-03-09 09:42:23 -07001196/**
1197 * This is the fast pwrite path, where we copy the data directly from the
1198 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001199 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001200 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001201 */
Eric Anholt673a3942008-07-30 12:06:12 -07001202static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001203i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1204 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001205{
Chris Wilsonfe115622016-10-28 13:58:40 +01001206 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301207 struct i915_ggtt *ggtt = &i915->ggtt;
1208 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001209 struct i915_vma *vma;
1210 u64 remain, offset;
1211 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301212 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301213
Chris Wilsonfe115622016-10-28 13:58:40 +01001214 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1215 if (ret)
1216 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001217
Chris Wilson9c870d02016-10-24 13:42:15 +01001218 intel_runtime_pm_get(i915);
Chris Wilson058d88c2016-08-15 10:49:06 +01001219 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsonde895082016-08-04 16:32:34 +01001220 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001221 if (!IS_ERR(vma)) {
1222 node.start = i915_ggtt_offset(vma);
1223 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001224 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001225 if (ret) {
1226 i915_vma_unpin(vma);
1227 vma = ERR_PTR(ret);
1228 }
1229 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001230 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001231 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301232 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001233 goto out_unlock;
1234 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301235 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001236
1237 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1238 if (ret)
1239 goto out_unpin;
1240
Chris Wilsonfe115622016-10-28 13:58:40 +01001241 mutex_unlock(&i915->drm.struct_mutex);
1242
Chris Wilsonb19482d2016-08-18 17:16:43 +01001243 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001244
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301245 user_data = u64_to_user_ptr(args->data_ptr);
1246 offset = args->offset;
1247 remain = args->size;
1248 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001249 /* Operation in this page
1250 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001251 * page_base = page offset within aperture
1252 * page_offset = offset within page
1253 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001254 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301255 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001256 unsigned int page_offset = offset_in_page(offset);
1257 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301258 page_length = remain < page_length ? remain : page_length;
1259 if (node.allocated) {
1260 wmb(); /* flush the write before we modify the GGTT */
1261 ggtt->base.insert_page(&ggtt->base,
1262 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1263 node.start, I915_CACHE_NONE, 0);
1264 wmb(); /* flush modifications to the GGTT (insert_page) */
1265 } else {
1266 page_base += offset & PAGE_MASK;
1267 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001268 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001269 * source page isn't available. Return the error and we'll
1270 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301271 * If the object is non-shmem backed, we retry again with the
1272 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001273 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001274 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1275 user_data, page_length)) {
1276 ret = -EFAULT;
1277 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001278 }
Eric Anholt673a3942008-07-30 12:06:12 -07001279
Keith Packard0839ccb2008-10-30 19:38:48 -07001280 remain -= page_length;
1281 user_data += page_length;
1282 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001283 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001284 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001285
1286 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001287out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301288 if (node.allocated) {
1289 wmb();
1290 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001291 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301292 remove_mappable_node(&node);
1293 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001294 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301295 }
Chris Wilsonfe115622016-10-28 13:58:40 +01001296out_unlock:
Chris Wilson9c870d02016-10-24 13:42:15 +01001297 intel_runtime_pm_put(i915);
Chris Wilsonfe115622016-10-28 13:58:40 +01001298 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001299 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001300}
1301
Eric Anholt673a3942008-07-30 12:06:12 -07001302static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001303shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001304 char __user *user_data,
1305 bool page_do_bit17_swizzling,
1306 bool needs_clflush_before,
1307 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001308{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001309 char *vaddr;
1310 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001311
Daniel Vetterd174bd62012-03-25 19:47:40 +02001312 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001313 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001314 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001315 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001316 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001317 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1318 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001319 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001320 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001321 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001322 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001323 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001324 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001325
Chris Wilson755d2212012-09-04 21:02:55 +01001326 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001327}
1328
Chris Wilsonfe115622016-10-28 13:58:40 +01001329/* Per-page copy function for the shmem pwrite fastpath.
1330 * Flushes invalid cachelines before writing to the target if
1331 * needs_clflush_before is set and flushes out any written cachelines after
1332 * writing if needs_clflush is set.
1333 */
Eric Anholt40123c12009-03-09 13:42:30 -07001334static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001335shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1336 bool page_do_bit17_swizzling,
1337 bool needs_clflush_before,
1338 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001339{
Chris Wilsonfe115622016-10-28 13:58:40 +01001340 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001341
Chris Wilsonfe115622016-10-28 13:58:40 +01001342 ret = -ENODEV;
1343 if (!page_do_bit17_swizzling) {
1344 char *vaddr = kmap_atomic(page);
1345
1346 if (needs_clflush_before)
1347 drm_clflush_virt_range(vaddr + offset, len);
1348 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1349 if (needs_clflush_after)
1350 drm_clflush_virt_range(vaddr + offset, len);
1351
1352 kunmap_atomic(vaddr);
1353 }
1354 if (ret == 0)
1355 return ret;
1356
1357 return shmem_pwrite_slow(page, offset, len, user_data,
1358 page_do_bit17_swizzling,
1359 needs_clflush_before,
1360 needs_clflush_after);
1361}
1362
1363static int
1364i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1365 const struct drm_i915_gem_pwrite *args)
1366{
1367 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1368 void __user *user_data;
1369 u64 remain;
1370 unsigned int obj_do_bit17_swizzling;
1371 unsigned int partial_cacheline_write;
1372 unsigned int needs_clflush;
1373 unsigned int offset, idx;
1374 int ret;
1375
1376 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001377 if (ret)
1378 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001379
Chris Wilsonfe115622016-10-28 13:58:40 +01001380 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1381 mutex_unlock(&i915->drm.struct_mutex);
1382 if (ret)
1383 return ret;
1384
1385 obj_do_bit17_swizzling = 0;
1386 if (i915_gem_object_needs_bit17_swizzle(obj))
1387 obj_do_bit17_swizzling = BIT(17);
1388
1389 /* If we don't overwrite a cacheline completely we need to be
1390 * careful to have up-to-date data by first clflushing. Don't
1391 * overcomplicate things and flush the entire patch.
1392 */
1393 partial_cacheline_write = 0;
1394 if (needs_clflush & CLFLUSH_BEFORE)
1395 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1396
Chris Wilson43394c72016-08-18 17:16:47 +01001397 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001398 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001399 offset = offset_in_page(args->offset);
1400 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1401 struct page *page = i915_gem_object_get_page(obj, idx);
1402 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001403
Chris Wilsonfe115622016-10-28 13:58:40 +01001404 length = remain;
1405 if (offset + length > PAGE_SIZE)
1406 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001407
Chris Wilsonfe115622016-10-28 13:58:40 +01001408 ret = shmem_pwrite(page, offset, length, user_data,
1409 page_to_phys(page) & obj_do_bit17_swizzling,
1410 (offset | length) & partial_cacheline_write,
1411 needs_clflush & CLFLUSH_AFTER);
1412 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001413 break;
1414
Chris Wilsonfe115622016-10-28 13:58:40 +01001415 remain -= length;
1416 user_data += length;
1417 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001418 }
1419
Chris Wilsond59b21e2017-02-22 11:40:49 +00001420 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001421 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001422 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001423}
1424
1425/**
1426 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001427 * @dev: drm device
1428 * @data: ioctl data blob
1429 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001430 *
1431 * On error, the contents of the buffer that were to be modified are undefined.
1432 */
1433int
1434i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001435 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001436{
1437 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001438 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001439 int ret;
1440
1441 if (args->size == 0)
1442 return 0;
1443
1444 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001445 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001446 args->size))
1447 return -EFAULT;
1448
Chris Wilson03ac0642016-07-20 13:31:51 +01001449 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001450 if (!obj)
1451 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001452
Chris Wilson7dcd2492010-09-26 20:21:44 +01001453 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001454 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001455 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001456 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001457 }
1458
Chris Wilsondb53a302011-02-03 11:57:46 +00001459 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1460
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001461 ret = -ENODEV;
1462 if (obj->ops->pwrite)
1463 ret = obj->ops->pwrite(obj, args);
1464 if (ret != -ENODEV)
1465 goto err;
1466
Chris Wilsone95433c2016-10-28 13:58:27 +01001467 ret = i915_gem_object_wait(obj,
1468 I915_WAIT_INTERRUPTIBLE |
1469 I915_WAIT_ALL,
1470 MAX_SCHEDULE_TIMEOUT,
1471 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001472 if (ret)
1473 goto err;
1474
Chris Wilsonfe115622016-10-28 13:58:40 +01001475 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001476 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001477 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001478
Daniel Vetter935aaa62012-03-25 19:47:35 +02001479 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001480 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1481 * it would end up going through the fenced access, and we'll get
1482 * different detiling behavior between reading and writing.
1483 * pread/pwrite currently are reading and writing from the CPU
1484 * perspective, requiring manual detiling by the client.
1485 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001486 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001487 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001488 /* Note that the gtt paths might fail with non-page-backed user
1489 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001490 * textures). Fallback to the shmem path in that case.
1491 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001492 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001493
Chris Wilsond1054ee2016-07-16 18:42:36 +01001494 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001495 if (obj->phys_handle)
1496 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301497 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001498 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001499 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001500
Chris Wilsonfe115622016-10-28 13:58:40 +01001501 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001502err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001503 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001504 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001505}
1506
Chris Wilsond243ad82016-08-18 17:16:44 +01001507static inline enum fb_op_origin
Chris Wilsonaeecc962016-06-17 14:46:39 -03001508write_origin(struct drm_i915_gem_object *obj, unsigned domain)
1509{
Chris Wilson50349242016-08-18 17:17:04 +01001510 return (domain == I915_GEM_DOMAIN_GTT ?
1511 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001512}
1513
Chris Wilson40e62d52016-10-28 13:58:41 +01001514static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1515{
1516 struct drm_i915_private *i915;
1517 struct list_head *list;
1518 struct i915_vma *vma;
1519
1520 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1521 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001522 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001523
1524 if (i915_vma_is_active(vma))
1525 continue;
1526
1527 if (!drm_mm_node_allocated(&vma->node))
1528 continue;
1529
1530 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1531 }
1532
1533 i915 = to_i915(obj->base.dev);
1534 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Joonas Lahtinen56cea322016-11-02 12:16:04 +02001535 list_move_tail(&obj->global_link, list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001536}
1537
Eric Anholt673a3942008-07-30 12:06:12 -07001538/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001539 * Called when user space prepares to use an object with the CPU, either
1540 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001541 * @dev: drm device
1542 * @data: ioctl data blob
1543 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001544 */
1545int
1546i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001547 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001548{
1549 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001550 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001551 uint32_t read_domains = args->read_domains;
1552 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001553 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001554
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001555 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001556 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001557 return -EINVAL;
1558
1559 /* Having something in the write domain implies it's in the read
1560 * domain, and only that read domain. Enforce that in the request.
1561 */
1562 if (write_domain != 0 && read_domains != write_domain)
1563 return -EINVAL;
1564
Chris Wilson03ac0642016-07-20 13:31:51 +01001565 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001566 if (!obj)
1567 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001568
Chris Wilson3236f572012-08-24 09:35:09 +01001569 /* Try to flush the object off the GPU without holding the lock.
1570 * We will repeat the flush holding the lock in the normal manner
1571 * to catch cases where we are gazumped.
1572 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001573 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001574 I915_WAIT_INTERRUPTIBLE |
1575 (write_domain ? I915_WAIT_ALL : 0),
1576 MAX_SCHEDULE_TIMEOUT,
1577 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001578 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001579 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001580
Chris Wilson40e62d52016-10-28 13:58:41 +01001581 /* Flush and acquire obj->pages so that we are coherent through
1582 * direct access in memory with previous cached writes through
1583 * shmemfs and that our cache domain tracking remains valid.
1584 * For example, if the obj->filp was moved to swap without us
1585 * being notified and releasing the pages, we would mistakenly
1586 * continue to assume that the obj remained out of the CPU cached
1587 * domain.
1588 */
1589 err = i915_gem_object_pin_pages(obj);
1590 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001591 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001592
1593 err = i915_mutex_lock_interruptible(dev);
1594 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001595 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001596
Chris Wilson43566de2015-01-02 16:29:29 +05301597 if (read_domains & I915_GEM_DOMAIN_GTT)
Chris Wilson40e62d52016-10-28 13:58:41 +01001598 err = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301599 else
Chris Wilson40e62d52016-10-28 13:58:41 +01001600 err = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1601
1602 /* And bump the LRU for this access */
1603 i915_gem_object_bump_inactive_ggtt(obj);
1604
1605 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001606
Daniel Vetter031b6982015-06-26 19:35:16 +02001607 if (write_domain != 0)
Chris Wilsonaeecc962016-06-17 14:46:39 -03001608 intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001609
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001610out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001611 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001612out:
1613 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001614 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001615}
1616
1617/**
1618 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001619 * @dev: drm device
1620 * @data: ioctl data blob
1621 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001622 */
1623int
1624i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001625 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001626{
1627 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001628 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001629
Chris Wilson03ac0642016-07-20 13:31:51 +01001630 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001631 if (!obj)
1632 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001633
Eric Anholt673a3942008-07-30 12:06:12 -07001634 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001635 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001636 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001637
1638 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001639}
1640
1641/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001642 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1643 * it is mapped to.
1644 * @dev: drm device
1645 * @data: ioctl data blob
1646 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001647 *
1648 * While the mapping holds a reference on the contents of the object, it doesn't
1649 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001650 *
1651 * IMPORTANT:
1652 *
1653 * DRM driver writers who look a this function as an example for how to do GEM
1654 * mmap support, please don't implement mmap support like here. The modern way
1655 * to implement DRM mmap support is with an mmap offset ioctl (like
1656 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1657 * That way debug tooling like valgrind will understand what's going on, hiding
1658 * the mmap call in a driver private ioctl will break that. The i915 driver only
1659 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001660 */
1661int
1662i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001663 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001664{
1665 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001666 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001667 unsigned long addr;
1668
Akash Goel1816f922015-01-02 16:29:30 +05301669 if (args->flags & ~(I915_MMAP_WC))
1670 return -EINVAL;
1671
Borislav Petkov568a58e2016-03-29 17:42:01 +02001672 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301673 return -ENODEV;
1674
Chris Wilson03ac0642016-07-20 13:31:51 +01001675 obj = i915_gem_object_lookup(file, args->handle);
1676 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001677 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001678
Daniel Vetter1286ff72012-05-10 15:25:09 +02001679 /* prime objects have no backing filp to GEM mmap
1680 * pages from.
1681 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001682 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001683 i915_gem_object_put(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001684 return -EINVAL;
1685 }
1686
Chris Wilson03ac0642016-07-20 13:31:51 +01001687 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001688 PROT_READ | PROT_WRITE, MAP_SHARED,
1689 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301690 if (args->flags & I915_MMAP_WC) {
1691 struct mm_struct *mm = current->mm;
1692 struct vm_area_struct *vma;
1693
Michal Hocko80a89a52016-05-23 16:26:11 -07001694 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001695 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001696 return -EINTR;
1697 }
Akash Goel1816f922015-01-02 16:29:30 +05301698 vma = find_vma(mm, addr);
1699 if (vma)
1700 vma->vm_page_prot =
1701 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1702 else
1703 addr = -ENOMEM;
1704 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001705
1706 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001707 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301708 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001709 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001710 if (IS_ERR((void *)addr))
1711 return addr;
1712
1713 args->addr_ptr = (uint64_t) addr;
1714
1715 return 0;
1716}
1717
Chris Wilson03af84f2016-08-18 17:17:01 +01001718static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1719{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001720 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001721}
1722
Jesse Barnesde151cf2008-11-12 10:03:55 -08001723/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001724 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1725 *
1726 * A history of the GTT mmap interface:
1727 *
1728 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1729 * aligned and suitable for fencing, and still fit into the available
1730 * mappable space left by the pinned display objects. A classic problem
1731 * we called the page-fault-of-doom where we would ping-pong between
1732 * two objects that could not fit inside the GTT and so the memcpy
1733 * would page one object in at the expense of the other between every
1734 * single byte.
1735 *
1736 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1737 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1738 * object is too large for the available space (or simply too large
1739 * for the mappable aperture!), a view is created instead and faulted
1740 * into userspace. (This view is aligned and sized appropriately for
1741 * fenced access.)
1742 *
1743 * Restrictions:
1744 *
1745 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1746 * hangs on some architectures, corruption on others. An attempt to service
1747 * a GTT page fault from a snoopable object will generate a SIGBUS.
1748 *
1749 * * the object must be able to fit into RAM (physical memory, though no
1750 * limited to the mappable aperture).
1751 *
1752 *
1753 * Caveats:
1754 *
1755 * * a new GTT page fault will synchronize rendering from the GPU and flush
1756 * all data to system memory. Subsequent access will not be synchronized.
1757 *
1758 * * all mappings are revoked on runtime device suspend.
1759 *
1760 * * there are only 8, 16 or 32 fence registers to share between all users
1761 * (older machines require fence register for display and blitter access
1762 * as well). Contention of the fence registers will cause the previous users
1763 * to be unmapped and any new access will generate new page faults.
1764 *
1765 * * running out of memory while servicing a fault may generate a SIGBUS,
1766 * rather than the expected SIGSEGV.
1767 */
1768int i915_gem_mmap_gtt_version(void)
1769{
1770 return 1;
1771}
1772
Chris Wilson2d4281b2017-01-10 09:56:32 +00001773static inline struct i915_ggtt_view
1774compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001775 pgoff_t page_offset,
1776 unsigned int chunk)
1777{
1778 struct i915_ggtt_view view;
1779
1780 if (i915_gem_object_is_tiled(obj))
1781 chunk = roundup(chunk, tile_row_pages(obj));
1782
Chris Wilson2d4281b2017-01-10 09:56:32 +00001783 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001784 view.partial.offset = rounddown(page_offset, chunk);
1785 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001786 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001787 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001788
1789 /* If the partial covers the entire object, just create a normal VMA. */
1790 if (chunk >= obj->base.size >> PAGE_SHIFT)
1791 view.type = I915_GGTT_VIEW_NORMAL;
1792
1793 return view;
1794}
1795
Chris Wilson4cc69072016-08-25 19:05:19 +01001796/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001797 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001798 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001799 *
1800 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1801 * from userspace. The fault handler takes care of binding the object to
1802 * the GTT (if needed), allocating and programming a fence register (again,
1803 * only if needed based on whether the old reg is still valid or the object
1804 * is tiled) and inserting a new PTE into the faulting process.
1805 *
1806 * Note that the faulting process may involve evicting existing objects
1807 * from the GTT and/or fence registers to make room. So performance may
1808 * suffer if the GTT working set is large or there are few fence registers
1809 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001810 *
1811 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1812 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001813 */
Dave Jiang11bac802017-02-24 14:56:41 -08001814int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001815{
Chris Wilson03af84f2016-08-18 17:17:01 +01001816#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001817 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001818 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001819 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001820 struct drm_i915_private *dev_priv = to_i915(dev);
1821 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001822 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001823 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001824 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001825 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001826 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001827
Jesse Barnesde151cf2008-11-12 10:03:55 -08001828 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001829 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001830
Chris Wilsondb53a302011-02-03 11:57:46 +00001831 trace_i915_gem_object_fault(obj, page_offset, true, write);
1832
Chris Wilson6e4930f2014-02-07 18:37:06 -02001833 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001834 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001835 * repeat the flush holding the lock in the normal manner to catch cases
1836 * where we are gazumped.
1837 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001838 ret = i915_gem_object_wait(obj,
1839 I915_WAIT_INTERRUPTIBLE,
1840 MAX_SCHEDULE_TIMEOUT,
1841 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001842 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001843 goto err;
1844
Chris Wilson40e62d52016-10-28 13:58:41 +01001845 ret = i915_gem_object_pin_pages(obj);
1846 if (ret)
1847 goto err;
1848
Chris Wilsonb8f90962016-08-05 10:14:07 +01001849 intel_runtime_pm_get(dev_priv);
1850
1851 ret = i915_mutex_lock_interruptible(dev);
1852 if (ret)
1853 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001854
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001855 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001856 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001857 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001858 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001859 }
1860
Chris Wilson82118872016-08-18 17:17:05 +01001861 /* If the object is smaller than a couple of partial vma, it is
1862 * not worth only creating a single partial vma - we may as well
1863 * clear enough space for the full object.
1864 */
1865 flags = PIN_MAPPABLE;
1866 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1867 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1868
Chris Wilsona61007a2016-08-18 17:17:02 +01001869 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001870 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001871 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001872 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001873 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001874 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001875
Chris Wilson50349242016-08-18 17:17:04 +01001876 /* Userspace is now writing through an untracked VMA, abandon
1877 * all hope that the hardware is able to track future writes.
1878 */
1879 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1880
Chris Wilsona61007a2016-08-18 17:17:02 +01001881 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1882 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001883 if (IS_ERR(vma)) {
1884 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001885 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001886 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001887
Chris Wilsonc9839302012-11-20 10:45:17 +00001888 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1889 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001890 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001891
Chris Wilson49ef5292016-08-18 17:17:00 +01001892 ret = i915_vma_get_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001893 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001894 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001895
Chris Wilson275f0392016-10-24 13:42:14 +01001896 /* Mark as being mmapped into userspace for later revocation */
Chris Wilson9c870d02016-10-24 13:42:15 +01001897 assert_rpm_wakelock_held(dev_priv);
Chris Wilson275f0392016-10-24 13:42:14 +01001898 if (list_empty(&obj->userfault_link))
1899 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
Chris Wilson275f0392016-10-24 13:42:14 +01001900
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001901 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001902 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001903 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001904 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1905 min_t(u64, vma->size, area->vm_end - area->vm_start),
1906 &ggtt->mappable);
Chris Wilsona61007a2016-08-18 17:17:02 +01001907
Chris Wilsonb8f90962016-08-05 10:14:07 +01001908err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001909 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001910err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001911 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001912err_rpm:
1913 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001914 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001915err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001916 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001917 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001918 /*
1919 * We eat errors when the gpu is terminally wedged to avoid
1920 * userspace unduly crashing (gl has no provisions for mmaps to
1921 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1922 * and so needs to be reported.
1923 */
1924 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001925 ret = VM_FAULT_SIGBUS;
1926 break;
1927 }
Chris Wilson045e7692010-11-07 09:18:22 +00001928 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001929 /*
1930 * EAGAIN means the gpu is hung and we'll wait for the error
1931 * handler to reset everything when re-faulting in
1932 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001933 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001934 case 0:
1935 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001936 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001937 case -EBUSY:
1938 /*
1939 * EBUSY is ok: this just means that another thread
1940 * already did the job.
1941 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001942 ret = VM_FAULT_NOPAGE;
1943 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001944 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001945 ret = VM_FAULT_OOM;
1946 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001947 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001948 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001949 ret = VM_FAULT_SIGBUS;
1950 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001951 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001952 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001953 ret = VM_FAULT_SIGBUS;
1954 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001955 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001956 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001957}
1958
1959/**
Chris Wilson901782b2009-07-10 08:18:50 +01001960 * i915_gem_release_mmap - remove physical page mappings
1961 * @obj: obj in question
1962 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001963 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001964 * relinquish ownership of the pages back to the system.
1965 *
1966 * It is vital that we remove the page mapping if we have mapped a tiled
1967 * object through the GTT and then lose the fence register due to
1968 * resource pressure. Similarly if the object has been moved out of the
1969 * aperture, than pages mapped into userspace must be revoked. Removing the
1970 * mapping will then trigger a page fault on the next user access, allowing
1971 * fixup by i915_gem_fault().
1972 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001973void
Chris Wilson05394f32010-11-08 19:18:58 +00001974i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001975{
Chris Wilson275f0392016-10-24 13:42:14 +01001976 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01001977
Chris Wilson349f2cc2016-04-13 17:35:12 +01001978 /* Serialisation between user GTT access and our code depends upon
1979 * revoking the CPU's PTE whilst the mutex is held. The next user
1980 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01001981 *
1982 * Note that RPM complicates somewhat by adding an additional
1983 * requirement that operations to the GGTT be made holding the RPM
1984 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01001985 */
Chris Wilson275f0392016-10-24 13:42:14 +01001986 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01001987 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001988
Chris Wilson3594a3e2016-10-24 13:42:16 +01001989 if (list_empty(&obj->userfault_link))
Chris Wilson9c870d02016-10-24 13:42:15 +01001990 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01001991
Chris Wilson3594a3e2016-10-24 13:42:16 +01001992 list_del_init(&obj->userfault_link);
David Herrmann6796cb12014-01-03 14:24:19 +01001993 drm_vma_node_unmap(&obj->base.vma_node,
1994 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001995
1996 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1997 * memory transactions from userspace before we return. The TLB
1998 * flushing implied above by changing the PTE above *should* be
1999 * sufficient, an extra barrier here just provides us with a bit
2000 * of paranoid documentation about our requirement to serialise
2001 * memory writes before touching registers / GSM.
2002 */
2003 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002004
2005out:
2006 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002007}
2008
Chris Wilson7c108fd2016-10-24 13:42:18 +01002009void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002010{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002011 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002012 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002013
Chris Wilson3594a3e2016-10-24 13:42:16 +01002014 /*
2015 * Only called during RPM suspend. All users of the userfault_list
2016 * must be holding an RPM wakeref to ensure that this can not
2017 * run concurrently with themselves (and use the struct_mutex for
2018 * protection between themselves).
2019 */
2020
2021 list_for_each_entry_safe(obj, on,
2022 &dev_priv->mm.userfault_list, userfault_link) {
Chris Wilson275f0392016-10-24 13:42:14 +01002023 list_del_init(&obj->userfault_link);
Chris Wilson275f0392016-10-24 13:42:14 +01002024 drm_vma_node_unmap(&obj->base.vma_node,
2025 obj->base.dev->anon_inode->i_mapping);
Chris Wilson275f0392016-10-24 13:42:14 +01002026 }
Chris Wilson7c108fd2016-10-24 13:42:18 +01002027
2028 /* The fence will be lost when the device powers down. If any were
2029 * in use by hardware (i.e. they are pinned), we should not be powering
2030 * down! All other fences will be reacquired by the user upon waking.
2031 */
2032 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2033 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2034
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002035 /* Ideally we want to assert that the fence register is not
2036 * live at this point (i.e. that no piece of code will be
2037 * trying to write through fence + GTT, as that both violates
2038 * our tracking of activity and associated locking/barriers,
2039 * but also is illegal given that the hw is powered down).
2040 *
2041 * Previously we used reg->pin_count as a "liveness" indicator.
2042 * That is not sufficient, and we need a more fine-grained
2043 * tool if we want to have a sanity check here.
2044 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002045
2046 if (!reg->vma)
2047 continue;
2048
2049 GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
2050 reg->dirty = true;
2051 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002052}
2053
Chris Wilsond8cb5082012-08-11 15:41:03 +01002054static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2055{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002056 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002057 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002058
Chris Wilsonf3f61842016-08-05 10:14:14 +01002059 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002060 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002061 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002062
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002063 /* Attempt to reap some mmap space from dead objects */
2064 do {
2065 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2066 if (err)
2067 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002068
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002069 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002070 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002071 if (!err)
2072 break;
2073
2074 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002075
Chris Wilsonf3f61842016-08-05 10:14:14 +01002076 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002077}
2078
2079static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2080{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002081 drm_gem_free_mmap_offset(&obj->base);
2082}
2083
Dave Airlieda6b51d2014-12-24 13:11:17 +10002084int
Dave Airlieff72145b2011-02-07 12:16:14 +10002085i915_gem_mmap_gtt(struct drm_file *file,
2086 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002087 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002088 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002089{
Chris Wilson05394f32010-11-08 19:18:58 +00002090 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002091 int ret;
2092
Chris Wilson03ac0642016-07-20 13:31:51 +01002093 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002094 if (!obj)
2095 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002096
Chris Wilsond8cb5082012-08-11 15:41:03 +01002097 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002098 if (ret == 0)
2099 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002100
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002101 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002102 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002103}
2104
Dave Airlieff72145b2011-02-07 12:16:14 +10002105/**
2106 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2107 * @dev: DRM device
2108 * @data: GTT mapping ioctl data
2109 * @file: GEM object info
2110 *
2111 * Simply returns the fake offset to userspace so it can mmap it.
2112 * The mmap call will end up in drm_gem_mmap(), which will set things
2113 * up so we can get faults in the handler above.
2114 *
2115 * The fault handler will take care of binding the object into the GTT
2116 * (since it may have been evicted to make room for something), allocating
2117 * a fence register, and mapping the appropriate aperture address into
2118 * userspace.
2119 */
2120int
2121i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2122 struct drm_file *file)
2123{
2124 struct drm_i915_gem_mmap_gtt *args = data;
2125
Dave Airlieda6b51d2014-12-24 13:11:17 +10002126 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002127}
2128
Daniel Vetter225067e2012-08-20 10:23:20 +02002129/* Immediately discard the backing storage */
2130static void
2131i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002132{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002133 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002134
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002135 if (obj->base.filp == NULL)
2136 return;
2137
Daniel Vetter225067e2012-08-20 10:23:20 +02002138 /* Our goal here is to return as much of the memory as
2139 * is possible back to the system as we are called from OOM.
2140 * To do this we must instruct the shmfs to drop all of its
2141 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002142 */
Chris Wilson55372522014-03-25 13:23:06 +00002143 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002144 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002145 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002146}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002147
Chris Wilson55372522014-03-25 13:23:06 +00002148/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002149void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002150{
Chris Wilson55372522014-03-25 13:23:06 +00002151 struct address_space *mapping;
2152
Chris Wilson1233e2d2016-10-28 13:58:37 +01002153 lockdep_assert_held(&obj->mm.lock);
2154 GEM_BUG_ON(obj->mm.pages);
2155
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002156 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002157 case I915_MADV_DONTNEED:
2158 i915_gem_object_truncate(obj);
2159 case __I915_MADV_PURGED:
2160 return;
2161 }
2162
2163 if (obj->base.filp == NULL)
2164 return;
2165
Al Viro93c76a32015-12-04 23:45:44 -05002166 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002167 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002168}
2169
Chris Wilson5cdf5882010-09-27 15:51:07 +01002170static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002171i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2172 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002173{
Dave Gordon85d12252016-05-20 11:54:06 +01002174 struct sgt_iter sgt_iter;
2175 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002176
Chris Wilsone5facdf2016-12-23 14:57:57 +00002177 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002178
Chris Wilson03ac84f2016-10-28 13:58:36 +01002179 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002180
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002181 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002182 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002183
Chris Wilson03ac84f2016-10-28 13:58:36 +01002184 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002185 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002186 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002187
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002188 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002189 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002190
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002191 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002192 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002193 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002194
Chris Wilson03ac84f2016-10-28 13:58:36 +01002195 sg_free_table(pages);
2196 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002197}
2198
Chris Wilson96d77632016-10-28 13:58:33 +01002199static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2200{
2201 struct radix_tree_iter iter;
2202 void **slot;
2203
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002204 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2205 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilson96d77632016-10-28 13:58:33 +01002206}
2207
Chris Wilson548625e2016-11-01 12:11:34 +00002208void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2209 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002210{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002211 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002212
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002213 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002214 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002215
Chris Wilson15717de2016-08-04 07:52:26 +01002216 GEM_BUG_ON(obj->bind_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002217 if (!READ_ONCE(obj->mm.pages))
2218 return;
2219
2220 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002221 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002222 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2223 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002224
Chris Wilsona2165e32012-12-03 11:49:00 +00002225 /* ->put_pages might need to allocate memory for the bit17 swizzle
2226 * array, hence protect them from being reaped by removing them from gtt
2227 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002228 pages = fetch_and_zero(&obj->mm.pages);
2229 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002230
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002231 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002232 void *ptr;
2233
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002234 ptr = ptr_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002235 if (is_vmalloc_addr(ptr))
2236 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002237 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002238 kunmap(kmap_to_page(ptr));
2239
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002240 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002241 }
2242
Chris Wilson96d77632016-10-28 13:58:33 +01002243 __i915_gem_object_reset_page_iter(obj);
2244
Chris Wilson4e5462e2017-03-07 13:20:31 +00002245 if (!IS_ERR(pages))
2246 obj->ops->put_pages(obj, pages);
2247
Chris Wilson1233e2d2016-10-28 13:58:37 +01002248unlock:
2249 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002250}
2251
Chris Wilson935a2f72017-02-13 17:15:13 +00002252static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002253{
2254 struct sg_table new_st;
2255 struct scatterlist *sg, *new_sg;
2256 unsigned int i;
2257
2258 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002259 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002260
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002261 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002262 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002263
2264 new_sg = new_st.sgl;
2265 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2266 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2267 /* called before being DMA mapped, no need to copy sg->dma_* */
2268 new_sg = sg_next(new_sg);
2269 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002270 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002271
2272 sg_free_table(orig_st);
2273
2274 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002275 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002276}
2277
Chris Wilson03ac84f2016-10-28 13:58:36 +01002278static struct sg_table *
Chris Wilson6c085a72012-08-20 11:40:46 +02002279i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002280{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002281 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002282 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2283 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002284 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002285 struct sg_table *st;
2286 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002287 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002288 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002289 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson4ff340f02016-10-18 13:02:50 +01002290 unsigned int max_segment;
Imre Deake2273302015-07-09 12:59:05 +03002291 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002292 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002293
Chris Wilson6c085a72012-08-20 11:40:46 +02002294 /* Assert that the object is not currently in any GPU domain. As it
2295 * wasn't in the GTT, there shouldn't be any way it could have been in
2296 * a GPU cache
2297 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002298 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2299 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002300
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -05002301 max_segment = swiotlb_max_segment();
Chris Wilson871dfbd2016-10-11 09:20:21 +01002302 if (!max_segment)
Chris Wilson4ff340f02016-10-18 13:02:50 +01002303 max_segment = rounddown(UINT_MAX, PAGE_SIZE);
Chris Wilson871dfbd2016-10-11 09:20:21 +01002304
Chris Wilson9da3da62012-06-01 15:20:22 +01002305 st = kmalloc(sizeof(*st), GFP_KERNEL);
2306 if (st == NULL)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002307 return ERR_PTR(-ENOMEM);
Eric Anholt673a3942008-07-30 12:06:12 -07002308
Chris Wilsond766ef52016-12-19 12:43:45 +00002309rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002310 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002311 kfree(st);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002312 return ERR_PTR(-ENOMEM);
Chris Wilson9da3da62012-06-01 15:20:22 +01002313 }
2314
2315 /* Get the list of pages out of our struct file. They'll be pinned
2316 * at this point until we release them.
2317 *
2318 * Fail silently without starting the shrinker
2319 */
Al Viro93c76a32015-12-04 23:45:44 -05002320 mapping = obj->base.filp->f_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002321 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002322 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002323 sg = st->sgl;
2324 st->nents = 0;
2325 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002326 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2327 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002328 i915_gem_shrink(dev_priv,
2329 page_count,
2330 I915_SHRINK_BOUND |
2331 I915_SHRINK_UNBOUND |
2332 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002333 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2334 }
2335 if (IS_ERR(page)) {
2336 /* We've tried hard to allocate the memory by reaping
2337 * our own buffer, now let the real VM do its job and
2338 * go down in flames if truly OOM.
2339 */
David Herrmannf461d1b2014-05-25 14:34:10 +02002340 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002341 if (IS_ERR(page)) {
2342 ret = PTR_ERR(page);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002343 goto err_sg;
Imre Deake2273302015-07-09 12:59:05 +03002344 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002345 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002346 if (!i ||
2347 sg->length >= max_segment ||
2348 page_to_pfn(page) != last_pfn + 1) {
Imre Deak90797e62013-02-18 19:28:03 +02002349 if (i)
2350 sg = sg_next(sg);
2351 st->nents++;
2352 sg_set_page(sg, page, PAGE_SIZE, 0);
2353 } else {
2354 sg->length += PAGE_SIZE;
2355 }
2356 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002357
2358 /* Check that the i965g/gm workaround works. */
2359 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002360 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002361 if (sg) /* loop terminated early; short sg table */
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002362 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002363
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002364 /* Trim unused sg entries to avoid wasting memory. */
2365 i915_sg_trim(st);
2366
Chris Wilson03ac84f2016-10-28 13:58:36 +01002367 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002368 if (ret) {
2369 /* DMA remapping failed? One possible cause is that
2370 * it could not reserve enough large entries, asking
2371 * for PAGE_SIZE chunks instead may be helpful.
2372 */
2373 if (max_segment > PAGE_SIZE) {
2374 for_each_sgt_page(page, sgt_iter, st)
2375 put_page(page);
2376 sg_free_table(st);
2377
2378 max_segment = PAGE_SIZE;
2379 goto rebuild_st;
2380 } else {
2381 dev_warn(&dev_priv->drm.pdev->dev,
2382 "Failed to DMA remap %lu pages\n",
2383 page_count);
2384 goto err_pages;
2385 }
2386 }
Imre Deake2273302015-07-09 12:59:05 +03002387
Eric Anholt673a3942008-07-30 12:06:12 -07002388 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002389 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002390
Chris Wilson03ac84f2016-10-28 13:58:36 +01002391 return st;
Eric Anholt673a3942008-07-30 12:06:12 -07002392
Chris Wilsonb17993b2016-11-14 11:29:30 +00002393err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002394 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002395err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002396 for_each_sgt_page(page, sgt_iter, st)
2397 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002398 sg_free_table(st);
2399 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002400
2401 /* shmemfs first checks if there is enough memory to allocate the page
2402 * and reports ENOSPC should there be insufficient, along with the usual
2403 * ENOMEM for a genuine allocation failure.
2404 *
2405 * We use ENOSPC in our driver to mean that we have run out of aperture
2406 * space and so want to translate the error from shmemfs back to our
2407 * usual understanding of ENOMEM.
2408 */
Imre Deake2273302015-07-09 12:59:05 +03002409 if (ret == -ENOSPC)
2410 ret = -ENOMEM;
2411
Chris Wilson03ac84f2016-10-28 13:58:36 +01002412 return ERR_PTR(ret);
2413}
2414
2415void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
2416 struct sg_table *pages)
2417{
Chris Wilson1233e2d2016-10-28 13:58:37 +01002418 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002419
2420 obj->mm.get_page.sg_pos = pages->sgl;
2421 obj->mm.get_page.sg_idx = 0;
2422
2423 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002424
2425 if (i915_gem_object_is_tiled(obj) &&
2426 to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
2427 GEM_BUG_ON(obj->mm.quirked);
2428 __i915_gem_object_pin_pages(obj);
2429 obj->mm.quirked = true;
2430 }
Chris Wilson03ac84f2016-10-28 13:58:36 +01002431}
2432
2433static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2434{
2435 struct sg_table *pages;
2436
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002437 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2438
Chris Wilson03ac84f2016-10-28 13:58:36 +01002439 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2440 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2441 return -EFAULT;
2442 }
2443
2444 pages = obj->ops->get_pages(obj);
2445 if (unlikely(IS_ERR(pages)))
2446 return PTR_ERR(pages);
2447
2448 __i915_gem_object_set_pages(obj, pages);
2449 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002450}
2451
Chris Wilson37e680a2012-06-07 15:38:42 +01002452/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002453 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002454 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002455 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002456 * either as a result of memory pressure (reaping pages under the shrinker)
2457 * or as the object is itself released.
2458 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002459int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002460{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002461 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002462
Chris Wilson1233e2d2016-10-28 13:58:37 +01002463 err = mutex_lock_interruptible(&obj->mm.lock);
2464 if (err)
2465 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002466
Chris Wilson4e5462e2017-03-07 13:20:31 +00002467 if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002468 err = ____i915_gem_object_get_pages(obj);
2469 if (err)
2470 goto unlock;
2471
2472 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002473 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002474 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002475
Chris Wilson1233e2d2016-10-28 13:58:37 +01002476unlock:
2477 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002478 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002479}
2480
Dave Gordondd6034c2016-05-20 11:54:04 +01002481/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002482static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2483 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002484{
2485 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002486 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002487 struct sgt_iter sgt_iter;
2488 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002489 struct page *stack_pages[32];
2490 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002491 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002492 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002493 void *addr;
2494
2495 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002496 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002497 return kmap(sg_page(sgt->sgl));
2498
Dave Gordonb338fa42016-05-20 11:54:05 +01002499 if (n_pages > ARRAY_SIZE(stack_pages)) {
2500 /* Too big for stack -- allocate temporary array instead */
2501 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2502 if (!pages)
2503 return NULL;
2504 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002505
Dave Gordon85d12252016-05-20 11:54:06 +01002506 for_each_sgt_page(page, sgt_iter, sgt)
2507 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002508
2509 /* Check that we have the expected number of pages */
2510 GEM_BUG_ON(i != n_pages);
2511
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002512 switch (type) {
2513 case I915_MAP_WB:
2514 pgprot = PAGE_KERNEL;
2515 break;
2516 case I915_MAP_WC:
2517 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2518 break;
2519 }
2520 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002521
Dave Gordonb338fa42016-05-20 11:54:05 +01002522 if (pages != stack_pages)
2523 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002524
2525 return addr;
2526}
2527
2528/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002529void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2530 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002531{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002532 enum i915_map_type has_type;
2533 bool pinned;
2534 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002535 int ret;
2536
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002537 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002538
Chris Wilson1233e2d2016-10-28 13:58:37 +01002539 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002540 if (ret)
2541 return ERR_PTR(ret);
2542
Chris Wilson1233e2d2016-10-28 13:58:37 +01002543 pinned = true;
2544 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilson4e5462e2017-03-07 13:20:31 +00002545 if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002546 ret = ____i915_gem_object_get_pages(obj);
2547 if (ret)
2548 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002549
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002550 smp_mb__before_atomic();
2551 }
2552 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002553 pinned = false;
2554 }
2555 GEM_BUG_ON(!obj->mm.pages);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002556
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002557 ptr = ptr_unpack_bits(obj->mm.mapping, has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002558 if (ptr && has_type != type) {
2559 if (pinned) {
2560 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002561 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002562 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002563
2564 if (is_vmalloc_addr(ptr))
2565 vunmap(ptr);
2566 else
2567 kunmap(kmap_to_page(ptr));
2568
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002569 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002570 }
2571
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002572 if (!ptr) {
2573 ptr = i915_gem_object_map(obj, type);
2574 if (!ptr) {
2575 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002576 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002577 }
2578
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002579 obj->mm.mapping = ptr_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002580 }
2581
Chris Wilson1233e2d2016-10-28 13:58:37 +01002582out_unlock:
2583 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002584 return ptr;
2585
Chris Wilson1233e2d2016-10-28 13:58:37 +01002586err_unpin:
2587 atomic_dec(&obj->mm.pages_pin_count);
2588err_unlock:
2589 ptr = ERR_PTR(ret);
2590 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002591}
2592
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002593static int
2594i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2595 const struct drm_i915_gem_pwrite *arg)
2596{
2597 struct address_space *mapping = obj->base.filp->f_mapping;
2598 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2599 u64 remain, offset;
2600 unsigned int pg;
2601
2602 /* Before we instantiate/pin the backing store for our use, we
2603 * can prepopulate the shmemfs filp efficiently using a write into
2604 * the pagecache. We avoid the penalty of instantiating all the
2605 * pages, important if the user is just writing to a few and never
2606 * uses the object on the GPU, and using a direct write into shmemfs
2607 * allows it to avoid the cost of retrieving a page (either swapin
2608 * or clearing-before-use) before it is overwritten.
2609 */
2610 if (READ_ONCE(obj->mm.pages))
2611 return -ENODEV;
2612
2613 /* Before the pages are instantiated the object is treated as being
2614 * in the CPU domain. The pages will be clflushed as required before
2615 * use, and we can freely write into the pages directly. If userspace
2616 * races pwrite with any other operation; corruption will ensue -
2617 * that is userspace's prerogative!
2618 */
2619
2620 remain = arg->size;
2621 offset = arg->offset;
2622 pg = offset_in_page(offset);
2623
2624 do {
2625 unsigned int len, unwritten;
2626 struct page *page;
2627 void *data, *vaddr;
2628 int err;
2629
2630 len = PAGE_SIZE - pg;
2631 if (len > remain)
2632 len = remain;
2633
2634 err = pagecache_write_begin(obj->base.filp, mapping,
2635 offset, len, 0,
2636 &page, &data);
2637 if (err < 0)
2638 return err;
2639
2640 vaddr = kmap(page);
2641 unwritten = copy_from_user(vaddr + pg, user_data, len);
2642 kunmap(page);
2643
2644 err = pagecache_write_end(obj->base.filp, mapping,
2645 offset, len, len - unwritten,
2646 page, data);
2647 if (err < 0)
2648 return err;
2649
2650 if (unwritten)
2651 return -EFAULT;
2652
2653 remain -= len;
2654 user_data += len;
2655 offset += len;
2656 pg = 0;
2657 } while (remain);
2658
2659 return 0;
2660}
2661
Chris Wilson60958682016-12-31 11:20:11 +00002662static bool ban_context(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002663{
Chris Wilson60958682016-12-31 11:20:11 +00002664 return (i915_gem_context_is_bannable(ctx) &&
2665 ctx->ban_score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002666}
2667
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002668static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002669{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002670 ctx->guilty_count++;
Chris Wilson60958682016-12-31 11:20:11 +00002671 ctx->ban_score += CONTEXT_SCORE_GUILTY;
2672 if (ban_context(ctx))
2673 i915_gem_context_set_banned(ctx);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002674
2675 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002676 ctx->name, ctx->ban_score,
Chris Wilson60958682016-12-31 11:20:11 +00002677 yesno(i915_gem_context_is_banned(ctx)));
Mika Kuoppalab083a082016-11-18 15:10:47 +02002678
Chris Wilson60958682016-12-31 11:20:11 +00002679 if (!i915_gem_context_is_banned(ctx) || IS_ERR_OR_NULL(ctx->file_priv))
Mika Kuoppalab083a082016-11-18 15:10:47 +02002680 return;
2681
Chris Wilsond9e9da62016-11-22 14:41:18 +00002682 ctx->file_priv->context_bans++;
2683 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2684 ctx->name, ctx->file_priv->context_bans);
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002685}
2686
2687static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2688{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002689 ctx->active_count++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002690}
2691
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002692struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002693i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002694{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002695 struct drm_i915_gem_request *request, *active = NULL;
2696 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002697
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002698 /* We are called by the error capture and reset at a random
2699 * point in time. In particular, note that neither is crucially
2700 * ordered with an interrupt. After a hang, the GPU is dead and we
2701 * assume that no more writes can happen (we waited long enough for
2702 * all writes that were in transaction to be flushed) - adding an
2703 * extra delay for a recent interrupt is pointless. Hence, we do
2704 * not need an engine->irq_seqno_barrier() before the seqno reads.
2705 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002706 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002707 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002708 if (__i915_gem_request_completed(request,
2709 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002710 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002711
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002712 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002713 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2714 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002715
Chris Wilson754c9fd2017-02-23 07:44:14 +00002716 active = request;
2717 break;
2718 }
2719 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2720
2721 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002722}
2723
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002724static bool engine_stalled(struct intel_engine_cs *engine)
2725{
2726 if (!engine->hangcheck.stalled)
2727 return false;
2728
2729 /* Check for possible seqno movement after hang declaration */
2730 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2731 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2732 return false;
2733 }
2734
2735 return true;
2736}
2737
Chris Wilson0e178ae2017-01-17 17:59:06 +02002738int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002739{
2740 struct intel_engine_cs *engine;
2741 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002742 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002743
2744 /* Ensure irq handler finishes, and not run again. */
Chris Wilson0e178ae2017-01-17 17:59:06 +02002745 for_each_engine(engine, dev_priv, id) {
2746 struct drm_i915_gem_request *request;
2747
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002748 /* Prevent the signaler thread from updating the request
2749 * state (by calling dma_fence_signal) as we are processing
2750 * the reset. The write from the GPU of the seqno is
2751 * asynchronous and the signaler thread may see a different
2752 * value to us and declare the request complete, even though
2753 * the reset routine have picked that request as the active
2754 * (incomplete) request. This conflict is not handled
2755 * gracefully!
2756 */
2757 kthread_park(engine->breadcrumbs.signaler);
2758
Chris Wilson1f7b8472017-02-08 14:30:33 +00002759 /* Prevent request submission to the hardware until we have
2760 * completed the reset in i915_gem_reset_finish(). If a request
2761 * is completed by one engine, it may then queue a request
2762 * to a second via its engine->irq_tasklet *just* as we are
2763 * calling engine->init_hw() and also writing the ELSP.
2764 * Turning off the engine->irq_tasklet until the reset is over
2765 * prevents the race.
2766 */
Chris Wilson4c965542017-01-17 17:59:01 +02002767 tasklet_kill(&engine->irq_tasklet);
Chris Wilson1d309632017-02-12 17:20:00 +00002768 tasklet_disable(&engine->irq_tasklet);
Chris Wilson4c965542017-01-17 17:59:01 +02002769
Chris Wilson8c12d122017-02-10 18:52:14 +00002770 if (engine->irq_seqno_barrier)
2771 engine->irq_seqno_barrier(engine);
2772
Chris Wilson0e178ae2017-01-17 17:59:06 +02002773 if (engine_stalled(engine)) {
2774 request = i915_gem_find_active_request(engine);
2775 if (request && request->fence.error == -EIO)
2776 err = -EIO; /* Previous reset failed! */
2777 }
2778 }
2779
Chris Wilson4c965542017-01-17 17:59:01 +02002780 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002781
2782 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002783}
2784
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002785static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002786{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002787 void *vaddr = request->ring->vaddr;
2788 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002789
Chris Wilson821ed7d2016-09-09 14:11:53 +01002790 /* As this request likely depends on state from the lost
2791 * context, clear out all the user operations leaving the
2792 * breadcrumb at the end (so we get the fence notifications).
2793 */
2794 head = request->head;
2795 if (request->postfix < head) {
2796 memset(vaddr + head, 0, request->ring->size - head);
2797 head = 0;
2798 }
2799 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00002800
2801 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00002802}
2803
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002804static void engine_skip_context(struct drm_i915_gem_request *request)
2805{
2806 struct intel_engine_cs *engine = request->engine;
2807 struct i915_gem_context *hung_ctx = request->ctx;
2808 struct intel_timeline *timeline;
2809 unsigned long flags;
2810
2811 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
2812
2813 spin_lock_irqsave(&engine->timeline->lock, flags);
2814 spin_lock(&timeline->lock);
2815
2816 list_for_each_entry_continue(request, &engine->timeline->requests, link)
2817 if (request->ctx == hung_ctx)
2818 skip_request(request);
2819
2820 list_for_each_entry(request, &timeline->requests, link)
2821 skip_request(request);
2822
2823 spin_unlock(&timeline->lock);
2824 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2825}
2826
Mika Kuoppala61da5362017-01-17 17:59:05 +02002827/* Returns true if the request was guilty of hang */
2828static bool i915_gem_reset_request(struct drm_i915_gem_request *request)
2829{
2830 /* Read once and return the resolution */
2831 const bool guilty = engine_stalled(request->engine);
2832
Mika Kuoppala71895a02017-01-17 17:59:07 +02002833 /* The guilty request will get skipped on a hung engine.
2834 *
2835 * Users of client default contexts do not rely on logical
2836 * state preserved between batches so it is safe to execute
2837 * queued requests following the hang. Non default contexts
2838 * rely on preserved state, so skipping a batch loses the
2839 * evolution of the state and it needs to be considered corrupted.
2840 * Executing more queued batches on top of corrupted state is
2841 * risky. But we take the risk by trying to advance through
2842 * the queued requests in order to make the client behaviour
2843 * more predictable around resets, by not throwing away random
2844 * amount of batches it has prepared for execution. Sophisticated
2845 * clients can use gem_reset_stats_ioctl and dma fence status
2846 * (exported via sync_file info ioctl on explicit fences) to observe
2847 * when it loses the context state and should rebuild accordingly.
2848 *
2849 * The context ban, and ultimately the client ban, mechanism are safety
2850 * valves if client submission ends up resulting in nothing more than
2851 * subsequent hangs.
2852 */
2853
Mika Kuoppala61da5362017-01-17 17:59:05 +02002854 if (guilty) {
2855 i915_gem_context_mark_guilty(request->ctx);
2856 skip_request(request);
2857 } else {
2858 i915_gem_context_mark_innocent(request->ctx);
2859 dma_fence_set_error(&request->fence, -EAGAIN);
2860 }
2861
2862 return guilty;
2863}
2864
Chris Wilson821ed7d2016-09-09 14:11:53 +01002865static void i915_gem_reset_engine(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002866{
Chris Wilsondcff85c2016-08-05 10:14:11 +01002867 struct drm_i915_gem_request *request;
Chris Wilson608c1a52015-09-03 13:01:40 +01002868
Chris Wilson821ed7d2016-09-09 14:11:53 +01002869 request = i915_gem_find_active_request(engine);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002870 if (request && i915_gem_reset_request(request)) {
2871 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
2872 engine->name, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002873
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002874 /* If this context is now banned, skip all pending requests. */
2875 if (i915_gem_context_is_banned(request->ctx))
2876 engine_skip_context(request);
2877 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002878
2879 /* Setup the CS to resume from the breadcrumb of the hung request */
2880 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002881}
2882
Chris Wilsond8027092017-02-08 14:30:32 +00002883void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002884{
2885 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302886 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002887
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002888 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2889
Chris Wilson821ed7d2016-09-09 14:11:53 +01002890 i915_gem_retire_requests(dev_priv);
2891
Chris Wilson2ae55732017-02-12 17:20:02 +00002892 for_each_engine(engine, dev_priv, id) {
2893 struct i915_gem_context *ctx;
2894
Chris Wilson821ed7d2016-09-09 14:11:53 +01002895 i915_gem_reset_engine(engine);
Chris Wilson2ae55732017-02-12 17:20:02 +00002896 ctx = fetch_and_zero(&engine->last_retired_context);
2897 if (ctx)
2898 engine->context_unpin(engine, ctx);
2899 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002900
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00002901 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01002902
2903 if (dev_priv->gt.awake) {
2904 intel_sanitize_gt_powersave(dev_priv);
2905 intel_enable_gt_powersave(dev_priv);
2906 if (INTEL_GEN(dev_priv) >= 6)
2907 gen6_rps_busy(dev_priv);
2908 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002909}
2910
Chris Wilsond8027092017-02-08 14:30:32 +00002911void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
2912{
Chris Wilson1f7b8472017-02-08 14:30:33 +00002913 struct intel_engine_cs *engine;
2914 enum intel_engine_id id;
2915
Chris Wilsond8027092017-02-08 14:30:32 +00002916 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00002917
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002918 for_each_engine(engine, dev_priv, id) {
Chris Wilson1f7b8472017-02-08 14:30:33 +00002919 tasklet_enable(&engine->irq_tasklet);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002920 kthread_unpark(engine->breadcrumbs.signaler);
2921 }
Chris Wilsond8027092017-02-08 14:30:32 +00002922}
2923
Chris Wilson821ed7d2016-09-09 14:11:53 +01002924static void nop_submit_request(struct drm_i915_gem_request *request)
2925{
Chris Wilson3cd94422017-01-10 17:22:45 +00002926 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00002927 i915_gem_request_submit(request);
2928 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002929}
2930
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002931static void engine_set_wedged(struct intel_engine_cs *engine)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002932{
Chris Wilson3cd94422017-01-10 17:22:45 +00002933 struct drm_i915_gem_request *request;
2934 unsigned long flags;
2935
Chris Wilson20e49332016-11-22 14:41:21 +00002936 /* We need to be sure that no thread is running the old callback as
2937 * we install the nop handler (otherwise we would submit a request
2938 * to hardware that will never complete). In order to prevent this
2939 * race, we wait until the machine is idle before making the swap
2940 * (using stop_machine()).
2941 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01002942 engine->submit_request = nop_submit_request;
Chris Wilson70c2a242016-09-09 14:11:46 +01002943
Chris Wilson3cd94422017-01-10 17:22:45 +00002944 /* Mark all executing requests as skipped */
2945 spin_lock_irqsave(&engine->timeline->lock, flags);
2946 list_for_each_entry(request, &engine->timeline->requests, link)
2947 dma_fence_set_error(&request->fence, -EIO);
2948 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2949
Chris Wilsonc4b09302016-07-20 09:21:10 +01002950 /* Mark all pending requests as complete so that any concurrent
2951 * (lockless) lookup doesn't try and wait upon the request as we
2952 * reset it.
2953 */
Chris Wilson73cb9702016-10-28 13:58:46 +01002954 intel_engine_init_global_seqno(engine,
Chris Wilsoncb399ea2016-11-01 10:03:16 +00002955 intel_engine_last_submit(engine));
Chris Wilsonc4b09302016-07-20 09:21:10 +01002956
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002957 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002958 * Clear the execlists queue up before freeing the requests, as those
2959 * are the ones that keep the context and ringbuffer backing objects
2960 * pinned in place.
2961 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002962
Tomas Elf7de1691a2015-10-19 16:32:32 +01002963 if (i915.enable_execlists) {
Chris Wilson663f71e2016-11-14 20:41:00 +00002964 unsigned long flags;
2965
2966 spin_lock_irqsave(&engine->timeline->lock, flags);
2967
Chris Wilson70c2a242016-09-09 14:11:46 +01002968 i915_gem_request_put(engine->execlist_port[0].request);
2969 i915_gem_request_put(engine->execlist_port[1].request);
2970 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
Chris Wilson20311bd2016-11-14 20:41:03 +00002971 engine->execlist_queue = RB_ROOT;
2972 engine->execlist_first = NULL;
Chris Wilson663f71e2016-11-14 20:41:00 +00002973
2974 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002975 }
Eric Anholt673a3942008-07-30 12:06:12 -07002976}
2977
Chris Wilson20e49332016-11-22 14:41:21 +00002978static int __i915_gem_set_wedged_BKL(void *data)
Eric Anholt673a3942008-07-30 12:06:12 -07002979{
Chris Wilson20e49332016-11-22 14:41:21 +00002980 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002981 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302982 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07002983
Chris Wilson20e49332016-11-22 14:41:21 +00002984 for_each_engine(engine, i915, id)
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002985 engine_set_wedged(engine);
Chris Wilson20e49332016-11-22 14:41:21 +00002986
2987 return 0;
2988}
2989
2990void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
2991{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002992 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2993 set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
Chris Wilson4db080f2013-12-04 11:37:09 +00002994
Chris Wilson20e49332016-11-22 14:41:21 +00002995 stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
Chris Wilsondfaae392010-09-22 10:31:52 +01002996
Chris Wilson20e49332016-11-22 14:41:21 +00002997 i915_gem_context_lost(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002998 i915_gem_retire_requests(dev_priv);
Chris Wilson20e49332016-11-22 14:41:21 +00002999
3000 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003001}
3002
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003003static void
Eric Anholt673a3942008-07-30 12:06:12 -07003004i915_gem_retire_work_handler(struct work_struct *work)
3005{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003006 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003007 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003008 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003009
Chris Wilson891b48c2010-09-29 12:26:37 +01003010 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003011 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003012 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003013 mutex_unlock(&dev->struct_mutex);
3014 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003015
3016 /* Keep the retire handler running until we are finally idle.
3017 * We do not need to do this test under locking as in the worst-case
3018 * we queue the retire worker once too often.
3019 */
Chris Wilsonc9615612016-07-09 10:12:06 +01003020 if (READ_ONCE(dev_priv->gt.awake)) {
3021 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01003022 queue_delayed_work(dev_priv->wq,
3023 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003024 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01003025 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003026}
Chris Wilson891b48c2010-09-29 12:26:37 +01003027
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003028static void
3029i915_gem_idle_work_handler(struct work_struct *work)
3030{
3031 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003032 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003033 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003034 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303035 enum intel_engine_id id;
Chris Wilson67d97da2016-07-04 08:08:31 +01003036 bool rearm_hangcheck;
3037
3038 if (!READ_ONCE(dev_priv->gt.awake))
3039 return;
3040
Imre Deak0cb56702016-11-07 11:20:04 +02003041 /*
3042 * Wait for last execlists context complete, but bail out in case a
3043 * new request is submitted.
3044 */
3045 wait_for(READ_ONCE(dev_priv->gt.active_requests) ||
Chris Wilson05425242017-03-03 12:19:47 +00003046 intel_engines_are_idle(dev_priv),
3047 10);
Chris Wilson28176ef2016-10-28 13:58:56 +01003048 if (READ_ONCE(dev_priv->gt.active_requests))
Chris Wilson67d97da2016-07-04 08:08:31 +01003049 return;
3050
3051 rearm_hangcheck =
3052 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3053
3054 if (!mutex_trylock(&dev->struct_mutex)) {
3055 /* Currently busy, come back later */
3056 mod_delayed_work(dev_priv->wq,
3057 &dev_priv->gt.idle_work,
3058 msecs_to_jiffies(50));
3059 goto out_rearm;
3060 }
3061
Imre Deak93c97dc2016-11-07 11:20:03 +02003062 /*
3063 * New request retired after this work handler started, extend active
3064 * period until next instance of the work.
3065 */
3066 if (work_pending(work))
3067 goto out_unlock;
3068
Chris Wilson28176ef2016-10-28 13:58:56 +01003069 if (dev_priv->gt.active_requests)
Chris Wilson67d97da2016-07-04 08:08:31 +01003070 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003071
Chris Wilson05425242017-03-03 12:19:47 +00003072 if (wait_for(intel_engines_are_idle(dev_priv), 10))
Imre Deak0cb56702016-11-07 11:20:04 +02003073 DRM_ERROR("Timeout waiting for engines to idle\n");
3074
Chris Wilson67b807a82017-02-27 20:58:50 +00003075 for_each_engine(engine, dev_priv, id) {
3076 intel_engine_disarm_breadcrumbs(engine);
Chris Wilson67d97da2016-07-04 08:08:31 +01003077 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson67b807a82017-02-27 20:58:50 +00003078 }
Zou Nan hai852835f2010-05-21 09:08:56 +08003079
Chris Wilson67d97da2016-07-04 08:08:31 +01003080 GEM_BUG_ON(!dev_priv->gt.awake);
3081 dev_priv->gt.awake = false;
3082 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003083
Chris Wilson67d97da2016-07-04 08:08:31 +01003084 if (INTEL_GEN(dev_priv) >= 6)
3085 gen6_rps_idle(dev_priv);
3086 intel_runtime_pm_put(dev_priv);
3087out_unlock:
3088 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003089
Chris Wilson67d97da2016-07-04 08:08:31 +01003090out_rearm:
3091 if (rearm_hangcheck) {
3092 GEM_BUG_ON(!dev_priv->gt.awake);
3093 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003094 }
Eric Anholt673a3942008-07-30 12:06:12 -07003095}
3096
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003097void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3098{
3099 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3100 struct drm_i915_file_private *fpriv = file->driver_priv;
3101 struct i915_vma *vma, *vn;
3102
3103 mutex_lock(&obj->base.dev->struct_mutex);
3104 list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
3105 if (vma->vm->file == fpriv)
3106 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003107
3108 if (i915_gem_object_is_active(obj) &&
3109 !i915_gem_object_has_active_reference(obj)) {
3110 i915_gem_object_set_active_reference(obj);
3111 i915_gem_object_get(obj);
3112 }
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003113 mutex_unlock(&obj->base.dev->struct_mutex);
3114}
3115
Chris Wilsone95433c2016-10-28 13:58:27 +01003116static unsigned long to_wait_timeout(s64 timeout_ns)
3117{
3118 if (timeout_ns < 0)
3119 return MAX_SCHEDULE_TIMEOUT;
3120
3121 if (timeout_ns == 0)
3122 return 0;
3123
3124 return nsecs_to_jiffies_timeout(timeout_ns);
3125}
3126
Ben Widawsky5816d642012-04-11 11:18:19 -07003127/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003128 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003129 * @dev: drm device pointer
3130 * @data: ioctl data blob
3131 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003132 *
3133 * Returns 0 if successful, else an error is returned with the remaining time in
3134 * the timeout parameter.
3135 * -ETIME: object is still busy after timeout
3136 * -ERESTARTSYS: signal interrupted the wait
3137 * -ENONENT: object doesn't exist
3138 * Also possible, but rare:
3139 * -EAGAIN: GPU wedged
3140 * -ENOMEM: damn
3141 * -ENODEV: Internal IRQ fail
3142 * -E?: The add request failed
3143 *
3144 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3145 * non-zero timeout parameter the wait ioctl will wait for the given number of
3146 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3147 * without holding struct_mutex the object may become re-busied before this
3148 * function completes. A similar but shorter * race condition exists in the busy
3149 * ioctl
3150 */
3151int
3152i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3153{
3154 struct drm_i915_gem_wait *args = data;
3155 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003156 ktime_t start;
3157 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003158
Daniel Vetter11b5d512014-09-29 15:31:26 +02003159 if (args->flags != 0)
3160 return -EINVAL;
3161
Chris Wilson03ac0642016-07-20 13:31:51 +01003162 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003163 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003164 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003165
Chris Wilsone95433c2016-10-28 13:58:27 +01003166 start = ktime_get();
3167
3168 ret = i915_gem_object_wait(obj,
3169 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3170 to_wait_timeout(args->timeout_ns),
3171 to_rps_client(file));
3172
3173 if (args->timeout_ns > 0) {
3174 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3175 if (args->timeout_ns < 0)
3176 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003177
3178 /*
3179 * Apparently ktime isn't accurate enough and occasionally has a
3180 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3181 * things up to make the test happy. We allow up to 1 jiffy.
3182 *
3183 * This is a regression from the timespec->ktime conversion.
3184 */
3185 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3186 args->timeout_ns = 0;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003187 }
3188
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003189 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003190 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003191}
3192
Chris Wilson73cb9702016-10-28 13:58:46 +01003193static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003194{
Chris Wilson73cb9702016-10-28 13:58:46 +01003195 int ret, i;
3196
3197 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3198 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3199 if (ret)
3200 return ret;
3201 }
3202
3203 return 0;
3204}
3205
3206int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3207{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003208 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003209
Chris Wilson9caa34a2016-11-11 14:58:08 +00003210 if (flags & I915_WAIT_LOCKED) {
3211 struct i915_gem_timeline *tl;
3212
3213 lockdep_assert_held(&i915->drm.struct_mutex);
3214
3215 list_for_each_entry(tl, &i915->gt.timelines, link) {
3216 ret = wait_for_timeline(tl, flags);
3217 if (ret)
3218 return ret;
3219 }
3220 } else {
3221 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003222 if (ret)
3223 return ret;
3224 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003225
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003226 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003227}
3228
Eric Anholte47c68e2008-11-14 13:35:19 -08003229/** Flushes the GTT write domain for the object if it's dirty. */
3230static void
Chris Wilson05394f32010-11-08 19:18:58 +00003231i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003232{
Chris Wilson3b5724d2016-08-18 17:16:49 +01003233 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003234
Chris Wilson05394f32010-11-08 19:18:58 +00003235 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003236 return;
3237
Chris Wilson63256ec2011-01-04 18:42:07 +00003238 /* No actual flushing is required for the GTT write domain. Writes
Chris Wilson3b5724d2016-08-18 17:16:49 +01003239 * to it "immediately" go to main memory as far as we know, so there's
Eric Anholte47c68e2008-11-14 13:35:19 -08003240 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003241 *
3242 * However, we do have to enforce the order so that all writes through
3243 * the GTT land before any writes to the device, such as updates to
3244 * the GATT itself.
Chris Wilson3b5724d2016-08-18 17:16:49 +01003245 *
3246 * We also have to wait a bit for the writes to land from the GTT.
3247 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
3248 * timing. This issue has only been observed when switching quickly
3249 * between GTT writes and CPU reads from inside the kernel on recent hw,
3250 * and it appears to only affect discrete GTT blocks (i.e. on LLC
3251 * system agents we cannot reproduce this behaviour).
Eric Anholte47c68e2008-11-14 13:35:19 -08003252 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003253 wmb();
Chris Wilson3b5724d2016-08-18 17:16:49 +01003254 if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
Akash Goel3b3f1652016-10-13 22:44:48 +05303255 POSTING_READ(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
Chris Wilson63256ec2011-01-04 18:42:07 +00003256
Chris Wilsond59b21e2017-02-22 11:40:49 +00003257 intel_fb_obj_flush(obj, write_origin(obj, I915_GEM_DOMAIN_GTT));
Daniel Vetterf99d7062014-06-19 16:01:59 +02003258
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003259 obj->base.write_domain = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08003260}
3261
3262/** Flushes the CPU write domain for the object if it's dirty. */
3263static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003264i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003265{
Chris Wilson05394f32010-11-08 19:18:58 +00003266 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003267 return;
3268
Chris Wilson57822dc2017-02-22 11:40:48 +00003269 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003270 obj->base.write_domain = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08003271}
3272
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003273static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3274{
3275 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU && !obj->cache_dirty)
3276 return;
3277
Chris Wilson57822dc2017-02-22 11:40:48 +00003278 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003279 obj->base.write_domain = 0;
3280}
3281
3282void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3283{
3284 if (!READ_ONCE(obj->pin_display))
3285 return;
3286
3287 mutex_lock(&obj->base.dev->struct_mutex);
3288 __i915_gem_object_flush_for_display(obj);
3289 mutex_unlock(&obj->base.dev->struct_mutex);
3290}
3291
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003292/**
3293 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003294 * @obj: object to act on
3295 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003296 *
3297 * This function returns when the move is complete, including waiting on
3298 * flushes to occur.
3299 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003300int
Chris Wilson20217462010-11-23 15:26:33 +00003301i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003302{
Eric Anholte47c68e2008-11-14 13:35:19 -08003303 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003304
Chris Wilsone95433c2016-10-28 13:58:27 +01003305 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003306
Chris Wilsone95433c2016-10-28 13:58:27 +01003307 ret = i915_gem_object_wait(obj,
3308 I915_WAIT_INTERRUPTIBLE |
3309 I915_WAIT_LOCKED |
3310 (write ? I915_WAIT_ALL : 0),
3311 MAX_SCHEDULE_TIMEOUT,
3312 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003313 if (ret)
3314 return ret;
3315
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003316 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3317 return 0;
3318
Chris Wilson43566de2015-01-02 16:29:29 +05303319 /* Flush and acquire obj->pages so that we are coherent through
3320 * direct access in memory with previous cached writes through
3321 * shmemfs and that our cache domain tracking remains valid.
3322 * For example, if the obj->filp was moved to swap without us
3323 * being notified and releasing the pages, we would mistakenly
3324 * continue to assume that the obj remained out of the CPU cached
3325 * domain.
3326 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003327 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303328 if (ret)
3329 return ret;
3330
Daniel Vettere62b59e2015-01-21 14:53:48 +01003331 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003332
Chris Wilsond0a57782012-10-09 19:24:37 +01003333 /* Serialise direct access to this object with the barriers for
3334 * coherent writes from the GPU, by effectively invalidating the
3335 * GTT domain upon first access.
3336 */
3337 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3338 mb();
3339
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003340 /* It should now be out of any other write domains, and we can update
3341 * the domain values for our changes.
3342 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003343 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003344 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003345 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003346 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3347 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003348 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003349 }
3350
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003351 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003352 return 0;
3353}
3354
Chris Wilsonef55f922015-10-09 14:11:27 +01003355/**
3356 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003357 * @obj: object to act on
3358 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003359 *
3360 * After this function returns, the object will be in the new cache-level
3361 * across all GTT and the contents of the backing storage will be coherent,
3362 * with respect to the new cache-level. In order to keep the backing storage
3363 * coherent for all users, we only allow a single cache level to be set
3364 * globally on the object and prevent it from being changed whilst the
3365 * hardware is reading from the object. That is if the object is currently
3366 * on the scanout it will be set to uncached (or equivalent display
3367 * cache coherency) and all non-MOCS GPU access will also be uncached so
3368 * that all direct access to the scanout remains coherent.
3369 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003370int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3371 enum i915_cache_level cache_level)
3372{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003373 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003374 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003375
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003376 lockdep_assert_held(&obj->base.dev->struct_mutex);
3377
Chris Wilsone4ffd172011-04-04 09:44:39 +01003378 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003379 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003380
Chris Wilsonef55f922015-10-09 14:11:27 +01003381 /* Inspect the list of currently bound VMA and unbind any that would
3382 * be invalid given the new cache-level. This is principally to
3383 * catch the issue of the CS prefetch crossing page boundaries and
3384 * reading an invalid PTE on older architectures.
3385 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003386restart:
3387 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003388 if (!drm_mm_node_allocated(&vma->node))
3389 continue;
3390
Chris Wilson20dfbde2016-08-04 16:32:30 +01003391 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003392 DRM_DEBUG("can not change the cache level of pinned objects\n");
3393 return -EBUSY;
3394 }
3395
Chris Wilsonaa653a62016-08-04 07:52:27 +01003396 if (i915_gem_valid_gtt_space(vma, cache_level))
3397 continue;
3398
3399 ret = i915_vma_unbind(vma);
3400 if (ret)
3401 return ret;
3402
3403 /* As unbinding may affect other elements in the
3404 * obj->vma_list (due to side-effects from retiring
3405 * an active vma), play safe and restart the iterator.
3406 */
3407 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003408 }
3409
Chris Wilsonef55f922015-10-09 14:11:27 +01003410 /* We can reuse the existing drm_mm nodes but need to change the
3411 * cache-level on the PTE. We could simply unbind them all and
3412 * rebind with the correct cache-level on next use. However since
3413 * we already have a valid slot, dma mapping, pages etc, we may as
3414 * rewrite the PTE in the belief that doing so tramples upon less
3415 * state and so involves less work.
3416 */
Chris Wilson15717de2016-08-04 07:52:26 +01003417 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003418 /* Before we change the PTE, the GPU must not be accessing it.
3419 * If we wait upon the object, we know that all the bound
3420 * VMA are no longer active.
3421 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003422 ret = i915_gem_object_wait(obj,
3423 I915_WAIT_INTERRUPTIBLE |
3424 I915_WAIT_LOCKED |
3425 I915_WAIT_ALL,
3426 MAX_SCHEDULE_TIMEOUT,
3427 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003428 if (ret)
3429 return ret;
3430
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003431 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3432 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003433 /* Access to snoopable pages through the GTT is
3434 * incoherent and on some machines causes a hard
3435 * lockup. Relinquish the CPU mmaping to force
3436 * userspace to refault in the pages and we can
3437 * then double check if the GTT mapping is still
3438 * valid for that pointer access.
3439 */
3440 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003441
Chris Wilsonef55f922015-10-09 14:11:27 +01003442 /* As we no longer need a fence for GTT access,
3443 * we can relinquish it now (and so prevent having
3444 * to steal a fence from someone else on the next
3445 * fence request). Note GPU activity would have
3446 * dropped the fence as all snoopable access is
3447 * supposed to be linear.
3448 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003449 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3450 ret = i915_vma_put_fence(vma);
3451 if (ret)
3452 return ret;
3453 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003454 } else {
3455 /* We either have incoherent backing store and
3456 * so no GTT access or the architecture is fully
3457 * coherent. In such cases, existing GTT mmaps
3458 * ignore the cache bit in the PTE and we can
3459 * rewrite it without confusing the GPU or having
3460 * to force userspace to fault back in its mmaps.
3461 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003462 }
3463
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003464 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003465 if (!drm_mm_node_allocated(&vma->node))
3466 continue;
3467
3468 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3469 if (ret)
3470 return ret;
3471 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003472 }
3473
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003474 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU &&
Chris Wilsone59dc172017-02-22 11:40:45 +00003475 i915_gem_object_is_coherent(obj))
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003476 obj->cache_dirty = true;
3477
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003478 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003479 vma->node.color = cache_level;
3480 obj->cache_level = cache_level;
3481
Chris Wilsone4ffd172011-04-04 09:44:39 +01003482 return 0;
3483}
3484
Ben Widawsky199adf42012-09-21 17:01:20 -07003485int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3486 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003487{
Ben Widawsky199adf42012-09-21 17:01:20 -07003488 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003489 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003490 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003491
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003492 rcu_read_lock();
3493 obj = i915_gem_object_lookup_rcu(file, args->handle);
3494 if (!obj) {
3495 err = -ENOENT;
3496 goto out;
3497 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003498
Chris Wilson651d7942013-08-08 14:41:10 +01003499 switch (obj->cache_level) {
3500 case I915_CACHE_LLC:
3501 case I915_CACHE_L3_LLC:
3502 args->caching = I915_CACHING_CACHED;
3503 break;
3504
Chris Wilson4257d3b2013-08-08 14:41:11 +01003505 case I915_CACHE_WT:
3506 args->caching = I915_CACHING_DISPLAY;
3507 break;
3508
Chris Wilson651d7942013-08-08 14:41:10 +01003509 default:
3510 args->caching = I915_CACHING_NONE;
3511 break;
3512 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003513out:
3514 rcu_read_unlock();
3515 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003516}
3517
Ben Widawsky199adf42012-09-21 17:01:20 -07003518int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3519 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003520{
Chris Wilson9c870d02016-10-24 13:42:15 +01003521 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003522 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003523 struct drm_i915_gem_object *obj;
3524 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003525 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003526
Ben Widawsky199adf42012-09-21 17:01:20 -07003527 switch (args->caching) {
3528 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003529 level = I915_CACHE_NONE;
3530 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003531 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003532 /*
3533 * Due to a HW issue on BXT A stepping, GPU stores via a
3534 * snooped mapping may leave stale data in a corresponding CPU
3535 * cacheline, whereas normally such cachelines would get
3536 * invalidated.
3537 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003538 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003539 return -ENODEV;
3540
Chris Wilsone6994ae2012-07-10 10:27:08 +01003541 level = I915_CACHE_LLC;
3542 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003543 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003544 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003545 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003546 default:
3547 return -EINVAL;
3548 }
3549
Chris Wilsond65415d2017-01-19 08:22:10 +00003550 obj = i915_gem_object_lookup(file, args->handle);
3551 if (!obj)
3552 return -ENOENT;
3553
3554 if (obj->cache_level == level)
3555 goto out;
3556
3557 ret = i915_gem_object_wait(obj,
3558 I915_WAIT_INTERRUPTIBLE,
3559 MAX_SCHEDULE_TIMEOUT,
3560 to_rps_client(file));
3561 if (ret)
3562 goto out;
3563
Ben Widawsky3bc29132012-09-26 16:15:20 -07003564 ret = i915_mutex_lock_interruptible(dev);
3565 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003566 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003567
3568 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003569 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003570
3571out:
3572 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003573 return ret;
3574}
3575
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003576/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003577 * Prepare buffer for display plane (scanout, cursors, etc).
3578 * Can be called from an uninterruptible phase (modesetting) and allows
3579 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003580 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003581struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003582i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3583 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003584 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003585{
Chris Wilson058d88c2016-08-15 10:49:06 +01003586 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003587 int ret;
3588
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003589 lockdep_assert_held(&obj->base.dev->struct_mutex);
3590
Chris Wilsoncc98b412013-08-09 12:25:09 +01003591 /* Mark the pin_display early so that we account for the
3592 * display coherency whilst setting up the cache domains.
3593 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003594 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003595
Eric Anholta7ef0642011-03-29 16:59:54 -07003596 /* The display engine is not coherent with the LLC cache on gen6. As
3597 * a result, we make sure that the pinning that is about to occur is
3598 * done with uncached PTEs. This is lowest common denominator for all
3599 * chipsets.
3600 *
3601 * However for gen6+, we could do better by using the GFDT bit instead
3602 * of uncaching, which would allow us to flush all the LLC-cached data
3603 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3604 */
Chris Wilson651d7942013-08-08 14:41:10 +01003605 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003606 HAS_WT(to_i915(obj->base.dev)) ?
3607 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003608 if (ret) {
3609 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003610 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003611 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003612
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003613 /* As the user may map the buffer once pinned in the display plane
3614 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003615 * always use map_and_fenceable for all scanout buffers. However,
3616 * it may simply be too big to fit into mappable, in which case
3617 * put it anyway and hope that userspace can cope (but always first
3618 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003619 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003620 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003621 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003622 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3623 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003624 if (IS_ERR(vma)) {
3625 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3626 unsigned int flags;
3627
3628 /* Valleyview is definitely limited to scanning out the first
3629 * 512MiB. Lets presume this behaviour was inherited from the
3630 * g4x display engine and that all earlier gen are similarly
3631 * limited. Testing suggests that it is a little more
3632 * complicated than this. For example, Cherryview appears quite
3633 * happy to scanout from anywhere within its global aperture.
3634 */
3635 flags = 0;
3636 if (HAS_GMCH_DISPLAY(i915))
3637 flags = PIN_MAPPABLE;
3638 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
3639 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003640 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003641 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003642
Chris Wilsond8923dc2016-08-18 17:17:07 +01003643 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3644
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003645 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003646 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00003647 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003648
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003649 /* It should now be out of any other write domains, and we can update
3650 * the domain values for our changes.
3651 */
Chris Wilson05394f32010-11-08 19:18:58 +00003652 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003653
Chris Wilson058d88c2016-08-15 10:49:06 +01003654 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003655
3656err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003657 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003658 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003659}
3660
3661void
Chris Wilson058d88c2016-08-15 10:49:06 +01003662i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003663{
Chris Wilson49d73912016-11-29 09:50:08 +00003664 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003665
Chris Wilson058d88c2016-08-15 10:49:06 +01003666 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003667 return;
3668
Chris Wilsond8923dc2016-08-18 17:17:07 +01003669 if (--vma->obj->pin_display == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003670 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003671
Chris Wilson383d5822016-08-18 17:17:08 +01003672 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003673 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003674
Chris Wilson058d88c2016-08-15 10:49:06 +01003675 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003676}
3677
Eric Anholte47c68e2008-11-14 13:35:19 -08003678/**
3679 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003680 * @obj: object to act on
3681 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003682 *
3683 * This function returns when the move is complete, including waiting on
3684 * flushes to occur.
3685 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003686int
Chris Wilson919926a2010-11-12 13:42:53 +00003687i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003688{
Eric Anholte47c68e2008-11-14 13:35:19 -08003689 int ret;
3690
Chris Wilsone95433c2016-10-28 13:58:27 +01003691 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003692
Chris Wilsone95433c2016-10-28 13:58:27 +01003693 ret = i915_gem_object_wait(obj,
3694 I915_WAIT_INTERRUPTIBLE |
3695 I915_WAIT_LOCKED |
3696 (write ? I915_WAIT_ALL : 0),
3697 MAX_SCHEDULE_TIMEOUT,
3698 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003699 if (ret)
3700 return ret;
3701
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003702 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3703 return 0;
3704
Eric Anholte47c68e2008-11-14 13:35:19 -08003705 i915_gem_object_flush_gtt_write_domain(obj);
3706
Eric Anholte47c68e2008-11-14 13:35:19 -08003707 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003708 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003709 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00003710 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003711 }
3712
3713 /* It should now be out of any other write domains, and we can update
3714 * the domain values for our changes.
3715 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003716 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003717
3718 /* If we're writing through the CPU, then the GPU read domains will
3719 * need to be invalidated at next use.
3720 */
3721 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003722 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3723 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003724 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003725
3726 return 0;
3727}
3728
Eric Anholt673a3942008-07-30 12:06:12 -07003729/* Throttle our rendering by waiting until the ring has completed our requests
3730 * emitted over 20 msec ago.
3731 *
Eric Anholtb9624422009-06-03 07:27:35 +00003732 * Note that if we were to use the current jiffies each time around the loop,
3733 * we wouldn't escape the function with any frames outstanding if the time to
3734 * render a frame was over 20ms.
3735 *
Eric Anholt673a3942008-07-30 12:06:12 -07003736 * This should get us reasonable parallelism between CPU and GPU but also
3737 * relatively low latency when blocking on a particular request to finish.
3738 */
3739static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003740i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003741{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003742 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003743 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003744 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003745 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003746 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003747
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003748 /* ABI: return -EIO if already wedged */
3749 if (i915_terminally_wedged(&dev_priv->gpu_error))
3750 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003751
Chris Wilson1c255952010-09-26 11:03:27 +01003752 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003753 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00003754 if (time_after_eq(request->emitted_jiffies, recent_enough))
3755 break;
3756
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003757 if (target) {
3758 list_del(&target->client_link);
3759 target->file_priv = NULL;
3760 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01003761
John Harrison54fb2412014-11-24 18:49:27 +00003762 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003763 }
John Harrisonff865882014-11-24 18:49:28 +00003764 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003765 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003766 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003767
John Harrison54fb2412014-11-24 18:49:27 +00003768 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003769 return 0;
3770
Chris Wilsone95433c2016-10-28 13:58:27 +01003771 ret = i915_wait_request(target,
3772 I915_WAIT_INTERRUPTIBLE,
3773 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003774 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003775
Chris Wilsone95433c2016-10-28 13:58:27 +01003776 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003777}
3778
Chris Wilson058d88c2016-08-15 10:49:06 +01003779struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003780i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3781 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003782 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003783 u64 alignment,
3784 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003785{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003786 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
3787 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01003788 struct i915_vma *vma;
3789 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003790
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003791 lockdep_assert_held(&obj->base.dev->struct_mutex);
3792
Chris Wilson718659a2017-01-16 15:21:28 +00003793 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00003794 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003795 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003796
3797 if (i915_vma_misplaced(vma, size, alignment, flags)) {
3798 if (flags & PIN_NONBLOCK &&
3799 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003800 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003801
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003802 if (flags & PIN_MAPPABLE) {
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003803 /* If the required space is larger than the available
3804 * aperture, we will not able to find a slot for the
3805 * object and unbinding the object now will be in
3806 * vain. Worse, doing so may cause us to ping-pong
3807 * the object in and out of the Global GTT and
3808 * waste a lot of cycles under the mutex.
3809 */
Chris Wilson944397f2017-01-09 16:16:11 +00003810 if (vma->fence_size > dev_priv->ggtt.mappable_end)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003811 return ERR_PTR(-E2BIG);
3812
3813 /* If NONBLOCK is set the caller is optimistically
3814 * trying to cache the full object within the mappable
3815 * aperture, and *must* have a fallback in place for
3816 * situations where we cannot bind the object. We
3817 * can be a little more lax here and use the fallback
3818 * more often to avoid costly migrations of ourselves
3819 * and other objects within the aperture.
3820 *
3821 * Half-the-aperture is used as a simple heuristic.
3822 * More interesting would to do search for a free
3823 * block prior to making the commitment to unbind.
3824 * That caters for the self-harm case, and with a
3825 * little more heuristics (e.g. NOFAULT, NOEVICT)
3826 * we could try to minimise harm to others.
3827 */
3828 if (flags & PIN_NONBLOCK &&
Chris Wilson944397f2017-01-09 16:16:11 +00003829 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003830 return ERR_PTR(-ENOSPC);
3831 }
3832
Chris Wilson59bfa122016-08-04 16:32:31 +01003833 WARN(i915_vma_is_pinned(vma),
3834 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003835 " offset=%08x, req.alignment=%llx,"
3836 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3837 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003838 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003839 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003840 ret = i915_vma_unbind(vma);
3841 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003842 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003843 }
3844
Chris Wilson058d88c2016-08-15 10:49:06 +01003845 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3846 if (ret)
3847 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003848
Chris Wilson058d88c2016-08-15 10:49:06 +01003849 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003850}
3851
Chris Wilsonedf6b762016-08-09 09:23:33 +01003852static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003853{
3854 /* Note that we could alias engines in the execbuf API, but
3855 * that would be very unwise as it prevents userspace from
3856 * fine control over engine selection. Ahem.
3857 *
3858 * This should be something like EXEC_MAX_ENGINE instead of
3859 * I915_NUM_ENGINES.
3860 */
3861 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
3862 return 0x10000 << id;
3863}
3864
3865static __always_inline unsigned int __busy_write_id(unsigned int id)
3866{
Chris Wilson70cb4722016-08-09 18:08:25 +01003867 /* The uABI guarantees an active writer is also amongst the read
3868 * engines. This would be true if we accessed the activity tracking
3869 * under the lock, but as we perform the lookup of the object and
3870 * its activity locklessly we can not guarantee that the last_write
3871 * being active implies that we have set the same engine flag from
3872 * last_read - hence we always set both read and write busy for
3873 * last_write.
3874 */
3875 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003876}
3877
Chris Wilsonedf6b762016-08-09 09:23:33 +01003878static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003879__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003880 unsigned int (*flag)(unsigned int id))
3881{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003882 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003883
Chris Wilsond07f0e52016-10-28 13:58:44 +01003884 /* We have to check the current hw status of the fence as the uABI
3885 * guarantees forward progress. We could rely on the idle worker
3886 * to eventually flush us, but to minimise latency just ask the
3887 * hardware.
3888 *
3889 * Note we only report on the status of native fences.
3890 */
3891 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003892 return 0;
3893
Chris Wilsond07f0e52016-10-28 13:58:44 +01003894 /* opencode to_request() in order to avoid const warnings */
3895 rq = container_of(fence, struct drm_i915_gem_request, fence);
3896 if (i915_gem_request_completed(rq))
3897 return 0;
3898
3899 return flag(rq->engine->exec_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003900}
3901
Chris Wilsonedf6b762016-08-09 09:23:33 +01003902static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003903busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003904{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003905 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003906}
3907
Chris Wilsonedf6b762016-08-09 09:23:33 +01003908static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003909busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003910{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003911 if (!fence)
3912 return 0;
3913
3914 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003915}
3916
Eric Anholt673a3942008-07-30 12:06:12 -07003917int
Eric Anholt673a3942008-07-30 12:06:12 -07003918i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003919 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003920{
3921 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003922 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003923 struct reservation_object_list *list;
3924 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003925 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003926
Chris Wilsond07f0e52016-10-28 13:58:44 +01003927 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003928 rcu_read_lock();
3929 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003930 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003931 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003932
3933 /* A discrepancy here is that we do not report the status of
3934 * non-i915 fences, i.e. even though we may report the object as idle,
3935 * a call to set-domain may still stall waiting for foreign rendering.
3936 * This also means that wait-ioctl may report an object as busy,
3937 * where busy-ioctl considers it idle.
3938 *
3939 * We trade the ability to warn of foreign fences to report on which
3940 * i915 engines are active for the object.
3941 *
3942 * Alternatively, we can trade that extra information on read/write
3943 * activity with
3944 * args->busy =
3945 * !reservation_object_test_signaled_rcu(obj->resv, true);
3946 * to report the overall busyness. This is what the wait-ioctl does.
3947 *
3948 */
3949retry:
3950 seq = raw_read_seqcount(&obj->resv->seq);
3951
3952 /* Translate the exclusive fence to the READ *and* WRITE engine */
3953 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
3954
3955 /* Translate shared fences to READ set of engines */
3956 list = rcu_dereference(obj->resv->fence);
3957 if (list) {
3958 unsigned int shared_count = list->shared_count, i;
3959
3960 for (i = 0; i < shared_count; ++i) {
3961 struct dma_fence *fence =
3962 rcu_dereference(list->shared[i]);
3963
3964 args->busy |= busy_check_reader(fence);
3965 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003966 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003967
Chris Wilsond07f0e52016-10-28 13:58:44 +01003968 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
3969 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00003970
Chris Wilsond07f0e52016-10-28 13:58:44 +01003971 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003972out:
3973 rcu_read_unlock();
3974 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07003975}
3976
3977int
3978i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3979 struct drm_file *file_priv)
3980{
Akshay Joshi0206e352011-08-16 15:34:10 -04003981 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003982}
3983
Chris Wilson3ef94da2009-09-14 16:50:29 +01003984int
3985i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3986 struct drm_file *file_priv)
3987{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003988 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01003989 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003990 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003991 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003992
3993 switch (args->madv) {
3994 case I915_MADV_DONTNEED:
3995 case I915_MADV_WILLNEED:
3996 break;
3997 default:
3998 return -EINVAL;
3999 }
4000
Chris Wilson03ac0642016-07-20 13:31:51 +01004001 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004002 if (!obj)
4003 return -ENOENT;
4004
4005 err = mutex_lock_interruptible(&obj->mm.lock);
4006 if (err)
4007 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004008
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004009 if (obj->mm.pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004010 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004011 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004012 if (obj->mm.madv == I915_MADV_WILLNEED) {
4013 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004014 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004015 obj->mm.quirked = false;
4016 }
4017 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004018 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004019 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004020 obj->mm.quirked = true;
4021 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004022 }
4023
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004024 if (obj->mm.madv != __I915_MADV_PURGED)
4025 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004026
Chris Wilson6c085a72012-08-20 11:40:46 +02004027 /* if the object is no longer attached, discard its backing storage */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004028 if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004029 i915_gem_object_truncate(obj);
4030
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004031 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004032 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004033
Chris Wilson1233e2d2016-10-28 13:58:37 +01004034out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004035 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004036 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004037}
4038
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004039static void
4040frontbuffer_retire(struct i915_gem_active *active,
4041 struct drm_i915_gem_request *request)
4042{
4043 struct drm_i915_gem_object *obj =
4044 container_of(active, typeof(*obj), frontbuffer_write);
4045
Chris Wilsond59b21e2017-02-22 11:40:49 +00004046 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004047}
4048
Chris Wilson37e680a2012-06-07 15:38:42 +01004049void i915_gem_object_init(struct drm_i915_gem_object *obj,
4050 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004051{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004052 mutex_init(&obj->mm.lock);
4053
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004054 INIT_LIST_HEAD(&obj->global_link);
Chris Wilson275f0392016-10-24 13:42:14 +01004055 INIT_LIST_HEAD(&obj->userfault_link);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004056 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004057 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004058 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004059
Chris Wilson37e680a2012-06-07 15:38:42 +01004060 obj->ops = ops;
4061
Chris Wilsond07f0e52016-10-28 13:58:44 +01004062 reservation_object_init(&obj->__builtin_resv);
4063 obj->resv = &obj->__builtin_resv;
4064
Chris Wilson50349242016-08-18 17:17:04 +01004065 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004066 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004067
4068 obj->mm.madv = I915_MADV_WILLNEED;
4069 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4070 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004071
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004072 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004073}
4074
Chris Wilson37e680a2012-06-07 15:38:42 +01004075static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004076 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4077 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004078
Chris Wilson37e680a2012-06-07 15:38:42 +01004079 .get_pages = i915_gem_object_get_pages_gtt,
4080 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004081
4082 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004083};
4084
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004085struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004086i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004087{
Daniel Vetterc397b902010-04-09 19:05:07 +00004088 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004089 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004090 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004091 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004092
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004093 /* There is a prevalence of the assumption that we fit the object's
4094 * page count inside a 32bit _signed_ variable. Let's document this and
4095 * catch if we ever need to fix it. In the meantime, if you do spot
4096 * such a local variable, please consider fixing!
4097 */
4098 if (WARN_ON(size >> PAGE_SHIFT > INT_MAX))
4099 return ERR_PTR(-E2BIG);
4100
4101 if (overflows_type(size, obj->base.size))
4102 return ERR_PTR(-E2BIG);
4103
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004104 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004105 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004106 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004107
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004108 ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004109 if (ret)
4110 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004111
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004112 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004113 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004114 /* 965gm cannot relocate objects above 4GiB. */
4115 mask &= ~__GFP_HIGHMEM;
4116 mask |= __GFP_DMA32;
4117 }
4118
Al Viro93c76a32015-12-04 23:45:44 -05004119 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004120 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004121
Chris Wilson37e680a2012-06-07 15:38:42 +01004122 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004123
Daniel Vetterc397b902010-04-09 19:05:07 +00004124 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4125 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4126
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004127 if (HAS_LLC(dev_priv)) {
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004128 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004129 * cache) for about a 10% performance improvement
4130 * compared to uncached. Graphics requests other than
4131 * display scanout are coherent with the CPU in
4132 * accessing this cache. This means in this mode we
4133 * don't need to clflush on the CPU side, and on the
4134 * GPU side we only need to flush internal caches to
4135 * get data visible to the CPU.
4136 *
4137 * However, we maintain the display planes as UC, and so
4138 * need to rebind when first used as such.
4139 */
4140 obj->cache_level = I915_CACHE_LLC;
4141 } else
4142 obj->cache_level = I915_CACHE_NONE;
4143
Daniel Vetterd861e332013-07-24 23:25:03 +02004144 trace_i915_gem_object_create(obj);
4145
Chris Wilson05394f32010-11-08 19:18:58 +00004146 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004147
4148fail:
4149 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004150 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004151}
4152
Chris Wilson340fbd82014-05-22 09:16:52 +01004153static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4154{
4155 /* If we are the last user of the backing storage (be it shmemfs
4156 * pages or stolen etc), we know that the pages are going to be
4157 * immediately released. In this case, we can then skip copying
4158 * back the contents from the GPU.
4159 */
4160
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004161 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004162 return false;
4163
4164 if (obj->base.filp == NULL)
4165 return true;
4166
4167 /* At first glance, this looks racy, but then again so would be
4168 * userspace racing mmap against close. However, the first external
4169 * reference to the filp can only be obtained through the
4170 * i915_gem_mmap_ioctl() which safeguards us against the user
4171 * acquiring such a reference whilst we are in the middle of
4172 * freeing the object.
4173 */
4174 return atomic_long_read(&obj->base.filp->f_count) == 1;
4175}
4176
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004177static void __i915_gem_free_objects(struct drm_i915_private *i915,
4178 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004179{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004180 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004181
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004182 mutex_lock(&i915->drm.struct_mutex);
4183 intel_runtime_pm_get(i915);
4184 llist_for_each_entry(obj, freed, freed) {
4185 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004186
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004187 trace_i915_gem_object_destroy(obj);
4188
4189 GEM_BUG_ON(i915_gem_object_is_active(obj));
4190 list_for_each_entry_safe(vma, vn,
4191 &obj->vma_list, obj_link) {
4192 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
4193 GEM_BUG_ON(i915_vma_is_active(vma));
4194 vma->flags &= ~I915_VMA_PIN_MASK;
4195 i915_vma_close(vma);
4196 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004197 GEM_BUG_ON(!list_empty(&obj->vma_list));
4198 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004199
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004200 list_del(&obj->global_link);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004201 }
4202 intel_runtime_pm_put(i915);
4203 mutex_unlock(&i915->drm.struct_mutex);
4204
4205 llist_for_each_entry_safe(obj, on, freed, freed) {
4206 GEM_BUG_ON(obj->bind_count);
4207 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
4208
4209 if (obj->ops->release)
4210 obj->ops->release(obj);
4211
4212 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4213 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004214 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004215 GEM_BUG_ON(obj->mm.pages);
4216
4217 if (obj->base.import_attach)
4218 drm_prime_gem_destroy(&obj->base, NULL);
4219
Chris Wilsond07f0e52016-10-28 13:58:44 +01004220 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004221 drm_gem_object_release(&obj->base);
4222 i915_gem_info_remove_obj(i915, obj->base.size);
4223
4224 kfree(obj->bit_17);
4225 i915_gem_object_free(obj);
4226 }
4227}
4228
4229static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4230{
4231 struct llist_node *freed;
4232
4233 freed = llist_del_all(&i915->mm.free_list);
4234 if (unlikely(freed))
4235 __i915_gem_free_objects(i915, freed);
4236}
4237
4238static void __i915_gem_free_work(struct work_struct *work)
4239{
4240 struct drm_i915_private *i915 =
4241 container_of(work, struct drm_i915_private, mm.free_work);
4242 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004243
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004244 /* All file-owned VMA should have been released by this point through
4245 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4246 * However, the object may also be bound into the global GTT (e.g.
4247 * older GPUs without per-process support, or for direct access through
4248 * the GTT either for the user or for scanout). Those VMA still need to
4249 * unbound now.
4250 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004251
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004252 while ((freed = llist_del_all(&i915->mm.free_list)))
4253 __i915_gem_free_objects(i915, freed);
4254}
4255
4256static void __i915_gem_free_object_rcu(struct rcu_head *head)
4257{
4258 struct drm_i915_gem_object *obj =
4259 container_of(head, typeof(*obj), rcu);
4260 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4261
4262 /* We can't simply use call_rcu() from i915_gem_free_object()
4263 * as we need to block whilst unbinding, and the call_rcu
4264 * task may be called from softirq context. So we take a
4265 * detour through a worker.
4266 */
4267 if (llist_add(&obj->freed, &i915->mm.free_list))
4268 schedule_work(&i915->mm.free_work);
4269}
4270
4271void i915_gem_free_object(struct drm_gem_object *gem_obj)
4272{
4273 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4274
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004275 if (obj->mm.quirked)
4276 __i915_gem_object_unpin_pages(obj);
4277
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004278 if (discard_backing_storage(obj))
4279 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004280
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004281 /* Before we free the object, make sure any pure RCU-only
4282 * read-side critical sections are complete, e.g.
4283 * i915_gem_busy_ioctl(). For the corresponding synchronized
4284 * lookup see i915_gem_object_lookup_rcu().
4285 */
4286 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004287}
4288
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004289void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4290{
4291 lockdep_assert_held(&obj->base.dev->struct_mutex);
4292
4293 GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
4294 if (i915_gem_object_is_active(obj))
4295 i915_gem_object_set_active_reference(obj);
4296 else
4297 i915_gem_object_put(obj);
4298}
4299
Chris Wilson3033aca2016-10-28 13:58:47 +01004300static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
4301{
4302 struct intel_engine_cs *engine;
4303 enum intel_engine_id id;
4304
4305 for_each_engine(engine, dev_priv, id)
Chris Wilsonf131e352016-12-29 14:40:37 +00004306 GEM_BUG_ON(engine->last_retired_context &&
4307 !i915_gem_context_is_kernel(engine->last_retired_context));
Chris Wilson3033aca2016-10-28 13:58:47 +01004308}
4309
Chris Wilson24145512017-01-24 11:01:35 +00004310void i915_gem_sanitize(struct drm_i915_private *i915)
4311{
4312 /*
4313 * If we inherit context state from the BIOS or earlier occupants
4314 * of the GPU, the GPU may be in an inconsistent state when we
4315 * try to take over. The only way to remove the earlier state
4316 * is by resetting. However, resetting on earlier gen is tricky as
4317 * it may impact the display and we are uncertain about the stability
4318 * of the reset, so we only reset recent machines with logical
4319 * context support (that must be reset to remove any stray contexts).
4320 */
4321 if (HAS_HW_CONTEXTS(i915)) {
4322 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4323 WARN_ON(reset && reset != -ENODEV);
4324 }
4325}
4326
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004327int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004328{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004329 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004330 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004331
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004332 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004333 intel_suspend_gt_powersave(dev_priv);
4334
Chris Wilson45c5f202013-10-16 11:50:01 +01004335 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004336
4337 /* We have to flush all the executing contexts to main memory so
4338 * that they can saved in the hibernation image. To ensure the last
4339 * context image is coherent, we have to switch away from it. That
4340 * leaves the dev_priv->kernel_context still active when
4341 * we actually suspend, and its image in memory may not match the GPU
4342 * state. Fortunately, the kernel_context is disposable and we do
4343 * not rely on its state.
4344 */
4345 ret = i915_gem_switch_to_kernel_context(dev_priv);
4346 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004347 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004348
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004349 ret = i915_gem_wait_for_idle(dev_priv,
4350 I915_WAIT_INTERRUPTIBLE |
4351 I915_WAIT_LOCKED);
Chris Wilsonf7403342013-09-13 23:57:04 +01004352 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004353 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004354
Chris Wilsonc0336662016-05-06 15:40:21 +01004355 i915_gem_retire_requests(dev_priv);
Chris Wilson28176ef2016-10-28 13:58:56 +01004356 GEM_BUG_ON(dev_priv->gt.active_requests);
Eric Anholt673a3942008-07-30 12:06:12 -07004357
Chris Wilson3033aca2016-10-28 13:58:47 +01004358 assert_kernel_context_is_current(dev_priv);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004359 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004360 mutex_unlock(&dev->struct_mutex);
4361
Chris Wilson737b1502015-01-26 18:03:03 +02004362 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004363 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004364
4365 /* As the idle_work is rearming if it detects a race, play safe and
4366 * repeat the flush until it is definitely idle.
4367 */
4368 while (flush_delayed_work(&dev_priv->gt.idle_work))
4369 ;
4370
4371 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson29105cc2010-01-07 10:39:13 +00004372
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004373 /* Assert that we sucessfully flushed all the work and
4374 * reset the GPU back to its idle, low power state.
4375 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004376 WARN_ON(dev_priv->gt.awake);
Chris Wilson05425242017-03-03 12:19:47 +00004377 WARN_ON(!intel_engines_are_idle(dev_priv));
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004378
Imre Deak1c777c52016-10-12 17:46:37 +03004379 /*
4380 * Neither the BIOS, ourselves or any other kernel
4381 * expects the system to be in execlists mode on startup,
4382 * so we need to reset the GPU back to legacy mode. And the only
4383 * known way to disable logical contexts is through a GPU reset.
4384 *
4385 * So in order to leave the system in a known default configuration,
4386 * always reset the GPU upon unload and suspend. Afterwards we then
4387 * clean up the GEM state tracking, flushing off the requests and
4388 * leaving the system in a known idle state.
4389 *
4390 * Note that is of the upmost importance that the GPU is idle and
4391 * all stray writes are flushed *before* we dismantle the backing
4392 * storage for the pinned objects.
4393 *
4394 * However, since we are uncertain that resetting the GPU on older
4395 * machines is a good idea, we don't - just in case it leaves the
4396 * machine in an unusable condition.
4397 */
Chris Wilson24145512017-01-24 11:01:35 +00004398 i915_gem_sanitize(dev_priv);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004399 goto out_rpm_put;
Imre Deak1c777c52016-10-12 17:46:37 +03004400
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004401err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004402 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004403out_rpm_put:
4404 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004405 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004406}
4407
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004408void i915_gem_resume(struct drm_i915_private *dev_priv)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004409{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004410 struct drm_device *dev = &dev_priv->drm;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004411
Imre Deak31ab49a2016-11-07 11:20:05 +02004412 WARN_ON(dev_priv->gt.awake);
4413
Chris Wilson5ab57c72016-07-15 14:56:20 +01004414 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00004415 i915_gem_restore_gtt_mappings(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004416
4417 /* As we didn't flush the kernel context before suspend, we cannot
4418 * guarantee that the context image is complete. So let's just reset
4419 * it and start again.
4420 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004421 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004422
4423 mutex_unlock(&dev->struct_mutex);
4424}
4425
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004426void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004427{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004428 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004429 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4430 return;
4431
4432 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4433 DISP_TILE_SURFACE_SWIZZLING);
4434
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004435 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004436 return;
4437
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004438 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004439 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004440 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004441 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004442 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004443 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004444 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004445 else
4446 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004447}
Daniel Vettere21af882012-02-09 20:53:27 +01004448
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004449static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004450{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004451 I915_WRITE(RING_CTL(base), 0);
4452 I915_WRITE(RING_HEAD(base), 0);
4453 I915_WRITE(RING_TAIL(base), 0);
4454 I915_WRITE(RING_START(base), 0);
4455}
4456
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004457static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004458{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004459 if (IS_I830(dev_priv)) {
4460 init_unused_ring(dev_priv, PRB1_BASE);
4461 init_unused_ring(dev_priv, SRB0_BASE);
4462 init_unused_ring(dev_priv, SRB1_BASE);
4463 init_unused_ring(dev_priv, SRB2_BASE);
4464 init_unused_ring(dev_priv, SRB3_BASE);
4465 } else if (IS_GEN2(dev_priv)) {
4466 init_unused_ring(dev_priv, SRB0_BASE);
4467 init_unused_ring(dev_priv, SRB1_BASE);
4468 } else if (IS_GEN3(dev_priv)) {
4469 init_unused_ring(dev_priv, PRB1_BASE);
4470 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004471 }
4472}
4473
Chris Wilson20a8a742017-02-08 14:30:31 +00004474static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004475{
Chris Wilson20a8a742017-02-08 14:30:31 +00004476 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004477 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304478 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004479 int err;
4480
4481 for_each_engine(engine, i915, id) {
4482 err = engine->init_hw(engine);
4483 if (err)
4484 return err;
4485 }
4486
4487 return 0;
4488}
4489
4490int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4491{
Chris Wilsond200cda2016-04-28 09:56:44 +01004492 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004493
Chris Wilsonde867c22016-10-25 13:16:02 +01004494 dev_priv->gt.last_init_time = ktime_get();
4495
Chris Wilson5e4f5182015-02-13 14:35:59 +00004496 /* Double layer security blanket, see i915_gem_init() */
4497 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4498
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004499 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004500 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004501
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004502 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004503 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004504 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004505
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004506 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004507 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004508 u32 temp = I915_READ(GEN7_MSG_CTL);
4509 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4510 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004511 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004512 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4513 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4514 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4515 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004516 }
4517
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004518 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004519
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004520 /*
4521 * At least 830 can leave some of the unused rings
4522 * "active" (ie. head != tail) after resume which
4523 * will prevent c3 entry. Makes sure all unused rings
4524 * are totally idle.
4525 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004526 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004527
Dave Gordoned54c1a2016-01-19 19:02:54 +00004528 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004529
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004530 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004531 if (ret) {
4532 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4533 goto out;
4534 }
4535
4536 /* Need to do basic initialisation of all rings first: */
Chris Wilson20a8a742017-02-08 14:30:31 +00004537 ret = __i915_gem_restart_engines(dev_priv);
4538 if (ret)
4539 goto out;
Mika Kuoppala99433932013-01-22 14:12:17 +02004540
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004541 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004542
Alex Dai33a732f2015-08-12 15:43:36 +01004543 /* We can't enable contexts until all firmware is loaded */
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +01004544 ret = intel_uc_init_hw(dev_priv);
Dave Gordone556f7c2016-06-07 09:14:49 +01004545 if (ret)
4546 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004547
Chris Wilson5e4f5182015-02-13 14:35:59 +00004548out:
4549 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004550 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004551}
4552
Chris Wilson39df9192016-07-20 13:31:57 +01004553bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4554{
4555 if (INTEL_INFO(dev_priv)->gen < 6)
4556 return false;
4557
4558 /* TODO: make semaphores and Execlists play nicely together */
4559 if (i915.enable_execlists)
4560 return false;
4561
4562 if (value >= 0)
4563 return value;
4564
4565#ifdef CONFIG_INTEL_IOMMU
4566 /* Enable semaphores on SNB when IO remapping is off */
4567 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4568 return false;
4569#endif
4570
4571 return true;
4572}
4573
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004574int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004575{
Chris Wilson1070a422012-04-24 15:47:41 +01004576 int ret;
4577
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004578 mutex_lock(&dev_priv->drm.struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004579
Chris Wilson57822dc2017-02-22 11:40:48 +00004580 i915_gem_clflush_init(dev_priv);
4581
Oscar Mateoa83014d2014-07-24 17:04:21 +01004582 if (!i915.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004583 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004584 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004585 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004586 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004587 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004588 }
4589
Chris Wilson5e4f5182015-02-13 14:35:59 +00004590 /* This is just a security blanket to placate dragons.
4591 * On some systems, we very sporadically observe that the first TLBs
4592 * used by the CS may be stale, despite us poking the TLB reset. If
4593 * we hold the forcewake during initialisation these problems
4594 * just magically go away.
4595 */
4596 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4597
Chris Wilson72778cb2016-05-19 16:17:16 +01004598 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004599
4600 ret = i915_gem_init_ggtt(dev_priv);
4601 if (ret)
4602 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004603
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004604 ret = i915_gem_context_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004605 if (ret)
4606 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004607
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004608 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004609 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004610 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004611
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004612 ret = i915_gem_init_hw(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004613 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004614 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004615 * wedged. But we only want to do this where the GPU is angry,
4616 * for all other failure, such as an allocation failure, bail.
4617 */
4618 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004619 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004620 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004621 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004622
4623out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004624 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004625 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004626
Chris Wilson60990322014-04-09 09:19:42 +01004627 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004628}
4629
Chris Wilson24145512017-01-24 11:01:35 +00004630void i915_gem_init_mmio(struct drm_i915_private *i915)
4631{
4632 i915_gem_sanitize(i915);
4633}
4634
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004635void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004636i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004637{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004638 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304639 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004640
Akash Goel3b3f1652016-10-13 22:44:48 +05304641 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004642 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004643}
4644
Eric Anholt673a3942008-07-30 12:06:12 -07004645void
Imre Deak40ae4e12016-03-16 14:54:03 +02004646i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4647{
Chris Wilson49ef5292016-08-18 17:17:00 +01004648 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004649
4650 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4651 !IS_CHERRYVIEW(dev_priv))
4652 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02004653 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
4654 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4655 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004656 dev_priv->num_fence_regs = 16;
4657 else
4658 dev_priv->num_fence_regs = 8;
4659
Chris Wilsonc0336662016-05-06 15:40:21 +01004660 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004661 dev_priv->num_fence_regs =
4662 I915_READ(vgtif_reg(avail_rs.fence_num));
4663
4664 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004665 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4666 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4667
4668 fence->i915 = dev_priv;
4669 fence->id = i;
4670 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4671 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004672 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004673
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004674 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004675}
4676
Chris Wilson73cb9702016-10-28 13:58:46 +01004677int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004678i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004679{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004680 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004681
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004682 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
4683 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01004684 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01004685
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004686 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
4687 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01004688 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01004689
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004690 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
4691 SLAB_HWCACHE_ALIGN |
4692 SLAB_RECLAIM_ACCOUNT |
4693 SLAB_DESTROY_BY_RCU);
4694 if (!dev_priv->requests)
Chris Wilson73cb9702016-10-28 13:58:46 +01004695 goto err_vmas;
Chris Wilson73cb9702016-10-28 13:58:46 +01004696
Chris Wilson52e54202016-11-14 20:41:02 +00004697 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
4698 SLAB_HWCACHE_ALIGN |
4699 SLAB_RECLAIM_ACCOUNT);
4700 if (!dev_priv->dependencies)
4701 goto err_requests;
4702
Chris Wilson73cb9702016-10-28 13:58:46 +01004703 mutex_lock(&dev_priv->drm.struct_mutex);
4704 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00004705 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01004706 mutex_unlock(&dev_priv->drm.struct_mutex);
4707 if (err)
Chris Wilson52e54202016-11-14 20:41:02 +00004708 goto err_dependencies;
Eric Anholt673a3942008-07-30 12:06:12 -07004709
Ben Widawskya33afea2013-09-17 21:12:45 -07004710 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004711 INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
4712 init_llist_head(&dev_priv->mm.free_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004713 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4714 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004715 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004716 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004717 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004718 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004719 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004720 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004721 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004722 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004723
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004724 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004725
Chris Wilsonce453d82011-02-21 14:43:56 +00004726 dev_priv->mm.interruptible = true;
4727
Joonas Lahtinen6f633402016-09-01 14:58:21 +03004728 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
4729
Chris Wilsonb5add952016-08-04 16:32:36 +01004730 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01004731
4732 return 0;
4733
Chris Wilson52e54202016-11-14 20:41:02 +00004734err_dependencies:
4735 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01004736err_requests:
4737 kmem_cache_destroy(dev_priv->requests);
4738err_vmas:
4739 kmem_cache_destroy(dev_priv->vmas);
4740err_objects:
4741 kmem_cache_destroy(dev_priv->objects);
4742err_out:
4743 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004744}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004745
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004746void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02004747{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004748 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004749 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004750 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004751
Matthew Auldea84aa72016-11-17 21:04:11 +00004752 mutex_lock(&dev_priv->drm.struct_mutex);
4753 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
4754 WARN_ON(!list_empty(&dev_priv->gt.timelines));
4755 mutex_unlock(&dev_priv->drm.struct_mutex);
4756
Chris Wilson52e54202016-11-14 20:41:02 +00004757 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02004758 kmem_cache_destroy(dev_priv->requests);
4759 kmem_cache_destroy(dev_priv->vmas);
4760 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01004761
4762 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
4763 rcu_barrier();
Imre Deakd64aa092016-01-19 15:26:29 +02004764}
4765
Chris Wilson6a800ea2016-09-21 14:51:07 +01004766int i915_gem_freeze(struct drm_i915_private *dev_priv)
4767{
Chris Wilson6a800ea2016-09-21 14:51:07 +01004768 mutex_lock(&dev_priv->drm.struct_mutex);
4769 i915_gem_shrink_all(dev_priv);
4770 mutex_unlock(&dev_priv->drm.struct_mutex);
4771
Chris Wilson6a800ea2016-09-21 14:51:07 +01004772 return 0;
4773}
4774
Chris Wilson461fb992016-05-14 07:26:33 +01004775int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4776{
4777 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01004778 struct list_head *phases[] = {
4779 &dev_priv->mm.unbound_list,
4780 &dev_priv->mm.bound_list,
4781 NULL
4782 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01004783
4784 /* Called just before we write the hibernation image.
4785 *
4786 * We need to update the domain tracking to reflect that the CPU
4787 * will be accessing all the pages to create and restore from the
4788 * hibernation, and so upon restoration those pages will be in the
4789 * CPU domain.
4790 *
4791 * To make sure the hibernation image contains the latest state,
4792 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01004793 *
4794 * To try and reduce the hibernation image, we manually shrink
4795 * the objects as well.
Chris Wilson461fb992016-05-14 07:26:33 +01004796 */
4797
Chris Wilson6a800ea2016-09-21 14:51:07 +01004798 mutex_lock(&dev_priv->drm.struct_mutex);
4799 i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
Chris Wilson461fb992016-05-14 07:26:33 +01004800
Chris Wilson7aab2d52016-09-09 20:02:18 +01004801 for (p = phases; *p; p++) {
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004802 list_for_each_entry(obj, *p, global_link) {
Chris Wilson7aab2d52016-09-09 20:02:18 +01004803 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4804 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4805 }
Chris Wilson461fb992016-05-14 07:26:33 +01004806 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01004807 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01004808
4809 return 0;
4810}
4811
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004812void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004813{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004814 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004815 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004816
4817 /* Clean up our request list when the client is going away, so that
4818 * later retire_requests won't dereference our soon-to-be-gone
4819 * file_priv.
4820 */
Chris Wilson1c255952010-09-26 11:03:27 +01004821 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004822 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004823 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004824 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004825
Chris Wilson2e1b8732015-04-27 13:41:22 +01004826 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004827 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004828 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004829 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004830 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004831}
4832
4833int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4834{
4835 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004836 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004837
Chris Wilsonc4c29d72016-11-09 10:45:07 +00004838 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004839
4840 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4841 if (!file_priv)
4842 return -ENOMEM;
4843
4844 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004845 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004846 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004847 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004848
4849 spin_lock_init(&file_priv->mm.lock);
4850 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004851
Chris Wilsonc80ff162016-07-27 09:07:27 +01004852 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004853
Ben Widawskye422b882013-12-06 14:10:58 -08004854 ret = i915_gem_context_open(dev, file);
4855 if (ret)
4856 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004857
Ben Widawskye422b882013-12-06 14:10:58 -08004858 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004859}
4860
Daniel Vetterb680c372014-09-19 18:27:27 +02004861/**
4862 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004863 * @old: current GEM buffer for the frontbuffer slots
4864 * @new: new GEM buffer for the frontbuffer slots
4865 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004866 *
4867 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4868 * from @old and setting them in @new. Both @old and @new can be NULL.
4869 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004870void i915_gem_track_fb(struct drm_i915_gem_object *old,
4871 struct drm_i915_gem_object *new,
4872 unsigned frontbuffer_bits)
4873{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004874 /* Control of individual bits within the mask are guarded by
4875 * the owning plane->mutex, i.e. we can never see concurrent
4876 * manipulation of individual bits. But since the bitfield as a whole
4877 * is updated using RMW, we need to use atomics in order to update
4878 * the bits.
4879 */
4880 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
4881 sizeof(atomic_t) * BITS_PER_BYTE);
4882
Daniel Vettera071fa02014-06-18 23:28:09 +02004883 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004884 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
4885 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004886 }
4887
4888 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004889 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
4890 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004891 }
4892}
4893
Dave Gordonea702992015-07-09 19:29:02 +01004894/* Allocate a new GEM object and fill it with the supplied data */
4895struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004896i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01004897 const void *data, size_t size)
4898{
4899 struct drm_i915_gem_object *obj;
4900 struct sg_table *sg;
4901 size_t bytes;
4902 int ret;
4903
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004904 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01004905 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01004906 return obj;
4907
4908 ret = i915_gem_object_set_to_cpu_domain(obj, true);
4909 if (ret)
4910 goto fail;
4911
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004912 ret = i915_gem_object_pin_pages(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004913 if (ret)
4914 goto fail;
4915
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004916 sg = obj->mm.pages;
Dave Gordonea702992015-07-09 19:29:02 +01004917 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004918 obj->mm.dirty = true; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01004919 i915_gem_object_unpin_pages(obj);
4920
4921 if (WARN_ON(bytes != size)) {
4922 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
4923 ret = -EFAULT;
4924 goto fail;
4925 }
4926
4927 return obj;
4928
4929fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004930 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004931 return ERR_PTR(ret);
4932}
Chris Wilson96d77632016-10-28 13:58:33 +01004933
4934struct scatterlist *
4935i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
4936 unsigned int n,
4937 unsigned int *offset)
4938{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004939 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01004940 struct scatterlist *sg;
4941 unsigned int idx, count;
4942
4943 might_sleep();
4944 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004945 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01004946
4947 /* As we iterate forward through the sg, we record each entry in a
4948 * radixtree for quick repeated (backwards) lookups. If we have seen
4949 * this index previously, we will have an entry for it.
4950 *
4951 * Initial lookup is O(N), but this is amortized to O(1) for
4952 * sequential page access (where each new request is consecutive
4953 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
4954 * i.e. O(1) with a large constant!
4955 */
4956 if (n < READ_ONCE(iter->sg_idx))
4957 goto lookup;
4958
4959 mutex_lock(&iter->lock);
4960
4961 /* We prefer to reuse the last sg so that repeated lookup of this
4962 * (or the subsequent) sg are fast - comparing against the last
4963 * sg is faster than going through the radixtree.
4964 */
4965
4966 sg = iter->sg_pos;
4967 idx = iter->sg_idx;
4968 count = __sg_page_count(sg);
4969
4970 while (idx + count <= n) {
4971 unsigned long exception, i;
4972 int ret;
4973
4974 /* If we cannot allocate and insert this entry, or the
4975 * individual pages from this range, cancel updating the
4976 * sg_idx so that on this lookup we are forced to linearly
4977 * scan onwards, but on future lookups we will try the
4978 * insertion again (in which case we need to be careful of
4979 * the error return reporting that we have already inserted
4980 * this index).
4981 */
4982 ret = radix_tree_insert(&iter->radix, idx, sg);
4983 if (ret && ret != -EEXIST)
4984 goto scan;
4985
4986 exception =
4987 RADIX_TREE_EXCEPTIONAL_ENTRY |
4988 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
4989 for (i = 1; i < count; i++) {
4990 ret = radix_tree_insert(&iter->radix, idx + i,
4991 (void *)exception);
4992 if (ret && ret != -EEXIST)
4993 goto scan;
4994 }
4995
4996 idx += count;
4997 sg = ____sg_next(sg);
4998 count = __sg_page_count(sg);
4999 }
5000
5001scan:
5002 iter->sg_pos = sg;
5003 iter->sg_idx = idx;
5004
5005 mutex_unlock(&iter->lock);
5006
5007 if (unlikely(n < idx)) /* insertion completed by another thread */
5008 goto lookup;
5009
5010 /* In case we failed to insert the entry into the radixtree, we need
5011 * to look beyond the current sg.
5012 */
5013 while (idx + count <= n) {
5014 idx += count;
5015 sg = ____sg_next(sg);
5016 count = __sg_page_count(sg);
5017 }
5018
5019 *offset = n - idx;
5020 return sg;
5021
5022lookup:
5023 rcu_read_lock();
5024
5025 sg = radix_tree_lookup(&iter->radix, n);
5026 GEM_BUG_ON(!sg);
5027
5028 /* If this index is in the middle of multi-page sg entry,
5029 * the radixtree will contain an exceptional entry that points
5030 * to the start of that range. We will return the pointer to
5031 * the base page and the offset of this page within the
5032 * sg entry's range.
5033 */
5034 *offset = 0;
5035 if (unlikely(radix_tree_exception(sg))) {
5036 unsigned long base =
5037 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5038
5039 sg = radix_tree_lookup(&iter->radix, base);
5040 GEM_BUG_ON(!sg);
5041
5042 *offset = n - base;
5043 }
5044
5045 rcu_read_unlock();
5046
5047 return sg;
5048}
5049
5050struct page *
5051i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5052{
5053 struct scatterlist *sg;
5054 unsigned int offset;
5055
5056 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5057
5058 sg = i915_gem_object_get_sg(obj, n, &offset);
5059 return nth_page(sg_page(sg), offset);
5060}
5061
5062/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5063struct page *
5064i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5065 unsigned int n)
5066{
5067 struct page *page;
5068
5069 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005070 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005071 set_page_dirty(page);
5072
5073 return page;
5074}
5075
5076dma_addr_t
5077i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5078 unsigned long n)
5079{
5080 struct scatterlist *sg;
5081 unsigned int offset;
5082
5083 sg = i915_gem_object_get_sg(obj, n, &offset);
5084 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5085}
Chris Wilson935a2f72017-02-13 17:15:13 +00005086
5087#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5088#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005089#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005090#include "selftests/huge_gem_object.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005091#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005092#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005093#endif