1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2020 Intel Corporation. All rights reserved. 4 // 5 // Author: Cezary Rojewski <cezary.rojewski@intel.com> 6 // 7 8 #include <linux/irqreturn.h> 9 #include "core.h" 10 #include "messages.h" 11 #include "registers.h" 12 13 #define CATPT_IPC_TIMEOUT_MS 300 14 15 void catpt_ipc_init(struct catpt_ipc *ipc, struct device *dev) 16 { 17 ipc->dev = dev; 18 ipc->ready = false; 19 ipc->default_timeout = CATPT_IPC_TIMEOUT_MS; 20 init_completion(&ipc->done_completion); 21 init_completion(&ipc->busy_completion); 22 spin_lock_init(&ipc->lock); 23 mutex_init(&ipc->mutex); 24 } 25 26 static int catpt_ipc_arm(struct catpt_ipc *ipc, struct catpt_fw_ready *config) 27 { 28 /* 29 * Both tx and rx are put into and received from outbox. Inbox is 30 * only used for notifications where payload size is known upfront, 31 * thus no separate buffer is allocated for it. 32 */ 33 ipc->rx.data = devm_kzalloc(ipc->dev, config->outbox_size, GFP_KERNEL); 34 if (!ipc->rx.data) 35 return -ENOMEM; 36 37 memcpy(&ipc->config, config, sizeof(*config)); 38 ipc->ready = true; 39 40 return 0; 41 } 42 43 static void catpt_ipc_msg_init(struct catpt_ipc *ipc, 44 struct catpt_ipc_msg *reply) 45 { 46 lockdep_assert_held(&ipc->lock); 47 48 ipc->rx.header = 0; 49 ipc->rx.size = reply ? reply->size : 0; 50 reinit_completion(&ipc->done_completion); 51 reinit_completion(&ipc->busy_completion); 52 } 53 54 static void catpt_dsp_send_tx(struct catpt_dev *cdev, 55 const struct catpt_ipc_msg *tx) 56 { 57 u32 header = tx->header | CATPT_IPCC_BUSY; 58 59 memcpy_toio(catpt_outbox_addr(cdev), tx->data, tx->size); 60 catpt_writel_shim(cdev, IPCC, header); 61 } 62 63 static int catpt_wait_msg_completion(struct catpt_dev *cdev, int timeout) 64 { 65 struct catpt_ipc *ipc = &cdev->ipc; 66 int ret; 67 68 ret = wait_for_completion_timeout(&ipc->done_completion, 69 msecs_to_jiffies(timeout)); 70 if (!ret) 71 return -ETIMEDOUT; 72 if (ipc->rx.rsp.status != CATPT_REPLY_PENDING) 73 return 0; 74 75 /* wait for delayed reply */ 76 ret = wait_for_completion_timeout(&ipc->busy_completion, 77 msecs_to_jiffies(timeout)); 78 return ret ? 0 : -ETIMEDOUT; 79 } 80 81 static int catpt_dsp_do_send_msg(struct catpt_dev *cdev, 82 struct catpt_ipc_msg request, 83 struct catpt_ipc_msg *reply, int timeout) 84 { 85 struct catpt_ipc *ipc = &cdev->ipc; 86 unsigned long flags; 87 int ret; 88 89 if (!ipc->ready) 90 return -EPERM; 91 if (request.size > ipc->config.outbox_size || 92 (reply && reply->size > ipc->config.outbox_size)) 93 return -EINVAL; 94 95 spin_lock_irqsave(&ipc->lock, flags); 96 catpt_ipc_msg_init(ipc, reply); 97 catpt_dsp_send_tx(cdev, &request); 98 spin_unlock_irqrestore(&ipc->lock, flags); 99 100 ret = catpt_wait_msg_completion(cdev, timeout); 101 if (ret) { 102 dev_crit(cdev->dev, "communication severed: %d, rebooting dsp..\n", 103 ret); 104 ipc->ready = false; 105 /* TODO: attempt recovery */ 106 return ret; 107 } 108 109 ret = ipc->rx.rsp.status; 110 if (reply) { 111 reply->header = ipc->rx.header; 112 113 if (!ret && reply->data) 114 memcpy(reply->data, ipc->rx.data, reply->size); 115 } 116 117 return ret; 118 } 119 120 int catpt_dsp_send_msg_timeout(struct catpt_dev *cdev, 121 struct catpt_ipc_msg request, 122 struct catpt_ipc_msg *reply, int timeout) 123 { 124 struct catpt_ipc *ipc = &cdev->ipc; 125 int ret; 126 127 mutex_lock(&ipc->mutex); 128 ret = catpt_dsp_do_send_msg(cdev, request, reply, timeout); 129 mutex_unlock(&ipc->mutex); 130 131 return ret; 132 } 133 134 int catpt_dsp_send_msg(struct catpt_dev *cdev, struct catpt_ipc_msg request, 135 struct catpt_ipc_msg *reply) 136 { 137 return catpt_dsp_send_msg_timeout(cdev, request, reply, 138 cdev->ipc.default_timeout); 139 } 140 141 static void catpt_dsp_copy_rx(struct catpt_dev *cdev, u32 header) 142 { 143 struct catpt_ipc *ipc = &cdev->ipc; 144 145 ipc->rx.header = header; 146 if (ipc->rx.rsp.status != CATPT_REPLY_SUCCESS) 147 return; 148 149 memcpy_fromio(ipc->rx.data, catpt_outbox_addr(cdev), ipc->rx.size); 150 } 151 152 static void catpt_dsp_process_response(struct catpt_dev *cdev, u32 header) 153 { 154 union catpt_notify_msg msg = CATPT_MSG(header); 155 struct catpt_ipc *ipc = &cdev->ipc; 156 157 if (msg.fw_ready) { 158 struct catpt_fw_ready config; 159 /* to fit 32b header original address is shifted right by 3 */ 160 u32 off = msg.mailbox_address << 3; 161 162 memcpy_fromio(&config, cdev->lpe_ba + off, sizeof(config)); 163 164 catpt_ipc_arm(ipc, &config); 165 complete(&cdev->fw_ready); 166 return; 167 } 168 169 switch (msg.global_msg_type) { 170 case CATPT_GLB_REQUEST_CORE_DUMP: 171 dev_err(cdev->dev, "ADSP device coredump received\n"); 172 ipc->ready = false; 173 catpt_coredump(cdev); 174 /* TODO: attempt recovery */ 175 break; 176 177 case CATPT_GLB_STREAM_MESSAGE: 178 switch (msg.stream_msg_type) { 179 case CATPT_STRM_NOTIFICATION: 180 break; 181 default: 182 catpt_dsp_copy_rx(cdev, header); 183 /* signal completion of delayed reply */ 184 complete(&ipc->busy_completion); 185 break; 186 } 187 break; 188 189 default: 190 dev_warn(cdev->dev, "unknown response: %d received\n", 191 msg.global_msg_type); 192 break; 193 } 194 } 195 196 irqreturn_t catpt_dsp_irq_thread(int irq, void *dev_id) 197 { 198 struct catpt_dev *cdev = dev_id; 199 u32 ipcd; 200 201 ipcd = catpt_readl_shim(cdev, IPCD); 202 203 /* ensure there is delayed reply or notification to process */ 204 if (!(ipcd & CATPT_IPCD_BUSY)) 205 return IRQ_NONE; 206 207 catpt_dsp_process_response(cdev, ipcd); 208 209 /* tell DSP processing is completed */ 210 catpt_updatel_shim(cdev, IPCD, CATPT_IPCD_BUSY | CATPT_IPCD_DONE, 211 CATPT_IPCD_DONE); 212 /* unmask dsp BUSY interrupt */ 213 catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCDB, 0); 214 215 return IRQ_HANDLED; 216 } 217 218 irqreturn_t catpt_dsp_irq_handler(int irq, void *dev_id) 219 { 220 struct catpt_dev *cdev = dev_id; 221 irqreturn_t ret = IRQ_NONE; 222 u32 isc, ipcc; 223 224 isc = catpt_readl_shim(cdev, ISC); 225 226 /* immediate reply */ 227 if (isc & CATPT_ISC_IPCCD) { 228 /* mask host DONE interrupt */ 229 catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCCD, CATPT_IMC_IPCCD); 230 231 ipcc = catpt_readl_shim(cdev, IPCC); 232 catpt_dsp_copy_rx(cdev, ipcc); 233 complete(&cdev->ipc.done_completion); 234 235 /* tell DSP processing is completed */ 236 catpt_updatel_shim(cdev, IPCC, CATPT_IPCC_DONE, 0); 237 /* unmask host DONE interrupt */ 238 catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCCD, 0); 239 ret = IRQ_HANDLED; 240 } 241 242 /* delayed reply or notification */ 243 if (isc & CATPT_ISC_IPCDB) { 244 /* mask dsp BUSY interrupt */ 245 catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCDB, CATPT_IMC_IPCDB); 246 ret = IRQ_WAKE_THREAD; 247 } 248 249 return ret; 250 } 251