18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#include <linux/types.h>
248c2ecf20Sopenharmony_ci#include <linux/kernel.h>
258c2ecf20Sopenharmony_ci#include <linux/log2.h>
268c2ecf20Sopenharmony_ci#include <linux/sched.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci#include <linux/device.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "kfd_priv.h"
318c2ecf20Sopenharmony_ci#include "cik_regs.h"
328c2ecf20Sopenharmony_ci#include "kfd_pm4_headers.h"
338c2ecf20Sopenharmony_ci#include "kfd_pm4_headers_diq.h"
348c2ecf20Sopenharmony_ci#include "kfd_dbgmgr.h"
358c2ecf20Sopenharmony_ci#include "kfd_dbgdev.h"
368c2ecf20Sopenharmony_ci#include "kfd_device_queue_manager.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(kfd_dbgmgr_mutex);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistruct mutex *kfd_get_dbgmgr_mutex(void)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	return &kfd_dbgmgr_mutex;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic void kfd_dbgmgr_uninitialize(struct kfd_dbgmgr *pmgr)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	kfree(pmgr->dbgdev);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	pmgr->dbgdev = NULL;
518c2ecf20Sopenharmony_ci	pmgr->pasid = 0;
528c2ecf20Sopenharmony_ci	pmgr->dev = NULL;
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_civoid kfd_dbgmgr_destroy(struct kfd_dbgmgr *pmgr)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	if (pmgr) {
588c2ecf20Sopenharmony_ci		kfd_dbgmgr_uninitialize(pmgr);
598c2ecf20Sopenharmony_ci		kfree(pmgr);
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cibool kfd_dbgmgr_create(struct kfd_dbgmgr **ppmgr, struct kfd_dev *pdev)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	enum DBGDEV_TYPE type = DBGDEV_TYPE_DIQ;
668c2ecf20Sopenharmony_ci	struct kfd_dbgmgr *new_buff;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (WARN_ON(!pdev->init_complete))
698c2ecf20Sopenharmony_ci		return false;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	new_buff = kfd_alloc_struct(new_buff);
728c2ecf20Sopenharmony_ci	if (!new_buff) {
738c2ecf20Sopenharmony_ci		pr_err("Failed to allocate dbgmgr instance\n");
748c2ecf20Sopenharmony_ci		return false;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	new_buff->pasid = 0;
788c2ecf20Sopenharmony_ci	new_buff->dev = pdev;
798c2ecf20Sopenharmony_ci	new_buff->dbgdev = kfd_alloc_struct(new_buff->dbgdev);
808c2ecf20Sopenharmony_ci	if (!new_buff->dbgdev) {
818c2ecf20Sopenharmony_ci		pr_err("Failed to allocate dbgdev instance\n");
828c2ecf20Sopenharmony_ci		kfree(new_buff);
838c2ecf20Sopenharmony_ci		return false;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* get actual type of DBGDevice cpsch or not */
878c2ecf20Sopenharmony_ci	if (pdev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
888c2ecf20Sopenharmony_ci		type = DBGDEV_TYPE_NODIQ;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	kfd_dbgdev_init(new_buff->dbgdev, pdev, type);
918c2ecf20Sopenharmony_ci	*ppmgr = new_buff;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return true;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cilong kfd_dbgmgr_register(struct kfd_dbgmgr *pmgr, struct kfd_process *p)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	if (pmgr->pasid != 0) {
998c2ecf20Sopenharmony_ci		pr_debug("H/W debugger is already active using pasid 0x%x\n",
1008c2ecf20Sopenharmony_ci				pmgr->pasid);
1018c2ecf20Sopenharmony_ci		return -EBUSY;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* remember pasid */
1058c2ecf20Sopenharmony_ci	pmgr->pasid = p->pasid;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* provide the pqm for diq generation */
1088c2ecf20Sopenharmony_ci	pmgr->dbgdev->pqm = &p->pqm;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* activate the actual registering */
1118c2ecf20Sopenharmony_ci	pmgr->dbgdev->dbgdev_register(pmgr->dbgdev);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return 0;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cilong kfd_dbgmgr_unregister(struct kfd_dbgmgr *pmgr, struct kfd_process *p)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	/* Is the requests coming from the already registered process? */
1198c2ecf20Sopenharmony_ci	if (pmgr->pasid != p->pasid) {
1208c2ecf20Sopenharmony_ci		pr_debug("H/W debugger is not registered by calling pasid 0x%x\n",
1218c2ecf20Sopenharmony_ci				p->pasid);
1228c2ecf20Sopenharmony_ci		return -EINVAL;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	pmgr->dbgdev->dbgdev_unregister(pmgr->dbgdev);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	pmgr->pasid = 0;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return 0;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cilong kfd_dbgmgr_wave_control(struct kfd_dbgmgr *pmgr,
1338c2ecf20Sopenharmony_ci				struct dbg_wave_control_info *wac_info)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	/* Is the requests coming from the already registered process? */
1368c2ecf20Sopenharmony_ci	if (pmgr->pasid != wac_info->process->pasid) {
1378c2ecf20Sopenharmony_ci		pr_debug("H/W debugger support was not registered for requester pasid 0x%x\n",
1388c2ecf20Sopenharmony_ci				wac_info->process->pasid);
1398c2ecf20Sopenharmony_ci		return -EINVAL;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return (long) pmgr->dbgdev->dbgdev_wave_control(pmgr->dbgdev, wac_info);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cilong kfd_dbgmgr_address_watch(struct kfd_dbgmgr *pmgr,
1468c2ecf20Sopenharmony_ci				struct dbg_address_watch_info *adw_info)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	/* Is the requests coming from the already registered process? */
1498c2ecf20Sopenharmony_ci	if (pmgr->pasid != adw_info->process->pasid) {
1508c2ecf20Sopenharmony_ci		pr_debug("H/W debugger support was not registered for requester pasid 0x%x\n",
1518c2ecf20Sopenharmony_ci				adw_info->process->pasid);
1528c2ecf20Sopenharmony_ci		return -EINVAL;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	return (long) pmgr->dbgdev->dbgdev_address_watch(pmgr->dbgdev,
1568c2ecf20Sopenharmony_ci							adw_info);
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
159