aboutsummaryrefslogtreecommitdiffstats
path: root/meta-amd-bsp/recipes-kernel/linux/linux-yocto-4.14.71/1278-drm-amdkfd-Implement-cleaned-up-memory-management-AP.patch
blob: fdbe48cca26696ece328cc0f4da6ce39bd368c16 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
From de5850d766e064241f688f7e36d369d897bb6862 Mon Sep 17 00:00:00 2001
From: Felix Kuehling <Felix.Kuehling@amd.com>
Date: Mon, 1 May 2017 16:59:20 -0400
Subject: [PATCH 1278/4131] drm/amdkfd: Implement cleaned-up memory management
 APIs

The _new APIs are still available as wrappers that adapt between
the original and cleaned up argument structures. Once the Thunk has
transitioned to the cleaned-up APIs, the wrappers can be removed.

Change-Id: If26ff74d8f2cc6cda27172d6816f8b22f9c65a3b
Signed-off-by: Felix Kuehling <Felix.Kuehling@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 152 ++++++++++++++-----------------
 1 file changed, 68 insertions(+), 84 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index b1c5750..521a8d2 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1174,52 +1174,6 @@ static int kfd_ioctl_alloc_scratch_memory(struct file *filep,
 	return -EFAULT;
 }
 
-static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
-					struct kfd_process *p, void *data)
-{
-	struct kfd_ioctl_alloc_memory_of_gpu_args *args = data;
-	struct kfd_process_device *pdd;
-	void *mem;
-	struct kfd_dev *dev;
-	int idr_handle;
-	long err;
-
-	if (args->size == 0)
-		return -EINVAL;
-
-	dev = kfd_device_by_id(args->gpu_id);
-	if (dev == NULL)
-		return -EINVAL;
-
-	down_write(&p->lock);
-	pdd = kfd_bind_process_to_device(dev, p);
-	up_write(&p->lock);
-	if (IS_ERR(pdd))
-		return PTR_ERR(pdd);
-
-	err = dev->kfd2kgd->alloc_memory_of_gpu(
-		dev->kgd, args->va_addr, args->size,
-		pdd->vm, (struct kgd_mem **) &mem, NULL, NULL, 0);
-
-	if (err != 0)
-		return err;
-
-	down_write(&p->lock);
-	idr_handle = kfd_process_device_create_obj_handle(pdd, mem,
-			args->va_addr, args->size, NULL);
-	up_write(&p->lock);
-	if (idr_handle < 0) {
-		dev->kfd2kgd->free_memory_of_gpu(dev->kgd,
-						 (struct kgd_mem *) mem,
-						 pdd->vm);
-		return -EFAULT;
-	}
-
-	args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
-
-	return 0;
-}
-
 bool kfd_is_large_bar(struct kfd_dev *dev)
 {
 	struct kfd_local_mem_info mem_info;
@@ -1239,6 +1193,8 @@ bool kfd_is_large_bar(struct kfd_dev *dev)
 	return false;
 }
 
+/* TODO: Remove this once the Thunk has transitioned to
+ * AMDKFD_IOC_ALLOC_MEMORY_OF_GPU */
 static uint32_t kfd_convert_user_mem_alloction_flags(
 		struct kfd_dev *dev,
 		uint32_t userspace_flags)
@@ -1305,17 +1261,16 @@ static uint32_t kfd_convert_user_mem_alloction_flags(
 	return kernel_allocation_flags;
 }
 
-static int kfd_ioctl_alloc_memory_of_gpu_new(struct file *filep,
+static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
 					struct kfd_process *p, void *data)
 {
-	struct kfd_ioctl_alloc_memory_of_gpu_new_args *args = data;
+	struct kfd_ioctl_alloc_memory_of_gpu_args *args = data;
 	struct kfd_process_device *pdd;
 	void *mem;
 	struct kfd_dev *dev;
 	int idr_handle;
 	long err;
 	uint64_t offset;
-	uint32_t alloc_flags = 0;
 
 	if (args->size == 0)
 		return -EINVAL;
@@ -1330,19 +1285,17 @@ static int kfd_ioctl_alloc_memory_of_gpu_new(struct file *filep,
 	if (IS_ERR(pdd))
 		return PTR_ERR(pdd);
 
-	if (args->flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL_OLD) {
+	if (args->flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL) {
 		if (args->size != kfd_doorbell_process_slice(dev))
 			return -EINVAL;
 		offset = kfd_get_process_doorbells(dev, p);
 	} else
 		offset = args->mmap_offset;
 
-	alloc_flags = kfd_convert_user_mem_alloction_flags(dev, args->flags);
-
 	err = dev->kfd2kgd->alloc_memory_of_gpu(
 		dev->kgd, args->va_addr, args->size,
 		pdd->vm, (struct kgd_mem **) &mem, &offset,
-		NULL, alloc_flags);
+		NULL, args->flags);
 
 	if (err != 0)
 		return err;
@@ -1359,7 +1312,7 @@ static int kfd_ioctl_alloc_memory_of_gpu_new(struct file *filep,
 	}
 
 	args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
-	if ((args->flags & KFD_IOC_ALLOC_MEM_FLAGS_DGPU_DEVICE) != 0 &&
+	if ((args->flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) != 0 &&
 			!kfd_is_large_bar(dev)) {
 		args->mmap_offset = 0;
 	} else {
@@ -1372,6 +1325,40 @@ static int kfd_ioctl_alloc_memory_of_gpu_new(struct file *filep,
 	return 0;
 }
 
+/* TODO: Remove this once the Thunk has transitioned to
+ * AMDKFD_IOC_ALLOC_MEMORY_OF_GPU */
+static int kfd_ioctl_alloc_memory_of_gpu_new(struct file *filep,
+					struct kfd_process *p, void *data)
+{
+	struct kfd_ioctl_alloc_memory_of_gpu_new_args *new_args = data;
+	struct kfd_ioctl_alloc_memory_of_gpu_args args;
+	struct kfd_dev *dev;
+	uint32_t flags;
+	int ret;
+
+	dev = kfd_device_by_id(new_args->gpu_id);
+	if (dev == NULL)
+		return -EINVAL;
+	/* New ioctl flags match kfd2kgd API */
+	flags = kfd_convert_user_mem_alloction_flags(dev, new_args->flags);
+
+	/* copy input arguments, adjusting the ABI */
+	args.va_addr = new_args->va_addr;
+	args.size = new_args->size;
+	args.handle = new_args->handle;
+	args.mmap_offset = new_args->mmap_offset;
+	args.gpu_id = new_args->gpu_id;
+	args.flags = flags;
+
+	ret = kfd_ioctl_alloc_memory_of_gpu(filep, p, &args);
+
+	/* copy output arguments */
+	new_args->handle = args.handle;
+	new_args->mmap_offset = args.mmap_offset;
+
+	return ret;
+}
+
 static int kfd_ioctl_free_memory_of_gpu(struct file *filep,
 					struct kfd_process *p, void *data)
 {
@@ -1443,7 +1430,7 @@ int kfd_map_memory_to_gpu(void *mem, struct kfd_process_device *pdd)
 static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
 					struct kfd_process *p, void *data)
 {
-	struct kfd_ioctl_map_memory_to_gpu_new_args *args = data;
+	struct kfd_ioctl_map_memory_to_gpu_args *args = data;
 	struct kfd_process_device *pdd, *peer_pdd;
 	void *mem;
 	struct kfd_dev *dev, *peer;
@@ -1468,7 +1455,7 @@ static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
 			return -ENOMEM;
 
 		err = copy_from_user(devices_arr,
-				(void __user *)args->device_ids_array,
+				(void __user *)args->device_ids_array_ptr,
 				args->device_ids_array_size);
 		if (err != 0) {
 			err = -EFAULT;
@@ -1533,17 +1520,19 @@ static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
 	return err;
 }
 
-static int kfd_ioctl_map_memory_to_gpu_wrapper(struct file *filep,
+/* TODO: Remove this once the Thunk has transitioned to
+ * AMDKFD_IOC_MAP_MEMORY_TO_GPU */
+static int kfd_ioctl_map_memory_to_gpu_new(struct file *filep,
 					struct kfd_process *p, void *data)
 {
-	struct kfd_ioctl_map_memory_to_gpu_args *args = data;
-	struct kfd_ioctl_map_memory_to_gpu_new_args new_args;
+	struct kfd_ioctl_map_memory_to_gpu_new_args *new_args = data;
+	struct kfd_ioctl_map_memory_to_gpu_args args;
 
-	new_args.handle = args->handle;
-	new_args.device_ids_array = NULL;
-	new_args.device_ids_array_size = 0;
+	args.handle = new_args->handle;
+	args.device_ids_array_ptr = (uint64_t)new_args->device_ids_array;
+	args.device_ids_array_size = new_args->device_ids_array_size;
 
-	return kfd_ioctl_map_memory_to_gpu(filep, p, &new_args);
+	return kfd_ioctl_map_memory_to_gpu(filep, p, &args);
 }
 
 int kfd_unmap_memory_from_gpu(void *mem, struct kfd_process_device *pdd)
@@ -1565,7 +1554,7 @@ int kfd_unmap_memory_from_gpu(void *mem, struct kfd_process_device *pdd)
 static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 					struct kfd_process *p, void *data)
 {
-	struct kfd_ioctl_unmap_memory_from_gpu_new_args *args = data;
+	struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
 	struct kfd_process_device *pdd, *peer_pdd;
 	void *mem;
 	struct kfd_dev *dev, *peer;
@@ -1589,7 +1578,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 			return -ENOMEM;
 
 		err = copy_from_user(devices_arr,
-				(void __user *)args->device_ids_array,
+				(void __user *)args->device_ids_array_ptr,
 				args->device_ids_array_size);
 		if (err != 0) {
 			err = -EFAULT;
@@ -1646,17 +1635,19 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 	return err;
 }
 
-static int kfd_ioctl_unmap_memory_from_gpu_wrapper(struct file *filep,
+/* TODO: Remove this once the Thunk has transitioned to
+ * AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU */
+static int kfd_ioctl_unmap_memory_from_gpu_new(struct file *filep,
 					struct kfd_process *p, void *data)
 {
-	struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
-	struct kfd_ioctl_unmap_memory_from_gpu_new_args new_args;
+	struct kfd_ioctl_unmap_memory_from_gpu_new_args *new_args = data;
+	struct kfd_ioctl_unmap_memory_from_gpu_args args;
 
-	new_args.handle = args->handle;
-	new_args.device_ids_array = NULL;
-	new_args.device_ids_array_size = 0;
+	args.handle = new_args->handle;
+	args.device_ids_array_ptr = (uint64_t)new_args->device_ids_array;
+	args.device_ids_array_size = new_args->device_ids_array_size;
 
-	return kfd_ioctl_unmap_memory_from_gpu(filep, p, &new_args);
+	return kfd_ioctl_unmap_memory_from_gpu(filep, p, &args);
 }
 
 static int kfd_ioctl_open_graphic_handle(struct file *filep,
@@ -1781,14 +1772,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file *filep,
 		goto exit;
 	}
 	args->gpu_id = kfd_get_gpu_id(dev);
-
-	/* Translate flags */
-	if (flags & ALLOC_MEM_FLAGS_VRAM) {
-		args->flags = KFD_IS_DGPU(dev->device_info->asic_family) ?
-			KFD_IOC_ALLOC_MEM_FLAGS_DGPU_DEVICE :
-			KFD_IOC_ALLOC_MEM_FLAGS_APU_DEVICE;
-	} else
-		args->flags = KFD_IOC_ALLOC_MEM_FLAGS_DGPU_HOST;
+	args->flags = flags;
 
 	/* Copy metadata buffer to user mode */
 	if (metadata_buffer) {
@@ -2213,10 +2197,10 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
 			kfd_ioctl_free_memory_of_gpu, 0),
 
 	AMDKFD_IOCTL_DEF(AMDKFD_IOC_MAP_MEMORY_TO_GPU,
-			kfd_ioctl_map_memory_to_gpu_wrapper, 0),
+			kfd_ioctl_map_memory_to_gpu, 0),
 
 	AMDKFD_IOCTL_DEF(AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU,
-			kfd_ioctl_unmap_memory_from_gpu_wrapper, 0),
+			kfd_ioctl_unmap_memory_from_gpu, 0),
 
 	AMDKFD_IOCTL_DEF(AMDKFD_IOC_OPEN_GRAPHIC_HANDLE,
 			kfd_ioctl_open_graphic_handle, 0),
@@ -2237,10 +2221,10 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
 				kfd_ioctl_alloc_memory_of_gpu_new, 0),
 
 	AMDKFD_IOCTL_DEF(AMDKFD_IOC_MAP_MEMORY_TO_GPU_NEW,
-				kfd_ioctl_map_memory_to_gpu, 0),
+				kfd_ioctl_map_memory_to_gpu_new, 0),
 
 	AMDKFD_IOCTL_DEF(AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU_NEW,
-				kfd_ioctl_unmap_memory_from_gpu, 0),
+				kfd_ioctl_unmap_memory_from_gpu_new, 0),
 
 	AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES_NEW,
 				kfd_ioctl_get_process_apertures_new, 0),
-- 
2.7.4