Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Applied Micro X-Gene SoC DMA engine Driver
0004  *
0005  * Copyright (c) 2015, Applied Micro Circuits Corporation
0006  * Authors: Rameshwar Prasad Sahu <rsahu@apm.com>
0007  *      Loc Ho <lho@apm.com>
0008  *
0009  * NOTE: PM support is currently not available.
0010  */
0011 
0012 #include <linux/acpi.h>
0013 #include <linux/clk.h>
0014 #include <linux/delay.h>
0015 #include <linux/dma-mapping.h>
0016 #include <linux/dmaengine.h>
0017 #include <linux/dmapool.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/io.h>
0020 #include <linux/irq.h>
0021 #include <linux/module.h>
0022 #include <linux/of_device.h>
0023 
0024 #include "dmaengine.h"
0025 
0026 /* X-Gene DMA ring csr registers and bit definations */
0027 #define XGENE_DMA_RING_CONFIG           0x04
0028 #define XGENE_DMA_RING_ENABLE           BIT(31)
0029 #define XGENE_DMA_RING_ID           0x08
0030 #define XGENE_DMA_RING_ID_SETUP(v)      ((v) | BIT(31))
0031 #define XGENE_DMA_RING_ID_BUF           0x0C
0032 #define XGENE_DMA_RING_ID_BUF_SETUP(v)      (((v) << 9) | BIT(21))
0033 #define XGENE_DMA_RING_THRESLD0_SET1        0x30
0034 #define XGENE_DMA_RING_THRESLD0_SET1_VAL    0X64
0035 #define XGENE_DMA_RING_THRESLD1_SET1        0x34
0036 #define XGENE_DMA_RING_THRESLD1_SET1_VAL    0xC8
0037 #define XGENE_DMA_RING_HYSTERESIS       0x68
0038 #define XGENE_DMA_RING_HYSTERESIS_VAL       0xFFFFFFFF
0039 #define XGENE_DMA_RING_STATE            0x6C
0040 #define XGENE_DMA_RING_STATE_WR_BASE        0x70
0041 #define XGENE_DMA_RING_NE_INT_MODE      0x017C
0042 #define XGENE_DMA_RING_NE_INT_MODE_SET(m, v)    \
0043     ((m) = ((m) & ~BIT(31 - (v))) | BIT(31 - (v)))
0044 #define XGENE_DMA_RING_NE_INT_MODE_RESET(m, v)  \
0045     ((m) &= (~BIT(31 - (v))))
0046 #define XGENE_DMA_RING_CLKEN            0xC208
0047 #define XGENE_DMA_RING_SRST         0xC200
0048 #define XGENE_DMA_RING_MEM_RAM_SHUTDOWN     0xD070
0049 #define XGENE_DMA_RING_BLK_MEM_RDY      0xD074
0050 #define XGENE_DMA_RING_BLK_MEM_RDY_VAL      0xFFFFFFFF
0051 #define XGENE_DMA_RING_ID_GET(owner, num)   (((owner) << 6) | (num))
0052 #define XGENE_DMA_RING_DST_ID(v)        ((1 << 10) | (v))
0053 #define XGENE_DMA_RING_CMD_OFFSET       0x2C
0054 #define XGENE_DMA_RING_CMD_BASE_OFFSET(v)   ((v) << 6)
0055 #define XGENE_DMA_RING_COHERENT_SET(m)      \
0056     (((u32 *)(m))[2] |= BIT(4))
0057 #define XGENE_DMA_RING_ADDRL_SET(m, v)      \
0058     (((u32 *)(m))[2] |= (((v) >> 8) << 5))
0059 #define XGENE_DMA_RING_ADDRH_SET(m, v)      \
0060     (((u32 *)(m))[3] |= ((v) >> 35))
0061 #define XGENE_DMA_RING_ACCEPTLERR_SET(m)    \
0062     (((u32 *)(m))[3] |= BIT(19))
0063 #define XGENE_DMA_RING_SIZE_SET(m, v)       \
0064     (((u32 *)(m))[3] |= ((v) << 23))
0065 #define XGENE_DMA_RING_RECOMBBUF_SET(m)     \
0066     (((u32 *)(m))[3] |= BIT(27))
0067 #define XGENE_DMA_RING_RECOMTIMEOUTL_SET(m) \
0068     (((u32 *)(m))[3] |= (0x7 << 28))
0069 #define XGENE_DMA_RING_RECOMTIMEOUTH_SET(m) \
0070     (((u32 *)(m))[4] |= 0x3)
0071 #define XGENE_DMA_RING_SELTHRSH_SET(m)      \
0072     (((u32 *)(m))[4] |= BIT(3))
0073 #define XGENE_DMA_RING_TYPE_SET(m, v)       \
0074     (((u32 *)(m))[4] |= ((v) << 19))
0075 
0076 /* X-Gene DMA device csr registers and bit definitions */
0077 #define XGENE_DMA_IPBRR             0x0
0078 #define XGENE_DMA_DEV_ID_RD(v)          ((v) & 0x00000FFF)
0079 #define XGENE_DMA_BUS_ID_RD(v)          (((v) >> 12) & 3)
0080 #define XGENE_DMA_REV_NO_RD(v)          (((v) >> 14) & 3)
0081 #define XGENE_DMA_GCR               0x10
0082 #define XGENE_DMA_CH_SETUP(v)           \
0083     ((v) = ((v) & ~0x000FFFFF) | 0x000AAFFF)
0084 #define XGENE_DMA_ENABLE(v)         ((v) |= BIT(31))
0085 #define XGENE_DMA_DISABLE(v)            ((v) &= ~BIT(31))
0086 #define XGENE_DMA_RAID6_CONT            0x14
0087 #define XGENE_DMA_RAID6_MULTI_CTRL(v)       ((v) << 24)
0088 #define XGENE_DMA_INT               0x70
0089 #define XGENE_DMA_INT_MASK          0x74
0090 #define XGENE_DMA_INT_ALL_MASK          0xFFFFFFFF
0091 #define XGENE_DMA_INT_ALL_UNMASK        0x0
0092 #define XGENE_DMA_INT_MASK_SHIFT        0x14
0093 #define XGENE_DMA_RING_INT0_MASK        0x90A0
0094 #define XGENE_DMA_RING_INT1_MASK        0x90A8
0095 #define XGENE_DMA_RING_INT2_MASK        0x90B0
0096 #define XGENE_DMA_RING_INT3_MASK        0x90B8
0097 #define XGENE_DMA_RING_INT4_MASK        0x90C0
0098 #define XGENE_DMA_CFG_RING_WQ_ASSOC     0x90E0
0099 #define XGENE_DMA_ASSOC_RING_MNGR1      0xFFFFFFFF
0100 #define XGENE_DMA_MEM_RAM_SHUTDOWN      0xD070
0101 #define XGENE_DMA_BLK_MEM_RDY           0xD074
0102 #define XGENE_DMA_BLK_MEM_RDY_VAL       0xFFFFFFFF
0103 #define XGENE_DMA_RING_CMD_SM_OFFSET        0x8000
0104 
0105 /* X-Gene SoC EFUSE csr register and bit defination */
0106 #define XGENE_SOC_JTAG1_SHADOW          0x18
0107 #define XGENE_DMA_PQ_DISABLE_MASK       BIT(13)
0108 
0109 /* X-Gene DMA Descriptor format */
0110 #define XGENE_DMA_DESC_NV_BIT           BIT_ULL(50)
0111 #define XGENE_DMA_DESC_IN_BIT           BIT_ULL(55)
0112 #define XGENE_DMA_DESC_C_BIT            BIT_ULL(63)
0113 #define XGENE_DMA_DESC_DR_BIT           BIT_ULL(61)
0114 #define XGENE_DMA_DESC_ELERR_POS        46
0115 #define XGENE_DMA_DESC_RTYPE_POS        56
0116 #define XGENE_DMA_DESC_LERR_POS         60
0117 #define XGENE_DMA_DESC_BUFLEN_POS       48
0118 #define XGENE_DMA_DESC_HOENQ_NUM_POS        48
0119 #define XGENE_DMA_DESC_ELERR_RD(m)      \
0120     (((m) >> XGENE_DMA_DESC_ELERR_POS) & 0x3)
0121 #define XGENE_DMA_DESC_LERR_RD(m)       \
0122     (((m) >> XGENE_DMA_DESC_LERR_POS) & 0x7)
0123 #define XGENE_DMA_DESC_STATUS(elerr, lerr)  \
0124     (((elerr) << 4) | (lerr))
0125 
0126 /* X-Gene DMA descriptor empty s/w signature */
0127 #define XGENE_DMA_DESC_EMPTY_SIGNATURE      ~0ULL
0128 
0129 /* X-Gene DMA configurable parameters defines */
0130 #define XGENE_DMA_RING_NUM      512
0131 #define XGENE_DMA_BUFNUM        0x0
0132 #define XGENE_DMA_CPU_BUFNUM        0x18
0133 #define XGENE_DMA_RING_OWNER_DMA    0x03
0134 #define XGENE_DMA_RING_OWNER_CPU    0x0F
0135 #define XGENE_DMA_RING_TYPE_REGULAR 0x01
0136 #define XGENE_DMA_RING_WQ_DESC_SIZE 32  /* 32 Bytes */
0137 #define XGENE_DMA_RING_NUM_CONFIG   5
0138 #define XGENE_DMA_MAX_CHANNEL       4
0139 #define XGENE_DMA_XOR_CHANNEL       0
0140 #define XGENE_DMA_PQ_CHANNEL        1
0141 #define XGENE_DMA_MAX_BYTE_CNT      0x4000  /* 16 KB */
0142 #define XGENE_DMA_MAX_64B_DESC_BYTE_CNT 0x14000 /* 80 KB */
0143 #define XGENE_DMA_MAX_XOR_SRC       5
0144 #define XGENE_DMA_16K_BUFFER_LEN_CODE   0x0
0145 #define XGENE_DMA_INVALID_LEN_CODE  0x7800000000000000ULL
0146 
0147 /* X-Gene DMA descriptor error codes */
0148 #define ERR_DESC_AXI            0x01
0149 #define ERR_BAD_DESC            0x02
0150 #define ERR_READ_DATA_AXI       0x03
0151 #define ERR_WRITE_DATA_AXI      0x04
0152 #define ERR_FBP_TIMEOUT         0x05
0153 #define ERR_ECC             0x06
0154 #define ERR_DIFF_SIZE           0x08
0155 #define ERR_SCT_GAT_LEN         0x09
0156 #define ERR_CRC_ERR         0x11
0157 #define ERR_CHKSUM          0x12
0158 #define ERR_DIF             0x13
0159 
0160 /* X-Gene DMA error interrupt codes */
0161 #define ERR_DIF_SIZE_INT        0x0
0162 #define ERR_GS_ERR_INT          0x1
0163 #define ERR_FPB_TIMEO_INT       0x2
0164 #define ERR_WFIFO_OVF_INT       0x3
0165 #define ERR_RFIFO_OVF_INT       0x4
0166 #define ERR_WR_TIMEO_INT        0x5
0167 #define ERR_RD_TIMEO_INT        0x6
0168 #define ERR_WR_ERR_INT          0x7
0169 #define ERR_RD_ERR_INT          0x8
0170 #define ERR_BAD_DESC_INT        0x9
0171 #define ERR_DESC_DST_INT        0xA
0172 #define ERR_DESC_SRC_INT        0xB
0173 
0174 /* X-Gene DMA flyby operation code */
0175 #define FLYBY_2SRC_XOR          0x80
0176 #define FLYBY_3SRC_XOR          0x90
0177 #define FLYBY_4SRC_XOR          0xA0
0178 #define FLYBY_5SRC_XOR          0xB0
0179 
0180 /* X-Gene DMA SW descriptor flags */
0181 #define XGENE_DMA_FLAG_64B_DESC     BIT(0)
0182 
0183 /* Define to dump X-Gene DMA descriptor */
0184 #define XGENE_DMA_DESC_DUMP(desc, m)    \
0185     print_hex_dump(KERN_ERR, (m),   \
0186             DUMP_PREFIX_ADDRESS, 16, 8, (desc), 32, 0)
0187 
0188 #define to_dma_desc_sw(tx)      \
0189     container_of(tx, struct xgene_dma_desc_sw, tx)
0190 #define to_dma_chan(dchan)      \
0191     container_of(dchan, struct xgene_dma_chan, dma_chan)
0192 
0193 #define chan_dbg(chan, fmt, arg...) \
0194     dev_dbg(chan->dev, "%s: " fmt, chan->name, ##arg)
0195 #define chan_err(chan, fmt, arg...) \
0196     dev_err(chan->dev, "%s: " fmt, chan->name, ##arg)
0197 
0198 struct xgene_dma_desc_hw {
0199     __le64 m0;
0200     __le64 m1;
0201     __le64 m2;
0202     __le64 m3;
0203 };
0204 
0205 enum xgene_dma_ring_cfgsize {
0206     XGENE_DMA_RING_CFG_SIZE_512B,
0207     XGENE_DMA_RING_CFG_SIZE_2KB,
0208     XGENE_DMA_RING_CFG_SIZE_16KB,
0209     XGENE_DMA_RING_CFG_SIZE_64KB,
0210     XGENE_DMA_RING_CFG_SIZE_512KB,
0211     XGENE_DMA_RING_CFG_SIZE_INVALID
0212 };
0213 
0214 struct xgene_dma_ring {
0215     struct xgene_dma *pdma;
0216     u8 buf_num;
0217     u16 id;
0218     u16 num;
0219     u16 head;
0220     u16 owner;
0221     u16 slots;
0222     u16 dst_ring_num;
0223     u32 size;
0224     void __iomem *cmd;
0225     void __iomem *cmd_base;
0226     dma_addr_t desc_paddr;
0227     u32 state[XGENE_DMA_RING_NUM_CONFIG];
0228     enum xgene_dma_ring_cfgsize cfgsize;
0229     union {
0230         void *desc_vaddr;
0231         struct xgene_dma_desc_hw *desc_hw;
0232     };
0233 };
0234 
0235 struct xgene_dma_desc_sw {
0236     struct xgene_dma_desc_hw desc1;
0237     struct xgene_dma_desc_hw desc2;
0238     u32 flags;
0239     struct list_head node;
0240     struct list_head tx_list;
0241     struct dma_async_tx_descriptor tx;
0242 };
0243 
0244 /**
0245  * struct xgene_dma_chan - internal representation of an X-Gene DMA channel
0246  * @dma_chan: dmaengine channel object member
0247  * @pdma: X-Gene DMA device structure reference
0248  * @dev: struct device reference for dma mapping api
0249  * @id: raw id of this channel
0250  * @rx_irq: channel IRQ
0251  * @name: name of X-Gene DMA channel
0252  * @lock: serializes enqueue/dequeue operations to the descriptor pool
0253  * @pending: number of transaction request pushed to DMA controller for
0254  *  execution, but still waiting for completion,
0255  * @max_outstanding: max number of outstanding request we can push to channel
0256  * @ld_pending: descriptors which are queued to run, but have not yet been
0257  *  submitted to the hardware for execution
0258  * @ld_running: descriptors which are currently being executing by the hardware
0259  * @ld_completed: descriptors which have finished execution by the hardware.
0260  *  These descriptors have already had their cleanup actions run. They
0261  *  are waiting for the ACK bit to be set by the async tx API.
0262  * @desc_pool: descriptor pool for DMA operations
0263  * @tasklet: bottom half where all completed descriptors cleans
0264  * @tx_ring: transmit ring descriptor that we use to prepare actual
0265  *  descriptors for further executions
0266  * @rx_ring: receive ring descriptor that we use to get completed DMA
0267  *  descriptors during cleanup time
0268  */
0269 struct xgene_dma_chan {
0270     struct dma_chan dma_chan;
0271     struct xgene_dma *pdma;
0272     struct device *dev;
0273     int id;
0274     int rx_irq;
0275     char name[10];
0276     spinlock_t lock;
0277     int pending;
0278     int max_outstanding;
0279     struct list_head ld_pending;
0280     struct list_head ld_running;
0281     struct list_head ld_completed;
0282     struct dma_pool *desc_pool;
0283     struct tasklet_struct tasklet;
0284     struct xgene_dma_ring tx_ring;
0285     struct xgene_dma_ring rx_ring;
0286 };
0287 
0288 /**
0289  * struct xgene_dma - internal representation of an X-Gene DMA device
0290  * @dev: reference to this device's struct device
0291  * @clk: reference to this device's clock
0292  * @err_irq: DMA error irq number
0293  * @ring_num: start id number for DMA ring
0294  * @csr_dma: base for DMA register access
0295  * @csr_ring: base for DMA ring register access
0296  * @csr_ring_cmd: base for DMA ring command register access
0297  * @csr_efuse: base for efuse register access
0298  * @dma_dev: embedded struct dma_device
0299  * @chan: reference to X-Gene DMA channels
0300  */
0301 struct xgene_dma {
0302     struct device *dev;
0303     struct clk *clk;
0304     int err_irq;
0305     int ring_num;
0306     void __iomem *csr_dma;
0307     void __iomem *csr_ring;
0308     void __iomem *csr_ring_cmd;
0309     void __iomem *csr_efuse;
0310     struct dma_device dma_dev[XGENE_DMA_MAX_CHANNEL];
0311     struct xgene_dma_chan chan[XGENE_DMA_MAX_CHANNEL];
0312 };
0313 
0314 static const char * const xgene_dma_desc_err[] = {
0315     [ERR_DESC_AXI] = "AXI error when reading src/dst link list",
0316     [ERR_BAD_DESC] = "ERR or El_ERR fields not set to zero in desc",
0317     [ERR_READ_DATA_AXI] = "AXI error when reading data",
0318     [ERR_WRITE_DATA_AXI] = "AXI error when writing data",
0319     [ERR_FBP_TIMEOUT] = "Timeout on bufpool fetch",
0320     [ERR_ECC] = "ECC double bit error",
0321     [ERR_DIFF_SIZE] = "Bufpool too small to hold all the DIF result",
0322     [ERR_SCT_GAT_LEN] = "Gather and scatter data length not same",
0323     [ERR_CRC_ERR] = "CRC error",
0324     [ERR_CHKSUM] = "Checksum error",
0325     [ERR_DIF] = "DIF error",
0326 };
0327 
0328 static const char * const xgene_dma_err[] = {
0329     [ERR_DIF_SIZE_INT] = "DIF size error",
0330     [ERR_GS_ERR_INT] = "Gather scatter not same size error",
0331     [ERR_FPB_TIMEO_INT] = "Free pool time out error",
0332     [ERR_WFIFO_OVF_INT] = "Write FIFO over flow error",
0333     [ERR_RFIFO_OVF_INT] = "Read FIFO over flow error",
0334     [ERR_WR_TIMEO_INT] = "Write time out error",
0335     [ERR_RD_TIMEO_INT] = "Read time out error",
0336     [ERR_WR_ERR_INT] = "HBF bus write error",
0337     [ERR_RD_ERR_INT] = "HBF bus read error",
0338     [ERR_BAD_DESC_INT] = "Ring descriptor HE0 not set error",
0339     [ERR_DESC_DST_INT] = "HFB reading dst link address error",
0340     [ERR_DESC_SRC_INT] = "HFB reading src link address error",
0341 };
0342 
0343 static bool is_pq_enabled(struct xgene_dma *pdma)
0344 {
0345     u32 val;
0346 
0347     val = ioread32(pdma->csr_efuse + XGENE_SOC_JTAG1_SHADOW);
0348     return !(val & XGENE_DMA_PQ_DISABLE_MASK);
0349 }
0350 
0351 static u64 xgene_dma_encode_len(size_t len)
0352 {
0353     return (len < XGENE_DMA_MAX_BYTE_CNT) ?
0354         ((u64)len << XGENE_DMA_DESC_BUFLEN_POS) :
0355         XGENE_DMA_16K_BUFFER_LEN_CODE;
0356 }
0357 
0358 static u8 xgene_dma_encode_xor_flyby(u32 src_cnt)
0359 {
0360     static u8 flyby_type[] = {
0361         FLYBY_2SRC_XOR, /* Dummy */
0362         FLYBY_2SRC_XOR, /* Dummy */
0363         FLYBY_2SRC_XOR,
0364         FLYBY_3SRC_XOR,
0365         FLYBY_4SRC_XOR,
0366         FLYBY_5SRC_XOR
0367     };
0368 
0369     return flyby_type[src_cnt];
0370 }
0371 
0372 static void xgene_dma_set_src_buffer(__le64 *ext8, size_t *len,
0373                      dma_addr_t *paddr)
0374 {
0375     size_t nbytes = (*len < XGENE_DMA_MAX_BYTE_CNT) ?
0376             *len : XGENE_DMA_MAX_BYTE_CNT;
0377 
0378     *ext8 |= cpu_to_le64(*paddr);
0379     *ext8 |= cpu_to_le64(xgene_dma_encode_len(nbytes));
0380     *len -= nbytes;
0381     *paddr += nbytes;
0382 }
0383 
0384 static __le64 *xgene_dma_lookup_ext8(struct xgene_dma_desc_hw *desc, int idx)
0385 {
0386     switch (idx) {
0387     case 0:
0388         return &desc->m1;
0389     case 1:
0390         return &desc->m0;
0391     case 2:
0392         return &desc->m3;
0393     case 3:
0394         return &desc->m2;
0395     default:
0396         pr_err("Invalid dma descriptor index\n");
0397     }
0398 
0399     return NULL;
0400 }
0401 
0402 static void xgene_dma_init_desc(struct xgene_dma_desc_hw *desc,
0403                 u16 dst_ring_num)
0404 {
0405     desc->m0 |= cpu_to_le64(XGENE_DMA_DESC_IN_BIT);
0406     desc->m0 |= cpu_to_le64((u64)XGENE_DMA_RING_OWNER_DMA <<
0407                 XGENE_DMA_DESC_RTYPE_POS);
0408     desc->m1 |= cpu_to_le64(XGENE_DMA_DESC_C_BIT);
0409     desc->m3 |= cpu_to_le64((u64)dst_ring_num <<
0410                 XGENE_DMA_DESC_HOENQ_NUM_POS);
0411 }
0412 
0413 static void xgene_dma_prep_xor_desc(struct xgene_dma_chan *chan,
0414                     struct xgene_dma_desc_sw *desc_sw,
0415                     dma_addr_t *dst, dma_addr_t *src,
0416                     u32 src_cnt, size_t *nbytes,
0417                     const u8 *scf)
0418 {
0419     struct xgene_dma_desc_hw *desc1, *desc2;
0420     size_t len = *nbytes;
0421     int i;
0422 
0423     desc1 = &desc_sw->desc1;
0424     desc2 = &desc_sw->desc2;
0425 
0426     /* Initialize DMA descriptor */
0427     xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
0428 
0429     /* Set destination address */
0430     desc1->m2 |= cpu_to_le64(XGENE_DMA_DESC_DR_BIT);
0431     desc1->m3 |= cpu_to_le64(*dst);
0432 
0433     /* We have multiple source addresses, so need to set NV bit*/
0434     desc1->m0 |= cpu_to_le64(XGENE_DMA_DESC_NV_BIT);
0435 
0436     /* Set flyby opcode */
0437     desc1->m2 |= cpu_to_le64(xgene_dma_encode_xor_flyby(src_cnt));
0438 
0439     /* Set 1st to 5th source addresses */
0440     for (i = 0; i < src_cnt; i++) {
0441         len = *nbytes;
0442         xgene_dma_set_src_buffer((i == 0) ? &desc1->m1 :
0443                      xgene_dma_lookup_ext8(desc2, i - 1),
0444                      &len, &src[i]);
0445         desc1->m2 |= cpu_to_le64((scf[i] << ((i + 1) * 8)));
0446     }
0447 
0448     /* Update meta data */
0449     *nbytes = len;
0450     *dst += XGENE_DMA_MAX_BYTE_CNT;
0451 
0452     /* We need always 64B descriptor to perform xor or pq operations */
0453     desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
0454 }
0455 
0456 static dma_cookie_t xgene_dma_tx_submit(struct dma_async_tx_descriptor *tx)
0457 {
0458     struct xgene_dma_desc_sw *desc;
0459     struct xgene_dma_chan *chan;
0460     dma_cookie_t cookie;
0461 
0462     if (unlikely(!tx))
0463         return -EINVAL;
0464 
0465     chan = to_dma_chan(tx->chan);
0466     desc = to_dma_desc_sw(tx);
0467 
0468     spin_lock_bh(&chan->lock);
0469 
0470     cookie = dma_cookie_assign(tx);
0471 
0472     /* Add this transaction list onto the tail of the pending queue */
0473     list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
0474 
0475     spin_unlock_bh(&chan->lock);
0476 
0477     return cookie;
0478 }
0479 
0480 static void xgene_dma_clean_descriptor(struct xgene_dma_chan *chan,
0481                        struct xgene_dma_desc_sw *desc)
0482 {
0483     list_del(&desc->node);
0484     chan_dbg(chan, "LD %p free\n", desc);
0485     dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
0486 }
0487 
0488 static struct xgene_dma_desc_sw *xgene_dma_alloc_descriptor(
0489                  struct xgene_dma_chan *chan)
0490 {
0491     struct xgene_dma_desc_sw *desc;
0492     dma_addr_t phys;
0493 
0494     desc = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
0495     if (!desc) {
0496         chan_err(chan, "Failed to allocate LDs\n");
0497         return NULL;
0498     }
0499 
0500     INIT_LIST_HEAD(&desc->tx_list);
0501     desc->tx.phys = phys;
0502     desc->tx.tx_submit = xgene_dma_tx_submit;
0503     dma_async_tx_descriptor_init(&desc->tx, &chan->dma_chan);
0504 
0505     chan_dbg(chan, "LD %p allocated\n", desc);
0506 
0507     return desc;
0508 }
0509 
0510 /**
0511  * xgene_dma_clean_completed_descriptor - free all descriptors which
0512  * has been completed and acked
0513  * @chan: X-Gene DMA channel
0514  *
0515  * This function is used on all completed and acked descriptors.
0516  */
0517 static void xgene_dma_clean_completed_descriptor(struct xgene_dma_chan *chan)
0518 {
0519     struct xgene_dma_desc_sw *desc, *_desc;
0520 
0521     /* Run the callback for each descriptor, in order */
0522     list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node) {
0523         if (async_tx_test_ack(&desc->tx))
0524             xgene_dma_clean_descriptor(chan, desc);
0525     }
0526 }
0527 
0528 /**
0529  * xgene_dma_run_tx_complete_actions - cleanup a single link descriptor
0530  * @chan: X-Gene DMA channel
0531  * @desc: descriptor to cleanup and free
0532  *
0533  * This function is used on a descriptor which has been executed by the DMA
0534  * controller. It will run any callbacks, submit any dependencies.
0535  */
0536 static void xgene_dma_run_tx_complete_actions(struct xgene_dma_chan *chan,
0537                           struct xgene_dma_desc_sw *desc)
0538 {
0539     struct dma_async_tx_descriptor *tx = &desc->tx;
0540 
0541     /*
0542      * If this is not the last transaction in the group,
0543      * then no need to complete cookie and run any callback as
0544      * this is not the tx_descriptor which had been sent to caller
0545      * of this DMA request
0546      */
0547 
0548     if (tx->cookie == 0)
0549         return;
0550 
0551     dma_cookie_complete(tx);
0552     dma_descriptor_unmap(tx);
0553 
0554     /* Run the link descriptor callback function */
0555     dmaengine_desc_get_callback_invoke(tx, NULL);
0556 
0557     /* Run any dependencies */
0558     dma_run_dependencies(tx);
0559 }
0560 
0561 /**
0562  * xgene_dma_clean_running_descriptor - move the completed descriptor from
0563  * ld_running to ld_completed
0564  * @chan: X-Gene DMA channel
0565  * @desc: the descriptor which is completed
0566  *
0567  * Free the descriptor directly if acked by async_tx api,
0568  * else move it to queue ld_completed.
0569  */
0570 static void xgene_dma_clean_running_descriptor(struct xgene_dma_chan *chan,
0571                            struct xgene_dma_desc_sw *desc)
0572 {
0573     /* Remove from the list of running transactions */
0574     list_del(&desc->node);
0575 
0576     /*
0577      * the client is allowed to attach dependent operations
0578      * until 'ack' is set
0579      */
0580     if (!async_tx_test_ack(&desc->tx)) {
0581         /*
0582          * Move this descriptor to the list of descriptors which is
0583          * completed, but still awaiting the 'ack' bit to be set.
0584          */
0585         list_add_tail(&desc->node, &chan->ld_completed);
0586         return;
0587     }
0588 
0589     chan_dbg(chan, "LD %p free\n", desc);
0590     dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
0591 }
0592 
0593 static void xgene_chan_xfer_request(struct xgene_dma_chan *chan,
0594                     struct xgene_dma_desc_sw *desc_sw)
0595 {
0596     struct xgene_dma_ring *ring = &chan->tx_ring;
0597     struct xgene_dma_desc_hw *desc_hw;
0598 
0599     /* Get hw descriptor from DMA tx ring */
0600     desc_hw = &ring->desc_hw[ring->head];
0601 
0602     /*
0603      * Increment the head count to point next
0604      * descriptor for next time
0605      */
0606     if (++ring->head == ring->slots)
0607         ring->head = 0;
0608 
0609     /* Copy prepared sw descriptor data to hw descriptor */
0610     memcpy(desc_hw, &desc_sw->desc1, sizeof(*desc_hw));
0611 
0612     /*
0613      * Check if we have prepared 64B descriptor,
0614      * in this case we need one more hw descriptor
0615      */
0616     if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) {
0617         desc_hw = &ring->desc_hw[ring->head];
0618 
0619         if (++ring->head == ring->slots)
0620             ring->head = 0;
0621 
0622         memcpy(desc_hw, &desc_sw->desc2, sizeof(*desc_hw));
0623     }
0624 
0625     /* Increment the pending transaction count */
0626     chan->pending += ((desc_sw->flags &
0627               XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
0628 
0629     /* Notify the hw that we have descriptor ready for execution */
0630     iowrite32((desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) ?
0631           2 : 1, ring->cmd);
0632 }
0633 
0634 /**
0635  * xgene_chan_xfer_ld_pending - push any pending transactions to hw
0636  * @chan : X-Gene DMA channel
0637  *
0638  * LOCKING: must hold chan->lock
0639  */
0640 static void xgene_chan_xfer_ld_pending(struct xgene_dma_chan *chan)
0641 {
0642     struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
0643 
0644     /*
0645      * If the list of pending descriptors is empty, then we
0646      * don't need to do any work at all
0647      */
0648     if (list_empty(&chan->ld_pending)) {
0649         chan_dbg(chan, "No pending LDs\n");
0650         return;
0651     }
0652 
0653     /*
0654      * Move elements from the queue of pending transactions onto the list
0655      * of running transactions and push it to hw for further executions
0656      */
0657     list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_pending, node) {
0658         /*
0659          * Check if have pushed max number of transactions to hw
0660          * as capable, so let's stop here and will push remaining
0661          * elements from pening ld queue after completing some
0662          * descriptors that we have already pushed
0663          */
0664         if (chan->pending >= chan->max_outstanding)
0665             return;
0666 
0667         xgene_chan_xfer_request(chan, desc_sw);
0668 
0669         /*
0670          * Delete this element from ld pending queue and append it to
0671          * ld running queue
0672          */
0673         list_move_tail(&desc_sw->node, &chan->ld_running);
0674     }
0675 }
0676 
0677 /**
0678  * xgene_dma_cleanup_descriptors - cleanup link descriptors which are completed
0679  * and move them to ld_completed to free until flag 'ack' is set
0680  * @chan: X-Gene DMA channel
0681  *
0682  * This function is used on descriptors which have been executed by the DMA
0683  * controller. It will run any callbacks, submit any dependencies, then
0684  * free these descriptors if flag 'ack' is set.
0685  */
0686 static void xgene_dma_cleanup_descriptors(struct xgene_dma_chan *chan)
0687 {
0688     struct xgene_dma_ring *ring = &chan->rx_ring;
0689     struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
0690     struct xgene_dma_desc_hw *desc_hw;
0691     struct list_head ld_completed;
0692     u8 status;
0693 
0694     INIT_LIST_HEAD(&ld_completed);
0695 
0696     spin_lock(&chan->lock);
0697 
0698     /* Clean already completed and acked descriptors */
0699     xgene_dma_clean_completed_descriptor(chan);
0700 
0701     /* Move all completed descriptors to ld completed queue, in order */
0702     list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_running, node) {
0703         /* Get subsequent hw descriptor from DMA rx ring */
0704         desc_hw = &ring->desc_hw[ring->head];
0705 
0706         /* Check if this descriptor has been completed */
0707         if (unlikely(le64_to_cpu(desc_hw->m0) ==
0708                  XGENE_DMA_DESC_EMPTY_SIGNATURE))
0709             break;
0710 
0711         if (++ring->head == ring->slots)
0712             ring->head = 0;
0713 
0714         /* Check if we have any error with DMA transactions */
0715         status = XGENE_DMA_DESC_STATUS(
0716                 XGENE_DMA_DESC_ELERR_RD(le64_to_cpu(
0717                             desc_hw->m0)),
0718                 XGENE_DMA_DESC_LERR_RD(le64_to_cpu(
0719                                desc_hw->m0)));
0720         if (status) {
0721             /* Print the DMA error type */
0722             chan_err(chan, "%s\n", xgene_dma_desc_err[status]);
0723 
0724             /*
0725              * We have DMA transactions error here. Dump DMA Tx
0726              * and Rx descriptors for this request */
0727             XGENE_DMA_DESC_DUMP(&desc_sw->desc1,
0728                         "X-Gene DMA TX DESC1: ");
0729 
0730             if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC)
0731                 XGENE_DMA_DESC_DUMP(&desc_sw->desc2,
0732                             "X-Gene DMA TX DESC2: ");
0733 
0734             XGENE_DMA_DESC_DUMP(desc_hw,
0735                         "X-Gene DMA RX ERR DESC: ");
0736         }
0737 
0738         /* Notify the hw about this completed descriptor */
0739         iowrite32(-1, ring->cmd);
0740 
0741         /* Mark this hw descriptor as processed */
0742         desc_hw->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
0743 
0744         /*
0745          * Decrement the pending transaction count
0746          * as we have processed one
0747          */
0748         chan->pending -= ((desc_sw->flags &
0749                   XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
0750 
0751         /*
0752          * Delete this node from ld running queue and append it to
0753          * ld completed queue for further processing
0754          */
0755         list_move_tail(&desc_sw->node, &ld_completed);
0756     }
0757 
0758     /*
0759      * Start any pending transactions automatically
0760      * In the ideal case, we keep the DMA controller busy while we go
0761      * ahead and free the descriptors below.
0762      */
0763     xgene_chan_xfer_ld_pending(chan);
0764 
0765     spin_unlock(&chan->lock);
0766 
0767     /* Run the callback for each descriptor, in order */
0768     list_for_each_entry_safe(desc_sw, _desc_sw, &ld_completed, node) {
0769         xgene_dma_run_tx_complete_actions(chan, desc_sw);
0770         xgene_dma_clean_running_descriptor(chan, desc_sw);
0771     }
0772 }
0773 
0774 static int xgene_dma_alloc_chan_resources(struct dma_chan *dchan)
0775 {
0776     struct xgene_dma_chan *chan = to_dma_chan(dchan);
0777 
0778     /* Has this channel already been allocated? */
0779     if (chan->desc_pool)
0780         return 1;
0781 
0782     chan->desc_pool = dma_pool_create(chan->name, chan->dev,
0783                       sizeof(struct xgene_dma_desc_sw),
0784                       0, 0);
0785     if (!chan->desc_pool) {
0786         chan_err(chan, "Failed to allocate descriptor pool\n");
0787         return -ENOMEM;
0788     }
0789 
0790     chan_dbg(chan, "Allocate descriptor pool\n");
0791 
0792     return 1;
0793 }
0794 
0795 /**
0796  * xgene_dma_free_desc_list - Free all descriptors in a queue
0797  * @chan: X-Gene DMA channel
0798  * @list: the list to free
0799  *
0800  * LOCKING: must hold chan->lock
0801  */
0802 static void xgene_dma_free_desc_list(struct xgene_dma_chan *chan,
0803                      struct list_head *list)
0804 {
0805     struct xgene_dma_desc_sw *desc, *_desc;
0806 
0807     list_for_each_entry_safe(desc, _desc, list, node)
0808         xgene_dma_clean_descriptor(chan, desc);
0809 }
0810 
0811 static void xgene_dma_free_chan_resources(struct dma_chan *dchan)
0812 {
0813     struct xgene_dma_chan *chan = to_dma_chan(dchan);
0814 
0815     chan_dbg(chan, "Free all resources\n");
0816 
0817     if (!chan->desc_pool)
0818         return;
0819 
0820     /* Process all running descriptor */
0821     xgene_dma_cleanup_descriptors(chan);
0822 
0823     spin_lock_bh(&chan->lock);
0824 
0825     /* Clean all link descriptor queues */
0826     xgene_dma_free_desc_list(chan, &chan->ld_pending);
0827     xgene_dma_free_desc_list(chan, &chan->ld_running);
0828     xgene_dma_free_desc_list(chan, &chan->ld_completed);
0829 
0830     spin_unlock_bh(&chan->lock);
0831 
0832     /* Delete this channel DMA pool */
0833     dma_pool_destroy(chan->desc_pool);
0834     chan->desc_pool = NULL;
0835 }
0836 
0837 static struct dma_async_tx_descriptor *xgene_dma_prep_xor(
0838     struct dma_chan *dchan, dma_addr_t dst, dma_addr_t *src,
0839     u32 src_cnt, size_t len, unsigned long flags)
0840 {
0841     struct xgene_dma_desc_sw *first = NULL, *new;
0842     struct xgene_dma_chan *chan;
0843     static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {
0844                 0x01, 0x01, 0x01, 0x01, 0x01};
0845 
0846     if (unlikely(!dchan || !len))
0847         return NULL;
0848 
0849     chan = to_dma_chan(dchan);
0850 
0851     do {
0852         /* Allocate the link descriptor from DMA pool */
0853         new = xgene_dma_alloc_descriptor(chan);
0854         if (!new)
0855             goto fail;
0856 
0857         /* Prepare xor DMA descriptor */
0858         xgene_dma_prep_xor_desc(chan, new, &dst, src,
0859                     src_cnt, &len, multi);
0860 
0861         if (!first)
0862             first = new;
0863 
0864         new->tx.cookie = 0;
0865         async_tx_ack(&new->tx);
0866 
0867         /* Insert the link descriptor to the LD ring */
0868         list_add_tail(&new->node, &first->tx_list);
0869     } while (len);
0870 
0871     new->tx.flags = flags; /* client is in control of this ack */
0872     new->tx.cookie = -EBUSY;
0873     list_splice(&first->tx_list, &new->tx_list);
0874 
0875     return &new->tx;
0876 
0877 fail:
0878     if (!first)
0879         return NULL;
0880 
0881     xgene_dma_free_desc_list(chan, &first->tx_list);
0882     return NULL;
0883 }
0884 
0885 static struct dma_async_tx_descriptor *xgene_dma_prep_pq(
0886     struct dma_chan *dchan, dma_addr_t *dst, dma_addr_t *src,
0887     u32 src_cnt, const u8 *scf, size_t len, unsigned long flags)
0888 {
0889     struct xgene_dma_desc_sw *first = NULL, *new;
0890     struct xgene_dma_chan *chan;
0891     size_t _len = len;
0892     dma_addr_t _src[XGENE_DMA_MAX_XOR_SRC];
0893     static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {0x01, 0x01, 0x01, 0x01, 0x01};
0894 
0895     if (unlikely(!dchan || !len))
0896         return NULL;
0897 
0898     chan = to_dma_chan(dchan);
0899 
0900     /*
0901      * Save source addresses on local variable, may be we have to
0902      * prepare two descriptor to generate P and Q if both enabled
0903      * in the flags by client
0904      */
0905     memcpy(_src, src, sizeof(*src) * src_cnt);
0906 
0907     if (flags & DMA_PREP_PQ_DISABLE_P)
0908         len = 0;
0909 
0910     if (flags & DMA_PREP_PQ_DISABLE_Q)
0911         _len = 0;
0912 
0913     do {
0914         /* Allocate the link descriptor from DMA pool */
0915         new = xgene_dma_alloc_descriptor(chan);
0916         if (!new)
0917             goto fail;
0918 
0919         if (!first)
0920             first = new;
0921 
0922         new->tx.cookie = 0;
0923         async_tx_ack(&new->tx);
0924 
0925         /* Insert the link descriptor to the LD ring */
0926         list_add_tail(&new->node, &first->tx_list);
0927 
0928         /*
0929          * Prepare DMA descriptor to generate P,
0930          * if DMA_PREP_PQ_DISABLE_P flag is not set
0931          */
0932         if (len) {
0933             xgene_dma_prep_xor_desc(chan, new, &dst[0], src,
0934                         src_cnt, &len, multi);
0935             continue;
0936         }
0937 
0938         /*
0939          * Prepare DMA descriptor to generate Q,
0940          * if DMA_PREP_PQ_DISABLE_Q flag is not set
0941          */
0942         if (_len) {
0943             xgene_dma_prep_xor_desc(chan, new, &dst[1], _src,
0944                         src_cnt, &_len, scf);
0945         }
0946     } while (len || _len);
0947 
0948     new->tx.flags = flags; /* client is in control of this ack */
0949     new->tx.cookie = -EBUSY;
0950     list_splice(&first->tx_list, &new->tx_list);
0951 
0952     return &new->tx;
0953 
0954 fail:
0955     if (!first)
0956         return NULL;
0957 
0958     xgene_dma_free_desc_list(chan, &first->tx_list);
0959     return NULL;
0960 }
0961 
0962 static void xgene_dma_issue_pending(struct dma_chan *dchan)
0963 {
0964     struct xgene_dma_chan *chan = to_dma_chan(dchan);
0965 
0966     spin_lock_bh(&chan->lock);
0967     xgene_chan_xfer_ld_pending(chan);
0968     spin_unlock_bh(&chan->lock);
0969 }
0970 
0971 static enum dma_status xgene_dma_tx_status(struct dma_chan *dchan,
0972                        dma_cookie_t cookie,
0973                        struct dma_tx_state *txstate)
0974 {
0975     return dma_cookie_status(dchan, cookie, txstate);
0976 }
0977 
0978 static void xgene_dma_tasklet_cb(struct tasklet_struct *t)
0979 {
0980     struct xgene_dma_chan *chan = from_tasklet(chan, t, tasklet);
0981 
0982     /* Run all cleanup for descriptors which have been completed */
0983     xgene_dma_cleanup_descriptors(chan);
0984 
0985     /* Re-enable DMA channel IRQ */
0986     enable_irq(chan->rx_irq);
0987 }
0988 
0989 static irqreturn_t xgene_dma_chan_ring_isr(int irq, void *id)
0990 {
0991     struct xgene_dma_chan *chan = (struct xgene_dma_chan *)id;
0992 
0993     BUG_ON(!chan);
0994 
0995     /*
0996      * Disable DMA channel IRQ until we process completed
0997      * descriptors
0998      */
0999     disable_irq_nosync(chan->rx_irq);
1000 
1001     /*
1002      * Schedule the tasklet to handle all cleanup of the current
1003      * transaction. It will start a new transaction if there is
1004      * one pending.
1005      */
1006     tasklet_schedule(&chan->tasklet);
1007 
1008     return IRQ_HANDLED;
1009 }
1010 
1011 static irqreturn_t xgene_dma_err_isr(int irq, void *id)
1012 {
1013     struct xgene_dma *pdma = (struct xgene_dma *)id;
1014     unsigned long int_mask;
1015     u32 val, i;
1016 
1017     val = ioread32(pdma->csr_dma + XGENE_DMA_INT);
1018 
1019     /* Clear DMA interrupts */
1020     iowrite32(val, pdma->csr_dma + XGENE_DMA_INT);
1021 
1022     /* Print DMA error info */
1023     int_mask = val >> XGENE_DMA_INT_MASK_SHIFT;
1024     for_each_set_bit(i, &int_mask, ARRAY_SIZE(xgene_dma_err))
1025         dev_err(pdma->dev,
1026             "Interrupt status 0x%08X %s\n", val, xgene_dma_err[i]);
1027 
1028     return IRQ_HANDLED;
1029 }
1030 
1031 static void xgene_dma_wr_ring_state(struct xgene_dma_ring *ring)
1032 {
1033     int i;
1034 
1035     iowrite32(ring->num, ring->pdma->csr_ring + XGENE_DMA_RING_STATE);
1036 
1037     for (i = 0; i < XGENE_DMA_RING_NUM_CONFIG; i++)
1038         iowrite32(ring->state[i], ring->pdma->csr_ring +
1039               XGENE_DMA_RING_STATE_WR_BASE + (i * 4));
1040 }
1041 
1042 static void xgene_dma_clr_ring_state(struct xgene_dma_ring *ring)
1043 {
1044     memset(ring->state, 0, sizeof(u32) * XGENE_DMA_RING_NUM_CONFIG);
1045     xgene_dma_wr_ring_state(ring);
1046 }
1047 
1048 static void xgene_dma_setup_ring(struct xgene_dma_ring *ring)
1049 {
1050     void *ring_cfg = ring->state;
1051     u64 addr = ring->desc_paddr;
1052     u32 i, val;
1053 
1054     ring->slots = ring->size / XGENE_DMA_RING_WQ_DESC_SIZE;
1055 
1056     /* Clear DMA ring state */
1057     xgene_dma_clr_ring_state(ring);
1058 
1059     /* Set DMA ring type */
1060     XGENE_DMA_RING_TYPE_SET(ring_cfg, XGENE_DMA_RING_TYPE_REGULAR);
1061 
1062     if (ring->owner == XGENE_DMA_RING_OWNER_DMA) {
1063         /* Set recombination buffer and timeout */
1064         XGENE_DMA_RING_RECOMBBUF_SET(ring_cfg);
1065         XGENE_DMA_RING_RECOMTIMEOUTL_SET(ring_cfg);
1066         XGENE_DMA_RING_RECOMTIMEOUTH_SET(ring_cfg);
1067     }
1068 
1069     /* Initialize DMA ring state */
1070     XGENE_DMA_RING_SELTHRSH_SET(ring_cfg);
1071     XGENE_DMA_RING_ACCEPTLERR_SET(ring_cfg);
1072     XGENE_DMA_RING_COHERENT_SET(ring_cfg);
1073     XGENE_DMA_RING_ADDRL_SET(ring_cfg, addr);
1074     XGENE_DMA_RING_ADDRH_SET(ring_cfg, addr);
1075     XGENE_DMA_RING_SIZE_SET(ring_cfg, ring->cfgsize);
1076 
1077     /* Write DMA ring configurations */
1078     xgene_dma_wr_ring_state(ring);
1079 
1080     /* Set DMA ring id */
1081     iowrite32(XGENE_DMA_RING_ID_SETUP(ring->id),
1082           ring->pdma->csr_ring + XGENE_DMA_RING_ID);
1083 
1084     /* Set DMA ring buffer */
1085     iowrite32(XGENE_DMA_RING_ID_BUF_SETUP(ring->num),
1086           ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
1087 
1088     if (ring->owner != XGENE_DMA_RING_OWNER_CPU)
1089         return;
1090 
1091     /* Set empty signature to DMA Rx ring descriptors */
1092     for (i = 0; i < ring->slots; i++) {
1093         struct xgene_dma_desc_hw *desc;
1094 
1095         desc = &ring->desc_hw[i];
1096         desc->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
1097     }
1098 
1099     /* Enable DMA Rx ring interrupt */
1100     val = ioread32(ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
1101     XGENE_DMA_RING_NE_INT_MODE_SET(val, ring->buf_num);
1102     iowrite32(val, ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
1103 }
1104 
1105 static void xgene_dma_clear_ring(struct xgene_dma_ring *ring)
1106 {
1107     u32 ring_id, val;
1108 
1109     if (ring->owner == XGENE_DMA_RING_OWNER_CPU) {
1110         /* Disable DMA Rx ring interrupt */
1111         val = ioread32(ring->pdma->csr_ring +
1112                    XGENE_DMA_RING_NE_INT_MODE);
1113         XGENE_DMA_RING_NE_INT_MODE_RESET(val, ring->buf_num);
1114         iowrite32(val, ring->pdma->csr_ring +
1115               XGENE_DMA_RING_NE_INT_MODE);
1116     }
1117 
1118     /* Clear DMA ring state */
1119     ring_id = XGENE_DMA_RING_ID_SETUP(ring->id);
1120     iowrite32(ring_id, ring->pdma->csr_ring + XGENE_DMA_RING_ID);
1121 
1122     iowrite32(0, ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
1123     xgene_dma_clr_ring_state(ring);
1124 }
1125 
1126 static void xgene_dma_set_ring_cmd(struct xgene_dma_ring *ring)
1127 {
1128     ring->cmd_base = ring->pdma->csr_ring_cmd +
1129                 XGENE_DMA_RING_CMD_BASE_OFFSET((ring->num -
1130                               XGENE_DMA_RING_NUM));
1131 
1132     ring->cmd = ring->cmd_base + XGENE_DMA_RING_CMD_OFFSET;
1133 }
1134 
1135 static int xgene_dma_get_ring_size(struct xgene_dma_chan *chan,
1136                    enum xgene_dma_ring_cfgsize cfgsize)
1137 {
1138     int size;
1139 
1140     switch (cfgsize) {
1141     case XGENE_DMA_RING_CFG_SIZE_512B:
1142         size = 0x200;
1143         break;
1144     case XGENE_DMA_RING_CFG_SIZE_2KB:
1145         size = 0x800;
1146         break;
1147     case XGENE_DMA_RING_CFG_SIZE_16KB:
1148         size = 0x4000;
1149         break;
1150     case XGENE_DMA_RING_CFG_SIZE_64KB:
1151         size = 0x10000;
1152         break;
1153     case XGENE_DMA_RING_CFG_SIZE_512KB:
1154         size = 0x80000;
1155         break;
1156     default:
1157         chan_err(chan, "Unsupported cfg ring size %d\n", cfgsize);
1158         return -EINVAL;
1159     }
1160 
1161     return size;
1162 }
1163 
1164 static void xgene_dma_delete_ring_one(struct xgene_dma_ring *ring)
1165 {
1166     /* Clear DMA ring configurations */
1167     xgene_dma_clear_ring(ring);
1168 
1169     /* De-allocate DMA ring descriptor */
1170     if (ring->desc_vaddr) {
1171         dma_free_coherent(ring->pdma->dev, ring->size,
1172                   ring->desc_vaddr, ring->desc_paddr);
1173         ring->desc_vaddr = NULL;
1174     }
1175 }
1176 
1177 static void xgene_dma_delete_chan_rings(struct xgene_dma_chan *chan)
1178 {
1179     xgene_dma_delete_ring_one(&chan->rx_ring);
1180     xgene_dma_delete_ring_one(&chan->tx_ring);
1181 }
1182 
1183 static int xgene_dma_create_ring_one(struct xgene_dma_chan *chan,
1184                      struct xgene_dma_ring *ring,
1185                      enum xgene_dma_ring_cfgsize cfgsize)
1186 {
1187     int ret;
1188 
1189     /* Setup DMA ring descriptor variables */
1190     ring->pdma = chan->pdma;
1191     ring->cfgsize = cfgsize;
1192     ring->num = chan->pdma->ring_num++;
1193     ring->id = XGENE_DMA_RING_ID_GET(ring->owner, ring->buf_num);
1194 
1195     ret = xgene_dma_get_ring_size(chan, cfgsize);
1196     if (ret <= 0)
1197         return ret;
1198     ring->size = ret;
1199 
1200     /* Allocate memory for DMA ring descriptor */
1201     ring->desc_vaddr = dma_alloc_coherent(chan->dev, ring->size,
1202                           &ring->desc_paddr, GFP_KERNEL);
1203     if (!ring->desc_vaddr) {
1204         chan_err(chan, "Failed to allocate ring desc\n");
1205         return -ENOMEM;
1206     }
1207 
1208     /* Configure and enable DMA ring */
1209     xgene_dma_set_ring_cmd(ring);
1210     xgene_dma_setup_ring(ring);
1211 
1212     return 0;
1213 }
1214 
1215 static int xgene_dma_create_chan_rings(struct xgene_dma_chan *chan)
1216 {
1217     struct xgene_dma_ring *rx_ring = &chan->rx_ring;
1218     struct xgene_dma_ring *tx_ring = &chan->tx_ring;
1219     int ret;
1220 
1221     /* Create DMA Rx ring descriptor */
1222     rx_ring->owner = XGENE_DMA_RING_OWNER_CPU;
1223     rx_ring->buf_num = XGENE_DMA_CPU_BUFNUM + chan->id;
1224 
1225     ret = xgene_dma_create_ring_one(chan, rx_ring,
1226                     XGENE_DMA_RING_CFG_SIZE_64KB);
1227     if (ret)
1228         return ret;
1229 
1230     chan_dbg(chan, "Rx ring id 0x%X num %d desc 0x%p\n",
1231          rx_ring->id, rx_ring->num, rx_ring->desc_vaddr);
1232 
1233     /* Create DMA Tx ring descriptor */
1234     tx_ring->owner = XGENE_DMA_RING_OWNER_DMA;
1235     tx_ring->buf_num = XGENE_DMA_BUFNUM + chan->id;
1236 
1237     ret = xgene_dma_create_ring_one(chan, tx_ring,
1238                     XGENE_DMA_RING_CFG_SIZE_64KB);
1239     if (ret) {
1240         xgene_dma_delete_ring_one(rx_ring);
1241         return ret;
1242     }
1243 
1244     tx_ring->dst_ring_num = XGENE_DMA_RING_DST_ID(rx_ring->num);
1245 
1246     chan_dbg(chan,
1247          "Tx ring id 0x%X num %d desc 0x%p\n",
1248          tx_ring->id, tx_ring->num, tx_ring->desc_vaddr);
1249 
1250     /* Set the max outstanding request possible to this channel */
1251     chan->max_outstanding = tx_ring->slots;
1252 
1253     return ret;
1254 }
1255 
1256 static int xgene_dma_init_rings(struct xgene_dma *pdma)
1257 {
1258     int ret, i, j;
1259 
1260     for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1261         ret = xgene_dma_create_chan_rings(&pdma->chan[i]);
1262         if (ret) {
1263             for (j = 0; j < i; j++)
1264                 xgene_dma_delete_chan_rings(&pdma->chan[j]);
1265             return ret;
1266         }
1267     }
1268 
1269     return ret;
1270 }
1271 
1272 static void xgene_dma_enable(struct xgene_dma *pdma)
1273 {
1274     u32 val;
1275 
1276     /* Configure and enable DMA engine */
1277     val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
1278     XGENE_DMA_CH_SETUP(val);
1279     XGENE_DMA_ENABLE(val);
1280     iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
1281 }
1282 
1283 static void xgene_dma_disable(struct xgene_dma *pdma)
1284 {
1285     u32 val;
1286 
1287     val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
1288     XGENE_DMA_DISABLE(val);
1289     iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
1290 }
1291 
1292 static void xgene_dma_mask_interrupts(struct xgene_dma *pdma)
1293 {
1294     /*
1295      * Mask DMA ring overflow, underflow and
1296      * AXI write/read error interrupts
1297      */
1298     iowrite32(XGENE_DMA_INT_ALL_MASK,
1299           pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
1300     iowrite32(XGENE_DMA_INT_ALL_MASK,
1301           pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
1302     iowrite32(XGENE_DMA_INT_ALL_MASK,
1303           pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
1304     iowrite32(XGENE_DMA_INT_ALL_MASK,
1305           pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
1306     iowrite32(XGENE_DMA_INT_ALL_MASK,
1307           pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
1308 
1309     /* Mask DMA error interrupts */
1310     iowrite32(XGENE_DMA_INT_ALL_MASK, pdma->csr_dma + XGENE_DMA_INT_MASK);
1311 }
1312 
1313 static void xgene_dma_unmask_interrupts(struct xgene_dma *pdma)
1314 {
1315     /*
1316      * Unmask DMA ring overflow, underflow and
1317      * AXI write/read error interrupts
1318      */
1319     iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1320           pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
1321     iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1322           pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
1323     iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1324           pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
1325     iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1326           pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
1327     iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1328           pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
1329 
1330     /* Unmask DMA error interrupts */
1331     iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1332           pdma->csr_dma + XGENE_DMA_INT_MASK);
1333 }
1334 
1335 static void xgene_dma_init_hw(struct xgene_dma *pdma)
1336 {
1337     u32 val;
1338 
1339     /* Associate DMA ring to corresponding ring HW */
1340     iowrite32(XGENE_DMA_ASSOC_RING_MNGR1,
1341           pdma->csr_dma + XGENE_DMA_CFG_RING_WQ_ASSOC);
1342 
1343     /* Configure RAID6 polynomial control setting */
1344     if (is_pq_enabled(pdma))
1345         iowrite32(XGENE_DMA_RAID6_MULTI_CTRL(0x1D),
1346               pdma->csr_dma + XGENE_DMA_RAID6_CONT);
1347     else
1348         dev_info(pdma->dev, "PQ is disabled in HW\n");
1349 
1350     xgene_dma_enable(pdma);
1351     xgene_dma_unmask_interrupts(pdma);
1352 
1353     /* Get DMA id and version info */
1354     val = ioread32(pdma->csr_dma + XGENE_DMA_IPBRR);
1355 
1356     /* DMA device info */
1357     dev_info(pdma->dev,
1358          "X-Gene DMA v%d.%02d.%02d driver registered %d channels",
1359          XGENE_DMA_REV_NO_RD(val), XGENE_DMA_BUS_ID_RD(val),
1360          XGENE_DMA_DEV_ID_RD(val), XGENE_DMA_MAX_CHANNEL);
1361 }
1362 
1363 static int xgene_dma_init_ring_mngr(struct xgene_dma *pdma)
1364 {
1365     if (ioread32(pdma->csr_ring + XGENE_DMA_RING_CLKEN) &&
1366         (!ioread32(pdma->csr_ring + XGENE_DMA_RING_SRST)))
1367         return 0;
1368 
1369     iowrite32(0x3, pdma->csr_ring + XGENE_DMA_RING_CLKEN);
1370     iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_SRST);
1371 
1372     /* Bring up memory */
1373     iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
1374 
1375     /* Force a barrier */
1376     ioread32(pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
1377 
1378     /* reset may take up to 1ms */
1379     usleep_range(1000, 1100);
1380 
1381     if (ioread32(pdma->csr_ring + XGENE_DMA_RING_BLK_MEM_RDY)
1382         != XGENE_DMA_RING_BLK_MEM_RDY_VAL) {
1383         dev_err(pdma->dev,
1384             "Failed to release ring mngr memory from shutdown\n");
1385         return -ENODEV;
1386     }
1387 
1388     /* program threshold set 1 and all hysteresis */
1389     iowrite32(XGENE_DMA_RING_THRESLD0_SET1_VAL,
1390           pdma->csr_ring + XGENE_DMA_RING_THRESLD0_SET1);
1391     iowrite32(XGENE_DMA_RING_THRESLD1_SET1_VAL,
1392           pdma->csr_ring + XGENE_DMA_RING_THRESLD1_SET1);
1393     iowrite32(XGENE_DMA_RING_HYSTERESIS_VAL,
1394           pdma->csr_ring + XGENE_DMA_RING_HYSTERESIS);
1395 
1396     /* Enable QPcore and assign error queue */
1397     iowrite32(XGENE_DMA_RING_ENABLE,
1398           pdma->csr_ring + XGENE_DMA_RING_CONFIG);
1399 
1400     return 0;
1401 }
1402 
1403 static int xgene_dma_init_mem(struct xgene_dma *pdma)
1404 {
1405     int ret;
1406 
1407     ret = xgene_dma_init_ring_mngr(pdma);
1408     if (ret)
1409         return ret;
1410 
1411     /* Bring up memory */
1412     iowrite32(0x0, pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
1413 
1414     /* Force a barrier */
1415     ioread32(pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
1416 
1417     /* reset may take up to 1ms */
1418     usleep_range(1000, 1100);
1419 
1420     if (ioread32(pdma->csr_dma + XGENE_DMA_BLK_MEM_RDY)
1421         != XGENE_DMA_BLK_MEM_RDY_VAL) {
1422         dev_err(pdma->dev,
1423             "Failed to release DMA memory from shutdown\n");
1424         return -ENODEV;
1425     }
1426 
1427     return 0;
1428 }
1429 
1430 static int xgene_dma_request_irqs(struct xgene_dma *pdma)
1431 {
1432     struct xgene_dma_chan *chan;
1433     int ret, i, j;
1434 
1435     /* Register DMA error irq */
1436     ret = devm_request_irq(pdma->dev, pdma->err_irq, xgene_dma_err_isr,
1437                    0, "dma_error", pdma);
1438     if (ret) {
1439         dev_err(pdma->dev,
1440             "Failed to register error IRQ %d\n", pdma->err_irq);
1441         return ret;
1442     }
1443 
1444     /* Register DMA channel rx irq */
1445     for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1446         chan = &pdma->chan[i];
1447         irq_set_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
1448         ret = devm_request_irq(chan->dev, chan->rx_irq,
1449                        xgene_dma_chan_ring_isr,
1450                        0, chan->name, chan);
1451         if (ret) {
1452             chan_err(chan, "Failed to register Rx IRQ %d\n",
1453                  chan->rx_irq);
1454             devm_free_irq(pdma->dev, pdma->err_irq, pdma);
1455 
1456             for (j = 0; j < i; j++) {
1457                 chan = &pdma->chan[i];
1458                 irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
1459                 devm_free_irq(chan->dev, chan->rx_irq, chan);
1460             }
1461 
1462             return ret;
1463         }
1464     }
1465 
1466     return 0;
1467 }
1468 
1469 static void xgene_dma_free_irqs(struct xgene_dma *pdma)
1470 {
1471     struct xgene_dma_chan *chan;
1472     int i;
1473 
1474     /* Free DMA device error irq */
1475     devm_free_irq(pdma->dev, pdma->err_irq, pdma);
1476 
1477     for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1478         chan = &pdma->chan[i];
1479         irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
1480         devm_free_irq(chan->dev, chan->rx_irq, chan);
1481     }
1482 }
1483 
1484 static void xgene_dma_set_caps(struct xgene_dma_chan *chan,
1485                    struct dma_device *dma_dev)
1486 {
1487     /* Initialize DMA device capability mask */
1488     dma_cap_zero(dma_dev->cap_mask);
1489 
1490     /* Set DMA device capability */
1491 
1492     /* Basically here, the X-Gene SoC DMA engine channel 0 supports XOR
1493      * and channel 1 supports XOR, PQ both. First thing here is we have
1494      * mechanism in hw to enable/disable PQ/XOR supports on channel 1,
1495      * we can make sure this by reading SoC Efuse register.
1496      * Second thing, we have hw errata that if we run channel 0 and
1497      * channel 1 simultaneously with executing XOR and PQ request,
1498      * suddenly DMA engine hangs, So here we enable XOR on channel 0 only
1499      * if XOR and PQ supports on channel 1 is disabled.
1500      */
1501     if ((chan->id == XGENE_DMA_PQ_CHANNEL) &&
1502         is_pq_enabled(chan->pdma)) {
1503         dma_cap_set(DMA_PQ, dma_dev->cap_mask);
1504         dma_cap_set(DMA_XOR, dma_dev->cap_mask);
1505     } else if ((chan->id == XGENE_DMA_XOR_CHANNEL) &&
1506            !is_pq_enabled(chan->pdma)) {
1507         dma_cap_set(DMA_XOR, dma_dev->cap_mask);
1508     }
1509 
1510     /* Set base and prep routines */
1511     dma_dev->dev = chan->dev;
1512     dma_dev->device_alloc_chan_resources = xgene_dma_alloc_chan_resources;
1513     dma_dev->device_free_chan_resources = xgene_dma_free_chan_resources;
1514     dma_dev->device_issue_pending = xgene_dma_issue_pending;
1515     dma_dev->device_tx_status = xgene_dma_tx_status;
1516 
1517     if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1518         dma_dev->device_prep_dma_xor = xgene_dma_prep_xor;
1519         dma_dev->max_xor = XGENE_DMA_MAX_XOR_SRC;
1520         dma_dev->xor_align = DMAENGINE_ALIGN_64_BYTES;
1521     }
1522 
1523     if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
1524         dma_dev->device_prep_dma_pq = xgene_dma_prep_pq;
1525         dma_dev->max_pq = XGENE_DMA_MAX_XOR_SRC;
1526         dma_dev->pq_align = DMAENGINE_ALIGN_64_BYTES;
1527     }
1528 }
1529 
1530 static int xgene_dma_async_register(struct xgene_dma *pdma, int id)
1531 {
1532     struct xgene_dma_chan *chan = &pdma->chan[id];
1533     struct dma_device *dma_dev = &pdma->dma_dev[id];
1534     int ret;
1535 
1536     chan->dma_chan.device = dma_dev;
1537 
1538     spin_lock_init(&chan->lock);
1539     INIT_LIST_HEAD(&chan->ld_pending);
1540     INIT_LIST_HEAD(&chan->ld_running);
1541     INIT_LIST_HEAD(&chan->ld_completed);
1542     tasklet_setup(&chan->tasklet, xgene_dma_tasklet_cb);
1543 
1544     chan->pending = 0;
1545     chan->desc_pool = NULL;
1546     dma_cookie_init(&chan->dma_chan);
1547 
1548     /* Setup dma device capabilities and prep routines */
1549     xgene_dma_set_caps(chan, dma_dev);
1550 
1551     /* Initialize DMA device list head */
1552     INIT_LIST_HEAD(&dma_dev->channels);
1553     list_add_tail(&chan->dma_chan.device_node, &dma_dev->channels);
1554 
1555     /* Register with Linux async DMA framework*/
1556     ret = dma_async_device_register(dma_dev);
1557     if (ret) {
1558         chan_err(chan, "Failed to register async device %d", ret);
1559         tasklet_kill(&chan->tasklet);
1560 
1561         return ret;
1562     }
1563 
1564     /* DMA capability info */
1565     dev_info(pdma->dev,
1566          "%s: CAPABILITY ( %s%s)\n", dma_chan_name(&chan->dma_chan),
1567          dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "XOR " : "",
1568          dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "PQ " : "");
1569 
1570     return 0;
1571 }
1572 
1573 static int xgene_dma_init_async(struct xgene_dma *pdma)
1574 {
1575     int ret, i, j;
1576 
1577     for (i = 0; i < XGENE_DMA_MAX_CHANNEL ; i++) {
1578         ret = xgene_dma_async_register(pdma, i);
1579         if (ret) {
1580             for (j = 0; j < i; j++) {
1581                 dma_async_device_unregister(&pdma->dma_dev[j]);
1582                 tasklet_kill(&pdma->chan[j].tasklet);
1583             }
1584 
1585             return ret;
1586         }
1587     }
1588 
1589     return ret;
1590 }
1591 
1592 static void xgene_dma_async_unregister(struct xgene_dma *pdma)
1593 {
1594     int i;
1595 
1596     for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
1597         dma_async_device_unregister(&pdma->dma_dev[i]);
1598 }
1599 
1600 static void xgene_dma_init_channels(struct xgene_dma *pdma)
1601 {
1602     struct xgene_dma_chan *chan;
1603     int i;
1604 
1605     pdma->ring_num = XGENE_DMA_RING_NUM;
1606 
1607     for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1608         chan = &pdma->chan[i];
1609         chan->dev = pdma->dev;
1610         chan->pdma = pdma;
1611         chan->id = i;
1612         snprintf(chan->name, sizeof(chan->name), "dmachan%d", chan->id);
1613     }
1614 }
1615 
1616 static int xgene_dma_get_resources(struct platform_device *pdev,
1617                    struct xgene_dma *pdma)
1618 {
1619     struct resource *res;
1620     int irq, i;
1621 
1622     /* Get DMA csr region */
1623     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1624     if (!res) {
1625         dev_err(&pdev->dev, "Failed to get csr region\n");
1626         return -ENXIO;
1627     }
1628 
1629     pdma->csr_dma = devm_ioremap(&pdev->dev, res->start,
1630                      resource_size(res));
1631     if (!pdma->csr_dma) {
1632         dev_err(&pdev->dev, "Failed to ioremap csr region");
1633         return -ENOMEM;
1634     }
1635 
1636     /* Get DMA ring csr region */
1637     res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1638     if (!res) {
1639         dev_err(&pdev->dev, "Failed to get ring csr region\n");
1640         return -ENXIO;
1641     }
1642 
1643     pdma->csr_ring =  devm_ioremap(&pdev->dev, res->start,
1644                        resource_size(res));
1645     if (!pdma->csr_ring) {
1646         dev_err(&pdev->dev, "Failed to ioremap ring csr region");
1647         return -ENOMEM;
1648     }
1649 
1650     /* Get DMA ring cmd csr region */
1651     res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1652     if (!res) {
1653         dev_err(&pdev->dev, "Failed to get ring cmd csr region\n");
1654         return -ENXIO;
1655     }
1656 
1657     pdma->csr_ring_cmd = devm_ioremap(&pdev->dev, res->start,
1658                       resource_size(res));
1659     if (!pdma->csr_ring_cmd) {
1660         dev_err(&pdev->dev, "Failed to ioremap ring cmd csr region");
1661         return -ENOMEM;
1662     }
1663 
1664     pdma->csr_ring_cmd += XGENE_DMA_RING_CMD_SM_OFFSET;
1665 
1666     /* Get efuse csr region */
1667     res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1668     if (!res) {
1669         dev_err(&pdev->dev, "Failed to get efuse csr region\n");
1670         return -ENXIO;
1671     }
1672 
1673     pdma->csr_efuse = devm_ioremap(&pdev->dev, res->start,
1674                        resource_size(res));
1675     if (!pdma->csr_efuse) {
1676         dev_err(&pdev->dev, "Failed to ioremap efuse csr region");
1677         return -ENOMEM;
1678     }
1679 
1680     /* Get DMA error interrupt */
1681     irq = platform_get_irq(pdev, 0);
1682     if (irq <= 0)
1683         return -ENXIO;
1684 
1685     pdma->err_irq = irq;
1686 
1687     /* Get DMA Rx ring descriptor interrupts for all DMA channels */
1688     for (i = 1; i <= XGENE_DMA_MAX_CHANNEL; i++) {
1689         irq = platform_get_irq(pdev, i);
1690         if (irq <= 0)
1691             return -ENXIO;
1692 
1693         pdma->chan[i - 1].rx_irq = irq;
1694     }
1695 
1696     return 0;
1697 }
1698 
1699 static int xgene_dma_probe(struct platform_device *pdev)
1700 {
1701     struct xgene_dma *pdma;
1702     int ret, i;
1703 
1704     pdma = devm_kzalloc(&pdev->dev, sizeof(*pdma), GFP_KERNEL);
1705     if (!pdma)
1706         return -ENOMEM;
1707 
1708     pdma->dev = &pdev->dev;
1709     platform_set_drvdata(pdev, pdma);
1710 
1711     ret = xgene_dma_get_resources(pdev, pdma);
1712     if (ret)
1713         return ret;
1714 
1715     pdma->clk = devm_clk_get(&pdev->dev, NULL);
1716     if (IS_ERR(pdma->clk) && !ACPI_COMPANION(&pdev->dev)) {
1717         dev_err(&pdev->dev, "Failed to get clk\n");
1718         return PTR_ERR(pdma->clk);
1719     }
1720 
1721     /* Enable clk before accessing registers */
1722     if (!IS_ERR(pdma->clk)) {
1723         ret = clk_prepare_enable(pdma->clk);
1724         if (ret) {
1725             dev_err(&pdev->dev, "Failed to enable clk %d\n", ret);
1726             return ret;
1727         }
1728     }
1729 
1730     /* Remove DMA RAM out of shutdown */
1731     ret = xgene_dma_init_mem(pdma);
1732     if (ret)
1733         goto err_clk_enable;
1734 
1735     ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(42));
1736     if (ret) {
1737         dev_err(&pdev->dev, "No usable DMA configuration\n");
1738         goto err_dma_mask;
1739     }
1740 
1741     /* Initialize DMA channels software state */
1742     xgene_dma_init_channels(pdma);
1743 
1744     /* Configue DMA rings */
1745     ret = xgene_dma_init_rings(pdma);
1746     if (ret)
1747         goto err_clk_enable;
1748 
1749     ret = xgene_dma_request_irqs(pdma);
1750     if (ret)
1751         goto err_request_irq;
1752 
1753     /* Configure and enable DMA engine */
1754     xgene_dma_init_hw(pdma);
1755 
1756     /* Register DMA device with linux async framework */
1757     ret = xgene_dma_init_async(pdma);
1758     if (ret)
1759         goto err_async_init;
1760 
1761     return 0;
1762 
1763 err_async_init:
1764     xgene_dma_free_irqs(pdma);
1765 
1766 err_request_irq:
1767     for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
1768         xgene_dma_delete_chan_rings(&pdma->chan[i]);
1769 
1770 err_dma_mask:
1771 err_clk_enable:
1772     if (!IS_ERR(pdma->clk))
1773         clk_disable_unprepare(pdma->clk);
1774 
1775     return ret;
1776 }
1777 
1778 static int xgene_dma_remove(struct platform_device *pdev)
1779 {
1780     struct xgene_dma *pdma = platform_get_drvdata(pdev);
1781     struct xgene_dma_chan *chan;
1782     int i;
1783 
1784     xgene_dma_async_unregister(pdma);
1785 
1786     /* Mask interrupts and disable DMA engine */
1787     xgene_dma_mask_interrupts(pdma);
1788     xgene_dma_disable(pdma);
1789     xgene_dma_free_irqs(pdma);
1790 
1791     for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1792         chan = &pdma->chan[i];
1793         tasklet_kill(&chan->tasklet);
1794         xgene_dma_delete_chan_rings(chan);
1795     }
1796 
1797     if (!IS_ERR(pdma->clk))
1798         clk_disable_unprepare(pdma->clk);
1799 
1800     return 0;
1801 }
1802 
1803 #ifdef CONFIG_ACPI
1804 static const struct acpi_device_id xgene_dma_acpi_match_ptr[] = {
1805     {"APMC0D43", 0},
1806     {},
1807 };
1808 MODULE_DEVICE_TABLE(acpi, xgene_dma_acpi_match_ptr);
1809 #endif
1810 
1811 static const struct of_device_id xgene_dma_of_match_ptr[] = {
1812     {.compatible = "apm,xgene-storm-dma",},
1813     {},
1814 };
1815 MODULE_DEVICE_TABLE(of, xgene_dma_of_match_ptr);
1816 
1817 static struct platform_driver xgene_dma_driver = {
1818     .probe = xgene_dma_probe,
1819     .remove = xgene_dma_remove,
1820     .driver = {
1821         .name = "X-Gene-DMA",
1822         .of_match_table = xgene_dma_of_match_ptr,
1823         .acpi_match_table = ACPI_PTR(xgene_dma_acpi_match_ptr),
1824     },
1825 };
1826 
1827 module_platform_driver(xgene_dma_driver);
1828 
1829 MODULE_DESCRIPTION("APM X-Gene SoC DMA driver");
1830 MODULE_AUTHOR("Rameshwar Prasad Sahu <rsahu@apm.com>");
1831 MODULE_AUTHOR("Loc Ho <lho@apm.com>");
1832 MODULE_LICENSE("GPL");
1833 MODULE_VERSION("1.0");