summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c
blob: 51cddfa3f1e8f5f42e6597c12dfb8e9916ce0145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-License-Identifier: GPL-2.0 OR MIT
/*
 * Copyright 2023 Advanced Micro Devices, Inc.
 *
 * 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.
 */

#include <linux/dma-fence.h>
#include <linux/workqueue.h>

#include "amdgpu.h"
#include "amdgpu_vm.h"
#include "amdgpu_gmc.h"

struct amdgpu_tlb_fence {
	struct dma_fence	base;
	struct amdgpu_device	*adev;
	struct dma_fence	*dependency;
	struct work_struct	work;
	spinlock_t		lock;
	uint16_t		pasid;

};

static const char *amdgpu_tlb_fence_get_driver_name(struct dma_fence *fence)
{
	return "amdgpu tlb fence";
}

static const char *amdgpu_tlb_fence_get_timeline_name(struct dma_fence *f)
{
	return "amdgpu tlb timeline";
}

static void amdgpu_tlb_fence_work(struct work_struct *work)
{
	struct amdgpu_tlb_fence *f = container_of(work, typeof(*f), work);
	int r;

	if (f->dependency) {
		dma_fence_wait(f->dependency, false);
		dma_fence_put(f->dependency);
		f->dependency = NULL;
	}

	r = amdgpu_gmc_flush_gpu_tlb_pasid(f->adev, f->pasid, 2, true, 0);
	if (r) {
		dev_err(f->adev->dev, "TLB flush failed for PASID %d.\n",
			f->pasid);
		dma_fence_set_error(&f->base, r);
	}

	dma_fence_signal(&f->base);
	dma_fence_put(&f->base);
}

static const struct dma_fence_ops amdgpu_tlb_fence_ops = {
	.use_64bit_seqno = true,
	.get_driver_name = amdgpu_tlb_fence_get_driver_name,
	.get_timeline_name = amdgpu_tlb_fence_get_timeline_name
};

void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
				struct dma_fence **fence)
{
	struct amdgpu_tlb_fence *f;

	f = kmalloc(sizeof(*f), GFP_KERNEL);
	if (!f) {
		/*
		 * We can't fail since the PDEs and PTEs are already updated, so
		 * just block for the dependency and execute the TLB flush
		 */
		if (*fence)
			dma_fence_wait(*fence, false);

		amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, 2, true, 0);
		*fence = dma_fence_get_stub();
		return;
	}

	f->adev = adev;
	f->dependency = *fence;
	f->pasid = vm->pasid;
	INIT_WORK(&f->work, amdgpu_tlb_fence_work);
	spin_lock_init(&f->lock);

	dma_fence_init(&f->base, &amdgpu_tlb_fence_ops, &f->lock,
		       vm->tlb_fence_context, atomic64_read(&vm->tlb_seq));

	/* TODO: We probably need a separate wq here */
	dma_fence_get(&f->base);
	schedule_work(&f->work);

	*fence = &f->base;
}