1 /* 2 * Renesas R-Car Gen2 DMA Controller Driver 3 * 4 * Copyright (C) 2014 Renesas Electronics Inc. 5 * 6 * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com> 7 * 8 * This is free software; you can redistribute it and/or modify 9 * it under the terms of version 2 of the GNU General Public License as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <linux/dma-mapping.h> 14 #include <linux/dmaengine.h> 15 #include <linux/interrupt.h> 16 #include <linux/list.h> 17 #include <linux/module.h> 18 #include <linux/mutex.h> 19 #include <linux/of.h> 20 #include <linux/of_dma.h> 21 #include <linux/of_platform.h> 22 #include <linux/platform_device.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/slab.h> 25 #include <linux/spinlock.h> 26 27 #include "../dmaengine.h" 28 29 /* 30 * struct rcar_dmac_xfer_chunk - Descriptor for a hardware transfer 31 * @node: entry in the parent's chunks list 32 * @src_addr: device source address 33 * @dst_addr: device destination address 34 * @size: transfer size in bytes 35 */ 36 struct rcar_dmac_xfer_chunk { 37 struct list_head node; 38 39 dma_addr_t src_addr; 40 dma_addr_t dst_addr; 41 u32 size; 42 }; 43 44 /* 45 * struct rcar_dmac_hw_desc - Hardware descriptor for a transfer chunk 46 * @sar: value of the SAR register (source address) 47 * @dar: value of the DAR register (destination address) 48 * @tcr: value of the TCR register (transfer count) 49 */ 50 struct rcar_dmac_hw_desc { 51 u32 sar; 52 u32 dar; 53 u32 tcr; 54 u32 reserved; 55 } __attribute__((__packed__)); 56 57 /* 58 * struct rcar_dmac_desc - R-Car Gen2 DMA Transfer Descriptor 59 * @async_tx: base DMA asynchronous transaction descriptor 60 * @direction: direction of the DMA transfer 61 * @xfer_shift: log2 of the transfer size 62 * @chcr: value of the channel configuration register for this transfer 63 * @node: entry in the channel's descriptors lists 64 * @chunks: list of transfer chunks for this transfer 65 * @running: the transfer chunk being currently processed 66 * @nchunks: number of transfer chunks for this transfer 67 * @hwdescs.use: whether the transfer descriptor uses hardware descriptors 68 * @hwdescs.mem: hardware descriptors memory for the transfer 69 * @hwdescs.dma: device address of the hardware descriptors memory 70 * @hwdescs.size: size of the hardware descriptors in bytes 71 * @size: transfer size in bytes 72 * @cyclic: when set indicates that the DMA transfer is cyclic 73 */ 74 struct rcar_dmac_desc { 75 struct dma_async_tx_descriptor async_tx; 76 enum dma_transfer_direction direction; 77 unsigned int xfer_shift; 78 u32 chcr; 79 80 struct list_head node; 81 struct list_head chunks; 82 struct rcar_dmac_xfer_chunk *running; 83 unsigned int nchunks; 84 85 struct { 86 bool use; 87 struct rcar_dmac_hw_desc *mem; 88 dma_addr_t dma; 89 size_t size; 90 } hwdescs; 91 92 unsigned int size; 93 bool cyclic; 94 }; 95 96 #define to_rcar_dmac_desc(d) container_of(d, struct rcar_dmac_desc, async_tx) 97 98 /* 99 * struct rcar_dmac_desc_page - One page worth of descriptors 100 * @node: entry in the channel's pages list 101 * @descs: array of DMA descriptors 102 * @chunks: array of transfer chunk descriptors 103 */ 104 struct rcar_dmac_desc_page { 105 struct list_head node; 106 107 union { 108 struct rcar_dmac_desc descs[0]; 109 struct rcar_dmac_xfer_chunk chunks[0]; 110 }; 111 }; 112 113 #define RCAR_DMAC_DESCS_PER_PAGE \ 114 ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, descs)) / \ 115 sizeof(struct rcar_dmac_desc)) 116 #define RCAR_DMAC_XFER_CHUNKS_PER_PAGE \ 117 ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, chunks)) / \ 118 sizeof(struct rcar_dmac_xfer_chunk)) 119 120 /* 121 * struct rcar_dmac_chan_slave - Slave configuration 122 * @slave_addr: slave memory address 123 * @xfer_size: size (in bytes) of hardware transfers 124 */ 125 struct rcar_dmac_chan_slave { 126 phys_addr_t slave_addr; 127 unsigned int xfer_size; 128 }; 129 130 /* 131 * struct rcar_dmac_chan_map - Map of slave device phys to dma address 132 * @addr: slave dma address 133 * @dir: direction of mapping 134 * @slave: slave configuration that is mapped 135 */ 136 struct rcar_dmac_chan_map { 137 dma_addr_t addr; 138 enum dma_data_direction dir; 139 struct rcar_dmac_chan_slave slave; 140 }; 141 142 /* 143 * struct rcar_dmac_chan - R-Car Gen2 DMA Controller Channel 144 * @chan: base DMA channel object 145 * @iomem: channel I/O memory base 146 * @index: index of this channel in the controller 147 * @src: slave memory address and size on the source side 148 * @dst: slave memory address and size on the destination side 149 * @mid_rid: hardware MID/RID for the DMA client using this channel 150 * @lock: protects the channel CHCR register and the desc members 151 * @desc.free: list of free descriptors 152 * @desc.pending: list of pending descriptors (submitted with tx_submit) 153 * @desc.active: list of active descriptors (activated with issue_pending) 154 * @desc.done: list of completed descriptors 155 * @desc.wait: list of descriptors waiting for an ack 156 * @desc.running: the descriptor being processed (a member of the active list) 157 * @desc.chunks_free: list of free transfer chunk descriptors 158 * @desc.pages: list of pages used by allocated descriptors 159 */ 160 struct rcar_dmac_chan { 161 struct dma_chan chan; 162 void __iomem *iomem; 163 unsigned int index; 164 165 struct rcar_dmac_chan_slave src; 166 struct rcar_dmac_chan_slave dst; 167 struct rcar_dmac_chan_map map; 168 int mid_rid; 169 170 spinlock_t lock; 171 172 struct { 173 struct list_head free; 174 struct list_head pending; 175 struct list_head active; 176 struct list_head done; 177 struct list_head wait; 178 struct rcar_dmac_desc *running; 179 180 struct list_head chunks_free; 181 182 struct list_head pages; 183 } desc; 184 }; 185 186 #define to_rcar_dmac_chan(c) container_of(c, struct rcar_dmac_chan, chan) 187 188 /* 189 * struct rcar_dmac - R-Car Gen2 DMA Controller 190 * @engine: base DMA engine object 191 * @dev: the hardware device 192 * @iomem: remapped I/O memory base 193 * @n_channels: number of available channels 194 * @channels: array of DMAC channels 195 * @modules: bitmask of client modules in use 196 */ 197 struct rcar_dmac { 198 struct dma_device engine; 199 struct device *dev; 200 void __iomem *iomem; 201 202 unsigned int n_channels; 203 struct rcar_dmac_chan *channels; 204 205 DECLARE_BITMAP(modules, 256); 206 }; 207 208 #define to_rcar_dmac(d) container_of(d, struct rcar_dmac, engine) 209 210 /* ----------------------------------------------------------------------------- 211 * Registers 212 */ 213 214 #define RCAR_DMAC_CHAN_OFFSET(i) (0x8000 + 0x80 * (i)) 215 216 #define RCAR_DMAISTA 0x0020 217 #define RCAR_DMASEC 0x0030 218 #define RCAR_DMAOR 0x0060 219 #define RCAR_DMAOR_PRI_FIXED (0 << 8) 220 #define RCAR_DMAOR_PRI_ROUND_ROBIN (3 << 8) 221 #define RCAR_DMAOR_AE (1 << 2) 222 #define RCAR_DMAOR_DME (1 << 0) 223 #define RCAR_DMACHCLR 0x0080 224 #define RCAR_DMADPSEC 0x00a0 225 226 #define RCAR_DMASAR 0x0000 227 #define RCAR_DMADAR 0x0004 228 #define RCAR_DMATCR 0x0008 229 #define RCAR_DMATCR_MASK 0x00ffffff 230 #define RCAR_DMATSR 0x0028 231 #define RCAR_DMACHCR 0x000c 232 #define RCAR_DMACHCR_CAE (1 << 31) 233 #define RCAR_DMACHCR_CAIE (1 << 30) 234 #define RCAR_DMACHCR_DPM_DISABLED (0 << 28) 235 #define RCAR_DMACHCR_DPM_ENABLED (1 << 28) 236 #define RCAR_DMACHCR_DPM_REPEAT (2 << 28) 237 #define RCAR_DMACHCR_DPM_INFINITE (3 << 28) 238 #define RCAR_DMACHCR_RPT_SAR (1 << 27) 239 #define RCAR_DMACHCR_RPT_DAR (1 << 26) 240 #define RCAR_DMACHCR_RPT_TCR (1 << 25) 241 #define RCAR_DMACHCR_DPB (1 << 22) 242 #define RCAR_DMACHCR_DSE (1 << 19) 243 #define RCAR_DMACHCR_DSIE (1 << 18) 244 #define RCAR_DMACHCR_TS_1B ((0 << 20) | (0 << 3)) 245 #define RCAR_DMACHCR_TS_2B ((0 << 20) | (1 << 3)) 246 #define RCAR_DMACHCR_TS_4B ((0 << 20) | (2 << 3)) 247 #define RCAR_DMACHCR_TS_16B ((0 << 20) | (3 << 3)) 248 #define RCAR_DMACHCR_TS_32B ((1 << 20) | (0 << 3)) 249 #define RCAR_DMACHCR_TS_64B ((1 << 20) | (1 << 3)) 250 #define RCAR_DMACHCR_TS_8B ((1 << 20) | (3 << 3)) 251 #define RCAR_DMACHCR_DM_FIXED (0 << 14) 252 #define RCAR_DMACHCR_DM_INC (1 << 14) 253 #define RCAR_DMACHCR_DM_DEC (2 << 14) 254 #define RCAR_DMACHCR_SM_FIXED (0 << 12) 255 #define RCAR_DMACHCR_SM_INC (1 << 12) 256 #define RCAR_DMACHCR_SM_DEC (2 << 12) 257 #define RCAR_DMACHCR_RS_AUTO (4 << 8) 258 #define RCAR_DMACHCR_RS_DMARS (8 << 8) 259 #define RCAR_DMACHCR_IE (1 << 2) 260 #define RCAR_DMACHCR_TE (1 << 1) 261 #define RCAR_DMACHCR_DE (1 << 0) 262 #define RCAR_DMATCRB 0x0018 263 #define RCAR_DMATSRB 0x0038 264 #define RCAR_DMACHCRB 0x001c 265 #define RCAR_DMACHCRB_DCNT(n) ((n) << 24) 266 #define RCAR_DMACHCRB_DPTR_MASK (0xff << 16) 267 #define RCAR_DMACHCRB_DPTR_SHIFT 16 268 #define RCAR_DMACHCRB_DRST (1 << 15) 269 #define RCAR_DMACHCRB_DTS (1 << 8) 270 #define RCAR_DMACHCRB_SLM_NORMAL (0 << 4) 271 #define RCAR_DMACHCRB_SLM_CLK(n) ((8 | (n)) << 4) 272 #define RCAR_DMACHCRB_PRI(n) ((n) << 0) 273 #define RCAR_DMARS 0x0040 274 #define RCAR_DMABUFCR 0x0048 275 #define RCAR_DMABUFCR_MBU(n) ((n) << 16) 276 #define RCAR_DMABUFCR_ULB(n) ((n) << 0) 277 #define RCAR_DMADPBASE 0x0050 278 #define RCAR_DMADPBASE_MASK 0xfffffff0 279 #define RCAR_DMADPBASE_SEL (1 << 0) 280 #define RCAR_DMADPCR 0x0054 281 #define RCAR_DMADPCR_DIPT(n) ((n) << 24) 282 #define RCAR_DMAFIXSAR 0x0010 283 #define RCAR_DMAFIXDAR 0x0014 284 #define RCAR_DMAFIXDPBASE 0x0060 285 286 /* Hardcode the MEMCPY transfer size to 4 bytes. */ 287 #define RCAR_DMAC_MEMCPY_XFER_SIZE 4 288 289 /* ----------------------------------------------------------------------------- 290 * Device access 291 */ 292 293 static void rcar_dmac_write(struct rcar_dmac *dmac, u32 reg, u32 data) 294 { 295 if (reg == RCAR_DMAOR) 296 writew(data, dmac->iomem + reg); 297 else 298 writel(data, dmac->iomem + reg); 299 } 300 301 static u32 rcar_dmac_read(struct rcar_dmac *dmac, u32 reg) 302 { 303 if (reg == RCAR_DMAOR) 304 return readw(dmac->iomem + reg); 305 else 306 return readl(dmac->iomem + reg); 307 } 308 309 static u32 rcar_dmac_chan_read(struct rcar_dmac_chan *chan, u32 reg) 310 { 311 if (reg == RCAR_DMARS) 312 return readw(chan->iomem + reg); 313 else 314 return readl(chan->iomem + reg); 315 } 316 317 static void rcar_dmac_chan_write(struct rcar_dmac_chan *chan, u32 reg, u32 data) 318 { 319 if (reg == RCAR_DMARS) 320 writew(data, chan->iomem + reg); 321 else 322 writel(data, chan->iomem + reg); 323 } 324 325 /* ----------------------------------------------------------------------------- 326 * Initialization and configuration 327 */ 328 329 static bool rcar_dmac_chan_is_busy(struct rcar_dmac_chan *chan) 330 { 331 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR); 332 333 return !!(chcr & (RCAR_DMACHCR_DE | RCAR_DMACHCR_TE)); 334 } 335 336 static void rcar_dmac_chan_start_xfer(struct rcar_dmac_chan *chan) 337 { 338 struct rcar_dmac_desc *desc = chan->desc.running; 339 u32 chcr = desc->chcr; 340 341 WARN_ON_ONCE(rcar_dmac_chan_is_busy(chan)); 342 343 if (chan->mid_rid >= 0) 344 rcar_dmac_chan_write(chan, RCAR_DMARS, chan->mid_rid); 345 346 if (desc->hwdescs.use) { 347 struct rcar_dmac_xfer_chunk *chunk; 348 349 dev_dbg(chan->chan.device->dev, 350 "chan%u: queue desc %p: %u@%pad\n", 351 chan->index, desc, desc->nchunks, &desc->hwdescs.dma); 352 353 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 354 rcar_dmac_chan_write(chan, RCAR_DMAFIXDPBASE, 355 desc->hwdescs.dma >> 32); 356 #endif 357 rcar_dmac_chan_write(chan, RCAR_DMADPBASE, 358 (desc->hwdescs.dma & 0xfffffff0) | 359 RCAR_DMADPBASE_SEL); 360 rcar_dmac_chan_write(chan, RCAR_DMACHCRB, 361 RCAR_DMACHCRB_DCNT(desc->nchunks - 1) | 362 RCAR_DMACHCRB_DRST); 363 364 /* 365 * Errata: When descriptor memory is accessed through an IOMMU 366 * the DMADAR register isn't initialized automatically from the 367 * first descriptor at beginning of transfer by the DMAC like it 368 * should. Initialize it manually with the destination address 369 * of the first chunk. 370 */ 371 chunk = list_first_entry(&desc->chunks, 372 struct rcar_dmac_xfer_chunk, node); 373 rcar_dmac_chan_write(chan, RCAR_DMADAR, 374 chunk->dst_addr & 0xffffffff); 375 376 /* 377 * Program the descriptor stage interrupt to occur after the end 378 * of the first stage. 379 */ 380 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(1)); 381 382 chcr |= RCAR_DMACHCR_RPT_SAR | RCAR_DMACHCR_RPT_DAR 383 | RCAR_DMACHCR_RPT_TCR | RCAR_DMACHCR_DPB; 384 385 /* 386 * If the descriptor isn't cyclic enable normal descriptor mode 387 * and the transfer completion interrupt. 388 */ 389 if (!desc->cyclic) 390 chcr |= RCAR_DMACHCR_DPM_ENABLED | RCAR_DMACHCR_IE; 391 /* 392 * If the descriptor is cyclic and has a callback enable the 393 * descriptor stage interrupt in infinite repeat mode. 394 */ 395 else if (desc->async_tx.callback) 396 chcr |= RCAR_DMACHCR_DPM_INFINITE | RCAR_DMACHCR_DSIE; 397 /* 398 * Otherwise just select infinite repeat mode without any 399 * interrupt. 400 */ 401 else 402 chcr |= RCAR_DMACHCR_DPM_INFINITE; 403 } else { 404 struct rcar_dmac_xfer_chunk *chunk = desc->running; 405 406 dev_dbg(chan->chan.device->dev, 407 "chan%u: queue chunk %p: %u@%pad -> %pad\n", 408 chan->index, chunk, chunk->size, &chunk->src_addr, 409 &chunk->dst_addr); 410 411 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 412 rcar_dmac_chan_write(chan, RCAR_DMAFIXSAR, 413 chunk->src_addr >> 32); 414 rcar_dmac_chan_write(chan, RCAR_DMAFIXDAR, 415 chunk->dst_addr >> 32); 416 #endif 417 rcar_dmac_chan_write(chan, RCAR_DMASAR, 418 chunk->src_addr & 0xffffffff); 419 rcar_dmac_chan_write(chan, RCAR_DMADAR, 420 chunk->dst_addr & 0xffffffff); 421 rcar_dmac_chan_write(chan, RCAR_DMATCR, 422 chunk->size >> desc->xfer_shift); 423 424 chcr |= RCAR_DMACHCR_DPM_DISABLED | RCAR_DMACHCR_IE; 425 } 426 427 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr | RCAR_DMACHCR_DE); 428 } 429 430 static int rcar_dmac_init(struct rcar_dmac *dmac) 431 { 432 u16 dmaor; 433 434 /* Clear all channels and enable the DMAC globally. */ 435 rcar_dmac_write(dmac, RCAR_DMACHCLR, GENMASK(dmac->n_channels - 1, 0)); 436 rcar_dmac_write(dmac, RCAR_DMAOR, 437 RCAR_DMAOR_PRI_FIXED | RCAR_DMAOR_DME); 438 439 dmaor = rcar_dmac_read(dmac, RCAR_DMAOR); 440 if ((dmaor & (RCAR_DMAOR_AE | RCAR_DMAOR_DME)) != RCAR_DMAOR_DME) { 441 dev_warn(dmac->dev, "DMAOR initialization failed.\n"); 442 return -EIO; 443 } 444 445 return 0; 446 } 447 448 /* ----------------------------------------------------------------------------- 449 * Descriptors submission 450 */ 451 452 static dma_cookie_t rcar_dmac_tx_submit(struct dma_async_tx_descriptor *tx) 453 { 454 struct rcar_dmac_chan *chan = to_rcar_dmac_chan(tx->chan); 455 struct rcar_dmac_desc *desc = to_rcar_dmac_desc(tx); 456 unsigned long flags; 457 dma_cookie_t cookie; 458 459 spin_lock_irqsave(&chan->lock, flags); 460 461 cookie = dma_cookie_assign(tx); 462 463 dev_dbg(chan->chan.device->dev, "chan%u: submit #%d@%p\n", 464 chan->index, tx->cookie, desc); 465 466 list_add_tail(&desc->node, &chan->desc.pending); 467 desc->running = list_first_entry(&desc->chunks, 468 struct rcar_dmac_xfer_chunk, node); 469 470 spin_unlock_irqrestore(&chan->lock, flags); 471 472 return cookie; 473 } 474 475 /* ----------------------------------------------------------------------------- 476 * Descriptors allocation and free 477 */ 478 479 /* 480 * rcar_dmac_desc_alloc - Allocate a page worth of DMA descriptors 481 * @chan: the DMA channel 482 * @gfp: allocation flags 483 */ 484 static int rcar_dmac_desc_alloc(struct rcar_dmac_chan *chan, gfp_t gfp) 485 { 486 struct rcar_dmac_desc_page *page; 487 unsigned long flags; 488 LIST_HEAD(list); 489 unsigned int i; 490 491 page = (void *)get_zeroed_page(gfp); 492 if (!page) 493 return -ENOMEM; 494 495 for (i = 0; i < RCAR_DMAC_DESCS_PER_PAGE; ++i) { 496 struct rcar_dmac_desc *desc = &page->descs[i]; 497 498 dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan); 499 desc->async_tx.tx_submit = rcar_dmac_tx_submit; 500 INIT_LIST_HEAD(&desc->chunks); 501 502 list_add_tail(&desc->node, &list); 503 } 504 505 spin_lock_irqsave(&chan->lock, flags); 506 list_splice_tail(&list, &chan->desc.free); 507 list_add_tail(&page->node, &chan->desc.pages); 508 spin_unlock_irqrestore(&chan->lock, flags); 509 510 return 0; 511 } 512 513 /* 514 * rcar_dmac_desc_put - Release a DMA transfer descriptor 515 * @chan: the DMA channel 516 * @desc: the descriptor 517 * 518 * Put the descriptor and its transfer chunk descriptors back in the channel's 519 * free descriptors lists. The descriptor's chunks list will be reinitialized to 520 * an empty list as a result. 521 * 522 * The descriptor must have been removed from the channel's lists before calling 523 * this function. 524 */ 525 static void rcar_dmac_desc_put(struct rcar_dmac_chan *chan, 526 struct rcar_dmac_desc *desc) 527 { 528 unsigned long flags; 529 530 spin_lock_irqsave(&chan->lock, flags); 531 list_splice_tail_init(&desc->chunks, &chan->desc.chunks_free); 532 list_add(&desc->node, &chan->desc.free); 533 spin_unlock_irqrestore(&chan->lock, flags); 534 } 535 536 static void rcar_dmac_desc_recycle_acked(struct rcar_dmac_chan *chan) 537 { 538 struct rcar_dmac_desc *desc, *_desc; 539 unsigned long flags; 540 LIST_HEAD(list); 541 542 /* 543 * We have to temporarily move all descriptors from the wait list to a 544 * local list as iterating over the wait list, even with 545 * list_for_each_entry_safe, isn't safe if we release the channel lock 546 * around the rcar_dmac_desc_put() call. 547 */ 548 spin_lock_irqsave(&chan->lock, flags); 549 list_splice_init(&chan->desc.wait, &list); 550 spin_unlock_irqrestore(&chan->lock, flags); 551 552 list_for_each_entry_safe(desc, _desc, &list, node) { 553 if (async_tx_test_ack(&desc->async_tx)) { 554 list_del(&desc->node); 555 rcar_dmac_desc_put(chan, desc); 556 } 557 } 558 559 if (list_empty(&list)) 560 return; 561 562 /* Put the remaining descriptors back in the wait list. */ 563 spin_lock_irqsave(&chan->lock, flags); 564 list_splice(&list, &chan->desc.wait); 565 spin_unlock_irqrestore(&chan->lock, flags); 566 } 567 568 /* 569 * rcar_dmac_desc_get - Allocate a descriptor for a DMA transfer 570 * @chan: the DMA channel 571 * 572 * Locking: This function must be called in a non-atomic context. 573 * 574 * Return: A pointer to the allocated descriptor or NULL if no descriptor can 575 * be allocated. 576 */ 577 static struct rcar_dmac_desc *rcar_dmac_desc_get(struct rcar_dmac_chan *chan) 578 { 579 struct rcar_dmac_desc *desc; 580 unsigned long flags; 581 int ret; 582 583 /* Recycle acked descriptors before attempting allocation. */ 584 rcar_dmac_desc_recycle_acked(chan); 585 586 spin_lock_irqsave(&chan->lock, flags); 587 588 while (list_empty(&chan->desc.free)) { 589 /* 590 * No free descriptors, allocate a page worth of them and try 591 * again, as someone else could race us to get the newly 592 * allocated descriptors. If the allocation fails return an 593 * error. 594 */ 595 spin_unlock_irqrestore(&chan->lock, flags); 596 ret = rcar_dmac_desc_alloc(chan, GFP_NOWAIT); 597 if (ret < 0) 598 return NULL; 599 spin_lock_irqsave(&chan->lock, flags); 600 } 601 602 desc = list_first_entry(&chan->desc.free, struct rcar_dmac_desc, node); 603 list_del(&desc->node); 604 605 spin_unlock_irqrestore(&chan->lock, flags); 606 607 return desc; 608 } 609 610 /* 611 * rcar_dmac_xfer_chunk_alloc - Allocate a page worth of transfer chunks 612 * @chan: the DMA channel 613 * @gfp: allocation flags 614 */ 615 static int rcar_dmac_xfer_chunk_alloc(struct rcar_dmac_chan *chan, gfp_t gfp) 616 { 617 struct rcar_dmac_desc_page *page; 618 unsigned long flags; 619 LIST_HEAD(list); 620 unsigned int i; 621 622 page = (void *)get_zeroed_page(gfp); 623 if (!page) 624 return -ENOMEM; 625 626 for (i = 0; i < RCAR_DMAC_XFER_CHUNKS_PER_PAGE; ++i) { 627 struct rcar_dmac_xfer_chunk *chunk = &page->chunks[i]; 628 629 list_add_tail(&chunk->node, &list); 630 } 631 632 spin_lock_irqsave(&chan->lock, flags); 633 list_splice_tail(&list, &chan->desc.chunks_free); 634 list_add_tail(&page->node, &chan->desc.pages); 635 spin_unlock_irqrestore(&chan->lock, flags); 636 637 return 0; 638 } 639 640 /* 641 * rcar_dmac_xfer_chunk_get - Allocate a transfer chunk for a DMA transfer 642 * @chan: the DMA channel 643 * 644 * Locking: This function must be called in a non-atomic context. 645 * 646 * Return: A pointer to the allocated transfer chunk descriptor or NULL if no 647 * descriptor can be allocated. 648 */ 649 static struct rcar_dmac_xfer_chunk * 650 rcar_dmac_xfer_chunk_get(struct rcar_dmac_chan *chan) 651 { 652 struct rcar_dmac_xfer_chunk *chunk; 653 unsigned long flags; 654 int ret; 655 656 spin_lock_irqsave(&chan->lock, flags); 657 658 while (list_empty(&chan->desc.chunks_free)) { 659 /* 660 * No free descriptors, allocate a page worth of them and try 661 * again, as someone else could race us to get the newly 662 * allocated descriptors. If the allocation fails return an 663 * error. 664 */ 665 spin_unlock_irqrestore(&chan->lock, flags); 666 ret = rcar_dmac_xfer_chunk_alloc(chan, GFP_NOWAIT); 667 if (ret < 0) 668 return NULL; 669 spin_lock_irqsave(&chan->lock, flags); 670 } 671 672 chunk = list_first_entry(&chan->desc.chunks_free, 673 struct rcar_dmac_xfer_chunk, node); 674 list_del(&chunk->node); 675 676 spin_unlock_irqrestore(&chan->lock, flags); 677 678 return chunk; 679 } 680 681 static void rcar_dmac_realloc_hwdesc(struct rcar_dmac_chan *chan, 682 struct rcar_dmac_desc *desc, size_t size) 683 { 684 /* 685 * dma_alloc_coherent() allocates memory in page size increments. To 686 * avoid reallocating the hardware descriptors when the allocated size 687 * wouldn't change align the requested size to a multiple of the page 688 * size. 689 */ 690 size = PAGE_ALIGN(size); 691 692 if (desc->hwdescs.size == size) 693 return; 694 695 if (desc->hwdescs.mem) { 696 dma_free_coherent(chan->chan.device->dev, desc->hwdescs.size, 697 desc->hwdescs.mem, desc->hwdescs.dma); 698 desc->hwdescs.mem = NULL; 699 desc->hwdescs.size = 0; 700 } 701 702 if (!size) 703 return; 704 705 desc->hwdescs.mem = dma_alloc_coherent(chan->chan.device->dev, size, 706 &desc->hwdescs.dma, GFP_NOWAIT); 707 if (!desc->hwdescs.mem) 708 return; 709 710 desc->hwdescs.size = size; 711 } 712 713 static int rcar_dmac_fill_hwdesc(struct rcar_dmac_chan *chan, 714 struct rcar_dmac_desc *desc) 715 { 716 struct rcar_dmac_xfer_chunk *chunk; 717 struct rcar_dmac_hw_desc *hwdesc; 718 719 rcar_dmac_realloc_hwdesc(chan, desc, desc->nchunks * sizeof(*hwdesc)); 720 721 hwdesc = desc->hwdescs.mem; 722 if (!hwdesc) 723 return -ENOMEM; 724 725 list_for_each_entry(chunk, &desc->chunks, node) { 726 hwdesc->sar = chunk->src_addr; 727 hwdesc->dar = chunk->dst_addr; 728 hwdesc->tcr = chunk->size >> desc->xfer_shift; 729 hwdesc++; 730 } 731 732 return 0; 733 } 734 735 /* ----------------------------------------------------------------------------- 736 * Stop and reset 737 */ 738 739 static void rcar_dmac_chan_halt(struct rcar_dmac_chan *chan) 740 { 741 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR); 742 743 chcr &= ~(RCAR_DMACHCR_DSE | RCAR_DMACHCR_DSIE | RCAR_DMACHCR_IE | 744 RCAR_DMACHCR_TE | RCAR_DMACHCR_DE); 745 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr); 746 } 747 748 static void rcar_dmac_chan_reinit(struct rcar_dmac_chan *chan) 749 { 750 struct rcar_dmac_desc *desc, *_desc; 751 unsigned long flags; 752 LIST_HEAD(descs); 753 754 spin_lock_irqsave(&chan->lock, flags); 755 756 /* Move all non-free descriptors to the local lists. */ 757 list_splice_init(&chan->desc.pending, &descs); 758 list_splice_init(&chan->desc.active, &descs); 759 list_splice_init(&chan->desc.done, &descs); 760 list_splice_init(&chan->desc.wait, &descs); 761 762 chan->desc.running = NULL; 763 764 spin_unlock_irqrestore(&chan->lock, flags); 765 766 list_for_each_entry_safe(desc, _desc, &descs, node) { 767 list_del(&desc->node); 768 rcar_dmac_desc_put(chan, desc); 769 } 770 } 771 772 static void rcar_dmac_stop(struct rcar_dmac *dmac) 773 { 774 rcar_dmac_write(dmac, RCAR_DMAOR, 0); 775 } 776 777 static void rcar_dmac_abort(struct rcar_dmac *dmac) 778 { 779 unsigned int i; 780 781 /* Stop all channels. */ 782 for (i = 0; i < dmac->n_channels; ++i) { 783 struct rcar_dmac_chan *chan = &dmac->channels[i]; 784 785 /* Stop and reinitialize the channel. */ 786 spin_lock(&chan->lock); 787 rcar_dmac_chan_halt(chan); 788 spin_unlock(&chan->lock); 789 790 rcar_dmac_chan_reinit(chan); 791 } 792 } 793 794 /* ----------------------------------------------------------------------------- 795 * Descriptors preparation 796 */ 797 798 static void rcar_dmac_chan_configure_desc(struct rcar_dmac_chan *chan, 799 struct rcar_dmac_desc *desc) 800 { 801 static const u32 chcr_ts[] = { 802 RCAR_DMACHCR_TS_1B, RCAR_DMACHCR_TS_2B, 803 RCAR_DMACHCR_TS_4B, RCAR_DMACHCR_TS_8B, 804 RCAR_DMACHCR_TS_16B, RCAR_DMACHCR_TS_32B, 805 RCAR_DMACHCR_TS_64B, 806 }; 807 808 unsigned int xfer_size; 809 u32 chcr; 810 811 switch (desc->direction) { 812 case DMA_DEV_TO_MEM: 813 chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_FIXED 814 | RCAR_DMACHCR_RS_DMARS; 815 xfer_size = chan->src.xfer_size; 816 break; 817 818 case DMA_MEM_TO_DEV: 819 chcr = RCAR_DMACHCR_DM_FIXED | RCAR_DMACHCR_SM_INC 820 | RCAR_DMACHCR_RS_DMARS; 821 xfer_size = chan->dst.xfer_size; 822 break; 823 824 case DMA_MEM_TO_MEM: 825 default: 826 chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_INC 827 | RCAR_DMACHCR_RS_AUTO; 828 xfer_size = RCAR_DMAC_MEMCPY_XFER_SIZE; 829 break; 830 } 831 832 desc->xfer_shift = ilog2(xfer_size); 833 desc->chcr = chcr | chcr_ts[desc->xfer_shift]; 834 } 835 836 /* 837 * rcar_dmac_chan_prep_sg - prepare transfer descriptors from an SG list 838 * 839 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also 840 * converted to scatter-gather to guarantee consistent locking and a correct 841 * list manipulation. For slave DMA direction carries the usual meaning, and, 842 * logically, the SG list is RAM and the addr variable contains slave address, 843 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM 844 * and the SG list contains only one element and points at the source buffer. 845 */ 846 static struct dma_async_tx_descriptor * 847 rcar_dmac_chan_prep_sg(struct rcar_dmac_chan *chan, struct scatterlist *sgl, 848 unsigned int sg_len, dma_addr_t dev_addr, 849 enum dma_transfer_direction dir, unsigned long dma_flags, 850 bool cyclic) 851 { 852 struct rcar_dmac_xfer_chunk *chunk; 853 struct rcar_dmac_desc *desc; 854 struct scatterlist *sg; 855 unsigned int nchunks = 0; 856 unsigned int max_chunk_size; 857 unsigned int full_size = 0; 858 bool highmem = false; 859 unsigned int i; 860 861 desc = rcar_dmac_desc_get(chan); 862 if (!desc) 863 return NULL; 864 865 desc->async_tx.flags = dma_flags; 866 desc->async_tx.cookie = -EBUSY; 867 868 desc->cyclic = cyclic; 869 desc->direction = dir; 870 871 rcar_dmac_chan_configure_desc(chan, desc); 872 873 max_chunk_size = (RCAR_DMATCR_MASK + 1) << desc->xfer_shift; 874 875 /* 876 * Allocate and fill the transfer chunk descriptors. We own the only 877 * reference to the DMA descriptor, there's no need for locking. 878 */ 879 for_each_sg(sgl, sg, sg_len, i) { 880 dma_addr_t mem_addr = sg_dma_address(sg); 881 unsigned int len = sg_dma_len(sg); 882 883 full_size += len; 884 885 while (len) { 886 unsigned int size = min(len, max_chunk_size); 887 888 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 889 /* 890 * Prevent individual transfers from crossing 4GB 891 * boundaries. 892 */ 893 if (dev_addr >> 32 != (dev_addr + size - 1) >> 32) 894 size = ALIGN(dev_addr, 1ULL << 32) - dev_addr; 895 if (mem_addr >> 32 != (mem_addr + size - 1) >> 32) 896 size = ALIGN(mem_addr, 1ULL << 32) - mem_addr; 897 898 /* 899 * Check if either of the source or destination address 900 * can't be expressed in 32 bits. If so we can't use 901 * hardware descriptor lists. 902 */ 903 if (dev_addr >> 32 || mem_addr >> 32) 904 highmem = true; 905 #endif 906 907 chunk = rcar_dmac_xfer_chunk_get(chan); 908 if (!chunk) { 909 rcar_dmac_desc_put(chan, desc); 910 return NULL; 911 } 912 913 if (dir == DMA_DEV_TO_MEM) { 914 chunk->src_addr = dev_addr; 915 chunk->dst_addr = mem_addr; 916 } else { 917 chunk->src_addr = mem_addr; 918 chunk->dst_addr = dev_addr; 919 } 920 921 chunk->size = size; 922 923 dev_dbg(chan->chan.device->dev, 924 "chan%u: chunk %p/%p sgl %u@%p, %u/%u %pad -> %pad\n", 925 chan->index, chunk, desc, i, sg, size, len, 926 &chunk->src_addr, &chunk->dst_addr); 927 928 mem_addr += size; 929 if (dir == DMA_MEM_TO_MEM) 930 dev_addr += size; 931 932 len -= size; 933 934 list_add_tail(&chunk->node, &desc->chunks); 935 nchunks++; 936 } 937 } 938 939 desc->nchunks = nchunks; 940 desc->size = full_size; 941 942 /* 943 * Use hardware descriptor lists if possible when more than one chunk 944 * needs to be transferred (otherwise they don't make much sense). 945 * 946 * The highmem check currently covers the whole transfer. As an 947 * optimization we could use descriptor lists for consecutive lowmem 948 * chunks and direct manual mode for highmem chunks. Whether the 949 * performance improvement would be significant enough compared to the 950 * additional complexity remains to be investigated. 951 */ 952 desc->hwdescs.use = !highmem && nchunks > 1; 953 if (desc->hwdescs.use) { 954 if (rcar_dmac_fill_hwdesc(chan, desc) < 0) 955 desc->hwdescs.use = false; 956 } 957 958 return &desc->async_tx; 959 } 960 961 /* ----------------------------------------------------------------------------- 962 * DMA engine operations 963 */ 964 965 static int rcar_dmac_alloc_chan_resources(struct dma_chan *chan) 966 { 967 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 968 int ret; 969 970 INIT_LIST_HEAD(&rchan->desc.chunks_free); 971 INIT_LIST_HEAD(&rchan->desc.pages); 972 973 /* Preallocate descriptors. */ 974 ret = rcar_dmac_xfer_chunk_alloc(rchan, GFP_KERNEL); 975 if (ret < 0) 976 return -ENOMEM; 977 978 ret = rcar_dmac_desc_alloc(rchan, GFP_KERNEL); 979 if (ret < 0) 980 return -ENOMEM; 981 982 return pm_runtime_get_sync(chan->device->dev); 983 } 984 985 static void rcar_dmac_free_chan_resources(struct dma_chan *chan) 986 { 987 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 988 struct rcar_dmac *dmac = to_rcar_dmac(chan->device); 989 struct rcar_dmac_chan_map *map = &rchan->map; 990 struct rcar_dmac_desc_page *page, *_page; 991 struct rcar_dmac_desc *desc; 992 LIST_HEAD(list); 993 994 /* Protect against ISR */ 995 spin_lock_irq(&rchan->lock); 996 rcar_dmac_chan_halt(rchan); 997 spin_unlock_irq(&rchan->lock); 998 999 /* Now no new interrupts will occur */ 1000 1001 if (rchan->mid_rid >= 0) { 1002 /* The caller is holding dma_list_mutex */ 1003 clear_bit(rchan->mid_rid, dmac->modules); 1004 rchan->mid_rid = -EINVAL; 1005 } 1006 1007 list_splice_init(&rchan->desc.free, &list); 1008 list_splice_init(&rchan->desc.pending, &list); 1009 list_splice_init(&rchan->desc.active, &list); 1010 list_splice_init(&rchan->desc.done, &list); 1011 list_splice_init(&rchan->desc.wait, &list); 1012 1013 rchan->desc.running = NULL; 1014 1015 list_for_each_entry(desc, &list, node) 1016 rcar_dmac_realloc_hwdesc(rchan, desc, 0); 1017 1018 list_for_each_entry_safe(page, _page, &rchan->desc.pages, node) { 1019 list_del(&page->node); 1020 free_page((unsigned long)page); 1021 } 1022 1023 /* Remove slave mapping if present. */ 1024 if (map->slave.xfer_size) { 1025 dma_unmap_resource(chan->device->dev, map->addr, 1026 map->slave.xfer_size, map->dir, 0); 1027 map->slave.xfer_size = 0; 1028 } 1029 1030 pm_runtime_put(chan->device->dev); 1031 } 1032 1033 static struct dma_async_tx_descriptor * 1034 rcar_dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest, 1035 dma_addr_t dma_src, size_t len, unsigned long flags) 1036 { 1037 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1038 struct scatterlist sgl; 1039 1040 if (!len) 1041 return NULL; 1042 1043 sg_init_table(&sgl, 1); 1044 sg_set_page(&sgl, pfn_to_page(PFN_DOWN(dma_src)), len, 1045 offset_in_page(dma_src)); 1046 sg_dma_address(&sgl) = dma_src; 1047 sg_dma_len(&sgl) = len; 1048 1049 return rcar_dmac_chan_prep_sg(rchan, &sgl, 1, dma_dest, 1050 DMA_MEM_TO_MEM, flags, false); 1051 } 1052 1053 static int rcar_dmac_map_slave_addr(struct dma_chan *chan, 1054 enum dma_transfer_direction dir) 1055 { 1056 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1057 struct rcar_dmac_chan_map *map = &rchan->map; 1058 phys_addr_t dev_addr; 1059 size_t dev_size; 1060 enum dma_data_direction dev_dir; 1061 1062 if (dir == DMA_DEV_TO_MEM) { 1063 dev_addr = rchan->src.slave_addr; 1064 dev_size = rchan->src.xfer_size; 1065 dev_dir = DMA_TO_DEVICE; 1066 } else { 1067 dev_addr = rchan->dst.slave_addr; 1068 dev_size = rchan->dst.xfer_size; 1069 dev_dir = DMA_FROM_DEVICE; 1070 } 1071 1072 /* Reuse current map if possible. */ 1073 if (dev_addr == map->slave.slave_addr && 1074 dev_size == map->slave.xfer_size && 1075 dev_dir == map->dir) 1076 return 0; 1077 1078 /* Remove old mapping if present. */ 1079 if (map->slave.xfer_size) 1080 dma_unmap_resource(chan->device->dev, map->addr, 1081 map->slave.xfer_size, map->dir, 0); 1082 map->slave.xfer_size = 0; 1083 1084 /* Create new slave address map. */ 1085 map->addr = dma_map_resource(chan->device->dev, dev_addr, dev_size, 1086 dev_dir, 0); 1087 1088 if (dma_mapping_error(chan->device->dev, map->addr)) { 1089 dev_err(chan->device->dev, 1090 "chan%u: failed to map %zx@%pap", rchan->index, 1091 dev_size, &dev_addr); 1092 return -EIO; 1093 } 1094 1095 dev_dbg(chan->device->dev, "chan%u: map %zx@%pap to %pad dir: %s\n", 1096 rchan->index, dev_size, &dev_addr, &map->addr, 1097 dev_dir == DMA_TO_DEVICE ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE"); 1098 1099 map->slave.slave_addr = dev_addr; 1100 map->slave.xfer_size = dev_size; 1101 map->dir = dev_dir; 1102 1103 return 0; 1104 } 1105 1106 static struct dma_async_tx_descriptor * 1107 rcar_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, 1108 unsigned int sg_len, enum dma_transfer_direction dir, 1109 unsigned long flags, void *context) 1110 { 1111 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1112 1113 /* Someone calling slave DMA on a generic channel? */ 1114 if (rchan->mid_rid < 0 || !sg_len) { 1115 dev_warn(chan->device->dev, 1116 "%s: bad parameter: len=%d, id=%d\n", 1117 __func__, sg_len, rchan->mid_rid); 1118 return NULL; 1119 } 1120 1121 if (rcar_dmac_map_slave_addr(chan, dir)) 1122 return NULL; 1123 1124 return rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr, 1125 dir, flags, false); 1126 } 1127 1128 #define RCAR_DMAC_MAX_SG_LEN 32 1129 1130 static struct dma_async_tx_descriptor * 1131 rcar_dmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, 1132 size_t buf_len, size_t period_len, 1133 enum dma_transfer_direction dir, unsigned long flags) 1134 { 1135 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1136 struct dma_async_tx_descriptor *desc; 1137 struct scatterlist *sgl; 1138 unsigned int sg_len; 1139 unsigned int i; 1140 1141 /* Someone calling slave DMA on a generic channel? */ 1142 if (rchan->mid_rid < 0 || buf_len < period_len) { 1143 dev_warn(chan->device->dev, 1144 "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n", 1145 __func__, buf_len, period_len, rchan->mid_rid); 1146 return NULL; 1147 } 1148 1149 if (rcar_dmac_map_slave_addr(chan, dir)) 1150 return NULL; 1151 1152 sg_len = buf_len / period_len; 1153 if (sg_len > RCAR_DMAC_MAX_SG_LEN) { 1154 dev_err(chan->device->dev, 1155 "chan%u: sg length %d exceds limit %d", 1156 rchan->index, sg_len, RCAR_DMAC_MAX_SG_LEN); 1157 return NULL; 1158 } 1159 1160 /* 1161 * Allocate the sg list dynamically as it would consume too much stack 1162 * space. 1163 */ 1164 sgl = kcalloc(sg_len, sizeof(*sgl), GFP_NOWAIT); 1165 if (!sgl) 1166 return NULL; 1167 1168 sg_init_table(sgl, sg_len); 1169 1170 for (i = 0; i < sg_len; ++i) { 1171 dma_addr_t src = buf_addr + (period_len * i); 1172 1173 sg_set_page(&sgl[i], pfn_to_page(PFN_DOWN(src)), period_len, 1174 offset_in_page(src)); 1175 sg_dma_address(&sgl[i]) = src; 1176 sg_dma_len(&sgl[i]) = period_len; 1177 } 1178 1179 desc = rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr, 1180 dir, flags, true); 1181 1182 kfree(sgl); 1183 return desc; 1184 } 1185 1186 static int rcar_dmac_device_config(struct dma_chan *chan, 1187 struct dma_slave_config *cfg) 1188 { 1189 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1190 1191 /* 1192 * We could lock this, but you shouldn't be configuring the 1193 * channel, while using it... 1194 */ 1195 rchan->src.slave_addr = cfg->src_addr; 1196 rchan->dst.slave_addr = cfg->dst_addr; 1197 rchan->src.xfer_size = cfg->src_addr_width; 1198 rchan->dst.xfer_size = cfg->dst_addr_width; 1199 1200 return 0; 1201 } 1202 1203 static int rcar_dmac_chan_terminate_all(struct dma_chan *chan) 1204 { 1205 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1206 unsigned long flags; 1207 1208 spin_lock_irqsave(&rchan->lock, flags); 1209 rcar_dmac_chan_halt(rchan); 1210 spin_unlock_irqrestore(&rchan->lock, flags); 1211 1212 /* 1213 * FIXME: No new interrupt can occur now, but the IRQ thread might still 1214 * be running. 1215 */ 1216 1217 rcar_dmac_chan_reinit(rchan); 1218 1219 return 0; 1220 } 1221 1222 static unsigned int rcar_dmac_chan_get_residue(struct rcar_dmac_chan *chan, 1223 dma_cookie_t cookie) 1224 { 1225 struct rcar_dmac_desc *desc = chan->desc.running; 1226 struct rcar_dmac_xfer_chunk *running = NULL; 1227 struct rcar_dmac_xfer_chunk *chunk; 1228 enum dma_status status; 1229 unsigned int residue = 0; 1230 unsigned int dptr = 0; 1231 1232 if (!desc) 1233 return 0; 1234 1235 /* 1236 * If the cookie corresponds to a descriptor that has been completed 1237 * there is no residue. The same check has already been performed by the 1238 * caller but without holding the channel lock, so the descriptor could 1239 * now be complete. 1240 */ 1241 status = dma_cookie_status(&chan->chan, cookie, NULL); 1242 if (status == DMA_COMPLETE) 1243 return 0; 1244 1245 /* 1246 * If the cookie doesn't correspond to the currently running transfer 1247 * then the descriptor hasn't been processed yet, and the residue is 1248 * equal to the full descriptor size. 1249 */ 1250 if (cookie != desc->async_tx.cookie) { 1251 list_for_each_entry(desc, &chan->desc.pending, node) { 1252 if (cookie == desc->async_tx.cookie) 1253 return desc->size; 1254 } 1255 list_for_each_entry(desc, &chan->desc.active, node) { 1256 if (cookie == desc->async_tx.cookie) 1257 return desc->size; 1258 } 1259 1260 /* 1261 * No descriptor found for the cookie, there's thus no residue. 1262 * This shouldn't happen if the calling driver passes a correct 1263 * cookie value. 1264 */ 1265 WARN(1, "No descriptor for cookie!"); 1266 return 0; 1267 } 1268 1269 /* 1270 * In descriptor mode the descriptor running pointer is not maintained 1271 * by the interrupt handler, find the running descriptor from the 1272 * descriptor pointer field in the CHCRB register. In non-descriptor 1273 * mode just use the running descriptor pointer. 1274 */ 1275 if (desc->hwdescs.use) { 1276 dptr = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) & 1277 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT; 1278 WARN_ON(dptr >= desc->nchunks); 1279 } else { 1280 running = desc->running; 1281 } 1282 1283 /* Compute the size of all chunks still to be transferred. */ 1284 list_for_each_entry_reverse(chunk, &desc->chunks, node) { 1285 if (chunk == running || ++dptr == desc->nchunks) 1286 break; 1287 1288 residue += chunk->size; 1289 } 1290 1291 /* Add the residue for the current chunk. */ 1292 residue += rcar_dmac_chan_read(chan, RCAR_DMATCR) << desc->xfer_shift; 1293 1294 return residue; 1295 } 1296 1297 static enum dma_status rcar_dmac_tx_status(struct dma_chan *chan, 1298 dma_cookie_t cookie, 1299 struct dma_tx_state *txstate) 1300 { 1301 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1302 enum dma_status status; 1303 unsigned long flags; 1304 unsigned int residue; 1305 1306 status = dma_cookie_status(chan, cookie, txstate); 1307 if (status == DMA_COMPLETE || !txstate) 1308 return status; 1309 1310 spin_lock_irqsave(&rchan->lock, flags); 1311 residue = rcar_dmac_chan_get_residue(rchan, cookie); 1312 spin_unlock_irqrestore(&rchan->lock, flags); 1313 1314 /* if there's no residue, the cookie is complete */ 1315 if (!residue) 1316 return DMA_COMPLETE; 1317 1318 dma_set_residue(txstate, residue); 1319 1320 return status; 1321 } 1322 1323 static void rcar_dmac_issue_pending(struct dma_chan *chan) 1324 { 1325 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1326 unsigned long flags; 1327 1328 spin_lock_irqsave(&rchan->lock, flags); 1329 1330 if (list_empty(&rchan->desc.pending)) 1331 goto done; 1332 1333 /* Append the pending list to the active list. */ 1334 list_splice_tail_init(&rchan->desc.pending, &rchan->desc.active); 1335 1336 /* 1337 * If no transfer is running pick the first descriptor from the active 1338 * list and start the transfer. 1339 */ 1340 if (!rchan->desc.running) { 1341 struct rcar_dmac_desc *desc; 1342 1343 desc = list_first_entry(&rchan->desc.active, 1344 struct rcar_dmac_desc, node); 1345 rchan->desc.running = desc; 1346 1347 rcar_dmac_chan_start_xfer(rchan); 1348 } 1349 1350 done: 1351 spin_unlock_irqrestore(&rchan->lock, flags); 1352 } 1353 1354 /* ----------------------------------------------------------------------------- 1355 * IRQ handling 1356 */ 1357 1358 static irqreturn_t rcar_dmac_isr_desc_stage_end(struct rcar_dmac_chan *chan) 1359 { 1360 struct rcar_dmac_desc *desc = chan->desc.running; 1361 unsigned int stage; 1362 1363 if (WARN_ON(!desc || !desc->cyclic)) { 1364 /* 1365 * This should never happen, there should always be a running 1366 * cyclic descriptor when a descriptor stage end interrupt is 1367 * triggered. Warn and return. 1368 */ 1369 return IRQ_NONE; 1370 } 1371 1372 /* Program the interrupt pointer to the next stage. */ 1373 stage = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) & 1374 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT; 1375 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(stage)); 1376 1377 return IRQ_WAKE_THREAD; 1378 } 1379 1380 static irqreturn_t rcar_dmac_isr_transfer_end(struct rcar_dmac_chan *chan) 1381 { 1382 struct rcar_dmac_desc *desc = chan->desc.running; 1383 irqreturn_t ret = IRQ_WAKE_THREAD; 1384 1385 if (WARN_ON_ONCE(!desc)) { 1386 /* 1387 * This should never happen, there should always be a running 1388 * descriptor when a transfer end interrupt is triggered. Warn 1389 * and return. 1390 */ 1391 return IRQ_NONE; 1392 } 1393 1394 /* 1395 * The transfer end interrupt isn't generated for each chunk when using 1396 * descriptor mode. Only update the running chunk pointer in 1397 * non-descriptor mode. 1398 */ 1399 if (!desc->hwdescs.use) { 1400 /* 1401 * If we haven't completed the last transfer chunk simply move 1402 * to the next one. Only wake the IRQ thread if the transfer is 1403 * cyclic. 1404 */ 1405 if (!list_is_last(&desc->running->node, &desc->chunks)) { 1406 desc->running = list_next_entry(desc->running, node); 1407 if (!desc->cyclic) 1408 ret = IRQ_HANDLED; 1409 goto done; 1410 } 1411 1412 /* 1413 * We've completed the last transfer chunk. If the transfer is 1414 * cyclic, move back to the first one. 1415 */ 1416 if (desc->cyclic) { 1417 desc->running = 1418 list_first_entry(&desc->chunks, 1419 struct rcar_dmac_xfer_chunk, 1420 node); 1421 goto done; 1422 } 1423 } 1424 1425 /* The descriptor is complete, move it to the done list. */ 1426 list_move_tail(&desc->node, &chan->desc.done); 1427 1428 /* Queue the next descriptor, if any. */ 1429 if (!list_empty(&chan->desc.active)) 1430 chan->desc.running = list_first_entry(&chan->desc.active, 1431 struct rcar_dmac_desc, 1432 node); 1433 else 1434 chan->desc.running = NULL; 1435 1436 done: 1437 if (chan->desc.running) 1438 rcar_dmac_chan_start_xfer(chan); 1439 1440 return ret; 1441 } 1442 1443 static irqreturn_t rcar_dmac_isr_channel(int irq, void *dev) 1444 { 1445 u32 mask = RCAR_DMACHCR_DSE | RCAR_DMACHCR_TE; 1446 struct rcar_dmac_chan *chan = dev; 1447 irqreturn_t ret = IRQ_NONE; 1448 u32 chcr; 1449 1450 spin_lock(&chan->lock); 1451 1452 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR); 1453 if (chcr & RCAR_DMACHCR_TE) 1454 mask |= RCAR_DMACHCR_DE; 1455 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr & ~mask); 1456 1457 if (chcr & RCAR_DMACHCR_DSE) 1458 ret |= rcar_dmac_isr_desc_stage_end(chan); 1459 1460 if (chcr & RCAR_DMACHCR_TE) 1461 ret |= rcar_dmac_isr_transfer_end(chan); 1462 1463 spin_unlock(&chan->lock); 1464 1465 return ret; 1466 } 1467 1468 static irqreturn_t rcar_dmac_isr_channel_thread(int irq, void *dev) 1469 { 1470 struct rcar_dmac_chan *chan = dev; 1471 struct rcar_dmac_desc *desc; 1472 struct dmaengine_desc_callback cb; 1473 1474 spin_lock_irq(&chan->lock); 1475 1476 /* For cyclic transfers notify the user after every chunk. */ 1477 if (chan->desc.running && chan->desc.running->cyclic) { 1478 desc = chan->desc.running; 1479 dmaengine_desc_get_callback(&desc->async_tx, &cb); 1480 1481 if (dmaengine_desc_callback_valid(&cb)) { 1482 spin_unlock_irq(&chan->lock); 1483 dmaengine_desc_callback_invoke(&cb, NULL); 1484 spin_lock_irq(&chan->lock); 1485 } 1486 } 1487 1488 /* 1489 * Call the callback function for all descriptors on the done list and 1490 * move them to the ack wait list. 1491 */ 1492 while (!list_empty(&chan->desc.done)) { 1493 desc = list_first_entry(&chan->desc.done, struct rcar_dmac_desc, 1494 node); 1495 dma_cookie_complete(&desc->async_tx); 1496 list_del(&desc->node); 1497 1498 dmaengine_desc_get_callback(&desc->async_tx, &cb); 1499 if (dmaengine_desc_callback_valid(&cb)) { 1500 spin_unlock_irq(&chan->lock); 1501 /* 1502 * We own the only reference to this descriptor, we can 1503 * safely dereference it without holding the channel 1504 * lock. 1505 */ 1506 dmaengine_desc_callback_invoke(&cb, NULL); 1507 spin_lock_irq(&chan->lock); 1508 } 1509 1510 list_add_tail(&desc->node, &chan->desc.wait); 1511 } 1512 1513 spin_unlock_irq(&chan->lock); 1514 1515 /* Recycle all acked descriptors. */ 1516 rcar_dmac_desc_recycle_acked(chan); 1517 1518 return IRQ_HANDLED; 1519 } 1520 1521 static irqreturn_t rcar_dmac_isr_error(int irq, void *data) 1522 { 1523 struct rcar_dmac *dmac = data; 1524 1525 if (!(rcar_dmac_read(dmac, RCAR_DMAOR) & RCAR_DMAOR_AE)) 1526 return IRQ_NONE; 1527 1528 /* 1529 * An unrecoverable error occurred on an unknown channel. Halt the DMAC, 1530 * abort transfers on all channels, and reinitialize the DMAC. 1531 */ 1532 rcar_dmac_stop(dmac); 1533 rcar_dmac_abort(dmac); 1534 rcar_dmac_init(dmac); 1535 1536 return IRQ_HANDLED; 1537 } 1538 1539 /* ----------------------------------------------------------------------------- 1540 * OF xlate and channel filter 1541 */ 1542 1543 static bool rcar_dmac_chan_filter(struct dma_chan *chan, void *arg) 1544 { 1545 struct rcar_dmac *dmac = to_rcar_dmac(chan->device); 1546 struct of_phandle_args *dma_spec = arg; 1547 1548 /* 1549 * FIXME: Using a filter on OF platforms is a nonsense. The OF xlate 1550 * function knows from which device it wants to allocate a channel from, 1551 * and would be perfectly capable of selecting the channel it wants. 1552 * Forcing it to call dma_request_channel() and iterate through all 1553 * channels from all controllers is just pointless. 1554 */ 1555 if (chan->device->device_config != rcar_dmac_device_config || 1556 dma_spec->np != chan->device->dev->of_node) 1557 return false; 1558 1559 return !test_and_set_bit(dma_spec->args[0], dmac->modules); 1560 } 1561 1562 static struct dma_chan *rcar_dmac_of_xlate(struct of_phandle_args *dma_spec, 1563 struct of_dma *ofdma) 1564 { 1565 struct rcar_dmac_chan *rchan; 1566 struct dma_chan *chan; 1567 dma_cap_mask_t mask; 1568 1569 if (dma_spec->args_count != 1) 1570 return NULL; 1571 1572 /* Only slave DMA channels can be allocated via DT */ 1573 dma_cap_zero(mask); 1574 dma_cap_set(DMA_SLAVE, mask); 1575 1576 chan = dma_request_channel(mask, rcar_dmac_chan_filter, dma_spec); 1577 if (!chan) 1578 return NULL; 1579 1580 rchan = to_rcar_dmac_chan(chan); 1581 rchan->mid_rid = dma_spec->args[0]; 1582 1583 return chan; 1584 } 1585 1586 /* ----------------------------------------------------------------------------- 1587 * Power management 1588 */ 1589 1590 #ifdef CONFIG_PM_SLEEP 1591 static int rcar_dmac_sleep_suspend(struct device *dev) 1592 { 1593 /* 1594 * TODO: Wait for the current transfer to complete and stop the device. 1595 */ 1596 return 0; 1597 } 1598 1599 static int rcar_dmac_sleep_resume(struct device *dev) 1600 { 1601 /* TODO: Resume transfers, if any. */ 1602 return 0; 1603 } 1604 #endif 1605 1606 #ifdef CONFIG_PM 1607 static int rcar_dmac_runtime_suspend(struct device *dev) 1608 { 1609 return 0; 1610 } 1611 1612 static int rcar_dmac_runtime_resume(struct device *dev) 1613 { 1614 struct rcar_dmac *dmac = dev_get_drvdata(dev); 1615 1616 return rcar_dmac_init(dmac); 1617 } 1618 #endif 1619 1620 static const struct dev_pm_ops rcar_dmac_pm = { 1621 SET_SYSTEM_SLEEP_PM_OPS(rcar_dmac_sleep_suspend, rcar_dmac_sleep_resume) 1622 SET_RUNTIME_PM_OPS(rcar_dmac_runtime_suspend, rcar_dmac_runtime_resume, 1623 NULL) 1624 }; 1625 1626 /* ----------------------------------------------------------------------------- 1627 * Probe and remove 1628 */ 1629 1630 static int rcar_dmac_chan_probe(struct rcar_dmac *dmac, 1631 struct rcar_dmac_chan *rchan, 1632 unsigned int index) 1633 { 1634 struct platform_device *pdev = to_platform_device(dmac->dev); 1635 struct dma_chan *chan = &rchan->chan; 1636 char pdev_irqname[5]; 1637 char *irqname; 1638 int irq; 1639 int ret; 1640 1641 rchan->index = index; 1642 rchan->iomem = dmac->iomem + RCAR_DMAC_CHAN_OFFSET(index); 1643 rchan->mid_rid = -EINVAL; 1644 1645 spin_lock_init(&rchan->lock); 1646 1647 INIT_LIST_HEAD(&rchan->desc.free); 1648 INIT_LIST_HEAD(&rchan->desc.pending); 1649 INIT_LIST_HEAD(&rchan->desc.active); 1650 INIT_LIST_HEAD(&rchan->desc.done); 1651 INIT_LIST_HEAD(&rchan->desc.wait); 1652 1653 /* Request the channel interrupt. */ 1654 sprintf(pdev_irqname, "ch%u", index); 1655 irq = platform_get_irq_byname(pdev, pdev_irqname); 1656 if (irq < 0) { 1657 dev_err(dmac->dev, "no IRQ specified for channel %u\n", index); 1658 return -ENODEV; 1659 } 1660 1661 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u", 1662 dev_name(dmac->dev), index); 1663 if (!irqname) 1664 return -ENOMEM; 1665 1666 ret = devm_request_threaded_irq(dmac->dev, irq, rcar_dmac_isr_channel, 1667 rcar_dmac_isr_channel_thread, 0, 1668 irqname, rchan); 1669 if (ret) { 1670 dev_err(dmac->dev, "failed to request IRQ %u (%d)\n", irq, ret); 1671 return ret; 1672 } 1673 1674 /* 1675 * Initialize the DMA engine channel and add it to the DMA engine 1676 * channels list. 1677 */ 1678 chan->device = &dmac->engine; 1679 dma_cookie_init(chan); 1680 1681 list_add_tail(&chan->device_node, &dmac->engine.channels); 1682 1683 return 0; 1684 } 1685 1686 static int rcar_dmac_parse_of(struct device *dev, struct rcar_dmac *dmac) 1687 { 1688 struct device_node *np = dev->of_node; 1689 int ret; 1690 1691 ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels); 1692 if (ret < 0) { 1693 dev_err(dev, "unable to read dma-channels property\n"); 1694 return ret; 1695 } 1696 1697 if (dmac->n_channels <= 0 || dmac->n_channels >= 100) { 1698 dev_err(dev, "invalid number of channels %u\n", 1699 dmac->n_channels); 1700 return -EINVAL; 1701 } 1702 1703 return 0; 1704 } 1705 1706 static int rcar_dmac_probe(struct platform_device *pdev) 1707 { 1708 const enum dma_slave_buswidth widths = DMA_SLAVE_BUSWIDTH_1_BYTE | 1709 DMA_SLAVE_BUSWIDTH_2_BYTES | DMA_SLAVE_BUSWIDTH_4_BYTES | 1710 DMA_SLAVE_BUSWIDTH_8_BYTES | DMA_SLAVE_BUSWIDTH_16_BYTES | 1711 DMA_SLAVE_BUSWIDTH_32_BYTES | DMA_SLAVE_BUSWIDTH_64_BYTES; 1712 unsigned int channels_offset = 0; 1713 struct dma_device *engine; 1714 struct rcar_dmac *dmac; 1715 struct resource *mem; 1716 unsigned int i; 1717 char *irqname; 1718 int irq; 1719 int ret; 1720 1721 dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL); 1722 if (!dmac) 1723 return -ENOMEM; 1724 1725 dmac->dev = &pdev->dev; 1726 platform_set_drvdata(pdev, dmac); 1727 dma_set_mask_and_coherent(dmac->dev, DMA_BIT_MASK(40)); 1728 1729 ret = rcar_dmac_parse_of(&pdev->dev, dmac); 1730 if (ret < 0) 1731 return ret; 1732 1733 /* 1734 * A still unconfirmed hardware bug prevents the IPMMU microTLB 0 to be 1735 * flushed correctly, resulting in memory corruption. DMAC 0 channel 0 1736 * is connected to microTLB 0 on currently supported platforms, so we 1737 * can't use it with the IPMMU. As the IOMMU API operates at the device 1738 * level we can't disable it selectively, so ignore channel 0 for now if 1739 * the device is part of an IOMMU group. 1740 */ 1741 if (pdev->dev.iommu_group) { 1742 dmac->n_channels--; 1743 channels_offset = 1; 1744 } 1745 1746 dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels, 1747 sizeof(*dmac->channels), GFP_KERNEL); 1748 if (!dmac->channels) 1749 return -ENOMEM; 1750 1751 /* Request resources. */ 1752 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1753 dmac->iomem = devm_ioremap_resource(&pdev->dev, mem); 1754 if (IS_ERR(dmac->iomem)) 1755 return PTR_ERR(dmac->iomem); 1756 1757 irq = platform_get_irq_byname(pdev, "error"); 1758 if (irq < 0) { 1759 dev_err(&pdev->dev, "no error IRQ specified\n"); 1760 return -ENODEV; 1761 } 1762 1763 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:error", 1764 dev_name(dmac->dev)); 1765 if (!irqname) 1766 return -ENOMEM; 1767 1768 ret = devm_request_irq(&pdev->dev, irq, rcar_dmac_isr_error, 0, 1769 irqname, dmac); 1770 if (ret) { 1771 dev_err(&pdev->dev, "failed to request IRQ %u (%d)\n", 1772 irq, ret); 1773 return ret; 1774 } 1775 1776 /* Enable runtime PM and initialize the device. */ 1777 pm_runtime_enable(&pdev->dev); 1778 ret = pm_runtime_get_sync(&pdev->dev); 1779 if (ret < 0) { 1780 dev_err(&pdev->dev, "runtime PM get sync failed (%d)\n", ret); 1781 return ret; 1782 } 1783 1784 ret = rcar_dmac_init(dmac); 1785 pm_runtime_put(&pdev->dev); 1786 1787 if (ret) { 1788 dev_err(&pdev->dev, "failed to reset device\n"); 1789 goto error; 1790 } 1791 1792 /* Initialize the channels. */ 1793 INIT_LIST_HEAD(&dmac->engine.channels); 1794 1795 for (i = 0; i < dmac->n_channels; ++i) { 1796 ret = rcar_dmac_chan_probe(dmac, &dmac->channels[i], 1797 i + channels_offset); 1798 if (ret < 0) 1799 goto error; 1800 } 1801 1802 /* Register the DMAC as a DMA provider for DT. */ 1803 ret = of_dma_controller_register(pdev->dev.of_node, rcar_dmac_of_xlate, 1804 NULL); 1805 if (ret < 0) 1806 goto error; 1807 1808 /* 1809 * Register the DMA engine device. 1810 * 1811 * Default transfer size of 32 bytes requires 32-byte alignment. 1812 */ 1813 engine = &dmac->engine; 1814 dma_cap_set(DMA_MEMCPY, engine->cap_mask); 1815 dma_cap_set(DMA_SLAVE, engine->cap_mask); 1816 1817 engine->dev = &pdev->dev; 1818 engine->copy_align = ilog2(RCAR_DMAC_MEMCPY_XFER_SIZE); 1819 1820 engine->src_addr_widths = widths; 1821 engine->dst_addr_widths = widths; 1822 engine->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM); 1823 engine->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; 1824 1825 engine->device_alloc_chan_resources = rcar_dmac_alloc_chan_resources; 1826 engine->device_free_chan_resources = rcar_dmac_free_chan_resources; 1827 engine->device_prep_dma_memcpy = rcar_dmac_prep_dma_memcpy; 1828 engine->device_prep_slave_sg = rcar_dmac_prep_slave_sg; 1829 engine->device_prep_dma_cyclic = rcar_dmac_prep_dma_cyclic; 1830 engine->device_config = rcar_dmac_device_config; 1831 engine->device_terminate_all = rcar_dmac_chan_terminate_all; 1832 engine->device_tx_status = rcar_dmac_tx_status; 1833 engine->device_issue_pending = rcar_dmac_issue_pending; 1834 1835 ret = dma_async_device_register(engine); 1836 if (ret < 0) 1837 goto error; 1838 1839 return 0; 1840 1841 error: 1842 of_dma_controller_free(pdev->dev.of_node); 1843 pm_runtime_disable(&pdev->dev); 1844 return ret; 1845 } 1846 1847 static int rcar_dmac_remove(struct platform_device *pdev) 1848 { 1849 struct rcar_dmac *dmac = platform_get_drvdata(pdev); 1850 1851 of_dma_controller_free(pdev->dev.of_node); 1852 dma_async_device_unregister(&dmac->engine); 1853 1854 pm_runtime_disable(&pdev->dev); 1855 1856 return 0; 1857 } 1858 1859 static void rcar_dmac_shutdown(struct platform_device *pdev) 1860 { 1861 struct rcar_dmac *dmac = platform_get_drvdata(pdev); 1862 1863 rcar_dmac_stop(dmac); 1864 } 1865 1866 static const struct of_device_id rcar_dmac_of_ids[] = { 1867 { .compatible = "renesas,rcar-dmac", }, 1868 { /* Sentinel */ } 1869 }; 1870 MODULE_DEVICE_TABLE(of, rcar_dmac_of_ids); 1871 1872 static struct platform_driver rcar_dmac_driver = { 1873 .driver = { 1874 .pm = &rcar_dmac_pm, 1875 .name = "rcar-dmac", 1876 .of_match_table = rcar_dmac_of_ids, 1877 }, 1878 .probe = rcar_dmac_probe, 1879 .remove = rcar_dmac_remove, 1880 .shutdown = rcar_dmac_shutdown, 1881 }; 1882 1883 module_platform_driver(rcar_dmac_driver); 1884 1885 MODULE_DESCRIPTION("R-Car Gen2 DMA Controller Driver"); 1886 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); 1887 MODULE_LICENSE("GPL v2"); 1888