1658 lines · 42.2 KB · C
RawDownload
/* * Copyright 2008 Advanced Micro Devices, Inc. * Copyright 2008 Red Hat Inc. * Copyright 2009 Jerome Glisse. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Authors: Dave Airlie *          Alex Deucher *          Jerome Glisse */#include <linux/fence-array.h>#include <drm/drmP.h>#include <drm/amdgpu_drm.h>#include "amdgpu.h"#include "amdgpu_trace.h" /* * GPUVM * GPUVM is similar to the legacy gart on older asics, however * rather than there being a single global gart table * for the entire GPU, there are multiple VM page tables active * at any given time.  The VM page tables can contain a mix * vram pages and system memory pages and system memory pages * can be mapped as snooped (cached system pages) or unsnooped * (uncached system pages). * Each VM has an ID associated with it and there is a page table * associated with each VMID.  When execting a command buffer, * the kernel tells the the ring what VMID to use for that command * buffer.  VMIDs are allocated dynamically as commands are submitted. * The userspace drivers maintain their own address space and the kernel * sets up their pages tables accordingly when they submit their * command buffers and a VMID is assigned. * Cayman/Trinity support up to 8 active VMs at any given time; * SI supports 16. */ /* Special value that no flush is necessary */#define AMDGPU_VM_NO_FLUSH (~0ll) /* Local structure. Encapsulate some VM table update parameters to reduce * the number of function parameters */struct amdgpu_vm_update_params {	/* address where to copy page table entries from */	uint64_t src;	/* DMA addresses to use for mapping */	dma_addr_t *pages_addr;	/* indirect buffer to fill with commands */	struct amdgpu_ib *ib;}; /** * amdgpu_vm_num_pde - return the number of page directory entries * * @adev: amdgpu_device pointer * * Calculate the number of page directory entries. */static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev){	return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;} /** * amdgpu_vm_directory_size - returns the size of the page directory in bytes * * @adev: amdgpu_device pointer * * Calculate the size of the page directory in bytes. */static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev){	return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);} /** * amdgpu_vm_get_pd_bo - add the VM PD to a validation list * * @vm: vm providing the BOs * @validated: head of validation list * @entry: entry to add * * Add the page directory to the list of BOs to * validate for command submission. */void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,			 struct list_head *validated,			 struct amdgpu_bo_list_entry *entry){	entry->robj = vm->page_directory;	entry->priority = 0;	entry->tv.bo = &vm->page_directory->tbo;	entry->tv.shared = true;	entry->user_pages = NULL;	list_add(&entry->tv.head, validated);} /** * amdgpu_vm_get_bos - add the vm BOs to a duplicates list * * @adev: amdgpu device pointer * @vm: vm providing the BOs * @duplicates: head of duplicates list * * Add the page directory to the BO duplicates list * for command submission. */void amdgpu_vm_get_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,			  struct list_head *duplicates){	uint64_t num_evictions;	unsigned i; 	/* We only need to validate the page tables	 * if they aren't already valid.	 */	num_evictions = atomic64_read(&adev->num_evictions);	if (num_evictions == vm->last_eviction_counter)		return; 	/* add the vm page table to the list */	for (i = 0; i <= vm->max_pde_used; ++i) {		struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry; 		if (!entry->robj)			continue; 		list_add(&entry->tv.head, duplicates);	} } /** * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail * * @adev: amdgpu device instance * @vm: vm providing the BOs * * Move the PT BOs to the tail of the LRU. */void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,				  struct amdgpu_vm *vm){	struct ttm_bo_global *glob = adev->mman.bdev.glob;	unsigned i; 	spin_lock(&glob->lru_lock);	for (i = 0; i <= vm->max_pde_used; ++i) {		struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry; 		if (!entry->robj)			continue; 		ttm_bo_move_to_lru_tail(&entry->robj->tbo);	}	spin_unlock(&glob->lru_lock);} static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,			      struct amdgpu_vm_id *id){	return id->current_gpu_reset_count !=		atomic_read(&adev->gpu_reset_counter) ? true : false;} /** * amdgpu_vm_grab_id - allocate the next free VMID * * @vm: vm to allocate id for * @ring: ring we want to submit job to * @sync: sync object where we add dependencies * @fence: fence protecting ID from reuse * * Allocate an id for the vm, adding fences to the sync obj as necessary. */int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,		      struct amdgpu_sync *sync, struct fence *fence,		      struct amdgpu_job *job){	struct amdgpu_device *adev = ring->adev;	uint64_t fence_context = adev->fence_context + ring->idx;	struct fence *updates = sync->last_vm_update;	struct amdgpu_vm_id *id, *idle;	struct fence **fences;	unsigned i;	int r = 0; 	fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,			       GFP_KERNEL);	if (!fences)		return -ENOMEM; 	mutex_lock(&adev->vm_manager.lock); 	/* Check if we have an idle VMID */	i = 0;	list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {		fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);		if (!fences[i])			break;		++i;	} 	/* If we can't find a idle VMID to use, wait till one becomes available */	if (&idle->list == &adev->vm_manager.ids_lru) {		u64 fence_context = adev->vm_manager.fence_context + ring->idx;		unsigned seqno = ++adev->vm_manager.seqno[ring->idx];		struct fence_array *array;		unsigned j; 		for (j = 0; j < i; ++j)			fence_get(fences[j]); 		array = fence_array_create(i, fences, fence_context,					   seqno, true);		if (!array) {			for (j = 0; j < i; ++j)				fence_put(fences[j]);			kfree(fences);			r = -ENOMEM;			goto error;		}  		r = amdgpu_sync_fence(ring->adev, sync, &array->base);		fence_put(&array->base);		if (r)			goto error; 		mutex_unlock(&adev->vm_manager.lock);		return 0; 	}	kfree(fences); 	job->vm_needs_flush = true;	/* Check if we can use a VMID already assigned to this VM */	i = ring->idx;	do {		struct fence *flushed; 		id = vm->ids[i++];		if (i == AMDGPU_MAX_RINGS)			i = 0; 		/* Check all the prerequisites to using this VMID */		if (!id)			continue;		if (amdgpu_vm_is_gpu_reset(adev, id))			continue; 		if (atomic64_read(&id->owner) != vm->client_id)			continue; 		if (job->vm_pd_addr != id->pd_gpu_addr)			continue; 		if (!id->last_flush)			continue; 		if (id->last_flush->context != fence_context &&		    !fence_is_signaled(id->last_flush))			continue; 		flushed  = id->flushed_updates;		if (updates &&		    (!flushed || fence_is_later(updates, flushed)))			continue; 		/* Good we can use this VMID. Remember this submission as		 * user of the VMID.		 */		r = amdgpu_sync_fence(ring->adev, &id->active, fence);		if (r)			goto error; 		id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);		list_move_tail(&id->list, &adev->vm_manager.ids_lru);		vm->ids[ring->idx] = id; 		job->vm_id = id - adev->vm_manager.ids;		job->vm_needs_flush = false;		trace_amdgpu_vm_grab_id(vm, ring->idx, job); 		mutex_unlock(&adev->vm_manager.lock);		return 0; 	} while (i != ring->idx); 	/* Still no ID to use? Then use the idle one found earlier */	id = idle; 	/* Remember this submission as user of the VMID */	r = amdgpu_sync_fence(ring->adev, &id->active, fence);	if (r)		goto error; 	fence_put(id->first);	id->first = fence_get(fence); 	fence_put(id->last_flush);	id->last_flush = NULL; 	fence_put(id->flushed_updates);	id->flushed_updates = fence_get(updates); 	id->pd_gpu_addr = job->vm_pd_addr;	id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);	list_move_tail(&id->list, &adev->vm_manager.ids_lru);	atomic64_set(&id->owner, vm->client_id);	vm->ids[ring->idx] = id; 	job->vm_id = id - adev->vm_manager.ids;	trace_amdgpu_vm_grab_id(vm, ring->idx, job); error:	mutex_unlock(&adev->vm_manager.lock);	return r;} static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring){	struct amdgpu_device *adev = ring->adev;	const struct amdgpu_ip_block_version *ip_block; 	if (ring->type != AMDGPU_RING_TYPE_COMPUTE)		/* only compute rings */		return false; 	ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);	if (!ip_block)		return false; 	if (ip_block->major <= 7) {		/* gfx7 has no workaround */		return true;	} else if (ip_block->major == 8) {		if (adev->gfx.mec_fw_version >= 673)			/* gfx8 is fixed in MEC firmware 673 */			return false;		else			return true;	}	return false;} /** * amdgpu_vm_flush - hardware flush the vm * * @ring: ring to use for flush * @vm_id: vmid number to use * @pd_addr: address of the page directory * * Emit a VM flush when it is necessary. */int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job){	struct amdgpu_device *adev = ring->adev;	struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];	bool gds_switch_needed = ring->funcs->emit_gds_switch && (		id->gds_base != job->gds_base ||		id->gds_size != job->gds_size ||		id->gws_base != job->gws_base ||		id->gws_size != job->gws_size ||		id->oa_base != job->oa_base ||		id->oa_size != job->oa_size);	int r; 	if (ring->funcs->emit_pipeline_sync && (	    job->vm_needs_flush || gds_switch_needed ||	    amdgpu_vm_ring_has_compute_vm_bug(ring)))		amdgpu_ring_emit_pipeline_sync(ring); 	if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||	    amdgpu_vm_is_gpu_reset(adev, id))) {		struct fence *fence; 		trace_amdgpu_vm_flush(job->vm_pd_addr, ring->idx, job->vm_id);		amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr); 		r = amdgpu_fence_emit(ring, &fence);		if (r)			return r; 		mutex_lock(&adev->vm_manager.lock);		fence_put(id->last_flush);		id->last_flush = fence;		mutex_unlock(&adev->vm_manager.lock);	} 	if (gds_switch_needed) {		id->gds_base = job->gds_base;		id->gds_size = job->gds_size;		id->gws_base = job->gws_base;		id->gws_size = job->gws_size;		id->oa_base = job->oa_base;		id->oa_size = job->oa_size;		amdgpu_ring_emit_gds_switch(ring, job->vm_id,					    job->gds_base, job->gds_size,					    job->gws_base, job->gws_size,					    job->oa_base, job->oa_size);	} 	return 0;} /** * amdgpu_vm_reset_id - reset VMID to zero * * @adev: amdgpu device structure * @vm_id: vmid number to use * * Reset saved GDW, GWS and OA to force switch on next flush. */void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id){	struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id]; 	id->gds_base = 0;	id->gds_size = 0;	id->gws_base = 0;	id->gws_size = 0;	id->oa_base = 0;	id->oa_size = 0;} /** * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo * * @vm: requested vm * @bo: requested buffer object * * Find @bo inside the requested vm. * Search inside the @bos vm list for the requested vm * Returns the found bo_va or NULL if none is found * * Object has to be reserved! */struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,				       struct amdgpu_bo *bo){	struct amdgpu_bo_va *bo_va; 	list_for_each_entry(bo_va, &bo->va, bo_list) {		if (bo_va->vm == vm) {			return bo_va;		}	}	return NULL;} /** * amdgpu_vm_update_pages - helper to call the right asic function * * @adev: amdgpu_device pointer * @vm_update_params: see amdgpu_vm_update_params definition * @pe: addr of the page entry * @addr: dst addr to write into pe * @count: number of page entries to update * @incr: increase next addr by incr bytes * @flags: hw access flags * * Traces the parameters and calls the right asic functions * to setup the page table using the DMA. */static void amdgpu_vm_update_pages(struct amdgpu_device *adev,				   struct amdgpu_vm_update_params					*vm_update_params,				   uint64_t pe, uint64_t addr,				   unsigned count, uint32_t incr,				   uint32_t flags){	trace_amdgpu_vm_set_page(pe, addr, count, incr, flags); 	if (vm_update_params->src) {		amdgpu_vm_copy_pte(adev, vm_update_params->ib,			pe, (vm_update_params->src + (addr >> 12) * 8), count); 	} else if (vm_update_params->pages_addr) {		amdgpu_vm_write_pte(adev, vm_update_params->ib,			vm_update_params->pages_addr,			pe, addr, count, incr, flags); 

Lines 1–500 of 1658.