aboutsummaryrefslogtreecommitdiffstats
path: root/common/recipes-kernel/linux/linux-yocto-4.19.8/2773-drm-amdkfd-Update-wave-suspend-resume-API.patch
blob: eacca05fa1d626b788a21ce7234c153151f17506 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
From 25022c7181cd7fa79eda866f5698893793115f87 Mon Sep 17 00:00:00 2001
From: Philip Cox <Philip.Cox@amd.com>
Date: Wed, 10 Apr 2019 12:57:00 -0400
Subject: [PATCH 2773/2940] drm/amdkfd: Update wave suspend/resume API

This is updating to the new suspend and resume API for the
KFD and the thunk.

Change-Id: Ie534264e6299962a034f771fd55b0d4fd27f623c
Signed-off-by: Philip Cox <Philip.Cox@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 214 ++++++++++--------
 .../drm/amd/amdkfd/kfd_device_queue_manager.c |   5 +-
 include/uapi/linux/kfd_ioctl.h                |  35 ++-
 3 files changed, 140 insertions(+), 114 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 4de6eb6779d4..1822074c53eb 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -2553,16 +2553,20 @@ static int kfd_ioctl_dbg_set_debug_trap(struct file *filep,
 				struct kfd_process *p, void *data)
 {
 	struct kfd_ioctl_dbg_trap_args *args = data;
-	struct kfd_process_device *pdd;
+	struct kfd_process_device *pdd = NULL;
 	int r = 0;
 	struct kfd_dev *dev;
-	struct kfd_process *process = NULL;
+	struct kfd_process *target = NULL;
 	struct pid *pid = NULL;
+	uint32_t *queue_id_array = NULL;
 	uint32_t gpu_id;
 	uint32_t debug_trap_action;
 	uint32_t data1;
 	uint32_t data2;
 	uint32_t data3;
+	bool is_suspend_or_resume;
+	bool is_debbugger_attached = false;
+	uint8_t id;
 
 	debug_trap_action = args->op;
 	gpu_id = args->gpu_id;
@@ -2570,75 +2574,110 @@ static int kfd_ioctl_dbg_set_debug_trap(struct file *filep,
 	data2 = args->data2;
 	data3 = args->data3;
 
-	dev = kfd_device_by_id(args->gpu_id);
-	if (!dev)
-		return -EINVAL;
+	if (sched_policy == KFD_SCHED_POLICY_NO_HWS) {
+		pr_err("Unsupported sched_policy: %i", sched_policy);
+		r = -EINVAL;
+		goto out;
+	}
 
-	if (dev->device_info->asic_family < CHIP_VEGA10)
-		return -EINVAL;
+	is_suspend_or_resume =
+		debug_trap_action == KFD_IOC_DBG_TRAP_NODE_SUSPEND ||
+		debug_trap_action == KFD_IOC_DBG_TRAP_NODE_RESUME;
 
-	if (dev->mec_fw_version < 406) {
-		pr_err("Unsupported firmware version [%i]\n",
-				dev->mec_fw_version);
-		return -EINVAL;
+
+	pid = find_get_pid(args->pid);
+	if (!pid) {
+		pr_err("Cannot find pid info for %i\n",
+				args->pid);
+		r =  -ESRCH;
+		goto out;
 	}
 
-	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
-		pr_err("Unsupported sched_policy: %i", dev->dqm->sched_policy);
-		return -EINVAL;
+	target = kfd_lookup_process_by_pid(pid);
+	if (!target) {
+		pr_err("Cannot find process info info for %i\n",
+				args->pid);
+		r = -ESRCH;
+		goto out;
 	}
 
-	mutex_lock(&p->mutex);
 
-	if (debug_trap_action == KFD_IOC_DBG_TRAP_NODE_SUSPEND ||
-		debug_trap_action == KFD_IOC_DBG_TRAP_NODE_RESUME) {
+	rcu_read_lock();
+	if (ptrace_parent(target->lead_thread) == current)
+		is_debbugger_attached = true;
+	rcu_read_unlock();
 
-		pid = find_get_pid(data1);
-		if (!pid) {
-			pr_err("Cannot find pid info for %i\n", data1);
-			r = -ESRCH;
+	if (!is_debbugger_attached) {
+		pr_err("Cannot debug process\n");
+		r = -ESRCH;
+		goto out;
+	}
+
+	mutex_lock(&target->mutex);
+
+	if (!is_suspend_or_resume) {
+
+		dev = kfd_device_by_id(args->gpu_id);
+		if (!dev) {
+			r = -EINVAL;
 			goto unlock_out;
 		}
 
-		process = kfd_lookup_process_by_pid(pid);
-		if (!process) {
-			pr_err("Cannot find process info info for %i\n", data1);
-			r = -ESRCH;
+		if (dev->device_info->asic_family < CHIP_VEGA10) {
+			r = -EINVAL;
 			goto unlock_out;
 		}
-		pdd = kfd_get_process_device_data(dev, process);
-	} else {
-		pdd = kfd_get_process_device_data(dev, p);
-	}
-	if (!pdd) {
-		r = -EINVAL;
-		goto unlock_out;
-	}
 
-	if ((pdd->is_debugging_enabled == false) &&
-		((debug_trap_action == KFD_IOC_DBG_TRAP_ENABLE &&
-			  data1 == 1) ||
-		 (debug_trap_action ==
-				 KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE &&
-			 data1 != 0))) {
+		pdd = kfd_get_process_device_data(dev, target);
 
-		/* We need to reserve the debug trap vmid if we haven't yet, and
-		 * are enabling trap debugging, or we are setting the wave
-		 * launch mode to something other than normal==0.
-		 */
-		r = reserve_debug_trap_vmid(dev->dqm);
-		if (r)
+		if (!pdd) {
+			r = -EINVAL;
 			goto unlock_out;
+		}
 
-		pdd->is_debugging_enabled = true;
-	}
+		if ((pdd->is_debugging_enabled == false) &&
+				((debug_trap_action == KFD_IOC_DBG_TRAP_ENABLE
+				  && data1 == 1) ||
+				 (debug_trap_action ==
+				  KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE &&
+				  data1 != 0))) {
+
+			/* We need to reserve the debug trap vmid if we haven't
+			 * yet, and are enabling trap debugging, or we are
+			 * setting the wave launch mode to something other than
+			 * normal==0.
+			 */
+			r = reserve_debug_trap_vmid(dev->dqm);
+			if (r)
+				goto unlock_out;
+
+			pdd->is_debugging_enabled = true;
+		}
 
-	if (!pdd->is_debugging_enabled) {
-		pr_err("Debugging is not enabled for this device\n");
-		r = -EINVAL;
-		goto unlock_out;
+		if (!pdd->is_debugging_enabled) {
+			pr_err("Debugging is not enabled for this device\n");
+			r = -EINVAL;
+			goto unlock_out;
+		}
+	} else {
+		/* data 2 has the number of queue IDs */
+		size_t queue_id_array_size = sizeof(uint32_t) * data2;
+
+		queue_id_array = kzalloc(queue_id_array_size, GFP_KERNEL);
+		if (!queue_id_array) {
+			r = -ENOMEM;
+			goto unlock_out;
+		}
+		/* We need to copy the queue IDs from userspace */
+		if (copy_from_user(queue_id_array,
+					(uint32_t *) args->ptr,
+					queue_id_array_size)) {
+			r = -EFAULT;
+			goto unlock_out;
+		}
 	}
 
+
 	switch (debug_trap_action) {
 	case KFD_IOC_DBG_TRAP_ENABLE:
 		switch (data1) {
@@ -2680,43 +2719,37 @@ static int kfd_ioctl_dbg_set_debug_trap(struct file *filep,
 				dev->vm_info.last_vmid_kfd);
 		break;
 	case KFD_IOC_DBG_TRAP_NODE_SUSPEND:
-		/*
-		 * To suspend/resume queues, we need:
-		 *  ptrace to be enabled,
-		 *         process->lead_thread->ptrace == true
-		 *  and we need either:
-		 *  i) be allowed to trace the process
-		 *			process->lead_thread->parent == current
-		 *  ii) or to be ptrace'ing ourself
-		 *		 process->lead_thread == current
-		 */
-		if (process->lead_thread->ptrace &&
-				(process->lead_thread->parent == current ||
-				 process->lead_thread == current)) {
-			r = suspend_queues(dev->dqm, process, data3);
-		} else {
-			pr_err("Cannot debug process to suspend queues\n");
-			r = -ESRCH;
+		id = 0;
+		/* We need to loop over all of the topology devices */
+		while (kfd_topology_enum_kfd_devices(id, &dev) == 0) {
+			if (!dev) {
+				/* Not a GPU.  Skip it */
+				id++;
+				continue;
+			}
+
+			r = suspend_queues(dev->dqm, target, data1);
+			if (r)
+				goto unlock_out;
+
+			id++;
 		}
 		break;
 	case KFD_IOC_DBG_TRAP_NODE_RESUME:
-		/*
-		 * To suspend/resume queues, we need:
-		 *  ptrace to be enabled,
-		 *         process->lead_thread->ptrace == true
-		 *  and we need either:
-		 *  i) be allowed to trace the process
-		 *			process->lead_thread->parent == current
-		 *  ii) or to be ptrace'ing ourself
-		 *		 process->lead_thread == current
-		 */
-		if (process->lead_thread->ptrace &&
-				(process->lead_thread->parent == current ||
-				 process->lead_thread == current)) {
-			r = resume_queues(dev->dqm, process);
-		} else {
-			pr_err("Cannot debug process to resume queues\n");
-			r = -ESRCH;
+		id = 0;
+		/* We need to loop over all of the topology devices */
+		while (kfd_topology_enum_kfd_devices(id, &dev) == 0) {
+			if (!dev) {
+				/* Not a GPU.  Skip it */
+				id++;
+				continue;
+			}
+
+			r = resume_queues(dev->dqm, target);
+			if (r)
+				goto unlock_out;
+
+			id++;
 		}
 		break;
 	default:
@@ -2724,7 +2757,7 @@ static int kfd_ioctl_dbg_set_debug_trap(struct file *filep,
 		r = -EINVAL;
 	}
 
-	if (pdd->trap_debug_wave_launch_mode == 0 &&
+	if (pdd && pdd->trap_debug_wave_launch_mode == 0 &&
 			!pdd->debug_trap_enabled) {
 		int result;
 
@@ -2739,11 +2772,14 @@ static int kfd_ioctl_dbg_set_debug_trap(struct file *filep,
 	}
 
 unlock_out:
+	mutex_unlock(&target->mutex);
+
+out:
 	if (pid)
 		put_pid(pid);
-	if (process)
-		kfd_unref_process(process);
-	mutex_unlock(&p->mutex);
+	if (target)
+		kfd_unref_process(target);
+	kfree(queue_id_array);
 	return r;
 }
 
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
index d5d94f6e60ab..d0c316b91af0 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
@@ -2062,11 +2062,8 @@ int suspend_queues(struct device_queue_manager *dqm,
 		}
 	}
 
-	/* Memory Fence */
-	if (!r && flags & KFD__DBG_NODE_SUSPEND_MEMORY_FENCE)
-		amdgpu_amdkfd_debug_mem_fence(dev->kgd);
-
 	if (queues_suspended) {
+		amdgpu_amdkfd_debug_mem_fence(dev->kgd);
 		flush_work(&copy_context_worker.copy_context_work);
 		mmput(copy_context_worker.mm);
 		destroy_work_on_stack(&copy_context_worker.copy_context_work);
diff --git a/include/uapi/linux/kfd_ioctl.h b/include/uapi/linux/kfd_ioctl.h
index f273d05708b6..8f19d8524b4f 100644
--- a/include/uapi/linux/kfd_ioctl.h
+++ b/include/uapi/linux/kfd_ioctl.h
@@ -188,68 +188,61 @@ struct kfd_ioctl_dbg_wave_control_args {
 };
 
 /* KFD_IOC_DBG_TRAP_ENABLE:
+ * ptr:   unused
  * data1: 0=disable, 1=enable
  * data2: queue ID (for future use)
  * data3: unused
- * data4: unused
  */
 #define KFD_IOC_DBG_TRAP_ENABLE 0
 
 /* KFD_IOC_DBG_TRAP_SET_TRAP_DATA:
+ * ptr:   unused
  * data1: SPI_GDBG_TRAP_DATA0
  * data2: SPI_GDBG_TRAP_DATA1
  * data3: unused
- * data4: unused
  */
 #define KFD_IOC_DBG_TRAP_SET_TRAP_DATA 1
 
 /* KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_OVERRIDE:
+ * ptr:   unused
  * data1: override mode: 0=OR, 1=REPLACE
  * data2: mask
  * data3: unused
- * data4: unused
  */
 #define KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_OVERRIDE 2
 
 /* KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE:
+ * ptr:   unused
  * data1: 0=normal, 1=halt, 2=kill, 3=singlestep, 4=disable
  * data2: unused
  * data3: unused
- * data4: unused
  */
 #define KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE 3
 
-
-#define KFD__DBG_NODE_SUSPEND_NO_GRACE		0x01
-#define KFD__DBG_NODE_SUSPEND_MEMORY_FENCE	0x02
-#define KFD__DBG_NODE_SUSPEND_UPDATE_CONTEXT	0x04
 /* KFD_IOC_DBG_TRAP_NODE_SUSPEND:
- * data1: pid
- * data2: nodeid
- * data3: flags : KFD__DBG_NODE_SUSPEND_NO_GRACE
- *                KFD__DBG_NODE_SUSPEND_MEMORY_FENCE
- *                KFD__DBG_NODE_SUSPEND_UPDATE_CONTEXT
- * data4: unused
+ * ptr:   pointer to an array of Queues IDs
+ * data1: flags
+ * data2: number of queues
+ * data3: grace period
  */
 #define KFD_IOC_DBG_TRAP_NODE_SUSPEND 4
 
 /* KFD_IOC_DBG_TRAP_NODE_RESUME:
- * data1: pid
- * data2: nodeid
- * data3: flags : KFD__DBG_NODE_SUSPEND_NO_GRACE
- *                KFD__DBG_NODE_SUSPEND_MEMORY_FENCE
- *                KFD__DBG_NODE_SUSPEND_UPDATE_CONTEXT
- * data4: unused
+ * ptr:   pointer to an array of Queues IDs
+ * data1: flags
+ * data2: number of queues
+ * data3: unused
  */
 #define KFD_IOC_DBG_TRAP_NODE_RESUME 5
 
 struct kfd_ioctl_dbg_trap_args {
+	__u64 ptr;     /* to KFD -- used for pointer arguments: queue arrays */
+	__u32 pid;     /* to KFD */
 	__u32 gpu_id;  /* to KFD */
 	__u32 op;      /* to KFD */
 	__u32 data1;   /* to KFD */
 	__u32 data2;   /* to KFD */
 	__u32 data3;   /* to KFD */
-	__u32 data4;   /* to KFD */
 };
 
 /* Matching HSA_EVENTTYPE */
-- 
2.17.1