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; 105fa5d823bSSanjay R Mehta memset(&desc, 0, sizeof(desc)); 106fa5d823bSSanjay R Mehta desc.dw0 = CMD_DESC_DW0_VAL; 107fa5d823bSSanjay R Mehta desc.length = pt_engine->src_len; 108fa5d823bSSanjay R Mehta desc.src_lo = lower_32_bits(pt_engine->src_dma); 109fa5d823bSSanjay R Mehta desc.dw3.src_hi = upper_32_bits(pt_engine->src_dma); 110fa5d823bSSanjay R Mehta desc.dst_lo = lower_32_bits(pt_engine->dst_dma); 111fa5d823bSSanjay R Mehta desc.dw5.dst_hi = upper_32_bits(pt_engine->dst_dma); 112fa5d823bSSanjay R Mehta 113fa5d823bSSanjay R Mehta return pt_core_execute_cmd(&desc, cmd_q); 114fa5d823bSSanjay R Mehta } 115fa5d823bSSanjay R Mehta 116fa5d823bSSanjay R Mehta static inline void pt_core_disable_queue_interrupts(struct pt_device *pt) 117fa5d823bSSanjay R Mehta { 118fa5d823bSSanjay R Mehta iowrite32(0, pt->cmd_q.reg_control + 0x000C); 119fa5d823bSSanjay R Mehta } 120fa5d823bSSanjay R Mehta 121fa5d823bSSanjay R Mehta static inline void pt_core_enable_queue_interrupts(struct pt_device *pt) 122fa5d823bSSanjay R Mehta { 123fa5d823bSSanjay R Mehta iowrite32(SUPPORTED_INTERRUPTS, pt->cmd_q.reg_control + 0x000C); 124fa5d823bSSanjay R Mehta } 125fa5d823bSSanjay R Mehta 126*b0b4a6b1SSanjay R Mehta static void pt_do_cmd_complete(unsigned long data) 127*b0b4a6b1SSanjay R Mehta { 128*b0b4a6b1SSanjay R Mehta struct pt_tasklet_data *tdata = (struct pt_tasklet_data *)data; 129*b0b4a6b1SSanjay R Mehta struct pt_cmd *cmd = tdata->cmd; 130*b0b4a6b1SSanjay R Mehta struct pt_cmd_queue *cmd_q = &cmd->pt->cmd_q; 131*b0b4a6b1SSanjay R Mehta u32 tail; 132*b0b4a6b1SSanjay R Mehta 133*b0b4a6b1SSanjay R Mehta if (cmd_q->cmd_error) { 134*b0b4a6b1SSanjay R Mehta /* 135*b0b4a6b1SSanjay R Mehta * Log the error and flush the queue by 136*b0b4a6b1SSanjay R Mehta * moving the head pointer 137*b0b4a6b1SSanjay R Mehta */ 138*b0b4a6b1SSanjay R Mehta tail = lower_32_bits(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE); 139*b0b4a6b1SSanjay R Mehta pt_log_error(cmd_q->pt, cmd_q->cmd_error); 140*b0b4a6b1SSanjay R Mehta iowrite32(tail, cmd_q->reg_control + 0x0008); 141*b0b4a6b1SSanjay R Mehta } 142*b0b4a6b1SSanjay R Mehta 143*b0b4a6b1SSanjay R Mehta cmd->pt_cmd_callback(cmd->data, cmd->ret); 144*b0b4a6b1SSanjay R Mehta } 145*b0b4a6b1SSanjay R Mehta 146fa5d823bSSanjay R Mehta static irqreturn_t pt_core_irq_handler(int irq, void *data) 147fa5d823bSSanjay R Mehta { 148fa5d823bSSanjay R Mehta struct pt_device *pt = data; 149fa5d823bSSanjay R Mehta struct pt_cmd_queue *cmd_q = &pt->cmd_q; 150fa5d823bSSanjay R Mehta u32 status; 151fa5d823bSSanjay R Mehta 152fa5d823bSSanjay R Mehta pt_core_disable_queue_interrupts(pt); 153fa5d823bSSanjay R Mehta status = ioread32(cmd_q->reg_control + 0x0010); 154fa5d823bSSanjay R Mehta if (status) { 155fa5d823bSSanjay R Mehta cmd_q->int_status = status; 156fa5d823bSSanjay R Mehta cmd_q->q_status = ioread32(cmd_q->reg_control + 0x0100); 157fa5d823bSSanjay R Mehta cmd_q->q_int_status = ioread32(cmd_q->reg_control + 0x0104); 158fa5d823bSSanjay R Mehta 159fa5d823bSSanjay R Mehta /* On error, only save the first error value */ 160fa5d823bSSanjay R Mehta if ((status & INT_ERROR) && !cmd_q->cmd_error) 161fa5d823bSSanjay R Mehta cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status); 162fa5d823bSSanjay R Mehta 163fa5d823bSSanjay R Mehta /* Acknowledge the interrupt */ 164fa5d823bSSanjay R Mehta iowrite32(status, cmd_q->reg_control + 0x0010); 165fa5d823bSSanjay R Mehta pt_core_enable_queue_interrupts(pt); 166*b0b4a6b1SSanjay R Mehta pt_do_cmd_complete((ulong)&pt->tdata); 167fa5d823bSSanjay R Mehta } 168fa5d823bSSanjay R Mehta return IRQ_HANDLED; 169fa5d823bSSanjay R Mehta } 170fa5d823bSSanjay R Mehta 171fa5d823bSSanjay R Mehta int pt_core_init(struct pt_device *pt) 172fa5d823bSSanjay R Mehta { 173fa5d823bSSanjay R Mehta char dma_pool_name[MAX_DMAPOOL_NAME_LEN]; 174fa5d823bSSanjay R Mehta struct pt_cmd_queue *cmd_q = &pt->cmd_q; 175fa5d823bSSanjay R Mehta u32 dma_addr_lo, dma_addr_hi; 176fa5d823bSSanjay R Mehta struct device *dev = pt->dev; 177fa5d823bSSanjay R Mehta struct dma_pool *dma_pool; 178fa5d823bSSanjay R Mehta int ret; 179fa5d823bSSanjay R Mehta 180fa5d823bSSanjay R Mehta /* Allocate a dma pool for the queue */ 181fa5d823bSSanjay R Mehta snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q", dev_name(pt->dev)); 182fa5d823bSSanjay R Mehta 183fa5d823bSSanjay R Mehta dma_pool = dma_pool_create(dma_pool_name, dev, 184fa5d823bSSanjay R Mehta PT_DMAPOOL_MAX_SIZE, 185fa5d823bSSanjay R Mehta PT_DMAPOOL_ALIGN, 0); 186fa5d823bSSanjay R Mehta if (!dma_pool) 187fa5d823bSSanjay R Mehta return -ENOMEM; 188fa5d823bSSanjay R Mehta 189fa5d823bSSanjay R Mehta /* ptdma core initialisation */ 190fa5d823bSSanjay R Mehta iowrite32(CMD_CONFIG_VHB_EN, pt->io_regs + CMD_CONFIG_OFFSET); 191fa5d823bSSanjay R Mehta iowrite32(CMD_QUEUE_PRIO, pt->io_regs + CMD_QUEUE_PRIO_OFFSET); 192fa5d823bSSanjay R Mehta iowrite32(CMD_TIMEOUT_DISABLE, pt->io_regs + CMD_TIMEOUT_OFFSET); 193fa5d823bSSanjay R Mehta iowrite32(CMD_CLK_GATE_CONFIG, pt->io_regs + CMD_CLK_GATE_CTL_OFFSET); 194fa5d823bSSanjay R Mehta iowrite32(CMD_CONFIG_REQID, pt->io_regs + CMD_REQID_CONFIG_OFFSET); 195fa5d823bSSanjay R Mehta 196fa5d823bSSanjay R Mehta cmd_q->pt = pt; 197fa5d823bSSanjay R Mehta cmd_q->dma_pool = dma_pool; 198fa5d823bSSanjay R Mehta mutex_init(&cmd_q->q_mutex); 199fa5d823bSSanjay R Mehta 200fa5d823bSSanjay R Mehta /* Page alignment satisfies our needs for N <= 128 */ 201fa5d823bSSanjay R Mehta cmd_q->qsize = Q_SIZE(Q_DESC_SIZE); 202fa5d823bSSanjay R Mehta cmd_q->qbase = dma_alloc_coherent(dev, cmd_q->qsize, 203fa5d823bSSanjay R Mehta &cmd_q->qbase_dma, 204fa5d823bSSanjay R Mehta GFP_KERNEL); 205fa5d823bSSanjay R Mehta if (!cmd_q->qbase) { 206fa5d823bSSanjay R Mehta dev_err(dev, "unable to allocate command queue\n"); 207fa5d823bSSanjay R Mehta ret = -ENOMEM; 208fa5d823bSSanjay R Mehta goto e_dma_alloc; 209fa5d823bSSanjay R Mehta } 210fa5d823bSSanjay R Mehta 211fa5d823bSSanjay R Mehta cmd_q->qidx = 0; 212fa5d823bSSanjay R Mehta 213fa5d823bSSanjay R Mehta /* Preset some register values */ 214fa5d823bSSanjay R Mehta cmd_q->reg_control = pt->io_regs + CMD_Q_STATUS_INCR; 215fa5d823bSSanjay R Mehta 216fa5d823bSSanjay R Mehta /* Turn off the queues and disable interrupts until ready */ 217fa5d823bSSanjay R Mehta pt_core_disable_queue_interrupts(pt); 218fa5d823bSSanjay R Mehta 219fa5d823bSSanjay R Mehta cmd_q->qcontrol = 0; /* Start with nothing */ 220fa5d823bSSanjay R Mehta iowrite32(cmd_q->qcontrol, cmd_q->reg_control); 221fa5d823bSSanjay R Mehta 222fa5d823bSSanjay R Mehta ioread32(cmd_q->reg_control + 0x0104); 223fa5d823bSSanjay R Mehta ioread32(cmd_q->reg_control + 0x0100); 224fa5d823bSSanjay R Mehta 225fa5d823bSSanjay R Mehta /* Clear the interrupt status */ 226fa5d823bSSanjay R Mehta iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_control + 0x0010); 227fa5d823bSSanjay R Mehta 228fa5d823bSSanjay R Mehta /* Request an irq */ 229fa5d823bSSanjay R Mehta ret = request_irq(pt->pt_irq, pt_core_irq_handler, 0, dev_name(pt->dev), pt); 230fa5d823bSSanjay R Mehta if (ret) 231fa5d823bSSanjay R Mehta goto e_pool; 232fa5d823bSSanjay R Mehta 233fa5d823bSSanjay R Mehta /* Update the device registers with queue information. */ 234fa5d823bSSanjay R Mehta cmd_q->qcontrol &= ~CMD_Q_SIZE; 235fa5d823bSSanjay R Mehta cmd_q->qcontrol |= FIELD_PREP(CMD_Q_SIZE, QUEUE_SIZE_VAL); 236fa5d823bSSanjay R Mehta 237fa5d823bSSanjay R Mehta cmd_q->qdma_tail = cmd_q->qbase_dma; 238fa5d823bSSanjay R Mehta dma_addr_lo = lower_32_bits(cmd_q->qdma_tail); 239fa5d823bSSanjay R Mehta iowrite32((u32)dma_addr_lo, cmd_q->reg_control + 0x0004); 240fa5d823bSSanjay R Mehta iowrite32((u32)dma_addr_lo, cmd_q->reg_control + 0x0008); 241fa5d823bSSanjay R Mehta 242fa5d823bSSanjay R Mehta dma_addr_hi = upper_32_bits(cmd_q->qdma_tail); 243fa5d823bSSanjay R Mehta cmd_q->qcontrol |= (dma_addr_hi << 16); 244fa5d823bSSanjay R Mehta iowrite32(cmd_q->qcontrol, cmd_q->reg_control); 245fa5d823bSSanjay R Mehta 246fa5d823bSSanjay R Mehta pt_core_enable_queue_interrupts(pt); 247fa5d823bSSanjay R Mehta 248*b0b4a6b1SSanjay R Mehta /* Register the DMA engine support */ 249*b0b4a6b1SSanjay R Mehta ret = pt_dmaengine_register(pt); 250*b0b4a6b1SSanjay R Mehta if (ret) 251*b0b4a6b1SSanjay R Mehta goto e_dmaengine; 252*b0b4a6b1SSanjay R Mehta 253fa5d823bSSanjay R Mehta return 0; 254fa5d823bSSanjay R Mehta 255*b0b4a6b1SSanjay R Mehta e_dmaengine: 256*b0b4a6b1SSanjay R Mehta free_irq(pt->pt_irq, pt); 257*b0b4a6b1SSanjay R Mehta 258fa5d823bSSanjay R Mehta e_dma_alloc: 259fa5d823bSSanjay R Mehta dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase, cmd_q->qbase_dma); 260fa5d823bSSanjay R Mehta 261fa5d823bSSanjay R Mehta e_pool: 262fa5d823bSSanjay R Mehta dev_err(dev, "unable to allocate an IRQ\n"); 263fa5d823bSSanjay R Mehta dma_pool_destroy(pt->cmd_q.dma_pool); 264fa5d823bSSanjay R Mehta 265fa5d823bSSanjay R Mehta return ret; 266fa5d823bSSanjay R Mehta } 267fa5d823bSSanjay R Mehta 268fa5d823bSSanjay R Mehta void pt_core_destroy(struct pt_device *pt) 269fa5d823bSSanjay R Mehta { 270fa5d823bSSanjay R Mehta struct device *dev = pt->dev; 271fa5d823bSSanjay R Mehta struct pt_cmd_queue *cmd_q = &pt->cmd_q; 272fa5d823bSSanjay R Mehta struct pt_cmd *cmd; 273fa5d823bSSanjay R Mehta 274*b0b4a6b1SSanjay R Mehta /* Unregister the DMA engine */ 275*b0b4a6b1SSanjay R Mehta pt_dmaengine_unregister(pt); 276*b0b4a6b1SSanjay R Mehta 277fa5d823bSSanjay R Mehta /* Disable and clear interrupts */ 278fa5d823bSSanjay R Mehta pt_core_disable_queue_interrupts(pt); 279fa5d823bSSanjay R Mehta 280fa5d823bSSanjay R Mehta /* Turn off the run bit */ 281fa5d823bSSanjay R Mehta pt_stop_queue(cmd_q); 282fa5d823bSSanjay R Mehta 283fa5d823bSSanjay R Mehta /* Clear the interrupt status */ 284fa5d823bSSanjay R Mehta iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_control + 0x0010); 285fa5d823bSSanjay R Mehta ioread32(cmd_q->reg_control + 0x0104); 286fa5d823bSSanjay R Mehta ioread32(cmd_q->reg_control + 0x0100); 287fa5d823bSSanjay R Mehta 288fa5d823bSSanjay R Mehta free_irq(pt->pt_irq, pt); 289fa5d823bSSanjay R Mehta 290fa5d823bSSanjay R Mehta dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase, 291fa5d823bSSanjay R Mehta cmd_q->qbase_dma); 292fa5d823bSSanjay R Mehta 293fa5d823bSSanjay R Mehta /* Flush the cmd queue */ 294fa5d823bSSanjay R Mehta while (!list_empty(&pt->cmd)) { 295fa5d823bSSanjay R Mehta /* Invoke the callback directly with an error code */ 296fa5d823bSSanjay R Mehta cmd = list_first_entry(&pt->cmd, struct pt_cmd, entry); 297fa5d823bSSanjay R Mehta list_del(&cmd->entry); 298fa5d823bSSanjay R Mehta cmd->pt_cmd_callback(cmd->data, -ENODEV); 299fa5d823bSSanjay R Mehta } 300fa5d823bSSanjay R Mehta } 301