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