linux/drivers/dma/xgene-dma.c
<<
>>
Prefs
   1/*
   2 * Applied Micro X-Gene SoC DMA engine Driver
   3 *
   4 * Copyright (c) 2015, Applied Micro Circuits Corporation
   5 * Authors: Rameshwar Prasad Sahu <rsahu@apm.com>
   6 *          Loc Ho <lho@apm.com>
   7 *
   8 * This program is free software; you can redistribute  it and/or modify it
   9 * under  the terms of  the GNU General  Public License as published by the
  10 * Free Software Foundation;  either version 2 of the  License, or (at your
  11 * option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20 *
  21 * NOTE: PM support is currently not available.
  22 */
  23
  24#include <linux/acpi.h>
  25#include <linux/clk.h>
  26#include <linux/delay.h>
  27#include <linux/dma-mapping.h>
  28#include <linux/dmaengine.h>
  29#include <linux/dmapool.h>
  30#include <linux/interrupt.h>
  31#include <linux/io.h>
  32#include <linux/irq.h>
  33#include <linux/module.h>
  34#include <linux/of_device.h>
  35
  36#include "dmaengine.h"
  37
  38/* X-Gene DMA ring csr registers and bit definations */
  39#define XGENE_DMA_RING_CONFIG                   0x04
  40#define XGENE_DMA_RING_ENABLE                   BIT(31)
  41#define XGENE_DMA_RING_ID                       0x08
  42#define XGENE_DMA_RING_ID_SETUP(v)              ((v) | BIT(31))
  43#define XGENE_DMA_RING_ID_BUF                   0x0C
  44#define XGENE_DMA_RING_ID_BUF_SETUP(v)          (((v) << 9) | BIT(21))
  45#define XGENE_DMA_RING_THRESLD0_SET1            0x30
  46#define XGENE_DMA_RING_THRESLD0_SET1_VAL        0X64
  47#define XGENE_DMA_RING_THRESLD1_SET1            0x34
  48#define XGENE_DMA_RING_THRESLD1_SET1_VAL        0xC8
  49#define XGENE_DMA_RING_HYSTERESIS               0x68
  50#define XGENE_DMA_RING_HYSTERESIS_VAL           0xFFFFFFFF
  51#define XGENE_DMA_RING_STATE                    0x6C
  52#define XGENE_DMA_RING_STATE_WR_BASE            0x70
  53#define XGENE_DMA_RING_NE_INT_MODE              0x017C
  54#define XGENE_DMA_RING_NE_INT_MODE_SET(m, v)    \
  55        ((m) = ((m) & ~BIT(31 - (v))) | BIT(31 - (v)))
  56#define XGENE_DMA_RING_NE_INT_MODE_RESET(m, v)  \
  57        ((m) &= (~BIT(31 - (v))))
  58#define XGENE_DMA_RING_CLKEN                    0xC208
  59#define XGENE_DMA_RING_SRST                     0xC200
  60#define XGENE_DMA_RING_MEM_RAM_SHUTDOWN         0xD070
  61#define XGENE_DMA_RING_BLK_MEM_RDY              0xD074
  62#define XGENE_DMA_RING_BLK_MEM_RDY_VAL          0xFFFFFFFF
  63#define XGENE_DMA_RING_ID_GET(owner, num)       (((owner) << 6) | (num))
  64#define XGENE_DMA_RING_DST_ID(v)                ((1 << 10) | (v))
  65#define XGENE_DMA_RING_CMD_OFFSET               0x2C
  66#define XGENE_DMA_RING_CMD_BASE_OFFSET(v)       ((v) << 6)
  67#define XGENE_DMA_RING_COHERENT_SET(m)          \
  68        (((u32 *)(m))[2] |= BIT(4))
  69#define XGENE_DMA_RING_ADDRL_SET(m, v)          \
  70        (((u32 *)(m))[2] |= (((v) >> 8) << 5))
  71#define XGENE_DMA_RING_ADDRH_SET(m, v)          \
  72        (((u32 *)(m))[3] |= ((v) >> 35))
  73#define XGENE_DMA_RING_ACCEPTLERR_SET(m)        \
  74        (((u32 *)(m))[3] |= BIT(19))
  75#define XGENE_DMA_RING_SIZE_SET(m, v)           \
  76        (((u32 *)(m))[3] |= ((v) << 23))
  77#define XGENE_DMA_RING_RECOMBBUF_SET(m)         \
  78        (((u32 *)(m))[3] |= BIT(27))
  79#define XGENE_DMA_RING_RECOMTIMEOUTL_SET(m)     \
  80        (((u32 *)(m))[3] |= (0x7 << 28))
  81#define XGENE_DMA_RING_RECOMTIMEOUTH_SET(m)     \
  82        (((u32 *)(m))[4] |= 0x3)
  83#define XGENE_DMA_RING_SELTHRSH_SET(m)          \
  84        (((u32 *)(m))[4] |= BIT(3))
  85#define XGENE_DMA_RING_TYPE_SET(m, v)           \
  86        (((u32 *)(m))[4] |= ((v) << 19))
  87
  88/* X-Gene DMA device csr registers and bit definitions */
  89#define XGENE_DMA_IPBRR                         0x0
  90#define XGENE_DMA_DEV_ID_RD(v)                  ((v) & 0x00000FFF)
  91#define XGENE_DMA_BUS_ID_RD(v)                  (((v) >> 12) & 3)
  92#define XGENE_DMA_REV_NO_RD(v)                  (((v) >> 14) & 3)
  93#define XGENE_DMA_GCR                           0x10
  94#define XGENE_DMA_CH_SETUP(v)                   \
  95        ((v) = ((v) & ~0x000FFFFF) | 0x000AAFFF)
  96#define XGENE_DMA_ENABLE(v)                     ((v) |= BIT(31))
  97#define XGENE_DMA_DISABLE(v)                    ((v) &= ~BIT(31))
  98#define XGENE_DMA_RAID6_CONT                    0x14
  99#define XGENE_DMA_RAID6_MULTI_CTRL(v)           ((v) << 24)
 100#define XGENE_DMA_INT                           0x70
 101#define XGENE_DMA_INT_MASK                      0x74
 102#define XGENE_DMA_INT_ALL_MASK                  0xFFFFFFFF
 103#define XGENE_DMA_INT_ALL_UNMASK                0x0
 104#define XGENE_DMA_INT_MASK_SHIFT                0x14
 105#define XGENE_DMA_RING_INT0_MASK                0x90A0
 106#define XGENE_DMA_RING_INT1_MASK                0x90A8
 107#define XGENE_DMA_RING_INT2_MASK                0x90B0
 108#define XGENE_DMA_RING_INT3_MASK                0x90B8
 109#define XGENE_DMA_RING_INT4_MASK                0x90C0
 110#define XGENE_DMA_CFG_RING_WQ_ASSOC             0x90E0
 111#define XGENE_DMA_ASSOC_RING_MNGR1              0xFFFFFFFF
 112#define XGENE_DMA_MEM_RAM_SHUTDOWN              0xD070
 113#define XGENE_DMA_BLK_MEM_RDY                   0xD074
 114#define XGENE_DMA_BLK_MEM_RDY_VAL               0xFFFFFFFF
 115#define XGENE_DMA_RING_CMD_SM_OFFSET            0x8000
 116
 117/* X-Gene SoC EFUSE csr register and bit defination */
 118#define XGENE_SOC_JTAG1_SHADOW                  0x18
 119#define XGENE_DMA_PQ_DISABLE_MASK               BIT(13)
 120
 121/* X-Gene DMA Descriptor format */
 122#define XGENE_DMA_DESC_NV_BIT                   BIT_ULL(50)
 123#define XGENE_DMA_DESC_IN_BIT                   BIT_ULL(55)
 124#define XGENE_DMA_DESC_C_BIT                    BIT_ULL(63)
 125#define XGENE_DMA_DESC_DR_BIT                   BIT_ULL(61)
 126#define XGENE_DMA_DESC_ELERR_POS                46
 127#define XGENE_DMA_DESC_RTYPE_POS                56
 128#define XGENE_DMA_DESC_LERR_POS                 60
 129#define XGENE_DMA_DESC_BUFLEN_POS               48
 130#define XGENE_DMA_DESC_HOENQ_NUM_POS            48
 131#define XGENE_DMA_DESC_ELERR_RD(m)              \
 132        (((m) >> XGENE_DMA_DESC_ELERR_POS) & 0x3)
 133#define XGENE_DMA_DESC_LERR_RD(m)               \
 134        (((m) >> XGENE_DMA_DESC_LERR_POS) & 0x7)
 135#define XGENE_DMA_DESC_STATUS(elerr, lerr)      \
 136        (((elerr) << 4) | (lerr))
 137
 138/* X-Gene DMA descriptor empty s/w signature */
 139#define XGENE_DMA_DESC_EMPTY_SIGNATURE          ~0ULL
 140
 141/* X-Gene DMA configurable parameters defines */
 142#define XGENE_DMA_RING_NUM              512
 143#define XGENE_DMA_BUFNUM                0x0
 144#define XGENE_DMA_CPU_BUFNUM            0x18
 145#define XGENE_DMA_RING_OWNER_DMA        0x03
 146#define XGENE_DMA_RING_OWNER_CPU        0x0F
 147#define XGENE_DMA_RING_TYPE_REGULAR     0x01
 148#define XGENE_DMA_RING_WQ_DESC_SIZE     32      /* 32 Bytes */
 149#define XGENE_DMA_RING_NUM_CONFIG       5
 150#define XGENE_DMA_MAX_CHANNEL           4
 151#define XGENE_DMA_XOR_CHANNEL           0
 152#define XGENE_DMA_PQ_CHANNEL            1
 153#define XGENE_DMA_MAX_BYTE_CNT          0x4000  /* 16 KB */
 154#define XGENE_DMA_MAX_64B_DESC_BYTE_CNT 0x14000 /* 80 KB */
 155#define XGENE_DMA_MAX_XOR_SRC           5
 156#define XGENE_DMA_16K_BUFFER_LEN_CODE   0x0
 157#define XGENE_DMA_INVALID_LEN_CODE      0x7800000000000000ULL
 158
 159/* X-Gene DMA descriptor error codes */
 160#define ERR_DESC_AXI                    0x01
 161#define ERR_BAD_DESC                    0x02
 162#define ERR_READ_DATA_AXI               0x03
 163#define ERR_WRITE_DATA_AXI              0x04
 164#define ERR_FBP_TIMEOUT                 0x05
 165#define ERR_ECC                         0x06
 166#define ERR_DIFF_SIZE                   0x08
 167#define ERR_SCT_GAT_LEN                 0x09
 168#define ERR_CRC_ERR                     0x11
 169#define ERR_CHKSUM                      0x12
 170#define ERR_DIF                         0x13
 171
 172/* X-Gene DMA error interrupt codes */
 173#define ERR_DIF_SIZE_INT                0x0
 174#define ERR_GS_ERR_INT                  0x1
 175#define ERR_FPB_TIMEO_INT               0x2
 176#define ERR_WFIFO_OVF_INT               0x3
 177#define ERR_RFIFO_OVF_INT               0x4
 178#define ERR_WR_TIMEO_INT                0x5
 179#define ERR_RD_TIMEO_INT                0x6
 180#define ERR_WR_ERR_INT                  0x7
 181#define ERR_RD_ERR_INT                  0x8
 182#define ERR_BAD_DESC_INT                0x9
 183#define ERR_DESC_DST_INT                0xA
 184#define ERR_DESC_SRC_INT                0xB
 185
 186/* X-Gene DMA flyby operation code */
 187#define FLYBY_2SRC_XOR                  0x80
 188#define FLYBY_3SRC_XOR                  0x90
 189#define FLYBY_4SRC_XOR                  0xA0
 190#define FLYBY_5SRC_XOR                  0xB0
 191
 192/* X-Gene DMA SW descriptor flags */
 193#define XGENE_DMA_FLAG_64B_DESC         BIT(0)
 194
 195/* Define to dump X-Gene DMA descriptor */
 196#define XGENE_DMA_DESC_DUMP(desc, m)    \
 197        print_hex_dump(KERN_ERR, (m),   \
 198                        DUMP_PREFIX_ADDRESS, 16, 8, (desc), 32, 0)
 199
 200#define to_dma_desc_sw(tx)              \
 201        container_of(tx, struct xgene_dma_desc_sw, tx)
 202#define to_dma_chan(dchan)              \
 203        container_of(dchan, struct xgene_dma_chan, dma_chan)
 204
 205#define chan_dbg(chan, fmt, arg...)     \
 206        dev_dbg(chan->dev, "%s: " fmt, chan->name, ##arg)
 207#define chan_err(chan, fmt, arg...)     \
 208        dev_err(chan->dev, "%s: " fmt, chan->name, ##arg)
 209
 210struct xgene_dma_desc_hw {
 211        __le64 m0;
 212        __le64 m1;
 213        __le64 m2;
 214        __le64 m3;
 215};
 216
 217enum xgene_dma_ring_cfgsize {
 218        XGENE_DMA_RING_CFG_SIZE_512B,
 219        XGENE_DMA_RING_CFG_SIZE_2KB,
 220        XGENE_DMA_RING_CFG_SIZE_16KB,
 221        XGENE_DMA_RING_CFG_SIZE_64KB,
 222        XGENE_DMA_RING_CFG_SIZE_512KB,
 223        XGENE_DMA_RING_CFG_SIZE_INVALID
 224};
 225
 226struct xgene_dma_ring {
 227        struct xgene_dma *pdma;
 228        u8 buf_num;
 229        u16 id;
 230        u16 num;
 231        u16 head;
 232        u16 owner;
 233        u16 slots;
 234        u16 dst_ring_num;
 235        u32 size;
 236        void __iomem *cmd;
 237        void __iomem *cmd_base;
 238        dma_addr_t desc_paddr;
 239        u32 state[XGENE_DMA_RING_NUM_CONFIG];
 240        enum xgene_dma_ring_cfgsize cfgsize;
 241        union {
 242                void *desc_vaddr;
 243                struct xgene_dma_desc_hw *desc_hw;
 244        };
 245};
 246
 247struct xgene_dma_desc_sw {
 248        struct xgene_dma_desc_hw desc1;
 249        struct xgene_dma_desc_hw desc2;
 250        u32 flags;
 251        struct list_head node;
 252        struct list_head tx_list;
 253        struct dma_async_tx_descriptor tx;
 254};
 255
 256/**
 257 * struct xgene_dma_chan - internal representation of an X-Gene DMA channel
 258 * @dma_chan: dmaengine channel object member
 259 * @pdma: X-Gene DMA device structure reference
 260 * @dev: struct device reference for dma mapping api
 261 * @id: raw id of this channel
 262 * @rx_irq: channel IRQ
 263 * @name: name of X-Gene DMA channel
 264 * @lock: serializes enqueue/dequeue operations to the descriptor pool
 265 * @pending: number of transaction request pushed to DMA controller for
 266 *      execution, but still waiting for completion,
 267 * @max_outstanding: max number of outstanding request we can push to channel
 268 * @ld_pending: descriptors which are queued to run, but have not yet been
 269 *      submitted to the hardware for execution
 270 * @ld_running: descriptors which are currently being executing by the hardware
 271 * @ld_completed: descriptors which have finished execution by the hardware.
 272 *      These descriptors have already had their cleanup actions run. They
 273 *      are waiting for the ACK bit to be set by the async tx API.
 274 * @desc_pool: descriptor pool for DMA operations
 275 * @tasklet: bottom half where all completed descriptors cleans
 276 * @tx_ring: transmit ring descriptor that we use to prepare actual
 277 *      descriptors for further executions
 278 * @rx_ring: receive ring descriptor that we use to get completed DMA
 279 *      descriptors during cleanup time
 280 */
 281struct xgene_dma_chan {
 282        struct dma_chan dma_chan;
 283        struct xgene_dma *pdma;
 284        struct device *dev;
 285        int id;
 286        int rx_irq;
 287        char name[10];
 288        spinlock_t lock;
 289        int pending;
 290        int max_outstanding;
 291        struct list_head ld_pending;
 292        struct list_head ld_running;
 293        struct list_head ld_completed;
 294        struct dma_pool *desc_pool;
 295        struct tasklet_struct tasklet;
 296        struct xgene_dma_ring tx_ring;
 297        struct xgene_dma_ring rx_ring;
 298};
 299
 300/**
 301 * struct xgene_dma - internal representation of an X-Gene DMA device
 302 * @err_irq: DMA error irq number
 303 * @ring_num: start id number for DMA ring
 304 * @csr_dma: base for DMA register access
 305 * @csr_ring: base for DMA ring register access
 306 * @csr_ring_cmd: base for DMA ring command register access
 307 * @csr_efuse: base for efuse register access
 308 * @dma_dev: embedded struct dma_device
 309 * @chan: reference to X-Gene DMA channels
 310 */
 311struct xgene_dma {
 312        struct device *dev;
 313        struct clk *clk;
 314        int err_irq;
 315        int ring_num;
 316        void __iomem *csr_dma;
 317        void __iomem *csr_ring;
 318        void __iomem *csr_ring_cmd;
 319        void __iomem *csr_efuse;
 320        struct dma_device dma_dev[XGENE_DMA_MAX_CHANNEL];
 321        struct xgene_dma_chan chan[XGENE_DMA_MAX_CHANNEL];
 322};
 323
 324static const char * const xgene_dma_desc_err[] = {
 325        [ERR_DESC_AXI] = "AXI error when reading src/dst link list",
 326        [ERR_BAD_DESC] = "ERR or El_ERR fields not set to zero in desc",
 327        [ERR_READ_DATA_AXI] = "AXI error when reading data",
 328        [ERR_WRITE_DATA_AXI] = "AXI error when writing data",
 329        [ERR_FBP_TIMEOUT] = "Timeout on bufpool fetch",
 330        [ERR_ECC] = "ECC double bit error",
 331        [ERR_DIFF_SIZE] = "Bufpool too small to hold all the DIF result",
 332        [ERR_SCT_GAT_LEN] = "Gather and scatter data length not same",
 333        [ERR_CRC_ERR] = "CRC error",
 334        [ERR_CHKSUM] = "Checksum error",
 335        [ERR_DIF] = "DIF error",
 336};
 337
 338static const char * const xgene_dma_err[] = {
 339        [ERR_DIF_SIZE_INT] = "DIF size error",
 340        [ERR_GS_ERR_INT] = "Gather scatter not same size error",
 341        [ERR_FPB_TIMEO_INT] = "Free pool time out error",
 342        [ERR_WFIFO_OVF_INT] = "Write FIFO over flow error",
 343        [ERR_RFIFO_OVF_INT] = "Read FIFO over flow error",
 344        [ERR_WR_TIMEO_INT] = "Write time out error",
 345        [ERR_RD_TIMEO_INT] = "Read time out error",
 346        [ERR_WR_ERR_INT] = "HBF bus write error",
 347        [ERR_RD_ERR_INT] = "HBF bus read error",
 348        [ERR_BAD_DESC_INT] = "Ring descriptor HE0 not set error",
 349        [ERR_DESC_DST_INT] = "HFB reading dst link address error",
 350        [ERR_DESC_SRC_INT] = "HFB reading src link address error",
 351};
 352
 353static bool is_pq_enabled(struct xgene_dma *pdma)
 354{
 355        u32 val;
 356
 357        val = ioread32(pdma->csr_efuse + XGENE_SOC_JTAG1_SHADOW);
 358        return !(val & XGENE_DMA_PQ_DISABLE_MASK);
 359}
 360
 361static u64 xgene_dma_encode_len(size_t len)
 362{
 363        return (len < XGENE_DMA_MAX_BYTE_CNT) ?
 364                ((u64)len << XGENE_DMA_DESC_BUFLEN_POS) :
 365                XGENE_DMA_16K_BUFFER_LEN_CODE;
 366}
 367
 368static u8 xgene_dma_encode_xor_flyby(u32 src_cnt)
 369{
 370        static u8 flyby_type[] = {
 371                FLYBY_2SRC_XOR, /* Dummy */
 372                FLYBY_2SRC_XOR, /* Dummy */
 373                FLYBY_2SRC_XOR,
 374                FLYBY_3SRC_XOR,
 375                FLYBY_4SRC_XOR,
 376                FLYBY_5SRC_XOR
 377        };
 378
 379        return flyby_type[src_cnt];
 380}
 381
 382static void xgene_dma_set_src_buffer(__le64 *ext8, size_t *len,
 383                                     dma_addr_t *paddr)
 384{
 385        size_t nbytes = (*len < XGENE_DMA_MAX_BYTE_CNT) ?
 386                        *len : XGENE_DMA_MAX_BYTE_CNT;
 387
 388        *ext8 |= cpu_to_le64(*paddr);
 389        *ext8 |= cpu_to_le64(xgene_dma_encode_len(nbytes));
 390        *len -= nbytes;
 391        *paddr += nbytes;
 392}
 393
 394static __le64 *xgene_dma_lookup_ext8(struct xgene_dma_desc_hw *desc, int idx)
 395{
 396        switch (idx) {
 397        case 0:
 398                return &desc->m1;
 399        case 1:
 400                return &desc->m0;
 401        case 2:
 402                return &desc->m3;
 403        case 3:
 404                return &desc->m2;
 405        default:
 406                pr_err("Invalid dma descriptor index\n");
 407        }
 408
 409        return NULL;
 410}
 411
 412static void xgene_dma_init_desc(struct xgene_dma_desc_hw *desc,
 413                                u16 dst_ring_num)
 414{
 415        desc->m0 |= cpu_to_le64(XGENE_DMA_DESC_IN_BIT);
 416        desc->m0 |= cpu_to_le64((u64)XGENE_DMA_RING_OWNER_DMA <<
 417                                XGENE_DMA_DESC_RTYPE_POS);
 418        desc->m1 |= cpu_to_le64(XGENE_DMA_DESC_C_BIT);
 419        desc->m3 |= cpu_to_le64((u64)dst_ring_num <<
 420                                XGENE_DMA_DESC_HOENQ_NUM_POS);
 421}
 422
 423static void xgene_dma_prep_cpy_desc(struct xgene_dma_chan *chan,
 424                                    struct xgene_dma_desc_sw *desc_sw,
 425                                    dma_addr_t dst, dma_addr_t src,
 426                                    size_t len)
 427{
 428        struct xgene_dma_desc_hw *desc1, *desc2;
 429        int i;
 430
 431        /* Get 1st descriptor */
 432        desc1 = &desc_sw->desc1;
 433        xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
 434
 435        /* Set destination address */
 436        desc1->m2 |= cpu_to_le64(XGENE_DMA_DESC_DR_BIT);
 437        desc1->m3 |= cpu_to_le64(dst);
 438
 439        /* Set 1st source address */
 440        xgene_dma_set_src_buffer(&desc1->m1, &len, &src);
 441
 442        if (!len)
 443                return;
 444
 445        /*
 446         * We need to split this source buffer,
 447         * and need to use 2nd descriptor
 448         */
 449        desc2 = &desc_sw->desc2;
 450        desc1->m0 |= cpu_to_le64(XGENE_DMA_DESC_NV_BIT);
 451
 452        /* Set 2nd to 5th source address */
 453        for (i = 0; i < 4 && len; i++)
 454                xgene_dma_set_src_buffer(xgene_dma_lookup_ext8(desc2, i),
 455                                         &len, &src);
 456
 457        /* Invalidate unused source address field */
 458        for (; i < 4; i++)
 459                xgene_dma_invalidate_buffer(xgene_dma_lookup_ext8(desc2, i));
 460
 461        /* Updated flag that we have prepared 64B descriptor */
 462        desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
 463}
 464
 465static void xgene_dma_prep_xor_desc(struct xgene_dma_chan *chan,
 466                                    struct xgene_dma_desc_sw *desc_sw,
 467                                    dma_addr_t *dst, dma_addr_t *src,
 468                                    u32 src_cnt, size_t *nbytes,
 469                                    const u8 *scf)
 470{
 471        struct xgene_dma_desc_hw *desc1, *desc2;
 472        size_t len = *nbytes;
 473        int i;
 474
 475        desc1 = &desc_sw->desc1;
 476        desc2 = &desc_sw->desc2;
 477
 478        /* Initialize DMA descriptor */
 479        xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
 480
 481        /* Set destination address */
 482        desc1->m2 |= cpu_to_le64(XGENE_DMA_DESC_DR_BIT);
 483        desc1->m3 |= cpu_to_le64(*dst);
 484
 485        /* We have multiple source addresses, so need to set NV bit*/
 486        desc1->m0 |= cpu_to_le64(XGENE_DMA_DESC_NV_BIT);
 487
 488        /* Set flyby opcode */
 489        desc1->m2 |= cpu_to_le64(xgene_dma_encode_xor_flyby(src_cnt));
 490
 491        /* Set 1st to 5th source addresses */
 492        for (i = 0; i < src_cnt; i++) {
 493                len = *nbytes;
 494                xgene_dma_set_src_buffer((i == 0) ? &desc1->m1 :
 495                                         xgene_dma_lookup_ext8(desc2, i - 1),
 496                                         &len, &src[i]);
 497                desc1->m2 |= cpu_to_le64((scf[i] << ((i + 1) * 8)));
 498        }
 499
 500        /* Update meta data */
 501        *nbytes = len;
 502        *dst += XGENE_DMA_MAX_BYTE_CNT;
 503
 504        /* We need always 64B descriptor to perform xor or pq operations */
 505        desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
 506}
 507
 508static dma_cookie_t xgene_dma_tx_submit(struct dma_async_tx_descriptor *tx)
 509{
 510        struct xgene_dma_desc_sw *desc;
 511        struct xgene_dma_chan *chan;
 512        dma_cookie_t cookie;
 513
 514        if (unlikely(!tx))
 515                return -EINVAL;
 516
 517        chan = to_dma_chan(tx->chan);
 518        desc = to_dma_desc_sw(tx);
 519
 520        spin_lock_bh(&chan->lock);
 521
 522        cookie = dma_cookie_assign(tx);
 523
 524        /* Add this transaction list onto the tail of the pending queue */
 525        list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
 526
 527        spin_unlock_bh(&chan->lock);
 528
 529        return cookie;
 530}
 531
 532static void xgene_dma_clean_descriptor(struct xgene_dma_chan *chan,
 533                                       struct xgene_dma_desc_sw *desc)
 534{
 535        list_del(&desc->node);
 536        chan_dbg(chan, "LD %p free\n", desc);
 537        dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
 538}
 539
 540static struct xgene_dma_desc_sw *xgene_dma_alloc_descriptor(
 541                                 struct xgene_dma_chan *chan)
 542{
 543        struct xgene_dma_desc_sw *desc;
 544        dma_addr_t phys;
 545
 546        desc = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
 547        if (!desc) {
 548                chan_err(chan, "Failed to allocate LDs\n");
 549                return NULL;
 550        }
 551
 552        INIT_LIST_HEAD(&desc->tx_list);
 553        desc->tx.phys = phys;
 554        desc->tx.tx_submit = xgene_dma_tx_submit;
 555        dma_async_tx_descriptor_init(&desc->tx, &chan->dma_chan);
 556
 557        chan_dbg(chan, "LD %p allocated\n", desc);
 558
 559        return desc;
 560}
 561
 562/**
 563 * xgene_dma_clean_completed_descriptor - free all descriptors which
 564 * has been completed and acked
 565 * @chan: X-Gene DMA channel
 566 *
 567 * This function is used on all completed and acked descriptors.
 568 */
 569static void xgene_dma_clean_completed_descriptor(struct xgene_dma_chan *chan)
 570{
 571        struct xgene_dma_desc_sw *desc, *_desc;
 572
 573        /* Run the callback for each descriptor, in order */
 574        list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node) {
 575                if (async_tx_test_ack(&desc->tx))
 576                        xgene_dma_clean_descriptor(chan, desc);
 577        }
 578}
 579
 580/**
 581 * xgene_dma_run_tx_complete_actions - cleanup a single link descriptor
 582 * @chan: X-Gene DMA channel
 583 * @desc: descriptor to cleanup and free
 584 *
 585 * This function is used on a descriptor which has been executed by the DMA
 586 * controller. It will run any callbacks, submit any dependencies.
 587 */
 588static void xgene_dma_run_tx_complete_actions(struct xgene_dma_chan *chan,
 589                                              struct xgene_dma_desc_sw *desc)
 590{
 591        struct dma_async_tx_descriptor *tx = &desc->tx;
 592
 593        /*
 594         * If this is not the last transaction in the group,
 595         * then no need to complete cookie and run any callback as
 596         * this is not the tx_descriptor which had been sent to caller
 597         * of this DMA request
 598         */
 599
 600        if (tx->cookie == 0)
 601                return;
 602
 603        dma_cookie_complete(tx);
 604        dma_descriptor_unmap(tx);
 605
 606        /* Run the link descriptor callback function */
 607        dmaengine_desc_get_callback_invoke(tx, NULL);
 608
 609        /* Run any dependencies */
 610        dma_run_dependencies(tx);
 611}
 612
 613/**
 614 * xgene_dma_clean_running_descriptor - move the completed descriptor from
 615 * ld_running to ld_completed
 616 * @chan: X-Gene DMA channel
 617 * @desc: the descriptor which is completed
 618 *
 619 * Free the descriptor directly if acked by async_tx api,
 620 * else move it to queue ld_completed.
 621 */
 622static void xgene_dma_clean_running_descriptor(struct xgene_dma_chan *chan,
 623                                               struct xgene_dma_desc_sw *desc)
 624{
 625        /* Remove from the list of running transactions */
 626        list_del(&desc->node);
 627
 628        /*
 629         * the client is allowed to attach dependent operations
 630         * until 'ack' is set
 631         */
 632        if (!async_tx_test_ack(&desc->tx)) {
 633                /*
 634                 * Move this descriptor to the list of descriptors which is
 635                 * completed, but still awaiting the 'ack' bit to be set.
 636                 */
 637                list_add_tail(&desc->node, &chan->ld_completed);
 638                return;
 639        }
 640
 641        chan_dbg(chan, "LD %p free\n", desc);
 642        dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
 643}
 644
 645static void xgene_chan_xfer_request(struct xgene_dma_chan *chan,
 646                                    struct xgene_dma_desc_sw *desc_sw)
 647{
 648        struct xgene_dma_ring *ring = &chan->tx_ring;
 649        struct xgene_dma_desc_hw *desc_hw;
 650
 651        /* Get hw descriptor from DMA tx ring */
 652        desc_hw = &ring->desc_hw[ring->head];
 653
 654        /*
 655         * Increment the head count to point next
 656         * descriptor for next time
 657         */
 658        if (++ring->head == ring->slots)
 659                ring->head = 0;
 660
 661        /* Copy prepared sw descriptor data to hw descriptor */
 662        memcpy(desc_hw, &desc_sw->desc1, sizeof(*desc_hw));
 663
 664        /*
 665         * Check if we have prepared 64B descriptor,
 666         * in this case we need one more hw descriptor
 667         */
 668        if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) {
 669                desc_hw = &ring->desc_hw[ring->head];
 670
 671                if (++ring->head == ring->slots)
 672                        ring->head = 0;
 673
 674                memcpy(desc_hw, &desc_sw->desc2, sizeof(*desc_hw));
 675        }
 676
 677        /* Increment the pending transaction count */
 678        chan->pending += ((desc_sw->flags &
 679                          XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
 680
 681        /* Notify the hw that we have descriptor ready for execution */
 682        iowrite32((desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) ?
 683                  2 : 1, ring->cmd);
 684}
 685
 686/**
 687 * xgene_chan_xfer_ld_pending - push any pending transactions to hw
 688 * @chan : X-Gene DMA channel
 689 *
 690 * LOCKING: must hold chan->lock
 691 */
 692static void xgene_chan_xfer_ld_pending(struct xgene_dma_chan *chan)
 693{
 694        struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
 695
 696        /*
 697         * If the list of pending descriptors is empty, then we
 698         * don't need to do any work at all
 699         */
 700        if (list_empty(&chan->ld_pending)) {
 701                chan_dbg(chan, "No pending LDs\n");
 702                return;
 703        }
 704
 705        /*
 706         * Move elements from the queue of pending transactions onto the list
 707         * of running transactions and push it to hw for further executions
 708         */
 709        list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_pending, node) {
 710                /*
 711                 * Check if have pushed max number of transactions to hw
 712                 * as capable, so let's stop here and will push remaining
 713                 * elements from pening ld queue after completing some
 714                 * descriptors that we have already pushed
 715                 */
 716                if (chan->pending >= chan->max_outstanding)
 717                        return;
 718
 719                xgene_chan_xfer_request(chan, desc_sw);
 720
 721                /*
 722                 * Delete this element from ld pending queue and append it to
 723                 * ld running queue
 724                 */
 725                list_move_tail(&desc_sw->node, &chan->ld_running);
 726        }
 727}
 728
 729/**
 730 * xgene_dma_cleanup_descriptors - cleanup link descriptors which are completed
 731 * and move them to ld_completed to free until flag 'ack' is set
 732 * @chan: X-Gene DMA channel
 733 *
 734 * This function is used on descriptors which have been executed by the DMA
 735 * controller. It will run any callbacks, submit any dependencies, then
 736 * free these descriptors if flag 'ack' is set.
 737 */
 738static void xgene_dma_cleanup_descriptors(struct xgene_dma_chan *chan)
 739{
 740        struct xgene_dma_ring *ring = &chan->rx_ring;
 741        struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
 742        struct xgene_dma_desc_hw *desc_hw;
 743        struct list_head ld_completed;
 744        u8 status;
 745
 746        INIT_LIST_HEAD(&ld_completed);
 747
 748        spin_lock_bh(&chan->lock);
 749
 750        /* Clean already completed and acked descriptors */
 751        xgene_dma_clean_completed_descriptor(chan);
 752
 753        /* Move all completed descriptors to ld completed queue, in order */
 754        list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_running, node) {
 755                /* Get subsequent hw descriptor from DMA rx ring */
 756                desc_hw = &ring->desc_hw[ring->head];
 757
 758                /* Check if this descriptor has been completed */
 759                if (unlikely(le64_to_cpu(desc_hw->m0) ==
 760                             XGENE_DMA_DESC_EMPTY_SIGNATURE))
 761                        break;
 762
 763                if (++ring->head == ring->slots)
 764                        ring->head = 0;
 765
 766                /* Check if we have any error with DMA transactions */
 767                status = XGENE_DMA_DESC_STATUS(
 768                                XGENE_DMA_DESC_ELERR_RD(le64_to_cpu(
 769                                                        desc_hw->m0)),
 770                                XGENE_DMA_DESC_LERR_RD(le64_to_cpu(
 771                                                       desc_hw->m0)));
 772                if (status) {
 773                        /* Print the DMA error type */
 774                        chan_err(chan, "%s\n", xgene_dma_desc_err[status]);
 775
 776                        /*
 777                         * We have DMA transactions error here. Dump DMA Tx
 778                         * and Rx descriptors for this request */
 779                        XGENE_DMA_DESC_DUMP(&desc_sw->desc1,
 780                                            "X-Gene DMA TX DESC1: ");
 781
 782                        if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC)
 783                                XGENE_DMA_DESC_DUMP(&desc_sw->desc2,
 784                                                    "X-Gene DMA TX DESC2: ");
 785
 786                        XGENE_DMA_DESC_DUMP(desc_hw,
 787                                            "X-Gene DMA RX ERR DESC: ");
 788                }
 789
 790                /* Notify the hw about this completed descriptor */
 791                iowrite32(-1, ring->cmd);
 792
 793                /* Mark this hw descriptor as processed */
 794                desc_hw->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
 795
 796                /*
 797                 * Decrement the pending transaction count
 798                 * as we have processed one
 799                 */
 800                chan->pending -= ((desc_sw->flags &
 801                                  XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
 802
 803                /*
 804                 * Delete this node from ld running queue and append it to
 805                 * ld completed queue for further processing
 806                 */
 807                list_move_tail(&desc_sw->node, &ld_completed);
 808        }
 809
 810        /*
 811         * Start any pending transactions automatically
 812         * In the ideal case, we keep the DMA controller busy while we go
 813         * ahead and free the descriptors below.
 814         */
 815        xgene_chan_xfer_ld_pending(chan);
 816
 817        spin_unlock_bh(&chan->lock);
 818
 819        /* Run the callback for each descriptor, in order */
 820        list_for_each_entry_safe(desc_sw, _desc_sw, &ld_completed, node) {
 821                xgene_dma_run_tx_complete_actions(chan, desc_sw);
 822                xgene_dma_clean_running_descriptor(chan, desc_sw);
 823        }
 824}
 825
 826static int xgene_dma_alloc_chan_resources(struct dma_chan *dchan)
 827{
 828        struct xgene_dma_chan *chan = to_dma_chan(dchan);
 829
 830        /* Has this channel already been allocated? */
 831        if (chan->desc_pool)
 832                return 1;
 833
 834        chan->desc_pool = dma_pool_create(chan->name, chan->dev,
 835                                          sizeof(struct xgene_dma_desc_sw),
 836                                          0, 0);
 837        if (!chan->desc_pool) {
 838                chan_err(chan, "Failed to allocate descriptor pool\n");
 839                return -ENOMEM;
 840        }
 841
 842        chan_dbg(chan, "Allocate descripto pool\n");
 843
 844        return 1;
 845}
 846
 847/**
 848 * xgene_dma_free_desc_list - Free all descriptors in a queue
 849 * @chan: X-Gene DMA channel
 850 * @list: the list to free
 851 *
 852 * LOCKING: must hold chan->lock
 853 */
 854static void xgene_dma_free_desc_list(struct xgene_dma_chan *chan,
 855                                     struct list_head *list)
 856{
 857        struct xgene_dma_desc_sw *desc, *_desc;
 858
 859        list_for_each_entry_safe(desc, _desc, list, node)
 860                xgene_dma_clean_descriptor(chan, desc);
 861}
 862
 863static void xgene_dma_free_chan_resources(struct dma_chan *dchan)
 864{
 865        struct xgene_dma_chan *chan = to_dma_chan(dchan);
 866
 867        chan_dbg(chan, "Free all resources\n");
 868
 869        if (!chan->desc_pool)
 870                return;
 871
 872        /* Process all running descriptor */
 873        xgene_dma_cleanup_descriptors(chan);
 874
 875        spin_lock_bh(&chan->lock);
 876
 877        /* Clean all link descriptor queues */
 878        xgene_dma_free_desc_list(chan, &chan->ld_pending);
 879        xgene_dma_free_desc_list(chan, &chan->ld_running);
 880        xgene_dma_free_desc_list(chan, &chan->ld_completed);
 881
 882        spin_unlock_bh(&chan->lock);
 883
 884        /* Delete this channel DMA pool */
 885        dma_pool_destroy(chan->desc_pool);
 886        chan->desc_pool = NULL;
 887}
 888
 889static struct dma_async_tx_descriptor *xgene_dma_prep_sg(
 890        struct dma_chan *dchan, struct scatterlist *dst_sg,
 891        u32 dst_nents, struct scatterlist *src_sg,
 892        u32 src_nents, unsigned long flags)
 893{
 894        struct xgene_dma_desc_sw *first = NULL, *new = NULL;
 895        struct xgene_dma_chan *chan;
 896        size_t dst_avail, src_avail;
 897        dma_addr_t dst, src;
 898        size_t len;
 899
 900        if (unlikely(!dchan))
 901                return NULL;
 902
 903        if (unlikely(!dst_nents || !src_nents))
 904                return NULL;
 905
 906        if (unlikely(!dst_sg || !src_sg))
 907                return NULL;
 908
 909        chan = to_dma_chan(dchan);
 910
 911        /* Get prepared for the loop */
 912        dst_avail = sg_dma_len(dst_sg);
 913        src_avail = sg_dma_len(src_sg);
 914        dst_nents--;
 915        src_nents--;
 916
 917        /* Run until we are out of scatterlist entries */
 918        while (true) {
 919                /* Create the largest transaction possible */
 920                len = min_t(size_t, src_avail, dst_avail);
 921                len = min_t(size_t, len, XGENE_DMA_MAX_64B_DESC_BYTE_CNT);
 922                if (len == 0)
 923                        goto fetch;
 924
 925                dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) - dst_avail;
 926                src = sg_dma_address(src_sg) + sg_dma_len(src_sg) - src_avail;
 927
 928                /* Allocate the link descriptor from DMA pool */
 929                new = xgene_dma_alloc_descriptor(chan);
 930                if (!new)
 931                        goto fail;
 932
 933                /* Prepare DMA descriptor */
 934                xgene_dma_prep_cpy_desc(chan, new, dst, src, len);
 935
 936                if (!first)
 937                        first = new;
 938
 939                new->tx.cookie = 0;
 940                async_tx_ack(&new->tx);
 941
 942                /* update metadata */
 943                dst_avail -= len;
 944                src_avail -= len;
 945
 946                /* Insert the link descriptor to the LD ring */
 947                list_add_tail(&new->node, &first->tx_list);
 948
 949fetch:
 950                /* fetch the next dst scatterlist entry */
 951                if (dst_avail == 0) {
 952                        /* no more entries: we're done */
 953                        if (dst_nents == 0)
 954                                break;
 955
 956                        /* fetch the next entry: if there are no more: done */
 957                        dst_sg = sg_next(dst_sg);
 958                        if (!dst_sg)
 959                                break;
 960
 961                        dst_nents--;
 962                        dst_avail = sg_dma_len(dst_sg);
 963                }
 964
 965                /* fetch the next src scatterlist entry */
 966                if (src_avail == 0) {
 967                        /* no more entries: we're done */
 968                        if (src_nents == 0)
 969                                break;
 970
 971                        /* fetch the next entry: if there are no more: done */
 972                        src_sg = sg_next(src_sg);
 973                        if (!src_sg)
 974                                break;
 975
 976                        src_nents--;
 977                        src_avail = sg_dma_len(src_sg);
 978                }
 979        }
 980
 981        if (!new)
 982                return NULL;
 983
 984        new->tx.flags = flags; /* client is in control of this ack */
 985        new->tx.cookie = -EBUSY;
 986        list_splice(&first->tx_list, &new->tx_list);
 987
 988        return &new->tx;
 989fail:
 990        if (!first)
 991                return NULL;
 992
 993        xgene_dma_free_desc_list(chan, &first->tx_list);
 994        return NULL;
 995}
 996
 997static struct dma_async_tx_descriptor *xgene_dma_prep_xor(
 998        struct dma_chan *dchan, dma_addr_t dst, dma_addr_t *src,
 999        u32 src_cnt, size_t len, unsigned long flags)
1000{
1001        struct xgene_dma_desc_sw *first = NULL, *new;
1002        struct xgene_dma_chan *chan;
1003        static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {
1004                                0x01, 0x01, 0x01, 0x01, 0x01};
1005
1006        if (unlikely(!dchan || !len))
1007                return NULL;
1008
1009        chan = to_dma_chan(dchan);
1010
1011        do {
1012                /* Allocate the link descriptor from DMA pool */
1013                new = xgene_dma_alloc_descriptor(chan);
1014                if (!new)
1015                        goto fail;
1016
1017                /* Prepare xor DMA descriptor */
1018                xgene_dma_prep_xor_desc(chan, new, &dst, src,
1019                                        src_cnt, &len, multi);
1020
1021                if (!first)
1022                        first = new;
1023
1024                new->tx.cookie = 0;
1025                async_tx_ack(&new->tx);
1026
1027                /* Insert the link descriptor to the LD ring */
1028                list_add_tail(&new->node, &first->tx_list);
1029        } while (len);
1030
1031        new->tx.flags = flags; /* client is in control of this ack */
1032        new->tx.cookie = -EBUSY;
1033        list_splice(&first->tx_list, &new->tx_list);
1034
1035        return &new->tx;
1036
1037fail:
1038        if (!first)
1039                return NULL;
1040
1041        xgene_dma_free_desc_list(chan, &first->tx_list);
1042        return NULL;
1043}
1044
1045static struct dma_async_tx_descriptor *xgene_dma_prep_pq(
1046        struct dma_chan *dchan, dma_addr_t *dst, dma_addr_t *src,
1047        u32 src_cnt, const u8 *scf, size_t len, unsigned long flags)
1048{
1049        struct xgene_dma_desc_sw *first = NULL, *new;
1050        struct xgene_dma_chan *chan;
1051        size_t _len = len;
1052        dma_addr_t _src[XGENE_DMA_MAX_XOR_SRC];
1053        static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {0x01, 0x01, 0x01, 0x01, 0x01};
1054
1055        if (unlikely(!dchan || !len))
1056                return NULL;
1057
1058        chan = to_dma_chan(dchan);
1059
1060        /*
1061         * Save source addresses on local variable, may be we have to
1062         * prepare two descriptor to generate P and Q if both enabled
1063         * in the flags by client
1064         */
1065        memcpy(_src, src, sizeof(*src) * src_cnt);
1066
1067        if (flags & DMA_PREP_PQ_DISABLE_P)
1068                len = 0;
1069
1070        if (flags & DMA_PREP_PQ_DISABLE_Q)
1071                _len = 0;
1072
1073        do {
1074                /* Allocate the link descriptor from DMA pool */
1075                new = xgene_dma_alloc_descriptor(chan);
1076                if (!new)
1077                        goto fail;
1078
1079                if (!first)
1080                        first = new;
1081
1082                new->tx.cookie = 0;
1083                async_tx_ack(&new->tx);
1084
1085                /* Insert the link descriptor to the LD ring */
1086                list_add_tail(&new->node, &first->tx_list);
1087
1088                /*
1089                 * Prepare DMA descriptor to generate P,
1090                 * if DMA_PREP_PQ_DISABLE_P flag is not set
1091                 */
1092                if (len) {
1093                        xgene_dma_prep_xor_desc(chan, new, &dst[0], src,
1094                                                src_cnt, &len, multi);
1095                        continue;
1096                }
1097
1098                /*
1099                 * Prepare DMA descriptor to generate Q,
1100                 * if DMA_PREP_PQ_DISABLE_Q flag is not set
1101                 */
1102                if (_len) {
1103                        xgene_dma_prep_xor_desc(chan, new, &dst[1], _src,
1104                                                src_cnt, &_len, scf);
1105                }
1106        } while (len || _len);
1107
1108        new->tx.flags = flags; /* client is in control of this ack */
1109        new->tx.cookie = -EBUSY;
1110        list_splice(&first->tx_list, &new->tx_list);
1111
1112        return &new->tx;
1113
1114fail:
1115        if (!first)
1116                return NULL;
1117
1118        xgene_dma_free_desc_list(chan, &first->tx_list);
1119        return NULL;
1120}
1121
1122static void xgene_dma_issue_pending(struct dma_chan *dchan)
1123{
1124        struct xgene_dma_chan *chan = to_dma_chan(dchan);
1125
1126        spin_lock_bh(&chan->lock);
1127        xgene_chan_xfer_ld_pending(chan);
1128        spin_unlock_bh(&chan->lock);
1129}
1130
1131static enum dma_status xgene_dma_tx_status(struct dma_chan *dchan,
1132                                           dma_cookie_t cookie,
1133                                           struct dma_tx_state *txstate)
1134{
1135        return dma_cookie_status(dchan, cookie, txstate);
1136}
1137
1138static void xgene_dma_tasklet_cb(unsigned long data)
1139{
1140        struct xgene_dma_chan *chan = (struct xgene_dma_chan *)data;
1141
1142        /* Run all cleanup for descriptors which have been completed */
1143        xgene_dma_cleanup_descriptors(chan);
1144
1145        /* Re-enable DMA channel IRQ */
1146        enable_irq(chan->rx_irq);
1147}
1148
1149static irqreturn_t xgene_dma_chan_ring_isr(int irq, void *id)
1150{
1151        struct xgene_dma_chan *chan = (struct xgene_dma_chan *)id;
1152
1153        BUG_ON(!chan);
1154
1155        /*
1156         * Disable DMA channel IRQ until we process completed
1157         * descriptors
1158         */
1159        disable_irq_nosync(chan->rx_irq);
1160
1161        /*
1162         * Schedule the tasklet to handle all cleanup of the current
1163         * transaction. It will start a new transaction if there is
1164         * one pending.
1165         */
1166        tasklet_schedule(&chan->tasklet);
1167
1168        return IRQ_HANDLED;
1169}
1170
1171static irqreturn_t xgene_dma_err_isr(int irq, void *id)
1172{
1173        struct xgene_dma *pdma = (struct xgene_dma *)id;
1174        unsigned long int_mask;
1175        u32 val, i;
1176
1177        val = ioread32(pdma->csr_dma + XGENE_DMA_INT);
1178
1179        /* Clear DMA interrupts */
1180        iowrite32(val, pdma->csr_dma + XGENE_DMA_INT);
1181
1182        /* Print DMA error info */
1183        int_mask = val >> XGENE_DMA_INT_MASK_SHIFT;
1184        for_each_set_bit(i, &int_mask, ARRAY_SIZE(xgene_dma_err))
1185                dev_err(pdma->dev,
1186                        "Interrupt status 0x%08X %s\n", val, xgene_dma_err[i]);
1187
1188        return IRQ_HANDLED;
1189}
1190
1191static void xgene_dma_wr_ring_state(struct xgene_dma_ring *ring)
1192{
1193        int i;
1194
1195        iowrite32(ring->num, ring->pdma->csr_ring + XGENE_DMA_RING_STATE);
1196
1197        for (i = 0; i < XGENE_DMA_RING_NUM_CONFIG; i++)
1198                iowrite32(ring->state[i], ring->pdma->csr_ring +
1199                          XGENE_DMA_RING_STATE_WR_BASE + (i * 4));
1200}
1201
1202static void xgene_dma_clr_ring_state(struct xgene_dma_ring *ring)
1203{
1204        memset(ring->state, 0, sizeof(u32) * XGENE_DMA_RING_NUM_CONFIG);
1205        xgene_dma_wr_ring_state(ring);
1206}
1207
1208static void xgene_dma_setup_ring(struct xgene_dma_ring *ring)
1209{
1210        void *ring_cfg = ring->state;
1211        u64 addr = ring->desc_paddr;
1212        u32 i, val;
1213
1214        ring->slots = ring->size / XGENE_DMA_RING_WQ_DESC_SIZE;
1215
1216        /* Clear DMA ring state */
1217        xgene_dma_clr_ring_state(ring);
1218
1219        /* Set DMA ring type */
1220        XGENE_DMA_RING_TYPE_SET(ring_cfg, XGENE_DMA_RING_TYPE_REGULAR);
1221
1222        if (ring->owner == XGENE_DMA_RING_OWNER_DMA) {
1223                /* Set recombination buffer and timeout */
1224                XGENE_DMA_RING_RECOMBBUF_SET(ring_cfg);
1225                XGENE_DMA_RING_RECOMTIMEOUTL_SET(ring_cfg);
1226                XGENE_DMA_RING_RECOMTIMEOUTH_SET(ring_cfg);
1227        }
1228
1229        /* Initialize DMA ring state */
1230        XGENE_DMA_RING_SELTHRSH_SET(ring_cfg);
1231        XGENE_DMA_RING_ACCEPTLERR_SET(ring_cfg);
1232        XGENE_DMA_RING_COHERENT_SET(ring_cfg);
1233        XGENE_DMA_RING_ADDRL_SET(ring_cfg, addr);
1234        XGENE_DMA_RING_ADDRH_SET(ring_cfg, addr);
1235        XGENE_DMA_RING_SIZE_SET(ring_cfg, ring->cfgsize);
1236
1237        /* Write DMA ring configurations */
1238        xgene_dma_wr_ring_state(ring);
1239
1240        /* Set DMA ring id */
1241        iowrite32(XGENE_DMA_RING_ID_SETUP(ring->id),
1242                  ring->pdma->csr_ring + XGENE_DMA_RING_ID);
1243
1244        /* Set DMA ring buffer */
1245        iowrite32(XGENE_DMA_RING_ID_BUF_SETUP(ring->num),
1246                  ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
1247
1248        if (ring->owner != XGENE_DMA_RING_OWNER_CPU)
1249                return;
1250
1251        /* Set empty signature to DMA Rx ring descriptors */
1252        for (i = 0; i < ring->slots; i++) {
1253                struct xgene_dma_desc_hw *desc;
1254
1255                desc = &ring->desc_hw[i];
1256                desc->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
1257        }
1258
1259        /* Enable DMA Rx ring interrupt */
1260        val = ioread32(ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
1261        XGENE_DMA_RING_NE_INT_MODE_SET(val, ring->buf_num);
1262        iowrite32(val, ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
1263}
1264
1265static void xgene_dma_clear_ring(struct xgene_dma_ring *ring)
1266{
1267        u32 ring_id, val;
1268
1269        if (ring->owner == XGENE_DMA_RING_OWNER_CPU) {
1270                /* Disable DMA Rx ring interrupt */
1271                val = ioread32(ring->pdma->csr_ring +
1272                               XGENE_DMA_RING_NE_INT_MODE);
1273                XGENE_DMA_RING_NE_INT_MODE_RESET(val, ring->buf_num);
1274                iowrite32(val, ring->pdma->csr_ring +
1275                          XGENE_DMA_RING_NE_INT_MODE);
1276        }
1277
1278        /* Clear DMA ring state */
1279        ring_id = XGENE_DMA_RING_ID_SETUP(ring->id);
1280        iowrite32(ring_id, ring->pdma->csr_ring + XGENE_DMA_RING_ID);
1281
1282        iowrite32(0, ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
1283        xgene_dma_clr_ring_state(ring);
1284}
1285
1286static void xgene_dma_set_ring_cmd(struct xgene_dma_ring *ring)
1287{
1288        ring->cmd_base = ring->pdma->csr_ring_cmd +
1289                                XGENE_DMA_RING_CMD_BASE_OFFSET((ring->num -
1290                                                          XGENE_DMA_RING_NUM));
1291
1292        ring->cmd = ring->cmd_base + XGENE_DMA_RING_CMD_OFFSET;
1293}
1294
1295static int xgene_dma_get_ring_size(struct xgene_dma_chan *chan,
1296                                   enum xgene_dma_ring_cfgsize cfgsize)
1297{
1298        int size;
1299
1300        switch (cfgsize) {
1301        case XGENE_DMA_RING_CFG_SIZE_512B:
1302                size = 0x200;
1303                break;
1304        case XGENE_DMA_RING_CFG_SIZE_2KB:
1305                size = 0x800;
1306                break;
1307        case XGENE_DMA_RING_CFG_SIZE_16KB:
1308                size = 0x4000;
1309                break;
1310        case XGENE_DMA_RING_CFG_SIZE_64KB:
1311                size = 0x10000;
1312                break;
1313        case XGENE_DMA_RING_CFG_SIZE_512KB:
1314                size = 0x80000;
1315                break;
1316        default:
1317                chan_err(chan, "Unsupported cfg ring size %d\n", cfgsize);
1318                return -EINVAL;
1319        }
1320
1321        return size;
1322}
1323
1324static void xgene_dma_delete_ring_one(struct xgene_dma_ring *ring)
1325{
1326        /* Clear DMA ring configurations */
1327        xgene_dma_clear_ring(ring);
1328
1329        /* De-allocate DMA ring descriptor */
1330        if (ring->desc_vaddr) {
1331                dma_free_coherent(ring->pdma->dev, ring->size,
1332                                  ring->desc_vaddr, ring->desc_paddr);
1333                ring->desc_vaddr = NULL;
1334        }
1335}
1336
1337static void xgene_dma_delete_chan_rings(struct xgene_dma_chan *chan)
1338{
1339        xgene_dma_delete_ring_one(&chan->rx_ring);
1340        xgene_dma_delete_ring_one(&chan->tx_ring);
1341}
1342
1343static int xgene_dma_create_ring_one(struct xgene_dma_chan *chan,
1344                                     struct xgene_dma_ring *ring,
1345                                     enum xgene_dma_ring_cfgsize cfgsize)
1346{
1347        int ret;
1348
1349        /* Setup DMA ring descriptor variables */
1350        ring->pdma = chan->pdma;
1351        ring->cfgsize = cfgsize;
1352        ring->num = chan->pdma->ring_num++;
1353        ring->id = XGENE_DMA_RING_ID_GET(ring->owner, ring->buf_num);
1354
1355        ret = xgene_dma_get_ring_size(chan, cfgsize);
1356        if (ret <= 0)
1357                return ret;
1358        ring->size = ret;
1359
1360        /* Allocate memory for DMA ring descriptor */
1361        ring->desc_vaddr = dma_zalloc_coherent(chan->dev, ring->size,
1362                                               &ring->desc_paddr, GFP_KERNEL);
1363        if (!ring->desc_vaddr) {
1364                chan_err(chan, "Failed to allocate ring desc\n");
1365                return -ENOMEM;
1366        }
1367
1368        /* Configure and enable DMA ring */
1369        xgene_dma_set_ring_cmd(ring);
1370        xgene_dma_setup_ring(ring);
1371
1372        return 0;
1373}
1374
1375static int xgene_dma_create_chan_rings(struct xgene_dma_chan *chan)
1376{
1377        struct xgene_dma_ring *rx_ring = &chan->rx_ring;
1378        struct xgene_dma_ring *tx_ring = &chan->tx_ring;
1379        int ret;
1380
1381        /* Create DMA Rx ring descriptor */
1382        rx_ring->owner = XGENE_DMA_RING_OWNER_CPU;
1383        rx_ring->buf_num = XGENE_DMA_CPU_BUFNUM + chan->id;
1384
1385        ret = xgene_dma_create_ring_one(chan, rx_ring,
1386                                        XGENE_DMA_RING_CFG_SIZE_64KB);
1387        if (ret)
1388                return ret;
1389
1390        chan_dbg(chan, "Rx ring id 0x%X num %d desc 0x%p\n",
1391                 rx_ring->id, rx_ring->num, rx_ring->desc_vaddr);
1392
1393        /* Create DMA Tx ring descriptor */
1394        tx_ring->owner = XGENE_DMA_RING_OWNER_DMA;
1395        tx_ring->buf_num = XGENE_DMA_BUFNUM + chan->id;
1396
1397        ret = xgene_dma_create_ring_one(chan, tx_ring,
1398                                        XGENE_DMA_RING_CFG_SIZE_64KB);
1399        if (ret) {
1400                xgene_dma_delete_ring_one(rx_ring);
1401                return ret;
1402        }
1403
1404        tx_ring->dst_ring_num = XGENE_DMA_RING_DST_ID(rx_ring->num);
1405
1406        chan_dbg(chan,
1407                 "Tx ring id 0x%X num %d desc 0x%p\n",
1408                 tx_ring->id, tx_ring->num, tx_ring->desc_vaddr);
1409
1410        /* Set the max outstanding request possible to this channel */
1411        chan->max_outstanding = tx_ring->slots;
1412
1413        return ret;
1414}
1415
1416static int xgene_dma_init_rings(struct xgene_dma *pdma)
1417{
1418        int ret, i, j;
1419
1420        for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1421                ret = xgene_dma_create_chan_rings(&pdma->chan[i]);
1422                if (ret) {
1423                        for (j = 0; j < i; j++)
1424                                xgene_dma_delete_chan_rings(&pdma->chan[j]);
1425                        return ret;
1426                }
1427        }
1428
1429        return ret;
1430}
1431
1432static void xgene_dma_enable(struct xgene_dma *pdma)
1433{
1434        u32 val;
1435
1436        /* Configure and enable DMA engine */
1437        val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
1438        XGENE_DMA_CH_SETUP(val);
1439        XGENE_DMA_ENABLE(val);
1440        iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
1441}
1442
1443static void xgene_dma_disable(struct xgene_dma *pdma)
1444{
1445        u32 val;
1446
1447        val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
1448        XGENE_DMA_DISABLE(val);
1449        iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
1450}
1451
1452static void xgene_dma_mask_interrupts(struct xgene_dma *pdma)
1453{
1454        /*
1455         * Mask DMA ring overflow, underflow and
1456         * AXI write/read error interrupts
1457         */
1458        iowrite32(XGENE_DMA_INT_ALL_MASK,
1459                  pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
1460        iowrite32(XGENE_DMA_INT_ALL_MASK,
1461                  pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
1462        iowrite32(XGENE_DMA_INT_ALL_MASK,
1463                  pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
1464        iowrite32(XGENE_DMA_INT_ALL_MASK,
1465                  pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
1466        iowrite32(XGENE_DMA_INT_ALL_MASK,
1467                  pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
1468
1469        /* Mask DMA error interrupts */
1470        iowrite32(XGENE_DMA_INT_ALL_MASK, pdma->csr_dma + XGENE_DMA_INT_MASK);
1471}
1472
1473static void xgene_dma_unmask_interrupts(struct xgene_dma *pdma)
1474{
1475        /*
1476         * Unmask DMA ring overflow, underflow and
1477         * AXI write/read error interrupts
1478         */
1479        iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1480                  pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
1481        iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1482                  pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
1483        iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1484                  pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
1485        iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1486                  pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
1487        iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1488                  pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
1489
1490        /* Unmask DMA error interrupts */
1491        iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1492                  pdma->csr_dma + XGENE_DMA_INT_MASK);
1493}
1494
1495static void xgene_dma_init_hw(struct xgene_dma *pdma)
1496{
1497        u32 val;
1498
1499        /* Associate DMA ring to corresponding ring HW */
1500        iowrite32(XGENE_DMA_ASSOC_RING_MNGR1,
1501                  pdma->csr_dma + XGENE_DMA_CFG_RING_WQ_ASSOC);
1502
1503        /* Configure RAID6 polynomial control setting */
1504        if (is_pq_enabled(pdma))
1505                iowrite32(XGENE_DMA_RAID6_MULTI_CTRL(0x1D),
1506                          pdma->csr_dma + XGENE_DMA_RAID6_CONT);
1507        else
1508                dev_info(pdma->dev, "PQ is disabled in HW\n");
1509
1510        xgene_dma_enable(pdma);
1511        xgene_dma_unmask_interrupts(pdma);
1512
1513        /* Get DMA id and version info */
1514        val = ioread32(pdma->csr_dma + XGENE_DMA_IPBRR);
1515
1516        /* DMA device info */
1517        dev_info(pdma->dev,
1518                 "X-Gene DMA v%d.%02d.%02d driver registered %d channels",
1519                 XGENE_DMA_REV_NO_RD(val), XGENE_DMA_BUS_ID_RD(val),
1520                 XGENE_DMA_DEV_ID_RD(val), XGENE_DMA_MAX_CHANNEL);
1521}
1522
1523static int xgene_dma_init_ring_mngr(struct xgene_dma *pdma)
1524{
1525        if (ioread32(pdma->csr_ring + XGENE_DMA_RING_CLKEN) &&
1526            (!ioread32(pdma->csr_ring + XGENE_DMA_RING_SRST)))
1527                return 0;
1528
1529        iowrite32(0x3, pdma->csr_ring + XGENE_DMA_RING_CLKEN);
1530        iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_SRST);
1531
1532        /* Bring up memory */
1533        iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
1534
1535        /* Force a barrier */
1536        ioread32(pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
1537
1538        /* reset may take up to 1ms */
1539        usleep_range(1000, 1100);
1540
1541        if (ioread32(pdma->csr_ring + XGENE_DMA_RING_BLK_MEM_RDY)
1542                != XGENE_DMA_RING_BLK_MEM_RDY_VAL) {
1543                dev_err(pdma->dev,
1544                        "Failed to release ring mngr memory from shutdown\n");
1545                return -ENODEV;
1546        }
1547
1548        /* program threshold set 1 and all hysteresis */
1549        iowrite32(XGENE_DMA_RING_THRESLD0_SET1_VAL,
1550                  pdma->csr_ring + XGENE_DMA_RING_THRESLD0_SET1);
1551        iowrite32(XGENE_DMA_RING_THRESLD1_SET1_VAL,
1552                  pdma->csr_ring + XGENE_DMA_RING_THRESLD1_SET1);
1553        iowrite32(XGENE_DMA_RING_HYSTERESIS_VAL,
1554                  pdma->csr_ring + XGENE_DMA_RING_HYSTERESIS);
1555
1556        /* Enable QPcore and assign error queue */
1557        iowrite32(XGENE_DMA_RING_ENABLE,
1558                  pdma->csr_ring + XGENE_DMA_RING_CONFIG);
1559
1560        return 0;
1561}
1562
1563static int xgene_dma_init_mem(struct xgene_dma *pdma)
1564{
1565        int ret;
1566
1567        ret = xgene_dma_init_ring_mngr(pdma);
1568        if (ret)
1569                return ret;
1570
1571        /* Bring up memory */
1572        iowrite32(0x0, pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
1573
1574        /* Force a barrier */
1575        ioread32(pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
1576
1577        /* reset may take up to 1ms */
1578        usleep_range(1000, 1100);
1579
1580        if (ioread32(pdma->csr_dma + XGENE_DMA_BLK_MEM_RDY)
1581                != XGENE_DMA_BLK_MEM_RDY_VAL) {
1582                dev_err(pdma->dev,
1583                        "Failed to release DMA memory from shutdown\n");
1584                return -ENODEV;
1585        }
1586
1587        return 0;
1588}
1589
1590static int xgene_dma_request_irqs(struct xgene_dma *pdma)
1591{
1592        struct xgene_dma_chan *chan;
1593        int ret, i, j;
1594
1595        /* Register DMA error irq */
1596        ret = devm_request_irq(pdma->dev, pdma->err_irq, xgene_dma_err_isr,
1597                               0, "dma_error", pdma);
1598        if (ret) {
1599                dev_err(pdma->dev,
1600                        "Failed to register error IRQ %d\n", pdma->err_irq);
1601                return ret;
1602        }
1603
1604        /* Register DMA channel rx irq */
1605        for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1606                chan = &pdma->chan[i];
1607                irq_set_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
1608                ret = devm_request_irq(chan->dev, chan->rx_irq,
1609                                       xgene_dma_chan_ring_isr,
1610                                       0, chan->name, chan);
1611                if (ret) {
1612                        chan_err(chan, "Failed to register Rx IRQ %d\n",
1613                                 chan->rx_irq);
1614                        devm_free_irq(pdma->dev, pdma->err_irq, pdma);
1615
1616                        for (j = 0; j < i; j++) {
1617                                chan = &pdma->chan[i];
1618                                irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
1619                                devm_free_irq(chan->dev, chan->rx_irq, chan);
1620                        }
1621
1622                        return ret;
1623                }
1624        }
1625
1626        return 0;
1627}
1628
1629static void xgene_dma_free_irqs(struct xgene_dma *pdma)
1630{
1631        struct xgene_dma_chan *chan;
1632        int i;
1633
1634        /* Free DMA device error irq */
1635        devm_free_irq(pdma->dev, pdma->err_irq, pdma);
1636
1637        for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1638                chan = &pdma->chan[i];
1639                irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
1640                devm_free_irq(chan->dev, chan->rx_irq, chan);
1641        }
1642}
1643
1644static void xgene_dma_set_caps(struct xgene_dma_chan *chan,
1645                               struct dma_device *dma_dev)
1646{
1647        /* Initialize DMA device capability mask */
1648        dma_cap_zero(dma_dev->cap_mask);
1649
1650        /* Set DMA device capability */
1651        dma_cap_set(DMA_SG, dma_dev->cap_mask);
1652
1653        /* Basically here, the X-Gene SoC DMA engine channel 0 supports XOR
1654         * and channel 1 supports XOR, PQ both. First thing here is we have
1655         * mechanism in hw to enable/disable PQ/XOR supports on channel 1,
1656         * we can make sure this by reading SoC Efuse register.
1657         * Second thing, we have hw errata that if we run channel 0 and
1658         * channel 1 simultaneously with executing XOR and PQ request,
1659         * suddenly DMA engine hangs, So here we enable XOR on channel 0 only
1660         * if XOR and PQ supports on channel 1 is disabled.
1661         */
1662        if ((chan->id == XGENE_DMA_PQ_CHANNEL) &&
1663            is_pq_enabled(chan->pdma)) {
1664                dma_cap_set(DMA_PQ, dma_dev->cap_mask);
1665                dma_cap_set(DMA_XOR, dma_dev->cap_mask);
1666        } else if ((chan->id == XGENE_DMA_XOR_CHANNEL) &&
1667                   !is_pq_enabled(chan->pdma)) {
1668                dma_cap_set(DMA_XOR, dma_dev->cap_mask);
1669        }
1670
1671        /* Set base and prep routines */
1672        dma_dev->dev = chan->dev;
1673        dma_dev->device_alloc_chan_resources = xgene_dma_alloc_chan_resources;
1674        dma_dev->device_free_chan_resources = xgene_dma_free_chan_resources;
1675        dma_dev->device_issue_pending = xgene_dma_issue_pending;
1676        dma_dev->device_tx_status = xgene_dma_tx_status;
1677        dma_dev->device_prep_dma_sg = xgene_dma_prep_sg;
1678
1679        if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1680                dma_dev->device_prep_dma_xor = xgene_dma_prep_xor;
1681                dma_dev->max_xor = XGENE_DMA_MAX_XOR_SRC;
1682                dma_dev->xor_align = DMAENGINE_ALIGN_64_BYTES;
1683        }
1684
1685        if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
1686                dma_dev->device_prep_dma_pq = xgene_dma_prep_pq;
1687                dma_dev->max_pq = XGENE_DMA_MAX_XOR_SRC;
1688                dma_dev->pq_align = DMAENGINE_ALIGN_64_BYTES;
1689        }
1690}
1691
1692static int xgene_dma_async_register(struct xgene_dma *pdma, int id)
1693{
1694        struct xgene_dma_chan *chan = &pdma->chan[id];
1695        struct dma_device *dma_dev = &pdma->dma_dev[id];
1696        int ret;
1697
1698        chan->dma_chan.device = dma_dev;
1699
1700        spin_lock_init(&chan->lock);
1701        INIT_LIST_HEAD(&chan->ld_pending);
1702        INIT_LIST_HEAD(&chan->ld_running);
1703        INIT_LIST_HEAD(&chan->ld_completed);
1704        tasklet_init(&chan->tasklet, xgene_dma_tasklet_cb,
1705                     (unsigned long)chan);
1706
1707        chan->pending = 0;
1708        chan->desc_pool = NULL;
1709        dma_cookie_init(&chan->dma_chan);
1710
1711        /* Setup dma device capabilities and prep routines */
1712        xgene_dma_set_caps(chan, dma_dev);
1713
1714        /* Initialize DMA device list head */
1715        INIT_LIST_HEAD(&dma_dev->channels);
1716        list_add_tail(&chan->dma_chan.device_node, &dma_dev->channels);
1717
1718        /* Register with Linux async DMA framework*/
1719        ret = dma_async_device_register(dma_dev);
1720        if (ret) {
1721                chan_err(chan, "Failed to register async device %d", ret);
1722                tasklet_kill(&chan->tasklet);
1723
1724                return ret;
1725        }
1726
1727        /* DMA capability info */
1728        dev_info(pdma->dev,
1729                 "%s: CAPABILITY ( %s%s%s)\n", dma_chan_name(&chan->dma_chan),
1730                 dma_has_cap(DMA_SG, dma_dev->cap_mask) ? "SGCPY " : "",
1731                 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "XOR " : "",
1732                 dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "PQ " : "");
1733
1734        return 0;
1735}
1736
1737static int xgene_dma_init_async(struct xgene_dma *pdma)
1738{
1739        int ret, i, j;
1740
1741        for (i = 0; i < XGENE_DMA_MAX_CHANNEL ; i++) {
1742                ret = xgene_dma_async_register(pdma, i);
1743                if (ret) {
1744                        for (j = 0; j < i; j++) {
1745                                dma_async_device_unregister(&pdma->dma_dev[j]);
1746                                tasklet_kill(&pdma->chan[j].tasklet);
1747                        }
1748
1749                        return ret;
1750                }
1751        }
1752
1753        return ret;
1754}
1755
1756static void xgene_dma_async_unregister(struct xgene_dma *pdma)
1757{
1758        int i;
1759
1760        for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
1761                dma_async_device_unregister(&pdma->dma_dev[i]);
1762}
1763
1764static void xgene_dma_init_channels(struct xgene_dma *pdma)
1765{
1766        struct xgene_dma_chan *chan;
1767        int i;
1768
1769        pdma->ring_num = XGENE_DMA_RING_NUM;
1770
1771        for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1772                chan = &pdma->chan[i];
1773                chan->dev = pdma->dev;
1774                chan->pdma = pdma;
1775                chan->id = i;
1776                snprintf(chan->name, sizeof(chan->name), "dmachan%d", chan->id);
1777        }
1778}
1779
1780static int xgene_dma_get_resources(struct platform_device *pdev,
1781                                   struct xgene_dma *pdma)
1782{
1783        struct resource *res;
1784        int irq, i;
1785
1786        /* Get DMA csr region */
1787        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1788        if (!res) {
1789                dev_err(&pdev->dev, "Failed to get csr region\n");
1790                return -ENXIO;
1791        }
1792
1793        pdma->csr_dma = devm_ioremap(&pdev->dev, res->start,
1794                                     resource_size(res));
1795        if (!pdma->csr_dma) {
1796                dev_err(&pdev->dev, "Failed to ioremap csr region");
1797                return -ENOMEM;
1798        }
1799
1800        /* Get DMA ring csr region */
1801        res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1802        if (!res) {
1803                dev_err(&pdev->dev, "Failed to get ring csr region\n");
1804                return -ENXIO;
1805        }
1806
1807        pdma->csr_ring =  devm_ioremap(&pdev->dev, res->start,
1808                                       resource_size(res));
1809        if (!pdma->csr_ring) {
1810                dev_err(&pdev->dev, "Failed to ioremap ring csr region");
1811                return -ENOMEM;
1812        }
1813
1814        /* Get DMA ring cmd csr region */
1815        res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1816        if (!res) {
1817                dev_err(&pdev->dev, "Failed to get ring cmd csr region\n");
1818                return -ENXIO;
1819        }
1820
1821        pdma->csr_ring_cmd = devm_ioremap(&pdev->dev, res->start,
1822                                          resource_size(res));
1823        if (!pdma->csr_ring_cmd) {
1824                dev_err(&pdev->dev, "Failed to ioremap ring cmd csr region");
1825                return -ENOMEM;
1826        }
1827
1828        pdma->csr_ring_cmd += XGENE_DMA_RING_CMD_SM_OFFSET;
1829
1830        /* Get efuse csr region */
1831        res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1832        if (!res) {
1833                dev_err(&pdev->dev, "Failed to get efuse csr region\n");
1834                return -ENXIO;
1835        }
1836
1837        pdma->csr_efuse = devm_ioremap(&pdev->dev, res->start,
1838                                       resource_size(res));
1839        if (!pdma->csr_efuse) {
1840                dev_err(&pdev->dev, "Failed to ioremap efuse csr region");
1841                return -ENOMEM;
1842        }
1843
1844        /* Get DMA error interrupt */
1845        irq = platform_get_irq(pdev, 0);
1846        if (irq <= 0) {
1847                dev_err(&pdev->dev, "Failed to get Error IRQ\n");
1848                return -ENXIO;
1849        }
1850
1851        pdma->err_irq = irq;
1852
1853        /* Get DMA Rx ring descriptor interrupts for all DMA channels */
1854        for (i = 1; i <= XGENE_DMA_MAX_CHANNEL; i++) {
1855                irq = platform_get_irq(pdev, i);
1856                if (irq <= 0) {
1857                        dev_err(&pdev->dev, "Failed to get Rx IRQ\n");
1858                        return -ENXIO;
1859                }
1860
1861                pdma->chan[i - 1].rx_irq = irq;
1862        }
1863
1864        return 0;
1865}
1866
1867static int xgene_dma_probe(struct platform_device *pdev)
1868{
1869        struct xgene_dma *pdma;
1870        int ret, i;
1871
1872        pdma = devm_kzalloc(&pdev->dev, sizeof(*pdma), GFP_KERNEL);
1873        if (!pdma)
1874                return -ENOMEM;
1875
1876        pdma->dev = &pdev->dev;
1877        platform_set_drvdata(pdev, pdma);
1878
1879        ret = xgene_dma_get_resources(pdev, pdma);
1880        if (ret)
1881                return ret;
1882
1883        pdma->clk = devm_clk_get(&pdev->dev, NULL);
1884        if (IS_ERR(pdma->clk) && !ACPI_COMPANION(&pdev->dev)) {
1885                dev_err(&pdev->dev, "Failed to get clk\n");
1886                return PTR_ERR(pdma->clk);
1887        }
1888
1889        /* Enable clk before accessing registers */
1890        if (!IS_ERR(pdma->clk)) {
1891                ret = clk_prepare_enable(pdma->clk);
1892                if (ret) {
1893                        dev_err(&pdev->dev, "Failed to enable clk %d\n", ret);
1894                        return ret;
1895                }
1896        }
1897
1898        /* Remove DMA RAM out of shutdown */
1899        ret = xgene_dma_init_mem(pdma);
1900        if (ret)
1901                goto err_clk_enable;
1902
1903        ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(42));
1904        if (ret) {
1905                dev_err(&pdev->dev, "No usable DMA configuration\n");
1906                goto err_dma_mask;
1907        }
1908
1909        /* Initialize DMA channels software state */
1910        xgene_dma_init_channels(pdma);
1911
1912        /* Configue DMA rings */
1913        ret = xgene_dma_init_rings(pdma);
1914        if (ret)
1915                goto err_clk_enable;
1916
1917        ret = xgene_dma_request_irqs(pdma);
1918        if (ret)
1919                goto err_request_irq;
1920
1921        /* Configure and enable DMA engine */
1922        xgene_dma_init_hw(pdma);
1923
1924        /* Register DMA device with linux async framework */
1925        ret = xgene_dma_init_async(pdma);
1926        if (ret)
1927                goto err_async_init;
1928
1929        return 0;
1930
1931err_async_init:
1932        xgene_dma_free_irqs(pdma);
1933
1934err_request_irq:
1935        for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
1936                xgene_dma_delete_chan_rings(&pdma->chan[i]);
1937
1938err_dma_mask:
1939err_clk_enable:
1940        if (!IS_ERR(pdma->clk))
1941                clk_disable_unprepare(pdma->clk);
1942
1943        return ret;
1944}
1945
1946static int xgene_dma_remove(struct platform_device *pdev)
1947{
1948        struct xgene_dma *pdma = platform_get_drvdata(pdev);
1949        struct xgene_dma_chan *chan;
1950        int i;
1951
1952        xgene_dma_async_unregister(pdma);
1953
1954        /* Mask interrupts and disable DMA engine */
1955        xgene_dma_mask_interrupts(pdma);
1956        xgene_dma_disable(pdma);
1957        xgene_dma_free_irqs(pdma);
1958
1959        for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1960                chan = &pdma->chan[i];
1961                tasklet_kill(&chan->tasklet);
1962                xgene_dma_delete_chan_rings(chan);
1963        }
1964
1965        if (!IS_ERR(pdma->clk))
1966                clk_disable_unprepare(pdma->clk);
1967
1968        return 0;
1969}
1970
1971#ifdef CONFIG_ACPI
1972static const struct acpi_device_id xgene_dma_acpi_match_ptr[] = {
1973        {"APMC0D43", 0},
1974        {},
1975};
1976MODULE_DEVICE_TABLE(acpi, xgene_dma_acpi_match_ptr);
1977#endif
1978
1979static const struct of_device_id xgene_dma_of_match_ptr[] = {
1980        {.compatible = "apm,xgene-storm-dma",},
1981        {},
1982};
1983MODULE_DEVICE_TABLE(of, xgene_dma_of_match_ptr);
1984
1985static struct platform_driver xgene_dma_driver = {
1986        .probe = xgene_dma_probe,
1987        .remove = xgene_dma_remove,
1988        .driver = {
1989                .name = "X-Gene-DMA",
1990                .of_match_table = xgene_dma_of_match_ptr,
1991                .acpi_match_table = ACPI_PTR(xgene_dma_acpi_match_ptr),
1992        },
1993};
1994
1995module_platform_driver(xgene_dma_driver);
1996
1997MODULE_DESCRIPTION("APM X-Gene SoC DMA driver");
1998MODULE_AUTHOR("Rameshwar Prasad Sahu <rsahu@apm.com>");
1999MODULE_AUTHOR("Loc Ho <lho@apm.com>");
2000MODULE_LICENSE("GPL");
2001MODULE_VERSION("1.0");
2002