xref: /openbmc/linux/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
16dd09027SChristian König /*
26dd09027SChristian König  * Copyright 2019 Advanced Micro Devices, Inc.
36dd09027SChristian König  *
46dd09027SChristian König  * Permission is hereby granted, free of charge, to any person obtaining a
56dd09027SChristian König  * copy of this software and associated documentation files (the "Software"),
66dd09027SChristian König  * to deal in the Software without restriction, including without limitation
76dd09027SChristian König  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
86dd09027SChristian König  * and/or sell copies of the Software, and to permit persons to whom the
96dd09027SChristian König  * Software is furnished to do so, subject to the following conditions:
106dd09027SChristian König  *
116dd09027SChristian König  * The above copyright notice and this permission notice shall be included in
126dd09027SChristian König  * all copies or substantial portions of the Software.
136dd09027SChristian König  *
146dd09027SChristian König  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
156dd09027SChristian König  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
166dd09027SChristian König  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
176dd09027SChristian König  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
186dd09027SChristian König  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
196dd09027SChristian König  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
206dd09027SChristian König  * OTHER DEALINGS IN THE SOFTWARE.
216dd09027SChristian König  */
226dd09027SChristian König 
236dd09027SChristian König #include "amdgpu_vm.h"
246dd09027SChristian König #include "amdgpu_object.h"
256dd09027SChristian König #include "amdgpu_trace.h"
266dd09027SChristian König 
276dd09027SChristian König /**
28ecf96b52SChristian König  * amdgpu_vm_cpu_map_table - make sure new PDs/PTs are kmapped
29ecf96b52SChristian König  *
30ecf96b52SChristian König  * @table: newly allocated or validated PD/PT
31ecf96b52SChristian König  */
amdgpu_vm_cpu_map_table(struct amdgpu_bo_vm * table)3259276f05SNirmoy Das static int amdgpu_vm_cpu_map_table(struct amdgpu_bo_vm *table)
33ecf96b52SChristian König {
34*eb58ad14SXiaogang Chen 	table->bo.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
3559276f05SNirmoy Das 	return amdgpu_bo_kmap(&table->bo, NULL);
36ecf96b52SChristian König }
37ecf96b52SChristian König 
38ecf96b52SChristian König /**
396dd09027SChristian König  * amdgpu_vm_cpu_prepare - prepare page table update with the CPU
406dd09027SChristian König  *
416dd09027SChristian König  * @p: see amdgpu_vm_update_params definition
427eff65d6SLee Jones  * @resv: reservation object with embedded fence
437eff65d6SLee Jones  * @sync_mode: synchronization mode
446dd09027SChristian König  *
456dd09027SChristian König  * Returns:
466dd09027SChristian König  * Negativ errno, 0 for success.
476dd09027SChristian König  */
amdgpu_vm_cpu_prepare(struct amdgpu_vm_update_params * p,struct dma_resv * resv,enum amdgpu_sync_mode sync_mode)489f3cc18dSChristian König static int amdgpu_vm_cpu_prepare(struct amdgpu_vm_update_params *p,
499f3cc18dSChristian König 				 struct dma_resv *resv,
509f3cc18dSChristian König 				 enum amdgpu_sync_mode sync_mode)
516dd09027SChristian König {
529f3cc18dSChristian König 	if (!resv)
536dd09027SChristian König 		return 0;
5447ca7efaSChristian König 
559f3cc18dSChristian König 	return amdgpu_bo_sync_wait_resv(p->adev, resv, sync_mode, p->vm, true);
566dd09027SChristian König }
576dd09027SChristian König 
586dd09027SChristian König /**
596dd09027SChristian König  * amdgpu_vm_cpu_update - helper to update page tables via CPU
606dd09027SChristian König  *
616dd09027SChristian König  * @p: see amdgpu_vm_update_params definition
6259276f05SNirmoy Das  * @vmbo: PD/PT to update
63719a6513SOak Zeng  * @pe: byte offset of the PDE/PTE, relative to start of PDB/PTB
646dd09027SChristian König  * @addr: dst addr to write into pe
656dd09027SChristian König  * @count: number of page entries to update
666dd09027SChristian König  * @incr: increase next addr by incr bytes
676dd09027SChristian König  * @flags: hw access flags
686dd09027SChristian König  *
696dd09027SChristian König  * Write count number of PT/PD entries directly.
706dd09027SChristian König  */
amdgpu_vm_cpu_update(struct amdgpu_vm_update_params * p,struct amdgpu_bo_vm * vmbo,uint64_t pe,uint64_t addr,unsigned count,uint32_t incr,uint64_t flags)716dd09027SChristian König static int amdgpu_vm_cpu_update(struct amdgpu_vm_update_params *p,
7259276f05SNirmoy Das 				struct amdgpu_bo_vm *vmbo, uint64_t pe,
736dd09027SChristian König 				uint64_t addr, unsigned count, uint32_t incr,
746dd09027SChristian König 				uint64_t flags)
756dd09027SChristian König {
766dd09027SChristian König 	unsigned int i;
776dd09027SChristian König 	uint64_t value;
788bb31587SChristian König 	long r;
7981417beaSChristian König 
808bb31587SChristian König 	r = dma_resv_wait_timeout(vmbo->bo.tbo.base.resv, DMA_RESV_USAGE_KERNEL,
818bb31587SChristian König 				  true, MAX_SCHEDULE_TIMEOUT);
828bb31587SChristian König 	if (r < 0)
8381417beaSChristian König 		return r;
846dd09027SChristian König 
8559276f05SNirmoy Das 	pe += (unsigned long)amdgpu_bo_kptr(&vmbo->bo);
866dd09027SChristian König 
87eaad0c3aSChristian König 	trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags, p->immediate);
886dd09027SChristian König 
896dd09027SChristian König 	for (i = 0; i < count; i++) {
906dd09027SChristian König 		value = p->pages_addr ?
916dd09027SChristian König 			amdgpu_vm_map_gart(p->pages_addr, addr) :
926dd09027SChristian König 			addr;
936dd09027SChristian König 		amdgpu_gmc_set_pte_pde(p->adev, (void *)(uintptr_t)pe,
946dd09027SChristian König 				       i, value, flags);
956dd09027SChristian König 		addr += incr;
966dd09027SChristian König 	}
976dd09027SChristian König 	return 0;
986dd09027SChristian König }
996dd09027SChristian König 
1006dd09027SChristian König /**
1016dd09027SChristian König  * amdgpu_vm_cpu_commit - commit page table update to the HW
1026dd09027SChristian König  *
1036dd09027SChristian König  * @p: see amdgpu_vm_update_params definition
1046dd09027SChristian König  * @fence: unused
1056dd09027SChristian König  *
1066dd09027SChristian König  * Make sure that the hardware sees the page table updates.
1076dd09027SChristian König  */
amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params * p,struct dma_fence ** fence)1086dd09027SChristian König static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,
1096dd09027SChristian König 				struct dma_fence **fence)
1106dd09027SChristian König {
1116dd09027SChristian König 	/* Flush HDP */
1126dd09027SChristian König 	mb();
113810085ddSEric Huang 	amdgpu_device_flush_hdp(p->adev, NULL);
1146dd09027SChristian König 	return 0;
1156dd09027SChristian König }
1166dd09027SChristian König 
1176dd09027SChristian König const struct amdgpu_vm_update_funcs amdgpu_vm_cpu_funcs = {
118ecf96b52SChristian König 	.map_table = amdgpu_vm_cpu_map_table,
1196dd09027SChristian König 	.prepare = amdgpu_vm_cpu_prepare,
1206dd09027SChristian König 	.update = amdgpu_vm_cpu_update,
1216dd09027SChristian König 	.commit = amdgpu_vm_cpu_commit
1226dd09027SChristian König };
123