1 /* $NetBSD: kfd_dbgmgr.c,v 1.2 2018/08/27 04:58:20 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.2 2018/08/27 04:58:20 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 42 static DEFINE_MUTEX(kfd_dbgmgr_mutex); 43 44 struct mutex *kfd_get_dbgmgr_mutex(void) 45 { 46 return &kfd_dbgmgr_mutex; 47 } 48 49 50 static void kfd_dbgmgr_uninitialize(struct kfd_dbgmgr *pmgr) 51 { 52 BUG_ON(!pmgr); 53 54 kfree(pmgr->dbgdev); 55 56 pmgr->dbgdev = NULL; 57 pmgr->pasid = 0; 58 pmgr->dev = NULL; 59 } 60 61 void kfd_dbgmgr_destroy(struct kfd_dbgmgr *pmgr) 62 { 63 if (pmgr != NULL) { 64 kfd_dbgmgr_uninitialize(pmgr); 65 kfree(pmgr); 66 } 67 } 68 69 bool kfd_dbgmgr_create(struct kfd_dbgmgr **ppmgr, struct kfd_dev *pdev) 70 { 71 enum DBGDEV_TYPE type = DBGDEV_TYPE_DIQ; 72 struct kfd_dbgmgr *new_buff; 73 74 BUG_ON(pdev == NULL); 75 BUG_ON(!pdev->init_complete); 76 77 new_buff = kfd_alloc_struct(new_buff); 78 if (!new_buff) { 79 pr_err("amdkfd: Failed to allocate dbgmgr instance\n"); 80 return false; 81 } 82 83 new_buff->pasid = 0; 84 new_buff->dev = pdev; 85 new_buff->dbgdev = kfd_alloc_struct(new_buff->dbgdev); 86 if (!new_buff->dbgdev) { 87 pr_err("amdkfd: Failed to allocate dbgdev instance\n"); 88 kfree(new_buff); 89 return false; 90 } 91 92 /* get actual type of DBGDevice cpsch or not */ 93 if (sched_policy == KFD_SCHED_POLICY_NO_HWS) 94 type = DBGDEV_TYPE_NODIQ; 95 96 kfd_dbgdev_init(new_buff->dbgdev, pdev, type); 97 *ppmgr = new_buff; 98 99 return true; 100 } 101 102 long kfd_dbgmgr_register(struct kfd_dbgmgr *pmgr, struct kfd_process *p) 103 { 104 BUG_ON(!p || !pmgr || !pmgr->dbgdev); 105 106 if (pmgr->pasid != 0) { 107 pr_debug("H/W debugger is already active using pasid %d\n", 108 pmgr->pasid); 109 return -EBUSY; 110 } 111 112 /* remember pasid */ 113 pmgr->pasid = p->pasid; 114 115 /* provide the pqm for diq generation */ 116 pmgr->dbgdev->pqm = &p->pqm; 117 118 /* activate the actual registering */ 119 pmgr->dbgdev->dbgdev_register(pmgr->dbgdev); 120 121 return 0; 122 } 123 124 long kfd_dbgmgr_unregister(struct kfd_dbgmgr *pmgr, struct kfd_process *p) 125 { 126 BUG_ON(!p || !pmgr || !pmgr->dbgdev); 127 128 /* Is the requests coming from the already registered process? */ 129 if (pmgr->pasid != p->pasid) { 130 pr_debug("H/W debugger is not registered by calling pasid %d\n", 131 p->pasid); 132 return -EINVAL; 133 } 134 135 pmgr->dbgdev->dbgdev_unregister(pmgr->dbgdev); 136 137 pmgr->pasid = 0; 138 139 return 0; 140 } 141 142 long kfd_dbgmgr_wave_control(struct kfd_dbgmgr *pmgr, 143 struct dbg_wave_control_info *wac_info) 144 { 145 BUG_ON(!pmgr || !pmgr->dbgdev || !wac_info); 146 147 /* Is the requests coming from the already registered process? */ 148 if (pmgr->pasid != wac_info->process->pasid) { 149 pr_debug("H/W debugger support was not registered for requester pasid %d\n", 150 wac_info->process->pasid); 151 return -EINVAL; 152 } 153 154 return (long) pmgr->dbgdev->dbgdev_wave_control(pmgr->dbgdev, wac_info); 155 } 156 157 long kfd_dbgmgr_address_watch(struct kfd_dbgmgr *pmgr, 158 struct dbg_address_watch_info *adw_info) 159 { 160 BUG_ON(!pmgr || !pmgr->dbgdev || !adw_info); 161 162 163 /* Is the requests coming from the already registered process? */ 164 if (pmgr->pasid != adw_info->process->pasid) { 165 pr_debug("H/W debugger support was not registered for requester pasid %d\n", 166 adw_info->process->pasid); 167 return -EINVAL; 168 } 169 170 return (long) pmgr->dbgdev->dbgdev_address_watch(pmgr->dbgdev, 171 adw_info); 172 } 173 174