1 /*	$NetBSD: kfd_dbgmgr.c,v 1.3 2021/12/18 23:44:59 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2014 Advanced Micro Devices, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: kfd_dbgmgr.c,v 1.3 2021/12/18 23:44:59 riastradh Exp $");
27 
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/log2.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/device.h>
34 
35 #include "kfd_priv.h"
36 #include "cik_regs.h"
37 #include "kfd_pm4_headers.h"
38 #include "kfd_pm4_headers_diq.h"
39 #include "kfd_dbgmgr.h"
40 #include "kfd_dbgdev.h"
41 #include "kfd_device_queue_manager.h"
42 
43 static DEFINE_MUTEX(kfd_dbgmgr_mutex);
44 
kfd_get_dbgmgr_mutex(void)45 struct mutex *kfd_get_dbgmgr_mutex(void)
46 {
47 	return &kfd_dbgmgr_mutex;
48 }
49 
50 
kfd_dbgmgr_uninitialize(struct kfd_dbgmgr * pmgr)51 static void kfd_dbgmgr_uninitialize(struct kfd_dbgmgr *pmgr)
52 {
53 	kfree(pmgr->dbgdev);
54 
55 	pmgr->dbgdev = NULL;
56 	pmgr->pasid = 0;
57 	pmgr->dev = NULL;
58 }
59 
kfd_dbgmgr_destroy(struct kfd_dbgmgr * pmgr)60 void kfd_dbgmgr_destroy(struct kfd_dbgmgr *pmgr)
61 {
62 	if (pmgr) {
63 		kfd_dbgmgr_uninitialize(pmgr);
64 		kfree(pmgr);
65 	}
66 }
67 
kfd_dbgmgr_create(struct kfd_dbgmgr ** ppmgr,struct kfd_dev * pdev)68 bool kfd_dbgmgr_create(struct kfd_dbgmgr **ppmgr, struct kfd_dev *pdev)
69 {
70 	enum DBGDEV_TYPE type = DBGDEV_TYPE_DIQ;
71 	struct kfd_dbgmgr *new_buff;
72 
73 	if (WARN_ON(!pdev->init_complete))
74 		return false;
75 
76 	new_buff = kfd_alloc_struct(new_buff);
77 	if (!new_buff) {
78 		pr_err("Failed to allocate dbgmgr instance\n");
79 		return false;
80 	}
81 
82 	new_buff->pasid = 0;
83 	new_buff->dev = pdev;
84 	new_buff->dbgdev = kfd_alloc_struct(new_buff->dbgdev);
85 	if (!new_buff->dbgdev) {
86 		pr_err("Failed to allocate dbgdev instance\n");
87 		kfree(new_buff);
88 		return false;
89 	}
90 
91 	/* get actual type of DBGDevice cpsch or not */
92 	if (pdev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
93 		type = DBGDEV_TYPE_NODIQ;
94 
95 	kfd_dbgdev_init(new_buff->dbgdev, pdev, type);
96 	*ppmgr = new_buff;
97 
98 	return true;
99 }
100 
kfd_dbgmgr_register(struct kfd_dbgmgr * pmgr,struct kfd_process * p)101 long kfd_dbgmgr_register(struct kfd_dbgmgr *pmgr, struct kfd_process *p)
102 {
103 	if (pmgr->pasid != 0) {
104 		pr_debug("H/W debugger is already active using pasid 0x%x\n",
105 				pmgr->pasid);
106 		return -EBUSY;
107 	}
108 
109 	/* remember pasid */
110 	pmgr->pasid = p->pasid;
111 
112 	/* provide the pqm for diq generation */
113 	pmgr->dbgdev->pqm = &p->pqm;
114 
115 	/* activate the actual registering */
116 	pmgr->dbgdev->dbgdev_register(pmgr->dbgdev);
117 
118 	return 0;
119 }
120 
kfd_dbgmgr_unregister(struct kfd_dbgmgr * pmgr,struct kfd_process * p)121 long kfd_dbgmgr_unregister(struct kfd_dbgmgr *pmgr, struct kfd_process *p)
122 {
123 	/* Is the requests coming from the already registered process? */
124 	if (pmgr->pasid != p->pasid) {
125 		pr_debug("H/W debugger is not registered by calling pasid 0x%x\n",
126 				p->pasid);
127 		return -EINVAL;
128 	}
129 
130 	pmgr->dbgdev->dbgdev_unregister(pmgr->dbgdev);
131 
132 	pmgr->pasid = 0;
133 
134 	return 0;
135 }
136 
kfd_dbgmgr_wave_control(struct kfd_dbgmgr * pmgr,struct dbg_wave_control_info * wac_info)137 long kfd_dbgmgr_wave_control(struct kfd_dbgmgr *pmgr,
138 				struct dbg_wave_control_info *wac_info)
139 {
140 	/* Is the requests coming from the already registered process? */
141 	if (pmgr->pasid != wac_info->process->pasid) {
142 		pr_debug("H/W debugger support was not registered for requester pasid 0x%x\n",
143 				wac_info->process->pasid);
144 		return -EINVAL;
145 	}
146 
147 	return (long) pmgr->dbgdev->dbgdev_wave_control(pmgr->dbgdev, wac_info);
148 }
149 
kfd_dbgmgr_address_watch(struct kfd_dbgmgr * pmgr,struct dbg_address_watch_info * adw_info)150 long kfd_dbgmgr_address_watch(struct kfd_dbgmgr *pmgr,
151 				struct dbg_address_watch_info *adw_info)
152 {
153 	/* Is the requests coming from the already registered process? */
154 	if (pmgr->pasid != adw_info->process->pasid) {
155 		pr_debug("H/W debugger support was not registered for requester pasid 0x%x\n",
156 				adw_info->process->pasid);
157 		return -EINVAL;
158 	}
159 
160 	return (long) pmgr->dbgdev->dbgdev_address_watch(pmgr->dbgdev,
161 							adw_info);
162 }
163 
164