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_desc_page *page, *_page; 990 struct rcar_dmac_desc *desc; 991 LIST_HEAD(list); 992 993 /* Protect against ISR */ 994 spin_lock_irq(&rchan->lock); 995 rcar_dmac_chan_halt(rchan); 996 spin_unlock_irq(&rchan->lock); 997 998 /* Now no new interrupts will occur */ 999 1000 if (rchan->mid_rid >= 0) { 1001 /* The caller is holding dma_list_mutex */ 1002 clear_bit(rchan->mid_rid, dmac->modules); 1003 rchan->mid_rid = -EINVAL; 1004 } 1005 1006 list_splice_init(&rchan->desc.free, &list); 1007 list_splice_init(&rchan->desc.pending, &list); 1008 list_splice_init(&rchan->desc.active, &list); 1009 list_splice_init(&rchan->desc.done, &list); 1010 list_splice_init(&rchan->desc.wait, &list); 1011 1012 rchan->desc.running = NULL; 1013 1014 list_for_each_entry(desc, &list, node) 1015 rcar_dmac_realloc_hwdesc(rchan, desc, 0); 1016 1017 list_for_each_entry_safe(page, _page, &rchan->desc.pages, node) { 1018 list_del(&page->node); 1019 free_page((unsigned long)page); 1020 } 1021 1022 pm_runtime_put(chan->device->dev); 1023 } 1024 1025 static struct dma_async_tx_descriptor * 1026 rcar_dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest, 1027 dma_addr_t dma_src, size_t len, unsigned long flags) 1028 { 1029 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1030 struct scatterlist sgl; 1031 1032 if (!len) 1033 return NULL; 1034 1035 sg_init_table(&sgl, 1); 1036 sg_set_page(&sgl, pfn_to_page(PFN_DOWN(dma_src)), len, 1037 offset_in_page(dma_src)); 1038 sg_dma_address(&sgl) = dma_src; 1039 sg_dma_len(&sgl) = len; 1040 1041 return rcar_dmac_chan_prep_sg(rchan, &sgl, 1, dma_dest, 1042 DMA_MEM_TO_MEM, flags, false); 1043 } 1044 1045 static int rcar_dmac_map_slave_addr(struct dma_chan *chan, 1046 enum dma_transfer_direction dir) 1047 { 1048 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1049 struct rcar_dmac_chan_map *map = &rchan->map; 1050 phys_addr_t dev_addr; 1051 size_t dev_size; 1052 enum dma_data_direction dev_dir; 1053 1054 if (dir == DMA_DEV_TO_MEM) { 1055 dev_addr = rchan->src.slave_addr; 1056 dev_size = rchan->src.xfer_size; 1057 dev_dir = DMA_TO_DEVICE; 1058 } else { 1059 dev_addr = rchan->dst.slave_addr; 1060 dev_size = rchan->dst.xfer_size; 1061 dev_dir = DMA_FROM_DEVICE; 1062 } 1063 1064 /* Reuse current map if possible. */ 1065 if (dev_addr == map->slave.slave_addr && 1066 dev_size == map->slave.xfer_size && 1067 dev_dir == map->dir) 1068 return 0; 1069 1070 /* Remove old mapping if present. */ 1071 if (map->slave.xfer_size) 1072 dma_unmap_resource(chan->device->dev, map->addr, 1073 map->slave.xfer_size, map->dir, 0); 1074 map->slave.xfer_size = 0; 1075 1076 /* Create new slave address map. */ 1077 map->addr = dma_map_resource(chan->device->dev, dev_addr, dev_size, 1078 dev_dir, 0); 1079 1080 if (dma_mapping_error(chan->device->dev, map->addr)) { 1081 dev_err(chan->device->dev, 1082 "chan%u: failed to map %zx@%pap", rchan->index, 1083 dev_size, &dev_addr); 1084 return -EIO; 1085 } 1086 1087 dev_dbg(chan->device->dev, "chan%u: map %zx@%pap to %pad dir: %s\n", 1088 rchan->index, dev_size, &dev_addr, &map->addr, 1089 dev_dir == DMA_TO_DEVICE ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE"); 1090 1091 map->slave.slave_addr = dev_addr; 1092 map->slave.xfer_size = dev_size; 1093 map->dir = dev_dir; 1094 1095 return 0; 1096 } 1097 1098 static struct dma_async_tx_descriptor * 1099 rcar_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, 1100 unsigned int sg_len, enum dma_transfer_direction dir, 1101 unsigned long flags, void *context) 1102 { 1103 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1104 1105 /* Someone calling slave DMA on a generic channel? */ 1106 if (rchan->mid_rid < 0 || !sg_len) { 1107 dev_warn(chan->device->dev, 1108 "%s: bad parameter: len=%d, id=%d\n", 1109 __func__, sg_len, rchan->mid_rid); 1110 return NULL; 1111 } 1112 1113 if (rcar_dmac_map_slave_addr(chan, dir)) 1114 return NULL; 1115 1116 return rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr, 1117 dir, flags, false); 1118 } 1119 1120 #define RCAR_DMAC_MAX_SG_LEN 32 1121 1122 static struct dma_async_tx_descriptor * 1123 rcar_dmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, 1124 size_t buf_len, size_t period_len, 1125 enum dma_transfer_direction dir, unsigned long flags) 1126 { 1127 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1128 struct dma_async_tx_descriptor *desc; 1129 struct scatterlist *sgl; 1130 unsigned int sg_len; 1131 unsigned int i; 1132 1133 /* Someone calling slave DMA on a generic channel? */ 1134 if (rchan->mid_rid < 0 || buf_len < period_len) { 1135 dev_warn(chan->device->dev, 1136 "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n", 1137 __func__, buf_len, period_len, rchan->mid_rid); 1138 return NULL; 1139 } 1140 1141 if (rcar_dmac_map_slave_addr(chan, dir)) 1142 return NULL; 1143 1144 sg_len = buf_len / period_len; 1145 if (sg_len > RCAR_DMAC_MAX_SG_LEN) { 1146 dev_err(chan->device->dev, 1147 "chan%u: sg length %d exceds limit %d", 1148 rchan->index, sg_len, RCAR_DMAC_MAX_SG_LEN); 1149 return NULL; 1150 } 1151 1152 /* 1153 * Allocate the sg list dynamically as it would consume too much stack 1154 * space. 1155 */ 1156 sgl = kcalloc(sg_len, sizeof(*sgl), GFP_NOWAIT); 1157 if (!sgl) 1158 return NULL; 1159 1160 sg_init_table(sgl, sg_len); 1161 1162 for (i = 0; i < sg_len; ++i) { 1163 dma_addr_t src = buf_addr + (period_len * i); 1164 1165 sg_set_page(&sgl[i], pfn_to_page(PFN_DOWN(src)), period_len, 1166 offset_in_page(src)); 1167 sg_dma_address(&sgl[i]) = src; 1168 sg_dma_len(&sgl[i]) = period_len; 1169 } 1170 1171 desc = rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr, 1172 dir, flags, true); 1173 1174 kfree(sgl); 1175 return desc; 1176 } 1177 1178 static int rcar_dmac_device_config(struct dma_chan *chan, 1179 struct dma_slave_config *cfg) 1180 { 1181 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1182 1183 /* 1184 * We could lock this, but you shouldn't be configuring the 1185 * channel, while using it... 1186 */ 1187 rchan->src.slave_addr = cfg->src_addr; 1188 rchan->dst.slave_addr = cfg->dst_addr; 1189 rchan->src.xfer_size = cfg->src_addr_width; 1190 rchan->dst.xfer_size = cfg->dst_addr_width; 1191 1192 return 0; 1193 } 1194 1195 static int rcar_dmac_chan_terminate_all(struct dma_chan *chan) 1196 { 1197 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1198 unsigned long flags; 1199 1200 spin_lock_irqsave(&rchan->lock, flags); 1201 rcar_dmac_chan_halt(rchan); 1202 spin_unlock_irqrestore(&rchan->lock, flags); 1203 1204 /* 1205 * FIXME: No new interrupt can occur now, but the IRQ thread might still 1206 * be running. 1207 */ 1208 1209 rcar_dmac_chan_reinit(rchan); 1210 1211 return 0; 1212 } 1213 1214 static unsigned int rcar_dmac_chan_get_residue(struct rcar_dmac_chan *chan, 1215 dma_cookie_t cookie) 1216 { 1217 struct rcar_dmac_desc *desc = chan->desc.running; 1218 struct rcar_dmac_xfer_chunk *running = NULL; 1219 struct rcar_dmac_xfer_chunk *chunk; 1220 enum dma_status status; 1221 unsigned int residue = 0; 1222 unsigned int dptr = 0; 1223 1224 if (!desc) 1225 return 0; 1226 1227 /* 1228 * If the cookie corresponds to a descriptor that has been completed 1229 * there is no residue. The same check has already been performed by the 1230 * caller but without holding the channel lock, so the descriptor could 1231 * now be complete. 1232 */ 1233 status = dma_cookie_status(&chan->chan, cookie, NULL); 1234 if (status == DMA_COMPLETE) 1235 return 0; 1236 1237 /* 1238 * If the cookie doesn't correspond to the currently running transfer 1239 * then the descriptor hasn't been processed yet, and the residue is 1240 * equal to the full descriptor size. 1241 */ 1242 if (cookie != desc->async_tx.cookie) { 1243 list_for_each_entry(desc, &chan->desc.pending, node) { 1244 if (cookie == desc->async_tx.cookie) 1245 return desc->size; 1246 } 1247 list_for_each_entry(desc, &chan->desc.active, node) { 1248 if (cookie == desc->async_tx.cookie) 1249 return desc->size; 1250 } 1251 1252 /* 1253 * No descriptor found for the cookie, there's thus no residue. 1254 * This shouldn't happen if the calling driver passes a correct 1255 * cookie value. 1256 */ 1257 WARN(1, "No descriptor for cookie!"); 1258 return 0; 1259 } 1260 1261 /* 1262 * In descriptor mode the descriptor running pointer is not maintained 1263 * by the interrupt handler, find the running descriptor from the 1264 * descriptor pointer field in the CHCRB register. In non-descriptor 1265 * mode just use the running descriptor pointer. 1266 */ 1267 if (desc->hwdescs.use) { 1268 dptr = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) & 1269 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT; 1270 WARN_ON(dptr >= desc->nchunks); 1271 } else { 1272 running = desc->running; 1273 } 1274 1275 /* Compute the size of all chunks still to be transferred. */ 1276 list_for_each_entry_reverse(chunk, &desc->chunks, node) { 1277 if (chunk == running || ++dptr == desc->nchunks) 1278 break; 1279 1280 residue += chunk->size; 1281 } 1282 1283 /* Add the residue for the current chunk. */ 1284 residue += rcar_dmac_chan_read(chan, RCAR_DMATCR) << desc->xfer_shift; 1285 1286 return residue; 1287 } 1288 1289 static enum dma_status rcar_dmac_tx_status(struct dma_chan *chan, 1290 dma_cookie_t cookie, 1291 struct dma_tx_state *txstate) 1292 { 1293 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1294 enum dma_status status; 1295 unsigned long flags; 1296 unsigned int residue; 1297 1298 status = dma_cookie_status(chan, cookie, txstate); 1299 if (status == DMA_COMPLETE || !txstate) 1300 return status; 1301 1302 spin_lock_irqsave(&rchan->lock, flags); 1303 residue = rcar_dmac_chan_get_residue(rchan, cookie); 1304 spin_unlock_irqrestore(&rchan->lock, flags); 1305 1306 /* if there's no residue, the cookie is complete */ 1307 if (!residue) 1308 return DMA_COMPLETE; 1309 1310 dma_set_residue(txstate, residue); 1311 1312 return status; 1313 } 1314 1315 static void rcar_dmac_issue_pending(struct dma_chan *chan) 1316 { 1317 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan); 1318 unsigned long flags; 1319 1320 spin_lock_irqsave(&rchan->lock, flags); 1321 1322 if (list_empty(&rchan->desc.pending)) 1323 goto done; 1324 1325 /* Append the pending list to the active list. */ 1326 list_splice_tail_init(&rchan->desc.pending, &rchan->desc.active); 1327 1328 /* 1329 * If no transfer is running pick the first descriptor from the active 1330 * list and start the transfer. 1331 */ 1332 if (!rchan->desc.running) { 1333 struct rcar_dmac_desc *desc; 1334 1335 desc = list_first_entry(&rchan->desc.active, 1336 struct rcar_dmac_desc, node); 1337 rchan->desc.running = desc; 1338 1339 rcar_dmac_chan_start_xfer(rchan); 1340 } 1341 1342 done: 1343 spin_unlock_irqrestore(&rchan->lock, flags); 1344 } 1345 1346 /* ----------------------------------------------------------------------------- 1347 * IRQ handling 1348 */ 1349 1350 static irqreturn_t rcar_dmac_isr_desc_stage_end(struct rcar_dmac_chan *chan) 1351 { 1352 struct rcar_dmac_desc *desc = chan->desc.running; 1353 unsigned int stage; 1354 1355 if (WARN_ON(!desc || !desc->cyclic)) { 1356 /* 1357 * This should never happen, there should always be a running 1358 * cyclic descriptor when a descriptor stage end interrupt is 1359 * triggered. Warn and return. 1360 */ 1361 return IRQ_NONE; 1362 } 1363 1364 /* Program the interrupt pointer to the next stage. */ 1365 stage = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) & 1366 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT; 1367 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(stage)); 1368 1369 return IRQ_WAKE_THREAD; 1370 } 1371 1372 static irqreturn_t rcar_dmac_isr_transfer_end(struct rcar_dmac_chan *chan) 1373 { 1374 struct rcar_dmac_desc *desc = chan->desc.running; 1375 irqreturn_t ret = IRQ_WAKE_THREAD; 1376 1377 if (WARN_ON_ONCE(!desc)) { 1378 /* 1379 * This should never happen, there should always be a running 1380 * descriptor when a transfer end interrupt is triggered. Warn 1381 * and return. 1382 */ 1383 return IRQ_NONE; 1384 } 1385 1386 /* 1387 * The transfer end interrupt isn't generated for each chunk when using 1388 * descriptor mode. Only update the running chunk pointer in 1389 * non-descriptor mode. 1390 */ 1391 if (!desc->hwdescs.use) { 1392 /* 1393 * If we haven't completed the last transfer chunk simply move 1394 * to the next one. Only wake the IRQ thread if the transfer is 1395 * cyclic. 1396 */ 1397 if (!list_is_last(&desc->running->node, &desc->chunks)) { 1398 desc->running = list_next_entry(desc->running, node); 1399 if (!desc->cyclic) 1400 ret = IRQ_HANDLED; 1401 goto done; 1402 } 1403 1404 /* 1405 * We've completed the last transfer chunk. If the transfer is 1406 * cyclic, move back to the first one. 1407 */ 1408 if (desc->cyclic) { 1409 desc->running = 1410 list_first_entry(&desc->chunks, 1411 struct rcar_dmac_xfer_chunk, 1412 node); 1413 goto done; 1414 } 1415 } 1416 1417 /* The descriptor is complete, move it to the done list. */ 1418 list_move_tail(&desc->node, &chan->desc.done); 1419 1420 /* Queue the next descriptor, if any. */ 1421 if (!list_empty(&chan->desc.active)) 1422 chan->desc.running = list_first_entry(&chan->desc.active, 1423 struct rcar_dmac_desc, 1424 node); 1425 else 1426 chan->desc.running = NULL; 1427 1428 done: 1429 if (chan->desc.running) 1430 rcar_dmac_chan_start_xfer(chan); 1431 1432 return ret; 1433 } 1434 1435 static irqreturn_t rcar_dmac_isr_channel(int irq, void *dev) 1436 { 1437 u32 mask = RCAR_DMACHCR_DSE | RCAR_DMACHCR_TE; 1438 struct rcar_dmac_chan *chan = dev; 1439 irqreturn_t ret = IRQ_NONE; 1440 u32 chcr; 1441 1442 spin_lock(&chan->lock); 1443 1444 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR); 1445 if (chcr & RCAR_DMACHCR_TE) 1446 mask |= RCAR_DMACHCR_DE; 1447 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr & ~mask); 1448 1449 if (chcr & RCAR_DMACHCR_DSE) 1450 ret |= rcar_dmac_isr_desc_stage_end(chan); 1451 1452 if (chcr & RCAR_DMACHCR_TE) 1453 ret |= rcar_dmac_isr_transfer_end(chan); 1454 1455 spin_unlock(&chan->lock); 1456 1457 return ret; 1458 } 1459 1460 static irqreturn_t rcar_dmac_isr_channel_thread(int irq, void *dev) 1461 { 1462 struct rcar_dmac_chan *chan = dev; 1463 struct rcar_dmac_desc *desc; 1464 struct dmaengine_desc_callback cb; 1465 1466 spin_lock_irq(&chan->lock); 1467 1468 /* For cyclic transfers notify the user after every chunk. */ 1469 if (chan->desc.running && chan->desc.running->cyclic) { 1470 desc = chan->desc.running; 1471 dmaengine_desc_get_callback(&desc->async_tx, &cb); 1472 1473 if (dmaengine_desc_callback_valid(&cb)) { 1474 spin_unlock_irq(&chan->lock); 1475 dmaengine_desc_callback_invoke(&cb, NULL); 1476 spin_lock_irq(&chan->lock); 1477 } 1478 } 1479 1480 /* 1481 * Call the callback function for all descriptors on the done list and 1482 * move them to the ack wait list. 1483 */ 1484 while (!list_empty(&chan->desc.done)) { 1485 desc = list_first_entry(&chan->desc.done, struct rcar_dmac_desc, 1486 node); 1487 dma_cookie_complete(&desc->async_tx); 1488 list_del(&desc->node); 1489 1490 dmaengine_desc_get_callback(&desc->async_tx, &cb); 1491 if (dmaengine_desc_callback_valid(&cb)) { 1492 spin_unlock_irq(&chan->lock); 1493 /* 1494 * We own the only reference to this descriptor, we can 1495 * safely dereference it without holding the channel 1496 * lock. 1497 */ 1498 dmaengine_desc_callback_invoke(&cb, NULL); 1499 spin_lock_irq(&chan->lock); 1500 } 1501 1502 list_add_tail(&desc->node, &chan->desc.wait); 1503 } 1504 1505 spin_unlock_irq(&chan->lock); 1506 1507 /* Recycle all acked descriptors. */ 1508 rcar_dmac_desc_recycle_acked(chan); 1509 1510 return IRQ_HANDLED; 1511 } 1512 1513 static irqreturn_t rcar_dmac_isr_error(int irq, void *data) 1514 { 1515 struct rcar_dmac *dmac = data; 1516 1517 if (!(rcar_dmac_read(dmac, RCAR_DMAOR) & RCAR_DMAOR_AE)) 1518 return IRQ_NONE; 1519 1520 /* 1521 * An unrecoverable error occurred on an unknown channel. Halt the DMAC, 1522 * abort transfers on all channels, and reinitialize the DMAC. 1523 */ 1524 rcar_dmac_stop(dmac); 1525 rcar_dmac_abort(dmac); 1526 rcar_dmac_init(dmac); 1527 1528 return IRQ_HANDLED; 1529 } 1530 1531 /* ----------------------------------------------------------------------------- 1532 * OF xlate and channel filter 1533 */ 1534 1535 static bool rcar_dmac_chan_filter(struct dma_chan *chan, void *arg) 1536 { 1537 struct rcar_dmac *dmac = to_rcar_dmac(chan->device); 1538 struct of_phandle_args *dma_spec = arg; 1539 1540 /* 1541 * FIXME: Using a filter on OF platforms is a nonsense. The OF xlate 1542 * function knows from which device it wants to allocate a channel from, 1543 * and would be perfectly capable of selecting the channel it wants. 1544 * Forcing it to call dma_request_channel() and iterate through all 1545 * channels from all controllers is just pointless. 1546 */ 1547 if (chan->device->device_config != rcar_dmac_device_config || 1548 dma_spec->np != chan->device->dev->of_node) 1549 return false; 1550 1551 return !test_and_set_bit(dma_spec->args[0], dmac->modules); 1552 } 1553 1554 static struct dma_chan *rcar_dmac_of_xlate(struct of_phandle_args *dma_spec, 1555 struct of_dma *ofdma) 1556 { 1557 struct rcar_dmac_chan *rchan; 1558 struct dma_chan *chan; 1559 dma_cap_mask_t mask; 1560 1561 if (dma_spec->args_count != 1) 1562 return NULL; 1563 1564 /* Only slave DMA channels can be allocated via DT */ 1565 dma_cap_zero(mask); 1566 dma_cap_set(DMA_SLAVE, mask); 1567 1568 chan = dma_request_channel(mask, rcar_dmac_chan_filter, dma_spec); 1569 if (!chan) 1570 return NULL; 1571 1572 rchan = to_rcar_dmac_chan(chan); 1573 rchan->mid_rid = dma_spec->args[0]; 1574 1575 return chan; 1576 } 1577 1578 /* ----------------------------------------------------------------------------- 1579 * Power management 1580 */ 1581 1582 #ifdef CONFIG_PM_SLEEP 1583 static int rcar_dmac_sleep_suspend(struct device *dev) 1584 { 1585 /* 1586 * TODO: Wait for the current transfer to complete and stop the device. 1587 */ 1588 return 0; 1589 } 1590 1591 static int rcar_dmac_sleep_resume(struct device *dev) 1592 { 1593 /* TODO: Resume transfers, if any. */ 1594 return 0; 1595 } 1596 #endif 1597 1598 #ifdef CONFIG_PM 1599 static int rcar_dmac_runtime_suspend(struct device *dev) 1600 { 1601 return 0; 1602 } 1603 1604 static int rcar_dmac_runtime_resume(struct device *dev) 1605 { 1606 struct rcar_dmac *dmac = dev_get_drvdata(dev); 1607 1608 return rcar_dmac_init(dmac); 1609 } 1610 #endif 1611 1612 static const struct dev_pm_ops rcar_dmac_pm = { 1613 SET_SYSTEM_SLEEP_PM_OPS(rcar_dmac_sleep_suspend, rcar_dmac_sleep_resume) 1614 SET_RUNTIME_PM_OPS(rcar_dmac_runtime_suspend, rcar_dmac_runtime_resume, 1615 NULL) 1616 }; 1617 1618 /* ----------------------------------------------------------------------------- 1619 * Probe and remove 1620 */ 1621 1622 static int rcar_dmac_chan_probe(struct rcar_dmac *dmac, 1623 struct rcar_dmac_chan *rchan, 1624 unsigned int index) 1625 { 1626 struct platform_device *pdev = to_platform_device(dmac->dev); 1627 struct dma_chan *chan = &rchan->chan; 1628 char pdev_irqname[5]; 1629 char *irqname; 1630 int irq; 1631 int ret; 1632 1633 rchan->index = index; 1634 rchan->iomem = dmac->iomem + RCAR_DMAC_CHAN_OFFSET(index); 1635 rchan->mid_rid = -EINVAL; 1636 1637 spin_lock_init(&rchan->lock); 1638 1639 INIT_LIST_HEAD(&rchan->desc.free); 1640 INIT_LIST_HEAD(&rchan->desc.pending); 1641 INIT_LIST_HEAD(&rchan->desc.active); 1642 INIT_LIST_HEAD(&rchan->desc.done); 1643 INIT_LIST_HEAD(&rchan->desc.wait); 1644 1645 /* Request the channel interrupt. */ 1646 sprintf(pdev_irqname, "ch%u", index); 1647 irq = platform_get_irq_byname(pdev, pdev_irqname); 1648 if (irq < 0) { 1649 dev_err(dmac->dev, "no IRQ specified for channel %u\n", index); 1650 return -ENODEV; 1651 } 1652 1653 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u", 1654 dev_name(dmac->dev), index); 1655 if (!irqname) 1656 return -ENOMEM; 1657 1658 ret = devm_request_threaded_irq(dmac->dev, irq, rcar_dmac_isr_channel, 1659 rcar_dmac_isr_channel_thread, 0, 1660 irqname, rchan); 1661 if (ret) { 1662 dev_err(dmac->dev, "failed to request IRQ %u (%d)\n", irq, ret); 1663 return ret; 1664 } 1665 1666 /* 1667 * Initialize the DMA engine channel and add it to the DMA engine 1668 * channels list. 1669 */ 1670 chan->device = &dmac->engine; 1671 dma_cookie_init(chan); 1672 1673 list_add_tail(&chan->device_node, &dmac->engine.channels); 1674 1675 return 0; 1676 } 1677 1678 static int rcar_dmac_parse_of(struct device *dev, struct rcar_dmac *dmac) 1679 { 1680 struct device_node *np = dev->of_node; 1681 int ret; 1682 1683 ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels); 1684 if (ret < 0) { 1685 dev_err(dev, "unable to read dma-channels property\n"); 1686 return ret; 1687 } 1688 1689 if (dmac->n_channels <= 0 || dmac->n_channels >= 100) { 1690 dev_err(dev, "invalid number of channels %u\n", 1691 dmac->n_channels); 1692 return -EINVAL; 1693 } 1694 1695 return 0; 1696 } 1697 1698 static int rcar_dmac_probe(struct platform_device *pdev) 1699 { 1700 const enum dma_slave_buswidth widths = DMA_SLAVE_BUSWIDTH_1_BYTE | 1701 DMA_SLAVE_BUSWIDTH_2_BYTES | DMA_SLAVE_BUSWIDTH_4_BYTES | 1702 DMA_SLAVE_BUSWIDTH_8_BYTES | DMA_SLAVE_BUSWIDTH_16_BYTES | 1703 DMA_SLAVE_BUSWIDTH_32_BYTES | DMA_SLAVE_BUSWIDTH_64_BYTES; 1704 unsigned int channels_offset = 0; 1705 struct dma_device *engine; 1706 struct rcar_dmac *dmac; 1707 struct resource *mem; 1708 unsigned int i; 1709 char *irqname; 1710 int irq; 1711 int ret; 1712 1713 dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL); 1714 if (!dmac) 1715 return -ENOMEM; 1716 1717 dmac->dev = &pdev->dev; 1718 platform_set_drvdata(pdev, dmac); 1719 1720 ret = rcar_dmac_parse_of(&pdev->dev, dmac); 1721 if (ret < 0) 1722 return ret; 1723 1724 /* 1725 * A still unconfirmed hardware bug prevents the IPMMU microTLB 0 to be 1726 * flushed correctly, resulting in memory corruption. DMAC 0 channel 0 1727 * is connected to microTLB 0 on currently supported platforms, so we 1728 * can't use it with the IPMMU. As the IOMMU API operates at the device 1729 * level we can't disable it selectively, so ignore channel 0 for now if 1730 * the device is part of an IOMMU group. 1731 */ 1732 if (pdev->dev.iommu_group) { 1733 dmac->n_channels--; 1734 channels_offset = 1; 1735 } 1736 1737 dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels, 1738 sizeof(*dmac->channels), GFP_KERNEL); 1739 if (!dmac->channels) 1740 return -ENOMEM; 1741 1742 /* Request resources. */ 1743 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1744 dmac->iomem = devm_ioremap_resource(&pdev->dev, mem); 1745 if (IS_ERR(dmac->iomem)) 1746 return PTR_ERR(dmac->iomem); 1747 1748 irq = platform_get_irq_byname(pdev, "error"); 1749 if (irq < 0) { 1750 dev_err(&pdev->dev, "no error IRQ specified\n"); 1751 return -ENODEV; 1752 } 1753 1754 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:error", 1755 dev_name(dmac->dev)); 1756 if (!irqname) 1757 return -ENOMEM; 1758 1759 ret = devm_request_irq(&pdev->dev, irq, rcar_dmac_isr_error, 0, 1760 irqname, dmac); 1761 if (ret) { 1762 dev_err(&pdev->dev, "failed to request IRQ %u (%d)\n", 1763 irq, ret); 1764 return ret; 1765 } 1766 1767 /* Enable runtime PM and initialize the device. */ 1768 pm_runtime_enable(&pdev->dev); 1769 ret = pm_runtime_get_sync(&pdev->dev); 1770 if (ret < 0) { 1771 dev_err(&pdev->dev, "runtime PM get sync failed (%d)\n", ret); 1772 return ret; 1773 } 1774 1775 ret = rcar_dmac_init(dmac); 1776 pm_runtime_put(&pdev->dev); 1777 1778 if (ret) { 1779 dev_err(&pdev->dev, "failed to reset device\n"); 1780 goto error; 1781 } 1782 1783 /* Initialize the channels. */ 1784 INIT_LIST_HEAD(&dmac->engine.channels); 1785 1786 for (i = 0; i < dmac->n_channels; ++i) { 1787 ret = rcar_dmac_chan_probe(dmac, &dmac->channels[i], 1788 i + channels_offset); 1789 if (ret < 0) 1790 goto error; 1791 } 1792 1793 /* Register the DMAC as a DMA provider for DT. */ 1794 ret = of_dma_controller_register(pdev->dev.of_node, rcar_dmac_of_xlate, 1795 NULL); 1796 if (ret < 0) 1797 goto error; 1798 1799 /* 1800 * Register the DMA engine device. 1801 * 1802 * Default transfer size of 32 bytes requires 32-byte alignment. 1803 */ 1804 engine = &dmac->engine; 1805 dma_cap_set(DMA_MEMCPY, engine->cap_mask); 1806 dma_cap_set(DMA_SLAVE, engine->cap_mask); 1807 1808 engine->dev = &pdev->dev; 1809 engine->copy_align = ilog2(RCAR_DMAC_MEMCPY_XFER_SIZE); 1810 1811 engine->src_addr_widths = widths; 1812 engine->dst_addr_widths = widths; 1813 engine->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM); 1814 engine->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; 1815 1816 engine->device_alloc_chan_resources = rcar_dmac_alloc_chan_resources; 1817 engine->device_free_chan_resources = rcar_dmac_free_chan_resources; 1818 engine->device_prep_dma_memcpy = rcar_dmac_prep_dma_memcpy; 1819 engine->device_prep_slave_sg = rcar_dmac_prep_slave_sg; 1820 engine->device_prep_dma_cyclic = rcar_dmac_prep_dma_cyclic; 1821 engine->device_config = rcar_dmac_device_config; 1822 engine->device_terminate_all = rcar_dmac_chan_terminate_all; 1823 engine->device_tx_status = rcar_dmac_tx_status; 1824 engine->device_issue_pending = rcar_dmac_issue_pending; 1825 1826 ret = dma_async_device_register(engine); 1827 if (ret < 0) 1828 goto error; 1829 1830 return 0; 1831 1832 error: 1833 of_dma_controller_free(pdev->dev.of_node); 1834 pm_runtime_disable(&pdev->dev); 1835 return ret; 1836 } 1837 1838 static int rcar_dmac_remove(struct platform_device *pdev) 1839 { 1840 struct rcar_dmac *dmac = platform_get_drvdata(pdev); 1841 1842 of_dma_controller_free(pdev->dev.of_node); 1843 dma_async_device_unregister(&dmac->engine); 1844 1845 pm_runtime_disable(&pdev->dev); 1846 1847 return 0; 1848 } 1849 1850 static void rcar_dmac_shutdown(struct platform_device *pdev) 1851 { 1852 struct rcar_dmac *dmac = platform_get_drvdata(pdev); 1853 1854 rcar_dmac_stop(dmac); 1855 } 1856 1857 static const struct of_device_id rcar_dmac_of_ids[] = { 1858 { .compatible = "renesas,rcar-dmac", }, 1859 { /* Sentinel */ } 1860 }; 1861 MODULE_DEVICE_TABLE(of, rcar_dmac_of_ids); 1862 1863 static struct platform_driver rcar_dmac_driver = { 1864 .driver = { 1865 .pm = &rcar_dmac_pm, 1866 .name = "rcar-dmac", 1867 .of_match_table = rcar_dmac_of_ids, 1868 }, 1869 .probe = rcar_dmac_probe, 1870 .remove = rcar_dmac_remove, 1871 .shutdown = rcar_dmac_shutdown, 1872 }; 1873 1874 module_platform_driver(rcar_dmac_driver); 1875 1876 MODULE_DESCRIPTION("R-Car Gen2 DMA Controller Driver"); 1877 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); 1878 MODULE_LICENSE("GPL v2"); 1879