1fa5d823bSSanjay R Mehta // SPDX-License-Identifier: GPL-2.0-only 2fa5d823bSSanjay R Mehta /* 3fa5d823bSSanjay R Mehta * AMD Passthru DMA device driver 4fa5d823bSSanjay R Mehta * -- Based on the CCP driver 5fa5d823bSSanjay R Mehta * 6fa5d823bSSanjay R Mehta * Copyright (C) 2016,2021 Advanced Micro Devices, Inc. 7fa5d823bSSanjay R Mehta * 8fa5d823bSSanjay R Mehta * Author: Sanjay R Mehta <sanju.mehta@amd.com> 9fa5d823bSSanjay R Mehta * Author: Gary R Hook <gary.hook@amd.com> 10fa5d823bSSanjay R Mehta */ 11fa5d823bSSanjay R Mehta 12fa5d823bSSanjay R Mehta #include <linux/bitfield.h> 13fa5d823bSSanjay R Mehta #include <linux/dma-mapping.h> 14fa5d823bSSanjay R Mehta #include <linux/debugfs.h> 15fa5d823bSSanjay R Mehta #include <linux/interrupt.h> 16fa5d823bSSanjay R Mehta #include <linux/kernel.h> 17fa5d823bSSanjay R Mehta #include <linux/module.h> 18fa5d823bSSanjay R Mehta #include <linux/pci.h> 19fa5d823bSSanjay R Mehta 20fa5d823bSSanjay R Mehta #include "ptdma.h" 21fa5d823bSSanjay R Mehta 22fa5d823bSSanjay R Mehta /* Human-readable error strings */ 23fa5d823bSSanjay R Mehta static char *pt_error_codes[] = { 24fa5d823bSSanjay R Mehta "", 25fa5d823bSSanjay R Mehta "ERR 01: ILLEGAL_ENGINE", 26fa5d823bSSanjay R Mehta "ERR 03: ILLEGAL_FUNCTION_TYPE", 27fa5d823bSSanjay R Mehta "ERR 04: ILLEGAL_FUNCTION_MODE", 28fa5d823bSSanjay R Mehta "ERR 06: ILLEGAL_FUNCTION_SIZE", 29fa5d823bSSanjay R Mehta "ERR 08: ILLEGAL_FUNCTION_RSVD", 30fa5d823bSSanjay R Mehta "ERR 09: ILLEGAL_BUFFER_LENGTH", 31fa5d823bSSanjay R Mehta "ERR 10: VLSB_FAULT", 32fa5d823bSSanjay R Mehta "ERR 11: ILLEGAL_MEM_ADDR", 33fa5d823bSSanjay R Mehta "ERR 12: ILLEGAL_MEM_SEL", 34fa5d823bSSanjay R Mehta "ERR 13: ILLEGAL_CONTEXT_ID", 35fa5d823bSSanjay R Mehta "ERR 15: 0xF Reserved", 36fa5d823bSSanjay R Mehta "ERR 18: CMD_TIMEOUT", 37fa5d823bSSanjay R Mehta "ERR 19: IDMA0_AXI_SLVERR", 38fa5d823bSSanjay R Mehta "ERR 20: IDMA0_AXI_DECERR", 39fa5d823bSSanjay R Mehta "ERR 21: 0x15 Reserved", 40fa5d823bSSanjay R Mehta "ERR 22: IDMA1_AXI_SLAVE_FAULT", 41fa5d823bSSanjay R Mehta "ERR 23: IDMA1_AIXI_DECERR", 42fa5d823bSSanjay R Mehta "ERR 24: 0x18 Reserved", 43fa5d823bSSanjay R Mehta "ERR 27: 0x1B Reserved", 44fa5d823bSSanjay R Mehta "ERR 38: ODMA0_AXI_SLVERR", 45fa5d823bSSanjay R Mehta "ERR 39: ODMA0_AXI_DECERR", 46fa5d823bSSanjay R Mehta "ERR 40: 0x28 Reserved", 47fa5d823bSSanjay R Mehta "ERR 41: ODMA1_AXI_SLVERR", 48fa5d823bSSanjay R Mehta "ERR 42: ODMA1_AXI_DECERR", 49fa5d823bSSanjay R Mehta "ERR 43: LSB_PARITY_ERR", 50fa5d823bSSanjay R Mehta }; 51fa5d823bSSanjay R Mehta 52fa5d823bSSanjay R Mehta static void pt_log_error(struct pt_device *d, int e) 53fa5d823bSSanjay R Mehta { 54fa5d823bSSanjay R Mehta dev_err(d->dev, "PTDMA error: %s (0x%x)\n", pt_error_codes[e], e); 55fa5d823bSSanjay R Mehta } 56fa5d823bSSanjay R Mehta 57fa5d823bSSanjay R Mehta void pt_start_queue(struct pt_cmd_queue *cmd_q) 58fa5d823bSSanjay R Mehta { 59fa5d823bSSanjay R Mehta /* Turn on the run bit */ 60fa5d823bSSanjay R Mehta iowrite32(cmd_q->qcontrol | CMD_Q_RUN, cmd_q->reg_control); 61fa5d823bSSanjay R Mehta } 62fa5d823bSSanjay R Mehta 63fa5d823bSSanjay R Mehta void pt_stop_queue(struct pt_cmd_queue *cmd_q) 64fa5d823bSSanjay R Mehta { 65fa5d823bSSanjay R Mehta /* Turn off the run bit */ 66fa5d823bSSanjay R Mehta iowrite32(cmd_q->qcontrol & ~CMD_Q_RUN, cmd_q->reg_control); 67fa5d823bSSanjay R Mehta } 68fa5d823bSSanjay R Mehta 69fa5d823bSSanjay R Mehta static int pt_core_execute_cmd(struct ptdma_desc *desc, struct pt_cmd_queue *cmd_q) 70fa5d823bSSanjay R Mehta { 71fa5d823bSSanjay R Mehta bool soc = FIELD_GET(DWORD0_SOC, desc->dw0); 72fa5d823bSSanjay R Mehta u8 *q_desc = (u8 *)&cmd_q->qbase[cmd_q->qidx]; 73fa5d823bSSanjay R Mehta u32 tail; 74fa5d823bSSanjay R Mehta 75fa5d823bSSanjay R Mehta if (soc) { 76fa5d823bSSanjay R Mehta desc->dw0 |= FIELD_PREP(DWORD0_IOC, desc->dw0); 77fa5d823bSSanjay R Mehta desc->dw0 &= ~DWORD0_SOC; 78fa5d823bSSanjay R Mehta } 79fa5d823bSSanjay R Mehta mutex_lock(&cmd_q->q_mutex); 80fa5d823bSSanjay R Mehta 81fa5d823bSSanjay R Mehta /* Copy 32-byte command descriptor to hw queue. */ 82fa5d823bSSanjay R Mehta memcpy(q_desc, desc, 32); 83fa5d823bSSanjay R Mehta cmd_q->qidx = (cmd_q->qidx + 1) % CMD_Q_LEN; 84fa5d823bSSanjay R Mehta 85fa5d823bSSanjay R Mehta /* The data used by this command must be flushed to memory */ 86fa5d823bSSanjay R Mehta wmb(); 87fa5d823bSSanjay R Mehta 88fa5d823bSSanjay R Mehta /* Write the new tail address back to the queue register */ 89fa5d823bSSanjay R Mehta tail = lower_32_bits(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE); 90fa5d823bSSanjay R Mehta iowrite32(tail, cmd_q->reg_control + 0x0004); 91fa5d823bSSanjay R Mehta 92fa5d823bSSanjay R Mehta /* Turn the queue back on using our cached control register */ 93fa5d823bSSanjay R Mehta pt_start_queue(cmd_q); 94fa5d823bSSanjay R Mehta mutex_unlock(&cmd_q->q_mutex); 95fa5d823bSSanjay R Mehta 96fa5d823bSSanjay R Mehta return 0; 97fa5d823bSSanjay R Mehta } 98fa5d823bSSanjay R Mehta 99fa5d823bSSanjay R Mehta int pt_core_perform_passthru(struct pt_cmd_queue *cmd_q, 100fa5d823bSSanjay R Mehta struct pt_passthru_engine *pt_engine) 101fa5d823bSSanjay R Mehta { 102fa5d823bSSanjay R Mehta struct ptdma_desc desc; 103fa5d823bSSanjay R Mehta 104fa5d823bSSanjay R Mehta cmd_q->cmd_error = 0; 105e2fb2e2aSSanjay R Mehta cmd_q->total_pt_ops++; 106fa5d823bSSanjay R Mehta memset(&desc, 0, sizeof(desc)); 107fa5d823bSSanjay R Mehta desc.dw0 = CMD_DESC_DW0_VAL; 108fa5d823bSSanjay R Mehta desc.length = pt_engine->src_len; 109fa5d823bSSanjay R Mehta desc.src_lo = lower_32_bits(pt_engine->src_dma); 110fa5d823bSSanjay R Mehta desc.dw3.src_hi = upper_32_bits(pt_engine->src_dma); 111fa5d823bSSanjay R Mehta desc.dst_lo = lower_32_bits(pt_engine->dst_dma); 112fa5d823bSSanjay R Mehta desc.dw5.dst_hi = upper_32_bits(pt_engine->dst_dma); 113fa5d823bSSanjay R Mehta 114fa5d823bSSanjay R Mehta return pt_core_execute_cmd(&desc, cmd_q); 115fa5d823bSSanjay R Mehta } 116fa5d823bSSanjay R Mehta 117fa5d823bSSanjay R Mehta static inline void pt_core_disable_queue_interrupts(struct pt_device *pt) 118fa5d823bSSanjay R Mehta { 119fa5d823bSSanjay R Mehta iowrite32(0, pt->cmd_q.reg_control + 0x000C); 120fa5d823bSSanjay R Mehta } 121fa5d823bSSanjay R Mehta 122fa5d823bSSanjay R Mehta static inline void pt_core_enable_queue_interrupts(struct pt_device *pt) 123fa5d823bSSanjay R Mehta { 124fa5d823bSSanjay R Mehta iowrite32(SUPPORTED_INTERRUPTS, pt->cmd_q.reg_control + 0x000C); 125fa5d823bSSanjay R Mehta } 126fa5d823bSSanjay R Mehta 127b0b4a6b1SSanjay R Mehta static void pt_do_cmd_complete(unsigned long data) 128b0b4a6b1SSanjay R Mehta { 129b0b4a6b1SSanjay R Mehta struct pt_tasklet_data *tdata = (struct pt_tasklet_data *)data; 130b0b4a6b1SSanjay R Mehta struct pt_cmd *cmd = tdata->cmd; 131b0b4a6b1SSanjay R Mehta struct pt_cmd_queue *cmd_q = &cmd->pt->cmd_q; 132b0b4a6b1SSanjay R Mehta u32 tail; 133b0b4a6b1SSanjay R Mehta 134b0b4a6b1SSanjay R Mehta if (cmd_q->cmd_error) { 135b0b4a6b1SSanjay R Mehta /* 136b0b4a6b1SSanjay R Mehta * Log the error and flush the queue by 137b0b4a6b1SSanjay R Mehta * moving the head pointer 138b0b4a6b1SSanjay R Mehta */ 139b0b4a6b1SSanjay R Mehta tail = lower_32_bits(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE); 140b0b4a6b1SSanjay R Mehta pt_log_error(cmd_q->pt, cmd_q->cmd_error); 141b0b4a6b1SSanjay R Mehta iowrite32(tail, cmd_q->reg_control + 0x0008); 142b0b4a6b1SSanjay R Mehta } 143b0b4a6b1SSanjay R Mehta 144b0b4a6b1SSanjay R Mehta cmd->pt_cmd_callback(cmd->data, cmd->ret); 145b0b4a6b1SSanjay R Mehta } 146b0b4a6b1SSanjay R Mehta 147fa5d823bSSanjay R Mehta static irqreturn_t pt_core_irq_handler(int irq, void *data) 148fa5d823bSSanjay R Mehta { 149fa5d823bSSanjay R Mehta struct pt_device *pt = data; 150fa5d823bSSanjay R Mehta struct pt_cmd_queue *cmd_q = &pt->cmd_q; 151fa5d823bSSanjay R Mehta u32 status; 152fa5d823bSSanjay R Mehta 153fa5d823bSSanjay R Mehta pt_core_disable_queue_interrupts(pt); 154e2fb2e2aSSanjay R Mehta pt->total_interrupts++; 155fa5d823bSSanjay R Mehta status = ioread32(cmd_q->reg_control + 0x0010); 156fa5d823bSSanjay R Mehta if (status) { 157fa5d823bSSanjay R Mehta cmd_q->int_status = status; 158fa5d823bSSanjay R Mehta cmd_q->q_status = ioread32(cmd_q->reg_control + 0x0100); 159fa5d823bSSanjay R Mehta cmd_q->q_int_status = ioread32(cmd_q->reg_control + 0x0104); 160fa5d823bSSanjay R Mehta 161fa5d823bSSanjay R Mehta /* On error, only save the first error value */ 162fa5d823bSSanjay R Mehta if ((status & INT_ERROR) && !cmd_q->cmd_error) 163fa5d823bSSanjay R Mehta cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status); 164fa5d823bSSanjay R Mehta 165fa5d823bSSanjay R Mehta /* Acknowledge the interrupt */ 166fa5d823bSSanjay R Mehta iowrite32(status, cmd_q->reg_control + 0x0010); 167fa5d823bSSanjay R Mehta pt_core_enable_queue_interrupts(pt); 168b0b4a6b1SSanjay R Mehta pt_do_cmd_complete((ulong)&pt->tdata); 169fa5d823bSSanjay R Mehta } 170fa5d823bSSanjay R Mehta return IRQ_HANDLED; 171fa5d823bSSanjay R Mehta } 172fa5d823bSSanjay R Mehta 173fa5d823bSSanjay R Mehta int pt_core_init(struct pt_device *pt) 174fa5d823bSSanjay R Mehta { 175fa5d823bSSanjay R Mehta char dma_pool_name[MAX_DMAPOOL_NAME_LEN]; 176fa5d823bSSanjay R Mehta struct pt_cmd_queue *cmd_q = &pt->cmd_q; 177fa5d823bSSanjay R Mehta u32 dma_addr_lo, dma_addr_hi; 178fa5d823bSSanjay R Mehta struct device *dev = pt->dev; 179fa5d823bSSanjay R Mehta struct dma_pool *dma_pool; 180fa5d823bSSanjay R Mehta int ret; 181fa5d823bSSanjay R Mehta 182fa5d823bSSanjay R Mehta /* Allocate a dma pool for the queue */ 183fa5d823bSSanjay R Mehta snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q", dev_name(pt->dev)); 184fa5d823bSSanjay R Mehta 185fa5d823bSSanjay R Mehta dma_pool = dma_pool_create(dma_pool_name, dev, 186fa5d823bSSanjay R Mehta PT_DMAPOOL_MAX_SIZE, 187fa5d823bSSanjay R Mehta PT_DMAPOOL_ALIGN, 0); 188fa5d823bSSanjay R Mehta if (!dma_pool) 189fa5d823bSSanjay R Mehta return -ENOMEM; 190fa5d823bSSanjay R Mehta 191fa5d823bSSanjay R Mehta /* ptdma core initialisation */ 192fa5d823bSSanjay R Mehta iowrite32(CMD_CONFIG_VHB_EN, pt->io_regs + CMD_CONFIG_OFFSET); 193fa5d823bSSanjay R Mehta iowrite32(CMD_QUEUE_PRIO, pt->io_regs + CMD_QUEUE_PRIO_OFFSET); 194fa5d823bSSanjay R Mehta iowrite32(CMD_TIMEOUT_DISABLE, pt->io_regs + CMD_TIMEOUT_OFFSET); 195fa5d823bSSanjay R Mehta iowrite32(CMD_CLK_GATE_CONFIG, pt->io_regs + CMD_CLK_GATE_CTL_OFFSET); 196fa5d823bSSanjay R Mehta iowrite32(CMD_CONFIG_REQID, pt->io_regs + CMD_REQID_CONFIG_OFFSET); 197fa5d823bSSanjay R Mehta 198fa5d823bSSanjay R Mehta cmd_q->pt = pt; 199fa5d823bSSanjay R Mehta cmd_q->dma_pool = dma_pool; 200fa5d823bSSanjay R Mehta mutex_init(&cmd_q->q_mutex); 201fa5d823bSSanjay R Mehta 202fa5d823bSSanjay R Mehta /* Page alignment satisfies our needs for N <= 128 */ 203fa5d823bSSanjay R Mehta cmd_q->qsize = Q_SIZE(Q_DESC_SIZE); 204fa5d823bSSanjay R Mehta cmd_q->qbase = dma_alloc_coherent(dev, cmd_q->qsize, 205fa5d823bSSanjay R Mehta &cmd_q->qbase_dma, 206fa5d823bSSanjay R Mehta GFP_KERNEL); 207fa5d823bSSanjay R Mehta if (!cmd_q->qbase) { 208fa5d823bSSanjay R Mehta dev_err(dev, "unable to allocate command queue\n"); 209fa5d823bSSanjay R Mehta ret = -ENOMEM; 210*3c62fd34SChristophe JAILLET goto e_destroy_pool; 211fa5d823bSSanjay R Mehta } 212fa5d823bSSanjay R Mehta 213fa5d823bSSanjay R Mehta cmd_q->qidx = 0; 214fa5d823bSSanjay R Mehta 215fa5d823bSSanjay R Mehta /* Preset some register values */ 216fa5d823bSSanjay R Mehta cmd_q->reg_control = pt->io_regs + CMD_Q_STATUS_INCR; 217fa5d823bSSanjay R Mehta 218fa5d823bSSanjay R Mehta /* Turn off the queues and disable interrupts until ready */ 219fa5d823bSSanjay R Mehta pt_core_disable_queue_interrupts(pt); 220fa5d823bSSanjay R Mehta 221fa5d823bSSanjay R Mehta cmd_q->qcontrol = 0; /* Start with nothing */ 222fa5d823bSSanjay R Mehta iowrite32(cmd_q->qcontrol, cmd_q->reg_control); 223fa5d823bSSanjay R Mehta 224fa5d823bSSanjay R Mehta ioread32(cmd_q->reg_control + 0x0104); 225fa5d823bSSanjay R Mehta ioread32(cmd_q->reg_control + 0x0100); 226fa5d823bSSanjay R Mehta 227fa5d823bSSanjay R Mehta /* Clear the interrupt status */ 228fa5d823bSSanjay R Mehta iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_control + 0x0010); 229fa5d823bSSanjay R Mehta 230fa5d823bSSanjay R Mehta /* Request an irq */ 231fa5d823bSSanjay R Mehta ret = request_irq(pt->pt_irq, pt_core_irq_handler, 0, dev_name(pt->dev), pt); 232*3c62fd34SChristophe JAILLET if (ret) { 233*3c62fd34SChristophe JAILLET dev_err(dev, "unable to allocate an IRQ\n"); 234*3c62fd34SChristophe JAILLET goto e_free_dma; 235*3c62fd34SChristophe JAILLET } 236fa5d823bSSanjay R Mehta 237fa5d823bSSanjay R Mehta /* Update the device registers with queue information. */ 238fa5d823bSSanjay R Mehta cmd_q->qcontrol &= ~CMD_Q_SIZE; 239fa5d823bSSanjay R Mehta cmd_q->qcontrol |= FIELD_PREP(CMD_Q_SIZE, QUEUE_SIZE_VAL); 240fa5d823bSSanjay R Mehta 241fa5d823bSSanjay R Mehta cmd_q->qdma_tail = cmd_q->qbase_dma; 242fa5d823bSSanjay R Mehta dma_addr_lo = lower_32_bits(cmd_q->qdma_tail); 243fa5d823bSSanjay R Mehta iowrite32((u32)dma_addr_lo, cmd_q->reg_control + 0x0004); 244fa5d823bSSanjay R Mehta iowrite32((u32)dma_addr_lo, cmd_q->reg_control + 0x0008); 245fa5d823bSSanjay R Mehta 246fa5d823bSSanjay R Mehta dma_addr_hi = upper_32_bits(cmd_q->qdma_tail); 247fa5d823bSSanjay R Mehta cmd_q->qcontrol |= (dma_addr_hi << 16); 248fa5d823bSSanjay R Mehta iowrite32(cmd_q->qcontrol, cmd_q->reg_control); 249fa5d823bSSanjay R Mehta 250fa5d823bSSanjay R Mehta pt_core_enable_queue_interrupts(pt); 251fa5d823bSSanjay R Mehta 252b0b4a6b1SSanjay R Mehta /* Register the DMA engine support */ 253b0b4a6b1SSanjay R Mehta ret = pt_dmaengine_register(pt); 254b0b4a6b1SSanjay R Mehta if (ret) 255*3c62fd34SChristophe JAILLET goto e_free_irq; 256b0b4a6b1SSanjay R Mehta 257e2fb2e2aSSanjay R Mehta /* Set up debugfs entries */ 258e2fb2e2aSSanjay R Mehta ptdma_debugfs_setup(pt); 259e2fb2e2aSSanjay R Mehta 260fa5d823bSSanjay R Mehta return 0; 261fa5d823bSSanjay R Mehta 262*3c62fd34SChristophe JAILLET e_free_irq: 263b0b4a6b1SSanjay R Mehta free_irq(pt->pt_irq, pt); 264b0b4a6b1SSanjay R Mehta 265*3c62fd34SChristophe JAILLET e_free_dma: 266fa5d823bSSanjay R Mehta dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase, cmd_q->qbase_dma); 267fa5d823bSSanjay R Mehta 268*3c62fd34SChristophe JAILLET e_destroy_pool: 269fa5d823bSSanjay R Mehta dma_pool_destroy(pt->cmd_q.dma_pool); 270fa5d823bSSanjay R Mehta 271fa5d823bSSanjay R Mehta return ret; 272fa5d823bSSanjay R Mehta } 273fa5d823bSSanjay R Mehta 274fa5d823bSSanjay R Mehta void pt_core_destroy(struct pt_device *pt) 275fa5d823bSSanjay R Mehta { 276fa5d823bSSanjay R Mehta struct device *dev = pt->dev; 277fa5d823bSSanjay R Mehta struct pt_cmd_queue *cmd_q = &pt->cmd_q; 278fa5d823bSSanjay R Mehta struct pt_cmd *cmd; 279fa5d823bSSanjay R Mehta 280b0b4a6b1SSanjay R Mehta /* Unregister the DMA engine */ 281b0b4a6b1SSanjay R Mehta pt_dmaengine_unregister(pt); 282b0b4a6b1SSanjay R Mehta 283fa5d823bSSanjay R Mehta /* Disable and clear interrupts */ 284fa5d823bSSanjay R Mehta pt_core_disable_queue_interrupts(pt); 285fa5d823bSSanjay R Mehta 286fa5d823bSSanjay R Mehta /* Turn off the run bit */ 287fa5d823bSSanjay R Mehta pt_stop_queue(cmd_q); 288fa5d823bSSanjay R Mehta 289fa5d823bSSanjay R Mehta /* Clear the interrupt status */ 290fa5d823bSSanjay R Mehta iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_control + 0x0010); 291fa5d823bSSanjay R Mehta ioread32(cmd_q->reg_control + 0x0104); 292fa5d823bSSanjay R Mehta ioread32(cmd_q->reg_control + 0x0100); 293fa5d823bSSanjay R Mehta 294fa5d823bSSanjay R Mehta free_irq(pt->pt_irq, pt); 295fa5d823bSSanjay R Mehta 296fa5d823bSSanjay R Mehta dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase, 297fa5d823bSSanjay R Mehta cmd_q->qbase_dma); 298fa5d823bSSanjay R Mehta 299fa5d823bSSanjay R Mehta /* Flush the cmd queue */ 300fa5d823bSSanjay R Mehta while (!list_empty(&pt->cmd)) { 301fa5d823bSSanjay R Mehta /* Invoke the callback directly with an error code */ 302fa5d823bSSanjay R Mehta cmd = list_first_entry(&pt->cmd, struct pt_cmd, entry); 303fa5d823bSSanjay R Mehta list_del(&cmd->entry); 304fa5d823bSSanjay R Mehta cmd->pt_cmd_callback(cmd->data, -ENODEV); 305fa5d823bSSanjay R Mehta } 306fa5d823bSSanjay R Mehta } 307