1184610Salfred/*	$NetBSD: kfd_dbgmgr.c,v 1.3 2021/12/18 23:44:59 riastradh Exp $	*/
2184610Salfred
3184610Salfred/*
4184610Salfred * Copyright 2014 Advanced Micro Devices, Inc.
5184610Salfred *
6184610Salfred * Permission is hereby granted, free of charge, to any person obtaining a
7184610Salfred * copy of this software and associated documentation files (the "Software"),
8184610Salfred * to deal in the Software without restriction, including without limitation
9184610Salfred * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10184610Salfred * and/or sell copies of the Software, and to permit persons to whom the
11184610Salfred * Software is furnished to do so, subject to the following conditions:
12184610Salfred *
13184610Salfred * The above copyright notice and this permission notice shall be included in
14184610Salfred * all copies or substantial portions of the Software.
15184610Salfred *
16184610Salfred * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17184610Salfred * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18184610Salfred * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19184610Salfred * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20184610Salfred * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21184610Salfred * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22184610Salfred * OTHER DEALINGS IN THE SOFTWARE.
23184610Salfred *
24184610Salfred */
25184610Salfred#include <sys/cdefs.h>
26184610Salfred__KERNEL_RCSID(0, "$NetBSD: kfd_dbgmgr.c,v 1.3 2021/12/18 23:44:59 riastradh Exp $");
27184610Salfred
28184610Salfred#include <linux/types.h>
29184610Salfred#include <linux/kernel.h>
30184610Salfred#include <linux/log2.h>
31184610Salfred#include <linux/sched.h>
32184610Salfred#include <linux/slab.h>
33184610Salfred#include <linux/device.h>
34184610Salfred
35184610Salfred#include "kfd_priv.h"
36184610Salfred#include "cik_regs.h"
37184610Salfred#include "kfd_pm4_headers.h"
38184610Salfred#include "kfd_pm4_headers_diq.h"
39184610Salfred#include "kfd_dbgmgr.h"
40194677Sthompsa#include "kfd_dbgdev.h"
41194677Sthompsa#include "kfd_device_queue_manager.h"
42194677Sthompsa
43194677Sthompsastatic DEFINE_MUTEX(kfd_dbgmgr_mutex);
44194677Sthompsa
45194677Sthompsastruct mutex *kfd_get_dbgmgr_mutex(void)
46194677Sthompsa{
47194677Sthompsa	return &kfd_dbgmgr_mutex;
48194677Sthompsa}
49194677Sthompsa
50194677Sthompsa
51194677Sthompsastatic void kfd_dbgmgr_uninitialize(struct kfd_dbgmgr *pmgr)
52194677Sthompsa{
53194677Sthompsa	kfree(pmgr->dbgdev);
54194677Sthompsa
55194677Sthompsa	pmgr->dbgdev = NULL;
56194677Sthompsa	pmgr->pasid = 0;
57194677Sthompsa	pmgr->dev = NULL;
58194677Sthompsa}
59194677Sthompsa
60194677Sthompsavoid kfd_dbgmgr_destroy(struct kfd_dbgmgr *pmgr)
61194677Sthompsa{
62194677Sthompsa	if (pmgr) {
63188746Sthompsa		kfd_dbgmgr_uninitialize(pmgr);
64184610Salfred		kfree(pmgr);
65184610Salfred	}
66188942Sthompsa}
67188942Sthompsa
68184610Salfredbool kfd_dbgmgr_create(struct kfd_dbgmgr **ppmgr, struct kfd_dev *pdev)
69188942Sthompsa{
70184610Salfred	enum DBGDEV_TYPE type = DBGDEV_TYPE_DIQ;
71207077Sthompsa	struct kfd_dbgmgr *new_buff;
72184610Salfred
73184610Salfred	if (WARN_ON(!pdev->init_complete))
74192502Sthompsa		return false;
75192502Sthompsa
76184610Salfred	new_buff = kfd_alloc_struct(new_buff);
77184610Salfred	if (!new_buff) {
78184610Salfred		pr_err("Failed to allocate dbgmgr instance\n");
79184610Salfred		return false;
80184610Salfred	}
81184610Salfred
82184610Salfred	new_buff->pasid = 0;
83184610Salfred	new_buff->dev = pdev;
84184610Salfred	new_buff->dbgdev = kfd_alloc_struct(new_buff->dbgdev);
85184610Salfred	if (!new_buff->dbgdev) {
86184610Salfred		pr_err("Failed to allocate dbgdev instance\n");
87184610Salfred		kfree(new_buff);
88184610Salfred		return false;
89184610Salfred	}
90184610Salfred
91184610Salfred	/* get actual type of DBGDevice cpsch or not */
92184610Salfred	if (pdev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
93184610Salfred		type = DBGDEV_TYPE_NODIQ;
94184610Salfred
95184610Salfred	kfd_dbgdev_init(new_buff->dbgdev, pdev, type);
96184610Salfred	*ppmgr = new_buff;
97184610Salfred
98184610Salfred	return true;
99184610Salfred}
100184610Salfred
101184610Salfredlong kfd_dbgmgr_register(struct kfd_dbgmgr *pmgr, struct kfd_process *p)
102184610Salfred{
103184610Salfred	if (pmgr->pasid != 0) {
104184610Salfred		pr_debug("H/W debugger is already active using pasid 0x%x\n",
105184610Salfred				pmgr->pasid);
106184610Salfred		return -EBUSY;
107184610Salfred	}
108184610Salfred
109184610Salfred	/* remember pasid */
110184610Salfred	pmgr->pasid = p->pasid;
111184610Salfred
112184610Salfred	/* provide the pqm for diq generation */
113184610Salfred	pmgr->dbgdev->pqm = &p->pqm;
114184610Salfred
115184610Salfred	/* activate the actual registering */
116184610Salfred	pmgr->dbgdev->dbgdev_register(pmgr->dbgdev);
117184610Salfred
118184610Salfred	return 0;
119184610Salfred}
120184610Salfred
121184610Salfredlong kfd_dbgmgr_unregister(struct kfd_dbgmgr *pmgr, struct kfd_process *p)
122184610Salfred{
123184610Salfred	/* Is the requests coming from the already registered process? */
124184610Salfred	if (pmgr->pasid != p->pasid) {
125184610Salfred		pr_debug("H/W debugger is not registered by calling pasid 0x%x\n",
126184610Salfred				p->pasid);
127184610Salfred		return -EINVAL;
128184610Salfred	}
129184610Salfred
130184610Salfred	pmgr->dbgdev->dbgdev_unregister(pmgr->dbgdev);
131184610Salfred
132184610Salfred	pmgr->pasid = 0;
133184610Salfred
134184610Salfred	return 0;
135184610Salfred}
136184610Salfred
137184610Salfredlong kfd_dbgmgr_wave_control(struct kfd_dbgmgr *pmgr,
138187259Sthompsa				struct dbg_wave_control_info *wac_info)
139187259Sthompsa{
140187259Sthompsa	/* Is the requests coming from the already registered process? */
141187259Sthompsa	if (pmgr->pasid != wac_info->process->pasid) {
142188413Sthompsa		pr_debug("H/W debugger support was not registered for requester pasid 0x%x\n",
143187259Sthompsa				wac_info->process->pasid);
144184610Salfred		return -EINVAL;
145184610Salfred	}
146192984Sthompsa
147192984Sthompsa	return (long) pmgr->dbgdev->dbgdev_wave_control(pmgr->dbgdev, wac_info);
148184610Salfred}
149192984Sthompsa
150192984Sthompsalong kfd_dbgmgr_address_watch(struct kfd_dbgmgr *pmgr,
151189265Sthompsa				struct dbg_address_watch_info *adw_info)
152184610Salfred{
153184610Salfred	/* Is the requests coming from the already registered process? */
154184610Salfred	if (pmgr->pasid != adw_info->process->pasid) {
155184610Salfred		pr_debug("H/W debugger support was not registered for requester pasid 0x%x\n",
156184610Salfred				adw_info->process->pasid);
157184610Salfred		return -EINVAL;
158184610Salfred	}
159184610Salfred
160184610Salfred	return (long) pmgr->dbgdev->dbgdev_address_watch(pmgr->dbgdev,
161184610Salfred							adw_info);
162184610Salfred}
163184610Salfred
164184610Salfred