blob: 54f27cabae2adc45e41f5e4fd1866d80d0b950f1 [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 Herrmann0de23972013-07-24 21:07:52 +020028#include <drm/drm_vma_manager.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010029#include <drm/drm_pci.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Chris Wilson6b5e90f2016-11-14 20:41:05 +000031#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000032#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010033#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000036#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020039#include <linux/dma-buf.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010040#include <linux/mman.h>
Eric Anholt673a3942008-07-30 12:06:12 -070041
Chris Wilson79ffac852019-04-24 21:07:17 +010042#include "gt/intel_engine_pm.h"
43#include "gt/intel_gt_pm.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +010044#include "gt/intel_mocs.h"
45#include "gt/intel_reset.h"
46#include "gt/intel_workarounds.h"
47
Chris Wilson9f588922019-01-16 15:33:04 +000048#include "i915_drv.h"
49#include "i915_gem_clflush.h"
50#include "i915_gemfs.h"
Chris Wilson23c3c3d2019-04-24 21:07:14 +010051#include "i915_gem_pm.h"
Chris Wilson9f588922019-01-16 15:33:04 +000052#include "i915_trace.h"
53#include "i915_vgpu.h"
54
55#include "intel_drv.h"
56#include "intel_frontbuffer.h"
Jani Nikula696173b2019-04-05 14:00:15 +030057#include "intel_pm.h"
Chris Wilson9f588922019-01-16 15:33:04 +000058
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010059static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010060
Chris Wilson2c225692013-08-09 12:26:45 +010061static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
62{
Chris Wilsone27ab732017-06-15 13:38:49 +010063 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053064 return false;
65
Chris Wilsonb8f55be2017-08-11 12:11:16 +010066 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010067 return true;
68
Chris Wilsonbd3d2252017-10-13 21:26:14 +010069 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010070}
71
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053072static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010073insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053074 struct drm_mm_node *node, u32 size)
75{
76 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010077 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000078 size, 0, I915_COLOR_UNEVICTABLE,
79 0, ggtt->mappable_end,
80 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053081}
82
83static void
84remove_mappable_node(struct drm_mm_node *node)
85{
86 drm_mm_remove_node(node);
87}
88
Chris Wilson73aa8082010-09-30 11:46:12 +010089/* some bookkeeping */
90static void i915_gem_info_add_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
99static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +0100100 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +0100101{
Daniel Vetterc20e8352013-07-24 22:40:23 +0200102 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100103 dev_priv->mm.object_count--;
104 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200105 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100106}
107
Eric Anholt673a3942008-07-30 12:06:12 -0700108int
Eric Anholt5a125c32008-10-22 21:40:13 -0700109i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000110 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700111{
Chris Wilson09d7e462019-01-28 10:23:53 +0000112 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300113 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100114 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800115 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700116
Chris Wilson09d7e462019-01-28 10:23:53 +0000117 mutex_lock(&ggtt->vm.mutex);
118
Chris Wilson82ad6442018-06-05 16:37:58 +0100119 pinned = ggtt->vm.reserved;
Chris Wilson499197d2019-01-28 10:23:52 +0000120 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100121 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100122 pinned += vma->node.size;
Chris Wilson09d7e462019-01-28 10:23:53 +0000123
124 mutex_unlock(&ggtt->vm.mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700125
Chris Wilson82ad6442018-06-05 16:37:58 +0100126 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400127 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000128
Eric Anholt5a125c32008-10-22 21:40:13 -0700129 return 0;
130}
131
Matthew Auldb91b09e2017-10-06 23:18:17 +0100132static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100133{
Al Viro93c76a32015-12-04 23:45:44 -0500134 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000135 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800136 struct sg_table *st;
137 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000138 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800139 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100140 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100141
Chris Wilson6a2c4232014-11-04 04:51:40 -0800142 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100143 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100144
Chris Wilsondbb43512016-12-07 13:34:11 +0000145 /* Always aligning to the object size, allows a single allocation
146 * to handle all possible callers, and given typical object sizes,
147 * the alignment of the buddy allocation will naturally match.
148 */
149 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300150 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000151 roundup_pow_of_two(obj->base.size));
152 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100153 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000154
155 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800156 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
157 struct page *page;
158 char *src;
159
160 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000161 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100162 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000163 goto err_phys;
164 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800165
166 src = kmap_atomic(page);
167 memcpy(vaddr, src, PAGE_SIZE);
168 drm_clflush_virt_range(vaddr, PAGE_SIZE);
169 kunmap_atomic(src);
170
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300171 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800172 vaddr += PAGE_SIZE;
173 }
174
Chris Wilsonc0336662016-05-06 15:40:21 +0100175 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800176
177 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100179 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000180 goto err_phys;
181 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800182
183 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
184 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100185 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000186 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800187 }
188
189 sg = st->sgl;
190 sg->offset = 0;
191 sg->length = obj->base.size;
192
Chris Wilsondbb43512016-12-07 13:34:11 +0000193 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800194 sg_dma_len(sg) = obj->base.size;
195
Chris Wilsondbb43512016-12-07 13:34:11 +0000196 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100197
Matthew Aulda5c081662017-10-06 23:18:18 +0100198 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100199
200 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000201
202err_phys:
203 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100204
205 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800206}
207
Chris Wilsone27ab732017-06-15 13:38:49 +0100208static void __start_cpu_write(struct drm_i915_gem_object *obj)
209{
Christian Königc0a51fd2018-02-16 13:43:38 +0100210 obj->read_domains = I915_GEM_DOMAIN_CPU;
211 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100212 if (cpu_write_needs_clflush(obj))
213 obj->cache_dirty = true;
214}
215
Chris Wilsonee8efa82019-03-31 10:46:20 +0100216void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000217__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000218 struct sg_table *pages,
219 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800220{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100221 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800222
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100223 if (obj->mm.madv == I915_MADV_DONTNEED)
224 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800225
Chris Wilsone5facdf2016-12-23 14:57:57 +0000226 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100227 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100228 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000229 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100230
Chris Wilsone27ab732017-06-15 13:38:49 +0100231 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100232}
233
234static void
235i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
236 struct sg_table *pages)
237{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000238 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100239
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100240 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500241 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800242 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100243 int i;
244
245 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800246 struct page *page;
247 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100248
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249 page = shmem_read_mapping_page(mapping, i);
250 if (IS_ERR(page))
251 continue;
252
253 dst = kmap_atomic(page);
254 drm_clflush_virt_range(vaddr, PAGE_SIZE);
255 memcpy(dst, vaddr, PAGE_SIZE);
256 kunmap_atomic(dst);
257
258 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100259 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100260 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300261 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100262 vaddr += PAGE_SIZE;
263 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100264 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100265 }
266
Chris Wilson03ac84f2016-10-28 13:58:36 +0100267 sg_free_table(pages);
268 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000269
270 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800271}
272
273static void
274i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
275{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100276 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800277}
278
279static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
280 .get_pages = i915_gem_object_get_pages_phys,
281 .put_pages = i915_gem_object_put_pages_phys,
282 .release = i915_gem_object_release_phys,
283};
284
Chris Wilson581ab1f2017-02-15 16:39:00 +0000285static const struct drm_i915_gem_object_ops i915_gem_object_ops;
286
Chris Wilson35a96112016-08-14 18:44:40 +0100287int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100288{
289 struct i915_vma *vma;
290 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100291 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100292
Chris Wilson02bef8f2016-08-14 18:44:41 +0100293 lockdep_assert_held(&obj->base.dev->struct_mutex);
294
295 /* Closed vma are removed from the obj->vma_list - but they may
296 * still have an active binding on the object. To remove those we
297 * must wait for all rendering to complete to the object (as unbinding
298 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100299 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000300 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100301 if (ret)
302 return ret;
303
Chris Wilson528cbd12019-01-28 10:23:54 +0000304 spin_lock(&obj->vma.lock);
305 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
306 struct i915_vma,
307 obj_link))) {
Chris Wilsonaa653a62016-08-04 07:52:27 +0100308 list_move_tail(&vma->obj_link, &still_in_list);
Chris Wilson528cbd12019-01-28 10:23:54 +0000309 spin_unlock(&obj->vma.lock);
310
Chris Wilsonaa653a62016-08-04 07:52:27 +0100311 ret = i915_vma_unbind(vma);
Chris Wilson528cbd12019-01-28 10:23:54 +0000312
313 spin_lock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100314 }
Chris Wilson528cbd12019-01-28 10:23:54 +0000315 list_splice(&still_in_list, &obj->vma.list);
316 spin_unlock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100317
318 return ret;
319}
320
Chris Wilsone95433c2016-10-28 13:58:27 +0100321static long
322i915_gem_object_wait_fence(struct dma_fence *fence,
323 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000324 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100325{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000326 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100327
328 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
329
330 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
331 return timeout;
332
333 if (!dma_fence_is_i915(fence))
334 return dma_fence_wait_timeout(fence,
335 flags & I915_WAIT_INTERRUPTIBLE,
336 timeout);
337
338 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000339 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100340 goto out;
341
Chris Wilsone61e0f52018-02-21 09:56:36 +0000342 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100343
344out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000345 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
346 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100347
Chris Wilsone95433c2016-10-28 13:58:27 +0100348 return timeout;
349}
350
351static long
352i915_gem_object_wait_reservation(struct reservation_object *resv,
353 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000354 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100355{
Chris Wilsone54ca972017-02-17 15:13:04 +0000356 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100357 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000358 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100359
360 if (flags & I915_WAIT_ALL) {
361 struct dma_fence **shared;
362 unsigned int count, i;
363 int ret;
364
365 ret = reservation_object_get_fences_rcu(resv,
366 &excl, &count, &shared);
367 if (ret)
368 return ret;
369
370 for (i = 0; i < count; i++) {
371 timeout = i915_gem_object_wait_fence(shared[i],
Chris Wilson62eb3c22019-02-13 09:25:04 +0000372 flags, timeout);
Chris Wilsond892e932017-02-12 21:53:43 +0000373 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100374 break;
375
376 dma_fence_put(shared[i]);
377 }
378
379 for (; i < count; i++)
380 dma_fence_put(shared[i]);
381 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000382
Chris Wilsonfa730552018-03-07 17:13:03 +0000383 /*
384 * If both shared fences and an exclusive fence exist,
385 * then by construction the shared fences must be later
386 * than the exclusive fence. If we successfully wait for
387 * all the shared fences, we know that the exclusive fence
388 * must all be signaled. If all the shared fences are
389 * signaled, we can prune the array and recover the
390 * floating references on the fences/requests.
391 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000392 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100393 } else {
394 excl = reservation_object_get_excl_rcu(resv);
395 }
396
Chris Wilsonfa730552018-03-07 17:13:03 +0000397 if (excl && timeout >= 0)
Chris Wilson62eb3c22019-02-13 09:25:04 +0000398 timeout = i915_gem_object_wait_fence(excl, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100399
400 dma_fence_put(excl);
401
Chris Wilsonfa730552018-03-07 17:13:03 +0000402 /*
403 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000404 * signaled and that the reservation object has not been changed (i.e.
405 * no new fences have been added).
406 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000407 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000408 if (reservation_object_trylock(resv)) {
409 if (!__read_seqcount_retry(&resv->seq, seq))
410 reservation_object_add_excl_fence(resv, NULL);
411 reservation_object_unlock(resv);
412 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000413 }
414
Chris Wilsone95433c2016-10-28 13:58:27 +0100415 return timeout;
416}
417
Chris Wilsonb7268c52018-04-18 19:40:52 +0100418static void __fence_set_priority(struct dma_fence *fence,
419 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000420{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000421 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000422 struct intel_engine_cs *engine;
423
Chris Wilsonc218ee02018-01-06 10:56:18 +0000424 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000425 return;
426
427 rq = to_request(fence);
428 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000429
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100430 local_bh_disable();
431 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000432 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100433 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000434 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100435 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000436}
437
Chris Wilsonb7268c52018-04-18 19:40:52 +0100438static void fence_set_priority(struct dma_fence *fence,
439 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000440{
441 /* Recurse once into a fence-array */
442 if (dma_fence_is_array(fence)) {
443 struct dma_fence_array *array = to_dma_fence_array(fence);
444 int i;
445
446 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100447 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000448 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100449 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000450 }
451}
452
453int
454i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
455 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100456 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000457{
458 struct dma_fence *excl;
459
460 if (flags & I915_WAIT_ALL) {
461 struct dma_fence **shared;
462 unsigned int count, i;
463 int ret;
464
465 ret = reservation_object_get_fences_rcu(obj->resv,
466 &excl, &count, &shared);
467 if (ret)
468 return ret;
469
470 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100471 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000472 dma_fence_put(shared[i]);
473 }
474
475 kfree(shared);
476 } else {
477 excl = reservation_object_get_excl_rcu(obj->resv);
478 }
479
480 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100481 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000482 dma_fence_put(excl);
483 }
484 return 0;
485}
486
Chris Wilson00e60f22016-08-04 16:32:40 +0100487/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100488 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100489 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100490 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
491 * @timeout: how long to wait
Chris Wilson00e60f22016-08-04 16:32:40 +0100492 */
493int
Chris Wilsone95433c2016-10-28 13:58:27 +0100494i915_gem_object_wait(struct drm_i915_gem_object *obj,
495 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000496 long timeout)
Chris Wilson00e60f22016-08-04 16:32:40 +0100497{
Chris Wilsone95433c2016-10-28 13:58:27 +0100498 might_sleep();
Chris Wilsone95433c2016-10-28 13:58:27 +0100499 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100500
Chris Wilson62eb3c22019-02-13 09:25:04 +0000501 timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100502 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100503}
504
Chris Wilson00731152014-05-21 12:42:56 +0100505static int
506i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
507 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100508 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100509{
Chris Wilson00731152014-05-21 12:42:56 +0100510 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300511 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800512
513 /* We manually control the domain here and pretend that it
514 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
515 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700516 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000517 if (copy_from_user(vaddr, user_data, args->size))
518 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100519
Chris Wilson6a2c4232014-11-04 04:51:40 -0800520 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000521 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200522
Chris Wilsond59b21e2017-02-22 11:40:49 +0000523 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000524 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100525}
526
Dave Airlieff72145b2011-02-07 12:16:14 +1000527static int
528i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000529 struct drm_i915_private *dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100530 u64 *size_p,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200531 u32 *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700532{
Chris Wilson05394f32010-11-08 19:18:58 +0000533 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300534 u32 handle;
Michał Winiarskie1634842019-03-26 18:02:18 +0100535 u64 size;
536 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700537
Michał Winiarskie1634842019-03-26 18:02:18 +0100538 size = round_up(*size_p, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200539 if (size == 0)
540 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700541
542 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000543 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100544 if (IS_ERR(obj))
545 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700546
Chris Wilson05394f32010-11-08 19:18:58 +0000547 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100548 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100549 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200550 if (ret)
551 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100552
Dave Airlieff72145b2011-02-07 12:16:14 +1000553 *handle_p = handle;
Chris Wilson99534022019-04-17 14:25:07 +0100554 *size_p = size;
Eric Anholt673a3942008-07-30 12:06:12 -0700555 return 0;
556}
557
Dave Airlieff72145b2011-02-07 12:16:14 +1000558int
559i915_gem_dumb_create(struct drm_file *file,
560 struct drm_device *dev,
561 struct drm_mode_create_dumb *args)
562{
563 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300564 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000565 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000566 return i915_gem_create(file, to_i915(dev),
Michał Winiarskie1634842019-03-26 18:02:18 +0100567 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000568}
569
Chris Wilsone27ab732017-06-15 13:38:49 +0100570static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
571{
572 return !(obj->cache_level == I915_CACHE_NONE ||
573 obj->cache_level == I915_CACHE_WT);
574}
575
Dave Airlieff72145b2011-02-07 12:16:14 +1000576/**
577 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100578 * @dev: drm device pointer
579 * @data: ioctl data blob
580 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000581 */
582int
583i915_gem_create_ioctl(struct drm_device *dev, void *data,
584 struct drm_file *file)
585{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000586 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000587 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200588
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000589 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100590
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000591 return i915_gem_create(file, dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100592 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000593}
594
Chris Wilsonef749212017-04-12 12:01:10 +0100595static inline enum fb_op_origin
596fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
597{
598 return (domain == I915_GEM_DOMAIN_GTT ?
599 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
600}
601
Chris Wilson7125397b2017-12-06 12:49:14 +0000602void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100603{
Chris Wilson538ef962019-01-14 14:21:18 +0000604 intel_wakeref_t wakeref;
605
Chris Wilson7125397b2017-12-06 12:49:14 +0000606 /*
607 * No actual flushing is required for the GTT write domain for reads
608 * from the GTT domain. Writes to it "immediately" go to main memory
609 * as far as we know, so there's no chipset flush. It also doesn't
610 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100611 *
612 * However, we do have to enforce the order so that all writes through
613 * the GTT land before any writes to the device, such as updates to
614 * the GATT itself.
615 *
616 * We also have to wait a bit for the writes to land from the GTT.
617 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
618 * timing. This issue has only been observed when switching quickly
619 * between GTT writes and CPU reads from inside the kernel on recent hw,
620 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000621 * system agents we cannot reproduce this behaviour, until Cannonlake
622 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100623 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000624
Chris Wilson900ccf32018-07-20 11:19:10 +0100625 wmb();
626
627 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
628 return;
629
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100630 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100631
Chris Wilsond4225a52019-01-14 14:21:23 +0000632 with_intel_runtime_pm(dev_priv, wakeref) {
633 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilson7125397b2017-12-06 12:49:14 +0000634
Chris Wilsond4225a52019-01-14 14:21:23 +0000635 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
Chris Wilson7125397b2017-12-06 12:49:14 +0000636
Chris Wilsond4225a52019-01-14 14:21:23 +0000637 spin_unlock_irq(&dev_priv->uncore.lock);
638 }
Chris Wilson7125397b2017-12-06 12:49:14 +0000639}
640
641static void
642flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
643{
644 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
645 struct i915_vma *vma;
646
Christian Königc0a51fd2018-02-16 13:43:38 +0100647 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000648 return;
649
Christian Königc0a51fd2018-02-16 13:43:38 +0100650 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100651 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000652 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100653
654 intel_fb_obj_flush(obj,
655 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000656
Chris Wilsone2189dd2017-12-07 21:14:07 +0000657 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000658 if (vma->iomap)
659 continue;
660
661 i915_vma_unset_ggtt_write(vma);
662 }
Chris Wilsonef749212017-04-12 12:01:10 +0100663 break;
664
Chris Wilsonadd00e62018-07-06 12:54:02 +0100665 case I915_GEM_DOMAIN_WC:
666 wmb();
667 break;
668
Chris Wilsonef749212017-04-12 12:01:10 +0100669 case I915_GEM_DOMAIN_CPU:
670 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
671 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100672
673 case I915_GEM_DOMAIN_RENDER:
674 if (gpu_write_needs_clflush(obj))
675 obj->cache_dirty = true;
676 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100677 }
678
Christian Königc0a51fd2018-02-16 13:43:38 +0100679 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100680}
681
Brad Volkin4c914c02014-02-18 10:15:45 -0800682/*
683 * Pins the specified object's pages and synchronizes the object with
684 * GPU accesses. Sets needs_clflush to non-zero if the caller should
685 * flush the object from the CPU cache.
686 */
687int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100688 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800689{
690 int ret;
691
Chris Wilsone95433c2016-10-28 13:58:27 +0100692 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800693
Chris Wilsone95433c2016-10-28 13:58:27 +0100694 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100695 if (!i915_gem_object_has_struct_page(obj))
696 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800697
Chris Wilsone95433c2016-10-28 13:58:27 +0100698 ret = i915_gem_object_wait(obj,
699 I915_WAIT_INTERRUPTIBLE |
700 I915_WAIT_LOCKED,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000701 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100702 if (ret)
703 return ret;
704
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100705 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100706 if (ret)
707 return ret;
708
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100709 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
710 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000711 ret = i915_gem_object_set_to_cpu_domain(obj, false);
712 if (ret)
713 goto err_unpin;
714 else
715 goto out;
716 }
717
Chris Wilsonef749212017-04-12 12:01:10 +0100718 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100719
Chris Wilson43394c72016-08-18 17:16:47 +0100720 /* If we're not in the cpu read domain, set ourself into the gtt
721 * read domain and manually flush cachelines (if required). This
722 * optimizes for the case when the gpu will dirty the data
723 * anyway again before the next pread happens.
724 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100725 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100726 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000727 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800728
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000729out:
Chris Wilson97649512016-08-18 17:16:50 +0100730 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100731 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100732
733err_unpin:
734 i915_gem_object_unpin_pages(obj);
735 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100736}
737
738int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
739 unsigned int *needs_clflush)
740{
741 int ret;
742
Chris Wilsone95433c2016-10-28 13:58:27 +0100743 lockdep_assert_held(&obj->base.dev->struct_mutex);
744
Chris Wilson43394c72016-08-18 17:16:47 +0100745 *needs_clflush = 0;
746 if (!i915_gem_object_has_struct_page(obj))
747 return -ENODEV;
748
Chris Wilsone95433c2016-10-28 13:58:27 +0100749 ret = i915_gem_object_wait(obj,
750 I915_WAIT_INTERRUPTIBLE |
751 I915_WAIT_LOCKED |
752 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000753 MAX_SCHEDULE_TIMEOUT);
Chris Wilson43394c72016-08-18 17:16:47 +0100754 if (ret)
755 return ret;
756
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100757 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100758 if (ret)
759 return ret;
760
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100761 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
762 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000763 ret = i915_gem_object_set_to_cpu_domain(obj, true);
764 if (ret)
765 goto err_unpin;
766 else
767 goto out;
768 }
769
Chris Wilsonef749212017-04-12 12:01:10 +0100770 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100771
Chris Wilson43394c72016-08-18 17:16:47 +0100772 /* If we're not in the cpu write domain, set ourself into the
773 * gtt write domain and manually flush cachelines (as required).
774 * This optimizes for the case when the gpu will use the data
775 * right away and we therefore have to clflush anyway.
776 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100777 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000778 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100779
Chris Wilsone27ab732017-06-15 13:38:49 +0100780 /*
781 * Same trick applies to invalidate partially written
782 * cachelines read before writing.
783 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100784 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100785 *needs_clflush |= CLFLUSH_BEFORE;
786 }
Chris Wilson43394c72016-08-18 17:16:47 +0100787
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000788out:
Chris Wilson43394c72016-08-18 17:16:47 +0100789 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100790 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100791 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100792 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100793
794err_unpin:
795 i915_gem_object_unpin_pages(obj);
796 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800797}
798
Daniel Vetterd174bd62012-03-25 19:47:40 +0200799static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000800shmem_pread(struct page *page, int offset, int len, char __user *user_data,
801 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200802{
803 char *vaddr;
804 int ret;
805
806 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200807
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000808 if (needs_clflush)
809 drm_clflush_virt_range(vaddr + offset, len);
810
811 ret = __copy_to_user(user_data, vaddr + offset, len);
812
Daniel Vetterd174bd62012-03-25 19:47:40 +0200813 kunmap(page);
814
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000815 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100816}
817
818static int
819i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
820 struct drm_i915_gem_pread *args)
821{
822 char __user *user_data;
823 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100824 unsigned int needs_clflush;
825 unsigned int idx, offset;
826 int ret;
827
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100828 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
829 if (ret)
830 return ret;
831
832 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
833 mutex_unlock(&obj->base.dev->struct_mutex);
834 if (ret)
835 return ret;
836
837 remain = args->size;
838 user_data = u64_to_user_ptr(args->data_ptr);
839 offset = offset_in_page(args->offset);
840 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
841 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100842 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100843
844 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100845 needs_clflush);
846 if (ret)
847 break;
848
849 remain -= length;
850 user_data += length;
851 offset = 0;
852 }
853
854 i915_gem_obj_finish_shmem_access(obj);
855 return ret;
856}
857
858static inline bool
859gtt_user_read(struct io_mapping *mapping,
860 loff_t base, int offset,
861 char __user *user_data, int length)
862{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300863 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100864 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530865
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530866 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300867 vaddr = io_mapping_map_atomic_wc(mapping, base);
868 unwritten = __copy_to_user_inatomic(user_data,
869 (void __force *)vaddr + offset,
870 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100871 io_mapping_unmap_atomic(vaddr);
872 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300873 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
874 unwritten = copy_to_user(user_data,
875 (void __force *)vaddr + offset,
876 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100877 io_mapping_unmap(vaddr);
878 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530879 return unwritten;
880}
881
882static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100883i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
884 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530885{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100886 struct drm_i915_private *i915 = to_i915(obj->base.dev);
887 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000888 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530889 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100890 struct i915_vma *vma;
891 void __user *user_data;
892 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530893 int ret;
894
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100895 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
896 if (ret)
897 return ret;
898
Chris Wilson538ef962019-01-14 14:21:18 +0000899 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100900 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +0100901 PIN_MAPPABLE |
902 PIN_NONFAULT |
903 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +0100904 if (!IS_ERR(vma)) {
905 node.start = i915_ggtt_offset(vma);
906 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +0100907 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +0100908 if (ret) {
909 i915_vma_unpin(vma);
910 vma = ERR_PTR(ret);
911 }
912 }
Chris Wilson058d88c2016-08-15 10:49:06 +0100913 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100914 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530915 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100916 goto out_unlock;
917 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530918 }
919
920 ret = i915_gem_object_set_to_gtt_domain(obj, false);
921 if (ret)
922 goto out_unpin;
923
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100924 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530925
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100926 user_data = u64_to_user_ptr(args->data_ptr);
927 remain = args->size;
928 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530929
930 while (remain > 0) {
931 /* Operation in this page
932 *
933 * page_base = page offset within aperture
934 * page_offset = offset within page
935 * page_length = bytes to copy for this page
936 */
937 u32 page_base = node.start;
938 unsigned page_offset = offset_in_page(offset);
939 unsigned page_length = PAGE_SIZE - page_offset;
940 page_length = remain < page_length ? remain : page_length;
941 if (node.allocated) {
942 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +0100943 ggtt->vm.insert_page(&ggtt->vm,
944 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
945 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530946 wmb();
947 } else {
948 page_base += offset & PAGE_MASK;
949 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100950
Matthew Auld73ebd502017-12-11 15:18:20 +0000951 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100952 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530953 ret = -EFAULT;
954 break;
955 }
956
957 remain -= page_length;
958 user_data += page_length;
959 offset += page_length;
960 }
961
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100962 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530963out_unpin:
964 if (node.allocated) {
965 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +0100966 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530967 remove_mappable_node(&node);
968 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +0100969 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530970 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100971out_unlock:
Chris Wilson538ef962019-01-14 14:21:18 +0000972 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100973 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100974
Eric Anholteb014592009-03-10 11:44:52 -0700975 return ret;
976}
977
Eric Anholt673a3942008-07-30 12:06:12 -0700978/**
979 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100980 * @dev: drm device pointer
981 * @data: ioctl data blob
982 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -0700983 *
984 * On error, the contents of *data are undefined.
985 */
986int
987i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000988 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700989{
990 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000991 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100992 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700993
Chris Wilson51311d02010-11-17 09:10:42 +0000994 if (args->size == 0)
995 return 0;
996
Linus Torvalds96d4f262019-01-03 18:57:57 -0800997 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000998 args->size))
999 return -EFAULT;
1000
Chris Wilson03ac0642016-07-20 13:31:51 +01001001 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001002 if (!obj)
1003 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001004
Chris Wilson7dcd2492010-09-26 20:21:44 +01001005 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001006 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001007 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001008 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001009 }
1010
Chris Wilsondb53a302011-02-03 11:57:46 +00001011 trace_i915_gem_object_pread(obj, args->offset, args->size);
1012
Chris Wilsone95433c2016-10-28 13:58:27 +01001013 ret = i915_gem_object_wait(obj,
1014 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001015 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001016 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001017 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001018
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001019 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001020 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001021 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001022
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001023 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001024 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001025 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301026
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001027 i915_gem_object_unpin_pages(obj);
1028out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001029 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001030 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001031}
1032
Keith Packard0839ccb2008-10-30 19:38:48 -07001033/* This is the fast write path which cannot handle
1034 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001035 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001036
Chris Wilsonfe115622016-10-28 13:58:40 +01001037static inline bool
1038ggtt_write(struct io_mapping *mapping,
1039 loff_t base, int offset,
1040 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001041{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001042 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001043 unsigned long unwritten;
1044
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001045 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001046 vaddr = io_mapping_map_atomic_wc(mapping, base);
1047 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001048 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001049 io_mapping_unmap_atomic(vaddr);
1050 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001051 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1052 unwritten = copy_from_user((void __force *)vaddr + offset,
1053 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001054 io_mapping_unmap(vaddr);
1055 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001056
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001057 return unwritten;
1058}
1059
Eric Anholt3de09aa2009-03-09 09:42:23 -07001060/**
1061 * This is the fast pwrite path, where we copy the data directly from the
1062 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001063 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001064 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001065 */
Eric Anholt673a3942008-07-30 12:06:12 -07001066static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001067i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1068 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001069{
Chris Wilsonfe115622016-10-28 13:58:40 +01001070 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301071 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001072 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301073 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001074 struct i915_vma *vma;
1075 u64 remain, offset;
1076 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301077 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301078
Chris Wilsonfe115622016-10-28 13:58:40 +01001079 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1080 if (ret)
1081 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001082
Chris Wilson8bd818152017-10-19 07:37:33 +01001083 if (i915_gem_object_has_struct_page(obj)) {
1084 /*
1085 * Avoid waking the device up if we can fallback, as
1086 * waking/resuming is very slow (worst-case 10-100 ms
1087 * depending on PCI sleeps and our own resume time).
1088 * This easily dwarfs any performance advantage from
1089 * using the cache bypass of indirect GGTT access.
1090 */
Chris Wilson538ef962019-01-14 14:21:18 +00001091 wakeref = intel_runtime_pm_get_if_in_use(i915);
1092 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +01001093 ret = -EFAULT;
1094 goto out_unlock;
1095 }
1096 } else {
1097 /* No backing pages, no fallback, we must force GGTT access */
Chris Wilson538ef962019-01-14 14:21:18 +00001098 wakeref = intel_runtime_pm_get(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001099 }
1100
Chris Wilson058d88c2016-08-15 10:49:06 +01001101 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001102 PIN_MAPPABLE |
1103 PIN_NONFAULT |
1104 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001105 if (!IS_ERR(vma)) {
1106 node.start = i915_ggtt_offset(vma);
1107 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001108 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001109 if (ret) {
1110 i915_vma_unpin(vma);
1111 vma = ERR_PTR(ret);
1112 }
1113 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001114 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001115 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301116 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001117 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001118 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301119 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001120
1121 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1122 if (ret)
1123 goto out_unpin;
1124
Chris Wilsonfe115622016-10-28 13:58:40 +01001125 mutex_unlock(&i915->drm.struct_mutex);
1126
Chris Wilsonb19482d2016-08-18 17:16:43 +01001127 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001128
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301129 user_data = u64_to_user_ptr(args->data_ptr);
1130 offset = args->offset;
1131 remain = args->size;
1132 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001133 /* Operation in this page
1134 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001135 * page_base = page offset within aperture
1136 * page_offset = offset within page
1137 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001138 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301139 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001140 unsigned int page_offset = offset_in_page(offset);
1141 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301142 page_length = remain < page_length ? remain : page_length;
1143 if (node.allocated) {
1144 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001145 ggtt->vm.insert_page(&ggtt->vm,
1146 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1147 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301148 wmb(); /* flush modifications to the GGTT (insert_page) */
1149 } else {
1150 page_base += offset & PAGE_MASK;
1151 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001152 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001153 * source page isn't available. Return the error and we'll
1154 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301155 * If the object is non-shmem backed, we retry again with the
1156 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001157 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001158 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001159 user_data, page_length)) {
1160 ret = -EFAULT;
1161 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001162 }
Eric Anholt673a3942008-07-30 12:06:12 -07001163
Keith Packard0839ccb2008-10-30 19:38:48 -07001164 remain -= page_length;
1165 user_data += page_length;
1166 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001167 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001168 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001169
1170 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001171out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301172 if (node.allocated) {
1173 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001174 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301175 remove_mappable_node(&node);
1176 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001177 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301178 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001179out_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001180 intel_runtime_pm_put(i915, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +01001181out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001182 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001183 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001184}
1185
Chris Wilsonfe115622016-10-28 13:58:40 +01001186/* Per-page copy function for the shmem pwrite fastpath.
1187 * Flushes invalid cachelines before writing to the target if
1188 * needs_clflush_before is set and flushes out any written cachelines after
1189 * writing if needs_clflush is set.
1190 */
Eric Anholt40123c12009-03-09 13:42:30 -07001191static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001192shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001193 bool needs_clflush_before,
1194 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001195{
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001196 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +01001197 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001198
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001199 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +01001200
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001201 if (needs_clflush_before)
1202 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001203
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001204 ret = __copy_from_user(vaddr + offset, user_data, len);
1205 if (!ret && needs_clflush_after)
1206 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001207
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001208 kunmap(page);
1209
1210 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +01001211}
1212
1213static int
1214i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1215 const struct drm_i915_gem_pwrite *args)
1216{
1217 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1218 void __user *user_data;
1219 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +01001220 unsigned int partial_cacheline_write;
1221 unsigned int needs_clflush;
1222 unsigned int offset, idx;
1223 int ret;
1224
1225 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001226 if (ret)
1227 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001228
Chris Wilsonfe115622016-10-28 13:58:40 +01001229 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1230 mutex_unlock(&i915->drm.struct_mutex);
1231 if (ret)
1232 return ret;
1233
Chris Wilsonfe115622016-10-28 13:58:40 +01001234 /* If we don't overwrite a cacheline completely we need to be
1235 * careful to have up-to-date data by first clflushing. Don't
1236 * overcomplicate things and flush the entire patch.
1237 */
1238 partial_cacheline_write = 0;
1239 if (needs_clflush & CLFLUSH_BEFORE)
1240 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1241
Chris Wilson43394c72016-08-18 17:16:47 +01001242 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001243 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001244 offset = offset_in_page(args->offset);
1245 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1246 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001247 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001248
Chris Wilsonfe115622016-10-28 13:58:40 +01001249 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001250 (offset | length) & partial_cacheline_write,
1251 needs_clflush & CLFLUSH_AFTER);
1252 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001253 break;
1254
Chris Wilsonfe115622016-10-28 13:58:40 +01001255 remain -= length;
1256 user_data += length;
1257 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001258 }
1259
Chris Wilsond59b21e2017-02-22 11:40:49 +00001260 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001261 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001262 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001263}
1264
1265/**
1266 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001267 * @dev: drm device
1268 * @data: ioctl data blob
1269 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001270 *
1271 * On error, the contents of the buffer that were to be modified are undefined.
1272 */
1273int
1274i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001275 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001276{
1277 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001278 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001279 int ret;
1280
1281 if (args->size == 0)
1282 return 0;
1283
Linus Torvalds96d4f262019-01-03 18:57:57 -08001284 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +00001285 return -EFAULT;
1286
Chris Wilson03ac0642016-07-20 13:31:51 +01001287 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001288 if (!obj)
1289 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001290
Chris Wilson7dcd2492010-09-26 20:21:44 +01001291 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001292 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001293 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001294 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001295 }
1296
Chris Wilsonf8c1cce2018-07-12 19:53:14 +01001297 /* Writes not allowed into this read-only object */
1298 if (i915_gem_object_is_readonly(obj)) {
1299 ret = -EINVAL;
1300 goto err;
1301 }
1302
Chris Wilsondb53a302011-02-03 11:57:46 +00001303 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1304
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001305 ret = -ENODEV;
1306 if (obj->ops->pwrite)
1307 ret = obj->ops->pwrite(obj, args);
1308 if (ret != -ENODEV)
1309 goto err;
1310
Chris Wilsone95433c2016-10-28 13:58:27 +01001311 ret = i915_gem_object_wait(obj,
1312 I915_WAIT_INTERRUPTIBLE |
1313 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001314 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001315 if (ret)
1316 goto err;
1317
Chris Wilsonfe115622016-10-28 13:58:40 +01001318 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001319 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001320 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001321
Daniel Vetter935aaa62012-03-25 19:47:35 +02001322 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001323 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1324 * it would end up going through the fenced access, and we'll get
1325 * different detiling behavior between reading and writing.
1326 * pread/pwrite currently are reading and writing from the CPU
1327 * perspective, requiring manual detiling by the client.
1328 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001329 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001330 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001331 /* Note that the gtt paths might fail with non-page-backed user
1332 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001333 * textures). Fallback to the shmem path in that case.
1334 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001335 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001336
Chris Wilsond1054ee2016-07-16 18:42:36 +01001337 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001338 if (obj->phys_handle)
1339 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301340 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001341 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001342 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001343
Chris Wilsonfe115622016-10-28 13:58:40 +01001344 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001345err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001346 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001347 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001348}
1349
Chris Wilson40e62d52016-10-28 13:58:41 +01001350static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1351{
Chris Wilson09d7e462019-01-28 10:23:53 +00001352 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson40e62d52016-10-28 13:58:41 +01001353 struct list_head *list;
1354 struct i915_vma *vma;
1355
Chris Wilsonf2123812017-10-16 12:40:37 +01001356 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1357
Chris Wilson09d7e462019-01-28 10:23:53 +00001358 mutex_lock(&i915->ggtt.vm.mutex);
Chris Wilsone2189dd2017-12-07 21:14:07 +00001359 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001360 if (!drm_mm_node_allocated(&vma->node))
1361 continue;
1362
Chris Wilson499197d2019-01-28 10:23:52 +00001363 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001364 }
Chris Wilson09d7e462019-01-28 10:23:53 +00001365 mutex_unlock(&i915->ggtt.vm.mutex);
Chris Wilson40e62d52016-10-28 13:58:41 +01001366
Chris Wilsonf2123812017-10-16 12:40:37 +01001367 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001368 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001369 list_move_tail(&obj->mm.link, list);
1370 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001371}
1372
Eric Anholt673a3942008-07-30 12:06:12 -07001373/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001374 * Called when user space prepares to use an object with the CPU, either
1375 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001376 * @dev: drm device
1377 * @data: ioctl data blob
1378 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001379 */
1380int
1381i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001382 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001383{
1384 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001385 struct drm_i915_gem_object *obj;
Jani Nikula739f3ab2019-01-16 11:15:19 +02001386 u32 read_domains = args->read_domains;
1387 u32 write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001388 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001389
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001390 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001391 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001392 return -EINVAL;
1393
Chris Wilson754a2542019-03-21 16:19:08 +00001394 /*
1395 * Having something in the write domain implies it's in the read
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001396 * domain, and only that read domain. Enforce that in the request.
1397 */
Chris Wilson754a2542019-03-21 16:19:08 +00001398 if (write_domain && read_domains != write_domain)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001399 return -EINVAL;
1400
Chris Wilson754a2542019-03-21 16:19:08 +00001401 if (!read_domains)
1402 return 0;
1403
Chris Wilson03ac0642016-07-20 13:31:51 +01001404 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001405 if (!obj)
1406 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001407
Chris Wilson754a2542019-03-21 16:19:08 +00001408 /*
1409 * Already in the desired write domain? Nothing for us to do!
1410 *
1411 * We apply a little bit of cunning here to catch a broader set of
1412 * no-ops. If obj->write_domain is set, we must be in the same
1413 * obj->read_domains, and only that domain. Therefore, if that
1414 * obj->write_domain matches the request read_domains, we are
1415 * already in the same read/write domain and can skip the operation,
1416 * without having to further check the requested write_domain.
1417 */
1418 if (READ_ONCE(obj->write_domain) == read_domains) {
1419 err = 0;
1420 goto out;
1421 }
1422
1423 /*
1424 * Try to flush the object off the GPU without holding the lock.
Chris Wilson3236f572012-08-24 09:35:09 +01001425 * We will repeat the flush holding the lock in the normal manner
1426 * to catch cases where we are gazumped.
1427 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001428 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001429 I915_WAIT_INTERRUPTIBLE |
Chris Wilsone9eaf822018-10-01 15:47:55 +01001430 I915_WAIT_PRIORITY |
Chris Wilsone95433c2016-10-28 13:58:27 +01001431 (write_domain ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00001432 MAX_SCHEDULE_TIMEOUT);
Chris Wilson40e62d52016-10-28 13:58:41 +01001433 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001434 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001435
Tina Zhanga03f3952017-11-14 10:25:13 +00001436 /*
1437 * Proxy objects do not control access to the backing storage, ergo
1438 * they cannot be used as a means to manipulate the cache domain
1439 * tracking for that backing storage. The proxy object is always
1440 * considered to be outside of any cache domain.
1441 */
1442 if (i915_gem_object_is_proxy(obj)) {
1443 err = -ENXIO;
1444 goto out;
1445 }
1446
1447 /*
1448 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001449 * direct access in memory with previous cached writes through
1450 * shmemfs and that our cache domain tracking remains valid.
1451 * For example, if the obj->filp was moved to swap without us
1452 * being notified and releasing the pages, we would mistakenly
1453 * continue to assume that the obj remained out of the CPU cached
1454 * domain.
1455 */
1456 err = i915_gem_object_pin_pages(obj);
1457 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001458 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001459
1460 err = i915_mutex_lock_interruptible(dev);
1461 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001462 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001463
Chris Wilsone22d8e32017-04-12 12:01:11 +01001464 if (read_domains & I915_GEM_DOMAIN_WC)
1465 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1466 else if (read_domains & I915_GEM_DOMAIN_GTT)
1467 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301468 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001469 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001470
1471 /* And bump the LRU for this access */
1472 i915_gem_object_bump_inactive_ggtt(obj);
1473
1474 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001475
Daniel Vetter031b6982015-06-26 19:35:16 +02001476 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001477 intel_fb_obj_invalidate(obj,
1478 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001479
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001480out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001481 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001482out:
1483 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001484 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001485}
1486
1487/**
1488 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001489 * @dev: drm device
1490 * @data: ioctl data blob
1491 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001492 */
1493int
1494i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001495 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001496{
1497 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001498 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001499
Chris Wilson03ac0642016-07-20 13:31:51 +01001500 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001501 if (!obj)
1502 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001503
Tina Zhanga03f3952017-11-14 10:25:13 +00001504 /*
1505 * Proxy objects are barred from CPU access, so there is no
1506 * need to ban sw_finish as it is a nop.
1507 */
1508
Eric Anholt673a3942008-07-30 12:06:12 -07001509 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001510 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001511 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001512
1513 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001514}
1515
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001516static inline bool
1517__vma_matches(struct vm_area_struct *vma, struct file *filp,
1518 unsigned long addr, unsigned long size)
1519{
1520 if (vma->vm_file != filp)
1521 return false;
1522
Tvrtko Ursulina90e1942019-03-05 11:04:08 +00001523 return vma->vm_start == addr &&
1524 (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size);
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001525}
1526
Eric Anholt673a3942008-07-30 12:06:12 -07001527/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001528 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1529 * it is mapped to.
1530 * @dev: drm device
1531 * @data: ioctl data blob
1532 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001533 *
1534 * While the mapping holds a reference on the contents of the object, it doesn't
1535 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001536 *
1537 * IMPORTANT:
1538 *
1539 * DRM driver writers who look a this function as an example for how to do GEM
1540 * mmap support, please don't implement mmap support like here. The modern way
1541 * to implement DRM mmap support is with an mmap offset ioctl (like
1542 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1543 * That way debug tooling like valgrind will understand what's going on, hiding
1544 * the mmap call in a driver private ioctl will break that. The i915 driver only
1545 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001546 */
1547int
1548i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001549 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001550{
1551 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001552 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001553 unsigned long addr;
1554
Akash Goel1816f922015-01-02 16:29:30 +05301555 if (args->flags & ~(I915_MMAP_WC))
1556 return -EINVAL;
1557
Borislav Petkov568a58e2016-03-29 17:42:01 +02001558 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301559 return -ENODEV;
1560
Chris Wilson03ac0642016-07-20 13:31:51 +01001561 obj = i915_gem_object_lookup(file, args->handle);
1562 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001563 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001564
Daniel Vetter1286ff72012-05-10 15:25:09 +02001565 /* prime objects have no backing filp to GEM mmap
1566 * pages from.
1567 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001568 if (!obj->base.filp) {
Chris Wilson794a11c2019-03-14 07:58:29 +00001569 addr = -ENXIO;
1570 goto err;
1571 }
1572
1573 if (range_overflows(args->offset, args->size, (u64)obj->base.size)) {
1574 addr = -EINVAL;
1575 goto err;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001576 }
1577
Chris Wilson03ac0642016-07-20 13:31:51 +01001578 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001579 PROT_READ | PROT_WRITE, MAP_SHARED,
1580 args->offset);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001581 if (IS_ERR_VALUE(addr))
1582 goto err;
1583
Akash Goel1816f922015-01-02 16:29:30 +05301584 if (args->flags & I915_MMAP_WC) {
1585 struct mm_struct *mm = current->mm;
1586 struct vm_area_struct *vma;
1587
Michal Hocko80a89a52016-05-23 16:26:11 -07001588 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilson794a11c2019-03-14 07:58:29 +00001589 addr = -EINTR;
1590 goto err;
Michal Hocko80a89a52016-05-23 16:26:11 -07001591 }
Akash Goel1816f922015-01-02 16:29:30 +05301592 vma = find_vma(mm, addr);
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001593 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
Akash Goel1816f922015-01-02 16:29:30 +05301594 vma->vm_page_prot =
1595 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1596 else
1597 addr = -ENOMEM;
1598 up_write(&mm->mmap_sem);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001599 if (IS_ERR_VALUE(addr))
1600 goto err;
Chris Wilsonaeecc962016-06-17 14:46:39 -03001601
1602 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001603 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301604 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001605 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001606
Jani Nikula739f3ab2019-01-16 11:15:19 +02001607 args->addr_ptr = (u64)addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001608 return 0;
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001609
1610err:
1611 i915_gem_object_put(obj);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001612 return addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001613}
1614
Chris Wilsond899ace2018-07-25 16:54:47 +01001615static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
Chris Wilson03af84f2016-08-18 17:17:01 +01001616{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001617 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001618}
1619
Jesse Barnesde151cf2008-11-12 10:03:55 -08001620/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001621 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1622 *
1623 * A history of the GTT mmap interface:
1624 *
1625 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1626 * aligned and suitable for fencing, and still fit into the available
1627 * mappable space left by the pinned display objects. A classic problem
1628 * we called the page-fault-of-doom where we would ping-pong between
1629 * two objects that could not fit inside the GTT and so the memcpy
1630 * would page one object in at the expense of the other between every
1631 * single byte.
1632 *
1633 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1634 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1635 * object is too large for the available space (or simply too large
1636 * for the mappable aperture!), a view is created instead and faulted
1637 * into userspace. (This view is aligned and sized appropriately for
1638 * fenced access.)
1639 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001640 * 2 - Recognise WC as a separate cache domain so that we can flush the
1641 * delayed writes via GTT before performing direct access via WC.
1642 *
Chris Wilsona679f582019-03-21 16:19:07 +00001643 * 3 - Remove implicit set-domain(GTT) and synchronisation on initial
1644 * pagefault; swapin remains transparent.
1645 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001646 * Restrictions:
1647 *
1648 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1649 * hangs on some architectures, corruption on others. An attempt to service
1650 * a GTT page fault from a snoopable object will generate a SIGBUS.
1651 *
1652 * * the object must be able to fit into RAM (physical memory, though no
1653 * limited to the mappable aperture).
1654 *
1655 *
1656 * Caveats:
1657 *
1658 * * a new GTT page fault will synchronize rendering from the GPU and flush
1659 * all data to system memory. Subsequent access will not be synchronized.
1660 *
1661 * * all mappings are revoked on runtime device suspend.
1662 *
1663 * * there are only 8, 16 or 32 fence registers to share between all users
1664 * (older machines require fence register for display and blitter access
1665 * as well). Contention of the fence registers will cause the previous users
1666 * to be unmapped and any new access will generate new page faults.
1667 *
1668 * * running out of memory while servicing a fault may generate a SIGBUS,
1669 * rather than the expected SIGSEGV.
1670 */
1671int i915_gem_mmap_gtt_version(void)
1672{
Chris Wilsona679f582019-03-21 16:19:07 +00001673 return 3;
Chris Wilson4cc69072016-08-25 19:05:19 +01001674}
1675
Chris Wilson2d4281b2017-01-10 09:56:32 +00001676static inline struct i915_ggtt_view
Chris Wilsond899ace2018-07-25 16:54:47 +01001677compute_partial_view(const struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001678 pgoff_t page_offset,
1679 unsigned int chunk)
1680{
1681 struct i915_ggtt_view view;
1682
1683 if (i915_gem_object_is_tiled(obj))
1684 chunk = roundup(chunk, tile_row_pages(obj));
1685
Chris Wilson2d4281b2017-01-10 09:56:32 +00001686 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001687 view.partial.offset = rounddown(page_offset, chunk);
1688 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001689 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001690 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001691
1692 /* If the partial covers the entire object, just create a normal VMA. */
1693 if (chunk >= obj->base.size >> PAGE_SHIFT)
1694 view.type = I915_GGTT_VIEW_NORMAL;
1695
1696 return view;
1697}
1698
Chris Wilson4cc69072016-08-25 19:05:19 +01001699/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001700 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001701 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001702 *
1703 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1704 * from userspace. The fault handler takes care of binding the object to
1705 * the GTT (if needed), allocating and programming a fence register (again,
1706 * only if needed based on whether the old reg is still valid or the object
1707 * is tiled) and inserting a new PTE into the faulting process.
1708 *
1709 * Note that the faulting process may involve evicting existing objects
1710 * from the GTT and/or fence registers to make room. So performance may
1711 * suffer if the GTT working set is large or there are few fence registers
1712 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001713 *
1714 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1715 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001716 */
Chris Wilson52137012018-06-06 22:45:20 +01001717vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001718{
Chris Wilson420980c2018-06-05 14:57:46 +01001719#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08001720 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001721 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001722 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001723 struct drm_i915_private *dev_priv = to_i915(dev);
1724 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonaae7c062018-09-03 09:33:34 +01001725 bool write = area->vm_flags & VM_WRITE;
Chris Wilson538ef962019-01-14 14:21:18 +00001726 intel_wakeref_t wakeref;
Chris Wilson058d88c2016-08-15 10:49:06 +01001727 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001728 pgoff_t page_offset;
Chris Wilson2caffbf2019-02-08 15:37:03 +00001729 int srcu;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001730 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001731
Chris Wilson3e977ac2018-07-12 19:53:13 +01001732 /* Sanity check that we allow writing into this object */
1733 if (i915_gem_object_is_readonly(obj) && write)
1734 return VM_FAULT_SIGBUS;
1735
Jesse Barnesde151cf2008-11-12 10:03:55 -08001736 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001737 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001738
Chris Wilsondb53a302011-02-03 11:57:46 +00001739 trace_i915_gem_object_fault(obj, page_offset, true, write);
1740
Chris Wilson40e62d52016-10-28 13:58:41 +01001741 ret = i915_gem_object_pin_pages(obj);
1742 if (ret)
1743 goto err;
1744
Chris Wilson538ef962019-01-14 14:21:18 +00001745 wakeref = intel_runtime_pm_get(dev_priv);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001746
Chris Wilson43a8f682019-02-21 10:29:19 +00001747 srcu = i915_reset_trylock(dev_priv);
1748 if (srcu < 0) {
1749 ret = srcu;
1750 goto err_rpm;
1751 }
1752
Chris Wilsonb8f90962016-08-05 10:14:07 +01001753 ret = i915_mutex_lock_interruptible(dev);
1754 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001755 goto err_reset;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001756
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001757 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001758 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001759 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001760 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001761 }
1762
Chris Wilsona61007a2016-08-18 17:17:02 +01001763 /* Now pin it into the GTT as needed */
Chris Wilson7e7367d2018-06-30 10:05:09 +01001764 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1765 PIN_MAPPABLE |
1766 PIN_NONBLOCK |
1767 PIN_NONFAULT);
Chris Wilsona61007a2016-08-18 17:17:02 +01001768 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001769 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001770 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001771 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilson7e7367d2018-06-30 10:05:09 +01001772 unsigned int flags;
Chris Wilsonaa136d92016-08-18 17:17:03 +01001773
Chris Wilson7e7367d2018-06-30 10:05:09 +01001774 flags = PIN_MAPPABLE;
1775 if (view.type == I915_GGTT_VIEW_NORMAL)
1776 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
1777
1778 /*
1779 * Userspace is now writing through an untracked VMA, abandon
Chris Wilson50349242016-08-18 17:17:04 +01001780 * all hope that the hardware is able to track future writes.
1781 */
1782 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1783
Chris Wilson7e7367d2018-06-30 10:05:09 +01001784 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1785 if (IS_ERR(vma) && !view.type) {
1786 flags = PIN_MAPPABLE;
1787 view.type = I915_GGTT_VIEW_PARTIAL;
1788 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1789 }
Chris Wilsona61007a2016-08-18 17:17:02 +01001790 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001791 if (IS_ERR(vma)) {
1792 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001793 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001794 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001795
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001796 ret = i915_vma_pin_fence(vma);
1797 if (ret)
1798 goto err_unpin;
1799
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001800 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001801 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001802 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001803 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001804 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001805 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001806 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001807 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001808
Chris Wilsona65adaf2017-10-09 09:43:57 +01001809 /* Mark as being mmapped into userspace for later revocation */
1810 assert_rpm_wakelock_held(dev_priv);
1811 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1812 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1813 GEM_BUG_ON(!obj->userfault_count);
1814
Chris Wilson7125397b2017-12-06 12:49:14 +00001815 i915_vma_set_ggtt_write(vma);
1816
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001817err_fence:
1818 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001819err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001820 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001821err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001822 mutex_unlock(&dev->struct_mutex);
Chris Wilson43a8f682019-02-21 10:29:19 +00001823err_reset:
1824 i915_reset_unlock(dev_priv, srcu);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001825err_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001826 intel_runtime_pm_put(dev_priv, wakeref);
Chris Wilson40e62d52016-10-28 13:58:41 +01001827 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001828err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001829 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001830 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001831 /*
1832 * We eat errors when the gpu is terminally wedged to avoid
1833 * userspace unduly crashing (gl has no provisions for mmaps to
1834 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1835 * and so needs to be reported.
1836 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00001837 if (!i915_terminally_wedged(dev_priv))
Chris Wilson52137012018-06-06 22:45:20 +01001838 return VM_FAULT_SIGBUS;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05001839 /* else: fall through */
Chris Wilson045e7692010-11-07 09:18:22 +00001840 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001841 /*
1842 * EAGAIN means the gpu is hung and we'll wait for the error
1843 * handler to reset everything when re-faulting in
1844 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001845 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001846 case 0:
1847 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001848 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001849 case -EBUSY:
1850 /*
1851 * EBUSY is ok: this just means that another thread
1852 * already did the job.
1853 */
Chris Wilson52137012018-06-06 22:45:20 +01001854 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001855 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01001856 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001857 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001858 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01001859 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001860 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001861 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01001862 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001863 }
1864}
1865
Chris Wilsona65adaf2017-10-09 09:43:57 +01001866static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
1867{
1868 struct i915_vma *vma;
1869
1870 GEM_BUG_ON(!obj->userfault_count);
1871
1872 obj->userfault_count = 0;
1873 list_del(&obj->userfault_link);
1874 drm_vma_node_unmap(&obj->base.vma_node,
1875 obj->base.dev->anon_inode->i_mapping);
1876
Chris Wilsone2189dd2017-12-07 21:14:07 +00001877 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01001878 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001879}
1880
Jesse Barnesde151cf2008-11-12 10:03:55 -08001881/**
Chris Wilson901782b2009-07-10 08:18:50 +01001882 * i915_gem_release_mmap - remove physical page mappings
1883 * @obj: obj in question
1884 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001885 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001886 * relinquish ownership of the pages back to the system.
1887 *
1888 * It is vital that we remove the page mapping if we have mapped a tiled
1889 * object through the GTT and then lose the fence register due to
1890 * resource pressure. Similarly if the object has been moved out of the
1891 * aperture, than pages mapped into userspace must be revoked. Removing the
1892 * mapping will then trigger a page fault on the next user access, allowing
1893 * fixup by i915_gem_fault().
1894 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001895void
Chris Wilson05394f32010-11-08 19:18:58 +00001896i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001897{
Chris Wilson275f0392016-10-24 13:42:14 +01001898 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson538ef962019-01-14 14:21:18 +00001899 intel_wakeref_t wakeref;
Chris Wilson275f0392016-10-24 13:42:14 +01001900
Chris Wilson349f2cc2016-04-13 17:35:12 +01001901 /* Serialisation between user GTT access and our code depends upon
1902 * revoking the CPU's PTE whilst the mutex is held. The next user
1903 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01001904 *
1905 * Note that RPM complicates somewhat by adding an additional
1906 * requirement that operations to the GGTT be made holding the RPM
1907 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01001908 */
Chris Wilson275f0392016-10-24 13:42:14 +01001909 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00001910 wakeref = intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001911
Chris Wilsona65adaf2017-10-09 09:43:57 +01001912 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01001913 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01001914
Chris Wilsona65adaf2017-10-09 09:43:57 +01001915 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001916
1917 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1918 * memory transactions from userspace before we return. The TLB
1919 * flushing implied above by changing the PTE above *should* be
1920 * sufficient, an extra barrier here just provides us with a bit
1921 * of paranoid documentation about our requirement to serialise
1922 * memory writes before touching registers / GSM.
1923 */
1924 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01001925
1926out:
Chris Wilson538ef962019-01-14 14:21:18 +00001927 intel_runtime_pm_put(i915, wakeref);
Chris Wilson901782b2009-07-10 08:18:50 +01001928}
1929
Chris Wilson7c108fd2016-10-24 13:42:18 +01001930void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001931{
Chris Wilson3594a3e2016-10-24 13:42:16 +01001932 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01001933 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001934
Chris Wilson3594a3e2016-10-24 13:42:16 +01001935 /*
1936 * Only called during RPM suspend. All users of the userfault_list
1937 * must be holding an RPM wakeref to ensure that this can not
1938 * run concurrently with themselves (and use the struct_mutex for
1939 * protection between themselves).
1940 */
1941
1942 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01001943 &dev_priv->mm.userfault_list, userfault_link)
1944 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01001945
1946 /* The fence will be lost when the device powers down. If any were
1947 * in use by hardware (i.e. they are pinned), we should not be powering
1948 * down! All other fences will be reacquired by the user upon waking.
1949 */
1950 for (i = 0; i < dev_priv->num_fence_regs; i++) {
1951 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1952
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00001953 /* Ideally we want to assert that the fence register is not
1954 * live at this point (i.e. that no piece of code will be
1955 * trying to write through fence + GTT, as that both violates
1956 * our tracking of activity and associated locking/barriers,
1957 * but also is illegal given that the hw is powered down).
1958 *
1959 * Previously we used reg->pin_count as a "liveness" indicator.
1960 * That is not sufficient, and we need a more fine-grained
1961 * tool if we want to have a sanity check here.
1962 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01001963
1964 if (!reg->vma)
1965 continue;
1966
Chris Wilsona65adaf2017-10-09 09:43:57 +01001967 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01001968 reg->dirty = true;
1969 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001970}
1971
Chris Wilsond8cb5082012-08-11 15:41:03 +01001972static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1973{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001974 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01001975 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001976
Chris Wilsonf3f61842016-08-05 10:14:14 +01001977 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00001978 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01001979 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01001980
Chris Wilsonb42a13d2017-01-06 15:22:40 +00001981 /* Attempt to reap some mmap space from dead objects */
1982 do {
Chris Wilsonec625fb2018-07-09 13:20:42 +01001983 err = i915_gem_wait_for_idle(dev_priv,
1984 I915_WAIT_INTERRUPTIBLE,
1985 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00001986 if (err)
1987 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001988
Chris Wilsonb42a13d2017-01-06 15:22:40 +00001989 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01001990 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00001991 if (!err)
1992 break;
1993
Chris Wilson23c3c3d2019-04-24 21:07:14 +01001994 } while (flush_delayed_work(&dev_priv->gem.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01001995
Chris Wilsonf3f61842016-08-05 10:14:14 +01001996 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001997}
1998
1999static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2000{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002001 drm_gem_free_mmap_offset(&obj->base);
2002}
2003
Dave Airlieda6b51d2014-12-24 13:11:17 +10002004int
Dave Airlieff72145b2011-02-07 12:16:14 +10002005i915_gem_mmap_gtt(struct drm_file *file,
2006 struct drm_device *dev,
Jani Nikula739f3ab2019-01-16 11:15:19 +02002007 u32 handle,
2008 u64 *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002009{
Chris Wilson05394f32010-11-08 19:18:58 +00002010 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002011 int ret;
2012
Chris Wilson03ac0642016-07-20 13:31:51 +01002013 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002014 if (!obj)
2015 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002016
Chris Wilsond8cb5082012-08-11 15:41:03 +01002017 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002018 if (ret == 0)
2019 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002020
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002021 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002022 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002023}
2024
Dave Airlieff72145b2011-02-07 12:16:14 +10002025/**
2026 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2027 * @dev: DRM device
2028 * @data: GTT mapping ioctl data
2029 * @file: GEM object info
2030 *
2031 * Simply returns the fake offset to userspace so it can mmap it.
2032 * The mmap call will end up in drm_gem_mmap(), which will set things
2033 * up so we can get faults in the handler above.
2034 *
2035 * The fault handler will take care of binding the object into the GTT
2036 * (since it may have been evicted to make room for something), allocating
2037 * a fence register, and mapping the appropriate aperture address into
2038 * userspace.
2039 */
2040int
2041i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2042 struct drm_file *file)
2043{
2044 struct drm_i915_gem_mmap_gtt *args = data;
2045
Dave Airlieda6b51d2014-12-24 13:11:17 +10002046 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002047}
2048
Daniel Vetter225067e2012-08-20 10:23:20 +02002049/* Immediately discard the backing storage */
Chris Wilson2d6692e2019-04-20 12:55:39 +01002050void __i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002051{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002052 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002053
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002054 if (obj->base.filp == NULL)
2055 return;
2056
Daniel Vetter225067e2012-08-20 10:23:20 +02002057 /* Our goal here is to return as much of the memory as
2058 * is possible back to the system as we are called from OOM.
2059 * To do this we must instruct the shmfs to drop all of its
2060 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002061 */
Chris Wilson55372522014-03-25 13:23:06 +00002062 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002063 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002064 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002065}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002066
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002067/*
2068 * Move pages to appropriate lru and release the pagevec, decrementing the
2069 * ref count of those pages.
2070 */
2071static void check_release_pagevec(struct pagevec *pvec)
2072{
2073 check_move_unevictable_pages(pvec);
2074 __pagevec_release(pvec);
2075 cond_resched();
2076}
2077
Chris Wilson5cdf5882010-09-27 15:51:07 +01002078static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002079i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2080 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002081{
Dave Gordon85d12252016-05-20 11:54:06 +01002082 struct sgt_iter sgt_iter;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002083 struct pagevec pvec;
Dave Gordon85d12252016-05-20 11:54:06 +01002084 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002085
Chris Wilsone5facdf2016-12-23 14:57:57 +00002086 __i915_gem_object_release_shmem(obj, pages, true);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002087 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002088
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002089 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002090 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002091
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002092 mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping);
2093
2094 pagevec_init(&pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002095 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002096 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002097 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002098
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002099 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002100 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002101
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002102 if (!pagevec_add(&pvec, page))
2103 check_release_pagevec(&pvec);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002104 }
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002105 if (pagevec_count(&pvec))
2106 check_release_pagevec(&pvec);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002107 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002108
Chris Wilson03ac84f2016-10-28 13:58:36 +01002109 sg_free_table(pages);
2110 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002111}
2112
Chris Wilson96d77632016-10-28 13:58:33 +01002113static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2114{
2115 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002116 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002117
Chris Wilsonbea6e982017-10-26 14:00:31 +01002118 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002119 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2120 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002121 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002122}
2123
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002124static struct sg_table *
2125__i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002126{
Chris Wilsonf2123812017-10-16 12:40:37 +01002127 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002128 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002129
Chris Wilson03ac84f2016-10-28 13:58:36 +01002130 pages = fetch_and_zero(&obj->mm.pages);
Chris Wilson484d9a82019-01-15 12:44:42 +00002131 if (IS_ERR_OR_NULL(pages))
2132 return pages;
Chris Wilsona2165e32012-12-03 11:49:00 +00002133
Chris Wilsonf2123812017-10-16 12:40:37 +01002134 spin_lock(&i915->mm.obj_lock);
2135 list_del(&obj->mm.link);
2136 spin_unlock(&i915->mm.obj_lock);
2137
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002138 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002139 void *ptr;
2140
Chris Wilson0ce81782017-05-17 13:09:59 +01002141 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002142 if (is_vmalloc_addr(ptr))
2143 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002144 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002145 kunmap(kmap_to_page(ptr));
2146
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002147 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002148 }
2149
Chris Wilson96d77632016-10-28 13:58:33 +01002150 __i915_gem_object_reset_page_iter(obj);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002151 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
Chris Wilson96d77632016-10-28 13:58:33 +01002152
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002153 return pages;
2154}
2155
Chris Wilson484d9a82019-01-15 12:44:42 +00002156int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2157 enum i915_mm_subclass subclass)
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002158{
2159 struct sg_table *pages;
Chris Wilson484d9a82019-01-15 12:44:42 +00002160 int ret;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002161
2162 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson484d9a82019-01-15 12:44:42 +00002163 return -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002164
2165 GEM_BUG_ON(obj->bind_count);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002166
2167 /* May be called by shrinker from within get_pages() (on another bo) */
2168 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson484d9a82019-01-15 12:44:42 +00002169 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
2170 ret = -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002171 goto unlock;
Chris Wilson484d9a82019-01-15 12:44:42 +00002172 }
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002173
2174 /*
2175 * ->put_pages might need to allocate memory for the bit17 swizzle
2176 * array, hence protect them from being reaped by removing them from gtt
2177 * lists early.
2178 */
2179 pages = __i915_gem_object_unset_pages(obj);
Chris Wilson484d9a82019-01-15 12:44:42 +00002180
2181 /*
2182 * XXX Temporary hijinx to avoid updating all backends to handle
2183 * NULL pages. In the future, when we have more asynchronous
2184 * get_pages backends we should be better able to handle the
2185 * cancellation of the async task in a more uniform manner.
2186 */
2187 if (!pages && !i915_gem_object_needs_async_cancel(obj))
2188 pages = ERR_PTR(-EINVAL);
2189
Chris Wilson4e5462e2017-03-07 13:20:31 +00002190 if (!IS_ERR(pages))
2191 obj->ops->put_pages(obj, pages);
2192
Chris Wilson484d9a82019-01-15 12:44:42 +00002193 ret = 0;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002194unlock:
2195 mutex_unlock(&obj->mm.lock);
Chris Wilson484d9a82019-01-15 12:44:42 +00002196
2197 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002198}
2199
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01002200bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002201{
2202 struct sg_table new_st;
2203 struct scatterlist *sg, *new_sg;
2204 unsigned int i;
2205
2206 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002207 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002208
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002209 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002210 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002211
2212 new_sg = new_st.sgl;
2213 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2214 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01002215 sg_dma_address(new_sg) = sg_dma_address(sg);
2216 sg_dma_len(new_sg) = sg_dma_len(sg);
2217
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002218 new_sg = sg_next(new_sg);
2219 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002220 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002221
2222 sg_free_table(orig_st);
2223
2224 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002225 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002226}
2227
Matthew Auldb91b09e2017-10-06 23:18:17 +01002228static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002229{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002230 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002231 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2232 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002233 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002234 struct sg_table *st;
2235 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002236 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002237 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002238 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002239 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002240 unsigned int sg_page_sizes;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002241 struct pagevec pvec;
Chris Wilson4846bf02017-06-09 12:03:46 +01002242 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002243 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002244
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002245 /*
2246 * Assert that the object is not currently in any GPU domain. As it
Chris Wilson6c085a72012-08-20 11:40:46 +02002247 * wasn't in the GTT, there shouldn't be any way it could have been in
2248 * a GPU cache
2249 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002250 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2251 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002252
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002253 /*
2254 * If there's no chance of allocating enough pages for the whole
2255 * object, bail early.
2256 */
Arun KSca79b0c2018-12-28 00:34:29 -08002257 if (page_count > totalram_pages())
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002258 return -ENOMEM;
2259
Chris Wilson9da3da62012-06-01 15:20:22 +01002260 st = kmalloc(sizeof(*st), GFP_KERNEL);
2261 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002262 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002263
Chris Wilsond766ef52016-12-19 12:43:45 +00002264rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002265 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002266 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002267 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002268 }
2269
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002270 /*
2271 * Get the list of pages out of our struct file. They'll be pinned
Chris Wilson9da3da62012-06-01 15:20:22 +01002272 * at this point until we release them.
2273 *
2274 * Fail silently without starting the shrinker
2275 */
Al Viro93c76a32015-12-04 23:45:44 -05002276 mapping = obj->base.filp->f_mapping;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002277 mapping_set_unevictable(mapping);
Chris Wilson0f6ab552017-06-09 12:03:48 +01002278 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002279 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2280
Imre Deak90797e62013-02-18 19:28:03 +02002281 sg = st->sgl;
2282 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002283 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002284 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002285 const unsigned int shrink[] = {
2286 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2287 0,
2288 }, *s = shrink;
2289 gfp_t gfp = noreclaim;
2290
2291 do {
Chris Wilsone6db7f42018-11-05 17:06:40 +00002292 cond_resched();
Chris Wilson6c085a72012-08-20 11:40:46 +02002293 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chengguang Xu772b5402019-02-21 10:08:19 +08002294 if (!IS_ERR(page))
Chris Wilson4846bf02017-06-09 12:03:46 +01002295 break;
2296
2297 if (!*s) {
2298 ret = PTR_ERR(page);
2299 goto err_sg;
2300 }
2301
Chris Wilson912d5722017-09-06 16:19:30 -07002302 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson24f8e002017-03-22 11:05:21 +00002303
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002304 /*
2305 * We've tried hard to allocate the memory by reaping
Chris Wilson6c085a72012-08-20 11:40:46 +02002306 * our own buffer, now let the real VM do its job and
2307 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002308 *
2309 * However, since graphics tend to be disposable,
2310 * defer the oom here by reporting the ENOMEM back
2311 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002312 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002313 if (!*s) {
2314 /* reclaim and warn, but no oom */
2315 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002316
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002317 /*
2318 * Our bo are always dirty and so we require
Chris Wilsoneaf41802017-06-09 12:03:47 +01002319 * kswapd to reclaim our pages (direct reclaim
2320 * does not effectively begin pageout of our
2321 * buffers on its own). However, direct reclaim
2322 * only waits for kswapd when under allocation
2323 * congestion. So as a result __GFP_RECLAIM is
2324 * unreliable and fails to actually reclaim our
2325 * dirty pages -- unless you try over and over
2326 * again with !__GFP_NORETRY. However, we still
2327 * want to fail this allocation rather than
2328 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002329 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002330 */
Michal Hockodbb32952017-07-12 14:36:55 -07002331 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002332 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002333 } while (1);
2334
Chris Wilson871dfbd2016-10-11 09:20:21 +01002335 if (!i ||
2336 sg->length >= max_segment ||
2337 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002338 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002339 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002340 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002341 }
Imre Deak90797e62013-02-18 19:28:03 +02002342 st->nents++;
2343 sg_set_page(sg, page, PAGE_SIZE, 0);
2344 } else {
2345 sg->length += PAGE_SIZE;
2346 }
2347 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002348
2349 /* Check that the i965g/gm workaround works. */
2350 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002351 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002352 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002353 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002354 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002355 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002356
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002357 /* Trim unused sg entries to avoid wasting memory. */
2358 i915_sg_trim(st);
2359
Chris Wilson03ac84f2016-10-28 13:58:36 +01002360 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002361 if (ret) {
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002362 /*
2363 * DMA remapping failed? One possible cause is that
Chris Wilsond766ef52016-12-19 12:43:45 +00002364 * it could not reserve enough large entries, asking
2365 * for PAGE_SIZE chunks instead may be helpful.
2366 */
2367 if (max_segment > PAGE_SIZE) {
2368 for_each_sgt_page(page, sgt_iter, st)
2369 put_page(page);
2370 sg_free_table(st);
2371
2372 max_segment = PAGE_SIZE;
2373 goto rebuild_st;
2374 } else {
2375 dev_warn(&dev_priv->drm.pdev->dev,
2376 "Failed to DMA remap %lu pages\n",
2377 page_count);
2378 goto err_pages;
2379 }
2380 }
Imre Deake2273302015-07-09 12:59:05 +03002381
Eric Anholt673a3942008-07-30 12:06:12 -07002382 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002383 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002384
Matthew Auld84e89782017-10-09 12:00:24 +01002385 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002386
2387 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002388
Chris Wilsonb17993b2016-11-14 11:29:30 +00002389err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002390 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002391err_pages:
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002392 mapping_clear_unevictable(mapping);
2393 pagevec_init(&pvec);
2394 for_each_sgt_page(page, sgt_iter, st) {
2395 if (!pagevec_add(&pvec, page))
2396 check_release_pagevec(&pvec);
2397 }
2398 if (pagevec_count(&pvec))
2399 check_release_pagevec(&pvec);
Chris Wilson9da3da62012-06-01 15:20:22 +01002400 sg_free_table(st);
2401 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002402
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002403 /*
2404 * shmemfs first checks if there is enough memory to allocate the page
Chris Wilson0820baf2014-03-25 13:23:03 +00002405 * and reports ENOSPC should there be insufficient, along with the usual
2406 * ENOMEM for a genuine allocation failure.
2407 *
2408 * We use ENOSPC in our driver to mean that we have run out of aperture
2409 * space and so want to translate the error from shmemfs back to our
2410 * usual understanding of ENOMEM.
2411 */
Imre Deake2273302015-07-09 12:59:05 +03002412 if (ret == -ENOSPC)
2413 ret = -ENOMEM;
2414
Matthew Auldb91b09e2017-10-06 23:18:17 +01002415 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002416}
2417
2418void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002419 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002420 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002421{
Matthew Aulda5c081662017-10-06 23:18:18 +01002422 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2423 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2424 int i;
2425
Chris Wilson1233e2d2016-10-28 13:58:37 +01002426 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002427
Chris Wilsona679f582019-03-21 16:19:07 +00002428 /* Make the pages coherent with the GPU (flushing any swapin). */
2429 if (obj->cache_dirty) {
2430 obj->write_domain = 0;
2431 if (i915_gem_object_has_struct_page(obj))
2432 drm_clflush_sg(pages);
2433 obj->cache_dirty = false;
2434 }
2435
Chris Wilson03ac84f2016-10-28 13:58:36 +01002436 obj->mm.get_page.sg_pos = pages->sgl;
2437 obj->mm.get_page.sg_idx = 0;
2438
2439 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002440
2441 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002442 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002443 GEM_BUG_ON(obj->mm.quirked);
2444 __i915_gem_object_pin_pages(obj);
2445 obj->mm.quirked = true;
2446 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002447
Matthew Auld84e89782017-10-09 12:00:24 +01002448 GEM_BUG_ON(!sg_page_sizes);
2449 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002450
2451 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002452 * Calculate the supported page-sizes which fit into the given
2453 * sg_page_sizes. This will give us the page-sizes which we may be able
2454 * to use opportunistically when later inserting into the GTT. For
2455 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2456 * 64K or 4K pages, although in practice this will depend on a number of
2457 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002458 */
2459 obj->mm.page_sizes.sg = 0;
2460 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2461 if (obj->mm.page_sizes.phys & ~0u << i)
2462 obj->mm.page_sizes.sg |= BIT(i);
2463 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002464 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002465
2466 spin_lock(&i915->mm.obj_lock);
2467 list_add(&obj->mm.link, &i915->mm.unbound_list);
2468 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002469}
2470
2471static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2472{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002473 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002474
2475 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2476 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2477 return -EFAULT;
2478 }
2479
Matthew Auldb91b09e2017-10-06 23:18:17 +01002480 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002481 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002482
Matthew Auldb91b09e2017-10-06 23:18:17 +01002483 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002484}
2485
Chris Wilson37e680a2012-06-07 15:38:42 +01002486/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002487 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002488 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002489 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002490 * either as a result of memory pressure (reaping pages under the shrinker)
2491 * or as the object is itself released.
2492 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002493int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002494{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002495 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002496
Chris Wilson1233e2d2016-10-28 13:58:37 +01002497 err = mutex_lock_interruptible(&obj->mm.lock);
2498 if (err)
2499 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002500
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002501 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002502 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2503
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002504 err = ____i915_gem_object_get_pages(obj);
2505 if (err)
2506 goto unlock;
2507
2508 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002509 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002510 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002511
Chris Wilson1233e2d2016-10-28 13:58:37 +01002512unlock:
2513 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002514 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002515}
2516
Dave Gordondd6034c2016-05-20 11:54:04 +01002517/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002518static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2519 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002520{
2521 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002522 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002523 struct sgt_iter sgt_iter;
2524 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002525 struct page *stack_pages[32];
2526 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002527 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002528 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002529 void *addr;
2530
2531 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002532 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002533 return kmap(sg_page(sgt->sgl));
2534
Dave Gordonb338fa42016-05-20 11:54:05 +01002535 if (n_pages > ARRAY_SIZE(stack_pages)) {
2536 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002537 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002538 if (!pages)
2539 return NULL;
2540 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002541
Dave Gordon85d12252016-05-20 11:54:06 +01002542 for_each_sgt_page(page, sgt_iter, sgt)
2543 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002544
2545 /* Check that we have the expected number of pages */
2546 GEM_BUG_ON(i != n_pages);
2547
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002548 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002549 default:
2550 MISSING_CASE(type);
2551 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002552 case I915_MAP_WB:
2553 pgprot = PAGE_KERNEL;
2554 break;
2555 case I915_MAP_WC:
2556 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2557 break;
2558 }
2559 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002560
Dave Gordonb338fa42016-05-20 11:54:05 +01002561 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002562 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002563
2564 return addr;
2565}
2566
2567/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002568void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2569 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002570{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002571 enum i915_map_type has_type;
2572 bool pinned;
2573 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002574 int ret;
2575
Tina Zhanga03f3952017-11-14 10:25:13 +00002576 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2577 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002578
Chris Wilson1233e2d2016-10-28 13:58:37 +01002579 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002580 if (ret)
2581 return ERR_PTR(ret);
2582
Chris Wilsona575c672017-08-28 11:46:31 +01002583 pinned = !(type & I915_MAP_OVERRIDE);
2584 type &= ~I915_MAP_OVERRIDE;
2585
Chris Wilson1233e2d2016-10-28 13:58:37 +01002586 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002587 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002588 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2589
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002590 ret = ____i915_gem_object_get_pages(obj);
2591 if (ret)
2592 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002593
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002594 smp_mb__before_atomic();
2595 }
2596 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002597 pinned = false;
2598 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002599 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002600
Chris Wilson0ce81782017-05-17 13:09:59 +01002601 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002602 if (ptr && has_type != type) {
2603 if (pinned) {
2604 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002605 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002606 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002607
2608 if (is_vmalloc_addr(ptr))
2609 vunmap(ptr);
2610 else
2611 kunmap(kmap_to_page(ptr));
2612
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002613 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002614 }
2615
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002616 if (!ptr) {
2617 ptr = i915_gem_object_map(obj, type);
2618 if (!ptr) {
2619 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002620 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002621 }
2622
Chris Wilson0ce81782017-05-17 13:09:59 +01002623 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002624 }
2625
Chris Wilson1233e2d2016-10-28 13:58:37 +01002626out_unlock:
2627 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002628 return ptr;
2629
Chris Wilson1233e2d2016-10-28 13:58:37 +01002630err_unpin:
2631 atomic_dec(&obj->mm.pages_pin_count);
2632err_unlock:
2633 ptr = ERR_PTR(ret);
2634 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002635}
2636
Chris Wilsona679f582019-03-21 16:19:07 +00002637void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
2638 unsigned long offset,
2639 unsigned long size)
2640{
2641 enum i915_map_type has_type;
2642 void *ptr;
2643
2644 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
2645 GEM_BUG_ON(range_overflows_t(typeof(obj->base.size),
2646 offset, size, obj->base.size));
2647
2648 obj->mm.dirty = true;
2649
2650 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)
2651 return;
2652
2653 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
2654 if (has_type == I915_MAP_WC)
2655 return;
2656
2657 drm_clflush_virt_range(ptr + offset, size);
2658 if (size == obj->base.size) {
2659 obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
2660 obj->cache_dirty = false;
2661 }
2662}
2663
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002664static int
2665i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2666 const struct drm_i915_gem_pwrite *arg)
2667{
2668 struct address_space *mapping = obj->base.filp->f_mapping;
2669 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2670 u64 remain, offset;
2671 unsigned int pg;
2672
Chris Wilsonb01720b2019-04-01 14:39:09 +01002673 /* Caller already validated user args */
2674 GEM_BUG_ON(!access_ok(user_data, arg->size));
2675
2676 /*
2677 * Before we instantiate/pin the backing store for our use, we
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002678 * can prepopulate the shmemfs filp efficiently using a write into
2679 * the pagecache. We avoid the penalty of instantiating all the
2680 * pages, important if the user is just writing to a few and never
2681 * uses the object on the GPU, and using a direct write into shmemfs
2682 * allows it to avoid the cost of retrieving a page (either swapin
2683 * or clearing-before-use) before it is overwritten.
2684 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002685 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002686 return -ENODEV;
2687
Chris Wilsona6d65e42017-10-16 21:27:32 +01002688 if (obj->mm.madv != I915_MADV_WILLNEED)
2689 return -EFAULT;
2690
Chris Wilsonb01720b2019-04-01 14:39:09 +01002691 /*
2692 * Before the pages are instantiated the object is treated as being
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002693 * in the CPU domain. The pages will be clflushed as required before
2694 * use, and we can freely write into the pages directly. If userspace
2695 * races pwrite with any other operation; corruption will ensue -
2696 * that is userspace's prerogative!
2697 */
2698
2699 remain = arg->size;
2700 offset = arg->offset;
2701 pg = offset_in_page(offset);
2702
2703 do {
2704 unsigned int len, unwritten;
2705 struct page *page;
2706 void *data, *vaddr;
2707 int err;
Chris Wilsonb01720b2019-04-01 14:39:09 +01002708 char c;
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002709
2710 len = PAGE_SIZE - pg;
2711 if (len > remain)
2712 len = remain;
2713
Chris Wilsonb01720b2019-04-01 14:39:09 +01002714 /* Prefault the user page to reduce potential recursion */
2715 err = __get_user(c, user_data);
2716 if (err)
2717 return err;
2718
2719 err = __get_user(c, user_data + len - 1);
2720 if (err)
2721 return err;
2722
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002723 err = pagecache_write_begin(obj->base.filp, mapping,
2724 offset, len, 0,
2725 &page, &data);
2726 if (err < 0)
2727 return err;
2728
Chris Wilsonb01720b2019-04-01 14:39:09 +01002729 vaddr = kmap_atomic(page);
2730 unwritten = __copy_from_user_inatomic(vaddr + pg,
2731 user_data,
2732 len);
2733 kunmap_atomic(vaddr);
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002734
2735 err = pagecache_write_end(obj->base.filp, mapping,
2736 offset, len, len - unwritten,
2737 page, data);
2738 if (err < 0)
2739 return err;
2740
Chris Wilsonb01720b2019-04-01 14:39:09 +01002741 /* We don't handle -EFAULT, leave it to the caller to check */
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002742 if (unwritten)
Chris Wilsonb01720b2019-04-01 14:39:09 +01002743 return -ENODEV;
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002744
2745 remain -= len;
2746 user_data += len;
2747 offset += len;
2748 pg = 0;
2749 } while (remain);
2750
2751 return 0;
2752}
2753
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002754void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
2755{
Chris Wilsond1b48c12017-08-16 09:52:08 +01002756 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002757 struct drm_i915_gem_object *obj = to_intel_bo(gem);
2758 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01002759 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002760
Chris Wilsond1b48c12017-08-16 09:52:08 +01002761 mutex_lock(&i915->drm.struct_mutex);
2762
2763 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
2764 struct i915_gem_context *ctx = lut->ctx;
2765 struct i915_vma *vma;
2766
Chris Wilson432295d2017-08-22 12:05:15 +01002767 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01002768 if (ctx->file_priv != fpriv)
2769 continue;
2770
2771 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01002772 GEM_BUG_ON(vma->obj != obj);
2773
2774 /* We allow the process to have multiple handles to the same
2775 * vma, in the same fd namespace, by virtue of flink/open.
2776 */
2777 GEM_BUG_ON(!vma->open_count);
2778 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002779 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01002780
Chris Wilsond1b48c12017-08-16 09:52:08 +01002781 list_del(&lut->obj_link);
2782 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01002783
Chris Wilson13f1bfd2019-02-28 10:20:34 +00002784 i915_lut_handle_free(lut);
Chris Wilsond1b48c12017-08-16 09:52:08 +01002785 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01002786 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01002787
2788 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002789}
2790
Chris Wilsone95433c2016-10-28 13:58:27 +01002791static unsigned long to_wait_timeout(s64 timeout_ns)
2792{
2793 if (timeout_ns < 0)
2794 return MAX_SCHEDULE_TIMEOUT;
2795
2796 if (timeout_ns == 0)
2797 return 0;
2798
2799 return nsecs_to_jiffies_timeout(timeout_ns);
2800}
2801
Ben Widawsky5816d642012-04-11 11:18:19 -07002802/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002803 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002804 * @dev: drm device pointer
2805 * @data: ioctl data blob
2806 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002807 *
2808 * Returns 0 if successful, else an error is returned with the remaining time in
2809 * the timeout parameter.
2810 * -ETIME: object is still busy after timeout
2811 * -ERESTARTSYS: signal interrupted the wait
2812 * -ENONENT: object doesn't exist
2813 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01002814 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002815 * -ENOMEM: damn
2816 * -ENODEV: Internal IRQ fail
2817 * -E?: The add request failed
2818 *
2819 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2820 * non-zero timeout parameter the wait ioctl will wait for the given number of
2821 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2822 * without holding struct_mutex the object may become re-busied before this
2823 * function completes. A similar but shorter * race condition exists in the busy
2824 * ioctl
2825 */
2826int
2827i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2828{
2829 struct drm_i915_gem_wait *args = data;
2830 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01002831 ktime_t start;
2832 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002833
Daniel Vetter11b5d512014-09-29 15:31:26 +02002834 if (args->flags != 0)
2835 return -EINVAL;
2836
Chris Wilson03ac0642016-07-20 13:31:51 +01002837 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01002838 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002839 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01002840
Chris Wilsone95433c2016-10-28 13:58:27 +01002841 start = ktime_get();
2842
2843 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01002844 I915_WAIT_INTERRUPTIBLE |
2845 I915_WAIT_PRIORITY |
2846 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00002847 to_wait_timeout(args->timeout_ns));
Chris Wilsone95433c2016-10-28 13:58:27 +01002848
2849 if (args->timeout_ns > 0) {
2850 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
2851 if (args->timeout_ns < 0)
2852 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00002853
2854 /*
2855 * Apparently ktime isn't accurate enough and occasionally has a
2856 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
2857 * things up to make the test happy. We allow up to 1 jiffy.
2858 *
2859 * This is a regression from the timespec->ktime conversion.
2860 */
2861 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
2862 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01002863
2864 /* Asked to wait beyond the jiffie/scheduler precision? */
2865 if (ret == -ETIME && args->timeout_ns)
2866 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002867 }
2868
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002869 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00002870 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002871}
2872
Chris Wilson25112b62017-03-30 15:50:39 +01002873static int wait_for_engines(struct drm_i915_private *i915)
2874{
Chris Wilsonee42c002017-12-11 19:41:34 +00002875 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00002876 dev_err(i915->drm.dev,
2877 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00002878 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01002879 i915_gem_set_wedged(i915);
2880 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01002881 }
2882
2883 return 0;
2884}
2885
Chris Wilson1e345562019-01-28 10:23:56 +00002886static long
2887wait_for_timelines(struct drm_i915_private *i915,
2888 unsigned int flags, long timeout)
2889{
2890 struct i915_gt_timelines *gt = &i915->gt.timelines;
2891 struct i915_timeline *tl;
2892
Chris Wilson1e345562019-01-28 10:23:56 +00002893 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00002894 list_for_each_entry(tl, &gt->active_list, link) {
Chris Wilson1e345562019-01-28 10:23:56 +00002895 struct i915_request *rq;
2896
Chris Wilson21950ee2019-02-05 13:00:05 +00002897 rq = i915_active_request_get_unlocked(&tl->last_request);
Chris Wilson1e345562019-01-28 10:23:56 +00002898 if (!rq)
2899 continue;
2900
2901 mutex_unlock(&gt->mutex);
2902
2903 /*
2904 * "Race-to-idle".
2905 *
2906 * Switching to the kernel context is often used a synchronous
2907 * step prior to idling, e.g. in suspend for flushing all
2908 * current operations to memory before sleeping. These we
2909 * want to complete as quickly as possible to avoid prolonged
2910 * stalls, so allow the gpu to boost to maximum clocks.
2911 */
2912 if (flags & I915_WAIT_FOR_IDLE_BOOST)
Chris Wilson62eb3c22019-02-13 09:25:04 +00002913 gen6_rps_boost(rq);
Chris Wilson1e345562019-01-28 10:23:56 +00002914
2915 timeout = i915_request_wait(rq, flags, timeout);
2916 i915_request_put(rq);
2917 if (timeout < 0)
2918 return timeout;
2919
2920 /* restart after reacquiring the lock */
2921 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00002922 tl = list_entry(&gt->active_list, typeof(*tl), link);
Chris Wilson1e345562019-01-28 10:23:56 +00002923 }
2924 mutex_unlock(&gt->mutex);
2925
2926 return timeout;
2927}
2928
Chris Wilsonec625fb2018-07-09 13:20:42 +01002929int i915_gem_wait_for_idle(struct drm_i915_private *i915,
2930 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01002931{
Chris Wilson79ffac852019-04-24 21:07:17 +01002932 GEM_TRACE("flags=%x (%s), timeout=%ld%s, awake?=%s\n",
Chris Wilsonec625fb2018-07-09 13:20:42 +01002933 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
Chris Wilson79ffac852019-04-24 21:07:17 +01002934 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "",
2935 yesno(i915->gt.awake));
Chris Wilson09a4c022018-05-24 09:11:35 +01002936
Chris Wilson863e9fd2017-05-30 13:13:32 +01002937 /* If the device is asleep, we have no requests outstanding */
2938 if (!READ_ONCE(i915->gt.awake))
2939 return 0;
2940
Chris Wilson1e345562019-01-28 10:23:56 +00002941 timeout = wait_for_timelines(i915, flags, timeout);
2942 if (timeout < 0)
2943 return timeout;
2944
Chris Wilson9caa34a2016-11-11 14:58:08 +00002945 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01002946 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00002947
2948 lockdep_assert_held(&i915->drm.struct_mutex);
2949
Chris Wilsona61b47f2018-06-27 12:53:34 +01002950 err = wait_for_engines(i915);
2951 if (err)
2952 return err;
2953
Chris Wilsone61e0f52018-02-21 09:56:36 +00002954 i915_retire_requests(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +01002955 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01002956
2957 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002958}
2959
Chris Wilson5a97bcc2017-02-22 11:40:46 +00002960static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
2961{
Chris Wilsone27ab732017-06-15 13:38:49 +01002962 /*
2963 * We manually flush the CPU domain so that we can override and
2964 * force the flush for the display, and perform it asyncrhonously.
2965 */
2966 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
2967 if (obj->cache_dirty)
2968 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01002969 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00002970}
2971
2972void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
2973{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01002974 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00002975 return;
2976
2977 mutex_lock(&obj->base.dev->struct_mutex);
2978 __i915_gem_object_flush_for_display(obj);
2979 mutex_unlock(&obj->base.dev->struct_mutex);
2980}
2981
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002982/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01002983 * Moves a single object to the WC read, and possibly write domain.
2984 * @obj: object to act on
2985 * @write: ask for write access or read only
2986 *
2987 * This function returns when the move is complete, including waiting on
2988 * flushes to occur.
2989 */
2990int
2991i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
2992{
2993 int ret;
2994
2995 lockdep_assert_held(&obj->base.dev->struct_mutex);
2996
2997 ret = i915_gem_object_wait(obj,
2998 I915_WAIT_INTERRUPTIBLE |
2999 I915_WAIT_LOCKED |
3000 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003001 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone22d8e32017-04-12 12:01:11 +01003002 if (ret)
3003 return ret;
3004
Christian Königc0a51fd2018-02-16 13:43:38 +01003005 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003006 return 0;
3007
3008 /* Flush and acquire obj->pages so that we are coherent through
3009 * direct access in memory with previous cached writes through
3010 * shmemfs and that our cache domain tracking remains valid.
3011 * For example, if the obj->filp was moved to swap without us
3012 * being notified and releasing the pages, we would mistakenly
3013 * continue to assume that the obj remained out of the CPU cached
3014 * domain.
3015 */
3016 ret = i915_gem_object_pin_pages(obj);
3017 if (ret)
3018 return ret;
3019
3020 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3021
3022 /* Serialise direct access to this object with the barriers for
3023 * coherent writes from the GPU, by effectively invalidating the
3024 * WC domain upon first access.
3025 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003026 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003027 mb();
3028
3029 /* It should now be out of any other write domains, and we can update
3030 * the domain values for our changes.
3031 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003032 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3033 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003034 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003035 obj->read_domains = I915_GEM_DOMAIN_WC;
3036 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003037 obj->mm.dirty = true;
3038 }
3039
3040 i915_gem_object_unpin_pages(obj);
3041 return 0;
3042}
3043
3044/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003045 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003046 * @obj: object to act on
3047 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003048 *
3049 * This function returns when the move is complete, including waiting on
3050 * flushes to occur.
3051 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003052int
Chris Wilson20217462010-11-23 15:26:33 +00003053i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003054{
Eric Anholte47c68e2008-11-14 13:35:19 -08003055 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003056
Chris Wilsone95433c2016-10-28 13:58:27 +01003057 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003058
Chris Wilsone95433c2016-10-28 13:58:27 +01003059 ret = i915_gem_object_wait(obj,
3060 I915_WAIT_INTERRUPTIBLE |
3061 I915_WAIT_LOCKED |
3062 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003063 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003064 if (ret)
3065 return ret;
3066
Christian Königc0a51fd2018-02-16 13:43:38 +01003067 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003068 return 0;
3069
Chris Wilson43566de2015-01-02 16:29:29 +05303070 /* Flush and acquire obj->pages so that we are coherent through
3071 * direct access in memory with previous cached writes through
3072 * shmemfs and that our cache domain tracking remains valid.
3073 * For example, if the obj->filp was moved to swap without us
3074 * being notified and releasing the pages, we would mistakenly
3075 * continue to assume that the obj remained out of the CPU cached
3076 * domain.
3077 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003078 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303079 if (ret)
3080 return ret;
3081
Chris Wilsonef749212017-04-12 12:01:10 +01003082 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003083
Chris Wilsond0a57782012-10-09 19:24:37 +01003084 /* Serialise direct access to this object with the barriers for
3085 * coherent writes from the GPU, by effectively invalidating the
3086 * GTT domain upon first access.
3087 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003088 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003089 mb();
3090
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003091 /* It should now be out of any other write domains, and we can update
3092 * the domain values for our changes.
3093 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003094 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3095 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003096 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003097 obj->read_domains = I915_GEM_DOMAIN_GTT;
3098 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003099 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003100 }
3101
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003102 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003103 return 0;
3104}
3105
Chris Wilsonef55f922015-10-09 14:11:27 +01003106/**
3107 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003108 * @obj: object to act on
3109 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003110 *
3111 * After this function returns, the object will be in the new cache-level
3112 * across all GTT and the contents of the backing storage will be coherent,
3113 * with respect to the new cache-level. In order to keep the backing storage
3114 * coherent for all users, we only allow a single cache level to be set
3115 * globally on the object and prevent it from being changed whilst the
3116 * hardware is reading from the object. That is if the object is currently
3117 * on the scanout it will be set to uncached (or equivalent display
3118 * cache coherency) and all non-MOCS GPU access will also be uncached so
3119 * that all direct access to the scanout remains coherent.
3120 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003121int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3122 enum i915_cache_level cache_level)
3123{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003124 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003125 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003126
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003127 lockdep_assert_held(&obj->base.dev->struct_mutex);
3128
Chris Wilsone4ffd172011-04-04 09:44:39 +01003129 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003130 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003131
Chris Wilsonef55f922015-10-09 14:11:27 +01003132 /* Inspect the list of currently bound VMA and unbind any that would
3133 * be invalid given the new cache-level. This is principally to
3134 * catch the issue of the CS prefetch crossing page boundaries and
3135 * reading an invalid PTE on older architectures.
3136 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003137restart:
Chris Wilson528cbd12019-01-28 10:23:54 +00003138 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003139 if (!drm_mm_node_allocated(&vma->node))
3140 continue;
3141
Chris Wilson20dfbde2016-08-04 16:32:30 +01003142 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003143 DRM_DEBUG("can not change the cache level of pinned objects\n");
3144 return -EBUSY;
3145 }
3146
Chris Wilson010e3e62017-12-06 12:49:13 +00003147 if (!i915_vma_is_closed(vma) &&
3148 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003149 continue;
3150
3151 ret = i915_vma_unbind(vma);
3152 if (ret)
3153 return ret;
3154
3155 /* As unbinding may affect other elements in the
3156 * obj->vma_list (due to side-effects from retiring
3157 * an active vma), play safe and restart the iterator.
3158 */
3159 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003160 }
3161
Chris Wilsonef55f922015-10-09 14:11:27 +01003162 /* We can reuse the existing drm_mm nodes but need to change the
3163 * cache-level on the PTE. We could simply unbind them all and
3164 * rebind with the correct cache-level on next use. However since
3165 * we already have a valid slot, dma mapping, pages etc, we may as
3166 * rewrite the PTE in the belief that doing so tramples upon less
3167 * state and so involves less work.
3168 */
Chris Wilson15717de2016-08-04 07:52:26 +01003169 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003170 /* Before we change the PTE, the GPU must not be accessing it.
3171 * If we wait upon the object, we know that all the bound
3172 * VMA are no longer active.
3173 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003174 ret = i915_gem_object_wait(obj,
3175 I915_WAIT_INTERRUPTIBLE |
3176 I915_WAIT_LOCKED |
3177 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003178 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003179 if (ret)
3180 return ret;
3181
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003182 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3183 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003184 /* Access to snoopable pages through the GTT is
3185 * incoherent and on some machines causes a hard
3186 * lockup. Relinquish the CPU mmaping to force
3187 * userspace to refault in the pages and we can
3188 * then double check if the GTT mapping is still
3189 * valid for that pointer access.
3190 */
3191 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003192
Chris Wilsonef55f922015-10-09 14:11:27 +01003193 /* As we no longer need a fence for GTT access,
3194 * we can relinquish it now (and so prevent having
3195 * to steal a fence from someone else on the next
3196 * fence request). Note GPU activity would have
3197 * dropped the fence as all snoopable access is
3198 * supposed to be linear.
3199 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003200 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003201 ret = i915_vma_put_fence(vma);
3202 if (ret)
3203 return ret;
3204 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003205 } else {
3206 /* We either have incoherent backing store and
3207 * so no GTT access or the architecture is fully
3208 * coherent. In such cases, existing GTT mmaps
3209 * ignore the cache bit in the PTE and we can
3210 * rewrite it without confusing the GPU or having
3211 * to force userspace to fault back in its mmaps.
3212 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003213 }
3214
Chris Wilson528cbd12019-01-28 10:23:54 +00003215 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003216 if (!drm_mm_node_allocated(&vma->node))
3217 continue;
3218
3219 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3220 if (ret)
3221 return ret;
3222 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003223 }
3224
Chris Wilson528cbd12019-01-28 10:23:54 +00003225 list_for_each_entry(vma, &obj->vma.list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003226 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003227 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003228 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003229
Chris Wilsone4ffd172011-04-04 09:44:39 +01003230 return 0;
3231}
3232
Ben Widawsky199adf42012-09-21 17:01:20 -07003233int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3234 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003235{
Ben Widawsky199adf42012-09-21 17:01:20 -07003236 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003237 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003238 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003239
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003240 rcu_read_lock();
3241 obj = i915_gem_object_lookup_rcu(file, args->handle);
3242 if (!obj) {
3243 err = -ENOENT;
3244 goto out;
3245 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003246
Chris Wilson651d7942013-08-08 14:41:10 +01003247 switch (obj->cache_level) {
3248 case I915_CACHE_LLC:
3249 case I915_CACHE_L3_LLC:
3250 args->caching = I915_CACHING_CACHED;
3251 break;
3252
Chris Wilson4257d3b2013-08-08 14:41:11 +01003253 case I915_CACHE_WT:
3254 args->caching = I915_CACHING_DISPLAY;
3255 break;
3256
Chris Wilson651d7942013-08-08 14:41:10 +01003257 default:
3258 args->caching = I915_CACHING_NONE;
3259 break;
3260 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003261out:
3262 rcu_read_unlock();
3263 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003264}
3265
Ben Widawsky199adf42012-09-21 17:01:20 -07003266int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3267 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003268{
Chris Wilson9c870d02016-10-24 13:42:15 +01003269 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003270 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003271 struct drm_i915_gem_object *obj;
3272 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003273 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003274
Ben Widawsky199adf42012-09-21 17:01:20 -07003275 switch (args->caching) {
3276 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003277 level = I915_CACHE_NONE;
3278 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003279 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003280 /*
3281 * Due to a HW issue on BXT A stepping, GPU stores via a
3282 * snooped mapping may leave stale data in a corresponding CPU
3283 * cacheline, whereas normally such cachelines would get
3284 * invalidated.
3285 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003286 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003287 return -ENODEV;
3288
Chris Wilsone6994ae2012-07-10 10:27:08 +01003289 level = I915_CACHE_LLC;
3290 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003291 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003292 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003293 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003294 default:
3295 return -EINVAL;
3296 }
3297
Chris Wilsond65415d2017-01-19 08:22:10 +00003298 obj = i915_gem_object_lookup(file, args->handle);
3299 if (!obj)
3300 return -ENOENT;
3301
Tina Zhanga03f3952017-11-14 10:25:13 +00003302 /*
3303 * The caching mode of proxy object is handled by its generator, and
3304 * not allowed to be changed by userspace.
3305 */
3306 if (i915_gem_object_is_proxy(obj)) {
3307 ret = -ENXIO;
3308 goto out;
3309 }
3310
Chris Wilsond65415d2017-01-19 08:22:10 +00003311 if (obj->cache_level == level)
3312 goto out;
3313
3314 ret = i915_gem_object_wait(obj,
3315 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003316 MAX_SCHEDULE_TIMEOUT);
Chris Wilsond65415d2017-01-19 08:22:10 +00003317 if (ret)
3318 goto out;
3319
Ben Widawsky3bc29132012-09-26 16:15:20 -07003320 ret = i915_mutex_lock_interruptible(dev);
3321 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003322 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003323
3324 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003325 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003326
3327out:
3328 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003329 return ret;
3330}
3331
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003332/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08003333 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
3334 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
3335 * (for pageflips). We only flush the caches while preparing the buffer for
3336 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003337 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003338struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003339i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3340 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003341 const struct i915_ggtt_view *view,
3342 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003343{
Chris Wilson058d88c2016-08-15 10:49:06 +01003344 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003345 int ret;
3346
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003347 lockdep_assert_held(&obj->base.dev->struct_mutex);
3348
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003349 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003350 * display coherency whilst setting up the cache domains.
3351 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003352 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003353
Eric Anholta7ef0642011-03-29 16:59:54 -07003354 /* The display engine is not coherent with the LLC cache on gen6. As
3355 * a result, we make sure that the pinning that is about to occur is
3356 * done with uncached PTEs. This is lowest common denominator for all
3357 * chipsets.
3358 *
3359 * However for gen6+, we could do better by using the GFDT bit instead
3360 * of uncaching, which would allow us to flush all the LLC-cached data
3361 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3362 */
Chris Wilson651d7942013-08-08 14:41:10 +01003363 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003364 HAS_WT(to_i915(obj->base.dev)) ?
3365 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003366 if (ret) {
3367 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003368 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003369 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003370
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003371 /* As the user may map the buffer once pinned in the display plane
3372 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003373 * always use map_and_fenceable for all scanout buffers. However,
3374 * it may simply be too big to fit into mappable, in which case
3375 * put it anyway and hope that userspace can cope (but always first
3376 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003377 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003378 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00003379 if ((flags & PIN_MAPPABLE) == 0 &&
3380 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01003381 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003382 flags |
3383 PIN_MAPPABLE |
3384 PIN_NONBLOCK);
3385 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00003386 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01003387 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003388 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003389
Chris Wilsond8923dc2016-08-18 17:17:07 +01003390 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3391
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003392 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003393
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003394 /* It should now be out of any other write domains, and we can update
3395 * the domain values for our changes.
3396 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003397 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003398
Chris Wilson058d88c2016-08-15 10:49:06 +01003399 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003400
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003401err_unpin_global:
3402 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003403 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003404}
3405
3406void
Chris Wilson058d88c2016-08-15 10:49:06 +01003407i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003408{
Chris Wilson49d73912016-11-29 09:50:08 +00003409 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003410
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003411 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003412 return;
3413
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003414 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003415 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003416
Chris Wilson383d5822016-08-18 17:17:08 +01003417 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003418 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003419
Chris Wilson058d88c2016-08-15 10:49:06 +01003420 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003421}
3422
Eric Anholte47c68e2008-11-14 13:35:19 -08003423/**
3424 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003425 * @obj: object to act on
3426 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003427 *
3428 * This function returns when the move is complete, including waiting on
3429 * flushes to occur.
3430 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003431int
Chris Wilson919926a2010-11-12 13:42:53 +00003432i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003433{
Eric Anholte47c68e2008-11-14 13:35:19 -08003434 int ret;
3435
Chris Wilsone95433c2016-10-28 13:58:27 +01003436 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003437
Chris Wilsone95433c2016-10-28 13:58:27 +01003438 ret = i915_gem_object_wait(obj,
3439 I915_WAIT_INTERRUPTIBLE |
3440 I915_WAIT_LOCKED |
3441 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003442 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003443 if (ret)
3444 return ret;
3445
Chris Wilsonef749212017-04-12 12:01:10 +01003446 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003447
Eric Anholte47c68e2008-11-14 13:35:19 -08003448 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01003449 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003450 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01003451 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003452 }
3453
3454 /* It should now be out of any other write domains, and we can update
3455 * the domain values for our changes.
3456 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003457 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003458
3459 /* If we're writing through the CPU, then the GPU read domains will
3460 * need to be invalidated at next use.
3461 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003462 if (write)
3463 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003464
3465 return 0;
3466}
3467
Eric Anholt673a3942008-07-30 12:06:12 -07003468/* Throttle our rendering by waiting until the ring has completed our requests
3469 * emitted over 20 msec ago.
3470 *
Eric Anholtb9624422009-06-03 07:27:35 +00003471 * Note that if we were to use the current jiffies each time around the loop,
3472 * we wouldn't escape the function with any frames outstanding if the time to
3473 * render a frame was over 20ms.
3474 *
Eric Anholt673a3942008-07-30 12:06:12 -07003475 * This should get us reasonable parallelism between CPU and GPU but also
3476 * relatively low latency when blocking on a particular request to finish.
3477 */
3478static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003479i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003480{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003481 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003482 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003483 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003484 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003485 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003486
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003487 /* ABI: return -EIO if already wedged */
Chris Wilsonc41166f2019-02-20 14:56:37 +00003488 ret = i915_terminally_wedged(dev_priv);
3489 if (ret)
3490 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003491
Chris Wilson1c255952010-09-26 11:03:27 +01003492 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003493 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00003494 if (time_after_eq(request->emitted_jiffies, recent_enough))
3495 break;
3496
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003497 if (target) {
3498 list_del(&target->client_link);
3499 target->file_priv = NULL;
3500 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01003501
John Harrison54fb2412014-11-24 18:49:27 +00003502 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003503 }
John Harrisonff865882014-11-24 18:49:28 +00003504 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00003505 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003506 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003507
John Harrison54fb2412014-11-24 18:49:27 +00003508 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003509 return 0;
3510
Chris Wilsone61e0f52018-02-21 09:56:36 +00003511 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01003512 I915_WAIT_INTERRUPTIBLE,
3513 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003514 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003515
Chris Wilsone95433c2016-10-28 13:58:27 +01003516 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003517}
3518
Chris Wilson058d88c2016-08-15 10:49:06 +01003519struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003520i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3521 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003522 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003523 u64 alignment,
3524 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003525{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003526 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01003527 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01003528 struct i915_vma *vma;
3529 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003530
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003531 lockdep_assert_held(&obj->base.dev->struct_mutex);
3532
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00003533 if (flags & PIN_MAPPABLE &&
3534 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003535 /* If the required space is larger than the available
3536 * aperture, we will not able to find a slot for the
3537 * object and unbinding the object now will be in
3538 * vain. Worse, doing so may cause us to ping-pong
3539 * the object in and out of the Global GTT and
3540 * waste a lot of cycles under the mutex.
3541 */
3542 if (obj->base.size > dev_priv->ggtt.mappable_end)
3543 return ERR_PTR(-E2BIG);
3544
3545 /* If NONBLOCK is set the caller is optimistically
3546 * trying to cache the full object within the mappable
3547 * aperture, and *must* have a fallback in place for
3548 * situations where we cannot bind the object. We
3549 * can be a little more lax here and use the fallback
3550 * more often to avoid costly migrations of ourselves
3551 * and other objects within the aperture.
3552 *
3553 * Half-the-aperture is used as a simple heuristic.
3554 * More interesting would to do search for a free
3555 * block prior to making the commitment to unbind.
3556 * That caters for the self-harm case, and with a
3557 * little more heuristics (e.g. NOFAULT, NOEVICT)
3558 * we could try to minimise harm to others.
3559 */
3560 if (flags & PIN_NONBLOCK &&
3561 obj->base.size > dev_priv->ggtt.mappable_end / 2)
3562 return ERR_PTR(-ENOSPC);
3563 }
3564
Chris Wilson718659a2017-01-16 15:21:28 +00003565 vma = i915_vma_instance(obj, vm, view);
Chengguang Xu772b5402019-02-21 10:08:19 +08003566 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01003567 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003568
3569 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003570 if (flags & PIN_NONBLOCK) {
3571 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
3572 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003573
Chris Wilson43ae70d92017-10-09 09:44:01 +01003574 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00003575 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003576 return ERR_PTR(-ENOSPC);
3577 }
3578
Chris Wilson59bfa122016-08-04 16:32:31 +01003579 WARN(i915_vma_is_pinned(vma),
3580 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003581 " offset=%08x, req.alignment=%llx,"
3582 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3583 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003584 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003585 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003586 ret = i915_vma_unbind(vma);
3587 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003588 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003589 }
3590
Chris Wilson058d88c2016-08-15 10:49:06 +01003591 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3592 if (ret)
3593 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003594
Chris Wilson058d88c2016-08-15 10:49:06 +01003595 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003596}
3597
Chris Wilson6960d9c2019-04-04 11:19:14 +01003598static __always_inline u32 __busy_read_flag(u8 id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003599{
Chris Wilson6960d9c2019-04-04 11:19:14 +01003600 if (id == (u8)I915_ENGINE_CLASS_INVALID)
3601 return 0xffff0000u;
Chris Wilsonc8b50242019-03-05 16:26:43 +00003602
3603 GEM_BUG_ON(id >= 16);
Chris Wilson6960d9c2019-04-04 11:19:14 +01003604 return 0x10000u << id;
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003605}
3606
Chris Wilson6960d9c2019-04-04 11:19:14 +01003607static __always_inline u32 __busy_write_id(u8 id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003608{
Chris Wilsonc8b50242019-03-05 16:26:43 +00003609 /*
3610 * The uABI guarantees an active writer is also amongst the read
Chris Wilson70cb4722016-08-09 18:08:25 +01003611 * engines. This would be true if we accessed the activity tracking
3612 * under the lock, but as we perform the lookup of the object and
3613 * its activity locklessly we can not guarantee that the last_write
3614 * being active implies that we have set the same engine flag from
3615 * last_read - hence we always set both read and write busy for
3616 * last_write.
3617 */
Chris Wilson6960d9c2019-04-04 11:19:14 +01003618 if (id == (u8)I915_ENGINE_CLASS_INVALID)
3619 return 0xffffffffu;
Chris Wilsonc8b50242019-03-05 16:26:43 +00003620
3621 return (id + 1) | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003622}
3623
Chris Wilsonedf6b762016-08-09 09:23:33 +01003624static __always_inline unsigned int
Chris Wilson6960d9c2019-04-04 11:19:14 +01003625__busy_set_if_active(const struct dma_fence *fence, u32 (*flag)(u8 id))
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003626{
Chris Wilsonc8b50242019-03-05 16:26:43 +00003627 const struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003628
Chris Wilsonc8b50242019-03-05 16:26:43 +00003629 /*
3630 * We have to check the current hw status of the fence as the uABI
Chris Wilsond07f0e52016-10-28 13:58:44 +01003631 * guarantees forward progress. We could rely on the idle worker
3632 * to eventually flush us, but to minimise latency just ask the
3633 * hardware.
3634 *
3635 * Note we only report on the status of native fences.
3636 */
3637 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003638 return 0;
3639
Chris Wilsond07f0e52016-10-28 13:58:44 +01003640 /* opencode to_request() in order to avoid const warnings */
Chris Wilsonc8b50242019-03-05 16:26:43 +00003641 rq = container_of(fence, const struct i915_request, fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003642 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01003643 return 0;
3644
Chris Wilson6960d9c2019-04-04 11:19:14 +01003645 /* Beware type-expansion follies! */
3646 BUILD_BUG_ON(!typecheck(u8, rq->engine->uabi_class));
Chris Wilsonc8b50242019-03-05 16:26:43 +00003647 return flag(rq->engine->uabi_class);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003648}
3649
Chris Wilsonedf6b762016-08-09 09:23:33 +01003650static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003651busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003652{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003653 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003654}
3655
Chris Wilsonedf6b762016-08-09 09:23:33 +01003656static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003657busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003658{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003659 if (!fence)
3660 return 0;
3661
3662 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003663}
3664
Eric Anholt673a3942008-07-30 12:06:12 -07003665int
Eric Anholt673a3942008-07-30 12:06:12 -07003666i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003667 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003668{
3669 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003670 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003671 struct reservation_object_list *list;
3672 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003673 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003674
Chris Wilsond07f0e52016-10-28 13:58:44 +01003675 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003676 rcu_read_lock();
3677 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003678 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003679 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003680
Chris Wilsonc8b50242019-03-05 16:26:43 +00003681 /*
3682 * A discrepancy here is that we do not report the status of
Chris Wilsond07f0e52016-10-28 13:58:44 +01003683 * non-i915 fences, i.e. even though we may report the object as idle,
3684 * a call to set-domain may still stall waiting for foreign rendering.
3685 * This also means that wait-ioctl may report an object as busy,
3686 * where busy-ioctl considers it idle.
3687 *
3688 * We trade the ability to warn of foreign fences to report on which
3689 * i915 engines are active for the object.
3690 *
3691 * Alternatively, we can trade that extra information on read/write
3692 * activity with
3693 * args->busy =
3694 * !reservation_object_test_signaled_rcu(obj->resv, true);
3695 * to report the overall busyness. This is what the wait-ioctl does.
3696 *
3697 */
3698retry:
3699 seq = raw_read_seqcount(&obj->resv->seq);
3700
3701 /* Translate the exclusive fence to the READ *and* WRITE engine */
3702 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
3703
3704 /* Translate shared fences to READ set of engines */
3705 list = rcu_dereference(obj->resv->fence);
3706 if (list) {
3707 unsigned int shared_count = list->shared_count, i;
3708
3709 for (i = 0; i < shared_count; ++i) {
3710 struct dma_fence *fence =
3711 rcu_dereference(list->shared[i]);
3712
3713 args->busy |= busy_check_reader(fence);
3714 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003715 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003716
Chris Wilsond07f0e52016-10-28 13:58:44 +01003717 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
3718 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00003719
Chris Wilsond07f0e52016-10-28 13:58:44 +01003720 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003721out:
3722 rcu_read_unlock();
3723 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07003724}
3725
3726int
3727i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3728 struct drm_file *file_priv)
3729{
Akshay Joshi0206e352011-08-16 15:34:10 -04003730 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003731}
3732
Chris Wilson3ef94da2009-09-14 16:50:29 +01003733int
3734i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3735 struct drm_file *file_priv)
3736{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003737 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01003738 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003739 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003740 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003741
3742 switch (args->madv) {
3743 case I915_MADV_DONTNEED:
3744 case I915_MADV_WILLNEED:
3745 break;
3746 default:
3747 return -EINVAL;
3748 }
3749
Chris Wilson03ac0642016-07-20 13:31:51 +01003750 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003751 if (!obj)
3752 return -ENOENT;
3753
3754 err = mutex_lock_interruptible(&obj->mm.lock);
3755 if (err)
3756 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003757
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01003758 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01003759 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01003760 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003761 if (obj->mm.madv == I915_MADV_WILLNEED) {
3762 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003763 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003764 obj->mm.quirked = false;
3765 }
3766 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003767 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003768 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003769 obj->mm.quirked = true;
3770 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01003771 }
3772
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003773 if (obj->mm.madv != __I915_MADV_PURGED)
3774 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003775
Chris Wilson6c085a72012-08-20 11:40:46 +02003776 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01003777 if (obj->mm.madv == I915_MADV_DONTNEED &&
3778 !i915_gem_object_has_pages(obj))
Chris Wilson2d6692e2019-04-20 12:55:39 +01003779 __i915_gem_object_truncate(obj);
Chris Wilson2d7ef392009-09-20 23:13:10 +01003780
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003781 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003782 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003783
Chris Wilson1233e2d2016-10-28 13:58:37 +01003784out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003785 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003786 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003787}
3788
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003789static void
Chris Wilson21950ee2019-02-05 13:00:05 +00003790frontbuffer_retire(struct i915_active_request *active,
3791 struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003792{
3793 struct drm_i915_gem_object *obj =
3794 container_of(active, typeof(*obj), frontbuffer_write);
3795
Chris Wilsond59b21e2017-02-22 11:40:49 +00003796 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003797}
3798
Chris Wilson37e680a2012-06-07 15:38:42 +01003799void i915_gem_object_init(struct drm_i915_gem_object *obj,
3800 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01003801{
Chris Wilson1233e2d2016-10-28 13:58:37 +01003802 mutex_init(&obj->mm.lock);
3803
Chris Wilson528cbd12019-01-28 10:23:54 +00003804 spin_lock_init(&obj->vma.lock);
3805 INIT_LIST_HEAD(&obj->vma.list);
3806
Chris Wilsond1b48c12017-08-16 09:52:08 +01003807 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01003808 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01003809
Chris Wilson8811d612018-11-09 09:03:11 +00003810 init_rcu_head(&obj->rcu);
3811
Chris Wilson37e680a2012-06-07 15:38:42 +01003812 obj->ops = ops;
3813
Chris Wilsond07f0e52016-10-28 13:58:44 +01003814 reservation_object_init(&obj->__builtin_resv);
3815 obj->resv = &obj->__builtin_resv;
3816
Chris Wilson50349242016-08-18 17:17:04 +01003817 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson21950ee2019-02-05 13:00:05 +00003818 i915_active_request_init(&obj->frontbuffer_write,
3819 NULL, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003820
3821 obj->mm.madv = I915_MADV_WILLNEED;
3822 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
3823 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01003824
Dave Gordonf19ec8c2016-07-04 11:34:37 +01003825 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01003826}
3827
Chris Wilson37e680a2012-06-07 15:38:42 +01003828static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00003829 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
3830 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00003831
Chris Wilson37e680a2012-06-07 15:38:42 +01003832 .get_pages = i915_gem_object_get_pages_gtt,
3833 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00003834
3835 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01003836};
3837
Matthew Auld465c4032017-10-06 23:18:14 +01003838static int i915_gem_object_create_shmem(struct drm_device *dev,
3839 struct drm_gem_object *obj,
3840 size_t size)
3841{
3842 struct drm_i915_private *i915 = to_i915(dev);
3843 unsigned long flags = VM_NORESERVE;
3844 struct file *filp;
3845
3846 drm_gem_private_object_init(dev, obj, size);
3847
3848 if (i915->mm.gemfs)
3849 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
3850 flags);
3851 else
3852 filp = shmem_file_setup("i915", size, flags);
3853
3854 if (IS_ERR(filp))
3855 return PTR_ERR(filp);
3856
3857 obj->filp = filp;
3858
3859 return 0;
3860}
3861
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01003862struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00003863i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003864{
Daniel Vetterc397b902010-04-09 19:05:07 +00003865 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003866 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003867 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01003868 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01003869 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00003870
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01003871 /* There is a prevalence of the assumption that we fit the object's
3872 * page count inside a 32bit _signed_ variable. Let's document this and
3873 * catch if we ever need to fix it. In the meantime, if you do spot
3874 * such a local variable, please consider fixing!
3875 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01003876 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01003877 return ERR_PTR(-E2BIG);
3878
3879 if (overflows_type(size, obj->base.size))
3880 return ERR_PTR(-E2BIG);
3881
Chris Wilson13f1bfd2019-02-28 10:20:34 +00003882 obj = i915_gem_object_alloc();
Daniel Vetterc397b902010-04-09 19:05:07 +00003883 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01003884 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00003885
Matthew Auld465c4032017-10-06 23:18:14 +01003886 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01003887 if (ret)
3888 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00003889
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003890 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02003891 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003892 /* 965gm cannot relocate objects above 4GiB. */
3893 mask &= ~__GFP_HIGHMEM;
3894 mask |= __GFP_DMA32;
3895 }
3896
Al Viro93c76a32015-12-04 23:45:44 -05003897 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003898 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01003899 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07003900
Chris Wilson37e680a2012-06-07 15:38:42 +01003901 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01003902
Christian Königc0a51fd2018-02-16 13:43:38 +01003903 obj->write_domain = I915_GEM_DOMAIN_CPU;
3904 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00003905
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003906 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003907 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003908 * cache) for about a 10% performance improvement
3909 * compared to uncached. Graphics requests other than
3910 * display scanout are coherent with the CPU in
3911 * accessing this cache. This means in this mode we
3912 * don't need to clflush on the CPU side, and on the
3913 * GPU side we only need to flush internal caches to
3914 * get data visible to the CPU.
3915 *
3916 * However, we maintain the display planes as UC, and so
3917 * need to rebind when first used as such.
3918 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003919 cache_level = I915_CACHE_LLC;
3920 else
3921 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07003922
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003923 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003924
Daniel Vetterd861e332013-07-24 23:25:03 +02003925 trace_i915_gem_object_create(obj);
3926
Chris Wilson05394f32010-11-08 19:18:58 +00003927 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01003928
3929fail:
3930 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01003931 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00003932}
3933
Chris Wilson340fbd82014-05-22 09:16:52 +01003934static bool discard_backing_storage(struct drm_i915_gem_object *obj)
3935{
3936 /* If we are the last user of the backing storage (be it shmemfs
3937 * pages or stolen etc), we know that the pages are going to be
3938 * immediately released. In this case, we can then skip copying
3939 * back the contents from the GPU.
3940 */
3941
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003942 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01003943 return false;
3944
3945 if (obj->base.filp == NULL)
3946 return true;
3947
3948 /* At first glance, this looks racy, but then again so would be
3949 * userspace racing mmap against close. However, the first external
3950 * reference to the filp can only be obtained through the
3951 * i915_gem_mmap_ioctl() which safeguards us against the user
3952 * acquiring such a reference whilst we are in the middle of
3953 * freeing the object.
3954 */
3955 return atomic_long_read(&obj->base.filp->f_count) == 1;
3956}
3957
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003958static void __i915_gem_free_objects(struct drm_i915_private *i915,
3959 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01003960{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003961 struct drm_i915_gem_object *obj, *on;
Chris Wilson538ef962019-01-14 14:21:18 +00003962 intel_wakeref_t wakeref;
Chris Wilsonbe726152010-07-23 23:18:50 +01003963
Chris Wilson538ef962019-01-14 14:21:18 +00003964 wakeref = intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01003965 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003966 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02003967
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003968 trace_i915_gem_object_destroy(obj);
3969
Chris Wilsoncc731f52017-10-13 21:26:21 +01003970 mutex_lock(&i915->drm.struct_mutex);
3971
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003972 GEM_BUG_ON(i915_gem_object_is_active(obj));
Chris Wilson528cbd12019-01-28 10:23:54 +00003973 list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003974 GEM_BUG_ON(i915_vma_is_active(vma));
3975 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01003976 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003977 }
Chris Wilson528cbd12019-01-28 10:23:54 +00003978 GEM_BUG_ON(!list_empty(&obj->vma.list));
3979 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003980
Chris Wilsonf2123812017-10-16 12:40:37 +01003981 /* This serializes freeing with the shrinker. Since the free
3982 * is delayed, first by RCU then by the workqueue, we want the
3983 * shrinker to be able to free pages of unreferenced objects,
3984 * or else we may oom whilst there are plenty of deferred
3985 * freed objects.
3986 */
3987 if (i915_gem_object_has_pages(obj)) {
3988 spin_lock(&i915->mm.obj_lock);
3989 list_del_init(&obj->mm.link);
3990 spin_unlock(&i915->mm.obj_lock);
3991 }
3992
Chris Wilsoncc731f52017-10-13 21:26:21 +01003993 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003994
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003995 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01003996 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003997 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01003998 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003999
4000 if (obj->ops->release)
4001 obj->ops->release(obj);
4002
4003 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4004 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004005 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004006 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004007
4008 if (obj->base.import_attach)
4009 drm_prime_gem_destroy(&obj->base, NULL);
4010
Chris Wilsond07f0e52016-10-28 13:58:44 +01004011 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004012 drm_gem_object_release(&obj->base);
4013 i915_gem_info_remove_obj(i915, obj->base.size);
4014
Andy Shevchenko6e514e32019-03-04 11:29:08 +02004015 bitmap_free(obj->bit_17);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004016 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004017
Chris Wilsonc9c704712018-02-19 22:06:31 +00004018 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4019 atomic_dec(&i915->mm.free_count);
4020
Chris Wilsoncc731f52017-10-13 21:26:21 +01004021 if (on)
4022 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004023 }
Chris Wilson538ef962019-01-14 14:21:18 +00004024 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004025}
4026
4027static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4028{
4029 struct llist_node *freed;
4030
Chris Wilson87701b42017-10-13 21:26:20 +01004031 /* Free the oldest, most stale object to keep the free_list short */
4032 freed = NULL;
4033 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4034 /* Only one consumer of llist_del_first() allowed */
4035 spin_lock(&i915->mm.free_lock);
4036 freed = llist_del_first(&i915->mm.free_list);
4037 spin_unlock(&i915->mm.free_lock);
4038 }
4039 if (unlikely(freed)) {
4040 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004041 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004042 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004043}
4044
4045static void __i915_gem_free_work(struct work_struct *work)
4046{
4047 struct drm_i915_private *i915 =
4048 container_of(work, struct drm_i915_private, mm.free_work);
4049 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004050
Chris Wilson2ef1e722018-01-15 20:57:59 +00004051 /*
4052 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004053 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4054 * However, the object may also be bound into the global GTT (e.g.
4055 * older GPUs without per-process support, or for direct access through
4056 * the GTT either for the user or for scanout). Those VMA still need to
4057 * unbound now.
4058 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004059
Chris Wilsonf991c492017-11-06 11:15:08 +00004060 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004061 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004062 spin_unlock(&i915->mm.free_lock);
4063
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004064 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004065 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004066 return;
4067
4068 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004069 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004070 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004071}
4072
4073static void __i915_gem_free_object_rcu(struct rcu_head *head)
4074{
4075 struct drm_i915_gem_object *obj =
4076 container_of(head, typeof(*obj), rcu);
4077 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4078
Chris Wilson2ef1e722018-01-15 20:57:59 +00004079 /*
Chris Wilson8811d612018-11-09 09:03:11 +00004080 * We reuse obj->rcu for the freed list, so we had better not treat
4081 * it like a rcu_head from this point forwards. And we expect all
4082 * objects to be freed via this path.
4083 */
4084 destroy_rcu_head(&obj->rcu);
4085
4086 /*
Chris Wilson2ef1e722018-01-15 20:57:59 +00004087 * Since we require blocking on struct_mutex to unbind the freed
4088 * object from the GPU before releasing resources back to the
4089 * system, we can not do that directly from the RCU callback (which may
4090 * be a softirq context), but must instead then defer that work onto a
4091 * kthread. We use the RCU callback rather than move the freed object
4092 * directly onto the work queue so that we can mix between using the
4093 * worker and performing frees directly from subsequent allocations for
4094 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004095 */
4096 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004097 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004098}
4099
4100void i915_gem_free_object(struct drm_gem_object *gem_obj)
4101{
4102 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4103
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004104 if (obj->mm.quirked)
4105 __i915_gem_object_unpin_pages(obj);
4106
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004107 if (discard_backing_storage(obj))
4108 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004109
Chris Wilson2ef1e722018-01-15 20:57:59 +00004110 /*
4111 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004112 * read-side critical sections are complete, e.g.
4113 * i915_gem_busy_ioctl(). For the corresponding synchronized
4114 * lookup see i915_gem_object_lookup_rcu().
4115 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004116 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004117 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004118}
4119
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004120void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4121{
4122 lockdep_assert_held(&obj->base.dev->struct_mutex);
4123
Chris Wilsond1b48c12017-08-16 09:52:08 +01004124 if (!i915_gem_object_has_active_reference(obj) &&
4125 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004126 i915_gem_object_set_active_reference(obj);
4127 else
4128 i915_gem_object_put(obj);
4129}
4130
Chris Wilson24145512017-01-24 11:01:35 +00004131void i915_gem_sanitize(struct drm_i915_private *i915)
4132{
Chris Wilson538ef962019-01-14 14:21:18 +00004133 intel_wakeref_t wakeref;
4134
Chris Wilsonc3160da2018-05-31 09:22:45 +01004135 GEM_TRACE("\n");
4136
Chris Wilson538ef962019-01-14 14:21:18 +00004137 wakeref = intel_runtime_pm_get(i915);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004138 intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004139
4140 /*
4141 * As we have just resumed the machine and woken the device up from
4142 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4143 * back to defaults, recovering from whatever wedged state we left it
4144 * in and so worth trying to use the device once more.
4145 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004146 if (i915_terminally_wedged(i915))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004147 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004148
Chris Wilson24145512017-01-24 11:01:35 +00004149 /*
4150 * If we inherit context state from the BIOS or earlier occupants
4151 * of the GPU, the GPU may be in an inconsistent state when we
4152 * try to take over. The only way to remove the earlier state
4153 * is by resetting. However, resetting on earlier gen is tricky as
4154 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004155 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004156 */
Chris Wilson79ffac852019-04-24 21:07:17 +01004157 intel_gt_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004158
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004159 intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
Chris Wilson538ef962019-01-14 14:21:18 +00004160 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004161
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004162 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson4dfacb02018-05-31 09:22:43 +01004163 i915_gem_contexts_lost(i915);
4164 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004165}
4166
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004167void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004168{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004169 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004170 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4171 return;
4172
4173 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4174 DISP_TILE_SURFACE_SWIZZLING);
4175
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004176 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01004177 return;
4178
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004179 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004180 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004181 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004182 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004183 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004184 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07004185 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004186 else
4187 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004188}
Daniel Vettere21af882012-02-09 20:53:27 +01004189
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004190static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004191{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004192 I915_WRITE(RING_CTL(base), 0);
4193 I915_WRITE(RING_HEAD(base), 0);
4194 I915_WRITE(RING_TAIL(base), 0);
4195 I915_WRITE(RING_START(base), 0);
4196}
4197
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004198static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004199{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004200 if (IS_I830(dev_priv)) {
4201 init_unused_ring(dev_priv, PRB1_BASE);
4202 init_unused_ring(dev_priv, SRB0_BASE);
4203 init_unused_ring(dev_priv, SRB1_BASE);
4204 init_unused_ring(dev_priv, SRB2_BASE);
4205 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004206 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004207 init_unused_ring(dev_priv, SRB0_BASE);
4208 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004209 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004210 init_unused_ring(dev_priv, PRB1_BASE);
4211 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004212 }
4213}
4214
Chris Wilson20a8a742017-02-08 14:30:31 +00004215int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4216{
Chris Wilsond200cda2016-04-28 09:56:44 +01004217 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004218
Chris Wilsonde867c22016-10-25 13:16:02 +01004219 dev_priv->gt.last_init_time = ktime_get();
4220
Chris Wilson5e4f5182015-02-13 14:35:59 +00004221 /* Double layer security blanket, see i915_gem_init() */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004222 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004223
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004224 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004225 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004226
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004227 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004228 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004229 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004230
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004231 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00004232 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004233 /* ...and determine whether they are sticking. */
4234 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07004235
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004236 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004237
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004238 /*
4239 * At least 830 can leave some of the unused rings
4240 * "active" (ie. head != tail) after resume which
4241 * will prevent c3 entry. Makes sure all unused rings
4242 * are totally idle.
4243 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004244 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004245
Dave Gordoned54c1a2016-01-19 19:02:54 +00004246 BUG_ON(!dev_priv->kernel_context);
Chris Wilsonc41166f2019-02-20 14:56:37 +00004247 ret = i915_terminally_wedged(dev_priv);
4248 if (ret)
Chris Wilson6f74b362017-10-15 15:37:25 +01004249 goto out;
John Harrison90638cc2015-05-29 17:43:37 +01004250
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004251 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004252 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00004253 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01004254 goto out;
4255 }
4256
Jackie Lif08e2032018-03-13 17:32:53 -07004257 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
4258 if (ret) {
4259 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
4260 goto out;
4261 }
4262
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004263 /* We can't enable contexts until all firmware is loaded */
4264 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00004265 if (ret) {
4266 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004267 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00004268 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004269
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004270 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004271
Chris Wilson136109c2017-11-02 13:14:30 +00004272 /* Only when the HW is re-initialised, can we replay the requests */
Chris Wilson79ffac852019-04-24 21:07:17 +01004273 ret = intel_engines_resume(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004274 if (ret)
4275 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02004276
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004277 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004278
Chris Wilson79ffac852019-04-24 21:07:17 +01004279 intel_engines_set_scheduler_caps(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004280 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004281
4282cleanup_uc:
4283 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004284out:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004285 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004286
4287 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004288}
4289
Chris Wilsond2b4b972017-11-10 14:26:33 +00004290static int __intel_engines_record_defaults(struct drm_i915_private *i915)
4291{
4292 struct i915_gem_context *ctx;
4293 struct intel_engine_cs *engine;
4294 enum intel_engine_id id;
Chris Wilson604c37d2019-03-08 09:36:55 +00004295 int err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004296
4297 /*
4298 * As we reset the gpu during very early sanitisation, the current
4299 * register state on the GPU should reflect its defaults values.
4300 * We load a context onto the hw (with restore-inhibit), then switch
4301 * over to a second context to save that default register state. We
4302 * can then prime every new context with that state so they all start
4303 * from the same default HW values.
4304 */
4305
4306 ctx = i915_gem_context_create_kernel(i915, 0);
4307 if (IS_ERR(ctx))
4308 return PTR_ERR(ctx);
4309
4310 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00004311 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004312
Chris Wilsone61e0f52018-02-21 09:56:36 +00004313 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004314 if (IS_ERR(rq)) {
4315 err = PTR_ERR(rq);
4316 goto out_ctx;
4317 }
4318
Chris Wilson3fef5cd2017-11-20 10:20:02 +00004319 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004320 if (engine->init_context)
4321 err = engine->init_context(rq);
4322
Chris Wilson697b9a82018-06-12 11:51:35 +01004323 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004324 if (err)
4325 goto err_active;
4326 }
4327
Chris Wilson604c37d2019-03-08 09:36:55 +00004328 /* Flush the default context image to memory, and enable powersaving. */
Chris Wilson23c3c3d2019-04-24 21:07:14 +01004329 if (!i915_gem_load_power_context(i915)) {
Chris Wilson604c37d2019-03-08 09:36:55 +00004330 err = -EIO;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004331 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01004332 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00004333
Chris Wilsond2b4b972017-11-10 14:26:33 +00004334 for_each_engine(engine, i915, id) {
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004335 struct intel_context *ce;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004336 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004337 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004338
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004339 ce = intel_context_lookup(ctx, engine);
4340 if (!ce)
4341 continue;
Chris Wilson666424a2018-09-14 13:35:04 +01004342
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004343 state = ce->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004344 if (!state)
4345 continue;
4346
Chris Wilson08819542019-03-08 13:25:22 +00004347 GEM_BUG_ON(intel_context_is_pinned(ce));
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004348
Chris Wilsond2b4b972017-11-10 14:26:33 +00004349 /*
4350 * As we will hold a reference to the logical state, it will
4351 * not be torn down with the context, and importantly the
4352 * object will hold onto its vma (making it possible for a
4353 * stray GTT write to corrupt our defaults). Unmap the vma
4354 * from the GTT to prevent such accidents and reclaim the
4355 * space.
4356 */
4357 err = i915_vma_unbind(state);
4358 if (err)
4359 goto err_active;
4360
4361 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
4362 if (err)
4363 goto err_active;
4364
4365 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilsona679f582019-03-21 16:19:07 +00004366 i915_gem_object_set_cache_coherency(engine->default_state,
4367 I915_CACHE_LLC);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004368
4369 /* Check we can acquire the image of the context state */
4370 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01004371 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004372 if (IS_ERR(vaddr)) {
4373 err = PTR_ERR(vaddr);
4374 goto err_active;
4375 }
4376
4377 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004378 }
4379
4380 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
4381 unsigned int found = intel_engines_has_context_isolation(i915);
4382
4383 /*
4384 * Make sure that classes with multiple engine instances all
4385 * share the same basic configuration.
4386 */
4387 for_each_engine(engine, i915, id) {
4388 unsigned int bit = BIT(engine->uabi_class);
4389 unsigned int expected = engine->default_state ? bit : 0;
4390
4391 if ((found & bit) != expected) {
4392 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
4393 engine->uabi_class, engine->name);
4394 }
4395 }
4396 }
4397
4398out_ctx:
4399 i915_gem_context_set_closed(ctx);
4400 i915_gem_context_put(ctx);
4401 return err;
4402
4403err_active:
4404 /*
4405 * If we have to abandon now, we expect the engines to be idle
Chris Wilson604c37d2019-03-08 09:36:55 +00004406 * and ready to be torn-down. The quickest way we can accomplish
4407 * this is by declaring ourselves wedged.
Chris Wilsond2b4b972017-11-10 14:26:33 +00004408 */
Chris Wilson604c37d2019-03-08 09:36:55 +00004409 i915_gem_set_wedged(i915);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004410 goto out_ctx;
4411}
4412
Chris Wilson51797492018-12-04 14:15:16 +00004413static int
4414i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
4415{
4416 struct drm_i915_gem_object *obj;
4417 struct i915_vma *vma;
4418 int ret;
4419
4420 obj = i915_gem_object_create_stolen(i915, size);
4421 if (!obj)
4422 obj = i915_gem_object_create_internal(i915, size);
4423 if (IS_ERR(obj)) {
4424 DRM_ERROR("Failed to allocate scratch page\n");
4425 return PTR_ERR(obj);
4426 }
4427
4428 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
4429 if (IS_ERR(vma)) {
4430 ret = PTR_ERR(vma);
4431 goto err_unref;
4432 }
4433
4434 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
4435 if (ret)
4436 goto err_unref;
4437
4438 i915->gt.scratch = vma;
4439 return 0;
4440
4441err_unref:
4442 i915_gem_object_put(obj);
4443 return ret;
4444}
4445
4446static void i915_gem_fini_scratch(struct drm_i915_private *i915)
4447{
4448 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
4449}
4450
Chris Wilson254e1182019-04-17 08:56:28 +01004451static int intel_engines_verify_workarounds(struct drm_i915_private *i915)
4452{
4453 struct intel_engine_cs *engine;
4454 enum intel_engine_id id;
4455 int err = 0;
4456
4457 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
4458 return 0;
4459
4460 for_each_engine(engine, i915, id) {
4461 if (intel_engine_verify_workarounds(engine, "load"))
4462 err = -EIO;
4463 }
4464
4465 return err;
4466}
4467
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004468int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004469{
Chris Wilson1070a422012-04-24 15:47:41 +01004470 int ret;
4471
Changbin Du52b24162018-05-08 17:07:05 +08004472 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
4473 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01004474 mkwrite_device_info(dev_priv)->page_sizes =
4475 I915_GTT_PAGE_SIZE_4K;
4476
Chris Wilson94312822017-05-03 10:39:18 +01004477 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004478
Chris Wilson97269202019-04-10 20:01:20 +01004479 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv))
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004480 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilson97269202019-04-10 20:01:20 +01004481 else
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004482 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004483
Chris Wilson1e345562019-01-28 10:23:56 +00004484 i915_timelines_init(dev_priv);
4485
Chris Wilsonee487002017-11-22 17:26:21 +00004486 ret = i915_gem_init_userptr(dev_priv);
4487 if (ret)
4488 return ret;
4489
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05304490 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01004491 if (ret)
4492 return ret;
4493
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00004494 ret = intel_wopcm_init(&dev_priv->wopcm);
4495 if (ret)
4496 goto err_uc_misc;
4497
Chris Wilson5e4f5182015-02-13 14:35:59 +00004498 /* This is just a security blanket to placate dragons.
4499 * On some systems, we very sporadically observe that the first TLBs
4500 * used by the CS may be stale, despite us poking the TLB reset. If
4501 * we hold the forcewake during initialisation these problems
4502 * just magically go away.
4503 */
Chris Wilsonee487002017-11-22 17:26:21 +00004504 mutex_lock(&dev_priv->drm.struct_mutex);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004505 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004506
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004507 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004508 if (ret) {
4509 GEM_BUG_ON(ret == -EIO);
4510 goto err_unlock;
4511 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08004512
Chris Wilson51797492018-12-04 14:15:16 +00004513 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004514 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004515 if (ret) {
4516 GEM_BUG_ON(ret == -EIO);
4517 goto err_ggtt;
4518 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004519
Chris Wilson51797492018-12-04 14:15:16 +00004520 ret = i915_gem_contexts_init(dev_priv);
4521 if (ret) {
4522 GEM_BUG_ON(ret == -EIO);
4523 goto err_scratch;
4524 }
4525
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004526 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004527 if (ret) {
4528 GEM_BUG_ON(ret == -EIO);
4529 goto err_context;
4530 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004531
Chris Wilsonf58d13d2017-11-10 14:26:29 +00004532 intel_init_gt_powersave(dev_priv);
4533
Michał Winiarski61b5c152017-12-13 23:13:48 +01004534 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004535 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004536 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004537
Michał Winiarski61b5c152017-12-13 23:13:48 +01004538 ret = i915_gem_init_hw(dev_priv);
4539 if (ret)
4540 goto err_uc_init;
4541
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004542 /*
4543 * Despite its name intel_init_clock_gating applies both display
4544 * clock gating workarounds; GT mmio workarounds and the occasional
4545 * GT power context workaround. Worse, sometimes it includes a context
4546 * register workaround which we need to apply before we record the
4547 * default HW state for all contexts.
4548 *
4549 * FIXME: break up the workarounds and apply them at the right time!
4550 */
4551 intel_init_clock_gating(dev_priv);
4552
Chris Wilson254e1182019-04-17 08:56:28 +01004553 ret = intel_engines_verify_workarounds(dev_priv);
4554 if (ret)
4555 goto err_init_hw;
4556
Chris Wilsond2b4b972017-11-10 14:26:33 +00004557 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004558 if (ret)
4559 goto err_init_hw;
4560
4561 if (i915_inject_load_failure()) {
4562 ret = -ENODEV;
4563 goto err_init_hw;
4564 }
4565
4566 if (i915_inject_load_failure()) {
4567 ret = -EIO;
4568 goto err_init_hw;
4569 }
4570
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004571 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004572 mutex_unlock(&dev_priv->drm.struct_mutex);
4573
4574 return 0;
4575
4576 /*
4577 * Unwinding is complicated by that we want to handle -EIO to mean
4578 * disable GPU submission but keep KMS alive. We want to mark the
4579 * HW as irrevisibly wedged, but keep enough state around that the
4580 * driver doesn't explode during runtime.
4581 */
4582err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01004583 mutex_unlock(&dev_priv->drm.struct_mutex);
4584
Chris Wilson79ffac852019-04-24 21:07:17 +01004585 i915_gem_set_wedged(dev_priv);
Chris Wilson5861b012019-03-08 09:36:54 +00004586 i915_gem_suspend(dev_priv);
Chris Wilson8571a052018-06-06 15:54:41 +01004587 i915_gem_suspend_late(dev_priv);
4588
Chris Wilson8bcf9f72018-07-10 10:44:20 +01004589 i915_gem_drain_workqueue(dev_priv);
4590
Chris Wilson8571a052018-06-06 15:54:41 +01004591 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004592 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01004593err_uc_init:
4594 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004595err_pm:
4596 if (ret != -EIO) {
4597 intel_cleanup_gt_powersave(dev_priv);
4598 i915_gem_cleanup_engines(dev_priv);
4599 }
4600err_context:
4601 if (ret != -EIO)
4602 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00004603err_scratch:
4604 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004605err_ggtt:
4606err_unlock:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004607 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004608 mutex_unlock(&dev_priv->drm.struct_mutex);
4609
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00004610err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05304611 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05304612
Chris Wilson1e345562019-01-28 10:23:56 +00004613 if (ret != -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004614 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00004615 i915_timelines_fini(dev_priv);
4616 }
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004617
Chris Wilson60990322014-04-09 09:19:42 +01004618 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01004619 mutex_lock(&dev_priv->drm.struct_mutex);
4620
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004621 /*
4622 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004623 * wedged. But we only want to do this where the GPU is angry,
4624 * for all other failure, such as an allocation failure, bail.
4625 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004626 if (!i915_reset_failed(dev_priv)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01004627 i915_load_error(dev_priv,
4628 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01004629 i915_gem_set_wedged(dev_priv);
4630 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01004631
4632 /* Minimal basic recovery for KMS */
4633 ret = i915_ggtt_enable_hw(dev_priv);
4634 i915_gem_restore_gtt_mappings(dev_priv);
4635 i915_gem_restore_fences(dev_priv);
4636 intel_init_clock_gating(dev_priv);
4637
4638 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004639 }
4640
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004641 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004642 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004643}
4644
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004645void i915_gem_fini(struct drm_i915_private *dev_priv)
4646{
Chris Wilson79ffac852019-04-24 21:07:17 +01004647 GEM_BUG_ON(dev_priv->gt.awake);
4648
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004649 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01004650 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004651
4652 /* Flush any outstanding unpin_work. */
4653 i915_gem_drain_workqueue(dev_priv);
4654
4655 mutex_lock(&dev_priv->drm.struct_mutex);
4656 intel_uc_fini_hw(dev_priv);
4657 intel_uc_fini(dev_priv);
4658 i915_gem_cleanup_engines(dev_priv);
4659 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00004660 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004661 mutex_unlock(&dev_priv->drm.struct_mutex);
4662
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00004663 intel_wa_list_free(&dev_priv->gt_wa_list);
4664
Chris Wilson30b710842018-08-12 23:36:29 +01004665 intel_cleanup_gt_powersave(dev_priv);
4666
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004667 intel_uc_fini_misc(dev_priv);
4668 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00004669 i915_timelines_fini(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004670
4671 i915_gem_drain_freed_objects(dev_priv);
4672
4673 WARN_ON(!list_empty(&dev_priv->contexts.list));
4674}
4675
Chris Wilson24145512017-01-24 11:01:35 +00004676void i915_gem_init_mmio(struct drm_i915_private *i915)
4677{
4678 i915_gem_sanitize(i915);
4679}
4680
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004681void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004682i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004683{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004684 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304685 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004686
Akash Goel3b3f1652016-10-13 22:44:48 +05304687 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004688 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004689}
4690
Eric Anholt673a3942008-07-30 12:06:12 -07004691void
Imre Deak40ae4e12016-03-16 14:54:03 +02004692i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4693{
Chris Wilson49ef5292016-08-18 17:17:00 +01004694 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004695
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00004696 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02004697 !IS_CHERRYVIEW(dev_priv))
4698 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00004699 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02004700 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4701 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004702 dev_priv->num_fence_regs = 16;
4703 else
4704 dev_priv->num_fence_regs = 8;
4705
Chris Wilsonc0336662016-05-06 15:40:21 +01004706 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004707 dev_priv->num_fence_regs =
4708 I915_READ(vgtif_reg(avail_rs.fence_num));
4709
4710 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004711 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4712 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4713
4714 fence->i915 = dev_priv;
4715 fence->id = i;
4716 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4717 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004718 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004719
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004720 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004721}
4722
Chris Wilson9c52d1c2017-11-10 23:24:47 +00004723static void i915_gem_init__mm(struct drm_i915_private *i915)
4724{
4725 spin_lock_init(&i915->mm.object_stat_lock);
4726 spin_lock_init(&i915->mm.obj_lock);
4727 spin_lock_init(&i915->mm.free_lock);
4728
4729 init_llist_head(&i915->mm.free_list);
4730
4731 INIT_LIST_HEAD(&i915->mm.unbound_list);
4732 INIT_LIST_HEAD(&i915->mm.bound_list);
4733 INIT_LIST_HEAD(&i915->mm.fence_list);
4734 INIT_LIST_HEAD(&i915->mm.userfault_list);
4735
4736 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
4737}
4738
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00004739int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004740{
Chris Wilson13f1bfd2019-02-28 10:20:34 +00004741 int err;
Chris Wilsond1b48c12017-08-16 09:52:08 +01004742
Chris Wilson79ffac852019-04-24 21:07:17 +01004743 intel_gt_pm_init(dev_priv);
4744
Chris Wilson643b4502018-04-30 14:15:03 +01004745 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01004746 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01004747
Chris Wilson9c52d1c2017-11-10 23:24:47 +00004748 i915_gem_init__mm(dev_priv);
Chris Wilson23c3c3d2019-04-24 21:07:14 +01004749 i915_gem_init__pm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01004750
Chris Wilson1f15b762016-07-01 17:23:14 +01004751 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004752 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson18bb2bc2019-01-14 21:04:01 +00004753 mutex_init(&dev_priv->gpu_error.wedge_mutex);
Chris Wilson2caffbf2019-02-08 15:37:03 +00004754 init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
Chris Wilson31169712009-09-14 16:50:28 +01004755
Joonas Lahtinen6f633402016-09-01 14:58:21 +03004756 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
4757
Chris Wilsonb5add952016-08-04 16:32:36 +01004758 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01004759
Matthew Auld465c4032017-10-06 23:18:14 +01004760 err = i915_gemfs_init(dev_priv);
4761 if (err)
4762 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
4763
Chris Wilson73cb9702016-10-28 13:58:46 +01004764 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004765}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004766
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00004767void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02004768{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004769 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00004770 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
4771 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004772 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00004773
Chris Wilson2caffbf2019-02-08 15:37:03 +00004774 cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
4775
Matthew Auld465c4032017-10-06 23:18:14 +01004776 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02004777}
4778
Chris Wilson6a800ea2016-09-21 14:51:07 +01004779int i915_gem_freeze(struct drm_i915_private *dev_priv)
4780{
Chris Wilsond0aa3012017-04-07 11:25:49 +01004781 /* Discard all purgeable objects, let userspace recover those as
4782 * required after resuming.
4783 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01004784 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01004785
Chris Wilson6a800ea2016-09-21 14:51:07 +01004786 return 0;
4787}
4788
Chris Wilson95c778d2018-06-01 15:41:25 +01004789int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01004790{
4791 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01004792 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01004793 &i915->mm.unbound_list,
4794 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01004795 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01004796 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01004797
Chris Wilson95c778d2018-06-01 15:41:25 +01004798 /*
4799 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01004800 *
4801 * We need to update the domain tracking to reflect that the CPU
4802 * will be accessing all the pages to create and restore from the
4803 * hibernation, and so upon restoration those pages will be in the
4804 * CPU domain.
4805 *
4806 * To make sure the hibernation image contains the latest state,
4807 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01004808 *
4809 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01004810 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01004811 */
4812
Chris Wilson95c778d2018-06-01 15:41:25 +01004813 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
4814 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01004815
Chris Wilson95c778d2018-06-01 15:41:25 +01004816 mutex_lock(&i915->drm.struct_mutex);
4817 for (phase = phases; *phase; phase++) {
4818 list_for_each_entry(obj, *phase, mm.link)
4819 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01004820 }
Chris Wilson95c778d2018-06-01 15:41:25 +01004821 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01004822
4823 return 0;
4824}
4825
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004826void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004827{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004828 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004829 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004830
4831 /* Clean up our request list when the client is going away, so that
4832 * later retire_requests won't dereference our soon-to-be-gone
4833 * file_priv.
4834 */
Chris Wilson1c255952010-09-26 11:03:27 +01004835 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004836 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004837 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004838 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004839}
4840
Chris Wilson829a0af2017-06-20 12:05:45 +01004841int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004842{
4843 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004844 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004845
Chris Wilsonc4c29d72016-11-09 10:45:07 +00004846 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004847
4848 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4849 if (!file_priv)
4850 return -ENOMEM;
4851
4852 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01004853 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004854 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004855
4856 spin_lock_init(&file_priv->mm.lock);
4857 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004858
Chris Wilsonc80ff162016-07-27 09:07:27 +01004859 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03004860 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004861
Chris Wilson829a0af2017-06-20 12:05:45 +01004862 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08004863 if (ret)
4864 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004865
Ben Widawskye422b882013-12-06 14:10:58 -08004866 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004867}
4868
Daniel Vetterb680c372014-09-19 18:27:27 +02004869/**
4870 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004871 * @old: current GEM buffer for the frontbuffer slots
4872 * @new: new GEM buffer for the frontbuffer slots
4873 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004874 *
4875 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4876 * from @old and setting them in @new. Both @old and @new can be NULL.
4877 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004878void i915_gem_track_fb(struct drm_i915_gem_object *old,
4879 struct drm_i915_gem_object *new,
4880 unsigned frontbuffer_bits)
4881{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004882 /* Control of individual bits within the mask are guarded by
4883 * the owning plane->mutex, i.e. we can never see concurrent
4884 * manipulation of individual bits. But since the bitfield as a whole
4885 * is updated using RMW, we need to use atomics in order to update
4886 * the bits.
4887 */
4888 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01004889 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004890
Daniel Vettera071fa02014-06-18 23:28:09 +02004891 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004892 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
4893 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004894 }
4895
4896 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004897 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
4898 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004899 }
4900}
4901
Dave Gordonea702992015-07-09 19:29:02 +01004902/* Allocate a new GEM object and fill it with the supplied data */
4903struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004904i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01004905 const void *data, size_t size)
4906{
4907 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00004908 struct file *file;
4909 size_t offset;
4910 int err;
Dave Gordonea702992015-07-09 19:29:02 +01004911
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004912 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01004913 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01004914 return obj;
4915
Christian Königc0a51fd2018-02-16 13:43:38 +01004916 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01004917
Chris Wilsonbe062fa2017-03-17 19:46:48 +00004918 file = obj->base.filp;
4919 offset = 0;
4920 do {
4921 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
4922 struct page *page;
4923 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01004924
Chris Wilsonbe062fa2017-03-17 19:46:48 +00004925 err = pagecache_write_begin(file, file->f_mapping,
4926 offset, len, 0,
4927 &page, &pgdata);
4928 if (err < 0)
4929 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01004930
Chris Wilsonbe062fa2017-03-17 19:46:48 +00004931 vaddr = kmap(page);
4932 memcpy(vaddr, data, len);
4933 kunmap(page);
4934
4935 err = pagecache_write_end(file, file->f_mapping,
4936 offset, len, len,
4937 page, pgdata);
4938 if (err < 0)
4939 goto fail;
4940
4941 size -= len;
4942 data += len;
4943 offset += len;
4944 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01004945
4946 return obj;
4947
4948fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004949 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00004950 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01004951}
Chris Wilson96d77632016-10-28 13:58:33 +01004952
4953struct scatterlist *
4954i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
4955 unsigned int n,
4956 unsigned int *offset)
4957{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004958 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01004959 struct scatterlist *sg;
4960 unsigned int idx, count;
4961
4962 might_sleep();
4963 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004964 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01004965
4966 /* As we iterate forward through the sg, we record each entry in a
4967 * radixtree for quick repeated (backwards) lookups. If we have seen
4968 * this index previously, we will have an entry for it.
4969 *
4970 * Initial lookup is O(N), but this is amortized to O(1) for
4971 * sequential page access (where each new request is consecutive
4972 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
4973 * i.e. O(1) with a large constant!
4974 */
4975 if (n < READ_ONCE(iter->sg_idx))
4976 goto lookup;
4977
4978 mutex_lock(&iter->lock);
4979
4980 /* We prefer to reuse the last sg so that repeated lookup of this
4981 * (or the subsequent) sg are fast - comparing against the last
4982 * sg is faster than going through the radixtree.
4983 */
4984
4985 sg = iter->sg_pos;
4986 idx = iter->sg_idx;
4987 count = __sg_page_count(sg);
4988
4989 while (idx + count <= n) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04004990 void *entry;
4991 unsigned long i;
Chris Wilson96d77632016-10-28 13:58:33 +01004992 int ret;
4993
4994 /* If we cannot allocate and insert this entry, or the
4995 * individual pages from this range, cancel updating the
4996 * sg_idx so that on this lookup we are forced to linearly
4997 * scan onwards, but on future lookups we will try the
4998 * insertion again (in which case we need to be careful of
4999 * the error return reporting that we have already inserted
5000 * this index).
5001 */
5002 ret = radix_tree_insert(&iter->radix, idx, sg);
5003 if (ret && ret != -EEXIST)
5004 goto scan;
5005
Matthew Wilcox3159f942017-11-03 13:30:42 -04005006 entry = xa_mk_value(idx);
Chris Wilson96d77632016-10-28 13:58:33 +01005007 for (i = 1; i < count; i++) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005008 ret = radix_tree_insert(&iter->radix, idx + i, entry);
Chris Wilson96d77632016-10-28 13:58:33 +01005009 if (ret && ret != -EEXIST)
5010 goto scan;
5011 }
5012
5013 idx += count;
5014 sg = ____sg_next(sg);
5015 count = __sg_page_count(sg);
5016 }
5017
5018scan:
5019 iter->sg_pos = sg;
5020 iter->sg_idx = idx;
5021
5022 mutex_unlock(&iter->lock);
5023
5024 if (unlikely(n < idx)) /* insertion completed by another thread */
5025 goto lookup;
5026
5027 /* In case we failed to insert the entry into the radixtree, we need
5028 * to look beyond the current sg.
5029 */
5030 while (idx + count <= n) {
5031 idx += count;
5032 sg = ____sg_next(sg);
5033 count = __sg_page_count(sg);
5034 }
5035
5036 *offset = n - idx;
5037 return sg;
5038
5039lookup:
5040 rcu_read_lock();
5041
5042 sg = radix_tree_lookup(&iter->radix, n);
5043 GEM_BUG_ON(!sg);
5044
5045 /* If this index is in the middle of multi-page sg entry,
Matthew Wilcox3159f942017-11-03 13:30:42 -04005046 * the radix tree will contain a value entry that points
Chris Wilson96d77632016-10-28 13:58:33 +01005047 * to the start of that range. We will return the pointer to
5048 * the base page and the offset of this page within the
5049 * sg entry's range.
5050 */
5051 *offset = 0;
Matthew Wilcox3159f942017-11-03 13:30:42 -04005052 if (unlikely(xa_is_value(sg))) {
5053 unsigned long base = xa_to_value(sg);
Chris Wilson96d77632016-10-28 13:58:33 +01005054
5055 sg = radix_tree_lookup(&iter->radix, base);
5056 GEM_BUG_ON(!sg);
5057
5058 *offset = n - base;
5059 }
5060
5061 rcu_read_unlock();
5062
5063 return sg;
5064}
5065
5066struct page *
5067i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5068{
5069 struct scatterlist *sg;
5070 unsigned int offset;
5071
5072 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5073
5074 sg = i915_gem_object_get_sg(obj, n, &offset);
5075 return nth_page(sg_page(sg), offset);
5076}
5077
5078/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5079struct page *
5080i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5081 unsigned int n)
5082{
5083 struct page *page;
5084
5085 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005086 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005087 set_page_dirty(page);
5088
5089 return page;
5090}
5091
5092dma_addr_t
5093i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5094 unsigned long n)
5095{
5096 struct scatterlist *sg;
5097 unsigned int offset;
5098
5099 sg = i915_gem_object_get_sg(obj, n, &offset);
5100 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5101}
Chris Wilson935a2f72017-02-13 17:15:13 +00005102
Chris Wilson8eeb7902017-07-26 19:16:01 +01005103int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5104{
5105 struct sg_table *pages;
5106 int err;
5107
5108 if (align > obj->base.size)
5109 return -EINVAL;
5110
5111 if (obj->ops == &i915_gem_phys_ops)
5112 return 0;
5113
5114 if (obj->ops != &i915_gem_object_ops)
5115 return -EINVAL;
5116
5117 err = i915_gem_object_unbind(obj);
5118 if (err)
5119 return err;
5120
5121 mutex_lock(&obj->mm.lock);
5122
5123 if (obj->mm.madv != I915_MADV_WILLNEED) {
5124 err = -EFAULT;
5125 goto err_unlock;
5126 }
5127
5128 if (obj->mm.quirked) {
5129 err = -EFAULT;
5130 goto err_unlock;
5131 }
5132
5133 if (obj->mm.mapping) {
5134 err = -EBUSY;
5135 goto err_unlock;
5136 }
5137
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005138 pages = __i915_gem_object_unset_pages(obj);
Chris Wilsonf2123812017-10-16 12:40:37 +01005139
Chris Wilson8eeb7902017-07-26 19:16:01 +01005140 obj->ops = &i915_gem_phys_ops;
5141
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005142 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005143 if (err)
5144 goto err_xfer;
5145
5146 /* Perma-pin (until release) the physical set of pages */
5147 __i915_gem_object_pin_pages(obj);
5148
5149 if (!IS_ERR_OR_NULL(pages))
5150 i915_gem_object_ops.put_pages(obj, pages);
5151 mutex_unlock(&obj->mm.lock);
5152 return 0;
5153
5154err_xfer:
5155 obj->ops = &i915_gem_object_ops;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005156 if (!IS_ERR_OR_NULL(pages)) {
5157 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl);
5158
5159 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
5160 }
Chris Wilson8eeb7902017-07-26 19:16:01 +01005161err_unlock:
5162 mutex_unlock(&obj->mm.lock);
5163 return err;
5164}
5165
Chris Wilson935a2f72017-02-13 17:15:13 +00005166#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5167#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005168#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005169#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005170#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005171#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005172#include "selftests/i915_gem_coherency.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01005173#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005174#endif