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