2 * Copyright 2008 Jerome Glisse.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Jerome Glisse <glisse@freedesktop.org>
27 #include <linux/list_sort.h>
29 #include <drm/amdgpu_drm.h>
31 #include "amdgpu_trace.h"
33 #define AMDGPU_CS_MAX_PRIORITY 32u
34 #define AMDGPU_CS_NUM_BUCKETS (AMDGPU_CS_MAX_PRIORITY + 1)
36 /* This is based on the bucket sort with O(n) time complexity.
37 * An item with priority "i" is added to bucket[i]. The lists are then
38 * concatenated in descending order.
40 struct amdgpu_cs_buckets {
41 struct list_head bucket[AMDGPU_CS_NUM_BUCKETS];
44 static void amdgpu_cs_buckets_init(struct amdgpu_cs_buckets *b)
48 for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++)
49 INIT_LIST_HEAD(&b->bucket[i]);
52 static void amdgpu_cs_buckets_add(struct amdgpu_cs_buckets *b,
53 struct list_head *item, unsigned priority)
55 /* Since buffers which appear sooner in the relocation list are
56 * likely to be used more often than buffers which appear later
57 * in the list, the sort mustn't change the ordering of buffers
58 * with the same priority, i.e. it must be stable.
60 list_add_tail(item, &b->bucket[min(priority, AMDGPU_CS_MAX_PRIORITY)]);
63 static void amdgpu_cs_buckets_get_list(struct amdgpu_cs_buckets *b,
64 struct list_head *out_list)
68 /* Connect the sorted buckets in the output list. */
69 for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++) {
70 list_splice(&b->bucket[i], out_list);
74 int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
75 u32 ip_instance, u32 ring,
76 struct amdgpu_ring **out_ring)
78 /* Right now all IPs have only one instance - multiple rings. */
79 if (ip_instance != 0) {
80 DRM_ERROR("invalid ip instance: %d\n", ip_instance);
86 DRM_ERROR("unknown ip type: %d\n", ip_type);
88 case AMDGPU_HW_IP_GFX:
89 if (ring < adev->gfx.num_gfx_rings) {
90 *out_ring = &adev->gfx.gfx_ring[ring];
92 DRM_ERROR("only %d gfx rings are supported now\n",
93 adev->gfx.num_gfx_rings);
97 case AMDGPU_HW_IP_COMPUTE:
98 if (ring < adev->gfx.num_compute_rings) {
99 *out_ring = &adev->gfx.compute_ring[ring];
101 DRM_ERROR("only %d compute rings are supported now\n",
102 adev->gfx.num_compute_rings);
106 case AMDGPU_HW_IP_DMA:
108 *out_ring = &adev->sdma[ring].ring;
110 DRM_ERROR("only two SDMA rings are supported\n");
114 case AMDGPU_HW_IP_UVD:
115 *out_ring = &adev->uvd.ring;
117 case AMDGPU_HW_IP_VCE:
119 *out_ring = &adev->vce.ring[ring];
121 DRM_ERROR("only two VCE rings are supported\n");
129 int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
131 union drm_amdgpu_cs *cs = data;
132 uint64_t *chunk_array_user;
133 uint64_t *chunk_array = NULL;
134 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
138 if (!cs->in.num_chunks)
141 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
146 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
149 INIT_LIST_HEAD(&p->validated);
150 chunk_array = kcalloc(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
151 if (chunk_array == NULL) {
156 chunk_array_user = (uint64_t *)(unsigned long)(cs->in.chunks);
157 if (copy_from_user(chunk_array, chunk_array_user,
158 sizeof(uint64_t)*cs->in.num_chunks)) {
163 p->nchunks = cs->in.num_chunks;
164 p->chunks = kcalloc(p->nchunks, sizeof(struct amdgpu_cs_chunk),
166 if (p->chunks == NULL) {
171 for (i = 0; i < p->nchunks; i++) {
172 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
173 struct drm_amdgpu_cs_chunk user_chunk;
174 uint32_t __user *cdata;
176 chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
177 if (copy_from_user(&user_chunk, chunk_ptr,
178 sizeof(struct drm_amdgpu_cs_chunk))) {
182 p->chunks[i].chunk_id = user_chunk.chunk_id;
183 p->chunks[i].length_dw = user_chunk.length_dw;
184 if (p->chunks[i].chunk_id == AMDGPU_CHUNK_ID_IB)
187 size = p->chunks[i].length_dw;
188 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
189 p->chunks[i].user_ptr = cdata;
191 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
192 if (p->chunks[i].kdata == NULL) {
196 size *= sizeof(uint32_t);
197 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
202 if (p->chunks[i].chunk_id == AMDGPU_CHUNK_ID_FENCE) {
203 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
204 if (p->chunks[i].length_dw * sizeof(uint32_t) >= size) {
206 struct drm_gem_object *gobj;
207 struct drm_amdgpu_cs_chunk_fence *fence_data;
209 fence_data = (void *)p->chunks[i].kdata;
210 handle = fence_data->handle;
211 gobj = drm_gem_object_lookup(p->adev->ddev,
218 p->uf.bo = gem_to_amdgpu_bo(gobj);
219 p->uf.offset = fence_data->offset;
227 p->ibs = kcalloc(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
233 p->ib_bos = kcalloc(p->num_ibs, sizeof(struct amdgpu_bo_list_entry),
243 /* Returns how many bytes TTM can move per IB.
245 static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
247 u64 real_vram_size = adev->mc.real_vram_size;
248 u64 vram_usage = atomic64_read(&adev->vram_usage);
250 /* This function is based on the current VRAM usage.
252 * - If all of VRAM is free, allow relocating the number of bytes that
253 * is equal to 1/4 of the size of VRAM for this IB.
255 * - If more than one half of VRAM is occupied, only allow relocating
256 * 1 MB of data for this IB.
258 * - From 0 to one half of used VRAM, the threshold decreases
273 * Note: It's a threshold, not a limit. The threshold must be crossed
274 * for buffer relocations to stop, so any buffer of an arbitrary size
275 * can be moved as long as the threshold isn't crossed before
276 * the relocation takes place. We don't want to disable buffer
277 * relocations completely.
279 * The idea is that buffers should be placed in VRAM at creation time
280 * and TTM should only do a minimum number of relocations during
281 * command submission. In practice, you need to submit at least
282 * a dozen IBs to move all buffers to VRAM if they are in GTT.
284 * Also, things can get pretty crazy under memory pressure and actual
285 * VRAM usage can change a lot, so playing safe even at 50% does
286 * consistently increase performance.
289 u64 half_vram = real_vram_size >> 1;
290 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
291 u64 bytes_moved_threshold = half_free_vram >> 1;
292 return max(bytes_moved_threshold, 1024*1024ull);
295 int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p)
297 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
298 struct amdgpu_vm *vm = &fpriv->vm;
299 struct amdgpu_device *adev = p->adev;
300 struct amdgpu_bo_list_entry *lobj;
301 struct list_head duplicates;
302 struct amdgpu_bo *bo;
303 u64 bytes_moved = 0, initial_bytes_moved;
304 u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev);
307 INIT_LIST_HEAD(&duplicates);
308 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
309 if (unlikely(r != 0)) {
313 list_for_each_entry(lobj, &p->validated, tv.head) {
315 if (!bo->pin_count) {
316 u32 domain = lobj->prefered_domains;
318 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
320 /* Check if this buffer will be moved and don't move it
321 * if we have moved too many buffers for this IB already.
323 * Note that this allows moving at least one buffer of
324 * any size, because it doesn't take the current "bo"
325 * into account. We don't want to disallow buffer moves
328 if (current_domain != AMDGPU_GEM_DOMAIN_CPU &&
329 (domain & current_domain) == 0 && /* will be moved */
330 bytes_moved > bytes_moved_threshold) {
332 domain = current_domain;
336 amdgpu_ttm_placement_from_domain(bo, domain);
337 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
338 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
339 bytes_moved += atomic64_read(&adev->num_bytes_moved) -
343 if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
344 domain = lobj->allowed_domains;
347 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
351 lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
356 static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p)
358 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
359 struct amdgpu_cs_buckets buckets;
360 bool need_mmap_lock = false;
364 need_mmap_lock = p->bo_list->has_userptr;
365 amdgpu_cs_buckets_init(&buckets);
366 for (i = 0; i < p->bo_list->num_entries; i++)
367 amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
368 p->bo_list->array[i].priority);
370 amdgpu_cs_buckets_get_list(&buckets, &p->validated);
373 p->vm_bos = amdgpu_vm_get_bos(p->adev, &fpriv->vm,
376 for (i = 0; i < p->num_ibs; i++) {
377 if (!p->ib_bos[i].robj)
380 list_add(&p->ib_bos[i].tv.head, &p->validated);
384 down_read(¤t->mm->mmap_sem);
386 r = amdgpu_cs_list_validate(p);
389 up_read(¤t->mm->mmap_sem);
394 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
396 struct amdgpu_bo_list_entry *e;
399 list_for_each_entry(e, &p->validated, tv.head) {
400 struct reservation_object *resv = e->robj->tbo.resv;
401 r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
409 static int cmp_size_smaller_first(void *priv, struct list_head *a,
412 struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
413 struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
415 /* Sort A before B if A is smaller. */
416 return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
420 * cs_parser_fini() - clean parser states
421 * @parser: parser structure holding parsing context.
422 * @error: error number
424 * If error is set than unvalidate buffer, otherwise just free memory
425 * used by parsing context.
427 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
432 /* Sort the buffer list from the smallest to largest buffer,
433 * which affects the order of buffers in the LRU list.
434 * This assures that the smallest buffers are added first
435 * to the LRU list, so they are likely to be later evicted
436 * first, instead of large buffers whose eviction is more
439 * This slightly lowers the number of bytes moved by TTM
440 * per frame under memory pressure.
442 list_sort(NULL, &parser->validated, cmp_size_smaller_first);
444 ttm_eu_fence_buffer_objects(&parser->ticket,
446 &parser->ibs[parser->num_ibs-1].fence->base);
447 } else if (backoff) {
448 ttm_eu_backoff_reservation(&parser->ticket,
453 amdgpu_ctx_put(parser->ctx);
455 amdgpu_bo_list_put(parser->bo_list);
456 drm_free_large(parser->vm_bos);
457 for (i = 0; i < parser->nchunks; i++)
458 drm_free_large(parser->chunks[i].kdata);
459 kfree(parser->chunks);
460 for (i = 0; i < parser->num_ibs; i++) {
461 struct amdgpu_bo *bo = parser->ib_bos[i].robj;
462 amdgpu_ib_free(parser->adev, &parser->ibs[i]);
465 drm_gem_object_unreference_unlocked(&bo->gem_base);
468 kfree(parser->ib_bos);
470 drm_gem_object_unreference_unlocked(&parser->uf.bo->gem_base);
473 static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
474 struct amdgpu_vm *vm)
476 struct amdgpu_device *adev = p->adev;
477 struct amdgpu_bo_va *bo_va;
478 struct amdgpu_bo *bo;
481 r = amdgpu_vm_update_page_directory(adev, vm);
485 r = amdgpu_vm_clear_freed(adev, vm);
490 for (i = 0; i < p->bo_list->num_entries; i++) {
491 /* ignore duplicates */
492 bo = p->bo_list->array[i].robj;
496 bo_va = p->bo_list->array[i].bo_va;
500 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
504 amdgpu_sync_fence(&p->ibs[0].sync, bo_va->last_pt_update);
508 for (i = 0; i < p->num_ibs; i++) {
509 bo = p->ib_bos[i].robj;
513 bo_va = p->ib_bos[i].bo_va;
517 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
521 amdgpu_sync_fence(&p->ibs[0].sync, bo_va->last_pt_update);
523 return amdgpu_vm_clear_invalids(adev, vm);
526 static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
527 struct amdgpu_cs_parser *parser)
529 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
530 struct amdgpu_vm *vm = &fpriv->vm;
531 struct amdgpu_ring *ring;
534 if (parser->num_ibs == 0)
537 /* Only for UVD/VCE VM emulation */
538 for (i = 0; i < parser->num_ibs; i++) {
539 ring = parser->ibs[i].ring;
540 if (ring->funcs->parse_cs) {
541 r = amdgpu_ring_parse_cs(ring, parser, i);
547 mutex_lock(&vm->mutex);
548 r = amdgpu_bo_vm_update_pte(parser, vm);
552 amdgpu_cs_sync_rings(parser);
554 r = amdgpu_ib_schedule(adev, parser->num_ibs, parser->ibs,
558 mutex_unlock(&vm->mutex);
562 static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
565 r = amdgpu_gpu_reset(adev);
572 static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
573 struct amdgpu_cs_parser *parser)
575 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
576 struct amdgpu_vm *vm = &fpriv->vm;
580 for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
581 struct amdgpu_cs_chunk *chunk;
582 struct amdgpu_ib *ib;
583 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
584 struct amdgpu_bo_list_entry *ib_bo;
585 struct amdgpu_ring *ring;
586 struct drm_gem_object *gobj;
587 struct amdgpu_bo *aobj;
590 chunk = &parser->chunks[i];
591 ib = &parser->ibs[j];
592 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
594 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
597 gobj = drm_gem_object_lookup(adev->ddev, parser->filp, chunk_ib->handle);
600 aobj = gem_to_amdgpu_bo(gobj);
602 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
603 chunk_ib->ip_instance, chunk_ib->ring,
606 drm_gem_object_unreference_unlocked(gobj);
610 if (ring->funcs->parse_cs) {
611 r = amdgpu_bo_reserve(aobj, false);
613 drm_gem_object_unreference_unlocked(gobj);
617 r = amdgpu_bo_kmap(aobj, &kptr);
619 amdgpu_bo_unreserve(aobj);
620 drm_gem_object_unreference_unlocked(gobj);
624 r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
626 DRM_ERROR("Failed to get ib !\n");
627 amdgpu_bo_unreserve(aobj);
628 drm_gem_object_unreference_unlocked(gobj);
632 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
633 amdgpu_bo_kunmap(aobj);
634 amdgpu_bo_unreserve(aobj);
636 r = amdgpu_ib_get(ring, vm, 0, ib);
638 DRM_ERROR("Failed to get ib !\n");
639 drm_gem_object_unreference_unlocked(gobj);
643 ib->gpu_addr = chunk_ib->va_start;
645 ib->length_dw = chunk_ib->ib_bytes / 4;
647 ib->flags = chunk_ib->flags;
648 ib->ctx = parser->ctx;
650 ib_bo = &parser->ib_bos[j];
652 ib_bo->prefered_domains = aobj->initial_domain;
653 ib_bo->allowed_domains = aobj->initial_domain;
655 ib_bo->tv.bo = &aobj->tbo;
656 ib_bo->tv.shared = true;
660 if (!parser->num_ibs)
663 /* add GDS resources to first IB */
664 if (parser->bo_list) {
665 struct amdgpu_bo *gds = parser->bo_list->gds_obj;
666 struct amdgpu_bo *gws = parser->bo_list->gws_obj;
667 struct amdgpu_bo *oa = parser->bo_list->oa_obj;
668 struct amdgpu_ib *ib = &parser->ibs[0];
671 ib->gds_base = amdgpu_bo_gpu_offset(gds);
672 ib->gds_size = amdgpu_bo_size(gds);
675 ib->gws_base = amdgpu_bo_gpu_offset(gws);
676 ib->gws_size = amdgpu_bo_size(gws);
679 ib->oa_base = amdgpu_bo_gpu_offset(oa);
680 ib->oa_size = amdgpu_bo_size(oa);
684 /* wrap the last IB with user fence */
686 struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
688 /* UVD & VCE fw doesn't support user fences */
689 if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
690 ib->ring->type == AMDGPU_RING_TYPE_VCE)
693 ib->user = &parser->uf;
699 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
701 struct amdgpu_device *adev = dev->dev_private;
702 union drm_amdgpu_cs *cs = data;
703 struct amdgpu_cs_parser parser;
706 down_read(&adev->exclusive_lock);
707 if (!adev->accel_working) {
708 up_read(&adev->exclusive_lock);
711 /* initialize parser */
712 memset(&parser, 0, sizeof(struct amdgpu_cs_parser));
715 r = amdgpu_cs_parser_init(&parser, data);
717 DRM_ERROR("Failed to initialize parser !\n");
718 amdgpu_cs_parser_fini(&parser, r, false);
719 up_read(&adev->exclusive_lock);
720 r = amdgpu_cs_handle_lockup(adev, r);
724 r = amdgpu_cs_ib_fill(adev, &parser);
726 r = amdgpu_cs_parser_relocs(&parser);
727 if (r && r != -ERESTARTSYS)
728 DRM_ERROR("Failed to parse relocation %d!\n", r);
732 amdgpu_cs_parser_fini(&parser, r, false);
733 up_read(&adev->exclusive_lock);
734 r = amdgpu_cs_handle_lockup(adev, r);
738 for (i = 0; i < parser.num_ibs; i++)
739 trace_amdgpu_cs(&parser, i);
741 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
746 cs->out.handle = parser.ibs[parser.num_ibs - 1].fence->seq;
748 amdgpu_cs_parser_fini(&parser, r, true);
749 up_read(&adev->exclusive_lock);
750 r = amdgpu_cs_handle_lockup(adev, r);
755 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
758 * @data: data from userspace
759 * @filp: file private
761 * Wait for the command submission identified by handle to finish.
763 int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
764 struct drm_file *filp)
766 union drm_amdgpu_wait_cs *wait = data;
767 struct amdgpu_device *adev = dev->dev_private;
768 uint64_t seq[AMDGPU_MAX_RINGS] = {0};
769 struct amdgpu_ring *ring = NULL;
770 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
771 struct amdgpu_ctx *ctx;
774 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
778 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
779 wait->in.ring, &ring);
783 seq[ring->idx] = wait->in.handle;
785 r = amdgpu_fence_wait_seq_timeout(adev, seq, true, timeout);
790 memset(wait, 0, sizeof(*wait));
791 wait->out.status = (r == 0);
797 * amdgpu_cs_find_bo_va - find bo_va for VM address
799 * @parser: command submission parser context
801 * @bo: resulting BO of the mapping found
803 * Search the buffer objects in the command submission context for a certain
804 * virtual memory address. Returns allocation structure when found, NULL
807 struct amdgpu_bo_va_mapping *
808 amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
809 uint64_t addr, struct amdgpu_bo **bo)
811 struct amdgpu_bo_list_entry *reloc;
812 struct amdgpu_bo_va_mapping *mapping;
814 addr /= AMDGPU_GPU_PAGE_SIZE;
816 list_for_each_entry(reloc, &parser->validated, tv.head) {
820 list_for_each_entry(mapping, &reloc->bo_va->mappings, list) {
821 if (mapping->it.start > addr ||
822 addr > mapping->it.last)
825 *bo = reloc->bo_va->bo;