]>
Commit | Line | Data |
---|---|---|
673a394b | 1 | /* |
be6a0376 | 2 | * Copyright © 2008-2015 Intel Corporation |
673a394b EA |
3 | * |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice (including the next | |
12 | * paragraph) shall be included in all copies or substantial portions of the | |
13 | * Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | * | |
23 | * Authors: | |
24 | * Eric Anholt <eric@anholt.net> | |
25 | * | |
26 | */ | |
27 | ||
760285e7 | 28 | #include <drm/drmP.h> |
0de23977 | 29 | #include <drm/drm_vma_manager.h> |
760285e7 | 30 | #include <drm/i915_drm.h> |
673a394b | 31 | #include "i915_drv.h" |
57822dc6 | 32 | #include "i915_gem_clflush.h" |
eb82289a | 33 | #include "i915_vgpu.h" |
1c5d22f7 | 34 | #include "i915_trace.h" |
652c393a | 35 | #include "intel_drv.h" |
5d723d7a | 36 | #include "intel_frontbuffer.h" |
0ccdacf6 | 37 | #include "intel_mocs.h" |
465c403c | 38 | #include "i915_gemfs.h" |
6b5e90f5 | 39 | #include <linux/dma-fence-array.h> |
fe3288b5 | 40 | #include <linux/kthread.h> |
c13d87ea | 41 | #include <linux/reservation.h> |
5949eac4 | 42 | #include <linux/shmem_fs.h> |
5a0e3ad6 | 43 | #include <linux/slab.h> |
20e4933c | 44 | #include <linux/stop_machine.h> |
673a394b | 45 | #include <linux/swap.h> |
79e53945 | 46 | #include <linux/pci.h> |
1286ff73 | 47 | #include <linux/dma-buf.h> |
673a394b | 48 | |
fbbd37b3 | 49 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
61050808 | 50 | |
2c22569b CW |
51 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
52 | { | |
e27ab73d | 53 | if (obj->cache_dirty) |
b50a5371 AS |
54 | return false; |
55 | ||
b8f55be6 | 56 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
2c22569b CW |
57 | return true; |
58 | ||
bd3d2252 | 59 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
2c22569b CW |
60 | } |
61 | ||
4f1959ee | 62 | static int |
bb6dc8d9 | 63 | insert_mappable_node(struct i915_ggtt *ggtt, |
4f1959ee AS |
64 | struct drm_mm_node *node, u32 size) |
65 | { | |
66 | memset(node, 0, sizeof(*node)); | |
4e64e553 CW |
67 | return drm_mm_insert_node_in_range(&ggtt->base.mm, node, |
68 | size, 0, I915_COLOR_UNEVICTABLE, | |
69 | 0, ggtt->mappable_end, | |
70 | DRM_MM_INSERT_LOW); | |
4f1959ee AS |
71 | } |
72 | ||
73 | static void | |
74 | remove_mappable_node(struct drm_mm_node *node) | |
75 | { | |
76 | drm_mm_remove_node(node); | |
77 | } | |
78 | ||
73aa808f CW |
79 | /* some bookkeeping */ |
80 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, | |
3ef7f228 | 81 | u64 size) |
73aa808f | 82 | { |
c20e8355 | 83 | spin_lock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
84 | dev_priv->mm.object_count++; |
85 | dev_priv->mm.object_memory += size; | |
c20e8355 | 86 | spin_unlock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
87 | } |
88 | ||
89 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, | |
3ef7f228 | 90 | u64 size) |
73aa808f | 91 | { |
c20e8355 | 92 | spin_lock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
93 | dev_priv->mm.object_count--; |
94 | dev_priv->mm.object_memory -= size; | |
c20e8355 | 95 | spin_unlock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
96 | } |
97 | ||
21dd3734 | 98 | static int |
33196ded | 99 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
30dbf0c0 | 100 | { |
30dbf0c0 CW |
101 | int ret; |
102 | ||
4c7d62c6 CW |
103 | might_sleep(); |
104 | ||
0a6759c6 DV |
105 | /* |
106 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging | |
107 | * userspace. If it takes that long something really bad is going on and | |
108 | * we should simply try to bail out and fail as gracefully as possible. | |
109 | */ | |
1f83fee0 | 110 | ret = wait_event_interruptible_timeout(error->reset_queue, |
8c185eca | 111 | !i915_reset_backoff(error), |
b52992c0 | 112 | I915_RESET_TIMEOUT); |
0a6759c6 DV |
113 | if (ret == 0) { |
114 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); | |
115 | return -EIO; | |
116 | } else if (ret < 0) { | |
30dbf0c0 | 117 | return ret; |
d98c52cf CW |
118 | } else { |
119 | return 0; | |
0a6759c6 | 120 | } |
30dbf0c0 CW |
121 | } |
122 | ||
54cf91dc | 123 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
76c1dec1 | 124 | { |
fac5e23e | 125 | struct drm_i915_private *dev_priv = to_i915(dev); |
76c1dec1 CW |
126 | int ret; |
127 | ||
33196ded | 128 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
76c1dec1 CW |
129 | if (ret) |
130 | return ret; | |
131 | ||
132 | ret = mutex_lock_interruptible(&dev->struct_mutex); | |
133 | if (ret) | |
134 | return ret; | |
135 | ||
76c1dec1 CW |
136 | return 0; |
137 | } | |
30dbf0c0 | 138 | |
5a125c3c EA |
139 | int |
140 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 141 | struct drm_file *file) |
5a125c3c | 142 | { |
72e96d64 | 143 | struct drm_i915_private *dev_priv = to_i915(dev); |
62106b4f | 144 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
72e96d64 | 145 | struct drm_i915_gem_get_aperture *args = data; |
ca1543be | 146 | struct i915_vma *vma; |
ff8f7975 | 147 | u64 pinned; |
5a125c3c | 148 | |
ff8f7975 | 149 | pinned = ggtt->base.reserved; |
73aa808f | 150 | mutex_lock(&dev->struct_mutex); |
1c7f4bca | 151 | list_for_each_entry(vma, &ggtt->base.active_list, vm_link) |
20dfbde4 | 152 | if (i915_vma_is_pinned(vma)) |
ca1543be | 153 | pinned += vma->node.size; |
1c7f4bca | 154 | list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link) |
20dfbde4 | 155 | if (i915_vma_is_pinned(vma)) |
ca1543be | 156 | pinned += vma->node.size; |
73aa808f | 157 | mutex_unlock(&dev->struct_mutex); |
5a125c3c | 158 | |
72e96d64 | 159 | args->aper_size = ggtt->base.total; |
0206e353 | 160 | args->aper_available_size = args->aper_size - pinned; |
6299f992 | 161 | |
5a125c3c EA |
162 | return 0; |
163 | } | |
164 | ||
b91b09ee | 165 | static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj) |
00731155 | 166 | { |
93c76a3d | 167 | struct address_space *mapping = obj->base.filp->f_mapping; |
dbb4351b | 168 | drm_dma_handle_t *phys; |
6a2c4232 CW |
169 | struct sg_table *st; |
170 | struct scatterlist *sg; | |
dbb4351b | 171 | char *vaddr; |
6a2c4232 | 172 | int i; |
b91b09ee | 173 | int err; |
00731155 | 174 | |
6a2c4232 | 175 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
b91b09ee | 176 | return -EINVAL; |
6a2c4232 | 177 | |
dbb4351b CW |
178 | /* Always aligning to the object size, allows a single allocation |
179 | * to handle all possible callers, and given typical object sizes, | |
180 | * the alignment of the buddy allocation will naturally match. | |
181 | */ | |
182 | phys = drm_pci_alloc(obj->base.dev, | |
750fae23 | 183 | roundup_pow_of_two(obj->base.size), |
dbb4351b CW |
184 | roundup_pow_of_two(obj->base.size)); |
185 | if (!phys) | |
b91b09ee | 186 | return -ENOMEM; |
dbb4351b CW |
187 | |
188 | vaddr = phys->vaddr; | |
6a2c4232 CW |
189 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
190 | struct page *page; | |
191 | char *src; | |
192 | ||
193 | page = shmem_read_mapping_page(mapping, i); | |
dbb4351b | 194 | if (IS_ERR(page)) { |
b91b09ee | 195 | err = PTR_ERR(page); |
dbb4351b CW |
196 | goto err_phys; |
197 | } | |
6a2c4232 CW |
198 | |
199 | src = kmap_atomic(page); | |
200 | memcpy(vaddr, src, PAGE_SIZE); | |
201 | drm_clflush_virt_range(vaddr, PAGE_SIZE); | |
202 | kunmap_atomic(src); | |
203 | ||
09cbfeaf | 204 | put_page(page); |
6a2c4232 CW |
205 | vaddr += PAGE_SIZE; |
206 | } | |
207 | ||
c033666a | 208 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
6a2c4232 CW |
209 | |
210 | st = kmalloc(sizeof(*st), GFP_KERNEL); | |
dbb4351b | 211 | if (!st) { |
b91b09ee | 212 | err = -ENOMEM; |
dbb4351b CW |
213 | goto err_phys; |
214 | } | |
6a2c4232 CW |
215 | |
216 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { | |
217 | kfree(st); | |
b91b09ee | 218 | err = -ENOMEM; |
dbb4351b | 219 | goto err_phys; |
6a2c4232 CW |
220 | } |
221 | ||
222 | sg = st->sgl; | |
223 | sg->offset = 0; | |
224 | sg->length = obj->base.size; | |
00731155 | 225 | |
dbb4351b | 226 | sg_dma_address(sg) = phys->busaddr; |
6a2c4232 CW |
227 | sg_dma_len(sg) = obj->base.size; |
228 | ||
dbb4351b | 229 | obj->phys_handle = phys; |
b91b09ee | 230 | |
a5c08166 | 231 | __i915_gem_object_set_pages(obj, st, sg->length); |
b91b09ee MA |
232 | |
233 | return 0; | |
dbb4351b CW |
234 | |
235 | err_phys: | |
236 | drm_pci_free(obj->base.dev, phys); | |
b91b09ee MA |
237 | |
238 | return err; | |
6a2c4232 CW |
239 | } |
240 | ||
e27ab73d CW |
241 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
242 | { | |
243 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
244 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
245 | if (cpu_write_needs_clflush(obj)) | |
246 | obj->cache_dirty = true; | |
247 | } | |
248 | ||
6a2c4232 | 249 | static void |
2b3c8317 | 250 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
e5facdf9 CW |
251 | struct sg_table *pages, |
252 | bool needs_clflush) | |
6a2c4232 | 253 | { |
a4f5ea64 | 254 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
00731155 | 255 | |
a4f5ea64 CW |
256 | if (obj->mm.madv == I915_MADV_DONTNEED) |
257 | obj->mm.dirty = false; | |
6a2c4232 | 258 | |
e5facdf9 CW |
259 | if (needs_clflush && |
260 | (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 && | |
b8f55be6 | 261 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
2b3c8317 | 262 | drm_clflush_sg(pages); |
03ac84f1 | 263 | |
e27ab73d | 264 | __start_cpu_write(obj); |
03ac84f1 CW |
265 | } |
266 | ||
267 | static void | |
268 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, | |
269 | struct sg_table *pages) | |
270 | { | |
e5facdf9 | 271 | __i915_gem_object_release_shmem(obj, pages, false); |
03ac84f1 | 272 | |
a4f5ea64 | 273 | if (obj->mm.dirty) { |
93c76a3d | 274 | struct address_space *mapping = obj->base.filp->f_mapping; |
6a2c4232 | 275 | char *vaddr = obj->phys_handle->vaddr; |
00731155 CW |
276 | int i; |
277 | ||
278 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { | |
6a2c4232 CW |
279 | struct page *page; |
280 | char *dst; | |
281 | ||
282 | page = shmem_read_mapping_page(mapping, i); | |
283 | if (IS_ERR(page)) | |
284 | continue; | |
285 | ||
286 | dst = kmap_atomic(page); | |
287 | drm_clflush_virt_range(vaddr, PAGE_SIZE); | |
288 | memcpy(dst, vaddr, PAGE_SIZE); | |
289 | kunmap_atomic(dst); | |
290 | ||
291 | set_page_dirty(page); | |
a4f5ea64 | 292 | if (obj->mm.madv == I915_MADV_WILLNEED) |
00731155 | 293 | mark_page_accessed(page); |
09cbfeaf | 294 | put_page(page); |
00731155 CW |
295 | vaddr += PAGE_SIZE; |
296 | } | |
a4f5ea64 | 297 | obj->mm.dirty = false; |
00731155 CW |
298 | } |
299 | ||
03ac84f1 CW |
300 | sg_free_table(pages); |
301 | kfree(pages); | |
dbb4351b CW |
302 | |
303 | drm_pci_free(obj->base.dev, obj->phys_handle); | |
6a2c4232 CW |
304 | } |
305 | ||
306 | static void | |
307 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) | |
308 | { | |
a4f5ea64 | 309 | i915_gem_object_unpin_pages(obj); |
6a2c4232 CW |
310 | } |
311 | ||
312 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { | |
313 | .get_pages = i915_gem_object_get_pages_phys, | |
314 | .put_pages = i915_gem_object_put_pages_phys, | |
315 | .release = i915_gem_object_release_phys, | |
316 | }; | |
317 | ||
581ab1fe CW |
318 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
319 | ||
35a9611c | 320 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
aa653a68 CW |
321 | { |
322 | struct i915_vma *vma; | |
323 | LIST_HEAD(still_in_list); | |
02bef8f9 CW |
324 | int ret; |
325 | ||
326 | lockdep_assert_held(&obj->base.dev->struct_mutex); | |
aa653a68 | 327 | |
02bef8f9 CW |
328 | /* Closed vma are removed from the obj->vma_list - but they may |
329 | * still have an active binding on the object. To remove those we | |
330 | * must wait for all rendering to complete to the object (as unbinding | |
331 | * must anyway), and retire the requests. | |
aa653a68 | 332 | */ |
2797c4a1 | 333 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
02bef8f9 CW |
334 | if (ret) |
335 | return ret; | |
336 | ||
aa653a68 CW |
337 | while ((vma = list_first_entry_or_null(&obj->vma_list, |
338 | struct i915_vma, | |
339 | obj_link))) { | |
340 | list_move_tail(&vma->obj_link, &still_in_list); | |
341 | ret = i915_vma_unbind(vma); | |
342 | if (ret) | |
343 | break; | |
344 | } | |
345 | list_splice(&still_in_list, &obj->vma_list); | |
346 | ||
347 | return ret; | |
348 | } | |
349 | ||
e95433c7 CW |
350 | static long |
351 | i915_gem_object_wait_fence(struct dma_fence *fence, | |
352 | unsigned int flags, | |
353 | long timeout, | |
562d9bae | 354 | struct intel_rps_client *rps_client) |
00e60f26 | 355 | { |
e95433c7 | 356 | struct drm_i915_gem_request *rq; |
00e60f26 | 357 | |
e95433c7 | 358 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
00e60f26 | 359 | |
e95433c7 CW |
360 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
361 | return timeout; | |
362 | ||
363 | if (!dma_fence_is_i915(fence)) | |
364 | return dma_fence_wait_timeout(fence, | |
365 | flags & I915_WAIT_INTERRUPTIBLE, | |
366 | timeout); | |
367 | ||
368 | rq = to_request(fence); | |
369 | if (i915_gem_request_completed(rq)) | |
370 | goto out; | |
371 | ||
372 | /* This client is about to stall waiting for the GPU. In many cases | |
373 | * this is undesirable and limits the throughput of the system, as | |
374 | * many clients cannot continue processing user input/output whilst | |
375 | * blocked. RPS autotuning may take tens of milliseconds to respond | |
376 | * to the GPU load and thus incurs additional latency for the client. | |
377 | * We can circumvent that by promoting the GPU frequency to maximum | |
378 | * before we wait. This makes the GPU throttle up much more quickly | |
379 | * (good for benchmarks and user experience, e.g. window animations), | |
380 | * but at a cost of spending more power processing the workload | |
381 | * (bad for battery). Not all clients even want their results | |
382 | * immediately and for them we should just let the GPU select its own | |
383 | * frequency to maximise efficiency. To prevent a single client from | |
384 | * forcing the clocks too high for the whole system, we only allow | |
385 | * each client to waitboost once in a busy period. | |
386 | */ | |
562d9bae | 387 | if (rps_client) { |
e95433c7 | 388 | if (INTEL_GEN(rq->i915) >= 6) |
562d9bae | 389 | gen6_rps_boost(rq, rps_client); |
e95433c7 | 390 | else |
562d9bae | 391 | rps_client = NULL; |
00e60f26 CW |
392 | } |
393 | ||
e95433c7 CW |
394 | timeout = i915_wait_request(rq, flags, timeout); |
395 | ||
396 | out: | |
397 | if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq)) | |
398 | i915_gem_request_retire_upto(rq); | |
399 | ||
e95433c7 CW |
400 | return timeout; |
401 | } | |
402 | ||
403 | static long | |
404 | i915_gem_object_wait_reservation(struct reservation_object *resv, | |
405 | unsigned int flags, | |
406 | long timeout, | |
562d9bae | 407 | struct intel_rps_client *rps_client) |
e95433c7 | 408 | { |
e54ca977 | 409 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
e95433c7 | 410 | struct dma_fence *excl; |
e54ca977 | 411 | bool prune_fences = false; |
e95433c7 CW |
412 | |
413 | if (flags & I915_WAIT_ALL) { | |
414 | struct dma_fence **shared; | |
415 | unsigned int count, i; | |
00e60f26 CW |
416 | int ret; |
417 | ||
e95433c7 CW |
418 | ret = reservation_object_get_fences_rcu(resv, |
419 | &excl, &count, &shared); | |
00e60f26 CW |
420 | if (ret) |
421 | return ret; | |
00e60f26 | 422 | |
e95433c7 CW |
423 | for (i = 0; i < count; i++) { |
424 | timeout = i915_gem_object_wait_fence(shared[i], | |
425 | flags, timeout, | |
562d9bae | 426 | rps_client); |
d892e939 | 427 | if (timeout < 0) |
e95433c7 | 428 | break; |
00e60f26 | 429 | |
e95433c7 CW |
430 | dma_fence_put(shared[i]); |
431 | } | |
432 | ||
433 | for (; i < count; i++) | |
434 | dma_fence_put(shared[i]); | |
435 | kfree(shared); | |
e54ca977 CW |
436 | |
437 | prune_fences = count && timeout >= 0; | |
e95433c7 CW |
438 | } else { |
439 | excl = reservation_object_get_excl_rcu(resv); | |
00e60f26 CW |
440 | } |
441 | ||
e54ca977 | 442 | if (excl && timeout >= 0) { |
562d9bae SAK |
443 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
444 | rps_client); | |
e54ca977 CW |
445 | prune_fences = timeout >= 0; |
446 | } | |
e95433c7 CW |
447 | |
448 | dma_fence_put(excl); | |
449 | ||
03d1cac6 CW |
450 | /* Oportunistically prune the fences iff we know they have *all* been |
451 | * signaled and that the reservation object has not been changed (i.e. | |
452 | * no new fences have been added). | |
453 | */ | |
e54ca977 | 454 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
03d1cac6 CW |
455 | if (reservation_object_trylock(resv)) { |
456 | if (!__read_seqcount_retry(&resv->seq, seq)) | |
457 | reservation_object_add_excl_fence(resv, NULL); | |
458 | reservation_object_unlock(resv); | |
459 | } | |
e54ca977 CW |
460 | } |
461 | ||
e95433c7 | 462 | return timeout; |
00e60f26 CW |
463 | } |
464 | ||
6b5e90f5 CW |
465 | static void __fence_set_priority(struct dma_fence *fence, int prio) |
466 | { | |
467 | struct drm_i915_gem_request *rq; | |
468 | struct intel_engine_cs *engine; | |
469 | ||
5005c851 | 470 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
6b5e90f5 CW |
471 | return; |
472 | ||
473 | rq = to_request(fence); | |
474 | engine = rq->engine; | |
475 | if (!engine->schedule) | |
476 | return; | |
477 | ||
478 | engine->schedule(rq, prio); | |
479 | } | |
480 | ||
481 | static void fence_set_priority(struct dma_fence *fence, int prio) | |
482 | { | |
483 | /* Recurse once into a fence-array */ | |
484 | if (dma_fence_is_array(fence)) { | |
485 | struct dma_fence_array *array = to_dma_fence_array(fence); | |
486 | int i; | |
487 | ||
488 | for (i = 0; i < array->num_fences; i++) | |
489 | __fence_set_priority(array->fences[i], prio); | |
490 | } else { | |
491 | __fence_set_priority(fence, prio); | |
492 | } | |
493 | } | |
494 | ||
495 | int | |
496 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, | |
497 | unsigned int flags, | |
498 | int prio) | |
499 | { | |
500 | struct dma_fence *excl; | |
501 | ||
502 | if (flags & I915_WAIT_ALL) { | |
503 | struct dma_fence **shared; | |
504 | unsigned int count, i; | |
505 | int ret; | |
506 | ||
507 | ret = reservation_object_get_fences_rcu(obj->resv, | |
508 | &excl, &count, &shared); | |
509 | if (ret) | |
510 | return ret; | |
511 | ||
512 | for (i = 0; i < count; i++) { | |
513 | fence_set_priority(shared[i], prio); | |
514 | dma_fence_put(shared[i]); | |
515 | } | |
516 | ||
517 | kfree(shared); | |
518 | } else { | |
519 | excl = reservation_object_get_excl_rcu(obj->resv); | |
520 | } | |
521 | ||
522 | if (excl) { | |
523 | fence_set_priority(excl, prio); | |
524 | dma_fence_put(excl); | |
525 | } | |
526 | return 0; | |
527 | } | |
528 | ||
e95433c7 CW |
529 | /** |
530 | * Waits for rendering to the object to be completed | |
531 | * @obj: i915 gem object | |
532 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) | |
533 | * @timeout: how long to wait | |
534 | * @rps: client (user process) to charge for any waitboosting | |
00e60f26 | 535 | */ |
e95433c7 CW |
536 | int |
537 | i915_gem_object_wait(struct drm_i915_gem_object *obj, | |
538 | unsigned int flags, | |
539 | long timeout, | |
562d9bae | 540 | struct intel_rps_client *rps_client) |
00e60f26 | 541 | { |
e95433c7 CW |
542 | might_sleep(); |
543 | #if IS_ENABLED(CONFIG_LOCKDEP) | |
544 | GEM_BUG_ON(debug_locks && | |
545 | !!lockdep_is_held(&obj->base.dev->struct_mutex) != | |
546 | !!(flags & I915_WAIT_LOCKED)); | |
547 | #endif | |
548 | GEM_BUG_ON(timeout < 0); | |
00e60f26 | 549 | |
d07f0e59 CW |
550 | timeout = i915_gem_object_wait_reservation(obj->resv, |
551 | flags, timeout, | |
562d9bae | 552 | rps_client); |
e95433c7 | 553 | return timeout < 0 ? timeout : 0; |
00e60f26 CW |
554 | } |
555 | ||
556 | static struct intel_rps_client *to_rps_client(struct drm_file *file) | |
557 | { | |
558 | struct drm_i915_file_private *fpriv = file->driver_priv; | |
559 | ||
562d9bae | 560 | return &fpriv->rps_client; |
00e60f26 CW |
561 | } |
562 | ||
00731155 CW |
563 | static int |
564 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, | |
565 | struct drm_i915_gem_pwrite *args, | |
03ac84f1 | 566 | struct drm_file *file) |
00731155 | 567 | { |
00731155 | 568 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
3ed605bc | 569 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
6a2c4232 CW |
570 | |
571 | /* We manually control the domain here and pretend that it | |
572 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. | |
573 | */ | |
77a0d1ca | 574 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
10466d2a CW |
575 | if (copy_from_user(vaddr, user_data, args->size)) |
576 | return -EFAULT; | |
00731155 | 577 | |
6a2c4232 | 578 | drm_clflush_virt_range(vaddr, args->size); |
10466d2a | 579 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
063e4e6b | 580 | |
d59b21ec | 581 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
10466d2a | 582 | return 0; |
00731155 CW |
583 | } |
584 | ||
187685cb | 585 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
42dcedd4 | 586 | { |
efab6d8d | 587 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
42dcedd4 CW |
588 | } |
589 | ||
590 | void i915_gem_object_free(struct drm_i915_gem_object *obj) | |
591 | { | |
fac5e23e | 592 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
efab6d8d | 593 | kmem_cache_free(dev_priv->objects, obj); |
42dcedd4 CW |
594 | } |
595 | ||
ff72145b DA |
596 | static int |
597 | i915_gem_create(struct drm_file *file, | |
12d79d78 | 598 | struct drm_i915_private *dev_priv, |
ff72145b DA |
599 | uint64_t size, |
600 | uint32_t *handle_p) | |
673a394b | 601 | { |
05394f39 | 602 | struct drm_i915_gem_object *obj; |
a1a2d1d3 PP |
603 | int ret; |
604 | u32 handle; | |
673a394b | 605 | |
ff72145b | 606 | size = roundup(size, PAGE_SIZE); |
8ffc0246 CW |
607 | if (size == 0) |
608 | return -EINVAL; | |
673a394b EA |
609 | |
610 | /* Allocate the new object */ | |
12d79d78 | 611 | obj = i915_gem_object_create(dev_priv, size); |
fe3db79b CW |
612 | if (IS_ERR(obj)) |
613 | return PTR_ERR(obj); | |
673a394b | 614 | |
05394f39 | 615 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
202f2fef | 616 | /* drop reference from allocate - handle holds it now */ |
f0cd5182 | 617 | i915_gem_object_put(obj); |
d861e338 DV |
618 | if (ret) |
619 | return ret; | |
202f2fef | 620 | |
ff72145b | 621 | *handle_p = handle; |
673a394b EA |
622 | return 0; |
623 | } | |
624 | ||
ff72145b DA |
625 | int |
626 | i915_gem_dumb_create(struct drm_file *file, | |
627 | struct drm_device *dev, | |
628 | struct drm_mode_create_dumb *args) | |
629 | { | |
630 | /* have to work out size/pitch and return them */ | |
de45eaf7 | 631 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
ff72145b | 632 | args->size = args->pitch * args->height; |
12d79d78 | 633 | return i915_gem_create(file, to_i915(dev), |
da6b51d0 | 634 | args->size, &args->handle); |
ff72145b DA |
635 | } |
636 | ||
e27ab73d CW |
637 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
638 | { | |
639 | return !(obj->cache_level == I915_CACHE_NONE || | |
640 | obj->cache_level == I915_CACHE_WT); | |
641 | } | |
642 | ||
ff72145b DA |
643 | /** |
644 | * Creates a new mm object and returns a handle to it. | |
14bb2c11 TU |
645 | * @dev: drm device pointer |
646 | * @data: ioctl data blob | |
647 | * @file: drm file pointer | |
ff72145b DA |
648 | */ |
649 | int | |
650 | i915_gem_create_ioctl(struct drm_device *dev, void *data, | |
651 | struct drm_file *file) | |
652 | { | |
12d79d78 | 653 | struct drm_i915_private *dev_priv = to_i915(dev); |
ff72145b | 654 | struct drm_i915_gem_create *args = data; |
63ed2cb2 | 655 | |
12d79d78 | 656 | i915_gem_flush_free_objects(dev_priv); |
fbbd37b3 | 657 | |
12d79d78 | 658 | return i915_gem_create(file, dev_priv, |
da6b51d0 | 659 | args->size, &args->handle); |
ff72145b DA |
660 | } |
661 | ||
ef74921b CW |
662 | static inline enum fb_op_origin |
663 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) | |
664 | { | |
665 | return (domain == I915_GEM_DOMAIN_GTT ? | |
666 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); | |
667 | } | |
668 | ||
669 | static void | |
670 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) | |
671 | { | |
672 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); | |
673 | ||
674 | if (!(obj->base.write_domain & flush_domains)) | |
675 | return; | |
676 | ||
677 | /* No actual flushing is required for the GTT write domain. Writes | |
678 | * to it "immediately" go to main memory as far as we know, so there's | |
679 | * no chipset flush. It also doesn't land in render cache. | |
680 | * | |
681 | * However, we do have to enforce the order so that all writes through | |
682 | * the GTT land before any writes to the device, such as updates to | |
683 | * the GATT itself. | |
684 | * | |
685 | * We also have to wait a bit for the writes to land from the GTT. | |
686 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip | |
687 | * timing. This issue has only been observed when switching quickly | |
688 | * between GTT writes and CPU reads from inside the kernel on recent hw, | |
689 | * and it appears to only affect discrete GTT blocks (i.e. on LLC | |
690 | * system agents we cannot reproduce this behaviour). | |
691 | */ | |
692 | wmb(); | |
693 | ||
694 | switch (obj->base.write_domain) { | |
695 | case I915_GEM_DOMAIN_GTT: | |
c5ba5b24 | 696 | if (!HAS_LLC(dev_priv)) { |
0168bdfc CW |
697 | intel_runtime_pm_get(dev_priv); |
698 | spin_lock_irq(&dev_priv->uncore.lock); | |
c5ba5b24 | 699 | POSTING_READ_FW(RING_HEAD(dev_priv->engine[RCS]->mmio_base)); |
0168bdfc CW |
700 | spin_unlock_irq(&dev_priv->uncore.lock); |
701 | intel_runtime_pm_put(dev_priv); | |
ef74921b CW |
702 | } |
703 | ||
704 | intel_fb_obj_flush(obj, | |
705 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); | |
706 | break; | |
707 | ||
708 | case I915_GEM_DOMAIN_CPU: | |
709 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); | |
710 | break; | |
e27ab73d CW |
711 | |
712 | case I915_GEM_DOMAIN_RENDER: | |
713 | if (gpu_write_needs_clflush(obj)) | |
714 | obj->cache_dirty = true; | |
715 | break; | |
ef74921b CW |
716 | } |
717 | ||
718 | obj->base.write_domain = 0; | |
719 | } | |
720 | ||
8461d226 DV |
721 | static inline int |
722 | __copy_to_user_swizzled(char __user *cpu_vaddr, | |
723 | const char *gpu_vaddr, int gpu_offset, | |
724 | int length) | |
725 | { | |
726 | int ret, cpu_offset = 0; | |
727 | ||
728 | while (length > 0) { | |
729 | int cacheline_end = ALIGN(gpu_offset + 1, 64); | |
730 | int this_length = min(cacheline_end - gpu_offset, length); | |
731 | int swizzled_gpu_offset = gpu_offset ^ 64; | |
732 | ||
733 | ret = __copy_to_user(cpu_vaddr + cpu_offset, | |
734 | gpu_vaddr + swizzled_gpu_offset, | |
735 | this_length); | |
736 | if (ret) | |
737 | return ret + length; | |
738 | ||
739 | cpu_offset += this_length; | |
740 | gpu_offset += this_length; | |
741 | length -= this_length; | |
742 | } | |
743 | ||
744 | return 0; | |
745 | } | |
746 | ||
8c59967c | 747 | static inline int |
4f0c7cfb BW |
748 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
749 | const char __user *cpu_vaddr, | |
8c59967c DV |
750 | int length) |
751 | { | |
752 | int ret, cpu_offset = 0; | |
753 | ||
754 | while (length > 0) { | |
755 | int cacheline_end = ALIGN(gpu_offset + 1, 64); | |
756 | int this_length = min(cacheline_end - gpu_offset, length); | |
757 | int swizzled_gpu_offset = gpu_offset ^ 64; | |
758 | ||
759 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, | |
760 | cpu_vaddr + cpu_offset, | |
761 | this_length); | |
762 | if (ret) | |
763 | return ret + length; | |
764 | ||
765 | cpu_offset += this_length; | |
766 | gpu_offset += this_length; | |
767 | length -= this_length; | |
768 | } | |
769 | ||
770 | return 0; | |
771 | } | |
772 | ||
4c914c0c BV |
773 | /* |
774 | * Pins the specified object's pages and synchronizes the object with | |
775 | * GPU accesses. Sets needs_clflush to non-zero if the caller should | |
776 | * flush the object from the CPU cache. | |
777 | */ | |
778 | int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj, | |
43394c7d | 779 | unsigned int *needs_clflush) |
4c914c0c BV |
780 | { |
781 | int ret; | |
782 | ||
e95433c7 | 783 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
4c914c0c | 784 | |
e95433c7 | 785 | *needs_clflush = 0; |
43394c7d CW |
786 | if (!i915_gem_object_has_struct_page(obj)) |
787 | return -ENODEV; | |
4c914c0c | 788 | |
e95433c7 CW |
789 | ret = i915_gem_object_wait(obj, |
790 | I915_WAIT_INTERRUPTIBLE | | |
791 | I915_WAIT_LOCKED, | |
792 | MAX_SCHEDULE_TIMEOUT, | |
793 | NULL); | |
c13d87ea CW |
794 | if (ret) |
795 | return ret; | |
796 | ||
a4f5ea64 | 797 | ret = i915_gem_object_pin_pages(obj); |
9764951e CW |
798 | if (ret) |
799 | return ret; | |
800 | ||
b8f55be6 CW |
801 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
802 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { | |
7f5f95d8 CW |
803 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
804 | if (ret) | |
805 | goto err_unpin; | |
806 | else | |
807 | goto out; | |
808 | } | |
809 | ||
ef74921b | 810 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
a314d5cb | 811 | |
43394c7d CW |
812 | /* If we're not in the cpu read domain, set ourself into the gtt |
813 | * read domain and manually flush cachelines (if required). This | |
814 | * optimizes for the case when the gpu will dirty the data | |
815 | * anyway again before the next pread happens. | |
816 | */ | |
e27ab73d CW |
817 | if (!obj->cache_dirty && |
818 | !(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) | |
7f5f95d8 | 819 | *needs_clflush = CLFLUSH_BEFORE; |
4c914c0c | 820 | |
7f5f95d8 | 821 | out: |
9764951e | 822 | /* return with the pages pinned */ |
43394c7d | 823 | return 0; |
9764951e CW |
824 | |
825 | err_unpin: | |
826 | i915_gem_object_unpin_pages(obj); | |
827 | return ret; | |
43394c7d CW |
828 | } |
829 | ||
830 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, | |
831 | unsigned int *needs_clflush) | |
832 | { | |
833 | int ret; | |
834 | ||
e95433c7 CW |
835 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
836 | ||
43394c7d CW |
837 | *needs_clflush = 0; |
838 | if (!i915_gem_object_has_struct_page(obj)) | |
839 | return -ENODEV; | |
840 | ||
e95433c7 CW |
841 | ret = i915_gem_object_wait(obj, |
842 | I915_WAIT_INTERRUPTIBLE | | |
843 | I915_WAIT_LOCKED | | |
844 | I915_WAIT_ALL, | |
845 | MAX_SCHEDULE_TIMEOUT, | |
846 | NULL); | |
43394c7d CW |
847 | if (ret) |
848 | return ret; | |
849 | ||
a4f5ea64 | 850 | ret = i915_gem_object_pin_pages(obj); |
9764951e CW |
851 | if (ret) |
852 | return ret; | |
853 | ||
b8f55be6 CW |
854 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
855 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { | |
7f5f95d8 CW |
856 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
857 | if (ret) | |
858 | goto err_unpin; | |
859 | else | |
860 | goto out; | |
861 | } | |
862 | ||
ef74921b | 863 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
a314d5cb | 864 | |
43394c7d CW |
865 | /* If we're not in the cpu write domain, set ourself into the |
866 | * gtt write domain and manually flush cachelines (as required). | |
867 | * This optimizes for the case when the gpu will use the data | |
868 | * right away and we therefore have to clflush anyway. | |
869 | */ | |
e27ab73d | 870 | if (!obj->cache_dirty) { |
7f5f95d8 | 871 | *needs_clflush |= CLFLUSH_AFTER; |
43394c7d | 872 | |
e27ab73d CW |
873 | /* |
874 | * Same trick applies to invalidate partially written | |
875 | * cachelines read before writing. | |
876 | */ | |
877 | if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) | |
878 | *needs_clflush |= CLFLUSH_BEFORE; | |
879 | } | |
43394c7d | 880 | |
7f5f95d8 | 881 | out: |
43394c7d | 882 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
a4f5ea64 | 883 | obj->mm.dirty = true; |
9764951e | 884 | /* return with the pages pinned */ |
43394c7d | 885 | return 0; |
9764951e CW |
886 | |
887 | err_unpin: | |
888 | i915_gem_object_unpin_pages(obj); | |
889 | return ret; | |
4c914c0c BV |
890 | } |
891 | ||
23c18c71 DV |
892 | static void |
893 | shmem_clflush_swizzled_range(char *addr, unsigned long length, | |
894 | bool swizzled) | |
895 | { | |
e7e58eb5 | 896 | if (unlikely(swizzled)) { |
23c18c71 DV |
897 | unsigned long start = (unsigned long) addr; |
898 | unsigned long end = (unsigned long) addr + length; | |
899 | ||
900 | /* For swizzling simply ensure that we always flush both | |
901 | * channels. Lame, but simple and it works. Swizzled | |
902 | * pwrite/pread is far from a hotpath - current userspace | |
903 | * doesn't use it at all. */ | |
904 | start = round_down(start, 128); | |
905 | end = round_up(end, 128); | |
906 | ||
907 | drm_clflush_virt_range((void *)start, end - start); | |
908 | } else { | |
909 | drm_clflush_virt_range(addr, length); | |
910 | } | |
911 | ||
912 | } | |
913 | ||
d174bd64 DV |
914 | /* Only difference to the fast-path function is that this can handle bit17 |
915 | * and uses non-atomic copy and kmap functions. */ | |
916 | static int | |
bb6dc8d9 | 917 | shmem_pread_slow(struct page *page, int offset, int length, |
d174bd64 DV |
918 | char __user *user_data, |
919 | bool page_do_bit17_swizzling, bool needs_clflush) | |
920 | { | |
921 | char *vaddr; | |
922 | int ret; | |
923 | ||
924 | vaddr = kmap(page); | |
925 | if (needs_clflush) | |
bb6dc8d9 | 926 | shmem_clflush_swizzled_range(vaddr + offset, length, |
23c18c71 | 927 | page_do_bit17_swizzling); |
d174bd64 DV |
928 | |
929 | if (page_do_bit17_swizzling) | |
bb6dc8d9 | 930 | ret = __copy_to_user_swizzled(user_data, vaddr, offset, length); |
d174bd64 | 931 | else |
bb6dc8d9 | 932 | ret = __copy_to_user(user_data, vaddr + offset, length); |
d174bd64 DV |
933 | kunmap(page); |
934 | ||
f60d7f0c | 935 | return ret ? - EFAULT : 0; |
d174bd64 DV |
936 | } |
937 | ||
bb6dc8d9 CW |
938 | static int |
939 | shmem_pread(struct page *page, int offset, int length, char __user *user_data, | |
940 | bool page_do_bit17_swizzling, bool needs_clflush) | |
941 | { | |
942 | int ret; | |
943 | ||
944 | ret = -ENODEV; | |
945 | if (!page_do_bit17_swizzling) { | |
946 | char *vaddr = kmap_atomic(page); | |
947 | ||
948 | if (needs_clflush) | |
949 | drm_clflush_virt_range(vaddr + offset, length); | |
950 | ret = __copy_to_user_inatomic(user_data, vaddr + offset, length); | |
951 | kunmap_atomic(vaddr); | |
952 | } | |
953 | if (ret == 0) | |
954 | return 0; | |
955 | ||
956 | return shmem_pread_slow(page, offset, length, user_data, | |
957 | page_do_bit17_swizzling, needs_clflush); | |
958 | } | |
959 | ||
960 | static int | |
961 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, | |
962 | struct drm_i915_gem_pread *args) | |
963 | { | |
964 | char __user *user_data; | |
965 | u64 remain; | |
966 | unsigned int obj_do_bit17_swizzling; | |
967 | unsigned int needs_clflush; | |
968 | unsigned int idx, offset; | |
969 | int ret; | |
970 | ||
971 | obj_do_bit17_swizzling = 0; | |
972 | if (i915_gem_object_needs_bit17_swizzle(obj)) | |
973 | obj_do_bit17_swizzling = BIT(17); | |
974 | ||
975 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); | |
976 | if (ret) | |
977 | return ret; | |
978 | ||
979 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); | |
980 | mutex_unlock(&obj->base.dev->struct_mutex); | |
981 | if (ret) | |
982 | return ret; | |
983 | ||
984 | remain = args->size; | |
985 | user_data = u64_to_user_ptr(args->data_ptr); | |
986 | offset = offset_in_page(args->offset); | |
987 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { | |
988 | struct page *page = i915_gem_object_get_page(obj, idx); | |
3500d559 | 989 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
bb6dc8d9 CW |
990 | |
991 | ret = shmem_pread(page, offset, length, user_data, | |
992 | page_to_phys(page) & obj_do_bit17_swizzling, | |
993 | needs_clflush); | |
994 | if (ret) | |
995 | break; | |
996 | ||
997 | remain -= length; | |
998 | user_data += length; | |
999 | offset = 0; | |
1000 | } | |
1001 | ||
1002 | i915_gem_obj_finish_shmem_access(obj); | |
1003 | return ret; | |
1004 | } | |
1005 | ||
1006 | static inline bool | |
1007 | gtt_user_read(struct io_mapping *mapping, | |
1008 | loff_t base, int offset, | |
1009 | char __user *user_data, int length) | |
b50a5371 | 1010 | { |
afe722be | 1011 | void __iomem *vaddr; |
bb6dc8d9 | 1012 | unsigned long unwritten; |
b50a5371 | 1013 | |
b50a5371 | 1014 | /* We can use the cpu mem copy function because this is X86. */ |
afe722be VS |
1015 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
1016 | unwritten = __copy_to_user_inatomic(user_data, | |
1017 | (void __force *)vaddr + offset, | |
1018 | length); | |
bb6dc8d9 CW |
1019 | io_mapping_unmap_atomic(vaddr); |
1020 | if (unwritten) { | |
afe722be VS |
1021 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
1022 | unwritten = copy_to_user(user_data, | |
1023 | (void __force *)vaddr + offset, | |
1024 | length); | |
bb6dc8d9 CW |
1025 | io_mapping_unmap(vaddr); |
1026 | } | |
b50a5371 AS |
1027 | return unwritten; |
1028 | } | |
1029 | ||
1030 | static int | |
bb6dc8d9 CW |
1031 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
1032 | const struct drm_i915_gem_pread *args) | |
b50a5371 | 1033 | { |
bb6dc8d9 CW |
1034 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
1035 | struct i915_ggtt *ggtt = &i915->ggtt; | |
b50a5371 | 1036 | struct drm_mm_node node; |
bb6dc8d9 CW |
1037 | struct i915_vma *vma; |
1038 | void __user *user_data; | |
1039 | u64 remain, offset; | |
b50a5371 AS |
1040 | int ret; |
1041 | ||
bb6dc8d9 CW |
1042 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
1043 | if (ret) | |
1044 | return ret; | |
1045 | ||
1046 | intel_runtime_pm_get(i915); | |
1047 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, | |
a3259ca9 CW |
1048 | PIN_MAPPABLE | |
1049 | PIN_NONFAULT | | |
1050 | PIN_NONBLOCK); | |
18034584 CW |
1051 | if (!IS_ERR(vma)) { |
1052 | node.start = i915_ggtt_offset(vma); | |
1053 | node.allocated = false; | |
49ef5294 | 1054 | ret = i915_vma_put_fence(vma); |
18034584 CW |
1055 | if (ret) { |
1056 | i915_vma_unpin(vma); | |
1057 | vma = ERR_PTR(ret); | |
1058 | } | |
1059 | } | |
058d88c4 | 1060 | if (IS_ERR(vma)) { |
bb6dc8d9 | 1061 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
b50a5371 | 1062 | if (ret) |
bb6dc8d9 CW |
1063 | goto out_unlock; |
1064 | GEM_BUG_ON(!node.allocated); | |
b50a5371 AS |
1065 | } |
1066 | ||
1067 | ret = i915_gem_object_set_to_gtt_domain(obj, false); | |
1068 | if (ret) | |
1069 | goto out_unpin; | |
1070 | ||
bb6dc8d9 | 1071 | mutex_unlock(&i915->drm.struct_mutex); |
b50a5371 | 1072 | |
bb6dc8d9 CW |
1073 | user_data = u64_to_user_ptr(args->data_ptr); |
1074 | remain = args->size; | |
1075 | offset = args->offset; | |
b50a5371 AS |
1076 | |
1077 | while (remain > 0) { | |
1078 | /* Operation in this page | |
1079 | * | |
1080 | * page_base = page offset within aperture | |
1081 | * page_offset = offset within page | |
1082 | * page_length = bytes to copy for this page | |
1083 | */ | |
1084 | u32 page_base = node.start; | |
1085 | unsigned page_offset = offset_in_page(offset); | |
1086 | unsigned page_length = PAGE_SIZE - page_offset; | |
1087 | page_length = remain < page_length ? remain : page_length; | |
1088 | if (node.allocated) { | |
1089 | wmb(); | |
1090 | ggtt->base.insert_page(&ggtt->base, | |
1091 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), | |
bb6dc8d9 | 1092 | node.start, I915_CACHE_NONE, 0); |
b50a5371 AS |
1093 | wmb(); |
1094 | } else { | |
1095 | page_base += offset & PAGE_MASK; | |
1096 | } | |
bb6dc8d9 | 1097 | |
b06f4c80 | 1098 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
bb6dc8d9 | 1099 | user_data, page_length)) { |
b50a5371 AS |
1100 | ret = -EFAULT; |
1101 | break; | |
1102 | } | |
1103 | ||
1104 | remain -= page_length; | |
1105 | user_data += page_length; | |
1106 | offset += page_length; | |
1107 | } | |
1108 | ||
bb6dc8d9 | 1109 | mutex_lock(&i915->drm.struct_mutex); |
b50a5371 AS |
1110 | out_unpin: |
1111 | if (node.allocated) { | |
1112 | wmb(); | |
1113 | ggtt->base.clear_range(&ggtt->base, | |
4fb84d99 | 1114 | node.start, node.size); |
b50a5371 AS |
1115 | remove_mappable_node(&node); |
1116 | } else { | |
058d88c4 | 1117 | i915_vma_unpin(vma); |
b50a5371 | 1118 | } |
bb6dc8d9 CW |
1119 | out_unlock: |
1120 | intel_runtime_pm_put(i915); | |
1121 | mutex_unlock(&i915->drm.struct_mutex); | |
f60d7f0c | 1122 | |
eb01459f EA |
1123 | return ret; |
1124 | } | |
1125 | ||
673a394b EA |
1126 | /** |
1127 | * Reads data from the object referenced by handle. | |
14bb2c11 TU |
1128 | * @dev: drm device pointer |
1129 | * @data: ioctl data blob | |
1130 | * @file: drm file pointer | |
673a394b EA |
1131 | * |
1132 | * On error, the contents of *data are undefined. | |
1133 | */ | |
1134 | int | |
1135 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1136 | struct drm_file *file) |
673a394b EA |
1137 | { |
1138 | struct drm_i915_gem_pread *args = data; | |
05394f39 | 1139 | struct drm_i915_gem_object *obj; |
bb6dc8d9 | 1140 | int ret; |
673a394b | 1141 | |
51311d0a CW |
1142 | if (args->size == 0) |
1143 | return 0; | |
1144 | ||
1145 | if (!access_ok(VERIFY_WRITE, | |
3ed605bc | 1146 | u64_to_user_ptr(args->data_ptr), |
51311d0a CW |
1147 | args->size)) |
1148 | return -EFAULT; | |
1149 | ||
03ac0642 | 1150 | obj = i915_gem_object_lookup(file, args->handle); |
258a5ede CW |
1151 | if (!obj) |
1152 | return -ENOENT; | |
673a394b | 1153 | |
7dcd2499 | 1154 | /* Bounds check source. */ |
966d5bf5 | 1155 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
ce9d419d | 1156 | ret = -EINVAL; |
bb6dc8d9 | 1157 | goto out; |
ce9d419d CW |
1158 | } |
1159 | ||
db53a302 CW |
1160 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
1161 | ||
e95433c7 CW |
1162 | ret = i915_gem_object_wait(obj, |
1163 | I915_WAIT_INTERRUPTIBLE, | |
1164 | MAX_SCHEDULE_TIMEOUT, | |
1165 | to_rps_client(file)); | |
258a5ede | 1166 | if (ret) |
bb6dc8d9 | 1167 | goto out; |
258a5ede | 1168 | |
bb6dc8d9 | 1169 | ret = i915_gem_object_pin_pages(obj); |
258a5ede | 1170 | if (ret) |
bb6dc8d9 | 1171 | goto out; |
673a394b | 1172 | |
bb6dc8d9 | 1173 | ret = i915_gem_shmem_pread(obj, args); |
9c870d03 | 1174 | if (ret == -EFAULT || ret == -ENODEV) |
bb6dc8d9 | 1175 | ret = i915_gem_gtt_pread(obj, args); |
b50a5371 | 1176 | |
bb6dc8d9 CW |
1177 | i915_gem_object_unpin_pages(obj); |
1178 | out: | |
f0cd5182 | 1179 | i915_gem_object_put(obj); |
eb01459f | 1180 | return ret; |
673a394b EA |
1181 | } |
1182 | ||
0839ccb8 KP |
1183 | /* This is the fast write path which cannot handle |
1184 | * page faults in the source data | |
9b7530cc | 1185 | */ |
0839ccb8 | 1186 | |
fe115628 CW |
1187 | static inline bool |
1188 | ggtt_write(struct io_mapping *mapping, | |
1189 | loff_t base, int offset, | |
1190 | char __user *user_data, int length) | |
9b7530cc | 1191 | { |
afe722be | 1192 | void __iomem *vaddr; |
0839ccb8 | 1193 | unsigned long unwritten; |
9b7530cc | 1194 | |
4f0c7cfb | 1195 | /* We can use the cpu mem copy function because this is X86. */ |
afe722be VS |
1196 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
1197 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, | |
0839ccb8 | 1198 | user_data, length); |
fe115628 CW |
1199 | io_mapping_unmap_atomic(vaddr); |
1200 | if (unwritten) { | |
afe722be VS |
1201 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
1202 | unwritten = copy_from_user((void __force *)vaddr + offset, | |
1203 | user_data, length); | |
fe115628 CW |
1204 | io_mapping_unmap(vaddr); |
1205 | } | |
bb6dc8d9 | 1206 | |
bb6dc8d9 CW |
1207 | return unwritten; |
1208 | } | |
1209 | ||
3de09aa3 EA |
1210 | /** |
1211 | * This is the fast pwrite path, where we copy the data directly from the | |
1212 | * user into the GTT, uncached. | |
fe115628 | 1213 | * @obj: i915 GEM object |
14bb2c11 | 1214 | * @args: pwrite arguments structure |
3de09aa3 | 1215 | */ |
673a394b | 1216 | static int |
fe115628 CW |
1217 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
1218 | const struct drm_i915_gem_pwrite *args) | |
673a394b | 1219 | { |
fe115628 | 1220 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
4f1959ee AS |
1221 | struct i915_ggtt *ggtt = &i915->ggtt; |
1222 | struct drm_mm_node node; | |
fe115628 CW |
1223 | struct i915_vma *vma; |
1224 | u64 remain, offset; | |
1225 | void __user *user_data; | |
4f1959ee | 1226 | int ret; |
b50a5371 | 1227 | |
fe115628 CW |
1228 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
1229 | if (ret) | |
1230 | return ret; | |
935aaa69 | 1231 | |
8bd81815 CW |
1232 | if (i915_gem_object_has_struct_page(obj)) { |
1233 | /* | |
1234 | * Avoid waking the device up if we can fallback, as | |
1235 | * waking/resuming is very slow (worst-case 10-100 ms | |
1236 | * depending on PCI sleeps and our own resume time). | |
1237 | * This easily dwarfs any performance advantage from | |
1238 | * using the cache bypass of indirect GGTT access. | |
1239 | */ | |
1240 | if (!intel_runtime_pm_get_if_in_use(i915)) { | |
1241 | ret = -EFAULT; | |
1242 | goto out_unlock; | |
1243 | } | |
1244 | } else { | |
1245 | /* No backing pages, no fallback, we must force GGTT access */ | |
1246 | intel_runtime_pm_get(i915); | |
1247 | } | |
1248 | ||
058d88c4 | 1249 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
a3259ca9 CW |
1250 | PIN_MAPPABLE | |
1251 | PIN_NONFAULT | | |
1252 | PIN_NONBLOCK); | |
18034584 CW |
1253 | if (!IS_ERR(vma)) { |
1254 | node.start = i915_ggtt_offset(vma); | |
1255 | node.allocated = false; | |
49ef5294 | 1256 | ret = i915_vma_put_fence(vma); |
18034584 CW |
1257 | if (ret) { |
1258 | i915_vma_unpin(vma); | |
1259 | vma = ERR_PTR(ret); | |
1260 | } | |
1261 | } | |
058d88c4 | 1262 | if (IS_ERR(vma)) { |
bb6dc8d9 | 1263 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
4f1959ee | 1264 | if (ret) |
8bd81815 | 1265 | goto out_rpm; |
fe115628 | 1266 | GEM_BUG_ON(!node.allocated); |
4f1959ee | 1267 | } |
935aaa69 DV |
1268 | |
1269 | ret = i915_gem_object_set_to_gtt_domain(obj, true); | |
1270 | if (ret) | |
1271 | goto out_unpin; | |
1272 | ||
fe115628 CW |
1273 | mutex_unlock(&i915->drm.struct_mutex); |
1274 | ||
b19482d7 | 1275 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
063e4e6b | 1276 | |
4f1959ee AS |
1277 | user_data = u64_to_user_ptr(args->data_ptr); |
1278 | offset = args->offset; | |
1279 | remain = args->size; | |
1280 | while (remain) { | |
673a394b EA |
1281 | /* Operation in this page |
1282 | * | |
0839ccb8 KP |
1283 | * page_base = page offset within aperture |
1284 | * page_offset = offset within page | |
1285 | * page_length = bytes to copy for this page | |
673a394b | 1286 | */ |
4f1959ee | 1287 | u32 page_base = node.start; |
bb6dc8d9 CW |
1288 | unsigned int page_offset = offset_in_page(offset); |
1289 | unsigned int page_length = PAGE_SIZE - page_offset; | |
4f1959ee AS |
1290 | page_length = remain < page_length ? remain : page_length; |
1291 | if (node.allocated) { | |
1292 | wmb(); /* flush the write before we modify the GGTT */ | |
1293 | ggtt->base.insert_page(&ggtt->base, | |
1294 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), | |
1295 | node.start, I915_CACHE_NONE, 0); | |
1296 | wmb(); /* flush modifications to the GGTT (insert_page) */ | |
1297 | } else { | |
1298 | page_base += offset & PAGE_MASK; | |
1299 | } | |
0839ccb8 | 1300 | /* If we get a fault while copying data, then (presumably) our |
3de09aa3 EA |
1301 | * source page isn't available. Return the error and we'll |
1302 | * retry in the slow path. | |
b50a5371 AS |
1303 | * If the object is non-shmem backed, we retry again with the |
1304 | * path that handles page fault. | |
0839ccb8 | 1305 | */ |
b06f4c80 | 1306 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
fe115628 CW |
1307 | user_data, page_length)) { |
1308 | ret = -EFAULT; | |
1309 | break; | |
935aaa69 | 1310 | } |
673a394b | 1311 | |
0839ccb8 KP |
1312 | remain -= page_length; |
1313 | user_data += page_length; | |
1314 | offset += page_length; | |
673a394b | 1315 | } |
d59b21ec | 1316 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
fe115628 CW |
1317 | |
1318 | mutex_lock(&i915->drm.struct_mutex); | |
935aaa69 | 1319 | out_unpin: |
4f1959ee AS |
1320 | if (node.allocated) { |
1321 | wmb(); | |
1322 | ggtt->base.clear_range(&ggtt->base, | |
4fb84d99 | 1323 | node.start, node.size); |
4f1959ee AS |
1324 | remove_mappable_node(&node); |
1325 | } else { | |
058d88c4 | 1326 | i915_vma_unpin(vma); |
4f1959ee | 1327 | } |
8bd81815 | 1328 | out_rpm: |
9c870d03 | 1329 | intel_runtime_pm_put(i915); |
8bd81815 | 1330 | out_unlock: |
fe115628 | 1331 | mutex_unlock(&i915->drm.struct_mutex); |
3de09aa3 | 1332 | return ret; |
673a394b EA |
1333 | } |
1334 | ||
3043c60c | 1335 | static int |
fe115628 | 1336 | shmem_pwrite_slow(struct page *page, int offset, int length, |
d174bd64 DV |
1337 | char __user *user_data, |
1338 | bool page_do_bit17_swizzling, | |
1339 | bool needs_clflush_before, | |
1340 | bool needs_clflush_after) | |
673a394b | 1341 | { |
d174bd64 DV |
1342 | char *vaddr; |
1343 | int ret; | |
e5281ccd | 1344 | |
d174bd64 | 1345 | vaddr = kmap(page); |
e7e58eb5 | 1346 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
fe115628 | 1347 | shmem_clflush_swizzled_range(vaddr + offset, length, |
23c18c71 | 1348 | page_do_bit17_swizzling); |
d174bd64 | 1349 | if (page_do_bit17_swizzling) |
fe115628 CW |
1350 | ret = __copy_from_user_swizzled(vaddr, offset, user_data, |
1351 | length); | |
d174bd64 | 1352 | else |
fe115628 | 1353 | ret = __copy_from_user(vaddr + offset, user_data, length); |
d174bd64 | 1354 | if (needs_clflush_after) |
fe115628 | 1355 | shmem_clflush_swizzled_range(vaddr + offset, length, |
23c18c71 | 1356 | page_do_bit17_swizzling); |
d174bd64 | 1357 | kunmap(page); |
40123c1f | 1358 | |
755d2218 | 1359 | return ret ? -EFAULT : 0; |
40123c1f EA |
1360 | } |
1361 | ||
fe115628 CW |
1362 | /* Per-page copy function for the shmem pwrite fastpath. |
1363 | * Flushes invalid cachelines before writing to the target if | |
1364 | * needs_clflush_before is set and flushes out any written cachelines after | |
1365 | * writing if needs_clflush is set. | |
1366 | */ | |
40123c1f | 1367 | static int |
fe115628 CW |
1368 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
1369 | bool page_do_bit17_swizzling, | |
1370 | bool needs_clflush_before, | |
1371 | bool needs_clflush_after) | |
40123c1f | 1372 | { |
fe115628 CW |
1373 | int ret; |
1374 | ||
1375 | ret = -ENODEV; | |
1376 | if (!page_do_bit17_swizzling) { | |
1377 | char *vaddr = kmap_atomic(page); | |
1378 | ||
1379 | if (needs_clflush_before) | |
1380 | drm_clflush_virt_range(vaddr + offset, len); | |
1381 | ret = __copy_from_user_inatomic(vaddr + offset, user_data, len); | |
1382 | if (needs_clflush_after) | |
1383 | drm_clflush_virt_range(vaddr + offset, len); | |
1384 | ||
1385 | kunmap_atomic(vaddr); | |
1386 | } | |
1387 | if (ret == 0) | |
1388 | return ret; | |
1389 | ||
1390 | return shmem_pwrite_slow(page, offset, len, user_data, | |
1391 | page_do_bit17_swizzling, | |
1392 | needs_clflush_before, | |
1393 | needs_clflush_after); | |
1394 | } | |
1395 | ||
1396 | static int | |
1397 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, | |
1398 | const struct drm_i915_gem_pwrite *args) | |
1399 | { | |
1400 | struct drm_i915_private *i915 = to_i915(obj->base.dev); | |
1401 | void __user *user_data; | |
1402 | u64 remain; | |
1403 | unsigned int obj_do_bit17_swizzling; | |
1404 | unsigned int partial_cacheline_write; | |
43394c7d | 1405 | unsigned int needs_clflush; |
fe115628 CW |
1406 | unsigned int offset, idx; |
1407 | int ret; | |
40123c1f | 1408 | |
fe115628 | 1409 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
755d2218 CW |
1410 | if (ret) |
1411 | return ret; | |
1412 | ||
fe115628 CW |
1413 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
1414 | mutex_unlock(&i915->drm.struct_mutex); | |
1415 | if (ret) | |
1416 | return ret; | |
673a394b | 1417 | |
fe115628 CW |
1418 | obj_do_bit17_swizzling = 0; |
1419 | if (i915_gem_object_needs_bit17_swizzle(obj)) | |
1420 | obj_do_bit17_swizzling = BIT(17); | |
e5281ccd | 1421 | |
fe115628 CW |
1422 | /* If we don't overwrite a cacheline completely we need to be |
1423 | * careful to have up-to-date data by first clflushing. Don't | |
1424 | * overcomplicate things and flush the entire patch. | |
1425 | */ | |
1426 | partial_cacheline_write = 0; | |
1427 | if (needs_clflush & CLFLUSH_BEFORE) | |
1428 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; | |
9da3da66 | 1429 | |
fe115628 CW |
1430 | user_data = u64_to_user_ptr(args->data_ptr); |
1431 | remain = args->size; | |
1432 | offset = offset_in_page(args->offset); | |
1433 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { | |
1434 | struct page *page = i915_gem_object_get_page(obj, idx); | |
3500d559 | 1435 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
755d2218 | 1436 | |
fe115628 CW |
1437 | ret = shmem_pwrite(page, offset, length, user_data, |
1438 | page_to_phys(page) & obj_do_bit17_swizzling, | |
1439 | (offset | length) & partial_cacheline_write, | |
1440 | needs_clflush & CLFLUSH_AFTER); | |
755d2218 | 1441 | if (ret) |
fe115628 | 1442 | break; |
755d2218 | 1443 | |
fe115628 CW |
1444 | remain -= length; |
1445 | user_data += length; | |
1446 | offset = 0; | |
8c59967c | 1447 | } |
673a394b | 1448 | |
d59b21ec | 1449 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
fe115628 | 1450 | i915_gem_obj_finish_shmem_access(obj); |
40123c1f | 1451 | return ret; |
673a394b EA |
1452 | } |
1453 | ||
1454 | /** | |
1455 | * Writes data to the object referenced by handle. | |
14bb2c11 TU |
1456 | * @dev: drm device |
1457 | * @data: ioctl data blob | |
1458 | * @file: drm file | |
673a394b EA |
1459 | * |
1460 | * On error, the contents of the buffer that were to be modified are undefined. | |
1461 | */ | |
1462 | int | |
1463 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, | |
fbd5a26d | 1464 | struct drm_file *file) |
673a394b EA |
1465 | { |
1466 | struct drm_i915_gem_pwrite *args = data; | |
05394f39 | 1467 | struct drm_i915_gem_object *obj; |
51311d0a CW |
1468 | int ret; |
1469 | ||
1470 | if (args->size == 0) | |
1471 | return 0; | |
1472 | ||
1473 | if (!access_ok(VERIFY_READ, | |
3ed605bc | 1474 | u64_to_user_ptr(args->data_ptr), |
51311d0a CW |
1475 | args->size)) |
1476 | return -EFAULT; | |
1477 | ||
03ac0642 | 1478 | obj = i915_gem_object_lookup(file, args->handle); |
258a5ede CW |
1479 | if (!obj) |
1480 | return -ENOENT; | |
673a394b | 1481 | |
7dcd2499 | 1482 | /* Bounds check destination. */ |
966d5bf5 | 1483 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
ce9d419d | 1484 | ret = -EINVAL; |
258a5ede | 1485 | goto err; |
ce9d419d CW |
1486 | } |
1487 | ||
db53a302 CW |
1488 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
1489 | ||
7c55e2c5 CW |
1490 | ret = -ENODEV; |
1491 | if (obj->ops->pwrite) | |
1492 | ret = obj->ops->pwrite(obj, args); | |
1493 | if (ret != -ENODEV) | |
1494 | goto err; | |
1495 | ||
e95433c7 CW |
1496 | ret = i915_gem_object_wait(obj, |
1497 | I915_WAIT_INTERRUPTIBLE | | |
1498 | I915_WAIT_ALL, | |
1499 | MAX_SCHEDULE_TIMEOUT, | |
1500 | to_rps_client(file)); | |
258a5ede CW |
1501 | if (ret) |
1502 | goto err; | |
1503 | ||
fe115628 | 1504 | ret = i915_gem_object_pin_pages(obj); |
258a5ede | 1505 | if (ret) |
fe115628 | 1506 | goto err; |
258a5ede | 1507 | |
935aaa69 | 1508 | ret = -EFAULT; |
673a394b EA |
1509 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
1510 | * it would end up going through the fenced access, and we'll get | |
1511 | * different detiling behavior between reading and writing. | |
1512 | * pread/pwrite currently are reading and writing from the CPU | |
1513 | * perspective, requiring manual detiling by the client. | |
1514 | */ | |
6eae0059 | 1515 | if (!i915_gem_object_has_struct_page(obj) || |
9c870d03 | 1516 | cpu_write_needs_clflush(obj)) |
935aaa69 DV |
1517 | /* Note that the gtt paths might fail with non-page-backed user |
1518 | * pointers (e.g. gtt mappings when moving data between | |
9c870d03 CW |
1519 | * textures). Fallback to the shmem path in that case. |
1520 | */ | |
fe115628 | 1521 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
673a394b | 1522 | |
d1054ee4 | 1523 | if (ret == -EFAULT || ret == -ENOSPC) { |
6a2c4232 CW |
1524 | if (obj->phys_handle) |
1525 | ret = i915_gem_phys_pwrite(obj, args, file); | |
b50a5371 | 1526 | else |
fe115628 | 1527 | ret = i915_gem_shmem_pwrite(obj, args); |
6a2c4232 | 1528 | } |
5c0480f2 | 1529 | |
fe115628 | 1530 | i915_gem_object_unpin_pages(obj); |
258a5ede | 1531 | err: |
f0cd5182 | 1532 | i915_gem_object_put(obj); |
258a5ede | 1533 | return ret; |
673a394b EA |
1534 | } |
1535 | ||
40e62d5d CW |
1536 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
1537 | { | |
1538 | struct drm_i915_private *i915; | |
1539 | struct list_head *list; | |
1540 | struct i915_vma *vma; | |
1541 | ||
f2123818 CW |
1542 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
1543 | ||
40e62d5d CW |
1544 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
1545 | if (!i915_vma_is_ggtt(vma)) | |
28f412e0 | 1546 | break; |
40e62d5d CW |
1547 | |
1548 | if (i915_vma_is_active(vma)) | |
1549 | continue; | |
1550 | ||
1551 | if (!drm_mm_node_allocated(&vma->node)) | |
1552 | continue; | |
1553 | ||
1554 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); | |
1555 | } | |
1556 | ||
1557 | i915 = to_i915(obj->base.dev); | |
f2123818 | 1558 | spin_lock(&i915->mm.obj_lock); |
40e62d5d | 1559 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
f2123818 CW |
1560 | list_move_tail(&obj->mm.link, list); |
1561 | spin_unlock(&i915->mm.obj_lock); | |
40e62d5d CW |
1562 | } |
1563 | ||
673a394b | 1564 | /** |
2ef7eeaa EA |
1565 | * Called when user space prepares to use an object with the CPU, either |
1566 | * through the mmap ioctl's mapping or a GTT mapping. | |
14bb2c11 TU |
1567 | * @dev: drm device |
1568 | * @data: ioctl data blob | |
1569 | * @file: drm file | |
673a394b EA |
1570 | */ |
1571 | int | |
1572 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1573 | struct drm_file *file) |
673a394b EA |
1574 | { |
1575 | struct drm_i915_gem_set_domain *args = data; | |
05394f39 | 1576 | struct drm_i915_gem_object *obj; |
2ef7eeaa EA |
1577 | uint32_t read_domains = args->read_domains; |
1578 | uint32_t write_domain = args->write_domain; | |
40e62d5d | 1579 | int err; |
673a394b | 1580 | |
2ef7eeaa | 1581 | /* Only handle setting domains to types used by the CPU. */ |
b8f9096d | 1582 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
2ef7eeaa EA |
1583 | return -EINVAL; |
1584 | ||
1585 | /* Having something in the write domain implies it's in the read | |
1586 | * domain, and only that read domain. Enforce that in the request. | |
1587 | */ | |
1588 | if (write_domain != 0 && read_domains != write_domain) | |
1589 | return -EINVAL; | |
1590 | ||
03ac0642 | 1591 | obj = i915_gem_object_lookup(file, args->handle); |
b8f9096d CW |
1592 | if (!obj) |
1593 | return -ENOENT; | |
673a394b | 1594 | |
3236f57a CW |
1595 | /* Try to flush the object off the GPU without holding the lock. |
1596 | * We will repeat the flush holding the lock in the normal manner | |
1597 | * to catch cases where we are gazumped. | |
1598 | */ | |
40e62d5d | 1599 | err = i915_gem_object_wait(obj, |
e95433c7 CW |
1600 | I915_WAIT_INTERRUPTIBLE | |
1601 | (write_domain ? I915_WAIT_ALL : 0), | |
1602 | MAX_SCHEDULE_TIMEOUT, | |
1603 | to_rps_client(file)); | |
40e62d5d | 1604 | if (err) |
f0cd5182 | 1605 | goto out; |
b8f9096d | 1606 | |
40e62d5d CW |
1607 | /* Flush and acquire obj->pages so that we are coherent through |
1608 | * direct access in memory with previous cached writes through | |
1609 | * shmemfs and that our cache domain tracking remains valid. | |
1610 | * For example, if the obj->filp was moved to swap without us | |
1611 | * being notified and releasing the pages, we would mistakenly | |
1612 | * continue to assume that the obj remained out of the CPU cached | |
1613 | * domain. | |
1614 | */ | |
1615 | err = i915_gem_object_pin_pages(obj); | |
1616 | if (err) | |
f0cd5182 | 1617 | goto out; |
40e62d5d CW |
1618 | |
1619 | err = i915_mutex_lock_interruptible(dev); | |
1620 | if (err) | |
f0cd5182 | 1621 | goto out_unpin; |
3236f57a | 1622 | |
e22d8e3c CW |
1623 | if (read_domains & I915_GEM_DOMAIN_WC) |
1624 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); | |
1625 | else if (read_domains & I915_GEM_DOMAIN_GTT) | |
1626 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); | |
43566ded | 1627 | else |
e22d8e3c | 1628 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
2ef7eeaa | 1629 | |
40e62d5d CW |
1630 | /* And bump the LRU for this access */ |
1631 | i915_gem_object_bump_inactive_ggtt(obj); | |
031b698a | 1632 | |
673a394b | 1633 | mutex_unlock(&dev->struct_mutex); |
b8f9096d | 1634 | |
40e62d5d | 1635 | if (write_domain != 0) |
ef74921b CW |
1636 | intel_fb_obj_invalidate(obj, |
1637 | fb_write_origin(obj, write_domain)); | |
40e62d5d | 1638 | |
f0cd5182 | 1639 | out_unpin: |
40e62d5d | 1640 | i915_gem_object_unpin_pages(obj); |
f0cd5182 CW |
1641 | out: |
1642 | i915_gem_object_put(obj); | |
40e62d5d | 1643 | return err; |
673a394b EA |
1644 | } |
1645 | ||
1646 | /** | |
1647 | * Called when user space has done writes to this buffer | |
14bb2c11 TU |
1648 | * @dev: drm device |
1649 | * @data: ioctl data blob | |
1650 | * @file: drm file | |
673a394b EA |
1651 | */ |
1652 | int | |
1653 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1654 | struct drm_file *file) |
673a394b EA |
1655 | { |
1656 | struct drm_i915_gem_sw_finish *args = data; | |
05394f39 | 1657 | struct drm_i915_gem_object *obj; |
1d7cfea1 | 1658 | |
03ac0642 | 1659 | obj = i915_gem_object_lookup(file, args->handle); |
c21724cc CW |
1660 | if (!obj) |
1661 | return -ENOENT; | |
673a394b | 1662 | |
673a394b | 1663 | /* Pinned buffers may be scanout, so flush the cache */ |
5a97bcc6 | 1664 | i915_gem_object_flush_if_display(obj); |
f0cd5182 | 1665 | i915_gem_object_put(obj); |
5a97bcc6 CW |
1666 | |
1667 | return 0; | |
673a394b EA |
1668 | } |
1669 | ||
a3e6af9a JL |
1670 | static inline bool |
1671 | __vma_matches(struct vm_area_struct *vma, struct file *filp, | |
1672 | unsigned long addr, unsigned long size) | |
1673 | { | |
1674 | if (vma->vm_file != filp) | |
1675 | return false; | |
1676 | ||
8f602c44 TU |
1677 | return vma->vm_start == addr && |
1678 | (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size); | |
a3e6af9a JL |
1679 | } |
1680 | ||
673a394b | 1681 | /** |
14bb2c11 TU |
1682 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
1683 | * it is mapped to. | |
1684 | * @dev: drm device | |
1685 | * @data: ioctl data blob | |
1686 | * @file: drm file | |
673a394b EA |
1687 | * |
1688 | * While the mapping holds a reference on the contents of the object, it doesn't | |
1689 | * imply a ref on the object itself. | |
34367381 DV |
1690 | * |
1691 | * IMPORTANT: | |
1692 | * | |
1693 | * DRM driver writers who look a this function as an example for how to do GEM | |
1694 | * mmap support, please don't implement mmap support like here. The modern way | |
1695 | * to implement DRM mmap support is with an mmap offset ioctl (like | |
1696 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. | |
1697 | * That way debug tooling like valgrind will understand what's going on, hiding | |
1698 | * the mmap call in a driver private ioctl will break that. The i915 driver only | |
1699 | * does cpu mmaps this way because we didn't know better. | |
673a394b EA |
1700 | */ |
1701 | int | |
1702 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1703 | struct drm_file *file) |
673a394b EA |
1704 | { |
1705 | struct drm_i915_gem_mmap *args = data; | |
03ac0642 | 1706 | struct drm_i915_gem_object *obj; |
673a394b EA |
1707 | unsigned long addr; |
1708 | ||
1816f923 AG |
1709 | if (args->flags & ~(I915_MMAP_WC)) |
1710 | return -EINVAL; | |
1711 | ||
568a58e5 | 1712 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
1816f923 AG |
1713 | return -ENODEV; |
1714 | ||
03ac0642 CW |
1715 | obj = i915_gem_object_lookup(file, args->handle); |
1716 | if (!obj) | |
bf79cb91 | 1717 | return -ENOENT; |
673a394b | 1718 | |
1286ff73 DV |
1719 | /* prime objects have no backing filp to GEM mmap |
1720 | * pages from. | |
1721 | */ | |
03ac0642 | 1722 | if (!obj->base.filp) { |
26f7213f CW |
1723 | addr = -ENXIO; |
1724 | goto err; | |
1725 | } | |
1726 | ||
1727 | if (range_overflows(args->offset, args->size, (u64)obj->base.size)) { | |
1728 | addr = -EINVAL; | |
1729 | goto err; | |
1286ff73 DV |
1730 | } |
1731 | ||
03ac0642 | 1732 | addr = vm_mmap(obj->base.filp, 0, args->size, |
673a394b EA |
1733 | PROT_READ | PROT_WRITE, MAP_SHARED, |
1734 | args->offset); | |
2b54ad24 JL |
1735 | if (IS_ERR_VALUE(addr)) |
1736 | goto err; | |
1737 | ||
1816f923 AG |
1738 | if (args->flags & I915_MMAP_WC) { |
1739 | struct mm_struct *mm = current->mm; | |
1740 | struct vm_area_struct *vma; | |
1741 | ||
80a89a5e | 1742 | if (down_write_killable(&mm->mmap_sem)) { |
26f7213f CW |
1743 | addr = -EINTR; |
1744 | goto err; | |
80a89a5e | 1745 | } |
1816f923 | 1746 | vma = find_vma(mm, addr); |
a3e6af9a | 1747 | if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) |
1816f923 AG |
1748 | vma->vm_page_prot = |
1749 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); | |
1750 | else | |
1751 | addr = -ENOMEM; | |
1752 | up_write(&mm->mmap_sem); | |
2b54ad24 JL |
1753 | if (IS_ERR_VALUE(addr)) |
1754 | goto err; | |
aeecc969 CW |
1755 | |
1756 | /* This may race, but that's ok, it only gets set */ | |
50349247 | 1757 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
1816f923 | 1758 | } |
f0cd5182 | 1759 | i915_gem_object_put(obj); |
673a394b EA |
1760 | |
1761 | args->addr_ptr = (uint64_t) addr; | |
673a394b | 1762 | return 0; |
2b54ad24 JL |
1763 | |
1764 | err: | |
1765 | i915_gem_object_put(obj); | |
2b54ad24 | 1766 | return addr; |
673a394b EA |
1767 | } |
1768 | ||
03af84fe CW |
1769 | static unsigned int tile_row_pages(struct drm_i915_gem_object *obj) |
1770 | { | |
6649a0b6 | 1771 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
03af84fe CW |
1772 | } |
1773 | ||
4cc69075 CW |
1774 | /** |
1775 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps | |
1776 | * | |
1777 | * A history of the GTT mmap interface: | |
1778 | * | |
1779 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to | |
1780 | * aligned and suitable for fencing, and still fit into the available | |
1781 | * mappable space left by the pinned display objects. A classic problem | |
1782 | * we called the page-fault-of-doom where we would ping-pong between | |
1783 | * two objects that could not fit inside the GTT and so the memcpy | |
1784 | * would page one object in at the expense of the other between every | |
1785 | * single byte. | |
1786 | * | |
1787 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none | |
1788 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the | |
1789 | * object is too large for the available space (or simply too large | |
1790 | * for the mappable aperture!), a view is created instead and faulted | |
1791 | * into userspace. (This view is aligned and sized appropriately for | |
1792 | * fenced access.) | |
1793 | * | |
e22d8e3c CW |
1794 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
1795 | * delayed writes via GTT before performing direct access via WC. | |
1796 | * | |
4cc69075 CW |
1797 | * Restrictions: |
1798 | * | |
1799 | * * snoopable objects cannot be accessed via the GTT. It can cause machine | |
1800 | * hangs on some architectures, corruption on others. An attempt to service | |
1801 | * a GTT page fault from a snoopable object will generate a SIGBUS. | |
1802 | * | |
1803 | * * the object must be able to fit into RAM (physical memory, though no | |
1804 | * limited to the mappable aperture). | |
1805 | * | |
1806 | * | |
1807 | * Caveats: | |
1808 | * | |
1809 | * * a new GTT page fault will synchronize rendering from the GPU and flush | |
1810 | * all data to system memory. Subsequent access will not be synchronized. | |
1811 | * | |
1812 | * * all mappings are revoked on runtime device suspend. | |
1813 | * | |
1814 | * * there are only 8, 16 or 32 fence registers to share between all users | |
1815 | * (older machines require fence register for display and blitter access | |
1816 | * as well). Contention of the fence registers will cause the previous users | |
1817 | * to be unmapped and any new access will generate new page faults. | |
1818 | * | |
1819 | * * running out of memory while servicing a fault may generate a SIGBUS, | |
1820 | * rather than the expected SIGSEGV. | |
1821 | */ | |
1822 | int i915_gem_mmap_gtt_version(void) | |
1823 | { | |
e22d8e3c | 1824 | return 2; |
4cc69075 CW |
1825 | } |
1826 | ||
2d4281bb CW |
1827 | static inline struct i915_ggtt_view |
1828 | compute_partial_view(struct drm_i915_gem_object *obj, | |
2d4281bb CW |
1829 | pgoff_t page_offset, |
1830 | unsigned int chunk) | |
1831 | { | |
1832 | struct i915_ggtt_view view; | |
1833 | ||
1834 | if (i915_gem_object_is_tiled(obj)) | |
1835 | chunk = roundup(chunk, tile_row_pages(obj)); | |
1836 | ||
2d4281bb | 1837 | view.type = I915_GGTT_VIEW_PARTIAL; |
8bab1193 CW |
1838 | view.partial.offset = rounddown(page_offset, chunk); |
1839 | view.partial.size = | |
2d4281bb | 1840 | min_t(unsigned int, chunk, |
8bab1193 | 1841 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
2d4281bb CW |
1842 | |
1843 | /* If the partial covers the entire object, just create a normal VMA. */ | |
1844 | if (chunk >= obj->base.size >> PAGE_SHIFT) | |
1845 | view.type = I915_GGTT_VIEW_NORMAL; | |
1846 | ||
1847 | return view; | |
1848 | } | |
1849 | ||
de151cf6 JB |
1850 | /** |
1851 | * i915_gem_fault - fault a page into the GTT | |
d9072a3e | 1852 | * @vmf: fault info |
de151cf6 JB |
1853 | * |
1854 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped | |
1855 | * from userspace. The fault handler takes care of binding the object to | |
1856 | * the GTT (if needed), allocating and programming a fence register (again, | |
1857 | * only if needed based on whether the old reg is still valid or the object | |
1858 | * is tiled) and inserting a new PTE into the faulting process. | |
1859 | * | |
1860 | * Note that the faulting process may involve evicting existing objects | |
1861 | * from the GTT and/or fence registers to make room. So performance may | |
1862 | * suffer if the GTT working set is large or there are few fence registers | |
1863 | * left. | |
4cc69075 CW |
1864 | * |
1865 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps | |
1866 | * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version). | |
de151cf6 | 1867 | */ |
11bac800 | 1868 | int i915_gem_fault(struct vm_fault *vmf) |
de151cf6 | 1869 | { |
03af84fe | 1870 | #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */ |
11bac800 | 1871 | struct vm_area_struct *area = vmf->vma; |
058d88c4 | 1872 | struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data); |
05394f39 | 1873 | struct drm_device *dev = obj->base.dev; |
72e96d64 JL |
1874 | struct drm_i915_private *dev_priv = to_i915(dev); |
1875 | struct i915_ggtt *ggtt = &dev_priv->ggtt; | |
b8f9096d | 1876 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
058d88c4 | 1877 | struct i915_vma *vma; |
de151cf6 | 1878 | pgoff_t page_offset; |
82118877 | 1879 | unsigned int flags; |
b8f9096d | 1880 | int ret; |
f65c9168 | 1881 | |
c89d570f CW |
1882 | /* Sanity check that we allow writing into this object */ |
1883 | if (i915_gem_object_is_readonly(obj) && write) | |
1884 | return VM_FAULT_SIGBUS; | |
1885 | ||
de151cf6 | 1886 | /* We don't use vmf->pgoff since that has the fake offset */ |
1a29d85e | 1887 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
de151cf6 | 1888 | |
db53a302 CW |
1889 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
1890 | ||
6e4930f6 | 1891 | /* Try to flush the object off the GPU first without holding the lock. |
b8f9096d | 1892 | * Upon acquiring the lock, we will perform our sanity checks and then |
6e4930f6 CW |
1893 | * repeat the flush holding the lock in the normal manner to catch cases |
1894 | * where we are gazumped. | |
1895 | */ | |
e95433c7 CW |
1896 | ret = i915_gem_object_wait(obj, |
1897 | I915_WAIT_INTERRUPTIBLE, | |
1898 | MAX_SCHEDULE_TIMEOUT, | |
1899 | NULL); | |
6e4930f6 | 1900 | if (ret) |
b8f9096d CW |
1901 | goto err; |
1902 | ||
40e62d5d CW |
1903 | ret = i915_gem_object_pin_pages(obj); |
1904 | if (ret) | |
1905 | goto err; | |
1906 | ||
b8f9096d CW |
1907 | intel_runtime_pm_get(dev_priv); |
1908 | ||
1909 | ret = i915_mutex_lock_interruptible(dev); | |
1910 | if (ret) | |
1911 | goto err_rpm; | |
6e4930f6 | 1912 | |
eb119bd6 | 1913 | /* Access to snoopable pages through the GTT is incoherent. */ |
0031fb96 | 1914 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
ddeff6ee | 1915 | ret = -EFAULT; |
b8f9096d | 1916 | goto err_unlock; |
eb119bd6 CW |
1917 | } |
1918 | ||
82118877 CW |
1919 | /* If the object is smaller than a couple of partial vma, it is |
1920 | * not worth only creating a single partial vma - we may as well | |
1921 | * clear enough space for the full object. | |
1922 | */ | |
1923 | flags = PIN_MAPPABLE; | |
1924 | if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT) | |
1925 | flags |= PIN_NONBLOCK | PIN_NONFAULT; | |
1926 | ||
a61007a8 | 1927 | /* Now pin it into the GTT as needed */ |
82118877 | 1928 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags); |
a61007a8 | 1929 | if (IS_ERR(vma)) { |
a61007a8 | 1930 | /* Use a partial view if it is bigger than available space */ |
2d4281bb | 1931 | struct i915_ggtt_view view = |
8201c1fa | 1932 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
aa136d9d | 1933 | |
50349247 CW |
1934 | /* Userspace is now writing through an untracked VMA, abandon |
1935 | * all hope that the hardware is able to track future writes. | |
1936 | */ | |
1937 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; | |
1938 | ||
a61007a8 CW |
1939 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); |
1940 | } | |
058d88c4 CW |
1941 | if (IS_ERR(vma)) { |
1942 | ret = PTR_ERR(vma); | |
b8f9096d | 1943 | goto err_unlock; |
058d88c4 | 1944 | } |
4a684a41 | 1945 | |
c9839303 CW |
1946 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
1947 | if (ret) | |
b8f9096d | 1948 | goto err_unpin; |
74898d7e | 1949 | |
3bd40735 | 1950 | ret = i915_vma_pin_fence(vma); |
d9e86c0e | 1951 | if (ret) |
b8f9096d | 1952 | goto err_unpin; |
7d1c4804 | 1953 | |
b90b91d8 | 1954 | /* Finally, remap it using the new GTT offset */ |
c58305af | 1955 | ret = remap_io_mapping(area, |
8bab1193 | 1956 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
b06f4c80 | 1957 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
c58305af | 1958 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
b06f4c80 | 1959 | &ggtt->iomap); |
a65adaf8 CW |
1960 | if (ret) |
1961 | goto err_fence; | |
a61007a8 | 1962 | |
a65adaf8 CW |
1963 | /* Mark as being mmapped into userspace for later revocation */ |
1964 | assert_rpm_wakelock_held(dev_priv); | |
1965 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) | |
1966 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); | |
1967 | GEM_BUG_ON(!obj->userfault_count); | |
1968 | ||
1969 | err_fence: | |
3bd40735 | 1970 | i915_vma_unpin_fence(vma); |
b8f9096d | 1971 | err_unpin: |
058d88c4 | 1972 | __i915_vma_unpin(vma); |
b8f9096d | 1973 | err_unlock: |
de151cf6 | 1974 | mutex_unlock(&dev->struct_mutex); |
b8f9096d CW |
1975 | err_rpm: |
1976 | intel_runtime_pm_put(dev_priv); | |
40e62d5d | 1977 | i915_gem_object_unpin_pages(obj); |
b8f9096d | 1978 | err: |
de151cf6 | 1979 | switch (ret) { |
d9bc7e9f | 1980 | case -EIO: |
2232f031 DV |
1981 | /* |
1982 | * We eat errors when the gpu is terminally wedged to avoid | |
1983 | * userspace unduly crashing (gl has no provisions for mmaps to | |
1984 | * fail). But any other -EIO isn't ours (e.g. swap in failure) | |
1985 | * and so needs to be reported. | |
1986 | */ | |
1987 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { | |
f65c9168 PZ |
1988 | ret = VM_FAULT_SIGBUS; |
1989 | break; | |
1990 | } | |
045e769a | 1991 | case -EAGAIN: |
571c608d DV |
1992 | /* |
1993 | * EAGAIN means the gpu is hung and we'll wait for the error | |
1994 | * handler to reset everything when re-faulting in | |
1995 | * i915_mutex_lock_interruptible. | |
d9bc7e9f | 1996 | */ |
c715089f CW |
1997 | case 0: |
1998 | case -ERESTARTSYS: | |
bed636ab | 1999 | case -EINTR: |
e79e0fe3 DR |
2000 | case -EBUSY: |
2001 | /* | |
2002 | * EBUSY is ok: this just means that another thread | |
2003 | * already did the job. | |
2004 | */ | |
f65c9168 PZ |
2005 | ret = VM_FAULT_NOPAGE; |
2006 | break; | |
de151cf6 | 2007 | case -ENOMEM: |
f65c9168 PZ |
2008 | ret = VM_FAULT_OOM; |
2009 | break; | |
a7c2e1aa | 2010 | case -ENOSPC: |
45d67817 | 2011 | case -EFAULT: |
f65c9168 PZ |
2012 | ret = VM_FAULT_SIGBUS; |
2013 | break; | |
de151cf6 | 2014 | default: |
a7c2e1aa | 2015 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
f65c9168 PZ |
2016 | ret = VM_FAULT_SIGBUS; |
2017 | break; | |
de151cf6 | 2018 | } |
f65c9168 | 2019 | return ret; |
de151cf6 JB |
2020 | } |
2021 | ||
a65adaf8 CW |
2022 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
2023 | { | |
2024 | struct i915_vma *vma; | |
2025 | ||
2026 | GEM_BUG_ON(!obj->userfault_count); | |
2027 | ||
2028 | obj->userfault_count = 0; | |
2029 | list_del(&obj->userfault_link); | |
2030 | drm_vma_node_unmap(&obj->base.vma_node, | |
2031 | obj->base.dev->anon_inode->i_mapping); | |
2032 | ||
2033 | list_for_each_entry(vma, &obj->vma_list, obj_link) { | |
2034 | if (!i915_vma_is_ggtt(vma)) | |
2035 | break; | |
2036 | ||
2037 | i915_vma_unset_userfault(vma); | |
2038 | } | |
2039 | } | |
2040 | ||
901782b2 CW |
2041 | /** |
2042 | * i915_gem_release_mmap - remove physical page mappings | |
2043 | * @obj: obj in question | |
2044 | * | |
af901ca1 | 2045 | * Preserve the reservation of the mmapping with the DRM core code, but |
901782b2 CW |
2046 | * relinquish ownership of the pages back to the system. |
2047 | * | |
2048 | * It is vital that we remove the page mapping if we have mapped a tiled | |
2049 | * object through the GTT and then lose the fence register due to | |
2050 | * resource pressure. Similarly if the object has been moved out of the | |
2051 | * aperture, than pages mapped into userspace must be revoked. Removing the | |
2052 | * mapping will then trigger a page fault on the next user access, allowing | |
2053 | * fixup by i915_gem_fault(). | |
2054 | */ | |
d05ca301 | 2055 | void |
05394f39 | 2056 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
901782b2 | 2057 | { |
275f039d | 2058 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
275f039d | 2059 | |
349f2ccf CW |
2060 | /* Serialisation between user GTT access and our code depends upon |
2061 | * revoking the CPU's PTE whilst the mutex is held. The next user | |
2062 | * pagefault then has to wait until we release the mutex. | |
9c870d03 CW |
2063 | * |
2064 | * Note that RPM complicates somewhat by adding an additional | |
2065 | * requirement that operations to the GGTT be made holding the RPM | |
2066 | * wakeref. | |
349f2ccf | 2067 | */ |
275f039d | 2068 | lockdep_assert_held(&i915->drm.struct_mutex); |
9c870d03 | 2069 | intel_runtime_pm_get(i915); |
349f2ccf | 2070 | |
a65adaf8 | 2071 | if (!obj->userfault_count) |
9c870d03 | 2072 | goto out; |
901782b2 | 2073 | |
a65adaf8 | 2074 | __i915_gem_object_release_mmap(obj); |
349f2ccf CW |
2075 | |
2076 | /* Ensure that the CPU's PTE are revoked and there are not outstanding | |
2077 | * memory transactions from userspace before we return. The TLB | |
2078 | * flushing implied above by changing the PTE above *should* be | |
2079 | * sufficient, an extra barrier here just provides us with a bit | |
2080 | * of paranoid documentation about our requirement to serialise | |
2081 | * memory writes before touching registers / GSM. | |
2082 | */ | |
2083 | wmb(); | |
9c870d03 CW |
2084 | |
2085 | out: | |
2086 | intel_runtime_pm_put(i915); | |
901782b2 CW |
2087 | } |
2088 | ||
7c108fd8 | 2089 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
eedd10f4 | 2090 | { |
3594a3e2 | 2091 | struct drm_i915_gem_object *obj, *on; |
7c108fd8 | 2092 | int i; |
eedd10f4 | 2093 | |
3594a3e2 CW |
2094 | /* |
2095 | * Only called during RPM suspend. All users of the userfault_list | |
2096 | * must be holding an RPM wakeref to ensure that this can not | |
2097 | * run concurrently with themselves (and use the struct_mutex for | |
2098 | * protection between themselves). | |
2099 | */ | |
275f039d | 2100 | |
3594a3e2 | 2101 | list_for_each_entry_safe(obj, on, |
a65adaf8 CW |
2102 | &dev_priv->mm.userfault_list, userfault_link) |
2103 | __i915_gem_object_release_mmap(obj); | |
7c108fd8 CW |
2104 | |
2105 | /* The fence will be lost when the device powers down. If any were | |
2106 | * in use by hardware (i.e. they are pinned), we should not be powering | |
2107 | * down! All other fences will be reacquired by the user upon waking. | |
2108 | */ | |
2109 | for (i = 0; i < dev_priv->num_fence_regs; i++) { | |
2110 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; | |
2111 | ||
e0ec3ec6 CW |
2112 | /* Ideally we want to assert that the fence register is not |
2113 | * live at this point (i.e. that no piece of code will be | |
2114 | * trying to write through fence + GTT, as that both violates | |
2115 | * our tracking of activity and associated locking/barriers, | |
2116 | * but also is illegal given that the hw is powered down). | |
2117 | * | |
2118 | * Previously we used reg->pin_count as a "liveness" indicator. | |
2119 | * That is not sufficient, and we need a more fine-grained | |
2120 | * tool if we want to have a sanity check here. | |
2121 | */ | |
7c108fd8 CW |
2122 | |
2123 | if (!reg->vma) | |
2124 | continue; | |
2125 | ||
a65adaf8 | 2126 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
7c108fd8 CW |
2127 | reg->dirty = true; |
2128 | } | |
eedd10f4 CW |
2129 | } |
2130 | ||
d8cb5086 CW |
2131 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
2132 | { | |
fac5e23e | 2133 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
f3f6184c | 2134 | int err; |
da494d7c | 2135 | |
f3f6184c | 2136 | err = drm_gem_create_mmap_offset(&obj->base); |
b42a13d9 | 2137 | if (likely(!err)) |
f3f6184c | 2138 | return 0; |
d8cb5086 | 2139 | |
b42a13d9 CW |
2140 | /* Attempt to reap some mmap space from dead objects */ |
2141 | do { | |
2142 | err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE); | |
2143 | if (err) | |
2144 | break; | |
f3f6184c | 2145 | |
b42a13d9 | 2146 | i915_gem_drain_freed_objects(dev_priv); |
f3f6184c | 2147 | err = drm_gem_create_mmap_offset(&obj->base); |
b42a13d9 CW |
2148 | if (!err) |
2149 | break; | |
2150 | ||
2151 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); | |
da494d7c | 2152 | |
f3f6184c | 2153 | return err; |
d8cb5086 CW |
2154 | } |
2155 | ||
2156 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) | |
2157 | { | |
d8cb5086 CW |
2158 | drm_gem_free_mmap_offset(&obj->base); |
2159 | } | |
2160 | ||
da6b51d0 | 2161 | int |
ff72145b DA |
2162 | i915_gem_mmap_gtt(struct drm_file *file, |
2163 | struct drm_device *dev, | |
da6b51d0 | 2164 | uint32_t handle, |
ff72145b | 2165 | uint64_t *offset) |
de151cf6 | 2166 | { |
05394f39 | 2167 | struct drm_i915_gem_object *obj; |
de151cf6 JB |
2168 | int ret; |
2169 | ||
03ac0642 | 2170 | obj = i915_gem_object_lookup(file, handle); |
f3f6184c CW |
2171 | if (!obj) |
2172 | return -ENOENT; | |
ab18282d | 2173 | |
d8cb5086 | 2174 | ret = i915_gem_object_create_mmap_offset(obj); |
f3f6184c CW |
2175 | if (ret == 0) |
2176 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); | |
de151cf6 | 2177 | |
f0cd5182 | 2178 | i915_gem_object_put(obj); |
1d7cfea1 | 2179 | return ret; |
de151cf6 JB |
2180 | } |
2181 | ||
ff72145b DA |
2182 | /** |
2183 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing | |
2184 | * @dev: DRM device | |
2185 | * @data: GTT mapping ioctl data | |
2186 | * @file: GEM object info | |
2187 | * | |
2188 | * Simply returns the fake offset to userspace so it can mmap it. | |
2189 | * The mmap call will end up in drm_gem_mmap(), which will set things | |
2190 | * up so we can get faults in the handler above. | |
2191 | * | |
2192 | * The fault handler will take care of binding the object into the GTT | |
2193 | * (since it may have been evicted to make room for something), allocating | |
2194 | * a fence register, and mapping the appropriate aperture address into | |
2195 | * userspace. | |
2196 | */ | |
2197 | int | |
2198 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, | |
2199 | struct drm_file *file) | |
2200 | { | |
2201 | struct drm_i915_gem_mmap_gtt *args = data; | |
2202 | ||
da6b51d0 | 2203 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
ff72145b DA |
2204 | } |
2205 | ||
225067ee DV |
2206 | /* Immediately discard the backing storage */ |
2207 | static void | |
2208 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) | |
e5281ccd | 2209 | { |
4d6294bf | 2210 | i915_gem_object_free_mmap_offset(obj); |
1286ff73 | 2211 | |
4d6294bf CW |
2212 | if (obj->base.filp == NULL) |
2213 | return; | |
e5281ccd | 2214 | |
225067ee DV |
2215 | /* Our goal here is to return as much of the memory as |
2216 | * is possible back to the system as we are called from OOM. | |
2217 | * To do this we must instruct the shmfs to drop all of its | |
2218 | * backing pages, *now*. | |
2219 | */ | |
5537252b | 2220 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
a4f5ea64 | 2221 | obj->mm.madv = __I915_MADV_PURGED; |
4e5462ee | 2222 | obj->mm.pages = ERR_PTR(-EFAULT); |
225067ee | 2223 | } |
e5281ccd | 2224 | |
5537252b | 2225 | /* Try to discard unwanted pages */ |
03ac84f1 | 2226 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
225067ee | 2227 | { |
5537252b CW |
2228 | struct address_space *mapping; |
2229 | ||
1233e2db | 2230 | lockdep_assert_held(&obj->mm.lock); |
f1fa4f44 | 2231 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
1233e2db | 2232 | |
a4f5ea64 | 2233 | switch (obj->mm.madv) { |
5537252b CW |
2234 | case I915_MADV_DONTNEED: |
2235 | i915_gem_object_truncate(obj); | |
2236 | case __I915_MADV_PURGED: | |
2237 | return; | |
2238 | } | |
2239 | ||
2240 | if (obj->base.filp == NULL) | |
2241 | return; | |
2242 | ||
93c76a3d | 2243 | mapping = obj->base.filp->f_mapping, |
5537252b | 2244 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
e5281ccd CW |
2245 | } |
2246 | ||
5cdf5881 | 2247 | static void |
03ac84f1 CW |
2248 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
2249 | struct sg_table *pages) | |
673a394b | 2250 | { |
85d1225e DG |
2251 | struct sgt_iter sgt_iter; |
2252 | struct page *page; | |
1286ff73 | 2253 | |
e5facdf9 | 2254 | __i915_gem_object_release_shmem(obj, pages, true); |
673a394b | 2255 | |
03ac84f1 | 2256 | i915_gem_gtt_finish_pages(obj, pages); |
e2273302 | 2257 | |
6dacfd2f | 2258 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
03ac84f1 | 2259 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
280b713b | 2260 | |
03ac84f1 | 2261 | for_each_sgt_page(page, sgt_iter, pages) { |
a4f5ea64 | 2262 | if (obj->mm.dirty) |
9da3da66 | 2263 | set_page_dirty(page); |
3ef94daa | 2264 | |
a4f5ea64 | 2265 | if (obj->mm.madv == I915_MADV_WILLNEED) |
9da3da66 | 2266 | mark_page_accessed(page); |
3ef94daa | 2267 | |
09cbfeaf | 2268 | put_page(page); |
3ef94daa | 2269 | } |
a4f5ea64 | 2270 | obj->mm.dirty = false; |
673a394b | 2271 | |
03ac84f1 CW |
2272 | sg_free_table(pages); |
2273 | kfree(pages); | |
37e680a1 | 2274 | } |
6c085a72 | 2275 | |
96d77634 CW |
2276 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
2277 | { | |
2278 | struct radix_tree_iter iter; | |
6910d852 | 2279 | void __rcu **slot; |
96d77634 | 2280 | |
23e87338 | 2281 | rcu_read_lock(); |
a4f5ea64 CW |
2282 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
2283 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); | |
23e87338 | 2284 | rcu_read_unlock(); |
96d77634 CW |
2285 | } |
2286 | ||
548625ee CW |
2287 | void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
2288 | enum i915_mm_subclass subclass) | |
37e680a1 | 2289 | { |
f2123818 | 2290 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
03ac84f1 | 2291 | struct sg_table *pages; |
37e680a1 | 2292 | |
a4f5ea64 | 2293 | if (i915_gem_object_has_pinned_pages(obj)) |
03ac84f1 | 2294 | return; |
a5570178 | 2295 | |
15717de2 | 2296 | GEM_BUG_ON(obj->bind_count); |
f1fa4f44 | 2297 | if (!i915_gem_object_has_pages(obj)) |
1233e2db CW |
2298 | return; |
2299 | ||
2300 | /* May be called by shrinker from within get_pages() (on another bo) */ | |
548625ee | 2301 | mutex_lock_nested(&obj->mm.lock, subclass); |
1233e2db CW |
2302 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) |
2303 | goto unlock; | |
3e123027 | 2304 | |
a2165e31 CW |
2305 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
2306 | * array, hence protect them from being reaped by removing them from gtt | |
2307 | * lists early. */ | |
03ac84f1 CW |
2308 | pages = fetch_and_zero(&obj->mm.pages); |
2309 | GEM_BUG_ON(!pages); | |
a2165e31 | 2310 | |
f2123818 CW |
2311 | spin_lock(&i915->mm.obj_lock); |
2312 | list_del(&obj->mm.link); | |
2313 | spin_unlock(&i915->mm.obj_lock); | |
2314 | ||
a4f5ea64 | 2315 | if (obj->mm.mapping) { |
4b30cb23 CW |
2316 | void *ptr; |
2317 | ||
0ce81788 | 2318 | ptr = page_mask_bits(obj->mm.mapping); |
4b30cb23 CW |
2319 | if (is_vmalloc_addr(ptr)) |
2320 | vunmap(ptr); | |
fb8621d3 | 2321 | else |
4b30cb23 CW |
2322 | kunmap(kmap_to_page(ptr)); |
2323 | ||
a4f5ea64 | 2324 | obj->mm.mapping = NULL; |
0a798eb9 CW |
2325 | } |
2326 | ||
96d77634 CW |
2327 | __i915_gem_object_reset_page_iter(obj); |
2328 | ||
4e5462ee CW |
2329 | if (!IS_ERR(pages)) |
2330 | obj->ops->put_pages(obj, pages); | |
2331 | ||
a5c08166 MA |
2332 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
2333 | ||
1233e2db CW |
2334 | unlock: |
2335 | mutex_unlock(&obj->mm.lock); | |
6c085a72 CW |
2336 | } |
2337 | ||
935a2f77 | 2338 | static bool i915_sg_trim(struct sg_table *orig_st) |
0c40ce13 TU |
2339 | { |
2340 | struct sg_table new_st; | |
2341 | struct scatterlist *sg, *new_sg; | |
2342 | unsigned int i; | |
2343 | ||
2344 | if (orig_st->nents == orig_st->orig_nents) | |
935a2f77 | 2345 | return false; |
0c40ce13 | 2346 | |
8bfc478f | 2347 | if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN)) |
935a2f77 | 2348 | return false; |
0c40ce13 TU |
2349 | |
2350 | new_sg = new_st.sgl; | |
2351 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { | |
2352 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); | |
2353 | /* called before being DMA mapped, no need to copy sg->dma_* */ | |
2354 | new_sg = sg_next(new_sg); | |
2355 | } | |
c2dc6cc9 | 2356 | GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */ |
0c40ce13 TU |
2357 | |
2358 | sg_free_table(orig_st); | |
2359 | ||
2360 | *orig_st = new_st; | |
935a2f77 | 2361 | return true; |
0c40ce13 TU |
2362 | } |
2363 | ||
b91b09ee | 2364 | static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) |
e5281ccd | 2365 | { |
fac5e23e | 2366 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
d766ef53 CW |
2367 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
2368 | unsigned long i; | |
e5281ccd | 2369 | struct address_space *mapping; |
9da3da66 CW |
2370 | struct sg_table *st; |
2371 | struct scatterlist *sg; | |
85d1225e | 2372 | struct sgt_iter sgt_iter; |
e5281ccd | 2373 | struct page *page; |
90797e6d | 2374 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
5602452e | 2375 | unsigned int max_segment = i915_sg_segment_size(); |
84e8978e | 2376 | unsigned int sg_page_sizes; |
4846bf0c | 2377 | gfp_t noreclaim; |
e2273302 | 2378 | int ret; |
e5281ccd | 2379 | |
6c085a72 CW |
2380 | /* Assert that the object is not currently in any GPU domain. As it |
2381 | * wasn't in the GTT, there shouldn't be any way it could have been in | |
2382 | * a GPU cache | |
2383 | */ | |
03ac84f1 CW |
2384 | GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); |
2385 | GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); | |
6c085a72 | 2386 | |
9da3da66 CW |
2387 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
2388 | if (st == NULL) | |
b91b09ee | 2389 | return -ENOMEM; |
9da3da66 | 2390 | |
d766ef53 | 2391 | rebuild_st: |
9da3da66 | 2392 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
9da3da66 | 2393 | kfree(st); |
b91b09ee | 2394 | return -ENOMEM; |
9da3da66 | 2395 | } |
e5281ccd | 2396 | |
9da3da66 CW |
2397 | /* Get the list of pages out of our struct file. They'll be pinned |
2398 | * at this point until we release them. | |
2399 | * | |
2400 | * Fail silently without starting the shrinker | |
2401 | */ | |
93c76a3d | 2402 | mapping = obj->base.filp->f_mapping; |
0f6ab55d | 2403 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
4846bf0c CW |
2404 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
2405 | ||
90797e6d ID |
2406 | sg = st->sgl; |
2407 | st->nents = 0; | |
84e8978e | 2408 | sg_page_sizes = 0; |
90797e6d | 2409 | for (i = 0; i < page_count; i++) { |
4846bf0c CW |
2410 | const unsigned int shrink[] = { |
2411 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, | |
2412 | 0, | |
2413 | }, *s = shrink; | |
2414 | gfp_t gfp = noreclaim; | |
2415 | ||
2416 | do { | |
6c085a72 | 2417 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
4846bf0c CW |
2418 | if (likely(!IS_ERR(page))) |
2419 | break; | |
2420 | ||
2421 | if (!*s) { | |
2422 | ret = PTR_ERR(page); | |
2423 | goto err_sg; | |
2424 | } | |
2425 | ||
912d572d | 2426 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
4846bf0c | 2427 | cond_resched(); |
24f8e00a | 2428 | |
6c085a72 CW |
2429 | /* We've tried hard to allocate the memory by reaping |
2430 | * our own buffer, now let the real VM do its job and | |
2431 | * go down in flames if truly OOM. | |
24f8e00a CW |
2432 | * |
2433 | * However, since graphics tend to be disposable, | |
2434 | * defer the oom here by reporting the ENOMEM back | |
2435 | * to userspace. | |
6c085a72 | 2436 | */ |
4846bf0c CW |
2437 | if (!*s) { |
2438 | /* reclaim and warn, but no oom */ | |
2439 | gfp = mapping_gfp_mask(mapping); | |
eaf41801 CW |
2440 | |
2441 | /* Our bo are always dirty and so we require | |
2442 | * kswapd to reclaim our pages (direct reclaim | |
2443 | * does not effectively begin pageout of our | |
2444 | * buffers on its own). However, direct reclaim | |
2445 | * only waits for kswapd when under allocation | |
2446 | * congestion. So as a result __GFP_RECLAIM is | |
2447 | * unreliable and fails to actually reclaim our | |
2448 | * dirty pages -- unless you try over and over | |
2449 | * again with !__GFP_NORETRY. However, we still | |
2450 | * want to fail this allocation rather than | |
2451 | * trigger the out-of-memory killer and for | |
dbb32956 | 2452 | * this we want __GFP_RETRY_MAYFAIL. |
eaf41801 | 2453 | */ |
dbb32956 | 2454 | gfp |= __GFP_RETRY_MAYFAIL; |
e2273302 | 2455 | } |
4846bf0c CW |
2456 | } while (1); |
2457 | ||
871dfbd6 CW |
2458 | if (!i || |
2459 | sg->length >= max_segment || | |
2460 | page_to_pfn(page) != last_pfn + 1) { | |
a5c08166 | 2461 | if (i) { |
84e8978e | 2462 | sg_page_sizes |= sg->length; |
90797e6d | 2463 | sg = sg_next(sg); |
a5c08166 | 2464 | } |
90797e6d ID |
2465 | st->nents++; |
2466 | sg_set_page(sg, page, PAGE_SIZE, 0); | |
2467 | } else { | |
2468 | sg->length += PAGE_SIZE; | |
2469 | } | |
2470 | last_pfn = page_to_pfn(page); | |
3bbbe706 DV |
2471 | |
2472 | /* Check that the i965g/gm workaround works. */ | |
2473 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); | |
e5281ccd | 2474 | } |
a5c08166 | 2475 | if (sg) { /* loop terminated early; short sg table */ |
84e8978e | 2476 | sg_page_sizes |= sg->length; |
426729dc | 2477 | sg_mark_end(sg); |
a5c08166 | 2478 | } |
74ce6b6c | 2479 | |
0c40ce13 TU |
2480 | /* Trim unused sg entries to avoid wasting memory. */ |
2481 | i915_sg_trim(st); | |
2482 | ||
03ac84f1 | 2483 | ret = i915_gem_gtt_prepare_pages(obj, st); |
d766ef53 CW |
2484 | if (ret) { |
2485 | /* DMA remapping failed? One possible cause is that | |
2486 | * it could not reserve enough large entries, asking | |
2487 | * for PAGE_SIZE chunks instead may be helpful. | |
2488 | */ | |
2489 | if (max_segment > PAGE_SIZE) { | |
2490 | for_each_sgt_page(page, sgt_iter, st) | |
2491 | put_page(page); | |
2492 | sg_free_table(st); | |
2493 | ||
2494 | max_segment = PAGE_SIZE; | |
2495 | goto rebuild_st; | |
2496 | } else { | |
2497 | dev_warn(&dev_priv->drm.pdev->dev, | |
2498 | "Failed to DMA remap %lu pages\n", | |
2499 | page_count); | |
2500 | goto err_pages; | |
2501 | } | |
2502 | } | |
e2273302 | 2503 | |
6dacfd2f | 2504 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
03ac84f1 | 2505 | i915_gem_object_do_bit_17_swizzle(obj, st); |
e5281ccd | 2506 | |
84e8978e | 2507 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
b91b09ee MA |
2508 | |
2509 | return 0; | |
e5281ccd | 2510 | |
b17993b7 | 2511 | err_sg: |
90797e6d | 2512 | sg_mark_end(sg); |
b17993b7 | 2513 | err_pages: |
85d1225e DG |
2514 | for_each_sgt_page(page, sgt_iter, st) |
2515 | put_page(page); | |
9da3da66 CW |
2516 | sg_free_table(st); |
2517 | kfree(st); | |
0820baf3 CW |
2518 | |
2519 | /* shmemfs first checks if there is enough memory to allocate the page | |
2520 | * and reports ENOSPC should there be insufficient, along with the usual | |
2521 | * ENOMEM for a genuine allocation failure. | |
2522 | * | |
2523 | * We use ENOSPC in our driver to mean that we have run out of aperture | |
2524 | * space and so want to translate the error from shmemfs back to our | |
2525 | * usual understanding of ENOMEM. | |
2526 | */ | |
e2273302 ID |
2527 | if (ret == -ENOSPC) |
2528 | ret = -ENOMEM; | |
2529 | ||
b91b09ee | 2530 | return ret; |
03ac84f1 CW |
2531 | } |
2532 | ||
2533 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, | |
a5c08166 | 2534 | struct sg_table *pages, |
84e8978e | 2535 | unsigned int sg_page_sizes) |
03ac84f1 | 2536 | { |
a5c08166 MA |
2537 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
2538 | unsigned long supported = INTEL_INFO(i915)->page_sizes; | |
2539 | int i; | |
2540 | ||
1233e2db | 2541 | lockdep_assert_held(&obj->mm.lock); |
03ac84f1 CW |
2542 | |
2543 | obj->mm.get_page.sg_pos = pages->sgl; | |
2544 | obj->mm.get_page.sg_idx = 0; | |
2545 | ||
2546 | obj->mm.pages = pages; | |
2c3a3f44 CW |
2547 | |
2548 | if (i915_gem_object_is_tiled(obj) && | |
f2123818 | 2549 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
2c3a3f44 CW |
2550 | GEM_BUG_ON(obj->mm.quirked); |
2551 | __i915_gem_object_pin_pages(obj); | |
2552 | obj->mm.quirked = true; | |
2553 | } | |
a5c08166 | 2554 | |
84e8978e MA |
2555 | GEM_BUG_ON(!sg_page_sizes); |
2556 | obj->mm.page_sizes.phys = sg_page_sizes; | |
a5c08166 MA |
2557 | |
2558 | /* | |
84e8978e MA |
2559 | * Calculate the supported page-sizes which fit into the given |
2560 | * sg_page_sizes. This will give us the page-sizes which we may be able | |
2561 | * to use opportunistically when later inserting into the GTT. For | |
2562 | * example if phys=2G, then in theory we should be able to use 1G, 2M, | |
2563 | * 64K or 4K pages, although in practice this will depend on a number of | |
2564 | * other factors. | |
a5c08166 MA |
2565 | */ |
2566 | obj->mm.page_sizes.sg = 0; | |
2567 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { | |
2568 | if (obj->mm.page_sizes.phys & ~0u << i) | |
2569 | obj->mm.page_sizes.sg |= BIT(i); | |
2570 | } | |
a5c08166 | 2571 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
f2123818 CW |
2572 | |
2573 | spin_lock(&i915->mm.obj_lock); | |
2574 | list_add(&obj->mm.link, &i915->mm.unbound_list); | |
2575 | spin_unlock(&i915->mm.obj_lock); | |
03ac84f1 CW |
2576 | } |
2577 | ||
2578 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) | |
2579 | { | |
b91b09ee | 2580 | int err; |
2c3a3f44 | 2581 | |
03ac84f1 CW |
2582 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
2583 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); | |
2584 | return -EFAULT; | |
2585 | } | |
2586 | ||
b91b09ee MA |
2587 | err = obj->ops->get_pages(obj); |
2588 | GEM_BUG_ON(!err && IS_ERR_OR_NULL(obj->mm.pages)); | |
03ac84f1 | 2589 | |
b91b09ee | 2590 | return err; |
673a394b EA |
2591 | } |
2592 | ||
37e680a1 | 2593 | /* Ensure that the associated pages are gathered from the backing storage |
1233e2db | 2594 | * and pinned into our object. i915_gem_object_pin_pages() may be called |
37e680a1 | 2595 | * multiple times before they are released by a single call to |
1233e2db | 2596 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
37e680a1 CW |
2597 | * either as a result of memory pressure (reaping pages under the shrinker) |
2598 | * or as the object is itself released. | |
2599 | */ | |
a4f5ea64 | 2600 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
37e680a1 | 2601 | { |
03ac84f1 | 2602 | int err; |
37e680a1 | 2603 | |
1233e2db CW |
2604 | err = mutex_lock_interruptible(&obj->mm.lock); |
2605 | if (err) | |
2606 | return err; | |
4c7d62c6 | 2607 | |
f1fa4f44 | 2608 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
88c880bb CW |
2609 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
2610 | ||
2c3a3f44 CW |
2611 | err = ____i915_gem_object_get_pages(obj); |
2612 | if (err) | |
2613 | goto unlock; | |
37e680a1 | 2614 | |
2c3a3f44 CW |
2615 | smp_mb__before_atomic(); |
2616 | } | |
2617 | atomic_inc(&obj->mm.pages_pin_count); | |
ee286370 | 2618 | |
1233e2db CW |
2619 | unlock: |
2620 | mutex_unlock(&obj->mm.lock); | |
03ac84f1 | 2621 | return err; |
673a394b EA |
2622 | } |
2623 | ||
dd6034c6 | 2624 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
d31d7cb1 CW |
2625 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
2626 | enum i915_map_type type) | |
dd6034c6 DG |
2627 | { |
2628 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; | |
a4f5ea64 | 2629 | struct sg_table *sgt = obj->mm.pages; |
85d1225e DG |
2630 | struct sgt_iter sgt_iter; |
2631 | struct page *page; | |
b338fa47 DG |
2632 | struct page *stack_pages[32]; |
2633 | struct page **pages = stack_pages; | |
dd6034c6 | 2634 | unsigned long i = 0; |
d31d7cb1 | 2635 | pgprot_t pgprot; |
dd6034c6 DG |
2636 | void *addr; |
2637 | ||
2638 | /* A single page can always be kmapped */ | |
d31d7cb1 | 2639 | if (n_pages == 1 && type == I915_MAP_WB) |
dd6034c6 DG |
2640 | return kmap(sg_page(sgt->sgl)); |
2641 | ||
b338fa47 DG |
2642 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
2643 | /* Too big for stack -- allocate temporary array instead */ | |
0ee931c4 | 2644 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
b338fa47 DG |
2645 | if (!pages) |
2646 | return NULL; | |
2647 | } | |
dd6034c6 | 2648 | |
85d1225e DG |
2649 | for_each_sgt_page(page, sgt_iter, sgt) |
2650 | pages[i++] = page; | |
dd6034c6 DG |
2651 | |
2652 | /* Check that we have the expected number of pages */ | |
2653 | GEM_BUG_ON(i != n_pages); | |
2654 | ||
d31d7cb1 | 2655 | switch (type) { |
3b24e7e8 CW |
2656 | default: |
2657 | MISSING_CASE(type); | |
2658 | /* fallthrough to use PAGE_KERNEL anyway */ | |
d31d7cb1 CW |
2659 | case I915_MAP_WB: |
2660 | pgprot = PAGE_KERNEL; | |
2661 | break; | |
2662 | case I915_MAP_WC: | |
2663 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); | |
2664 | break; | |
2665 | } | |
2666 | addr = vmap(pages, n_pages, 0, pgprot); | |
dd6034c6 | 2667 | |
b338fa47 | 2668 | if (pages != stack_pages) |
2098105e | 2669 | kvfree(pages); |
dd6034c6 DG |
2670 | |
2671 | return addr; | |
2672 | } | |
2673 | ||
2674 | /* get, pin, and map the pages of the object into kernel space */ | |
d31d7cb1 CW |
2675 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
2676 | enum i915_map_type type) | |
0a798eb9 | 2677 | { |
d31d7cb1 CW |
2678 | enum i915_map_type has_type; |
2679 | bool pinned; | |
2680 | void *ptr; | |
0a798eb9 CW |
2681 | int ret; |
2682 | ||
d31d7cb1 | 2683 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
0a798eb9 | 2684 | |
1233e2db | 2685 | ret = mutex_lock_interruptible(&obj->mm.lock); |
0a798eb9 CW |
2686 | if (ret) |
2687 | return ERR_PTR(ret); | |
2688 | ||
3b24e7e8 CW |
2689 | pinned = !(type & I915_MAP_OVERRIDE); |
2690 | type &= ~I915_MAP_OVERRIDE; | |
2691 | ||
1233e2db | 2692 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
f1fa4f44 | 2693 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
88c880bb CW |
2694 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
2695 | ||
2c3a3f44 CW |
2696 | ret = ____i915_gem_object_get_pages(obj); |
2697 | if (ret) | |
2698 | goto err_unlock; | |
1233e2db | 2699 | |
2c3a3f44 CW |
2700 | smp_mb__before_atomic(); |
2701 | } | |
2702 | atomic_inc(&obj->mm.pages_pin_count); | |
1233e2db CW |
2703 | pinned = false; |
2704 | } | |
f1fa4f44 | 2705 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
0a798eb9 | 2706 | |
0ce81788 | 2707 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
d31d7cb1 CW |
2708 | if (ptr && has_type != type) { |
2709 | if (pinned) { | |
2710 | ret = -EBUSY; | |
1233e2db | 2711 | goto err_unpin; |
0a798eb9 | 2712 | } |
d31d7cb1 CW |
2713 | |
2714 | if (is_vmalloc_addr(ptr)) | |
2715 | vunmap(ptr); | |
2716 | else | |
2717 | kunmap(kmap_to_page(ptr)); | |
2718 | ||
a4f5ea64 | 2719 | ptr = obj->mm.mapping = NULL; |
0a798eb9 CW |
2720 | } |
2721 | ||
d31d7cb1 CW |
2722 | if (!ptr) { |
2723 | ptr = i915_gem_object_map(obj, type); | |
2724 | if (!ptr) { | |
2725 | ret = -ENOMEM; | |
1233e2db | 2726 | goto err_unpin; |
d31d7cb1 CW |
2727 | } |
2728 | ||
0ce81788 | 2729 | obj->mm.mapping = page_pack_bits(ptr, type); |
d31d7cb1 CW |
2730 | } |
2731 | ||
1233e2db CW |
2732 | out_unlock: |
2733 | mutex_unlock(&obj->mm.lock); | |
d31d7cb1 CW |
2734 | return ptr; |
2735 | ||
1233e2db CW |
2736 | err_unpin: |
2737 | atomic_dec(&obj->mm.pages_pin_count); | |
2738 | err_unlock: | |
2739 | ptr = ERR_PTR(ret); | |
2740 | goto out_unlock; | |
0a798eb9 CW |
2741 | } |
2742 | ||
7c55e2c5 CW |
2743 | static int |
2744 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, | |
2745 | const struct drm_i915_gem_pwrite *arg) | |
2746 | { | |
2747 | struct address_space *mapping = obj->base.filp->f_mapping; | |
2748 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); | |
2749 | u64 remain, offset; | |
2750 | unsigned int pg; | |
2751 | ||
2752 | /* Before we instantiate/pin the backing store for our use, we | |
2753 | * can prepopulate the shmemfs filp efficiently using a write into | |
2754 | * the pagecache. We avoid the penalty of instantiating all the | |
2755 | * pages, important if the user is just writing to a few and never | |
2756 | * uses the object on the GPU, and using a direct write into shmemfs | |
2757 | * allows it to avoid the cost of retrieving a page (either swapin | |
2758 | * or clearing-before-use) before it is overwritten. | |
2759 | */ | |
f1fa4f44 | 2760 | if (i915_gem_object_has_pages(obj)) |
7c55e2c5 CW |
2761 | return -ENODEV; |
2762 | ||
ca8d7822 CW |
2763 | if (obj->mm.madv != I915_MADV_WILLNEED) |
2764 | return -EFAULT; | |
2765 | ||
7c55e2c5 CW |
2766 | /* Before the pages are instantiated the object is treated as being |
2767 | * in the CPU domain. The pages will be clflushed as required before | |
2768 | * use, and we can freely write into the pages directly. If userspace | |
2769 | * races pwrite with any other operation; corruption will ensue - | |
2770 | * that is userspace's prerogative! | |
2771 | */ | |
2772 | ||
2773 | remain = arg->size; | |
2774 | offset = arg->offset; | |
2775 | pg = offset_in_page(offset); | |
2776 | ||
2777 | do { | |
2778 | unsigned int len, unwritten; | |
2779 | struct page *page; | |
2780 | void *data, *vaddr; | |
2781 | int err; | |
2782 | ||
2783 | len = PAGE_SIZE - pg; | |
2784 | if (len > remain) | |
2785 | len = remain; | |
2786 | ||
2787 | err = pagecache_write_begin(obj->base.filp, mapping, | |
2788 | offset, len, 0, | |
2789 | &page, &data); | |
2790 | if (err < 0) | |
2791 | return err; | |
2792 | ||
2793 | vaddr = kmap(page); | |
2794 | unwritten = copy_from_user(vaddr + pg, user_data, len); | |
2795 | kunmap(page); | |
2796 | ||
2797 | err = pagecache_write_end(obj->base.filp, mapping, | |
2798 | offset, len, len - unwritten, | |
2799 | page, data); | |
2800 | if (err < 0) | |
2801 | return err; | |
2802 | ||
2803 | if (unwritten) | |
2804 | return -EFAULT; | |
2805 | ||
2806 | remain -= len; | |
2807 | user_data += len; | |
2808 | offset += len; | |
2809 | pg = 0; | |
2810 | } while (remain); | |
2811 | ||
2812 | return 0; | |
2813 | } | |
2814 | ||
77b25a97 CW |
2815 | static bool ban_context(const struct i915_gem_context *ctx, |
2816 | unsigned int score) | |
be62acb4 | 2817 | { |
6095868a | 2818 | return (i915_gem_context_is_bannable(ctx) && |
77b25a97 | 2819 | score >= CONTEXT_SCORE_BAN_THRESHOLD); |
be62acb4 MK |
2820 | } |
2821 | ||
e5e1fc47 | 2822 | static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) |
aa60c664 | 2823 | { |
77b25a97 CW |
2824 | unsigned int score; |
2825 | bool banned; | |
b083a087 | 2826 | |
77b25a97 | 2827 | atomic_inc(&ctx->guilty_count); |
b083a087 | 2828 | |
77b25a97 CW |
2829 | score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score); |
2830 | banned = ban_context(ctx, score); | |
2831 | DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n", | |
2832 | ctx->name, score, yesno(banned)); | |
2833 | if (!banned) | |
b083a087 MK |
2834 | return; |
2835 | ||
77b25a97 CW |
2836 | i915_gem_context_set_banned(ctx); |
2837 | if (!IS_ERR_OR_NULL(ctx->file_priv)) { | |
2838 | atomic_inc(&ctx->file_priv->context_bans); | |
2839 | DRM_DEBUG_DRIVER("client %s has had %d context banned\n", | |
2840 | ctx->name, atomic_read(&ctx->file_priv->context_bans)); | |
2841 | } | |
e5e1fc47 MK |
2842 | } |
2843 | ||
2844 | static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) | |
2845 | { | |
77b25a97 | 2846 | atomic_inc(&ctx->active_count); |
aa60c664 MK |
2847 | } |
2848 | ||
8d9fc7fd | 2849 | struct drm_i915_gem_request * |
0bc40be8 | 2850 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
9375e446 | 2851 | { |
754c9fd5 CW |
2852 | struct drm_i915_gem_request *request, *active = NULL; |
2853 | unsigned long flags; | |
4db080f9 | 2854 | |
f69a02c9 CW |
2855 | /* We are called by the error capture and reset at a random |
2856 | * point in time. In particular, note that neither is crucially | |
2857 | * ordered with an interrupt. After a hang, the GPU is dead and we | |
2858 | * assume that no more writes can happen (we waited long enough for | |
2859 | * all writes that were in transaction to be flushed) - adding an | |
2860 | * extra delay for a recent interrupt is pointless. Hence, we do | |
2861 | * not need an engine->irq_seqno_barrier() before the seqno reads. | |
2862 | */ | |
754c9fd5 | 2863 | spin_lock_irqsave(&engine->timeline->lock, flags); |
73cb9701 | 2864 | list_for_each_entry(request, &engine->timeline->requests, link) { |
754c9fd5 CW |
2865 | if (__i915_gem_request_completed(request, |
2866 | request->global_seqno)) | |
4db080f9 | 2867 | continue; |
aa60c664 | 2868 | |
36193acd | 2869 | GEM_BUG_ON(request->engine != engine); |
c00122f3 CW |
2870 | GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
2871 | &request->fence.flags)); | |
754c9fd5 CW |
2872 | |
2873 | active = request; | |
2874 | break; | |
4db080f9 | 2875 | } |
754c9fd5 | 2876 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
b6b0fac0 | 2877 | |
754c9fd5 | 2878 | return active; |
b6b0fac0 MK |
2879 | } |
2880 | ||
bf2f0436 MK |
2881 | static bool engine_stalled(struct intel_engine_cs *engine) |
2882 | { | |
2883 | if (!engine->hangcheck.stalled) | |
2884 | return false; | |
2885 | ||
2886 | /* Check for possible seqno movement after hang declaration */ | |
2887 | if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) { | |
2888 | DRM_DEBUG_DRIVER("%s pardoned\n", engine->name); | |
2889 | return false; | |
2890 | } | |
2891 | ||
2892 | return true; | |
2893 | } | |
2894 | ||
a1ef70e1 MT |
2895 | /* |
2896 | * Ensure irq handler finishes, and not run again. | |
2897 | * Also return the active request so that we only search for it once. | |
2898 | */ | |
2899 | struct drm_i915_gem_request * | |
2900 | i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) | |
2901 | { | |
2902 | struct drm_i915_gem_request *request = NULL; | |
2903 | ||
1749d90f CW |
2904 | /* |
2905 | * During the reset sequence, we must prevent the engine from | |
2906 | * entering RC6. As the context state is undefined until we restart | |
2907 | * the engine, if it does enter RC6 during the reset, the state | |
2908 | * written to the powercontext is undefined and so we may lose | |
2909 | * GPU state upon resume, i.e. fail to restart after a reset. | |
2910 | */ | |
2911 | intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); | |
2912 | ||
2913 | /* | |
2914 | * Prevent the signaler thread from updating the request | |
a1ef70e1 MT |
2915 | * state (by calling dma_fence_signal) as we are processing |
2916 | * the reset. The write from the GPU of the seqno is | |
2917 | * asynchronous and the signaler thread may see a different | |
2918 | * value to us and declare the request complete, even though | |
2919 | * the reset routine have picked that request as the active | |
2920 | * (incomplete) request. This conflict is not handled | |
2921 | * gracefully! | |
2922 | */ | |
2923 | kthread_park(engine->breadcrumbs.signaler); | |
2924 | ||
1749d90f CW |
2925 | /* |
2926 | * Prevent request submission to the hardware until we have | |
a1ef70e1 MT |
2927 | * completed the reset in i915_gem_reset_finish(). If a request |
2928 | * is completed by one engine, it may then queue a request | |
2929 | * to a second via its engine->irq_tasklet *just* as we are | |
2930 | * calling engine->init_hw() and also writing the ELSP. | |
2931 | * Turning off the engine->irq_tasklet until the reset is over | |
2932 | * prevents the race. | |
2933 | */ | |
b620e870 MK |
2934 | tasklet_kill(&engine->execlists.irq_tasklet); |
2935 | tasklet_disable(&engine->execlists.irq_tasklet); | |
a1ef70e1 MT |
2936 | |
2937 | if (engine->irq_seqno_barrier) | |
2938 | engine->irq_seqno_barrier(engine); | |
2939 | ||
d1d1ebf4 CW |
2940 | request = i915_gem_find_active_request(engine); |
2941 | if (request && request->fence.error == -EIO) | |
2942 | request = ERR_PTR(-EIO); /* Previous reset failed! */ | |
a1ef70e1 MT |
2943 | |
2944 | return request; | |
2945 | } | |
2946 | ||
0e178aef | 2947 | int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) |
4c965543 CW |
2948 | { |
2949 | struct intel_engine_cs *engine; | |
a1ef70e1 | 2950 | struct drm_i915_gem_request *request; |
4c965543 | 2951 | enum intel_engine_id id; |
0e178aef | 2952 | int err = 0; |
4c965543 | 2953 | |
0e178aef | 2954 | for_each_engine(engine, dev_priv, id) { |
a1ef70e1 MT |
2955 | request = i915_gem_reset_prepare_engine(engine); |
2956 | if (IS_ERR(request)) { | |
2957 | err = PTR_ERR(request); | |
2958 | continue; | |
0e178aef | 2959 | } |
c64992e0 MT |
2960 | |
2961 | engine->hangcheck.active_request = request; | |
0e178aef CW |
2962 | } |
2963 | ||
4c965543 | 2964 | i915_gem_revoke_fences(dev_priv); |
0e178aef CW |
2965 | |
2966 | return err; | |
4c965543 CW |
2967 | } |
2968 | ||
36193acd | 2969 | static void skip_request(struct drm_i915_gem_request *request) |
821ed7df CW |
2970 | { |
2971 | void *vaddr = request->ring->vaddr; | |
2972 | u32 head; | |
2973 | ||
2974 | /* As this request likely depends on state from the lost | |
2975 | * context, clear out all the user operations leaving the | |
2976 | * breadcrumb at the end (so we get the fence notifications). | |
2977 | */ | |
2978 | head = request->head; | |
2979 | if (request->postfix < head) { | |
2980 | memset(vaddr + head, 0, request->ring->size - head); | |
2981 | head = 0; | |
2982 | } | |
2983 | memset(vaddr + head, 0, request->postfix - head); | |
c0d5f32c CW |
2984 | |
2985 | dma_fence_set_error(&request->fence, -EIO); | |
821ed7df CW |
2986 | } |
2987 | ||
36193acd MK |
2988 | static void engine_skip_context(struct drm_i915_gem_request *request) |
2989 | { | |
2990 | struct intel_engine_cs *engine = request->engine; | |
2991 | struct i915_gem_context *hung_ctx = request->ctx; | |
2992 | struct intel_timeline *timeline; | |
2993 | unsigned long flags; | |
2994 | ||
2995 | timeline = i915_gem_context_lookup_timeline(hung_ctx, engine); | |
2996 | ||
2997 | spin_lock_irqsave(&engine->timeline->lock, flags); | |
2998 | spin_lock(&timeline->lock); | |
2999 | ||
3000 | list_for_each_entry_continue(request, &engine->timeline->requests, link) | |
3001 | if (request->ctx == hung_ctx) | |
3002 | skip_request(request); | |
3003 | ||
3004 | list_for_each_entry(request, &timeline->requests, link) | |
3005 | skip_request(request); | |
3006 | ||
3007 | spin_unlock(&timeline->lock); | |
3008 | spin_unlock_irqrestore(&engine->timeline->lock, flags); | |
3009 | } | |
3010 | ||
d1d1ebf4 CW |
3011 | /* Returns the request if it was guilty of the hang */ |
3012 | static struct drm_i915_gem_request * | |
3013 | i915_gem_reset_request(struct intel_engine_cs *engine, | |
3014 | struct drm_i915_gem_request *request) | |
61da5362 | 3015 | { |
71895a08 MK |
3016 | /* The guilty request will get skipped on a hung engine. |
3017 | * | |
3018 | * Users of client default contexts do not rely on logical | |
3019 | * state preserved between batches so it is safe to execute | |
3020 | * queued requests following the hang. Non default contexts | |
3021 | * rely on preserved state, so skipping a batch loses the | |
3022 | * evolution of the state and it needs to be considered corrupted. | |
3023 | * Executing more queued batches on top of corrupted state is | |
3024 | * risky. But we take the risk by trying to advance through | |
3025 | * the queued requests in order to make the client behaviour | |
3026 | * more predictable around resets, by not throwing away random | |
3027 | * amount of batches it has prepared for execution. Sophisticated | |
3028 | * clients can use gem_reset_stats_ioctl and dma fence status | |
3029 | * (exported via sync_file info ioctl on explicit fences) to observe | |
3030 | * when it loses the context state and should rebuild accordingly. | |
3031 | * | |
3032 | * The context ban, and ultimately the client ban, mechanism are safety | |
3033 | * valves if client submission ends up resulting in nothing more than | |
3034 | * subsequent hangs. | |
3035 | */ | |
3036 | ||
d1d1ebf4 | 3037 | if (engine_stalled(engine)) { |
61da5362 MK |
3038 | i915_gem_context_mark_guilty(request->ctx); |
3039 | skip_request(request); | |
d1d1ebf4 CW |
3040 | |
3041 | /* If this context is now banned, skip all pending requests. */ | |
3042 | if (i915_gem_context_is_banned(request->ctx)) | |
3043 | engine_skip_context(request); | |
61da5362 | 3044 | } else { |
d1d1ebf4 CW |
3045 | /* |
3046 | * Since this is not the hung engine, it may have advanced | |
3047 | * since the hang declaration. Double check by refinding | |
3048 | * the active request at the time of the reset. | |
3049 | */ | |
3050 | request = i915_gem_find_active_request(engine); | |
3051 | if (request) { | |
3052 | i915_gem_context_mark_innocent(request->ctx); | |
3053 | dma_fence_set_error(&request->fence, -EAGAIN); | |
3054 | ||
3055 | /* Rewind the engine to replay the incomplete rq */ | |
3056 | spin_lock_irq(&engine->timeline->lock); | |
3057 | request = list_prev_entry(request, link); | |
3058 | if (&request->link == &engine->timeline->requests) | |
3059 | request = NULL; | |
3060 | spin_unlock_irq(&engine->timeline->lock); | |
3061 | } | |
61da5362 MK |
3062 | } |
3063 | ||
d1d1ebf4 | 3064 | return request; |
61da5362 MK |
3065 | } |
3066 | ||
a1ef70e1 MT |
3067 | void i915_gem_reset_engine(struct intel_engine_cs *engine, |
3068 | struct drm_i915_gem_request *request) | |
b6b0fac0 | 3069 | { |
ed454f2c CW |
3070 | engine->irq_posted = 0; |
3071 | ||
d1d1ebf4 CW |
3072 | if (request) |
3073 | request = i915_gem_reset_request(engine, request); | |
3074 | ||
3075 | if (request) { | |
c0dcb203 CW |
3076 | DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n", |
3077 | engine->name, request->global_seqno); | |
c0dcb203 | 3078 | } |
821ed7df CW |
3079 | |
3080 | /* Setup the CS to resume from the breadcrumb of the hung request */ | |
3081 | engine->reset_hw(engine, request); | |
4db080f9 | 3082 | } |
aa60c664 | 3083 | |
d8027093 | 3084 | void i915_gem_reset(struct drm_i915_private *dev_priv) |
4db080f9 | 3085 | { |
821ed7df | 3086 | struct intel_engine_cs *engine; |
3b3f1650 | 3087 | enum intel_engine_id id; |
608c1a52 | 3088 | |
4c7d62c6 CW |
3089 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
3090 | ||
821ed7df CW |
3091 | i915_gem_retire_requests(dev_priv); |
3092 | ||
2ae55738 CW |
3093 | for_each_engine(engine, dev_priv, id) { |
3094 | struct i915_gem_context *ctx; | |
3095 | ||
c64992e0 | 3096 | i915_gem_reset_engine(engine, engine->hangcheck.active_request); |
2ae55738 CW |
3097 | ctx = fetch_and_zero(&engine->last_retired_context); |
3098 | if (ctx) | |
3099 | engine->context_unpin(engine, ctx); | |
3100 | } | |
821ed7df | 3101 | |
4362f4f6 | 3102 | i915_gem_restore_fences(dev_priv); |
f2a91d1a CW |
3103 | |
3104 | if (dev_priv->gt.awake) { | |
3105 | intel_sanitize_gt_powersave(dev_priv); | |
3106 | intel_enable_gt_powersave(dev_priv); | |
3107 | if (INTEL_GEN(dev_priv) >= 6) | |
3108 | gen6_rps_busy(dev_priv); | |
3109 | } | |
821ed7df CW |
3110 | } |
3111 | ||
a1ef70e1 MT |
3112 | void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) |
3113 | { | |
b620e870 | 3114 | tasklet_enable(&engine->execlists.irq_tasklet); |
a1ef70e1 | 3115 | kthread_unpark(engine->breadcrumbs.signaler); |
1749d90f CW |
3116 | |
3117 | intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); | |
a1ef70e1 MT |
3118 | } |
3119 | ||
d8027093 CW |
3120 | void i915_gem_reset_finish(struct drm_i915_private *dev_priv) |
3121 | { | |
1f7b847d CW |
3122 | struct intel_engine_cs *engine; |
3123 | enum intel_engine_id id; | |
3124 | ||
d8027093 | 3125 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
1f7b847d | 3126 | |
fe3288b5 | 3127 | for_each_engine(engine, dev_priv, id) { |
c64992e0 | 3128 | engine->hangcheck.active_request = NULL; |
a1ef70e1 | 3129 | i915_gem_reset_finish_engine(engine); |
fe3288b5 | 3130 | } |
d8027093 CW |
3131 | } |
3132 | ||
821ed7df | 3133 | static void nop_submit_request(struct drm_i915_gem_request *request) |
af7a8ffa | 3134 | { |
af7a8ffa DV |
3135 | dma_fence_set_error(&request->fence, -EIO); |
3136 | ||
3137 | i915_gem_request_submit(request); | |
3138 | } | |
3139 | ||
3140 | static void nop_complete_submit_request(struct drm_i915_gem_request *request) | |
821ed7df | 3141 | { |
b85577b7 CW |
3142 | unsigned long flags; |
3143 | ||
3cd9442f | 3144 | dma_fence_set_error(&request->fence, -EIO); |
b85577b7 CW |
3145 | |
3146 | spin_lock_irqsave(&request->engine->timeline->lock, flags); | |
3147 | __i915_gem_request_submit(request); | |
3dcf93f7 | 3148 | intel_engine_init_global_seqno(request->engine, request->global_seqno); |
b85577b7 | 3149 | spin_unlock_irqrestore(&request->engine->timeline->lock, flags); |
821ed7df CW |
3150 | } |
3151 | ||
af7a8ffa | 3152 | void i915_gem_set_wedged(struct drm_i915_private *i915) |
821ed7df | 3153 | { |
af7a8ffa DV |
3154 | struct intel_engine_cs *engine; |
3155 | enum intel_engine_id id; | |
3cd9442f | 3156 | |
af7a8ffa DV |
3157 | /* |
3158 | * First, stop submission to hw, but do not yet complete requests by | |
3159 | * rolling the global seqno forward (since this would complete requests | |
3160 | * for which we haven't set the fence error to EIO yet). | |
20e4933c | 3161 | */ |
af7a8ffa DV |
3162 | for_each_engine(engine, i915, id) |
3163 | engine->submit_request = nop_submit_request; | |
3cd9442f | 3164 | |
dcb4c12a | 3165 | /* |
af7a8ffa DV |
3166 | * Make sure no one is running the old callback before we proceed with |
3167 | * cancelling requests and resetting the completion tracking. Otherwise | |
3168 | * we might submit a request to the hardware which never completes. | |
dcb4c12a | 3169 | */ |
af7a8ffa | 3170 | synchronize_rcu(); |
dcb4c12a | 3171 | |
af7a8ffa DV |
3172 | for_each_engine(engine, i915, id) { |
3173 | /* Mark all executing requests as skipped */ | |
3174 | engine->cancel_requests(engine); | |
4ee056f4 | 3175 | |
af7a8ffa DV |
3176 | /* |
3177 | * Only once we've force-cancelled all in-flight requests can we | |
3178 | * start to complete all requests. | |
4ee056f4 | 3179 | */ |
af7a8ffa | 3180 | engine->submit_request = nop_complete_submit_request; |
dcb4c12a | 3181 | } |
5e32d748 | 3182 | |
af7a8ffa DV |
3183 | /* |
3184 | * Make sure no request can slip through without getting completed by | |
3185 | * either this call here to intel_engine_init_global_seqno, or the one | |
3186 | * in nop_complete_submit_request. | |
5e32d748 | 3187 | */ |
af7a8ffa | 3188 | synchronize_rcu(); |
673a394b | 3189 | |
af7a8ffa DV |
3190 | for_each_engine(engine, i915, id) { |
3191 | unsigned long flags; | |
673a394b | 3192 | |
af7a8ffa DV |
3193 | /* Mark all pending requests as complete so that any concurrent |
3194 | * (lockless) lookup doesn't try and wait upon the request as we | |
3195 | * reset it. | |
3196 | */ | |
3197 | spin_lock_irqsave(&engine->timeline->lock, flags); | |
3198 | intel_engine_init_global_seqno(engine, | |
3199 | intel_engine_last_submit(engine)); | |
3200 | spin_unlock_irqrestore(&engine->timeline->lock, flags); | |
3201 | } | |
20e4933c | 3202 | |
3d7adbbf CW |
3203 | set_bit(I915_WEDGED, &i915->gpu_error.flags); |
3204 | wake_up_all(&i915->gpu_error.reset_queue); | |
673a394b EA |
3205 | } |
3206 | ||
2e8f9d32 CW |
3207 | bool i915_gem_unset_wedged(struct drm_i915_private *i915) |
3208 | { | |
3209 | struct i915_gem_timeline *tl; | |
3210 | int i; | |
3211 | ||
3212 | lockdep_assert_held(&i915->drm.struct_mutex); | |
3213 | if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) | |
3214 | return true; | |
3215 | ||
3216 | /* Before unwedging, make sure that all pending operations | |
3217 | * are flushed and errored out - we may have requests waiting upon | |
3218 | * third party fences. We marked all inflight requests as EIO, and | |
3219 | * every execbuf since returned EIO, for consistency we want all | |
3220 | * the currently pending requests to also be marked as EIO, which | |
3221 | * is done inside our nop_submit_request - and so we must wait. | |
3222 | * | |
3223 | * No more can be submitted until we reset the wedged bit. | |
3224 | */ | |
3225 | list_for_each_entry(tl, &i915->gt.timelines, link) { | |
3226 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { | |
3227 | struct drm_i915_gem_request *rq; | |
3228 | ||
3229 | rq = i915_gem_active_peek(&tl->engine[i].last_request, | |
3230 | &i915->drm.struct_mutex); | |
3231 | if (!rq) | |
3232 | continue; | |
3233 | ||
3234 | /* We can't use our normal waiter as we want to | |
3235 | * avoid recursively trying to handle the current | |
3236 | * reset. The basic dma_fence_default_wait() installs | |
3237 | * a callback for dma_fence_signal(), which is | |
3238 | * triggered by our nop handler (indirectly, the | |
3239 | * callback enables the signaler thread which is | |
3240 | * woken by the nop_submit_request() advancing the seqno | |
3241 | * and when the seqno passes the fence, the signaler | |
3242 | * then signals the fence waking us up). | |
3243 | */ | |
3244 | if (dma_fence_default_wait(&rq->fence, true, | |
3245 | MAX_SCHEDULE_TIMEOUT) < 0) | |
3246 | return false; | |
3247 | } | |
3248 | } | |
3249 | ||
3250 | /* Undo nop_submit_request. We prevent all new i915 requests from | |
3251 | * being queued (by disallowing execbuf whilst wedged) so having | |
3252 | * waited for all active requests above, we know the system is idle | |
3253 | * and do not have to worry about a thread being inside | |
3254 | * engine->submit_request() as we swap over. So unlike installing | |
3255 | * the nop_submit_request on reset, we can do this from normal | |
3256 | * context and do not require stop_machine(). | |
3257 | */ | |
3258 | intel_engines_reset_default_submission(i915); | |
36703e79 | 3259 | i915_gem_contexts_lost(i915); |
2e8f9d32 CW |
3260 | |
3261 | smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ | |
3262 | clear_bit(I915_WEDGED, &i915->gpu_error.flags); | |
3263 | ||
3264 | return true; | |
3265 | } | |
3266 | ||
75ef9da2 | 3267 | static void |
673a394b EA |
3268 | i915_gem_retire_work_handler(struct work_struct *work) |
3269 | { | |
b29c19b6 | 3270 | struct drm_i915_private *dev_priv = |
67d97da3 | 3271 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
91c8a326 | 3272 | struct drm_device *dev = &dev_priv->drm; |
673a394b | 3273 | |
891b48cf | 3274 | /* Come back later if the device is busy... */ |
b29c19b6 | 3275 | if (mutex_trylock(&dev->struct_mutex)) { |
67d97da3 | 3276 | i915_gem_retire_requests(dev_priv); |
b29c19b6 | 3277 | mutex_unlock(&dev->struct_mutex); |
673a394b | 3278 | } |
67d97da3 CW |
3279 | |
3280 | /* Keep the retire handler running until we are finally idle. | |
3281 | * We do not need to do this test under locking as in the worst-case | |
3282 | * we queue the retire worker once too often. | |
3283 | */ | |
c9615613 CW |
3284 | if (READ_ONCE(dev_priv->gt.awake)) { |
3285 | i915_queue_hangcheck(dev_priv); | |
67d97da3 CW |
3286 | queue_delayed_work(dev_priv->wq, |
3287 | &dev_priv->gt.retire_work, | |
bcb45086 | 3288 | round_jiffies_up_relative(HZ)); |
c9615613 | 3289 | } |
b29c19b6 | 3290 | } |
0a58705b | 3291 | |
b29c19b6 CW |
3292 | static void |
3293 | i915_gem_idle_work_handler(struct work_struct *work) | |
3294 | { | |
3295 | struct drm_i915_private *dev_priv = | |
67d97da3 | 3296 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
91c8a326 | 3297 | struct drm_device *dev = &dev_priv->drm; |
67d97da3 CW |
3298 | bool rearm_hangcheck; |
3299 | ||
3300 | if (!READ_ONCE(dev_priv->gt.awake)) | |
3301 | return; | |
3302 | ||
0cb5670b ID |
3303 | /* |
3304 | * Wait for last execlists context complete, but bail out in case a | |
3305 | * new request is submitted. | |
3306 | */ | |
8490ae20 | 3307 | wait_for(intel_engines_are_idle(dev_priv), 10); |
28176ef4 | 3308 | if (READ_ONCE(dev_priv->gt.active_requests)) |
67d97da3 CW |
3309 | return; |
3310 | ||
3311 | rearm_hangcheck = | |
3312 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); | |
3313 | ||
3314 | if (!mutex_trylock(&dev->struct_mutex)) { | |
3315 | /* Currently busy, come back later */ | |
3316 | mod_delayed_work(dev_priv->wq, | |
3317 | &dev_priv->gt.idle_work, | |
3318 | msecs_to_jiffies(50)); | |
3319 | goto out_rearm; | |
3320 | } | |
3321 | ||
93c97dc1 ID |
3322 | /* |
3323 | * New request retired after this work handler started, extend active | |
3324 | * period until next instance of the work. | |
3325 | */ | |
3326 | if (work_pending(work)) | |
3327 | goto out_unlock; | |
3328 | ||
28176ef4 | 3329 | if (dev_priv->gt.active_requests) |
67d97da3 | 3330 | goto out_unlock; |
b29c19b6 | 3331 | |
05425249 | 3332 | if (wait_for(intel_engines_are_idle(dev_priv), 10)) |
0cb5670b ID |
3333 | DRM_ERROR("Timeout waiting for engines to idle\n"); |
3334 | ||
6c067579 | 3335 | intel_engines_mark_idle(dev_priv); |
47979480 | 3336 | i915_gem_timelines_mark_idle(dev_priv); |
35c94185 | 3337 | |
67d97da3 CW |
3338 | GEM_BUG_ON(!dev_priv->gt.awake); |
3339 | dev_priv->gt.awake = false; | |
3340 | rearm_hangcheck = false; | |
30ecad77 | 3341 | |
67d97da3 CW |
3342 | if (INTEL_GEN(dev_priv) >= 6) |
3343 | gen6_rps_idle(dev_priv); | |
3344 | intel_runtime_pm_put(dev_priv); | |
3345 | out_unlock: | |
3346 | mutex_unlock(&dev->struct_mutex); | |
b29c19b6 | 3347 | |
67d97da3 CW |
3348 | out_rearm: |
3349 | if (rearm_hangcheck) { | |
3350 | GEM_BUG_ON(!dev_priv->gt.awake); | |
3351 | i915_queue_hangcheck(dev_priv); | |
35c94185 | 3352 | } |
673a394b EA |
3353 | } |
3354 | ||
b1f788c6 CW |
3355 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
3356 | { | |
d1b48c1e | 3357 | struct drm_i915_private *i915 = to_i915(gem->dev); |
b1f788c6 CW |
3358 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
3359 | struct drm_i915_file_private *fpriv = file->driver_priv; | |
d1b48c1e | 3360 | struct i915_lut_handle *lut, *ln; |
b1f788c6 | 3361 | |
d1b48c1e CW |
3362 | mutex_lock(&i915->drm.struct_mutex); |
3363 | ||
3364 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { | |
3365 | struct i915_gem_context *ctx = lut->ctx; | |
3366 | struct i915_vma *vma; | |
3367 | ||
432295d7 | 3368 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
d1b48c1e CW |
3369 | if (ctx->file_priv != fpriv) |
3370 | continue; | |
3371 | ||
3372 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); | |
fa3722f6 CW |
3373 | GEM_BUG_ON(vma->obj != obj); |
3374 | ||
3375 | /* We allow the process to have multiple handles to the same | |
3376 | * vma, in the same fd namespace, by virtue of flink/open. | |
3377 | */ | |
3378 | GEM_BUG_ON(!vma->open_count); | |
3379 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) | |
b1f788c6 | 3380 | i915_vma_close(vma); |
f8a7fde4 | 3381 | |
d1b48c1e CW |
3382 | list_del(&lut->obj_link); |
3383 | list_del(&lut->ctx_link); | |
4ff4b44c | 3384 | |
d1b48c1e CW |
3385 | kmem_cache_free(i915->luts, lut); |
3386 | __i915_gem_object_release_unless_active(obj); | |
f8a7fde4 | 3387 | } |
d1b48c1e CW |
3388 | |
3389 | mutex_unlock(&i915->drm.struct_mutex); | |
b1f788c6 CW |
3390 | } |
3391 | ||
e95433c7 CW |
3392 | static unsigned long to_wait_timeout(s64 timeout_ns) |
3393 | { | |
3394 | if (timeout_ns < 0) | |
3395 | return MAX_SCHEDULE_TIMEOUT; | |
3396 | ||
3397 | if (timeout_ns == 0) | |
3398 | return 0; | |
3399 | ||
3400 | return nsecs_to_jiffies_timeout(timeout_ns); | |
3401 | } | |
3402 | ||
23ba4fd0 BW |
3403 | /** |
3404 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT | |
14bb2c11 TU |
3405 | * @dev: drm device pointer |
3406 | * @data: ioctl data blob | |
3407 | * @file: drm file pointer | |
23ba4fd0 BW |
3408 | * |
3409 | * Returns 0 if successful, else an error is returned with the remaining time in | |
3410 | * the timeout parameter. | |
3411 | * -ETIME: object is still busy after timeout | |
3412 | * -ERESTARTSYS: signal interrupted the wait | |
3413 | * -ENONENT: object doesn't exist | |
3414 | * Also possible, but rare: | |
b8050148 | 3415 | * -EAGAIN: incomplete, restart syscall |
23ba4fd0 BW |
3416 | * -ENOMEM: damn |
3417 | * -ENODEV: Internal IRQ fail | |
3418 | * -E?: The add request failed | |
3419 | * | |
3420 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any | |
3421 | * non-zero timeout parameter the wait ioctl will wait for the given number of | |
3422 | * nanoseconds on an object becoming unbusy. Since the wait itself does so | |
3423 | * without holding struct_mutex the object may become re-busied before this | |
3424 | * function completes. A similar but shorter * race condition exists in the busy | |
3425 | * ioctl | |
3426 | */ | |
3427 | int | |
3428 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) | |
3429 | { | |
3430 | struct drm_i915_gem_wait *args = data; | |
3431 | struct drm_i915_gem_object *obj; | |
e95433c7 CW |
3432 | ktime_t start; |
3433 | long ret; | |
23ba4fd0 | 3434 | |
11b5d511 DV |
3435 | if (args->flags != 0) |
3436 | return -EINVAL; | |
3437 | ||
03ac0642 | 3438 | obj = i915_gem_object_lookup(file, args->bo_handle); |
033d549b | 3439 | if (!obj) |
23ba4fd0 | 3440 | return -ENOENT; |
23ba4fd0 | 3441 | |
e95433c7 CW |
3442 | start = ktime_get(); |
3443 | ||
3444 | ret = i915_gem_object_wait(obj, | |
3445 | I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL, | |
3446 | to_wait_timeout(args->timeout_ns), | |
3447 | to_rps_client(file)); | |
3448 | ||
3449 | if (args->timeout_ns > 0) { | |
3450 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); | |
3451 | if (args->timeout_ns < 0) | |
3452 | args->timeout_ns = 0; | |
c1d2061b CW |
3453 | |
3454 | /* | |
3455 | * Apparently ktime isn't accurate enough and occasionally has a | |
3456 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch | |
3457 | * things up to make the test happy. We allow up to 1 jiffy. | |
3458 | * | |
3459 | * This is a regression from the timespec->ktime conversion. | |
3460 | */ | |
3461 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) | |
3462 | args->timeout_ns = 0; | |
b8050148 CW |
3463 | |
3464 | /* Asked to wait beyond the jiffie/scheduler precision? */ | |
3465 | if (ret == -ETIME && args->timeout_ns) | |
3466 | ret = -EAGAIN; | |
b4716185 CW |
3467 | } |
3468 | ||
f0cd5182 | 3469 | i915_gem_object_put(obj); |
ff865885 | 3470 | return ret; |
23ba4fd0 BW |
3471 | } |
3472 | ||
73cb9701 | 3473 | static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags) |
4df2faf4 | 3474 | { |
73cb9701 | 3475 | int ret, i; |
4df2faf4 | 3476 | |
73cb9701 CW |
3477 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
3478 | ret = i915_gem_active_wait(&tl->engine[i].last_request, flags); | |
3479 | if (ret) | |
3480 | return ret; | |
3481 | } | |
62e63007 | 3482 | |
73cb9701 CW |
3483 | return 0; |
3484 | } | |
3485 | ||
25112b64 CW |
3486 | static int wait_for_engines(struct drm_i915_private *i915) |
3487 | { | |
cad9946c CW |
3488 | if (wait_for(intel_engines_are_idle(i915), 50)) { |
3489 | DRM_ERROR("Failed to idle engines, declaring wedged!\n"); | |
3490 | i915_gem_set_wedged(i915); | |
3491 | return -EIO; | |
25112b64 CW |
3492 | } |
3493 | ||
3494 | return 0; | |
3495 | } | |
3496 | ||
73cb9701 CW |
3497 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags) |
3498 | { | |
73cb9701 CW |
3499 | int ret; |
3500 | ||
863e9fde CW |
3501 | /* If the device is asleep, we have no requests outstanding */ |
3502 | if (!READ_ONCE(i915->gt.awake)) | |
3503 | return 0; | |
3504 | ||
9caa34aa CW |
3505 | if (flags & I915_WAIT_LOCKED) { |
3506 | struct i915_gem_timeline *tl; | |
3507 | ||
3508 | lockdep_assert_held(&i915->drm.struct_mutex); | |
3509 | ||
3510 | list_for_each_entry(tl, &i915->gt.timelines, link) { | |
3511 | ret = wait_for_timeline(tl, flags); | |
3512 | if (ret) | |
3513 | return ret; | |
3514 | } | |
72022a70 CW |
3515 | |
3516 | i915_gem_retire_requests(i915); | |
3517 | GEM_BUG_ON(i915->gt.active_requests); | |
25112b64 CW |
3518 | |
3519 | ret = wait_for_engines(i915); | |
9caa34aa CW |
3520 | } else { |
3521 | ret = wait_for_timeline(&i915->gt.global_timeline, flags); | |
1ec14ad3 | 3522 | } |
4df2faf4 | 3523 | |
25112b64 | 3524 | return ret; |
4df2faf4 DV |
3525 | } |
3526 | ||
5a97bcc6 CW |
3527 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
3528 | { | |
e27ab73d CW |
3529 | /* |
3530 | * We manually flush the CPU domain so that we can override and | |
3531 | * force the flush for the display, and perform it asyncrhonously. | |
3532 | */ | |
3533 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); | |
3534 | if (obj->cache_dirty) | |
3535 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); | |
5a97bcc6 CW |
3536 | obj->base.write_domain = 0; |
3537 | } | |
3538 | ||
3539 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) | |
3540 | { | |
bd3d2252 | 3541 | if (!READ_ONCE(obj->pin_global)) |
5a97bcc6 CW |
3542 | return; |
3543 | ||
3544 | mutex_lock(&obj->base.dev->struct_mutex); | |
3545 | __i915_gem_object_flush_for_display(obj); | |
3546 | mutex_unlock(&obj->base.dev->struct_mutex); | |
3547 | } | |
3548 | ||
e22d8e3c CW |
3549 | /** |
3550 | * Moves a single object to the WC read, and possibly write domain. | |
3551 | * @obj: object to act on | |
3552 | * @write: ask for write access or read only | |
3553 | * | |
3554 | * This function returns when the move is complete, including waiting on | |
3555 | * flushes to occur. | |
3556 | */ | |
3557 | int | |
3558 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) | |
3559 | { | |
3560 | int ret; | |
3561 | ||
3562 | lockdep_assert_held(&obj->base.dev->struct_mutex); | |
3563 | ||
3564 | ret = i915_gem_object_wait(obj, | |
3565 | I915_WAIT_INTERRUPTIBLE | | |
3566 | I915_WAIT_LOCKED | | |
3567 | (write ? I915_WAIT_ALL : 0), | |
3568 | MAX_SCHEDULE_TIMEOUT, | |
3569 | NULL); | |
3570 | if (ret) | |
3571 | return ret; | |
3572 | ||
3573 | if (obj->base.write_domain == I915_GEM_DOMAIN_WC) | |
3574 | return 0; | |
3575 | ||
3576 | /* Flush and acquire obj->pages so that we are coherent through | |
3577 | * direct access in memory with previous cached writes through | |
3578 | * shmemfs and that our cache domain tracking remains valid. | |
3579 | * For example, if the obj->filp was moved to swap without us | |
3580 | * being notified and releasing the pages, we would mistakenly | |
3581 | * continue to assume that the obj remained out of the CPU cached | |
3582 | * domain. | |
3583 | */ | |
3584 | ret = i915_gem_object_pin_pages(obj); | |
3585 | if (ret) | |
3586 | return ret; | |
3587 | ||
3588 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); | |
3589 | ||
3590 | /* Serialise direct access to this object with the barriers for | |
3591 | * coherent writes from the GPU, by effectively invalidating the | |
3592 | * WC domain upon first access. | |
3593 | */ | |
3594 | if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0) | |
3595 | mb(); | |
3596 | ||
3597 | /* It should now be out of any other write domains, and we can update | |
3598 | * the domain values for our changes. | |
3599 | */ | |
3600 | GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0); | |
3601 | obj->base.read_domains |= I915_GEM_DOMAIN_WC; | |
3602 | if (write) { | |
3603 | obj->base.read_domains = I915_GEM_DOMAIN_WC; | |
3604 | obj->base.write_domain = I915_GEM_DOMAIN_WC; | |
3605 | obj->mm.dirty = true; | |
3606 | } | |
3607 | ||
3608 | i915_gem_object_unpin_pages(obj); | |
3609 | return 0; | |
3610 | } | |
3611 | ||
2ef7eeaa EA |
3612 | /** |
3613 | * Moves a single object to the GTT read, and possibly write domain. | |
14bb2c11 TU |
3614 | * @obj: object to act on |
3615 | * @write: ask for write access or read only | |
2ef7eeaa EA |
3616 | * |
3617 | * This function returns when the move is complete, including waiting on | |
3618 | * flushes to occur. | |
3619 | */ | |
79e53945 | 3620 | int |
2021746e | 3621 | i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) |
2ef7eeaa | 3622 | { |
e47c68e9 | 3623 | int ret; |
2ef7eeaa | 3624 | |
e95433c7 | 3625 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
4c7d62c6 | 3626 | |
e95433c7 CW |
3627 | ret = i915_gem_object_wait(obj, |
3628 | I915_WAIT_INTERRUPTIBLE | | |
3629 | I915_WAIT_LOCKED | | |
3630 | (write ? I915_WAIT_ALL : 0), | |
3631 | MAX_SCHEDULE_TIMEOUT, | |
3632 | NULL); | |
88241785 CW |
3633 | if (ret) |
3634 | return ret; | |
3635 | ||
c13d87ea CW |
3636 | if (obj->base.write_domain == I915_GEM_DOMAIN_GTT) |
3637 | return 0; | |
3638 | ||
43566ded CW |
3639 | /* Flush and acquire obj->pages so that we are coherent through |
3640 | * direct access in memory with previous cached writes through | |
3641 | * shmemfs and that our cache domain tracking remains valid. | |
3642 | * For example, if the obj->filp was moved to swap without us | |
3643 | * being notified and releasing the pages, we would mistakenly | |
3644 | * continue to assume that the obj remained out of the CPU cached | |
3645 | * domain. | |
3646 | */ | |
a4f5ea64 | 3647 | ret = i915_gem_object_pin_pages(obj); |
43566ded CW |
3648 | if (ret) |
3649 | return ret; | |
3650 | ||
ef74921b | 3651 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
1c5d22f7 | 3652 | |
d0a57789 CW |
3653 | /* Serialise direct access to this object with the barriers for |
3654 | * coherent writes from the GPU, by effectively invalidating the | |
3655 | * GTT domain upon first access. | |
3656 | */ | |
3657 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) | |
3658 | mb(); | |
3659 | ||
e47c68e9 EA |
3660 | /* It should now be out of any other write domains, and we can update |
3661 | * the domain values for our changes. | |
3662 | */ | |
40e62d5d | 3663 | GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
05394f39 | 3664 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
e47c68e9 | 3665 | if (write) { |
05394f39 CW |
3666 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
3667 | obj->base.write_domain = I915_GEM_DOMAIN_GTT; | |
a4f5ea64 | 3668 | obj->mm.dirty = true; |
2ef7eeaa EA |
3669 | } |
3670 | ||
a4f5ea64 | 3671 | i915_gem_object_unpin_pages(obj); |
e47c68e9 EA |
3672 | return 0; |
3673 | } | |
3674 | ||
ef55f92a CW |
3675 | /** |
3676 | * Changes the cache-level of an object across all VMA. | |
14bb2c11 TU |
3677 | * @obj: object to act on |
3678 | * @cache_level: new cache level to set for the object | |
ef55f92a CW |
3679 | * |
3680 | * After this function returns, the object will be in the new cache-level | |
3681 | * across all GTT and the contents of the backing storage will be coherent, | |
3682 | * with respect to the new cache-level. In order to keep the backing storage | |
3683 | * coherent for all users, we only allow a single cache level to be set | |
3684 | * globally on the object and prevent it from being changed whilst the | |
3685 | * hardware is reading from the object. That is if the object is currently | |
3686 | * on the scanout it will be set to uncached (or equivalent display | |
3687 | * cache coherency) and all non-MOCS GPU access will also be uncached so | |
3688 | * that all direct access to the scanout remains coherent. | |
3689 | */ | |
e4ffd173 CW |
3690 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
3691 | enum i915_cache_level cache_level) | |
3692 | { | |
aa653a68 | 3693 | struct i915_vma *vma; |
a6a7cc4b | 3694 | int ret; |
e4ffd173 | 3695 | |
4c7d62c6 CW |
3696 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
3697 | ||
e4ffd173 | 3698 | if (obj->cache_level == cache_level) |
a6a7cc4b | 3699 | return 0; |
e4ffd173 | 3700 | |
ef55f92a CW |
3701 | /* Inspect the list of currently bound VMA and unbind any that would |
3702 | * be invalid given the new cache-level. This is principally to | |
3703 | * catch the issue of the CS prefetch crossing page boundaries and | |
3704 | * reading an invalid PTE on older architectures. | |
3705 | */ | |
aa653a68 CW |
3706 | restart: |
3707 | list_for_each_entry(vma, &obj->vma_list, obj_link) { | |
ef55f92a CW |
3708 | if (!drm_mm_node_allocated(&vma->node)) |
3709 | continue; | |
3710 | ||
20dfbde4 | 3711 | if (i915_vma_is_pinned(vma)) { |
ef55f92a CW |
3712 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
3713 | return -EBUSY; | |
3714 | } | |
3715 | ||
da8f1274 CW |
3716 | if (!i915_vma_is_closed(vma) && |
3717 | i915_gem_valid_gtt_space(vma, cache_level)) | |
aa653a68 CW |
3718 | continue; |
3719 | ||
3720 | ret = i915_vma_unbind(vma); | |
3721 | if (ret) | |
3722 | return ret; | |
3723 | ||
3724 | /* As unbinding may affect other elements in the | |
3725 | * obj->vma_list (due to side-effects from retiring | |
3726 | * an active vma), play safe and restart the iterator. | |
3727 | */ | |
3728 | goto restart; | |
42d6ab48 CW |
3729 | } |
3730 | ||
ef55f92a CW |
3731 | /* We can reuse the existing drm_mm nodes but need to change the |
3732 | * cache-level on the PTE. We could simply unbind them all and | |
3733 | * rebind with the correct cache-level on next use. However since | |
3734 | * we already have a valid slot, dma mapping, pages etc, we may as | |
3735 | * rewrite the PTE in the belief that doing so tramples upon less | |
3736 | * state and so involves less work. | |
3737 | */ | |
15717de2 | 3738 | if (obj->bind_count) { |
ef55f92a CW |
3739 | /* Before we change the PTE, the GPU must not be accessing it. |
3740 | * If we wait upon the object, we know that all the bound | |
3741 | * VMA are no longer active. | |
3742 | */ | |
e95433c7 CW |
3743 | ret = i915_gem_object_wait(obj, |
3744 | I915_WAIT_INTERRUPTIBLE | | |
3745 | I915_WAIT_LOCKED | | |
3746 | I915_WAIT_ALL, | |
3747 | MAX_SCHEDULE_TIMEOUT, | |
3748 | NULL); | |
e4ffd173 CW |
3749 | if (ret) |
3750 | return ret; | |
3751 | ||
0031fb96 TU |
3752 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
3753 | cache_level != I915_CACHE_NONE) { | |
ef55f92a CW |
3754 | /* Access to snoopable pages through the GTT is |
3755 | * incoherent and on some machines causes a hard | |
3756 | * lockup. Relinquish the CPU mmaping to force | |
3757 | * userspace to refault in the pages and we can | |
3758 | * then double check if the GTT mapping is still | |
3759 | * valid for that pointer access. | |
3760 | */ | |
3761 | i915_gem_release_mmap(obj); | |
3762 | ||
3763 | /* As we no longer need a fence for GTT access, | |
3764 | * we can relinquish it now (and so prevent having | |
3765 | * to steal a fence from someone else on the next | |
3766 | * fence request). Note GPU activity would have | |
3767 | * dropped the fence as all snoopable access is | |
3768 | * supposed to be linear. | |
3769 | */ | |
49ef5294 CW |
3770 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
3771 | ret = i915_vma_put_fence(vma); | |
3772 | if (ret) | |
3773 | return ret; | |
3774 | } | |
ef55f92a CW |
3775 | } else { |
3776 | /* We either have incoherent backing store and | |
3777 | * so no GTT access or the architecture is fully | |
3778 | * coherent. In such cases, existing GTT mmaps | |
3779 | * ignore the cache bit in the PTE and we can | |
3780 | * rewrite it without confusing the GPU or having | |
3781 | * to force userspace to fault back in its mmaps. | |
3782 | */ | |
e4ffd173 CW |
3783 | } |
3784 | ||
1c7f4bca | 3785 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
ef55f92a CW |
3786 | if (!drm_mm_node_allocated(&vma->node)) |
3787 | continue; | |
3788 | ||
3789 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); | |
3790 | if (ret) | |
3791 | return ret; | |
3792 | } | |
e4ffd173 CW |
3793 | } |
3794 | ||
1c7f4bca | 3795 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
2c22569b | 3796 | vma->node.color = cache_level; |
b8f55be6 | 3797 | i915_gem_object_set_cache_coherency(obj, cache_level); |
e27ab73d | 3798 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
2c22569b | 3799 | |
e4ffd173 CW |
3800 | return 0; |
3801 | } | |
3802 | ||
199adf40 BW |
3803 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
3804 | struct drm_file *file) | |
e6994aee | 3805 | { |
199adf40 | 3806 | struct drm_i915_gem_caching *args = data; |
e6994aee | 3807 | struct drm_i915_gem_object *obj; |
fbbd37b3 | 3808 | int err = 0; |
e6994aee | 3809 | |
fbbd37b3 CW |
3810 | rcu_read_lock(); |
3811 | obj = i915_gem_object_lookup_rcu(file, args->handle); | |
3812 | if (!obj) { | |
3813 | err = -ENOENT; | |
3814 | goto out; | |
3815 | } | |
e6994aee | 3816 | |
651d794f CW |
3817 | switch (obj->cache_level) { |
3818 | case I915_CACHE_LLC: | |
3819 | case I915_CACHE_L3_LLC: | |
3820 | args->caching = I915_CACHING_CACHED; | |
3821 | break; | |
3822 | ||
4257d3ba CW |
3823 | case I915_CACHE_WT: |
3824 | args->caching = I915_CACHING_DISPLAY; | |
3825 | break; | |
3826 | ||
651d794f CW |
3827 | default: |
3828 | args->caching = I915_CACHING_NONE; | |
3829 | break; | |
3830 | } | |
fbbd37b3 CW |
3831 | out: |
3832 | rcu_read_unlock(); | |
3833 | return err; | |
e6994aee CW |
3834 | } |
3835 | ||
199adf40 BW |
3836 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
3837 | struct drm_file *file) | |
e6994aee | 3838 | { |
9c870d03 | 3839 | struct drm_i915_private *i915 = to_i915(dev); |
199adf40 | 3840 | struct drm_i915_gem_caching *args = data; |
e6994aee CW |
3841 | struct drm_i915_gem_object *obj; |
3842 | enum i915_cache_level level; | |
d65415df | 3843 | int ret = 0; |
e6994aee | 3844 | |
199adf40 BW |
3845 | switch (args->caching) { |
3846 | case I915_CACHING_NONE: | |
e6994aee CW |
3847 | level = I915_CACHE_NONE; |
3848 | break; | |
199adf40 | 3849 | case I915_CACHING_CACHED: |
e5756c10 ID |
3850 | /* |
3851 | * Due to a HW issue on BXT A stepping, GPU stores via a | |
3852 | * snooped mapping may leave stale data in a corresponding CPU | |
3853 | * cacheline, whereas normally such cachelines would get | |
3854 | * invalidated. | |
3855 | */ | |
9c870d03 | 3856 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
e5756c10 ID |
3857 | return -ENODEV; |
3858 | ||
e6994aee CW |
3859 | level = I915_CACHE_LLC; |
3860 | break; | |
4257d3ba | 3861 | case I915_CACHING_DISPLAY: |
9c870d03 | 3862 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
4257d3ba | 3863 | break; |
e6994aee CW |
3864 | default: |
3865 | return -EINVAL; | |
3866 | } | |
3867 | ||
d65415df CW |
3868 | obj = i915_gem_object_lookup(file, args->handle); |
3869 | if (!obj) | |
3870 | return -ENOENT; | |
3871 | ||
3872 | if (obj->cache_level == level) | |
3873 | goto out; | |
3874 | ||
3875 | ret = i915_gem_object_wait(obj, | |
3876 | I915_WAIT_INTERRUPTIBLE, | |
3877 | MAX_SCHEDULE_TIMEOUT, | |
3878 | to_rps_client(file)); | |
3bc2913e | 3879 | if (ret) |
d65415df | 3880 | goto out; |
3bc2913e | 3881 | |
d65415df CW |
3882 | ret = i915_mutex_lock_interruptible(dev); |
3883 | if (ret) | |
3884 | goto out; | |
e6994aee CW |
3885 | |
3886 | ret = i915_gem_object_set_cache_level(obj, level); | |
e6994aee | 3887 | mutex_unlock(&dev->struct_mutex); |
d65415df CW |
3888 | |
3889 | out: | |
3890 | i915_gem_object_put(obj); | |
e6994aee CW |
3891 | return ret; |
3892 | } | |
3893 | ||
b9241ea3 | 3894 | /* |
2da3b9b9 CW |
3895 | * Prepare buffer for display plane (scanout, cursors, etc). |
3896 | * Can be called from an uninterruptible phase (modesetting) and allows | |
3897 | * any flushes to be pipelined (for pageflips). | |
b9241ea3 | 3898 | */ |
058d88c4 | 3899 | struct i915_vma * |
2da3b9b9 CW |
3900 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
3901 | u32 alignment, | |
e6617330 | 3902 | const struct i915_ggtt_view *view) |
b9241ea3 | 3903 | { |
058d88c4 | 3904 | struct i915_vma *vma; |
b9241ea3 ZW |
3905 | int ret; |
3906 | ||
4c7d62c6 CW |
3907 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
3908 | ||
bd3d2252 | 3909 | /* Mark the global pin early so that we account for the |
cc98b413 CW |
3910 | * display coherency whilst setting up the cache domains. |
3911 | */ | |
bd3d2252 | 3912 | obj->pin_global++; |
cc98b413 | 3913 | |
a7ef0640 EA |
3914 | /* The display engine is not coherent with the LLC cache on gen6. As |
3915 | * a result, we make sure that the pinning that is about to occur is | |
3916 | * done with uncached PTEs. This is lowest common denominator for all | |
3917 | * chipsets. | |
3918 | * | |
3919 | * However for gen6+, we could do better by using the GFDT bit instead | |
3920 | * of uncaching, which would allow us to flush all the LLC-cached data | |
3921 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. | |
3922 | */ | |
651d794f | 3923 | ret = i915_gem_object_set_cache_level(obj, |
8652744b TU |
3924 | HAS_WT(to_i915(obj->base.dev)) ? |
3925 | I915_CACHE_WT : I915_CACHE_NONE); | |
058d88c4 CW |
3926 | if (ret) { |
3927 | vma = ERR_PTR(ret); | |
bd3d2252 | 3928 | goto err_unpin_global; |
058d88c4 | 3929 | } |
a7ef0640 | 3930 | |
2da3b9b9 CW |
3931 | /* As the user may map the buffer once pinned in the display plane |
3932 | * (e.g. libkms for the bootup splash), we have to ensure that we | |
2efb813d CW |
3933 | * always use map_and_fenceable for all scanout buffers. However, |
3934 | * it may simply be too big to fit into mappable, in which case | |
3935 | * put it anyway and hope that userspace can cope (but always first | |
3936 | * try to preserve the existing ABI). | |
2da3b9b9 | 3937 | */ |
2efb813d | 3938 | vma = ERR_PTR(-ENOSPC); |
47a8e3f6 | 3939 | if (!view || view->type == I915_GGTT_VIEW_NORMAL) |
2efb813d CW |
3940 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
3941 | PIN_MAPPABLE | PIN_NONBLOCK); | |
767a222e CW |
3942 | if (IS_ERR(vma)) { |
3943 | struct drm_i915_private *i915 = to_i915(obj->base.dev); | |
3944 | unsigned int flags; | |
3945 | ||
3946 | /* Valleyview is definitely limited to scanning out the first | |
3947 | * 512MiB. Lets presume this behaviour was inherited from the | |
3948 | * g4x display engine and that all earlier gen are similarly | |
3949 | * limited. Testing suggests that it is a little more | |
3950 | * complicated than this. For example, Cherryview appears quite | |
3951 | * happy to scanout from anywhere within its global aperture. | |
3952 | */ | |
3953 | flags = 0; | |
3954 | if (HAS_GMCH_DISPLAY(i915)) | |
3955 | flags = PIN_MAPPABLE; | |
3956 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); | |
3957 | } | |
058d88c4 | 3958 | if (IS_ERR(vma)) |
bd3d2252 | 3959 | goto err_unpin_global; |
2da3b9b9 | 3960 | |
d8923dcf CW |
3961 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
3962 | ||
a6a7cc4b | 3963 | /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */ |
5a97bcc6 | 3964 | __i915_gem_object_flush_for_display(obj); |
d59b21ec | 3965 | intel_fb_obj_flush(obj, ORIGIN_DIRTYFB); |
b118c1e3 | 3966 | |
2da3b9b9 CW |
3967 | /* It should now be out of any other write domains, and we can update |
3968 | * the domain values for our changes. | |
3969 | */ | |
05394f39 | 3970 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
b9241ea3 | 3971 | |
058d88c4 | 3972 | return vma; |
cc98b413 | 3973 | |
bd3d2252 CW |
3974 | err_unpin_global: |
3975 | obj->pin_global--; | |
058d88c4 | 3976 | return vma; |
cc98b413 CW |
3977 | } |
3978 | ||
3979 | void | |
058d88c4 | 3980 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
cc98b413 | 3981 | { |
49d73912 | 3982 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
4c7d62c6 | 3983 | |
bd3d2252 | 3984 | if (WARN_ON(vma->obj->pin_global == 0)) |
8a0c39b1 TU |
3985 | return; |
3986 | ||
bd3d2252 | 3987 | if (--vma->obj->pin_global == 0) |
f51455d4 | 3988 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
e6617330 | 3989 | |
383d5823 | 3990 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
befedbb7 | 3991 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
383d5823 | 3992 | |
058d88c4 | 3993 | i915_vma_unpin(vma); |
b9241ea3 ZW |
3994 | } |
3995 | ||
e47c68e9 EA |
3996 | /** |
3997 | * Moves a single object to the CPU read, and possibly write domain. | |
14bb2c11 TU |
3998 | * @obj: object to act on |
3999 | * @write: requesting write or read-only access | |
e47c68e9 EA |
4000 | * |
4001 | * This function returns when the move is complete, including waiting on | |
4002 | * flushes to occur. | |
4003 | */ | |
dabdfe02 | 4004 | int |
919926ae | 4005 | i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) |
e47c68e9 | 4006 | { |
e47c68e9 EA |
4007 | int ret; |
4008 | ||
e95433c7 | 4009 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
4c7d62c6 | 4010 | |
e95433c7 CW |
4011 | ret = i915_gem_object_wait(obj, |
4012 | I915_WAIT_INTERRUPTIBLE | | |
4013 | I915_WAIT_LOCKED | | |
4014 | (write ? I915_WAIT_ALL : 0), | |
4015 | MAX_SCHEDULE_TIMEOUT, | |
4016 | NULL); | |
88241785 CW |
4017 | if (ret) |
4018 | return ret; | |
4019 | ||
ef74921b | 4020 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
2ef7eeaa | 4021 | |
e47c68e9 | 4022 | /* Flush the CPU cache if it's still invalid. */ |
05394f39 | 4023 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
57822dc6 | 4024 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
05394f39 | 4025 | obj->base.read_domains |= I915_GEM_DOMAIN_CPU; |
2ef7eeaa EA |
4026 | } |
4027 | ||
4028 | /* It should now be out of any other write domains, and we can update | |
4029 | * the domain values for our changes. | |
4030 | */ | |
e27ab73d | 4031 | GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU); |
e47c68e9 EA |
4032 | |
4033 | /* If we're writing through the CPU, then the GPU read domains will | |
4034 | * need to be invalidated at next use. | |
4035 | */ | |
e27ab73d CW |
4036 | if (write) |
4037 | __start_cpu_write(obj); | |
2ef7eeaa EA |
4038 | |
4039 | return 0; | |
4040 | } | |
4041 | ||
673a394b EA |
4042 | /* Throttle our rendering by waiting until the ring has completed our requests |
4043 | * emitted over 20 msec ago. | |
4044 | * | |
b962442e EA |
4045 | * Note that if we were to use the current jiffies each time around the loop, |
4046 | * we wouldn't escape the function with any frames outstanding if the time to | |
4047 | * render a frame was over 20ms. | |
4048 | * | |
673a394b EA |
4049 | * This should get us reasonable parallelism between CPU and GPU but also |
4050 | * relatively low latency when blocking on a particular request to finish. | |
4051 | */ | |
40a5f0de | 4052 | static int |
f787a5f5 | 4053 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
40a5f0de | 4054 | { |
fac5e23e | 4055 | struct drm_i915_private *dev_priv = to_i915(dev); |
f787a5f5 | 4056 | struct drm_i915_file_private *file_priv = file->driver_priv; |
d0bc54f2 | 4057 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
54fb2411 | 4058 | struct drm_i915_gem_request *request, *target = NULL; |
e95433c7 | 4059 | long ret; |
93533c29 | 4060 | |
f4457ae7 CW |
4061 | /* ABI: return -EIO if already wedged */ |
4062 | if (i915_terminally_wedged(&dev_priv->gpu_error)) | |
4063 | return -EIO; | |
e110e8d6 | 4064 | |
1c25595f | 4065 | spin_lock(&file_priv->mm.lock); |
c8659efa | 4066 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
b962442e EA |
4067 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
4068 | break; | |
40a5f0de | 4069 | |
c8659efa CW |
4070 | if (target) { |
4071 | list_del(&target->client_link); | |
4072 | target->file_priv = NULL; | |
4073 | } | |
fcfa423c | 4074 | |
54fb2411 | 4075 | target = request; |
b962442e | 4076 | } |
ff865885 | 4077 | if (target) |
e8a261ea | 4078 | i915_gem_request_get(target); |
1c25595f | 4079 | spin_unlock(&file_priv->mm.lock); |
40a5f0de | 4080 | |
54fb2411 | 4081 | if (target == NULL) |
f787a5f5 | 4082 | return 0; |
2bc43b5c | 4083 | |
e95433c7 CW |
4084 | ret = i915_wait_request(target, |
4085 | I915_WAIT_INTERRUPTIBLE, | |
4086 | MAX_SCHEDULE_TIMEOUT); | |
e8a261ea | 4087 | i915_gem_request_put(target); |
ff865885 | 4088 | |
e95433c7 | 4089 | return ret < 0 ? ret : 0; |
40a5f0de EA |
4090 | } |
4091 | ||
058d88c4 | 4092 | struct i915_vma * |
ec7adb6e JL |
4093 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
4094 | const struct i915_ggtt_view *view, | |
91b2db6f | 4095 | u64 size, |
2ffffd0f CW |
4096 | u64 alignment, |
4097 | u64 flags) | |
ec7adb6e | 4098 | { |
ad16d2ed CW |
4099 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
4100 | struct i915_address_space *vm = &dev_priv->ggtt.base; | |
1b06dff9 JB |
4101 | |
4102 | return i915_gem_object_pin(obj, vm, view, size, alignment, | |
4103 | flags | PIN_GLOBAL); | |
4104 | } | |
4105 | ||
4106 | struct i915_vma * | |
4107 | i915_gem_object_pin(struct drm_i915_gem_object *obj, | |
4108 | struct i915_address_space *vm, | |
4109 | const struct i915_ggtt_view *view, | |
4110 | u64 size, | |
4111 | u64 alignment, | |
4112 | u64 flags) | |
4113 | { | |
4114 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); | |
59bfa124 CW |
4115 | struct i915_vma *vma; |
4116 | int ret; | |
72e96d64 | 4117 | |
4c7d62c6 CW |
4118 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
4119 | ||
43ae70d9 CW |
4120 | if (!view && flags & PIN_MAPPABLE) { |
4121 | /* If the required space is larger than the available | |
4122 | * aperture, we will not able to find a slot for the | |
4123 | * object and unbinding the object now will be in | |
4124 | * vain. Worse, doing so may cause us to ping-pong | |
4125 | * the object in and out of the Global GTT and | |
4126 | * waste a lot of cycles under the mutex. | |
4127 | */ | |
4128 | if (obj->base.size > dev_priv->ggtt.mappable_end) | |
4129 | return ERR_PTR(-E2BIG); | |
4130 | ||
4131 | /* If NONBLOCK is set the caller is optimistically | |
4132 | * trying to cache the full object within the mappable | |
4133 | * aperture, and *must* have a fallback in place for | |
4134 | * situations where we cannot bind the object. We | |
4135 | * can be a little more lax here and use the fallback | |
4136 | * more often to avoid costly migrations of ourselves | |
4137 | * and other objects within the aperture. | |
4138 | * | |
4139 | * Half-the-aperture is used as a simple heuristic. | |
4140 | * More interesting would to do search for a free | |
4141 | * block prior to making the commitment to unbind. | |
4142 | * That caters for the self-harm case, and with a | |
4143 | * little more heuristics (e.g. NOFAULT, NOEVICT) | |
4144 | * we could try to minimise harm to others. | |
4145 | */ | |
4146 | if (flags & PIN_NONBLOCK && | |
4147 | obj->base.size > dev_priv->ggtt.mappable_end / 2) | |
4148 | return ERR_PTR(-ENOSPC); | |
4149 | } | |
4150 | ||
718659a6 | 4151 | vma = i915_vma_instance(obj, vm, view); |
e0216b76 | 4152 | if (unlikely(IS_ERR(vma))) |
058d88c4 | 4153 | return vma; |
59bfa124 CW |
4154 | |
4155 | if (i915_vma_misplaced(vma, size, alignment, flags)) { | |
43ae70d9 CW |
4156 | if (flags & PIN_NONBLOCK) { |
4157 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) | |
4158 | return ERR_PTR(-ENOSPC); | |
59bfa124 | 4159 | |
43ae70d9 | 4160 | if (flags & PIN_MAPPABLE && |
944397f0 | 4161 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
ad16d2ed CW |
4162 | return ERR_PTR(-ENOSPC); |
4163 | } | |
4164 | ||
59bfa124 CW |
4165 | WARN(i915_vma_is_pinned(vma), |
4166 | "bo is already pinned in ggtt with incorrect alignment:" | |
05a20d09 CW |
4167 | " offset=%08x, req.alignment=%llx," |
4168 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", | |
4169 | i915_ggtt_offset(vma), alignment, | |
59bfa124 | 4170 | !!(flags & PIN_MAPPABLE), |
05a20d09 | 4171 | i915_vma_is_map_and_fenceable(vma)); |
59bfa124 CW |
4172 | ret = i915_vma_unbind(vma); |
4173 | if (ret) | |
058d88c4 | 4174 | return ERR_PTR(ret); |
59bfa124 CW |
4175 | } |
4176 | ||
1b06dff9 | 4177 | ret = i915_vma_pin(vma, size, alignment, flags); |
058d88c4 CW |
4178 | if (ret) |
4179 | return ERR_PTR(ret); | |
ec7adb6e | 4180 | |
058d88c4 | 4181 | return vma; |
673a394b EA |
4182 | } |
4183 | ||
edf6b76f | 4184 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
3fdc13c7 CW |
4185 | { |
4186 | /* Note that we could alias engines in the execbuf API, but | |
4187 | * that would be very unwise as it prevents userspace from | |
4188 | * fine control over engine selection. Ahem. | |
4189 | * | |
4190 | * This should be something like EXEC_MAX_ENGINE instead of | |
4191 | * I915_NUM_ENGINES. | |
4192 | */ | |
4193 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); | |
4194 | return 0x10000 << id; | |
4195 | } | |
4196 | ||
4197 | static __always_inline unsigned int __busy_write_id(unsigned int id) | |
4198 | { | |
70cb472c CW |
4199 | /* The uABI guarantees an active writer is also amongst the read |
4200 | * engines. This would be true if we accessed the activity tracking | |
4201 | * under the lock, but as we perform the lookup of the object and | |
4202 | * its activity locklessly we can not guarantee that the last_write | |
4203 | * being active implies that we have set the same engine flag from | |
4204 | * last_read - hence we always set both read and write busy for | |
4205 | * last_write. | |
4206 | */ | |
4207 | return id | __busy_read_flag(id); | |
3fdc13c7 CW |
4208 | } |
4209 | ||
edf6b76f | 4210 | static __always_inline unsigned int |
d07f0e59 | 4211 | __busy_set_if_active(const struct dma_fence *fence, |
3fdc13c7 CW |
4212 | unsigned int (*flag)(unsigned int id)) |
4213 | { | |
d07f0e59 | 4214 | struct drm_i915_gem_request *rq; |
3fdc13c7 | 4215 | |
d07f0e59 CW |
4216 | /* We have to check the current hw status of the fence as the uABI |
4217 | * guarantees forward progress. We could rely on the idle worker | |
4218 | * to eventually flush us, but to minimise latency just ask the | |
4219 | * hardware. | |
1255501d | 4220 | * |
d07f0e59 | 4221 | * Note we only report on the status of native fences. |
1255501d | 4222 | */ |
d07f0e59 CW |
4223 | if (!dma_fence_is_i915(fence)) |
4224 | return 0; | |
4225 | ||
4226 | /* opencode to_request() in order to avoid const warnings */ | |
4227 | rq = container_of(fence, struct drm_i915_gem_request, fence); | |
4228 | if (i915_gem_request_completed(rq)) | |
4229 | return 0; | |
4230 | ||
1d39f281 | 4231 | return flag(rq->engine->uabi_id); |
3fdc13c7 CW |
4232 | } |
4233 | ||
edf6b76f | 4234 | static __always_inline unsigned int |
d07f0e59 | 4235 | busy_check_reader(const struct dma_fence *fence) |
3fdc13c7 | 4236 | { |
d07f0e59 | 4237 | return __busy_set_if_active(fence, __busy_read_flag); |
3fdc13c7 CW |
4238 | } |
4239 | ||
edf6b76f | 4240 | static __always_inline unsigned int |
d07f0e59 | 4241 | busy_check_writer(const struct dma_fence *fence) |
3fdc13c7 | 4242 | { |
d07f0e59 CW |
4243 | if (!fence) |
4244 | return 0; | |
4245 | ||
4246 | return __busy_set_if_active(fence, __busy_write_id); | |
3fdc13c7 CW |
4247 | } |
4248 | ||
673a394b EA |
4249 | int |
4250 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 4251 | struct drm_file *file) |
673a394b EA |
4252 | { |
4253 | struct drm_i915_gem_busy *args = data; | |
05394f39 | 4254 | struct drm_i915_gem_object *obj; |
d07f0e59 CW |
4255 | struct reservation_object_list *list; |
4256 | unsigned int seq; | |
fbbd37b3 | 4257 | int err; |
673a394b | 4258 | |
d07f0e59 | 4259 | err = -ENOENT; |
fbbd37b3 CW |
4260 | rcu_read_lock(); |
4261 | obj = i915_gem_object_lookup_rcu(file, args->handle); | |
d07f0e59 | 4262 | if (!obj) |
fbbd37b3 | 4263 | goto out; |
d1b851fc | 4264 | |
d07f0e59 CW |
4265 | /* A discrepancy here is that we do not report the status of |
4266 | * non-i915 fences, i.e. even though we may report the object as idle, | |
4267 | * a call to set-domain may still stall waiting for foreign rendering. | |
4268 | * This also means that wait-ioctl may report an object as busy, | |
4269 | * where busy-ioctl considers it idle. | |
4270 | * | |
4271 | * We trade the ability to warn of foreign fences to report on which | |
4272 | * i915 engines are active for the object. | |
4273 | * | |
4274 | * Alternatively, we can trade that extra information on read/write | |
4275 | * activity with | |
4276 | * args->busy = | |
4277 | * !reservation_object_test_signaled_rcu(obj->resv, true); | |
4278 | * to report the overall busyness. This is what the wait-ioctl does. | |
4279 | * | |
4280 | */ | |
4281 | retry: | |
4282 | seq = raw_read_seqcount(&obj->resv->seq); | |
426960be | 4283 | |
d07f0e59 CW |
4284 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
4285 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); | |
3fdc13c7 | 4286 | |
d07f0e59 CW |
4287 | /* Translate shared fences to READ set of engines */ |
4288 | list = rcu_dereference(obj->resv->fence); | |
4289 | if (list) { | |
4290 | unsigned int shared_count = list->shared_count, i; | |
3fdc13c7 | 4291 | |
d07f0e59 CW |
4292 | for (i = 0; i < shared_count; ++i) { |
4293 | struct dma_fence *fence = | |
4294 | rcu_dereference(list->shared[i]); | |
4295 | ||
4296 | args->busy |= busy_check_reader(fence); | |
4297 | } | |
426960be | 4298 | } |
673a394b | 4299 | |
d07f0e59 CW |
4300 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
4301 | goto retry; | |
4302 | ||
4303 | err = 0; | |
fbbd37b3 CW |
4304 | out: |
4305 | rcu_read_unlock(); | |
4306 | return err; | |
673a394b EA |
4307 | } |
4308 | ||
4309 | int | |
4310 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, | |
4311 | struct drm_file *file_priv) | |
4312 | { | |
0206e353 | 4313 | return i915_gem_ring_throttle(dev, file_priv); |
673a394b EA |
4314 | } |
4315 | ||
3ef94daa CW |
4316 | int |
4317 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, | |
4318 | struct drm_file *file_priv) | |
4319 | { | |
fac5e23e | 4320 | struct drm_i915_private *dev_priv = to_i915(dev); |
3ef94daa | 4321 | struct drm_i915_gem_madvise *args = data; |
05394f39 | 4322 | struct drm_i915_gem_object *obj; |
1233e2db | 4323 | int err; |
3ef94daa CW |
4324 | |
4325 | switch (args->madv) { | |
4326 | case I915_MADV_DONTNEED: | |
4327 | case I915_MADV_WILLNEED: | |
4328 | break; | |
4329 | default: | |
4330 | return -EINVAL; | |
4331 | } | |
4332 | ||
03ac0642 | 4333 | obj = i915_gem_object_lookup(file_priv, args->handle); |
1233e2db CW |
4334 | if (!obj) |
4335 | return -ENOENT; | |
4336 | ||
4337 | err = mutex_lock_interruptible(&obj->mm.lock); | |
4338 | if (err) | |
4339 | goto out; | |
3ef94daa | 4340 | |
f1fa4f44 | 4341 | if (i915_gem_object_has_pages(obj) && |
3e510a8e | 4342 | i915_gem_object_is_tiled(obj) && |
656bfa3a | 4343 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
bc0629a7 CW |
4344 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
4345 | GEM_BUG_ON(!obj->mm.quirked); | |
a4f5ea64 | 4346 | __i915_gem_object_unpin_pages(obj); |
bc0629a7 CW |
4347 | obj->mm.quirked = false; |
4348 | } | |
4349 | if (args->madv == I915_MADV_WILLNEED) { | |
2c3a3f44 | 4350 | GEM_BUG_ON(obj->mm.quirked); |
a4f5ea64 | 4351 | __i915_gem_object_pin_pages(obj); |
bc0629a7 CW |
4352 | obj->mm.quirked = true; |
4353 | } | |
656bfa3a DV |
4354 | } |
4355 | ||
a4f5ea64 CW |
4356 | if (obj->mm.madv != __I915_MADV_PURGED) |
4357 | obj->mm.madv = args->madv; | |
3ef94daa | 4358 | |
6c085a72 | 4359 | /* if the object is no longer attached, discard its backing storage */ |
f1fa4f44 CW |
4360 | if (obj->mm.madv == I915_MADV_DONTNEED && |
4361 | !i915_gem_object_has_pages(obj)) | |
2d7ef395 CW |
4362 | i915_gem_object_truncate(obj); |
4363 | ||
a4f5ea64 | 4364 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
1233e2db | 4365 | mutex_unlock(&obj->mm.lock); |
bb6baf76 | 4366 | |
1233e2db | 4367 | out: |
f8c417cd | 4368 | i915_gem_object_put(obj); |
1233e2db | 4369 | return err; |
3ef94daa CW |
4370 | } |
4371 | ||
5b8c8aec CW |
4372 | static void |
4373 | frontbuffer_retire(struct i915_gem_active *active, | |
4374 | struct drm_i915_gem_request *request) | |
4375 | { | |
4376 | struct drm_i915_gem_object *obj = | |
4377 | container_of(active, typeof(*obj), frontbuffer_write); | |
4378 | ||
d59b21ec | 4379 | intel_fb_obj_flush(obj, ORIGIN_CS); |
5b8c8aec CW |
4380 | } |
4381 | ||
37e680a1 CW |
4382 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
4383 | const struct drm_i915_gem_object_ops *ops) | |
0327d6ba | 4384 | { |
1233e2db CW |
4385 | mutex_init(&obj->mm.lock); |
4386 | ||
2f633156 | 4387 | INIT_LIST_HEAD(&obj->vma_list); |
d1b48c1e | 4388 | INIT_LIST_HEAD(&obj->lut_list); |
8d9d5744 | 4389 | INIT_LIST_HEAD(&obj->batch_pool_link); |
0327d6ba | 4390 | |
37e680a1 CW |
4391 | obj->ops = ops; |
4392 | ||
d07f0e59 CW |
4393 | reservation_object_init(&obj->__builtin_resv); |
4394 | obj->resv = &obj->__builtin_resv; | |
4395 | ||
50349247 | 4396 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
5b8c8aec | 4397 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
a4f5ea64 CW |
4398 | |
4399 | obj->mm.madv = I915_MADV_WILLNEED; | |
4400 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); | |
4401 | mutex_init(&obj->mm.get_page.lock); | |
0327d6ba | 4402 | |
f19ec8cb | 4403 | i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size); |
0327d6ba CW |
4404 | } |
4405 | ||
37e680a1 | 4406 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
3599a91c TU |
4407 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
4408 | I915_GEM_OBJECT_IS_SHRINKABLE, | |
7c55e2c5 | 4409 | |
37e680a1 CW |
4410 | .get_pages = i915_gem_object_get_pages_gtt, |
4411 | .put_pages = i915_gem_object_put_pages_gtt, | |
7c55e2c5 CW |
4412 | |
4413 | .pwrite = i915_gem_object_pwrite_gtt, | |
37e680a1 CW |
4414 | }; |
4415 | ||
465c403c MA |
4416 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
4417 | struct drm_gem_object *obj, | |
4418 | size_t size) | |
4419 | { | |
4420 | struct drm_i915_private *i915 = to_i915(dev); | |
4421 | unsigned long flags = VM_NORESERVE; | |
4422 | struct file *filp; | |
4423 | ||
4424 | drm_gem_private_object_init(dev, obj, size); | |
4425 | ||
4426 | if (i915->mm.gemfs) | |
4427 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, | |
4428 | flags); | |
4429 | else | |
4430 | filp = shmem_file_setup("i915", size, flags); | |
4431 | ||
4432 | if (IS_ERR(filp)) | |
4433 | return PTR_ERR(filp); | |
4434 | ||
4435 | obj->filp = filp; | |
4436 | ||
4437 | return 0; | |
4438 | } | |
4439 | ||
b4bcbe2a | 4440 | struct drm_i915_gem_object * |
12d79d78 | 4441 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
ac52bc56 | 4442 | { |
c397b908 | 4443 | struct drm_i915_gem_object *obj; |
5949eac4 | 4444 | struct address_space *mapping; |
b8f55be6 | 4445 | unsigned int cache_level; |
1a240d4d | 4446 | gfp_t mask; |
fe3db79b | 4447 | int ret; |
ac52bc56 | 4448 | |
b4bcbe2a CW |
4449 | /* There is a prevalence of the assumption that we fit the object's |
4450 | * page count inside a 32bit _signed_ variable. Let's document this and | |
4451 | * catch if we ever need to fix it. In the meantime, if you do spot | |
4452 | * such a local variable, please consider fixing! | |
4453 | */ | |
7a3ee5de | 4454 | if (size >> PAGE_SHIFT > INT_MAX) |
b4bcbe2a CW |
4455 | return ERR_PTR(-E2BIG); |
4456 | ||
4457 | if (overflows_type(size, obj->base.size)) | |
4458 | return ERR_PTR(-E2BIG); | |
4459 | ||
187685cb | 4460 | obj = i915_gem_object_alloc(dev_priv); |
c397b908 | 4461 | if (obj == NULL) |
fe3db79b | 4462 | return ERR_PTR(-ENOMEM); |
673a394b | 4463 | |
465c403c | 4464 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
fe3db79b CW |
4465 | if (ret) |
4466 | goto fail; | |
673a394b | 4467 | |
bed1ea95 | 4468 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
c0f86832 | 4469 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
bed1ea95 CW |
4470 | /* 965gm cannot relocate objects above 4GiB. */ |
4471 | mask &= ~__GFP_HIGHMEM; | |
4472 | mask |= __GFP_DMA32; | |
4473 | } | |
4474 | ||
93c76a3d | 4475 | mapping = obj->base.filp->f_mapping; |
bed1ea95 | 4476 | mapping_set_gfp_mask(mapping, mask); |
4846bf0c | 4477 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
5949eac4 | 4478 | |
37e680a1 | 4479 | i915_gem_object_init(obj, &i915_gem_object_ops); |
73aa808f | 4480 | |
c397b908 DV |
4481 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
4482 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
673a394b | 4483 | |
b8f55be6 | 4484 | if (HAS_LLC(dev_priv)) |
3d29b842 | 4485 | /* On some devices, we can have the GPU use the LLC (the CPU |
a1871112 EA |
4486 | * cache) for about a 10% performance improvement |
4487 | * compared to uncached. Graphics requests other than | |
4488 | * display scanout are coherent with the CPU in | |
4489 | * accessing this cache. This means in this mode we | |
4490 | * don't need to clflush on the CPU side, and on the | |
4491 | * GPU side we only need to flush internal caches to | |
4492 | * get data visible to the CPU. | |
4493 | * | |
4494 | * However, we maintain the display planes as UC, and so | |
4495 | * need to rebind when first used as such. | |
4496 | */ | |
b8f55be6 CW |
4497 | cache_level = I915_CACHE_LLC; |
4498 | else | |
4499 | cache_level = I915_CACHE_NONE; | |
a1871112 | 4500 | |
b8f55be6 | 4501 | i915_gem_object_set_cache_coherency(obj, cache_level); |
e27ab73d | 4502 | |
d861e338 DV |
4503 | trace_i915_gem_object_create(obj); |
4504 | ||
05394f39 | 4505 | return obj; |
fe3db79b CW |
4506 | |
4507 | fail: | |
4508 | i915_gem_object_free(obj); | |
fe3db79b | 4509 | return ERR_PTR(ret); |
c397b908 DV |
4510 | } |
4511 | ||
340fbd8c CW |
4512 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
4513 | { | |
4514 | /* If we are the last user of the backing storage (be it shmemfs | |
4515 | * pages or stolen etc), we know that the pages are going to be | |
4516 | * immediately released. In this case, we can then skip copying | |
4517 | * back the contents from the GPU. | |
4518 | */ | |
4519 | ||
a4f5ea64 | 4520 | if (obj->mm.madv != I915_MADV_WILLNEED) |
340fbd8c CW |
4521 | return false; |
4522 | ||
4523 | if (obj->base.filp == NULL) | |
4524 | return true; | |
4525 | ||
4526 | /* At first glance, this looks racy, but then again so would be | |
4527 | * userspace racing mmap against close. However, the first external | |
4528 | * reference to the filp can only be obtained through the | |
4529 | * i915_gem_mmap_ioctl() which safeguards us against the user | |
4530 | * acquiring such a reference whilst we are in the middle of | |
4531 | * freeing the object. | |
4532 | */ | |
4533 | return atomic_long_read(&obj->base.filp->f_count) == 1; | |
4534 | } | |
4535 | ||
fbbd37b3 CW |
4536 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
4537 | struct llist_node *freed) | |
673a394b | 4538 | { |
fbbd37b3 | 4539 | struct drm_i915_gem_object *obj, *on; |
673a394b | 4540 | |
fbbd37b3 | 4541 | intel_runtime_pm_get(i915); |
cc731f5a | 4542 | llist_for_each_entry_safe(obj, on, freed, freed) { |
fbbd37b3 CW |
4543 | struct i915_vma *vma, *vn; |
4544 | ||
4545 | trace_i915_gem_object_destroy(obj); | |
4546 | ||
cc731f5a CW |
4547 | mutex_lock(&i915->drm.struct_mutex); |
4548 | ||
fbbd37b3 CW |
4549 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
4550 | list_for_each_entry_safe(vma, vn, | |
4551 | &obj->vma_list, obj_link) { | |
fbbd37b3 CW |
4552 | GEM_BUG_ON(i915_vma_is_active(vma)); |
4553 | vma->flags &= ~I915_VMA_PIN_MASK; | |
4554 | i915_vma_close(vma); | |
4555 | } | |
db6c2b41 CW |
4556 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
4557 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); | |
fbbd37b3 | 4558 | |
f2123818 CW |
4559 | /* This serializes freeing with the shrinker. Since the free |
4560 | * is delayed, first by RCU then by the workqueue, we want the | |
4561 | * shrinker to be able to free pages of unreferenced objects, | |
4562 | * or else we may oom whilst there are plenty of deferred | |
4563 | * freed objects. | |
4564 | */ | |
4565 | if (i915_gem_object_has_pages(obj)) { | |
4566 | spin_lock(&i915->mm.obj_lock); | |
4567 | list_del_init(&obj->mm.link); | |
4568 | spin_unlock(&i915->mm.obj_lock); | |
4569 | } | |
fbbd37b3 | 4570 | |
cc731f5a | 4571 | mutex_unlock(&i915->drm.struct_mutex); |
f2be9d68 | 4572 | |
fbbd37b3 | 4573 | GEM_BUG_ON(obj->bind_count); |
a65adaf8 | 4574 | GEM_BUG_ON(obj->userfault_count); |
fbbd37b3 | 4575 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
67b48040 | 4576 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
fbbd37b3 CW |
4577 | |
4578 | if (obj->ops->release) | |
4579 | obj->ops->release(obj); | |
f65c9168 | 4580 | |
fbbd37b3 CW |
4581 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
4582 | atomic_set(&obj->mm.pages_pin_count, 0); | |
548625ee | 4583 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
f1fa4f44 | 4584 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
fbbd37b3 CW |
4585 | |
4586 | if (obj->base.import_attach) | |
4587 | drm_prime_gem_destroy(&obj->base, NULL); | |
4588 | ||
d07f0e59 | 4589 | reservation_object_fini(&obj->__builtin_resv); |
fbbd37b3 CW |
4590 | drm_gem_object_release(&obj->base); |
4591 | i915_gem_info_remove_obj(i915, obj->base.size); | |
4592 | ||
4593 | kfree(obj->bit_17); | |
4594 | i915_gem_object_free(obj); | |
cc731f5a CW |
4595 | |
4596 | if (on) | |
4597 | cond_resched(); | |
fbbd37b3 | 4598 | } |
cc731f5a | 4599 | intel_runtime_pm_put(i915); |
fbbd37b3 CW |
4600 | } |
4601 | ||
4602 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) | |
4603 | { | |
4604 | struct llist_node *freed; | |
4605 | ||
87701b4b CW |
4606 | /* Free the oldest, most stale object to keep the free_list short */ |
4607 | freed = NULL; | |
4608 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ | |
4609 | /* Only one consumer of llist_del_first() allowed */ | |
4610 | spin_lock(&i915->mm.free_lock); | |
4611 | freed = llist_del_first(&i915->mm.free_list); | |
4612 | spin_unlock(&i915->mm.free_lock); | |
4613 | } | |
4614 | if (unlikely(freed)) { | |
4615 | freed->next = NULL; | |
fbbd37b3 | 4616 | __i915_gem_free_objects(i915, freed); |
87701b4b | 4617 | } |
fbbd37b3 CW |
4618 | } |
4619 | ||
4620 | static void __i915_gem_free_work(struct work_struct *work) | |
4621 | { | |
4622 | struct drm_i915_private *i915 = | |
4623 | container_of(work, struct drm_i915_private, mm.free_work); | |
4624 | struct llist_node *freed; | |
26e12f89 | 4625 | |
b1f788c6 CW |
4626 | /* All file-owned VMA should have been released by this point through |
4627 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). | |
4628 | * However, the object may also be bound into the global GTT (e.g. | |
4629 | * older GPUs without per-process support, or for direct access through | |
4630 | * the GTT either for the user or for scanout). Those VMA still need to | |
4631 | * unbound now. | |
4632 | */ | |
1488fc08 | 4633 | |
0f763ff3 | 4634 | spin_lock(&i915->mm.free_lock); |
5ad08be7 | 4635 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
0f763ff3 CW |
4636 | spin_unlock(&i915->mm.free_lock); |
4637 | ||
fbbd37b3 | 4638 | __i915_gem_free_objects(i915, freed); |
5ad08be7 | 4639 | if (need_resched()) |
0f763ff3 CW |
4640 | return; |
4641 | ||
4642 | spin_lock(&i915->mm.free_lock); | |
5ad08be7 | 4643 | } |
0f763ff3 | 4644 | spin_unlock(&i915->mm.free_lock); |
fbbd37b3 | 4645 | } |
a071fa00 | 4646 | |
fbbd37b3 CW |
4647 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
4648 | { | |
4649 | struct drm_i915_gem_object *obj = | |
4650 | container_of(head, typeof(*obj), rcu); | |
4651 | struct drm_i915_private *i915 = to_i915(obj->base.dev); | |
4652 | ||
4653 | /* We can't simply use call_rcu() from i915_gem_free_object() | |
4654 | * as we need to block whilst unbinding, and the call_rcu | |
4655 | * task may be called from softirq context. So we take a | |
4656 | * detour through a worker. | |
4657 | */ | |
4658 | if (llist_add(&obj->freed, &i915->mm.free_list)) | |
4659 | schedule_work(&i915->mm.free_work); | |
4660 | } | |
656bfa3a | 4661 | |
fbbd37b3 CW |
4662 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
4663 | { | |
4664 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); | |
a4f5ea64 | 4665 | |
bc0629a7 CW |
4666 | if (obj->mm.quirked) |
4667 | __i915_gem_object_unpin_pages(obj); | |
4668 | ||
340fbd8c | 4669 | if (discard_backing_storage(obj)) |
a4f5ea64 | 4670 | obj->mm.madv = I915_MADV_DONTNEED; |
de151cf6 | 4671 | |
fbbd37b3 CW |
4672 | /* Before we free the object, make sure any pure RCU-only |
4673 | * read-side critical sections are complete, e.g. | |
4674 | * i915_gem_busy_ioctl(). For the corresponding synchronized | |
4675 | * lookup see i915_gem_object_lookup_rcu(). | |
4676 | */ | |
4677 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); | |
673a394b EA |
4678 | } |
4679 | ||
f8a7fde4 CW |
4680 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
4681 | { | |
4682 | lockdep_assert_held(&obj->base.dev->struct_mutex); | |
4683 | ||
d1b48c1e CW |
4684 | if (!i915_gem_object_has_active_reference(obj) && |
4685 | i915_gem_object_is_active(obj)) | |
f8a7fde4 CW |
4686 | i915_gem_object_set_active_reference(obj); |
4687 | else | |
4688 | i915_gem_object_put(obj); | |
4689 | } | |
4690 | ||
3033acab CW |
4691 | static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv) |
4692 | { | |
4693 | struct intel_engine_cs *engine; | |
4694 | enum intel_engine_id id; | |
4695 | ||
4696 | for_each_engine(engine, dev_priv, id) | |
f131e356 CW |
4697 | GEM_BUG_ON(engine->last_retired_context && |
4698 | !i915_gem_context_is_kernel(engine->last_retired_context)); | |
3033acab CW |
4699 | } |
4700 | ||
24145517 CW |
4701 | void i915_gem_sanitize(struct drm_i915_private *i915) |
4702 | { | |
f36325f3 CW |
4703 | if (i915_terminally_wedged(&i915->gpu_error)) { |
4704 | mutex_lock(&i915->drm.struct_mutex); | |
4705 | i915_gem_unset_wedged(i915); | |
4706 | mutex_unlock(&i915->drm.struct_mutex); | |
4707 | } | |
4708 | ||
24145517 CW |
4709 | /* |
4710 | * If we inherit context state from the BIOS or earlier occupants | |
4711 | * of the GPU, the GPU may be in an inconsistent state when we | |
4712 | * try to take over. The only way to remove the earlier state | |
4713 | * is by resetting. However, resetting on earlier gen is tricky as | |
4714 | * it may impact the display and we are uncertain about the stability | |
ea117b8d | 4715 | * of the reset, so this could be applied to even earlier gen. |
24145517 | 4716 | */ |
ea117b8d | 4717 | if (INTEL_GEN(i915) >= 5) { |
24145517 CW |
4718 | int reset = intel_gpu_reset(i915, ALL_ENGINES); |
4719 | WARN_ON(reset && reset != -ENODEV); | |
4720 | } | |
4721 | } | |
4722 | ||
bf9e8429 | 4723 | int i915_gem_suspend(struct drm_i915_private *dev_priv) |
29105ccc | 4724 | { |
bf9e8429 | 4725 | struct drm_device *dev = &dev_priv->drm; |
dcff85c8 | 4726 | int ret; |
28dfe52a | 4727 | |
c998e8a0 | 4728 | intel_runtime_pm_get(dev_priv); |
54b4f68f CW |
4729 | intel_suspend_gt_powersave(dev_priv); |
4730 | ||
45c5f202 | 4731 | mutex_lock(&dev->struct_mutex); |
5ab57c70 CW |
4732 | |
4733 | /* We have to flush all the executing contexts to main memory so | |
4734 | * that they can saved in the hibernation image. To ensure the last | |
4735 | * context image is coherent, we have to switch away from it. That | |
4736 | * leaves the dev_priv->kernel_context still active when | |
4737 | * we actually suspend, and its image in memory may not match the GPU | |
4738 | * state. Fortunately, the kernel_context is disposable and we do | |
4739 | * not rely on its state. | |
4740 | */ | |
dda4b8f7 CW |
4741 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
4742 | ret = i915_gem_switch_to_kernel_context(dev_priv); | |
4743 | if (ret) | |
4744 | goto err_unlock; | |
5ab57c70 | 4745 | |
dda4b8f7 CW |
4746 | ret = i915_gem_wait_for_idle(dev_priv, |
4747 | I915_WAIT_INTERRUPTIBLE | | |
4748 | I915_WAIT_LOCKED); | |
4749 | if (ret && ret != -EIO) | |
4750 | goto err_unlock; | |
f7403347 | 4751 | |
dda4b8f7 CW |
4752 | assert_kernel_context_is_current(dev_priv); |
4753 | } | |
829a0af2 | 4754 | i915_gem_contexts_lost(dev_priv); |
45c5f202 CW |
4755 | mutex_unlock(&dev->struct_mutex); |
4756 | ||
63987bfe SAK |
4757 | intel_guc_suspend(dev_priv); |
4758 | ||
737b1506 | 4759 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
67d97da3 | 4760 | cancel_delayed_work_sync(&dev_priv->gt.retire_work); |
bdeb9785 CW |
4761 | |
4762 | /* As the idle_work is rearming if it detects a race, play safe and | |
4763 | * repeat the flush until it is definitely idle. | |
4764 | */ | |
7c26240e | 4765 | drain_delayed_work(&dev_priv->gt.idle_work); |
bdeb9785 | 4766 | |
bdcf120b CW |
4767 | /* Assert that we sucessfully flushed all the work and |
4768 | * reset the GPU back to its idle, low power state. | |
4769 | */ | |
67d97da3 | 4770 | WARN_ON(dev_priv->gt.awake); |
fc692bd3 CW |
4771 | if (WARN_ON(!intel_engines_are_idle(dev_priv))) |
4772 | i915_gem_set_wedged(dev_priv); /* no hope, discard everything */ | |
bdcf120b | 4773 | |
1c777c5d ID |
4774 | /* |
4775 | * Neither the BIOS, ourselves or any other kernel | |
4776 | * expects the system to be in execlists mode on startup, | |
4777 | * so we need to reset the GPU back to legacy mode. And the only | |
4778 | * known way to disable logical contexts is through a GPU reset. | |
4779 | * | |
4780 | * So in order to leave the system in a known default configuration, | |
4781 | * always reset the GPU upon unload and suspend. Afterwards we then | |
4782 | * clean up the GEM state tracking, flushing off the requests and | |
4783 | * leaving the system in a known idle state. | |
4784 | * | |
4785 | * Note that is of the upmost importance that the GPU is idle and | |
4786 | * all stray writes are flushed *before* we dismantle the backing | |
4787 | * storage for the pinned objects. | |
4788 | * | |
4789 | * However, since we are uncertain that resetting the GPU on older | |
4790 | * machines is a good idea, we don't - just in case it leaves the | |
4791 | * machine in an unusable condition. | |
4792 | */ | |
24145517 | 4793 | i915_gem_sanitize(dev_priv); |
cad9946c CW |
4794 | |
4795 | intel_runtime_pm_put(dev_priv); | |
4796 | return 0; | |
1c777c5d | 4797 | |
c998e8a0 | 4798 | err_unlock: |
45c5f202 | 4799 | mutex_unlock(&dev->struct_mutex); |
c998e8a0 | 4800 | intel_runtime_pm_put(dev_priv); |
45c5f202 | 4801 | return ret; |
673a394b EA |
4802 | } |
4803 | ||
bf9e8429 | 4804 | void i915_gem_resume(struct drm_i915_private *dev_priv) |
5ab57c70 | 4805 | { |
bf9e8429 | 4806 | struct drm_device *dev = &dev_priv->drm; |
5ab57c70 | 4807 | |
31ab49ab ID |
4808 | WARN_ON(dev_priv->gt.awake); |
4809 | ||
5ab57c70 | 4810 | mutex_lock(&dev->struct_mutex); |
275a991c | 4811 | i915_gem_restore_gtt_mappings(dev_priv); |
269e6ea9 | 4812 | i915_gem_restore_fences(dev_priv); |
5ab57c70 CW |
4813 | |
4814 | /* As we didn't flush the kernel context before suspend, we cannot | |
4815 | * guarantee that the context image is complete. So let's just reset | |
4816 | * it and start again. | |
4817 | */ | |
821ed7df | 4818 | dev_priv->gt.resume(dev_priv); |
5ab57c70 CW |
4819 | |
4820 | mutex_unlock(&dev->struct_mutex); | |
4821 | } | |
4822 | ||
c6be607a | 4823 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
f691e2f4 | 4824 | { |
c6be607a | 4825 | if (INTEL_GEN(dev_priv) < 5 || |
f691e2f4 DV |
4826 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
4827 | return; | |
4828 | ||
4829 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | | |
4830 | DISP_TILE_SURFACE_SWIZZLING); | |
4831 | ||
5db94019 | 4832 | if (IS_GEN5(dev_priv)) |
11782b02 DV |
4833 | return; |
4834 | ||
f691e2f4 | 4835 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
5db94019 | 4836 | if (IS_GEN6(dev_priv)) |
6b26c86d | 4837 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
5db94019 | 4838 | else if (IS_GEN7(dev_priv)) |
6b26c86d | 4839 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
5db94019 | 4840 | else if (IS_GEN8(dev_priv)) |
31a5336e | 4841 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
8782e26c BW |
4842 | else |
4843 | BUG(); | |
f691e2f4 | 4844 | } |
e21af88d | 4845 | |
50a0bc90 | 4846 | static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base) |
81e7f200 | 4847 | { |
81e7f200 VS |
4848 | I915_WRITE(RING_CTL(base), 0); |
4849 | I915_WRITE(RING_HEAD(base), 0); | |
4850 | I915_WRITE(RING_TAIL(base), 0); | |
4851 | I915_WRITE(RING_START(base), 0); | |
4852 | } | |
4853 | ||
50a0bc90 | 4854 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
81e7f200 | 4855 | { |
50a0bc90 TU |
4856 | if (IS_I830(dev_priv)) { |
4857 | init_unused_ring(dev_priv, PRB1_BASE); | |
4858 | init_unused_ring(dev_priv, SRB0_BASE); | |
4859 | init_unused_ring(dev_priv, SRB1_BASE); | |
4860 | init_unused_ring(dev_priv, SRB2_BASE); | |
4861 | init_unused_ring(dev_priv, SRB3_BASE); | |
4862 | } else if (IS_GEN2(dev_priv)) { | |
4863 | init_unused_ring(dev_priv, SRB0_BASE); | |
4864 | init_unused_ring(dev_priv, SRB1_BASE); | |
4865 | } else if (IS_GEN3(dev_priv)) { | |
4866 | init_unused_ring(dev_priv, PRB1_BASE); | |
4867 | init_unused_ring(dev_priv, PRB2_BASE); | |
81e7f200 VS |
4868 | } |
4869 | } | |
4870 | ||
20a8a74a | 4871 | static int __i915_gem_restart_engines(void *data) |
4fc7c971 | 4872 | { |
20a8a74a | 4873 | struct drm_i915_private *i915 = data; |
e2f80391 | 4874 | struct intel_engine_cs *engine; |
3b3f1650 | 4875 | enum intel_engine_id id; |
20a8a74a CW |
4876 | int err; |
4877 | ||
4878 | for_each_engine(engine, i915, id) { | |
4879 | err = engine->init_hw(engine); | |
4880 | if (err) | |
4881 | return err; | |
4882 | } | |
4883 | ||
4884 | return 0; | |
4885 | } | |
4886 | ||
4887 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) | |
4888 | { | |
d200cda6 | 4889 | int ret; |
4fc7c971 | 4890 | |
de867c20 CW |
4891 | dev_priv->gt.last_init_time = ktime_get(); |
4892 | ||
5e4f5189 CW |
4893 | /* Double layer security blanket, see i915_gem_init() */ |
4894 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); | |
4895 | ||
0031fb96 | 4896 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
05e21cc4 | 4897 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
4fc7c971 | 4898 | |
772c2a51 | 4899 | if (IS_HASWELL(dev_priv)) |
50a0bc90 | 4900 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
0bf21347 | 4901 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
9435373e | 4902 | |
6e266956 | 4903 | if (HAS_PCH_NOP(dev_priv)) { |
fd6b8f43 | 4904 | if (IS_IVYBRIDGE(dev_priv)) { |
6ba844b0 DV |
4905 | u32 temp = I915_READ(GEN7_MSG_CTL); |
4906 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); | |
4907 | I915_WRITE(GEN7_MSG_CTL, temp); | |
c6be607a | 4908 | } else if (INTEL_GEN(dev_priv) >= 7) { |
6ba844b0 DV |
4909 | u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); |
4910 | temp &= ~RESET_PCH_HANDSHAKE_ENABLE; | |
4911 | I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); | |
4912 | } | |
88a2b2a3 BW |
4913 | } |
4914 | ||
c6be607a | 4915 | i915_gem_init_swizzling(dev_priv); |
4fc7c971 | 4916 | |
d5abdfda DV |
4917 | /* |
4918 | * At least 830 can leave some of the unused rings | |
4919 | * "active" (ie. head != tail) after resume which | |
4920 | * will prevent c3 entry. Makes sure all unused rings | |
4921 | * are totally idle. | |
4922 | */ | |
50a0bc90 | 4923 | init_unused_rings(dev_priv); |
d5abdfda | 4924 | |
ed54c1a1 | 4925 | BUG_ON(!dev_priv->kernel_context); |
6f74b36b CW |
4926 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
4927 | ret = -EIO; | |
4928 | goto out; | |
4929 | } | |
90638cc1 | 4930 | |
c6be607a | 4931 | ret = i915_ppgtt_init_hw(dev_priv); |
4ad2fd88 JH |
4932 | if (ret) { |
4933 | DRM_ERROR("PPGTT enable HW failed %d\n", ret); | |
4934 | goto out; | |
4935 | } | |
4936 | ||
4937 | /* Need to do basic initialisation of all rings first: */ | |
20a8a74a CW |
4938 | ret = __i915_gem_restart_engines(dev_priv); |
4939 | if (ret) | |
4940 | goto out; | |
99433931 | 4941 | |
bf9e8429 | 4942 | intel_mocs_init_l3cc_table(dev_priv); |
0ccdacf6 | 4943 | |
b8991403 OM |
4944 | /* We can't enable contexts until all firmware is loaded */ |
4945 | ret = intel_uc_init_hw(dev_priv); | |
4946 | if (ret) | |
4947 | goto out; | |
33a732f4 | 4948 | |
5e4f5189 CW |
4949 | out: |
4950 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); | |
2fa48d8d | 4951 | return ret; |
8187a2b7 ZN |
4952 | } |
4953 | ||
39df9190 CW |
4954 | bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value) |
4955 | { | |
4956 | if (INTEL_INFO(dev_priv)->gen < 6) | |
4957 | return false; | |
4958 | ||
4959 | /* TODO: make semaphores and Execlists play nicely together */ | |
4f044a88 | 4960 | if (i915_modparams.enable_execlists) |
39df9190 CW |
4961 | return false; |
4962 | ||
4963 | if (value >= 0) | |
4964 | return value; | |
4965 | ||
39df9190 | 4966 | /* Enable semaphores on SNB when IO remapping is off */ |
80debff8 | 4967 | if (IS_GEN6(dev_priv) && intel_vtd_active()) |
39df9190 | 4968 | return false; |
39df9190 CW |
4969 | |
4970 | return true; | |
4971 | } | |
4972 | ||
bf9e8429 | 4973 | int i915_gem_init(struct drm_i915_private *dev_priv) |
1070a42b | 4974 | { |
1070a42b CW |
4975 | int ret; |
4976 | ||
da9fe3f3 MA |
4977 | /* |
4978 | * We need to fallback to 4K pages since gvt gtt handling doesn't | |
4979 | * support huge page entries - we will need to check either hypervisor | |
4980 | * mm can support huge guest page or just do emulation in gvt. | |
4981 | */ | |
4982 | if (intel_vgpu_active(dev_priv)) | |
4983 | mkwrite_device_info(dev_priv)->page_sizes = | |
4984 | I915_GTT_PAGE_SIZE_4K; | |
4985 | ||
94312828 | 4986 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
57822dc6 | 4987 | |
4f044a88 | 4988 | if (!i915_modparams.enable_execlists) { |
821ed7df | 4989 | dev_priv->gt.resume = intel_legacy_submission_resume; |
7e37f889 | 4990 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
454afebd | 4991 | } else { |
821ed7df | 4992 | dev_priv->gt.resume = intel_lr_context_resume; |
117897f4 | 4993 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
a83014d3 OM |
4994 | } |
4995 | ||
ef78970a CW |
4996 | ret = i915_gem_init_userptr(dev_priv); |
4997 | if (ret) | |
4998 | return ret; | |
4999 | ||
5e4f5189 CW |
5000 | /* This is just a security blanket to placate dragons. |
5001 | * On some systems, we very sporadically observe that the first TLBs | |
5002 | * used by the CS may be stale, despite us poking the TLB reset. If | |
5003 | * we hold the forcewake during initialisation these problems | |
5004 | * just magically go away. | |
5005 | */ | |
ef78970a | 5006 | mutex_lock(&dev_priv->drm.struct_mutex); |
5e4f5189 CW |
5007 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
5008 | ||
f6b9d5ca CW |
5009 | ret = i915_gem_init_ggtt(dev_priv); |
5010 | if (ret) | |
5011 | goto out_unlock; | |
d62b4892 | 5012 | |
829a0af2 | 5013 | ret = i915_gem_contexts_init(dev_priv); |
7bcc3777 JN |
5014 | if (ret) |
5015 | goto out_unlock; | |
2fa48d8d | 5016 | |
bf9e8429 | 5017 | ret = intel_engines_init(dev_priv); |
35a57ffb | 5018 | if (ret) |
7bcc3777 | 5019 | goto out_unlock; |
2fa48d8d | 5020 | |
bf9e8429 | 5021 | ret = i915_gem_init_hw(dev_priv); |
60990320 | 5022 | if (ret == -EIO) { |
d9729391 CW |
5023 | mutex_lock(&dev_priv->drm.struct_mutex); |
5024 | ||
7e21d648 | 5025 | /* Allow engine initialisation to fail by marking the GPU as |
60990320 CW |
5026 | * wedged. But we only want to do this where the GPU is angry, |
5027 | * for all other failure, such as an allocation failure, bail. | |
5028 | */ | |
6f74b36b CW |
5029 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
5030 | DRM_ERROR("Failed to initialize GPU, declaring it wedged\n"); | |
5031 | i915_gem_set_wedged(dev_priv); | |
5032 | } | |
d9729391 CW |
5033 | |
5034 | /* Minimal basic recovery for KMS */ | |
5035 | ret = i915_ggtt_enable_hw(dev_priv); | |
5036 | i915_gem_restore_gtt_mappings(dev_priv); | |
5037 | i915_gem_restore_fences(dev_priv); | |
5038 | intel_init_clock_gating(dev_priv); | |
5039 | ||
5040 | mutex_unlock(&dev_priv->drm.struct_mutex); | |
1070a42b | 5041 | } |
7bcc3777 JN |
5042 | |
5043 | out_unlock: | |
5e4f5189 | 5044 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
bf9e8429 | 5045 | mutex_unlock(&dev_priv->drm.struct_mutex); |
1070a42b | 5046 | |
60990320 | 5047 | return ret; |
1070a42b CW |
5048 | } |
5049 | ||
24145517 CW |
5050 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
5051 | { | |
5052 | i915_gem_sanitize(i915); | |
5053 | } | |
5054 | ||
8187a2b7 | 5055 | void |
cb15d9f8 | 5056 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
8187a2b7 | 5057 | { |
e2f80391 | 5058 | struct intel_engine_cs *engine; |
3b3f1650 | 5059 | enum intel_engine_id id; |
8187a2b7 | 5060 | |
3b3f1650 | 5061 | for_each_engine(engine, dev_priv, id) |
117897f4 | 5062 | dev_priv->gt.cleanup_engine(engine); |
8187a2b7 ZN |
5063 | } |
5064 | ||
40ae4e16 ID |
5065 | void |
5066 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) | |
5067 | { | |
49ef5294 | 5068 | int i; |
40ae4e16 ID |
5069 | |
5070 | if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) && | |
5071 | !IS_CHERRYVIEW(dev_priv)) | |
5072 | dev_priv->num_fence_regs = 32; | |
73f67aa8 JN |
5073 | else if (INTEL_INFO(dev_priv)->gen >= 4 || |
5074 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || | |
5075 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) | |
40ae4e16 ID |
5076 | dev_priv->num_fence_regs = 16; |
5077 | else | |
5078 | dev_priv->num_fence_regs = 8; | |
5079 | ||
c033666a | 5080 | if (intel_vgpu_active(dev_priv)) |
40ae4e16 ID |
5081 | dev_priv->num_fence_regs = |
5082 | I915_READ(vgtif_reg(avail_rs.fence_num)); | |
5083 | ||
5084 | /* Initialize fence registers to zero */ | |
49ef5294 CW |
5085 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
5086 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; | |
5087 | ||
5088 | fence->i915 = dev_priv; | |
5089 | fence->id = i; | |
5090 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); | |
5091 | } | |
4362f4f6 | 5092 | i915_gem_restore_fences(dev_priv); |
40ae4e16 | 5093 | |
4362f4f6 | 5094 | i915_gem_detect_bit_6_swizzle(dev_priv); |
40ae4e16 ID |
5095 | } |
5096 | ||
73cb9701 | 5097 | int |
cb15d9f8 | 5098 | i915_gem_load_init(struct drm_i915_private *dev_priv) |
673a394b | 5099 | { |
a933568e | 5100 | int err = -ENOMEM; |
42dcedd4 | 5101 | |
a933568e TU |
5102 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
5103 | if (!dev_priv->objects) | |
73cb9701 | 5104 | goto err_out; |
73cb9701 | 5105 | |
a933568e TU |
5106 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
5107 | if (!dev_priv->vmas) | |
73cb9701 | 5108 | goto err_objects; |
73cb9701 | 5109 | |
d1b48c1e CW |
5110 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
5111 | if (!dev_priv->luts) | |
5112 | goto err_vmas; | |
5113 | ||
a933568e TU |
5114 | dev_priv->requests = KMEM_CACHE(drm_i915_gem_request, |
5115 | SLAB_HWCACHE_ALIGN | | |
5116 | SLAB_RECLAIM_ACCOUNT | | |
5f0d5a3a | 5117 | SLAB_TYPESAFE_BY_RCU); |
a933568e | 5118 | if (!dev_priv->requests) |
d1b48c1e | 5119 | goto err_luts; |
73cb9701 | 5120 | |
52e54209 CW |
5121 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
5122 | SLAB_HWCACHE_ALIGN | | |
5123 | SLAB_RECLAIM_ACCOUNT); | |
5124 | if (!dev_priv->dependencies) | |
5125 | goto err_requests; | |
5126 | ||
c5cf9a91 CW |
5127 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
5128 | if (!dev_priv->priorities) | |
5129 | goto err_dependencies; | |
5130 | ||
73cb9701 CW |
5131 | mutex_lock(&dev_priv->drm.struct_mutex); |
5132 | INIT_LIST_HEAD(&dev_priv->gt.timelines); | |
bb89485e | 5133 | err = i915_gem_timeline_init__global(dev_priv); |
73cb9701 CW |
5134 | mutex_unlock(&dev_priv->drm.struct_mutex); |
5135 | if (err) | |
c5cf9a91 | 5136 | goto err_priorities; |
673a394b | 5137 | |
fbbd37b3 | 5138 | INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work); |
f2123818 CW |
5139 | |
5140 | spin_lock_init(&dev_priv->mm.obj_lock); | |
87701b4b | 5141 | spin_lock_init(&dev_priv->mm.free_lock); |
fbbd37b3 | 5142 | init_llist_head(&dev_priv->mm.free_list); |
6c085a72 CW |
5143 | INIT_LIST_HEAD(&dev_priv->mm.unbound_list); |
5144 | INIT_LIST_HEAD(&dev_priv->mm.bound_list); | |
a09ba7fa | 5145 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
275f039d | 5146 | INIT_LIST_HEAD(&dev_priv->mm.userfault_list); |
f2123818 | 5147 | |
67d97da3 | 5148 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
673a394b | 5149 | i915_gem_retire_work_handler); |
67d97da3 | 5150 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
b29c19b6 | 5151 | i915_gem_idle_work_handler); |
1f15b76f | 5152 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
1f83fee0 | 5153 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
31169714 | 5154 | |
6f633402 JL |
5155 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
5156 | ||
b5add959 | 5157 | spin_lock_init(&dev_priv->fb_tracking.lock); |
73cb9701 | 5158 | |
465c403c MA |
5159 | err = i915_gemfs_init(dev_priv); |
5160 | if (err) | |
5161 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); | |
5162 | ||
73cb9701 CW |
5163 | return 0; |
5164 | ||
c5cf9a91 CW |
5165 | err_priorities: |
5166 | kmem_cache_destroy(dev_priv->priorities); | |
52e54209 CW |
5167 | err_dependencies: |
5168 | kmem_cache_destroy(dev_priv->dependencies); | |
73cb9701 CW |
5169 | err_requests: |
5170 | kmem_cache_destroy(dev_priv->requests); | |
d1b48c1e CW |
5171 | err_luts: |
5172 | kmem_cache_destroy(dev_priv->luts); | |
73cb9701 CW |
5173 | err_vmas: |
5174 | kmem_cache_destroy(dev_priv->vmas); | |
5175 | err_objects: | |
5176 | kmem_cache_destroy(dev_priv->objects); | |
5177 | err_out: | |
5178 | return err; | |
673a394b | 5179 | } |
71acb5eb | 5180 | |
cb15d9f8 | 5181 | void i915_gem_load_cleanup(struct drm_i915_private *dev_priv) |
d64aa096 | 5182 | { |
c4d4c1c6 | 5183 | i915_gem_drain_freed_objects(dev_priv); |
7d5d59e5 | 5184 | WARN_ON(!llist_empty(&dev_priv->mm.free_list)); |
c4d4c1c6 | 5185 | WARN_ON(dev_priv->mm.object_count); |
7d5d59e5 | 5186 | |
ea84aa77 MA |
5187 | mutex_lock(&dev_priv->drm.struct_mutex); |
5188 | i915_gem_timeline_fini(&dev_priv->gt.global_timeline); | |
5189 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); | |
5190 | mutex_unlock(&dev_priv->drm.struct_mutex); | |
5191 | ||
c5cf9a91 | 5192 | kmem_cache_destroy(dev_priv->priorities); |
52e54209 | 5193 | kmem_cache_destroy(dev_priv->dependencies); |
d64aa096 | 5194 | kmem_cache_destroy(dev_priv->requests); |
d1b48c1e | 5195 | kmem_cache_destroy(dev_priv->luts); |
d64aa096 ID |
5196 | kmem_cache_destroy(dev_priv->vmas); |
5197 | kmem_cache_destroy(dev_priv->objects); | |
0eafec6d CW |
5198 | |
5199 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ | |
5200 | rcu_barrier(); | |
465c403c MA |
5201 | |
5202 | i915_gemfs_fini(dev_priv); | |
d64aa096 ID |
5203 | } |
5204 | ||
6a800eab CW |
5205 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
5206 | { | |
d0aa301a CW |
5207 | /* Discard all purgeable objects, let userspace recover those as |
5208 | * required after resuming. | |
5209 | */ | |
6a800eab | 5210 | i915_gem_shrink_all(dev_priv); |
6a800eab | 5211 | |
6a800eab CW |
5212 | return 0; |
5213 | } | |
5214 | ||
461fb99c CW |
5215 | int i915_gem_freeze_late(struct drm_i915_private *dev_priv) |
5216 | { | |
5217 | struct drm_i915_gem_object *obj; | |
7aab2d53 CW |
5218 | struct list_head *phases[] = { |
5219 | &dev_priv->mm.unbound_list, | |
5220 | &dev_priv->mm.bound_list, | |
5221 | NULL | |
5222 | }, **p; | |
461fb99c CW |
5223 | |
5224 | /* Called just before we write the hibernation image. | |
5225 | * | |
5226 | * We need to update the domain tracking to reflect that the CPU | |
5227 | * will be accessing all the pages to create and restore from the | |
5228 | * hibernation, and so upon restoration those pages will be in the | |
5229 | * CPU domain. | |
5230 | * | |
5231 | * To make sure the hibernation image contains the latest state, | |
5232 | * we update that state just before writing out the image. | |
7aab2d53 CW |
5233 | * |
5234 | * To try and reduce the hibernation image, we manually shrink | |
d0aa301a | 5235 | * the objects as well, see i915_gem_freeze() |
461fb99c CW |
5236 | */ |
5237 | ||
912d572d | 5238 | i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND); |
17b93c40 | 5239 | i915_gem_drain_freed_objects(dev_priv); |
461fb99c | 5240 | |
f2123818 | 5241 | spin_lock(&dev_priv->mm.obj_lock); |
7aab2d53 | 5242 | for (p = phases; *p; p++) { |
f2123818 | 5243 | list_for_each_entry(obj, *p, mm.link) |
e27ab73d | 5244 | __start_cpu_write(obj); |
461fb99c | 5245 | } |
f2123818 | 5246 | spin_unlock(&dev_priv->mm.obj_lock); |
461fb99c CW |
5247 | |
5248 | return 0; | |
5249 | } | |
5250 | ||
f787a5f5 | 5251 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
b962442e | 5252 | { |
f787a5f5 | 5253 | struct drm_i915_file_private *file_priv = file->driver_priv; |
15f7bbc7 | 5254 | struct drm_i915_gem_request *request; |
b962442e EA |
5255 | |
5256 | /* Clean up our request list when the client is going away, so that | |
5257 | * later retire_requests won't dereference our soon-to-be-gone | |
5258 | * file_priv. | |
5259 | */ | |
1c25595f | 5260 | spin_lock(&file_priv->mm.lock); |
c8659efa | 5261 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
f787a5f5 | 5262 | request->file_priv = NULL; |
1c25595f | 5263 | spin_unlock(&file_priv->mm.lock); |
b29c19b6 CW |
5264 | } |
5265 | ||
829a0af2 | 5266 | int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file) |
b29c19b6 CW |
5267 | { |
5268 | struct drm_i915_file_private *file_priv; | |
e422b888 | 5269 | int ret; |
b29c19b6 | 5270 | |
c4c29d7b | 5271 | DRM_DEBUG("\n"); |
b29c19b6 CW |
5272 | |
5273 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); | |
5274 | if (!file_priv) | |
5275 | return -ENOMEM; | |
5276 | ||
5277 | file->driver_priv = file_priv; | |
829a0af2 | 5278 | file_priv->dev_priv = i915; |
ab0e7ff9 | 5279 | file_priv->file = file; |
b29c19b6 CW |
5280 | |
5281 | spin_lock_init(&file_priv->mm.lock); | |
5282 | INIT_LIST_HEAD(&file_priv->mm.request_list); | |
b29c19b6 | 5283 | |
c80ff16e | 5284 | file_priv->bsd_engine = -1; |
de1add36 | 5285 | |
829a0af2 | 5286 | ret = i915_gem_context_open(i915, file); |
e422b888 BW |
5287 | if (ret) |
5288 | kfree(file_priv); | |
b29c19b6 | 5289 | |
e422b888 | 5290 | return ret; |
b29c19b6 CW |
5291 | } |
5292 | ||
b680c37a DV |
5293 | /** |
5294 | * i915_gem_track_fb - update frontbuffer tracking | |
d9072a3e GT |
5295 | * @old: current GEM buffer for the frontbuffer slots |
5296 | * @new: new GEM buffer for the frontbuffer slots | |
5297 | * @frontbuffer_bits: bitmask of frontbuffer slots | |
b680c37a DV |
5298 | * |
5299 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them | |
5300 | * from @old and setting them in @new. Both @old and @new can be NULL. | |
5301 | */ | |
a071fa00 DV |
5302 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
5303 | struct drm_i915_gem_object *new, | |
5304 | unsigned frontbuffer_bits) | |
5305 | { | |
faf5bf0a CW |
5306 | /* Control of individual bits within the mask are guarded by |
5307 | * the owning plane->mutex, i.e. we can never see concurrent | |
5308 | * manipulation of individual bits. But since the bitfield as a whole | |
5309 | * is updated using RMW, we need to use atomics in order to update | |
5310 | * the bits. | |
5311 | */ | |
5312 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > | |
5313 | sizeof(atomic_t) * BITS_PER_BYTE); | |
5314 | ||
a071fa00 | 5315 | if (old) { |
faf5bf0a CW |
5316 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
5317 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); | |
a071fa00 DV |
5318 | } |
5319 | ||
5320 | if (new) { | |
faf5bf0a CW |
5321 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
5322 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); | |
a071fa00 DV |
5323 | } |
5324 | } | |
5325 | ||
ea70299d DG |
5326 | /* Allocate a new GEM object and fill it with the supplied data */ |
5327 | struct drm_i915_gem_object * | |
12d79d78 | 5328 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
ea70299d DG |
5329 | const void *data, size_t size) |
5330 | { | |
5331 | struct drm_i915_gem_object *obj; | |
be062fa4 CW |
5332 | struct file *file; |
5333 | size_t offset; | |
5334 | int err; | |
ea70299d | 5335 | |
12d79d78 | 5336 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
fe3db79b | 5337 | if (IS_ERR(obj)) |
ea70299d DG |
5338 | return obj; |
5339 | ||
ce8ff099 | 5340 | GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU); |
ea70299d | 5341 | |
be062fa4 CW |
5342 | file = obj->base.filp; |
5343 | offset = 0; | |
5344 | do { | |
5345 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); | |
5346 | struct page *page; | |
5347 | void *pgdata, *vaddr; | |
ea70299d | 5348 | |
be062fa4 CW |
5349 | err = pagecache_write_begin(file, file->f_mapping, |
5350 | offset, len, 0, | |
5351 | &page, &pgdata); | |
5352 | if (err < 0) | |
5353 | goto fail; | |
ea70299d | 5354 | |
be062fa4 CW |
5355 | vaddr = kmap(page); |
5356 | memcpy(vaddr, data, len); | |
5357 | kunmap(page); | |
5358 | ||
5359 | err = pagecache_write_end(file, file->f_mapping, | |
5360 | offset, len, len, | |
5361 | page, pgdata); | |
5362 | if (err < 0) | |
5363 | goto fail; | |
5364 | ||
5365 | size -= len; | |
5366 | data += len; | |
5367 | offset += len; | |
5368 | } while (size); | |
ea70299d DG |
5369 | |
5370 | return obj; | |
5371 | ||
5372 | fail: | |
f8c417cd | 5373 | i915_gem_object_put(obj); |
be062fa4 | 5374 | return ERR_PTR(err); |
ea70299d | 5375 | } |
96d77634 CW |
5376 | |
5377 | struct scatterlist * | |
5378 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, | |
5379 | unsigned int n, | |
5380 | unsigned int *offset) | |
5381 | { | |
a4f5ea64 | 5382 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
96d77634 CW |
5383 | struct scatterlist *sg; |
5384 | unsigned int idx, count; | |
5385 | ||
5386 | might_sleep(); | |
5387 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); | |
a4f5ea64 | 5388 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
96d77634 CW |
5389 | |
5390 | /* As we iterate forward through the sg, we record each entry in a | |
5391 | * radixtree for quick repeated (backwards) lookups. If we have seen | |
5392 | * this index previously, we will have an entry for it. | |
5393 | * | |
5394 | * Initial lookup is O(N), but this is amortized to O(1) for | |
5395 | * sequential page access (where each new request is consecutive | |
5396 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), | |
5397 | * i.e. O(1) with a large constant! | |
5398 | */ | |
5399 | if (n < READ_ONCE(iter->sg_idx)) | |
5400 | goto lookup; | |
5401 | ||
5402 | mutex_lock(&iter->lock); | |
5403 | ||
5404 | /* We prefer to reuse the last sg so that repeated lookup of this | |
5405 | * (or the subsequent) sg are fast - comparing against the last | |
5406 | * sg is faster than going through the radixtree. | |
5407 | */ | |
5408 | ||
5409 | sg = iter->sg_pos; | |
5410 | idx = iter->sg_idx; | |
5411 | count = __sg_page_count(sg); | |
5412 | ||
5413 | while (idx + count <= n) { | |
5414 | unsigned long exception, i; | |
5415 | int ret; | |
5416 | ||
5417 | /* If we cannot allocate and insert this entry, or the | |
5418 | * individual pages from this range, cancel updating the | |
5419 | * sg_idx so that on this lookup we are forced to linearly | |
5420 | * scan onwards, but on future lookups we will try the | |
5421 | * insertion again (in which case we need to be careful of | |
5422 | * the error return reporting that we have already inserted | |
5423 | * this index). | |
5424 | */ | |
5425 | ret = radix_tree_insert(&iter->radix, idx, sg); | |
5426 | if (ret && ret != -EEXIST) | |
5427 | goto scan; | |
5428 | ||
5429 | exception = | |
5430 | RADIX_TREE_EXCEPTIONAL_ENTRY | | |
5431 | idx << RADIX_TREE_EXCEPTIONAL_SHIFT; | |
5432 | for (i = 1; i < count; i++) { | |
5433 | ret = radix_tree_insert(&iter->radix, idx + i, | |
5434 | (void *)exception); | |
5435 | if (ret && ret != -EEXIST) | |
5436 | goto scan; | |
5437 | } | |
5438 | ||
5439 | idx += count; | |
5440 | sg = ____sg_next(sg); | |
5441 | count = __sg_page_count(sg); | |
5442 | } | |
5443 | ||
5444 | scan: | |
5445 | iter->sg_pos = sg; | |
5446 | iter->sg_idx = idx; | |
5447 | ||
5448 | mutex_unlock(&iter->lock); | |
5449 | ||
5450 | if (unlikely(n < idx)) /* insertion completed by another thread */ | |
5451 | goto lookup; | |
5452 | ||
5453 | /* In case we failed to insert the entry into the radixtree, we need | |
5454 | * to look beyond the current sg. | |
5455 | */ | |
5456 | while (idx + count <= n) { | |
5457 | idx += count; | |
5458 | sg = ____sg_next(sg); | |
5459 | count = __sg_page_count(sg); | |
5460 | } | |
5461 | ||
5462 | *offset = n - idx; | |
5463 | return sg; | |
5464 | ||
5465 | lookup: | |
5466 | rcu_read_lock(); | |
5467 | ||
5468 | sg = radix_tree_lookup(&iter->radix, n); | |
5469 | GEM_BUG_ON(!sg); | |
5470 | ||
5471 | /* If this index is in the middle of multi-page sg entry, | |
5472 | * the radixtree will contain an exceptional entry that points | |
5473 | * to the start of that range. We will return the pointer to | |
5474 | * the base page and the offset of this page within the | |
5475 | * sg entry's range. | |
5476 | */ | |
5477 | *offset = 0; | |
5478 | if (unlikely(radix_tree_exception(sg))) { | |
5479 | unsigned long base = | |
5480 | (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT; | |
5481 | ||
5482 | sg = radix_tree_lookup(&iter->radix, base); | |
5483 | GEM_BUG_ON(!sg); | |
5484 | ||
5485 | *offset = n - base; | |
5486 | } | |
5487 | ||
5488 | rcu_read_unlock(); | |
5489 | ||
5490 | return sg; | |
5491 | } | |
5492 | ||
5493 | struct page * | |
5494 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) | |
5495 | { | |
5496 | struct scatterlist *sg; | |
5497 | unsigned int offset; | |
5498 | ||
5499 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); | |
5500 | ||
5501 | sg = i915_gem_object_get_sg(obj, n, &offset); | |
5502 | return nth_page(sg_page(sg), offset); | |
5503 | } | |
5504 | ||
5505 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ | |
5506 | struct page * | |
5507 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, | |
5508 | unsigned int n) | |
5509 | { | |
5510 | struct page *page; | |
5511 | ||
5512 | page = i915_gem_object_get_page(obj, n); | |
a4f5ea64 | 5513 | if (!obj->mm.dirty) |
96d77634 CW |
5514 | set_page_dirty(page); |
5515 | ||
5516 | return page; | |
5517 | } | |
5518 | ||
5519 | dma_addr_t | |
5520 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, | |
5521 | unsigned long n) | |
5522 | { | |
5523 | struct scatterlist *sg; | |
5524 | unsigned int offset; | |
5525 | ||
5526 | sg = i915_gem_object_get_sg(obj, n, &offset); | |
5527 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); | |
5528 | } | |
935a2f77 | 5529 | |
8eeb7906 CW |
5530 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
5531 | { | |
5532 | struct sg_table *pages; | |
5533 | int err; | |
5534 | ||
5535 | if (align > obj->base.size) | |
5536 | return -EINVAL; | |
5537 | ||
5538 | if (obj->ops == &i915_gem_phys_ops) | |
5539 | return 0; | |
5540 | ||
5541 | if (obj->ops != &i915_gem_object_ops) | |
5542 | return -EINVAL; | |
5543 | ||
5544 | err = i915_gem_object_unbind(obj); | |
5545 | if (err) | |
5546 | return err; | |
5547 | ||
5548 | mutex_lock(&obj->mm.lock); | |
5549 | ||
5550 | if (obj->mm.madv != I915_MADV_WILLNEED) { | |
5551 | err = -EFAULT; | |
5552 | goto err_unlock; | |
5553 | } | |
5554 | ||
5555 | if (obj->mm.quirked) { | |
5556 | err = -EFAULT; | |
5557 | goto err_unlock; | |
5558 | } | |
5559 | ||
5560 | if (obj->mm.mapping) { | |
5561 | err = -EBUSY; | |
5562 | goto err_unlock; | |
5563 | } | |
5564 | ||
f2123818 CW |
5565 | pages = fetch_and_zero(&obj->mm.pages); |
5566 | if (pages) { | |
5567 | struct drm_i915_private *i915 = to_i915(obj->base.dev); | |
5568 | ||
5569 | __i915_gem_object_reset_page_iter(obj); | |
5570 | ||
5571 | spin_lock(&i915->mm.obj_lock); | |
5572 | list_del(&obj->mm.link); | |
5573 | spin_unlock(&i915->mm.obj_lock); | |
5574 | } | |
5575 | ||
8eeb7906 CW |
5576 | obj->ops = &i915_gem_phys_ops; |
5577 | ||
8fb6a5df | 5578 | err = ____i915_gem_object_get_pages(obj); |
8eeb7906 CW |
5579 | if (err) |
5580 | goto err_xfer; | |
5581 | ||
5582 | /* Perma-pin (until release) the physical set of pages */ | |
5583 | __i915_gem_object_pin_pages(obj); | |
5584 | ||
5585 | if (!IS_ERR_OR_NULL(pages)) | |
5586 | i915_gem_object_ops.put_pages(obj, pages); | |
5587 | mutex_unlock(&obj->mm.lock); | |
5588 | return 0; | |
5589 | ||
5590 | err_xfer: | |
5591 | obj->ops = &i915_gem_object_ops; | |
5592 | obj->mm.pages = pages; | |
5593 | err_unlock: | |
5594 | mutex_unlock(&obj->mm.lock); | |
5595 | return err; | |
5596 | } | |
5597 | ||
935a2f77 CW |
5598 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
5599 | #include "selftests/scatterlist.c" | |
66d9cb5d | 5600 | #include "selftests/mock_gem_device.c" |
44653988 | 5601 | #include "selftests/huge_gem_object.c" |
4049866f | 5602 | #include "selftests/huge_pages.c" |
8335fd65 | 5603 | #include "selftests/i915_gem_object.c" |
17059450 | 5604 | #include "selftests/i915_gem_coherency.c" |
935a2f77 | 5605 | #endif |