1ff7b0479SSaeed Bishara /* 2ff7b0479SSaeed Bishara * offload engine driver for the Marvell XOR engine 3ff7b0479SSaeed Bishara * Copyright (C) 2007, 2008, Marvell International Ltd. 4ff7b0479SSaeed Bishara * 5ff7b0479SSaeed Bishara * This program is free software; you can redistribute it and/or modify it 6ff7b0479SSaeed Bishara * under the terms and conditions of the GNU General Public License, 7ff7b0479SSaeed Bishara * version 2, as published by the Free Software Foundation. 8ff7b0479SSaeed Bishara * 9ff7b0479SSaeed Bishara * This program is distributed in the hope it will be useful, but WITHOUT 10ff7b0479SSaeed Bishara * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11ff7b0479SSaeed Bishara * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12ff7b0479SSaeed Bishara * more details. 13ff7b0479SSaeed Bishara * 14ff7b0479SSaeed Bishara * You should have received a copy of the GNU General Public License along with 15ff7b0479SSaeed Bishara * this program; if not, write to the Free Software Foundation, Inc., 16ff7b0479SSaeed Bishara * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 17ff7b0479SSaeed Bishara */ 18ff7b0479SSaeed Bishara 19ff7b0479SSaeed Bishara #include <linux/init.h> 20ff7b0479SSaeed Bishara #include <linux/module.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 22ff7b0479SSaeed Bishara #include <linux/delay.h> 23ff7b0479SSaeed Bishara #include <linux/dma-mapping.h> 24ff7b0479SSaeed Bishara #include <linux/spinlock.h> 25ff7b0479SSaeed Bishara #include <linux/interrupt.h> 26ff7b0479SSaeed Bishara #include <linux/platform_device.h> 27ff7b0479SSaeed Bishara #include <linux/memory.h> 28c510182bSAndrew Lunn #include <linux/clk.h> 29f7d12ef5SThomas Petazzoni #include <linux/of.h> 30f7d12ef5SThomas Petazzoni #include <linux/of_irq.h> 31f7d12ef5SThomas Petazzoni #include <linux/irqdomain.h> 32c02cecb9SArnd Bergmann #include <linux/platform_data/dma-mv_xor.h> 33d2ebfb33SRussell King - ARM Linux 34d2ebfb33SRussell King - ARM Linux #include "dmaengine.h" 35ff7b0479SSaeed Bishara #include "mv_xor.h" 36ff7b0479SSaeed Bishara 37ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan); 38ff7b0479SSaeed Bishara 39ff7b0479SSaeed Bishara #define to_mv_xor_chan(chan) \ 4098817b99SThomas Petazzoni container_of(chan, struct mv_xor_chan, dmachan) 41ff7b0479SSaeed Bishara 42ff7b0479SSaeed Bishara #define to_mv_xor_slot(tx) \ 43ff7b0479SSaeed Bishara container_of(tx, struct mv_xor_desc_slot, async_tx) 44ff7b0479SSaeed Bishara 45c98c1781SThomas Petazzoni #define mv_chan_to_devp(chan) \ 461ef48a26SThomas Petazzoni ((chan)->dmadev.dev) 47c98c1781SThomas Petazzoni 48ff7b0479SSaeed Bishara static void mv_desc_init(struct mv_xor_desc_slot *desc, unsigned long flags) 49ff7b0479SSaeed Bishara { 50ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 51ff7b0479SSaeed Bishara 52ff7b0479SSaeed Bishara hw_desc->status = (1 << 31); 53ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 54ff7b0479SSaeed Bishara hw_desc->desc_command = (1 << 31); 55ff7b0479SSaeed Bishara } 56ff7b0479SSaeed Bishara 57ff7b0479SSaeed Bishara static void mv_desc_set_byte_count(struct mv_xor_desc_slot *desc, 58ff7b0479SSaeed Bishara u32 byte_count) 59ff7b0479SSaeed Bishara { 60ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 61ff7b0479SSaeed Bishara hw_desc->byte_count = byte_count; 62ff7b0479SSaeed Bishara } 63ff7b0479SSaeed Bishara 64ff7b0479SSaeed Bishara static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc, 65ff7b0479SSaeed Bishara u32 next_desc_addr) 66ff7b0479SSaeed Bishara { 67ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 68ff7b0479SSaeed Bishara BUG_ON(hw_desc->phy_next_desc); 69ff7b0479SSaeed Bishara hw_desc->phy_next_desc = next_desc_addr; 70ff7b0479SSaeed Bishara } 71ff7b0479SSaeed Bishara 72ff7b0479SSaeed Bishara static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc) 73ff7b0479SSaeed Bishara { 74ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 75ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 76ff7b0479SSaeed Bishara } 77ff7b0479SSaeed Bishara 78ff7b0479SSaeed Bishara static void mv_desc_set_dest_addr(struct mv_xor_desc_slot *desc, 79ff7b0479SSaeed Bishara dma_addr_t addr) 80ff7b0479SSaeed Bishara { 81ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 82ff7b0479SSaeed Bishara hw_desc->phy_dest_addr = addr; 83ff7b0479SSaeed Bishara } 84ff7b0479SSaeed Bishara 85ff7b0479SSaeed Bishara static int mv_chan_memset_slot_count(size_t len) 86ff7b0479SSaeed Bishara { 87ff7b0479SSaeed Bishara return 1; 88ff7b0479SSaeed Bishara } 89ff7b0479SSaeed Bishara 90ff7b0479SSaeed Bishara #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c) 91ff7b0479SSaeed Bishara 92ff7b0479SSaeed Bishara static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc, 93ff7b0479SSaeed Bishara int index, dma_addr_t addr) 94ff7b0479SSaeed Bishara { 95ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 96e03bc654SThomas Petazzoni hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr; 97ff7b0479SSaeed Bishara if (desc->type == DMA_XOR) 98ff7b0479SSaeed Bishara hw_desc->desc_command |= (1 << index); 99ff7b0479SSaeed Bishara } 100ff7b0479SSaeed Bishara 101ff7b0479SSaeed Bishara static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan) 102ff7b0479SSaeed Bishara { 1035733c38aSThomas Petazzoni return readl_relaxed(XOR_CURR_DESC(chan)); 104ff7b0479SSaeed Bishara } 105ff7b0479SSaeed Bishara 106ff7b0479SSaeed Bishara static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan, 107ff7b0479SSaeed Bishara u32 next_desc_addr) 108ff7b0479SSaeed Bishara { 1095733c38aSThomas Petazzoni writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan)); 110ff7b0479SSaeed Bishara } 111ff7b0479SSaeed Bishara 112ff7b0479SSaeed Bishara static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan) 113ff7b0479SSaeed Bishara { 1145733c38aSThomas Petazzoni u32 val = readl_relaxed(XOR_INTR_MASK(chan)); 115ff7b0479SSaeed Bishara val |= XOR_INTR_MASK_VALUE << (chan->idx * 16); 1165733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_MASK(chan)); 117ff7b0479SSaeed Bishara } 118ff7b0479SSaeed Bishara 119ff7b0479SSaeed Bishara static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan) 120ff7b0479SSaeed Bishara { 1215733c38aSThomas Petazzoni u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan)); 122ff7b0479SSaeed Bishara intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF; 123ff7b0479SSaeed Bishara return intr_cause; 124ff7b0479SSaeed Bishara } 125ff7b0479SSaeed Bishara 126ff7b0479SSaeed Bishara static int mv_is_err_intr(u32 intr_cause) 127ff7b0479SSaeed Bishara { 128ff7b0479SSaeed Bishara if (intr_cause & ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9))) 129ff7b0479SSaeed Bishara return 1; 130ff7b0479SSaeed Bishara 131ff7b0479SSaeed Bishara return 0; 132ff7b0479SSaeed Bishara } 133ff7b0479SSaeed Bishara 134ff7b0479SSaeed Bishara static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan) 135ff7b0479SSaeed Bishara { 13686363682SSimon Guinot u32 val = ~(1 << (chan->idx * 16)); 137c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val); 1385733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_CAUSE(chan)); 139ff7b0479SSaeed Bishara } 140ff7b0479SSaeed Bishara 141ff7b0479SSaeed Bishara static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan) 142ff7b0479SSaeed Bishara { 143ff7b0479SSaeed Bishara u32 val = 0xFFFF0000 >> (chan->idx * 16); 1445733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_CAUSE(chan)); 145ff7b0479SSaeed Bishara } 146ff7b0479SSaeed Bishara 147ff7b0479SSaeed Bishara static int mv_can_chain(struct mv_xor_desc_slot *desc) 148ff7b0479SSaeed Bishara { 149ff7b0479SSaeed Bishara struct mv_xor_desc_slot *chain_old_tail = list_entry( 150ff7b0479SSaeed Bishara desc->chain_node.prev, struct mv_xor_desc_slot, chain_node); 151ff7b0479SSaeed Bishara 152ff7b0479SSaeed Bishara if (chain_old_tail->type != desc->type) 153ff7b0479SSaeed Bishara return 0; 154ff7b0479SSaeed Bishara 155ff7b0479SSaeed Bishara return 1; 156ff7b0479SSaeed Bishara } 157ff7b0479SSaeed Bishara 158ff7b0479SSaeed Bishara static void mv_set_mode(struct mv_xor_chan *chan, 159ff7b0479SSaeed Bishara enum dma_transaction_type type) 160ff7b0479SSaeed Bishara { 161ff7b0479SSaeed Bishara u32 op_mode; 1625733c38aSThomas Petazzoni u32 config = readl_relaxed(XOR_CONFIG(chan)); 163ff7b0479SSaeed Bishara 164ff7b0479SSaeed Bishara switch (type) { 165ff7b0479SSaeed Bishara case DMA_XOR: 166ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_XOR; 167ff7b0479SSaeed Bishara break; 168ff7b0479SSaeed Bishara case DMA_MEMCPY: 169ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_MEMCPY; 170ff7b0479SSaeed Bishara break; 171ff7b0479SSaeed Bishara default: 172c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 1731ba151cdSJoe Perches "error: unsupported operation %d\n", 174ff7b0479SSaeed Bishara type); 175ff7b0479SSaeed Bishara BUG(); 176ff7b0479SSaeed Bishara return; 177ff7b0479SSaeed Bishara } 178ff7b0479SSaeed Bishara 179ff7b0479SSaeed Bishara config &= ~0x7; 180ff7b0479SSaeed Bishara config |= op_mode; 181e03bc654SThomas Petazzoni 182e03bc654SThomas Petazzoni #if defined(__BIG_ENDIAN) 183e03bc654SThomas Petazzoni config |= XOR_DESCRIPTOR_SWAP; 184e03bc654SThomas Petazzoni #else 185e03bc654SThomas Petazzoni config &= ~XOR_DESCRIPTOR_SWAP; 186e03bc654SThomas Petazzoni #endif 187e03bc654SThomas Petazzoni 1885733c38aSThomas Petazzoni writel_relaxed(config, XOR_CONFIG(chan)); 189ff7b0479SSaeed Bishara chan->current_type = type; 190ff7b0479SSaeed Bishara } 191ff7b0479SSaeed Bishara 192ff7b0479SSaeed Bishara static void mv_chan_activate(struct mv_xor_chan *chan) 193ff7b0479SSaeed Bishara { 194ff7b0479SSaeed Bishara u32 activation; 195ff7b0479SSaeed Bishara 196c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), " activate chan.\n"); 1975733c38aSThomas Petazzoni activation = readl_relaxed(XOR_ACTIVATION(chan)); 198ff7b0479SSaeed Bishara activation |= 0x1; 1995733c38aSThomas Petazzoni writel_relaxed(activation, XOR_ACTIVATION(chan)); 200ff7b0479SSaeed Bishara } 201ff7b0479SSaeed Bishara 202ff7b0479SSaeed Bishara static char mv_chan_is_busy(struct mv_xor_chan *chan) 203ff7b0479SSaeed Bishara { 2045733c38aSThomas Petazzoni u32 state = readl_relaxed(XOR_ACTIVATION(chan)); 205ff7b0479SSaeed Bishara 206ff7b0479SSaeed Bishara state = (state >> 4) & 0x3; 207ff7b0479SSaeed Bishara 208ff7b0479SSaeed Bishara return (state == 1) ? 1 : 0; 209ff7b0479SSaeed Bishara } 210ff7b0479SSaeed Bishara 211ff7b0479SSaeed Bishara static int mv_chan_xor_slot_count(size_t len, int src_cnt) 212ff7b0479SSaeed Bishara { 213ff7b0479SSaeed Bishara return 1; 214ff7b0479SSaeed Bishara } 215ff7b0479SSaeed Bishara 216ff7b0479SSaeed Bishara /** 217ff7b0479SSaeed Bishara * mv_xor_free_slots - flags descriptor slots for reuse 218ff7b0479SSaeed Bishara * @slot: Slot to free 219ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 220ff7b0479SSaeed Bishara */ 221ff7b0479SSaeed Bishara static void mv_xor_free_slots(struct mv_xor_chan *mv_chan, 222ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot) 223ff7b0479SSaeed Bishara { 224c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n", 225ff7b0479SSaeed Bishara __func__, __LINE__, slot); 226ff7b0479SSaeed Bishara 227ff7b0479SSaeed Bishara slot->slots_per_op = 0; 228ff7b0479SSaeed Bishara 229ff7b0479SSaeed Bishara } 230ff7b0479SSaeed Bishara 231ff7b0479SSaeed Bishara /* 232ff7b0479SSaeed Bishara * mv_xor_start_new_chain - program the engine to operate on new chain headed by 233ff7b0479SSaeed Bishara * sw_desc 234ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 235ff7b0479SSaeed Bishara */ 236ff7b0479SSaeed Bishara static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan, 237ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc) 238ff7b0479SSaeed Bishara { 239c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n", 240ff7b0479SSaeed Bishara __func__, __LINE__, sw_desc); 241ff7b0479SSaeed Bishara if (sw_desc->type != mv_chan->current_type) 242ff7b0479SSaeed Bishara mv_set_mode(mv_chan, sw_desc->type); 243ff7b0479SSaeed Bishara 244ff7b0479SSaeed Bishara /* set the hardware chain */ 245ff7b0479SSaeed Bishara mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys); 24648a9db46SBartlomiej Zolnierkiewicz 247ff7b0479SSaeed Bishara mv_chan->pending += sw_desc->slot_cnt; 24898817b99SThomas Petazzoni mv_xor_issue_pending(&mv_chan->dmachan); 249ff7b0479SSaeed Bishara } 250ff7b0479SSaeed Bishara 251ff7b0479SSaeed Bishara static dma_cookie_t 252ff7b0479SSaeed Bishara mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc, 253ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan, dma_cookie_t cookie) 254ff7b0479SSaeed Bishara { 255ff7b0479SSaeed Bishara BUG_ON(desc->async_tx.cookie < 0); 256ff7b0479SSaeed Bishara 257ff7b0479SSaeed Bishara if (desc->async_tx.cookie > 0) { 258ff7b0479SSaeed Bishara cookie = desc->async_tx.cookie; 259ff7b0479SSaeed Bishara 260ff7b0479SSaeed Bishara /* call the callback (must not sleep or submit new 261ff7b0479SSaeed Bishara * operations to this channel) 262ff7b0479SSaeed Bishara */ 263ff7b0479SSaeed Bishara if (desc->async_tx.callback) 264ff7b0479SSaeed Bishara desc->async_tx.callback( 265ff7b0479SSaeed Bishara desc->async_tx.callback_param); 266ff7b0479SSaeed Bishara 267d38a8c62SDan Williams dma_descriptor_unmap(&desc->async_tx); 26854f8d501SBartlomiej Zolnierkiewicz if (desc->group_head) 269ff7b0479SSaeed Bishara desc->group_head = NULL; 270ff7b0479SSaeed Bishara } 271ff7b0479SSaeed Bishara 272ff7b0479SSaeed Bishara /* run dependent operations */ 27307f2211eSDan Williams dma_run_dependencies(&desc->async_tx); 274ff7b0479SSaeed Bishara 275ff7b0479SSaeed Bishara return cookie; 276ff7b0479SSaeed Bishara } 277ff7b0479SSaeed Bishara 278ff7b0479SSaeed Bishara static int 279ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan) 280ff7b0479SSaeed Bishara { 281ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 282ff7b0479SSaeed Bishara 283c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 284ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 285ff7b0479SSaeed Bishara completed_node) { 286ff7b0479SSaeed Bishara 287ff7b0479SSaeed Bishara if (async_tx_test_ack(&iter->async_tx)) { 288ff7b0479SSaeed Bishara list_del(&iter->completed_node); 289ff7b0479SSaeed Bishara mv_xor_free_slots(mv_chan, iter); 290ff7b0479SSaeed Bishara } 291ff7b0479SSaeed Bishara } 292ff7b0479SSaeed Bishara return 0; 293ff7b0479SSaeed Bishara } 294ff7b0479SSaeed Bishara 295ff7b0479SSaeed Bishara static int 296ff7b0479SSaeed Bishara mv_xor_clean_slot(struct mv_xor_desc_slot *desc, 297ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan) 298ff7b0479SSaeed Bishara { 299c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n", 300ff7b0479SSaeed Bishara __func__, __LINE__, desc, desc->async_tx.flags); 301ff7b0479SSaeed Bishara list_del(&desc->chain_node); 302ff7b0479SSaeed Bishara /* the client is allowed to attach dependent operations 303ff7b0479SSaeed Bishara * until 'ack' is set 304ff7b0479SSaeed Bishara */ 305ff7b0479SSaeed Bishara if (!async_tx_test_ack(&desc->async_tx)) { 306ff7b0479SSaeed Bishara /* move this slot to the completed_slots */ 307ff7b0479SSaeed Bishara list_add_tail(&desc->completed_node, &mv_chan->completed_slots); 308ff7b0479SSaeed Bishara return 0; 309ff7b0479SSaeed Bishara } 310ff7b0479SSaeed Bishara 311ff7b0479SSaeed Bishara mv_xor_free_slots(mv_chan, desc); 312ff7b0479SSaeed Bishara return 0; 313ff7b0479SSaeed Bishara } 314ff7b0479SSaeed Bishara 315ff7b0479SSaeed Bishara static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) 316ff7b0479SSaeed Bishara { 317ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 318ff7b0479SSaeed Bishara dma_cookie_t cookie = 0; 319ff7b0479SSaeed Bishara int busy = mv_chan_is_busy(mv_chan); 320ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 321ff7b0479SSaeed Bishara int seen_current = 0; 322ff7b0479SSaeed Bishara 323c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 324c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc); 325ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(mv_chan); 326ff7b0479SSaeed Bishara 327ff7b0479SSaeed Bishara /* free completed slots from the chain starting with 328ff7b0479SSaeed Bishara * the oldest descriptor 329ff7b0479SSaeed Bishara */ 330ff7b0479SSaeed Bishara 331ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 332ff7b0479SSaeed Bishara chain_node) { 333ff7b0479SSaeed Bishara prefetch(_iter); 334ff7b0479SSaeed Bishara prefetch(&_iter->async_tx); 335ff7b0479SSaeed Bishara 336ff7b0479SSaeed Bishara /* do not advance past the current descriptor loaded into the 337ff7b0479SSaeed Bishara * hardware channel, subsequent descriptors are either in 338ff7b0479SSaeed Bishara * process or have not been submitted 339ff7b0479SSaeed Bishara */ 340ff7b0479SSaeed Bishara if (seen_current) 341ff7b0479SSaeed Bishara break; 342ff7b0479SSaeed Bishara 343ff7b0479SSaeed Bishara /* stop the search if we reach the current descriptor and the 344ff7b0479SSaeed Bishara * channel is busy 345ff7b0479SSaeed Bishara */ 346ff7b0479SSaeed Bishara if (iter->async_tx.phys == current_desc) { 347ff7b0479SSaeed Bishara seen_current = 1; 348ff7b0479SSaeed Bishara if (busy) 349ff7b0479SSaeed Bishara break; 350ff7b0479SSaeed Bishara } 351ff7b0479SSaeed Bishara 352ff7b0479SSaeed Bishara cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie); 353ff7b0479SSaeed Bishara 354ff7b0479SSaeed Bishara if (mv_xor_clean_slot(iter, mv_chan)) 355ff7b0479SSaeed Bishara break; 356ff7b0479SSaeed Bishara } 357ff7b0479SSaeed Bishara 358ff7b0479SSaeed Bishara if ((busy == 0) && !list_empty(&mv_chan->chain)) { 359ff7b0479SSaeed Bishara struct mv_xor_desc_slot *chain_head; 360ff7b0479SSaeed Bishara chain_head = list_entry(mv_chan->chain.next, 361ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 362ff7b0479SSaeed Bishara chain_node); 363ff7b0479SSaeed Bishara 364ff7b0479SSaeed Bishara mv_xor_start_new_chain(mv_chan, chain_head); 365ff7b0479SSaeed Bishara } 366ff7b0479SSaeed Bishara 367ff7b0479SSaeed Bishara if (cookie > 0) 36898817b99SThomas Petazzoni mv_chan->dmachan.completed_cookie = cookie; 369ff7b0479SSaeed Bishara } 370ff7b0479SSaeed Bishara 371ff7b0479SSaeed Bishara static void 372ff7b0479SSaeed Bishara mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) 373ff7b0479SSaeed Bishara { 374ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 375ff7b0479SSaeed Bishara __mv_xor_slot_cleanup(mv_chan); 376ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 377ff7b0479SSaeed Bishara } 378ff7b0479SSaeed Bishara 379ff7b0479SSaeed Bishara static void mv_xor_tasklet(unsigned long data) 380ff7b0479SSaeed Bishara { 381ff7b0479SSaeed Bishara struct mv_xor_chan *chan = (struct mv_xor_chan *) data; 3828333f65eSSaeed Bishara mv_xor_slot_cleanup(chan); 383ff7b0479SSaeed Bishara } 384ff7b0479SSaeed Bishara 385ff7b0479SSaeed Bishara static struct mv_xor_desc_slot * 386ff7b0479SSaeed Bishara mv_xor_alloc_slots(struct mv_xor_chan *mv_chan, int num_slots, 387ff7b0479SSaeed Bishara int slots_per_op) 388ff7b0479SSaeed Bishara { 389ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter, *alloc_start = NULL; 390ff7b0479SSaeed Bishara LIST_HEAD(chain); 391ff7b0479SSaeed Bishara int slots_found, retry = 0; 392ff7b0479SSaeed Bishara 393ff7b0479SSaeed Bishara /* start search from the last allocated descrtiptor 394ff7b0479SSaeed Bishara * if a contiguous allocation can not be found start searching 395ff7b0479SSaeed Bishara * from the beginning of the list 396ff7b0479SSaeed Bishara */ 397ff7b0479SSaeed Bishara retry: 398ff7b0479SSaeed Bishara slots_found = 0; 399ff7b0479SSaeed Bishara if (retry == 0) 400ff7b0479SSaeed Bishara iter = mv_chan->last_used; 401ff7b0479SSaeed Bishara else 402ff7b0479SSaeed Bishara iter = list_entry(&mv_chan->all_slots, 403ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 404ff7b0479SSaeed Bishara slot_node); 405ff7b0479SSaeed Bishara 406ff7b0479SSaeed Bishara list_for_each_entry_safe_continue( 407ff7b0479SSaeed Bishara iter, _iter, &mv_chan->all_slots, slot_node) { 408ff7b0479SSaeed Bishara prefetch(_iter); 409ff7b0479SSaeed Bishara prefetch(&_iter->async_tx); 410ff7b0479SSaeed Bishara if (iter->slots_per_op) { 411ff7b0479SSaeed Bishara /* give up after finding the first busy slot 412ff7b0479SSaeed Bishara * on the second pass through the list 413ff7b0479SSaeed Bishara */ 414ff7b0479SSaeed Bishara if (retry) 415ff7b0479SSaeed Bishara break; 416ff7b0479SSaeed Bishara 417ff7b0479SSaeed Bishara slots_found = 0; 418ff7b0479SSaeed Bishara continue; 419ff7b0479SSaeed Bishara } 420ff7b0479SSaeed Bishara 421ff7b0479SSaeed Bishara /* start the allocation if the slot is correctly aligned */ 422ff7b0479SSaeed Bishara if (!slots_found++) 423ff7b0479SSaeed Bishara alloc_start = iter; 424ff7b0479SSaeed Bishara 425ff7b0479SSaeed Bishara if (slots_found == num_slots) { 426ff7b0479SSaeed Bishara struct mv_xor_desc_slot *alloc_tail = NULL; 427ff7b0479SSaeed Bishara struct mv_xor_desc_slot *last_used = NULL; 428ff7b0479SSaeed Bishara iter = alloc_start; 429ff7b0479SSaeed Bishara while (num_slots) { 430ff7b0479SSaeed Bishara int i; 431ff7b0479SSaeed Bishara 432ff7b0479SSaeed Bishara /* pre-ack all but the last descriptor */ 433ff7b0479SSaeed Bishara async_tx_ack(&iter->async_tx); 434ff7b0479SSaeed Bishara 435ff7b0479SSaeed Bishara list_add_tail(&iter->chain_node, &chain); 436ff7b0479SSaeed Bishara alloc_tail = iter; 437ff7b0479SSaeed Bishara iter->async_tx.cookie = 0; 438ff7b0479SSaeed Bishara iter->slot_cnt = num_slots; 439ff7b0479SSaeed Bishara iter->xor_check_result = NULL; 440ff7b0479SSaeed Bishara for (i = 0; i < slots_per_op; i++) { 441ff7b0479SSaeed Bishara iter->slots_per_op = slots_per_op - i; 442ff7b0479SSaeed Bishara last_used = iter; 443ff7b0479SSaeed Bishara iter = list_entry(iter->slot_node.next, 444ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 445ff7b0479SSaeed Bishara slot_node); 446ff7b0479SSaeed Bishara } 447ff7b0479SSaeed Bishara num_slots -= slots_per_op; 448ff7b0479SSaeed Bishara } 449ff7b0479SSaeed Bishara alloc_tail->group_head = alloc_start; 450ff7b0479SSaeed Bishara alloc_tail->async_tx.cookie = -EBUSY; 45164203b67SDan Williams list_splice(&chain, &alloc_tail->tx_list); 452ff7b0479SSaeed Bishara mv_chan->last_used = last_used; 453ff7b0479SSaeed Bishara mv_desc_clear_next_desc(alloc_start); 454ff7b0479SSaeed Bishara mv_desc_clear_next_desc(alloc_tail); 455ff7b0479SSaeed Bishara return alloc_tail; 456ff7b0479SSaeed Bishara } 457ff7b0479SSaeed Bishara } 458ff7b0479SSaeed Bishara if (!retry++) 459ff7b0479SSaeed Bishara goto retry; 460ff7b0479SSaeed Bishara 461ff7b0479SSaeed Bishara /* try to free some slots if the allocation fails */ 462ff7b0479SSaeed Bishara tasklet_schedule(&mv_chan->irq_tasklet); 463ff7b0479SSaeed Bishara 464ff7b0479SSaeed Bishara return NULL; 465ff7b0479SSaeed Bishara } 466ff7b0479SSaeed Bishara 467ff7b0479SSaeed Bishara /************************ DMA engine API functions ****************************/ 468ff7b0479SSaeed Bishara static dma_cookie_t 469ff7b0479SSaeed Bishara mv_xor_tx_submit(struct dma_async_tx_descriptor *tx) 470ff7b0479SSaeed Bishara { 471ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx); 472ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan); 473ff7b0479SSaeed Bishara struct mv_xor_desc_slot *grp_start, *old_chain_tail; 474ff7b0479SSaeed Bishara dma_cookie_t cookie; 475ff7b0479SSaeed Bishara int new_hw_chain = 1; 476ff7b0479SSaeed Bishara 477c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 478ff7b0479SSaeed Bishara "%s sw_desc %p: async_tx %p\n", 479ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 480ff7b0479SSaeed Bishara 481ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 482ff7b0479SSaeed Bishara 483ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 484884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 485ff7b0479SSaeed Bishara 486ff7b0479SSaeed Bishara if (list_empty(&mv_chan->chain)) 48764203b67SDan Williams list_splice_init(&sw_desc->tx_list, &mv_chan->chain); 488ff7b0479SSaeed Bishara else { 489ff7b0479SSaeed Bishara new_hw_chain = 0; 490ff7b0479SSaeed Bishara 491ff7b0479SSaeed Bishara old_chain_tail = list_entry(mv_chan->chain.prev, 492ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 493ff7b0479SSaeed Bishara chain_node); 49464203b67SDan Williams list_splice_init(&grp_start->tx_list, 495ff7b0479SSaeed Bishara &old_chain_tail->chain_node); 496ff7b0479SSaeed Bishara 497ff7b0479SSaeed Bishara if (!mv_can_chain(grp_start)) 498ff7b0479SSaeed Bishara goto submit_done; 499ff7b0479SSaeed Bishara 500c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %x\n", 501ff7b0479SSaeed Bishara old_chain_tail->async_tx.phys); 502ff7b0479SSaeed Bishara 503ff7b0479SSaeed Bishara /* fix up the hardware chain */ 504ff7b0479SSaeed Bishara mv_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys); 505ff7b0479SSaeed Bishara 506ff7b0479SSaeed Bishara /* if the channel is not busy */ 507ff7b0479SSaeed Bishara if (!mv_chan_is_busy(mv_chan)) { 508ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 509ff7b0479SSaeed Bishara /* 510ff7b0479SSaeed Bishara * and the curren desc is the end of the chain before 511ff7b0479SSaeed Bishara * the append, then we need to start the channel 512ff7b0479SSaeed Bishara */ 513ff7b0479SSaeed Bishara if (current_desc == old_chain_tail->async_tx.phys) 514ff7b0479SSaeed Bishara new_hw_chain = 1; 515ff7b0479SSaeed Bishara } 516ff7b0479SSaeed Bishara } 517ff7b0479SSaeed Bishara 518ff7b0479SSaeed Bishara if (new_hw_chain) 519ff7b0479SSaeed Bishara mv_xor_start_new_chain(mv_chan, grp_start); 520ff7b0479SSaeed Bishara 521ff7b0479SSaeed Bishara submit_done: 522ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 523ff7b0479SSaeed Bishara 524ff7b0479SSaeed Bishara return cookie; 525ff7b0479SSaeed Bishara } 526ff7b0479SSaeed Bishara 527ff7b0479SSaeed Bishara /* returns the number of allocated descriptors */ 528aa1e6f1aSDan Williams static int mv_xor_alloc_chan_resources(struct dma_chan *chan) 529ff7b0479SSaeed Bishara { 530ff7b0479SSaeed Bishara char *hw_desc; 531ff7b0479SSaeed Bishara int idx; 532ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 533ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot = NULL; 534b503fa01SThomas Petazzoni int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE; 535ff7b0479SSaeed Bishara 536ff7b0479SSaeed Bishara /* Allocate descriptor slots */ 537ff7b0479SSaeed Bishara idx = mv_chan->slots_allocated; 538ff7b0479SSaeed Bishara while (idx < num_descs_in_pool) { 539ff7b0479SSaeed Bishara slot = kzalloc(sizeof(*slot), GFP_KERNEL); 540ff7b0479SSaeed Bishara if (!slot) { 541ff7b0479SSaeed Bishara printk(KERN_INFO "MV XOR Channel only initialized" 542ff7b0479SSaeed Bishara " %d descriptor slots", idx); 543ff7b0479SSaeed Bishara break; 544ff7b0479SSaeed Bishara } 5451ef48a26SThomas Petazzoni hw_desc = (char *) mv_chan->dma_desc_pool_virt; 546ff7b0479SSaeed Bishara slot->hw_desc = (void *) &hw_desc[idx * MV_XOR_SLOT_SIZE]; 547ff7b0479SSaeed Bishara 548ff7b0479SSaeed Bishara dma_async_tx_descriptor_init(&slot->async_tx, chan); 549ff7b0479SSaeed Bishara slot->async_tx.tx_submit = mv_xor_tx_submit; 550ff7b0479SSaeed Bishara INIT_LIST_HEAD(&slot->chain_node); 551ff7b0479SSaeed Bishara INIT_LIST_HEAD(&slot->slot_node); 55264203b67SDan Williams INIT_LIST_HEAD(&slot->tx_list); 5531ef48a26SThomas Petazzoni hw_desc = (char *) mv_chan->dma_desc_pool; 554ff7b0479SSaeed Bishara slot->async_tx.phys = 555ff7b0479SSaeed Bishara (dma_addr_t) &hw_desc[idx * MV_XOR_SLOT_SIZE]; 556ff7b0479SSaeed Bishara slot->idx = idx++; 557ff7b0479SSaeed Bishara 558ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 559ff7b0479SSaeed Bishara mv_chan->slots_allocated = idx; 560ff7b0479SSaeed Bishara list_add_tail(&slot->slot_node, &mv_chan->all_slots); 561ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 562ff7b0479SSaeed Bishara } 563ff7b0479SSaeed Bishara 564ff7b0479SSaeed Bishara if (mv_chan->slots_allocated && !mv_chan->last_used) 565ff7b0479SSaeed Bishara mv_chan->last_used = list_entry(mv_chan->all_slots.next, 566ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 567ff7b0479SSaeed Bishara slot_node); 568ff7b0479SSaeed Bishara 569c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 570ff7b0479SSaeed Bishara "allocated %d descriptor slots last_used: %p\n", 571ff7b0479SSaeed Bishara mv_chan->slots_allocated, mv_chan->last_used); 572ff7b0479SSaeed Bishara 573ff7b0479SSaeed Bishara return mv_chan->slots_allocated ? : -ENOMEM; 574ff7b0479SSaeed Bishara } 575ff7b0479SSaeed Bishara 576ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 577ff7b0479SSaeed Bishara mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 578ff7b0479SSaeed Bishara size_t len, unsigned long flags) 579ff7b0479SSaeed Bishara { 580ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 581ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc, *grp_start; 582ff7b0479SSaeed Bishara int slot_cnt; 583ff7b0479SSaeed Bishara 584c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 585ff7b0479SSaeed Bishara "%s dest: %x src %x len: %u flags: %ld\n", 586ff7b0479SSaeed Bishara __func__, dest, src, len, flags); 587ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 588ff7b0479SSaeed Bishara return NULL; 589ff7b0479SSaeed Bishara 5907912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 591ff7b0479SSaeed Bishara 592ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 593ff7b0479SSaeed Bishara slot_cnt = mv_chan_memcpy_slot_count(len); 594ff7b0479SSaeed Bishara sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1); 595ff7b0479SSaeed Bishara if (sw_desc) { 596ff7b0479SSaeed Bishara sw_desc->type = DMA_MEMCPY; 597ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 598ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 599ff7b0479SSaeed Bishara mv_desc_init(grp_start, flags); 600ff7b0479SSaeed Bishara mv_desc_set_byte_count(grp_start, len); 601ff7b0479SSaeed Bishara mv_desc_set_dest_addr(sw_desc->group_head, dest); 602ff7b0479SSaeed Bishara mv_desc_set_src_addr(grp_start, 0, src); 603ff7b0479SSaeed Bishara sw_desc->unmap_src_cnt = 1; 604ff7b0479SSaeed Bishara sw_desc->unmap_len = len; 605ff7b0479SSaeed Bishara } 606ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 607ff7b0479SSaeed Bishara 608c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 609ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p\n", 6104c143725SJingoo Han __func__, sw_desc, sw_desc ? &sw_desc->async_tx : NULL); 611ff7b0479SSaeed Bishara 612ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 613ff7b0479SSaeed Bishara } 614ff7b0479SSaeed Bishara 615ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 616ff7b0479SSaeed Bishara mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src, 617ff7b0479SSaeed Bishara unsigned int src_cnt, size_t len, unsigned long flags) 618ff7b0479SSaeed Bishara { 619ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 620ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc, *grp_start; 621ff7b0479SSaeed Bishara int slot_cnt; 622ff7b0479SSaeed Bishara 623ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 624ff7b0479SSaeed Bishara return NULL; 625ff7b0479SSaeed Bishara 6267912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 627ff7b0479SSaeed Bishara 628c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 629ff7b0479SSaeed Bishara "%s src_cnt: %d len: dest %x %u flags: %ld\n", 630ff7b0479SSaeed Bishara __func__, src_cnt, len, dest, flags); 631ff7b0479SSaeed Bishara 632ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 633ff7b0479SSaeed Bishara slot_cnt = mv_chan_xor_slot_count(len, src_cnt); 634ff7b0479SSaeed Bishara sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1); 635ff7b0479SSaeed Bishara if (sw_desc) { 636ff7b0479SSaeed Bishara sw_desc->type = DMA_XOR; 637ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 638ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 639ff7b0479SSaeed Bishara mv_desc_init(grp_start, flags); 640ff7b0479SSaeed Bishara /* the byte count field is the same as in memcpy desc*/ 641ff7b0479SSaeed Bishara mv_desc_set_byte_count(grp_start, len); 642ff7b0479SSaeed Bishara mv_desc_set_dest_addr(sw_desc->group_head, dest); 643ff7b0479SSaeed Bishara sw_desc->unmap_src_cnt = src_cnt; 644ff7b0479SSaeed Bishara sw_desc->unmap_len = len; 645ff7b0479SSaeed Bishara while (src_cnt--) 646ff7b0479SSaeed Bishara mv_desc_set_src_addr(grp_start, src_cnt, src[src_cnt]); 647ff7b0479SSaeed Bishara } 648ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 649c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 650ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p \n", 651ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 652ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 653ff7b0479SSaeed Bishara } 654ff7b0479SSaeed Bishara 655ff7b0479SSaeed Bishara static void mv_xor_free_chan_resources(struct dma_chan *chan) 656ff7b0479SSaeed Bishara { 657ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 658ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 659ff7b0479SSaeed Bishara int in_use_descs = 0; 660ff7b0479SSaeed Bishara 661ff7b0479SSaeed Bishara mv_xor_slot_cleanup(mv_chan); 662ff7b0479SSaeed Bishara 663ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 664ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 665ff7b0479SSaeed Bishara chain_node) { 666ff7b0479SSaeed Bishara in_use_descs++; 667ff7b0479SSaeed Bishara list_del(&iter->chain_node); 668ff7b0479SSaeed Bishara } 669ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 670ff7b0479SSaeed Bishara completed_node) { 671ff7b0479SSaeed Bishara in_use_descs++; 672ff7b0479SSaeed Bishara list_del(&iter->completed_node); 673ff7b0479SSaeed Bishara } 674ff7b0479SSaeed Bishara list_for_each_entry_safe_reverse( 675ff7b0479SSaeed Bishara iter, _iter, &mv_chan->all_slots, slot_node) { 676ff7b0479SSaeed Bishara list_del(&iter->slot_node); 677ff7b0479SSaeed Bishara kfree(iter); 678ff7b0479SSaeed Bishara mv_chan->slots_allocated--; 679ff7b0479SSaeed Bishara } 680ff7b0479SSaeed Bishara mv_chan->last_used = NULL; 681ff7b0479SSaeed Bishara 682c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n", 683ff7b0479SSaeed Bishara __func__, mv_chan->slots_allocated); 684ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 685ff7b0479SSaeed Bishara 686ff7b0479SSaeed Bishara if (in_use_descs) 687c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(mv_chan), 688ff7b0479SSaeed Bishara "freeing %d in use descriptors!\n", in_use_descs); 689ff7b0479SSaeed Bishara } 690ff7b0479SSaeed Bishara 691ff7b0479SSaeed Bishara /** 69207934481SLinus Walleij * mv_xor_status - poll the status of an XOR transaction 693ff7b0479SSaeed Bishara * @chan: XOR channel handle 694ff7b0479SSaeed Bishara * @cookie: XOR transaction identifier 69507934481SLinus Walleij * @txstate: XOR transactions state holder (or NULL) 696ff7b0479SSaeed Bishara */ 69707934481SLinus Walleij static enum dma_status mv_xor_status(struct dma_chan *chan, 698ff7b0479SSaeed Bishara dma_cookie_t cookie, 69907934481SLinus Walleij struct dma_tx_state *txstate) 700ff7b0479SSaeed Bishara { 701ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 702ff7b0479SSaeed Bishara enum dma_status ret; 703ff7b0479SSaeed Bishara 70496a2af41SRussell King - ARM Linux ret = dma_cookie_status(chan, cookie, txstate); 705b3efb8fcSVinod Koul if (ret == DMA_COMPLETE) { 706ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(mv_chan); 707ff7b0479SSaeed Bishara return ret; 708ff7b0479SSaeed Bishara } 709ff7b0479SSaeed Bishara mv_xor_slot_cleanup(mv_chan); 710ff7b0479SSaeed Bishara 71196a2af41SRussell King - ARM Linux return dma_cookie_status(chan, cookie, txstate); 712ff7b0479SSaeed Bishara } 713ff7b0479SSaeed Bishara 714ff7b0479SSaeed Bishara static void mv_dump_xor_regs(struct mv_xor_chan *chan) 715ff7b0479SSaeed Bishara { 716ff7b0479SSaeed Bishara u32 val; 717ff7b0479SSaeed Bishara 7185733c38aSThomas Petazzoni val = readl_relaxed(XOR_CONFIG(chan)); 7191ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val); 720ff7b0479SSaeed Bishara 7215733c38aSThomas Petazzoni val = readl_relaxed(XOR_ACTIVATION(chan)); 7221ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val); 723ff7b0479SSaeed Bishara 7245733c38aSThomas Petazzoni val = readl_relaxed(XOR_INTR_CAUSE(chan)); 7251ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val); 726ff7b0479SSaeed Bishara 7275733c38aSThomas Petazzoni val = readl_relaxed(XOR_INTR_MASK(chan)); 7281ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val); 729ff7b0479SSaeed Bishara 7305733c38aSThomas Petazzoni val = readl_relaxed(XOR_ERROR_CAUSE(chan)); 7311ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val); 732ff7b0479SSaeed Bishara 7335733c38aSThomas Petazzoni val = readl_relaxed(XOR_ERROR_ADDR(chan)); 7341ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val); 735ff7b0479SSaeed Bishara } 736ff7b0479SSaeed Bishara 737ff7b0479SSaeed Bishara static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan, 738ff7b0479SSaeed Bishara u32 intr_cause) 739ff7b0479SSaeed Bishara { 740ff7b0479SSaeed Bishara if (intr_cause & (1 << 4)) { 741c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), 742ff7b0479SSaeed Bishara "ignore this error\n"); 743ff7b0479SSaeed Bishara return; 744ff7b0479SSaeed Bishara } 745ff7b0479SSaeed Bishara 746c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 7471ba151cdSJoe Perches "error on chan %d. intr cause 0x%08x\n", 748ff7b0479SSaeed Bishara chan->idx, intr_cause); 749ff7b0479SSaeed Bishara 750ff7b0479SSaeed Bishara mv_dump_xor_regs(chan); 751ff7b0479SSaeed Bishara BUG(); 752ff7b0479SSaeed Bishara } 753ff7b0479SSaeed Bishara 754ff7b0479SSaeed Bishara static irqreturn_t mv_xor_interrupt_handler(int irq, void *data) 755ff7b0479SSaeed Bishara { 756ff7b0479SSaeed Bishara struct mv_xor_chan *chan = data; 757ff7b0479SSaeed Bishara u32 intr_cause = mv_chan_get_intr_cause(chan); 758ff7b0479SSaeed Bishara 759c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause); 760ff7b0479SSaeed Bishara 761ff7b0479SSaeed Bishara if (mv_is_err_intr(intr_cause)) 762ff7b0479SSaeed Bishara mv_xor_err_interrupt_handler(chan, intr_cause); 763ff7b0479SSaeed Bishara 764ff7b0479SSaeed Bishara tasklet_schedule(&chan->irq_tasklet); 765ff7b0479SSaeed Bishara 766ff7b0479SSaeed Bishara mv_xor_device_clear_eoc_cause(chan); 767ff7b0479SSaeed Bishara 768ff7b0479SSaeed Bishara return IRQ_HANDLED; 769ff7b0479SSaeed Bishara } 770ff7b0479SSaeed Bishara 771ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan) 772ff7b0479SSaeed Bishara { 773ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 774ff7b0479SSaeed Bishara 775ff7b0479SSaeed Bishara if (mv_chan->pending >= MV_XOR_THRESHOLD) { 776ff7b0479SSaeed Bishara mv_chan->pending = 0; 777ff7b0479SSaeed Bishara mv_chan_activate(mv_chan); 778ff7b0479SSaeed Bishara } 779ff7b0479SSaeed Bishara } 780ff7b0479SSaeed Bishara 781ff7b0479SSaeed Bishara /* 782ff7b0479SSaeed Bishara * Perform a transaction to verify the HW works. 783ff7b0479SSaeed Bishara */ 784ff7b0479SSaeed Bishara 785c2714334SLinus Torvalds static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan) 786ff7b0479SSaeed Bishara { 787ff7b0479SSaeed Bishara int i; 788ff7b0479SSaeed Bishara void *src, *dest; 789ff7b0479SSaeed Bishara dma_addr_t src_dma, dest_dma; 790ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 791ff7b0479SSaeed Bishara dma_cookie_t cookie; 792ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 793d16695a7SEzequiel Garcia struct dmaengine_unmap_data *unmap; 794ff7b0479SSaeed Bishara int err = 0; 795ff7b0479SSaeed Bishara 796d16695a7SEzequiel Garcia src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); 797ff7b0479SSaeed Bishara if (!src) 798ff7b0479SSaeed Bishara return -ENOMEM; 799ff7b0479SSaeed Bishara 800d16695a7SEzequiel Garcia dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); 801ff7b0479SSaeed Bishara if (!dest) { 802ff7b0479SSaeed Bishara kfree(src); 803ff7b0479SSaeed Bishara return -ENOMEM; 804ff7b0479SSaeed Bishara } 805ff7b0479SSaeed Bishara 806ff7b0479SSaeed Bishara /* Fill in src buffer */ 807d16695a7SEzequiel Garcia for (i = 0; i < PAGE_SIZE; i++) 808ff7b0479SSaeed Bishara ((u8 *) src)[i] = (u8)i; 809ff7b0479SSaeed Bishara 810275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 811aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 812ff7b0479SSaeed Bishara err = -ENODEV; 813ff7b0479SSaeed Bishara goto out; 814ff7b0479SSaeed Bishara } 815ff7b0479SSaeed Bishara 816d16695a7SEzequiel Garcia unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL); 817d16695a7SEzequiel Garcia if (!unmap) { 818d16695a7SEzequiel Garcia err = -ENOMEM; 819d16695a7SEzequiel Garcia goto free_resources; 820d16695a7SEzequiel Garcia } 821ff7b0479SSaeed Bishara 822d16695a7SEzequiel Garcia src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0, 823d16695a7SEzequiel Garcia PAGE_SIZE, DMA_TO_DEVICE); 824d16695a7SEzequiel Garcia unmap->to_cnt = 1; 825d16695a7SEzequiel Garcia unmap->addr[0] = src_dma; 826d16695a7SEzequiel Garcia 827d16695a7SEzequiel Garcia dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0, 828d16695a7SEzequiel Garcia PAGE_SIZE, DMA_FROM_DEVICE); 829d16695a7SEzequiel Garcia unmap->from_cnt = 1; 830d16695a7SEzequiel Garcia unmap->addr[1] = dest_dma; 831d16695a7SEzequiel Garcia 832d16695a7SEzequiel Garcia unmap->len = PAGE_SIZE; 833ff7b0479SSaeed Bishara 834ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma, 835d16695a7SEzequiel Garcia PAGE_SIZE, 0); 836ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 837ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 838ff7b0479SSaeed Bishara async_tx_ack(tx); 839ff7b0479SSaeed Bishara msleep(1); 840ff7b0479SSaeed Bishara 84107934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 842b3efb8fcSVinod Koul DMA_COMPLETE) { 843a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 844ff7b0479SSaeed Bishara "Self-test copy timed out, disabling\n"); 845ff7b0479SSaeed Bishara err = -ENODEV; 846ff7b0479SSaeed Bishara goto free_resources; 847ff7b0479SSaeed Bishara } 848ff7b0479SSaeed Bishara 849c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 850d16695a7SEzequiel Garcia PAGE_SIZE, DMA_FROM_DEVICE); 851d16695a7SEzequiel Garcia if (memcmp(src, dest, PAGE_SIZE)) { 852a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 853ff7b0479SSaeed Bishara "Self-test copy failed compare, disabling\n"); 854ff7b0479SSaeed Bishara err = -ENODEV; 855ff7b0479SSaeed Bishara goto free_resources; 856ff7b0479SSaeed Bishara } 857ff7b0479SSaeed Bishara 858ff7b0479SSaeed Bishara free_resources: 859d16695a7SEzequiel Garcia dmaengine_unmap_put(unmap); 860ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 861ff7b0479SSaeed Bishara out: 862ff7b0479SSaeed Bishara kfree(src); 863ff7b0479SSaeed Bishara kfree(dest); 864ff7b0479SSaeed Bishara return err; 865ff7b0479SSaeed Bishara } 866ff7b0479SSaeed Bishara 867ff7b0479SSaeed Bishara #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */ 868463a1f8bSBill Pemberton static int 869275cc0c8SThomas Petazzoni mv_xor_xor_self_test(struct mv_xor_chan *mv_chan) 870ff7b0479SSaeed Bishara { 871ff7b0479SSaeed Bishara int i, src_idx; 872ff7b0479SSaeed Bishara struct page *dest; 873ff7b0479SSaeed Bishara struct page *xor_srcs[MV_XOR_NUM_SRC_TEST]; 874ff7b0479SSaeed Bishara dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST]; 875ff7b0479SSaeed Bishara dma_addr_t dest_dma; 876ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 877d16695a7SEzequiel Garcia struct dmaengine_unmap_data *unmap; 878ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 879ff7b0479SSaeed Bishara dma_cookie_t cookie; 880ff7b0479SSaeed Bishara u8 cmp_byte = 0; 881ff7b0479SSaeed Bishara u32 cmp_word; 882ff7b0479SSaeed Bishara int err = 0; 883d16695a7SEzequiel Garcia int src_count = MV_XOR_NUM_SRC_TEST; 884ff7b0479SSaeed Bishara 885d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) { 886ff7b0479SSaeed Bishara xor_srcs[src_idx] = alloc_page(GFP_KERNEL); 887a09b09aeSRoel Kluin if (!xor_srcs[src_idx]) { 888a09b09aeSRoel Kluin while (src_idx--) 889ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 890ff7b0479SSaeed Bishara return -ENOMEM; 891ff7b0479SSaeed Bishara } 892ff7b0479SSaeed Bishara } 893ff7b0479SSaeed Bishara 894ff7b0479SSaeed Bishara dest = alloc_page(GFP_KERNEL); 895a09b09aeSRoel Kluin if (!dest) { 896a09b09aeSRoel Kluin while (src_idx--) 897ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 898ff7b0479SSaeed Bishara return -ENOMEM; 899ff7b0479SSaeed Bishara } 900ff7b0479SSaeed Bishara 901ff7b0479SSaeed Bishara /* Fill in src buffers */ 902d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) { 903ff7b0479SSaeed Bishara u8 *ptr = page_address(xor_srcs[src_idx]); 904ff7b0479SSaeed Bishara for (i = 0; i < PAGE_SIZE; i++) 905ff7b0479SSaeed Bishara ptr[i] = (1 << src_idx); 906ff7b0479SSaeed Bishara } 907ff7b0479SSaeed Bishara 908d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) 909ff7b0479SSaeed Bishara cmp_byte ^= (u8) (1 << src_idx); 910ff7b0479SSaeed Bishara 911ff7b0479SSaeed Bishara cmp_word = (cmp_byte << 24) | (cmp_byte << 16) | 912ff7b0479SSaeed Bishara (cmp_byte << 8) | cmp_byte; 913ff7b0479SSaeed Bishara 914ff7b0479SSaeed Bishara memset(page_address(dest), 0, PAGE_SIZE); 915ff7b0479SSaeed Bishara 916275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 917aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 918ff7b0479SSaeed Bishara err = -ENODEV; 919ff7b0479SSaeed Bishara goto out; 920ff7b0479SSaeed Bishara } 921ff7b0479SSaeed Bishara 922d16695a7SEzequiel Garcia unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1, 923d16695a7SEzequiel Garcia GFP_KERNEL); 924d16695a7SEzequiel Garcia if (!unmap) { 925d16695a7SEzequiel Garcia err = -ENOMEM; 926d16695a7SEzequiel Garcia goto free_resources; 927d16695a7SEzequiel Garcia } 928ff7b0479SSaeed Bishara 929d16695a7SEzequiel Garcia /* test xor */ 930d16695a7SEzequiel Garcia for (i = 0; i < src_count; i++) { 931d16695a7SEzequiel Garcia unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i], 932ff7b0479SSaeed Bishara 0, PAGE_SIZE, DMA_TO_DEVICE); 933d16695a7SEzequiel Garcia dma_srcs[i] = unmap->addr[i]; 934d16695a7SEzequiel Garcia unmap->to_cnt++; 935d16695a7SEzequiel Garcia } 936d16695a7SEzequiel Garcia 937d16695a7SEzequiel Garcia unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE, 938d16695a7SEzequiel Garcia DMA_FROM_DEVICE); 939d16695a7SEzequiel Garcia dest_dma = unmap->addr[src_count]; 940d16695a7SEzequiel Garcia unmap->from_cnt = 1; 941d16695a7SEzequiel Garcia unmap->len = PAGE_SIZE; 942ff7b0479SSaeed Bishara 943ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs, 944d16695a7SEzequiel Garcia src_count, PAGE_SIZE, 0); 945ff7b0479SSaeed Bishara 946ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 947ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 948ff7b0479SSaeed Bishara async_tx_ack(tx); 949ff7b0479SSaeed Bishara msleep(8); 950ff7b0479SSaeed Bishara 95107934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 952b3efb8fcSVinod Koul DMA_COMPLETE) { 953a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 954ff7b0479SSaeed Bishara "Self-test xor timed out, disabling\n"); 955ff7b0479SSaeed Bishara err = -ENODEV; 956ff7b0479SSaeed Bishara goto free_resources; 957ff7b0479SSaeed Bishara } 958ff7b0479SSaeed Bishara 959c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 960ff7b0479SSaeed Bishara PAGE_SIZE, DMA_FROM_DEVICE); 961ff7b0479SSaeed Bishara for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) { 962ff7b0479SSaeed Bishara u32 *ptr = page_address(dest); 963ff7b0479SSaeed Bishara if (ptr[i] != cmp_word) { 964a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 9651ba151cdSJoe Perches "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n", 9661ba151cdSJoe Perches i, ptr[i], cmp_word); 967ff7b0479SSaeed Bishara err = -ENODEV; 968ff7b0479SSaeed Bishara goto free_resources; 969ff7b0479SSaeed Bishara } 970ff7b0479SSaeed Bishara } 971ff7b0479SSaeed Bishara 972ff7b0479SSaeed Bishara free_resources: 973d16695a7SEzequiel Garcia dmaengine_unmap_put(unmap); 974ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 975ff7b0479SSaeed Bishara out: 976d16695a7SEzequiel Garcia src_idx = src_count; 977ff7b0479SSaeed Bishara while (src_idx--) 978ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 979ff7b0479SSaeed Bishara __free_page(dest); 980ff7b0479SSaeed Bishara return err; 981ff7b0479SSaeed Bishara } 982ff7b0479SSaeed Bishara 98334c93c86SAndrew Lunn /* This driver does not implement any of the optional DMA operations. */ 98434c93c86SAndrew Lunn static int 98534c93c86SAndrew Lunn mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, 98634c93c86SAndrew Lunn unsigned long arg) 987ff7b0479SSaeed Bishara { 98834c93c86SAndrew Lunn return -ENOSYS; 98934c93c86SAndrew Lunn } 99034c93c86SAndrew Lunn 9911ef48a26SThomas Petazzoni static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan) 992ff7b0479SSaeed Bishara { 993ff7b0479SSaeed Bishara struct dma_chan *chan, *_chan; 9941ef48a26SThomas Petazzoni struct device *dev = mv_chan->dmadev.dev; 995ff7b0479SSaeed Bishara 9961ef48a26SThomas Petazzoni dma_async_device_unregister(&mv_chan->dmadev); 997ff7b0479SSaeed Bishara 998b503fa01SThomas Petazzoni dma_free_coherent(dev, MV_XOR_POOL_SIZE, 9991ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 1000ff7b0479SSaeed Bishara 10011ef48a26SThomas Petazzoni list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels, 1002ff7b0479SSaeed Bishara device_node) { 1003ff7b0479SSaeed Bishara list_del(&chan->device_node); 1004ff7b0479SSaeed Bishara } 1005ff7b0479SSaeed Bishara 100688eb92cbSThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 100788eb92cbSThomas Petazzoni 1008ff7b0479SSaeed Bishara return 0; 1009ff7b0479SSaeed Bishara } 1010ff7b0479SSaeed Bishara 10111ef48a26SThomas Petazzoni static struct mv_xor_chan * 1012297eedbaSThomas Petazzoni mv_xor_channel_add(struct mv_xor_device *xordev, 1013a6b4a9d2SThomas Petazzoni struct platform_device *pdev, 1014b503fa01SThomas Petazzoni int idx, dma_cap_mask_t cap_mask, int irq) 1015ff7b0479SSaeed Bishara { 1016ff7b0479SSaeed Bishara int ret = 0; 1017ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan; 1018ff7b0479SSaeed Bishara struct dma_device *dma_dev; 1019ff7b0479SSaeed Bishara 10201ef48a26SThomas Petazzoni mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL); 1021a577659fSSachin Kamat if (!mv_chan) 1022a577659fSSachin Kamat return ERR_PTR(-ENOMEM); 1023ff7b0479SSaeed Bishara 10249aedbdbaSThomas Petazzoni mv_chan->idx = idx; 102588eb92cbSThomas Petazzoni mv_chan->irq = irq; 1026ff7b0479SSaeed Bishara 10271ef48a26SThomas Petazzoni dma_dev = &mv_chan->dmadev; 1028ff7b0479SSaeed Bishara 1029ff7b0479SSaeed Bishara /* allocate coherent memory for hardware descriptors 1030ff7b0479SSaeed Bishara * note: writecombine gives slightly better performance, but 1031ff7b0479SSaeed Bishara * requires that we explicitly flush the writes 1032ff7b0479SSaeed Bishara */ 10331ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt = 1034b503fa01SThomas Petazzoni dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE, 10351ef48a26SThomas Petazzoni &mv_chan->dma_desc_pool, GFP_KERNEL); 10361ef48a26SThomas Petazzoni if (!mv_chan->dma_desc_pool_virt) 1037a6b4a9d2SThomas Petazzoni return ERR_PTR(-ENOMEM); 1038ff7b0479SSaeed Bishara 1039ff7b0479SSaeed Bishara /* discover transaction capabilites from the platform data */ 1040a6b4a9d2SThomas Petazzoni dma_dev->cap_mask = cap_mask; 1041ff7b0479SSaeed Bishara 1042ff7b0479SSaeed Bishara INIT_LIST_HEAD(&dma_dev->channels); 1043ff7b0479SSaeed Bishara 1044ff7b0479SSaeed Bishara /* set base routines */ 1045ff7b0479SSaeed Bishara dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources; 1046ff7b0479SSaeed Bishara dma_dev->device_free_chan_resources = mv_xor_free_chan_resources; 104707934481SLinus Walleij dma_dev->device_tx_status = mv_xor_status; 1048ff7b0479SSaeed Bishara dma_dev->device_issue_pending = mv_xor_issue_pending; 104934c93c86SAndrew Lunn dma_dev->device_control = mv_xor_control; 1050ff7b0479SSaeed Bishara dma_dev->dev = &pdev->dev; 1051ff7b0479SSaeed Bishara 1052ff7b0479SSaeed Bishara /* set prep routines based on capability */ 1053ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) 1054ff7b0479SSaeed Bishara dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy; 1055ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 1056c019894eSJoe Perches dma_dev->max_xor = 8; 1057ff7b0479SSaeed Bishara dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor; 1058ff7b0479SSaeed Bishara } 1059ff7b0479SSaeed Bishara 1060297eedbaSThomas Petazzoni mv_chan->mmr_base = xordev->xor_base; 106182a1402eSEzequiel Garcia mv_chan->mmr_high_base = xordev->xor_high_base; 1062ff7b0479SSaeed Bishara tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long) 1063ff7b0479SSaeed Bishara mv_chan); 1064ff7b0479SSaeed Bishara 1065ff7b0479SSaeed Bishara /* clear errors before enabling interrupts */ 1066ff7b0479SSaeed Bishara mv_xor_device_clear_err_status(mv_chan); 1067ff7b0479SSaeed Bishara 10682d0a0745SThomas Petazzoni ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler, 1069ff7b0479SSaeed Bishara 0, dev_name(&pdev->dev), mv_chan); 1070ff7b0479SSaeed Bishara if (ret) 1071ff7b0479SSaeed Bishara goto err_free_dma; 1072ff7b0479SSaeed Bishara 1073ff7b0479SSaeed Bishara mv_chan_unmask_interrupts(mv_chan); 1074ff7b0479SSaeed Bishara 1075ff7b0479SSaeed Bishara mv_set_mode(mv_chan, DMA_MEMCPY); 1076ff7b0479SSaeed Bishara 1077ff7b0479SSaeed Bishara spin_lock_init(&mv_chan->lock); 1078ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->chain); 1079ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->completed_slots); 1080ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->all_slots); 108198817b99SThomas Petazzoni mv_chan->dmachan.device = dma_dev; 108298817b99SThomas Petazzoni dma_cookie_init(&mv_chan->dmachan); 1083ff7b0479SSaeed Bishara 108498817b99SThomas Petazzoni list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels); 1085ff7b0479SSaeed Bishara 1086ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { 1087275cc0c8SThomas Petazzoni ret = mv_xor_memcpy_self_test(mv_chan); 1088ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret); 1089ff7b0479SSaeed Bishara if (ret) 10902d0a0745SThomas Petazzoni goto err_free_irq; 1091ff7b0479SSaeed Bishara } 1092ff7b0479SSaeed Bishara 1093ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 1094275cc0c8SThomas Petazzoni ret = mv_xor_xor_self_test(mv_chan); 1095ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "xor self test returned %d\n", ret); 1096ff7b0479SSaeed Bishara if (ret) 10972d0a0745SThomas Petazzoni goto err_free_irq; 1098ff7b0479SSaeed Bishara } 1099ff7b0479SSaeed Bishara 110048a9db46SBartlomiej Zolnierkiewicz dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n", 1101ff7b0479SSaeed Bishara dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "", 1102ff7b0479SSaeed Bishara dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "", 1103ff7b0479SSaeed Bishara dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : ""); 1104ff7b0479SSaeed Bishara 1105ff7b0479SSaeed Bishara dma_async_device_register(dma_dev); 11061ef48a26SThomas Petazzoni return mv_chan; 1107ff7b0479SSaeed Bishara 11082d0a0745SThomas Petazzoni err_free_irq: 11092d0a0745SThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 1110ff7b0479SSaeed Bishara err_free_dma: 1111b503fa01SThomas Petazzoni dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE, 11121ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 1113a6b4a9d2SThomas Petazzoni return ERR_PTR(ret); 1114ff7b0479SSaeed Bishara } 1115ff7b0479SSaeed Bishara 1116ff7b0479SSaeed Bishara static void 1117297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(struct mv_xor_device *xordev, 111863a9332bSAndrew Lunn const struct mbus_dram_target_info *dram) 1119ff7b0479SSaeed Bishara { 112082a1402eSEzequiel Garcia void __iomem *base = xordev->xor_high_base; 1121ff7b0479SSaeed Bishara u32 win_enable = 0; 1122ff7b0479SSaeed Bishara int i; 1123ff7b0479SSaeed Bishara 1124ff7b0479SSaeed Bishara for (i = 0; i < 8; i++) { 1125ff7b0479SSaeed Bishara writel(0, base + WINDOW_BASE(i)); 1126ff7b0479SSaeed Bishara writel(0, base + WINDOW_SIZE(i)); 1127ff7b0479SSaeed Bishara if (i < 4) 1128ff7b0479SSaeed Bishara writel(0, base + WINDOW_REMAP_HIGH(i)); 1129ff7b0479SSaeed Bishara } 1130ff7b0479SSaeed Bishara 1131ff7b0479SSaeed Bishara for (i = 0; i < dram->num_cs; i++) { 113263a9332bSAndrew Lunn const struct mbus_dram_window *cs = dram->cs + i; 1133ff7b0479SSaeed Bishara 1134ff7b0479SSaeed Bishara writel((cs->base & 0xffff0000) | 1135ff7b0479SSaeed Bishara (cs->mbus_attr << 8) | 1136ff7b0479SSaeed Bishara dram->mbus_dram_target_id, base + WINDOW_BASE(i)); 1137ff7b0479SSaeed Bishara writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i)); 1138ff7b0479SSaeed Bishara 1139ff7b0479SSaeed Bishara win_enable |= (1 << i); 1140ff7b0479SSaeed Bishara win_enable |= 3 << (16 + (2 * i)); 1141ff7b0479SSaeed Bishara } 1142ff7b0479SSaeed Bishara 1143ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(0)); 1144ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(1)); 1145c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(0)); 1146c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(1)); 1147ff7b0479SSaeed Bishara } 1148ff7b0479SSaeed Bishara 1149c2714334SLinus Torvalds static int mv_xor_probe(struct platform_device *pdev) 1150ff7b0479SSaeed Bishara { 115163a9332bSAndrew Lunn const struct mbus_dram_target_info *dram; 1152297eedbaSThomas Petazzoni struct mv_xor_device *xordev; 1153d4adcc01SJingoo Han struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev); 1154ff7b0479SSaeed Bishara struct resource *res; 115560d151f3SThomas Petazzoni int i, ret; 1156ff7b0479SSaeed Bishara 11571ba151cdSJoe Perches dev_notice(&pdev->dev, "Marvell shared XOR driver\n"); 1158ff7b0479SSaeed Bishara 1159297eedbaSThomas Petazzoni xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL); 1160297eedbaSThomas Petazzoni if (!xordev) 1161ff7b0479SSaeed Bishara return -ENOMEM; 1162ff7b0479SSaeed Bishara 1163ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1164ff7b0479SSaeed Bishara if (!res) 1165ff7b0479SSaeed Bishara return -ENODEV; 1166ff7b0479SSaeed Bishara 1167297eedbaSThomas Petazzoni xordev->xor_base = devm_ioremap(&pdev->dev, res->start, 11684de1ba15SH Hartley Sweeten resource_size(res)); 1169297eedbaSThomas Petazzoni if (!xordev->xor_base) 1170ff7b0479SSaeed Bishara return -EBUSY; 1171ff7b0479SSaeed Bishara 1172ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1173ff7b0479SSaeed Bishara if (!res) 1174ff7b0479SSaeed Bishara return -ENODEV; 1175ff7b0479SSaeed Bishara 1176297eedbaSThomas Petazzoni xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start, 11774de1ba15SH Hartley Sweeten resource_size(res)); 1178297eedbaSThomas Petazzoni if (!xordev->xor_high_base) 1179ff7b0479SSaeed Bishara return -EBUSY; 1180ff7b0479SSaeed Bishara 1181297eedbaSThomas Petazzoni platform_set_drvdata(pdev, xordev); 1182ff7b0479SSaeed Bishara 1183ff7b0479SSaeed Bishara /* 1184ff7b0479SSaeed Bishara * (Re-)program MBUS remapping windows if we are asked to. 1185ff7b0479SSaeed Bishara */ 118663a9332bSAndrew Lunn dram = mv_mbus_dram_info(); 118763a9332bSAndrew Lunn if (dram) 1188297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(xordev, dram); 1189ff7b0479SSaeed Bishara 1190c510182bSAndrew Lunn /* Not all platforms can gate the clock, so it is not 1191c510182bSAndrew Lunn * an error if the clock does not exists. 1192c510182bSAndrew Lunn */ 1193297eedbaSThomas Petazzoni xordev->clk = clk_get(&pdev->dev, NULL); 1194297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) 1195297eedbaSThomas Petazzoni clk_prepare_enable(xordev->clk); 1196c510182bSAndrew Lunn 1197f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) { 1198f7d12ef5SThomas Petazzoni struct device_node *np; 1199f7d12ef5SThomas Petazzoni int i = 0; 1200f7d12ef5SThomas Petazzoni 1201f7d12ef5SThomas Petazzoni for_each_child_of_node(pdev->dev.of_node, np) { 1202*0be8253fSRussell King struct mv_xor_chan *chan; 1203f7d12ef5SThomas Petazzoni dma_cap_mask_t cap_mask; 1204f7d12ef5SThomas Petazzoni int irq; 1205f7d12ef5SThomas Petazzoni 1206f7d12ef5SThomas Petazzoni dma_cap_zero(cap_mask); 1207f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,memcpy")) 1208f7d12ef5SThomas Petazzoni dma_cap_set(DMA_MEMCPY, cap_mask); 1209f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,xor")) 1210f7d12ef5SThomas Petazzoni dma_cap_set(DMA_XOR, cap_mask); 1211f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,interrupt")) 1212f7d12ef5SThomas Petazzoni dma_cap_set(DMA_INTERRUPT, cap_mask); 1213f7d12ef5SThomas Petazzoni 1214f7d12ef5SThomas Petazzoni irq = irq_of_parse_and_map(np, 0); 1215f8eb9e7dSThomas Petazzoni if (!irq) { 1216f8eb9e7dSThomas Petazzoni ret = -ENODEV; 1217f7d12ef5SThomas Petazzoni goto err_channel_add; 1218f7d12ef5SThomas Petazzoni } 1219f7d12ef5SThomas Petazzoni 1220*0be8253fSRussell King chan = mv_xor_channel_add(xordev, pdev, i, 1221f7d12ef5SThomas Petazzoni cap_mask, irq); 1222*0be8253fSRussell King if (IS_ERR(chan)) { 1223*0be8253fSRussell King ret = PTR_ERR(chan); 1224f7d12ef5SThomas Petazzoni irq_dispose_mapping(irq); 1225f7d12ef5SThomas Petazzoni goto err_channel_add; 1226f7d12ef5SThomas Petazzoni } 1227f7d12ef5SThomas Petazzoni 1228*0be8253fSRussell King xordev->channels[i] = chan; 1229f7d12ef5SThomas Petazzoni i++; 1230f7d12ef5SThomas Petazzoni } 1231f7d12ef5SThomas Petazzoni } else if (pdata && pdata->channels) { 123260d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { 1233e39f6ec1SThomas Petazzoni struct mv_xor_channel_data *cd; 1234*0be8253fSRussell King struct mv_xor_chan *chan; 123560d151f3SThomas Petazzoni int irq; 123660d151f3SThomas Petazzoni 123760d151f3SThomas Petazzoni cd = &pdata->channels[i]; 123860d151f3SThomas Petazzoni if (!cd) { 123960d151f3SThomas Petazzoni ret = -ENODEV; 124060d151f3SThomas Petazzoni goto err_channel_add; 124160d151f3SThomas Petazzoni } 124260d151f3SThomas Petazzoni 124360d151f3SThomas Petazzoni irq = platform_get_irq(pdev, i); 124460d151f3SThomas Petazzoni if (irq < 0) { 124560d151f3SThomas Petazzoni ret = irq; 124660d151f3SThomas Petazzoni goto err_channel_add; 124760d151f3SThomas Petazzoni } 124860d151f3SThomas Petazzoni 1249*0be8253fSRussell King chan = mv_xor_channel_add(xordev, pdev, i, 1250b503fa01SThomas Petazzoni cd->cap_mask, irq); 1251*0be8253fSRussell King if (IS_ERR(chan)) { 1252*0be8253fSRussell King ret = PTR_ERR(chan); 125360d151f3SThomas Petazzoni goto err_channel_add; 125460d151f3SThomas Petazzoni } 1255*0be8253fSRussell King 1256*0be8253fSRussell King xordev->channels[i] = chan; 125760d151f3SThomas Petazzoni } 125860d151f3SThomas Petazzoni } 125960d151f3SThomas Petazzoni 1260ff7b0479SSaeed Bishara return 0; 126160d151f3SThomas Petazzoni 126260d151f3SThomas Petazzoni err_channel_add: 126360d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) 1264f7d12ef5SThomas Petazzoni if (xordev->channels[i]) { 1265ab6e439fSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 1266f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) 1267f7d12ef5SThomas Petazzoni irq_dispose_mapping(xordev->channels[i]->irq); 1268f7d12ef5SThomas Petazzoni } 126960d151f3SThomas Petazzoni 1270dab92064SThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1271297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1272297eedbaSThomas Petazzoni clk_put(xordev->clk); 1273dab92064SThomas Petazzoni } 1274dab92064SThomas Petazzoni 127560d151f3SThomas Petazzoni return ret; 1276ff7b0479SSaeed Bishara } 1277ff7b0479SSaeed Bishara 1278c2714334SLinus Torvalds static int mv_xor_remove(struct platform_device *pdev) 1279ff7b0479SSaeed Bishara { 1280297eedbaSThomas Petazzoni struct mv_xor_device *xordev = platform_get_drvdata(pdev); 128160d151f3SThomas Petazzoni int i; 128260d151f3SThomas Petazzoni 128360d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { 1284297eedbaSThomas Petazzoni if (xordev->channels[i]) 1285297eedbaSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 128660d151f3SThomas Petazzoni } 1287c510182bSAndrew Lunn 1288297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1289297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1290297eedbaSThomas Petazzoni clk_put(xordev->clk); 1291c510182bSAndrew Lunn } 1292c510182bSAndrew Lunn 1293ff7b0479SSaeed Bishara return 0; 1294ff7b0479SSaeed Bishara } 1295ff7b0479SSaeed Bishara 1296f7d12ef5SThomas Petazzoni #ifdef CONFIG_OF 1297c2714334SLinus Torvalds static struct of_device_id mv_xor_dt_ids[] = { 1298f7d12ef5SThomas Petazzoni { .compatible = "marvell,orion-xor", }, 1299f7d12ef5SThomas Petazzoni {}, 1300f7d12ef5SThomas Petazzoni }; 1301f7d12ef5SThomas Petazzoni MODULE_DEVICE_TABLE(of, mv_xor_dt_ids); 1302f7d12ef5SThomas Petazzoni #endif 1303f7d12ef5SThomas Petazzoni 1304ff7b0479SSaeed Bishara static struct platform_driver mv_xor_driver = { 1305ff7b0479SSaeed Bishara .probe = mv_xor_probe, 1306a7d6e3ecSBill Pemberton .remove = mv_xor_remove, 1307ff7b0479SSaeed Bishara .driver = { 1308ff7b0479SSaeed Bishara .owner = THIS_MODULE, 1309ff7b0479SSaeed Bishara .name = MV_XOR_NAME, 1310f7d12ef5SThomas Petazzoni .of_match_table = of_match_ptr(mv_xor_dt_ids), 1311ff7b0479SSaeed Bishara }, 1312ff7b0479SSaeed Bishara }; 1313ff7b0479SSaeed Bishara 1314ff7b0479SSaeed Bishara 1315ff7b0479SSaeed Bishara static int __init mv_xor_init(void) 1316ff7b0479SSaeed Bishara { 131761971656SThomas Petazzoni return platform_driver_register(&mv_xor_driver); 1318ff7b0479SSaeed Bishara } 1319ff7b0479SSaeed Bishara module_init(mv_xor_init); 1320ff7b0479SSaeed Bishara 1321ff7b0479SSaeed Bishara /* it's currently unsafe to unload this module */ 1322ff7b0479SSaeed Bishara #if 0 1323ff7b0479SSaeed Bishara static void __exit mv_xor_exit(void) 1324ff7b0479SSaeed Bishara { 1325ff7b0479SSaeed Bishara platform_driver_unregister(&mv_xor_driver); 1326ff7b0479SSaeed Bishara return; 1327ff7b0479SSaeed Bishara } 1328ff7b0479SSaeed Bishara 1329ff7b0479SSaeed Bishara module_exit(mv_xor_exit); 1330ff7b0479SSaeed Bishara #endif 1331ff7b0479SSaeed Bishara 1332ff7b0479SSaeed Bishara MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>"); 1333ff7b0479SSaeed Bishara MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine"); 1334ff7b0479SSaeed Bishara MODULE_LICENSE("GPL"); 1335