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