PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/dma/pl330.c

http://github.com/mirrors/linux
C | 3268 lines | 2353 code | 626 blank | 289 comment | 352 complexity | a8ebb7ee87d107b5c78302103eda92c0 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Copyright (C) 2010 Samsung Electronics Co. Ltd.
  7. * Jaswinder Singh <jassi.brar@samsung.com>
  8. */
  9. #include <linux/debugfs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/io.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/dmaengine.h>
  20. #include <linux/amba/bus.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/of.h>
  23. #include <linux/of_dma.h>
  24. #include <linux/err.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/bug.h>
  27. #include <linux/reset.h>
  28. #include "dmaengine.h"
  29. #define PL330_MAX_CHAN 8
  30. #define PL330_MAX_IRQS 32
  31. #define PL330_MAX_PERI 32
  32. #define PL330_MAX_BURST 16
  33. #define PL330_QUIRK_BROKEN_NO_FLUSHP BIT(0)
  34. enum pl330_cachectrl {
  35. CCTRL0, /* Noncacheable and nonbufferable */
  36. CCTRL1, /* Bufferable only */
  37. CCTRL2, /* Cacheable, but do not allocate */
  38. CCTRL3, /* Cacheable and bufferable, but do not allocate */
  39. INVALID1, /* AWCACHE = 0x1000 */
  40. INVALID2,
  41. CCTRL6, /* Cacheable write-through, allocate on writes only */
  42. CCTRL7, /* Cacheable write-back, allocate on writes only */
  43. };
  44. enum pl330_byteswap {
  45. SWAP_NO,
  46. SWAP_2,
  47. SWAP_4,
  48. SWAP_8,
  49. SWAP_16,
  50. };
  51. /* Register and Bit field Definitions */
  52. #define DS 0x0
  53. #define DS_ST_STOP 0x0
  54. #define DS_ST_EXEC 0x1
  55. #define DS_ST_CMISS 0x2
  56. #define DS_ST_UPDTPC 0x3
  57. #define DS_ST_WFE 0x4
  58. #define DS_ST_ATBRR 0x5
  59. #define DS_ST_QBUSY 0x6
  60. #define DS_ST_WFP 0x7
  61. #define DS_ST_KILL 0x8
  62. #define DS_ST_CMPLT 0x9
  63. #define DS_ST_FLTCMP 0xe
  64. #define DS_ST_FAULT 0xf
  65. #define DPC 0x4
  66. #define INTEN 0x20
  67. #define ES 0x24
  68. #define INTSTATUS 0x28
  69. #define INTCLR 0x2c
  70. #define FSM 0x30
  71. #define FSC 0x34
  72. #define FTM 0x38
  73. #define _FTC 0x40
  74. #define FTC(n) (_FTC + (n)*0x4)
  75. #define _CS 0x100
  76. #define CS(n) (_CS + (n)*0x8)
  77. #define CS_CNS (1 << 21)
  78. #define _CPC 0x104
  79. #define CPC(n) (_CPC + (n)*0x8)
  80. #define _SA 0x400
  81. #define SA(n) (_SA + (n)*0x20)
  82. #define _DA 0x404
  83. #define DA(n) (_DA + (n)*0x20)
  84. #define _CC 0x408
  85. #define CC(n) (_CC + (n)*0x20)
  86. #define CC_SRCINC (1 << 0)
  87. #define CC_DSTINC (1 << 14)
  88. #define CC_SRCPRI (1 << 8)
  89. #define CC_DSTPRI (1 << 22)
  90. #define CC_SRCNS (1 << 9)
  91. #define CC_DSTNS (1 << 23)
  92. #define CC_SRCIA (1 << 10)
  93. #define CC_DSTIA (1 << 24)
  94. #define CC_SRCBRSTLEN_SHFT 4
  95. #define CC_DSTBRSTLEN_SHFT 18
  96. #define CC_SRCBRSTSIZE_SHFT 1
  97. #define CC_DSTBRSTSIZE_SHFT 15
  98. #define CC_SRCCCTRL_SHFT 11
  99. #define CC_SRCCCTRL_MASK 0x7
  100. #define CC_DSTCCTRL_SHFT 25
  101. #define CC_DRCCCTRL_MASK 0x7
  102. #define CC_SWAP_SHFT 28
  103. #define _LC0 0x40c
  104. #define LC0(n) (_LC0 + (n)*0x20)
  105. #define _LC1 0x410
  106. #define LC1(n) (_LC1 + (n)*0x20)
  107. #define DBGSTATUS 0xd00
  108. #define DBG_BUSY (1 << 0)
  109. #define DBGCMD 0xd04
  110. #define DBGINST0 0xd08
  111. #define DBGINST1 0xd0c
  112. #define CR0 0xe00
  113. #define CR1 0xe04
  114. #define CR2 0xe08
  115. #define CR3 0xe0c
  116. #define CR4 0xe10
  117. #define CRD 0xe14
  118. #define PERIPH_ID 0xfe0
  119. #define PERIPH_REV_SHIFT 20
  120. #define PERIPH_REV_MASK 0xf
  121. #define PERIPH_REV_R0P0 0
  122. #define PERIPH_REV_R1P0 1
  123. #define PERIPH_REV_R1P1 2
  124. #define CR0_PERIPH_REQ_SET (1 << 0)
  125. #define CR0_BOOT_EN_SET (1 << 1)
  126. #define CR0_BOOT_MAN_NS (1 << 2)
  127. #define CR0_NUM_CHANS_SHIFT 4
  128. #define CR0_NUM_CHANS_MASK 0x7
  129. #define CR0_NUM_PERIPH_SHIFT 12
  130. #define CR0_NUM_PERIPH_MASK 0x1f
  131. #define CR0_NUM_EVENTS_SHIFT 17
  132. #define CR0_NUM_EVENTS_MASK 0x1f
  133. #define CR1_ICACHE_LEN_SHIFT 0
  134. #define CR1_ICACHE_LEN_MASK 0x7
  135. #define CR1_NUM_ICACHELINES_SHIFT 4
  136. #define CR1_NUM_ICACHELINES_MASK 0xf
  137. #define CRD_DATA_WIDTH_SHIFT 0
  138. #define CRD_DATA_WIDTH_MASK 0x7
  139. #define CRD_WR_CAP_SHIFT 4
  140. #define CRD_WR_CAP_MASK 0x7
  141. #define CRD_WR_Q_DEP_SHIFT 8
  142. #define CRD_WR_Q_DEP_MASK 0xf
  143. #define CRD_RD_CAP_SHIFT 12
  144. #define CRD_RD_CAP_MASK 0x7
  145. #define CRD_RD_Q_DEP_SHIFT 16
  146. #define CRD_RD_Q_DEP_MASK 0xf
  147. #define CRD_DATA_BUFF_SHIFT 20
  148. #define CRD_DATA_BUFF_MASK 0x3ff
  149. #define PART 0x330
  150. #define DESIGNER 0x41
  151. #define REVISION 0x0
  152. #define INTEG_CFG 0x0
  153. #define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))
  154. #define PL330_STATE_STOPPED (1 << 0)
  155. #define PL330_STATE_EXECUTING (1 << 1)
  156. #define PL330_STATE_WFE (1 << 2)
  157. #define PL330_STATE_FAULTING (1 << 3)
  158. #define PL330_STATE_COMPLETING (1 << 4)
  159. #define PL330_STATE_WFP (1 << 5)
  160. #define PL330_STATE_KILLING (1 << 6)
  161. #define PL330_STATE_FAULT_COMPLETING (1 << 7)
  162. #define PL330_STATE_CACHEMISS (1 << 8)
  163. #define PL330_STATE_UPDTPC (1 << 9)
  164. #define PL330_STATE_ATBARRIER (1 << 10)
  165. #define PL330_STATE_QUEUEBUSY (1 << 11)
  166. #define PL330_STATE_INVALID (1 << 15)
  167. #define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
  168. | PL330_STATE_WFE | PL330_STATE_FAULTING)
  169. #define CMD_DMAADDH 0x54
  170. #define CMD_DMAEND 0x00
  171. #define CMD_DMAFLUSHP 0x35
  172. #define CMD_DMAGO 0xa0
  173. #define CMD_DMALD 0x04
  174. #define CMD_DMALDP 0x25
  175. #define CMD_DMALP 0x20
  176. #define CMD_DMALPEND 0x28
  177. #define CMD_DMAKILL 0x01
  178. #define CMD_DMAMOV 0xbc
  179. #define CMD_DMANOP 0x18
  180. #define CMD_DMARMB 0x12
  181. #define CMD_DMASEV 0x34
  182. #define CMD_DMAST 0x08
  183. #define CMD_DMASTP 0x29
  184. #define CMD_DMASTZ 0x0c
  185. #define CMD_DMAWFE 0x36
  186. #define CMD_DMAWFP 0x30
  187. #define CMD_DMAWMB 0x13
  188. #define SZ_DMAADDH 3
  189. #define SZ_DMAEND 1
  190. #define SZ_DMAFLUSHP 2
  191. #define SZ_DMALD 1
  192. #define SZ_DMALDP 2
  193. #define SZ_DMALP 2
  194. #define SZ_DMALPEND 2
  195. #define SZ_DMAKILL 1
  196. #define SZ_DMAMOV 6
  197. #define SZ_DMANOP 1
  198. #define SZ_DMARMB 1
  199. #define SZ_DMASEV 2
  200. #define SZ_DMAST 1
  201. #define SZ_DMASTP 2
  202. #define SZ_DMASTZ 1
  203. #define SZ_DMAWFE 2
  204. #define SZ_DMAWFP 2
  205. #define SZ_DMAWMB 1
  206. #define SZ_DMAGO 6
  207. #define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
  208. #define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
  209. #define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
  210. #define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
  211. /*
  212. * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
  213. * at 1byte/burst for P<->M and M<->M respectively.
  214. * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
  215. * should be enough for P<->M and M<->M respectively.
  216. */
  217. #define MCODE_BUFF_PER_REQ 256
  218. /* Use this _only_ to wait on transient states */
  219. #define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax();
  220. #ifdef PL330_DEBUG_MCGEN
  221. static unsigned cmd_line;
  222. #define PL330_DBGCMD_DUMP(off, x...) do { \
  223. printk("%x:", cmd_line); \
  224. printk(x); \
  225. cmd_line += off; \
  226. } while (0)
  227. #define PL330_DBGMC_START(addr) (cmd_line = addr)
  228. #else
  229. #define PL330_DBGCMD_DUMP(off, x...) do {} while (0)
  230. #define PL330_DBGMC_START(addr) do {} while (0)
  231. #endif
  232. /* The number of default descriptors */
  233. #define NR_DEFAULT_DESC 16
  234. /* Delay for runtime PM autosuspend, ms */
  235. #define PL330_AUTOSUSPEND_DELAY 20
  236. /* Populated by the PL330 core driver for DMA API driver's info */
  237. struct pl330_config {
  238. u32 periph_id;
  239. #define DMAC_MODE_NS (1 << 0)
  240. unsigned int mode;
  241. unsigned int data_bus_width:10; /* In number of bits */
  242. unsigned int data_buf_dep:11;
  243. unsigned int num_chan:4;
  244. unsigned int num_peri:6;
  245. u32 peri_ns;
  246. unsigned int num_events:6;
  247. u32 irq_ns;
  248. };
  249. /**
  250. * Request Configuration.
  251. * The PL330 core does not modify this and uses the last
  252. * working configuration if the request doesn't provide any.
  253. *
  254. * The Client may want to provide this info only for the
  255. * first request and a request with new settings.
  256. */
  257. struct pl330_reqcfg {
  258. /* Address Incrementing */
  259. unsigned dst_inc:1;
  260. unsigned src_inc:1;
  261. /*
  262. * For now, the SRC & DST protection levels
  263. * and burst size/length are assumed same.
  264. */
  265. bool nonsecure;
  266. bool privileged;
  267. bool insnaccess;
  268. unsigned brst_len:5;
  269. unsigned brst_size:3; /* in power of 2 */
  270. enum pl330_cachectrl dcctl;
  271. enum pl330_cachectrl scctl;
  272. enum pl330_byteswap swap;
  273. struct pl330_config *pcfg;
  274. };
  275. /*
  276. * One cycle of DMAC operation.
  277. * There may be more than one xfer in a request.
  278. */
  279. struct pl330_xfer {
  280. u32 src_addr;
  281. u32 dst_addr;
  282. /* Size to xfer */
  283. u32 bytes;
  284. };
  285. /* The xfer callbacks are made with one of these arguments. */
  286. enum pl330_op_err {
  287. /* The all xfers in the request were success. */
  288. PL330_ERR_NONE,
  289. /* If req aborted due to global error. */
  290. PL330_ERR_ABORT,
  291. /* If req failed due to problem with Channel. */
  292. PL330_ERR_FAIL,
  293. };
  294. enum dmamov_dst {
  295. SAR = 0,
  296. CCR,
  297. DAR,
  298. };
  299. enum pl330_dst {
  300. SRC = 0,
  301. DST,
  302. };
  303. enum pl330_cond {
  304. SINGLE,
  305. BURST,
  306. ALWAYS,
  307. };
  308. struct dma_pl330_desc;
  309. struct _pl330_req {
  310. u32 mc_bus;
  311. void *mc_cpu;
  312. struct dma_pl330_desc *desc;
  313. };
  314. /* ToBeDone for tasklet */
  315. struct _pl330_tbd {
  316. bool reset_dmac;
  317. bool reset_mngr;
  318. u8 reset_chan;
  319. };
  320. /* A DMAC Thread */
  321. struct pl330_thread {
  322. u8 id;
  323. int ev;
  324. /* If the channel is not yet acquired by any client */
  325. bool free;
  326. /* Parent DMAC */
  327. struct pl330_dmac *dmac;
  328. /* Only two at a time */
  329. struct _pl330_req req[2];
  330. /* Index of the last enqueued request */
  331. unsigned lstenq;
  332. /* Index of the last submitted request or -1 if the DMA is stopped */
  333. int req_running;
  334. };
  335. enum pl330_dmac_state {
  336. UNINIT,
  337. INIT,
  338. DYING,
  339. };
  340. enum desc_status {
  341. /* In the DMAC pool */
  342. FREE,
  343. /*
  344. * Allocated to some channel during prep_xxx
  345. * Also may be sitting on the work_list.
  346. */
  347. PREP,
  348. /*
  349. * Sitting on the work_list and already submitted
  350. * to the PL330 core. Not more than two descriptors
  351. * of a channel can be BUSY at any time.
  352. */
  353. BUSY,
  354. /*
  355. * Sitting on the channel work_list but xfer done
  356. * by PL330 core
  357. */
  358. DONE,
  359. };
  360. struct dma_pl330_chan {
  361. /* Schedule desc completion */
  362. struct tasklet_struct task;
  363. /* DMA-Engine Channel */
  364. struct dma_chan chan;
  365. /* List of submitted descriptors */
  366. struct list_head submitted_list;
  367. /* List of issued descriptors */
  368. struct list_head work_list;
  369. /* List of completed descriptors */
  370. struct list_head completed_list;
  371. /* Pointer to the DMAC that manages this channel,
  372. * NULL if the channel is available to be acquired.
  373. * As the parent, this DMAC also provides descriptors
  374. * to the channel.
  375. */
  376. struct pl330_dmac *dmac;
  377. /* To protect channel manipulation */
  378. spinlock_t lock;
  379. /*
  380. * Hardware channel thread of PL330 DMAC. NULL if the channel is
  381. * available.
  382. */
  383. struct pl330_thread *thread;
  384. /* For D-to-M and M-to-D channels */
  385. int burst_sz; /* the peripheral fifo width */
  386. int burst_len; /* the number of burst */
  387. phys_addr_t fifo_addr;
  388. /* DMA-mapped view of the FIFO; may differ if an IOMMU is present */
  389. dma_addr_t fifo_dma;
  390. enum dma_data_direction dir;
  391. struct dma_slave_config slave_config;
  392. /* for cyclic capability */
  393. bool cyclic;
  394. /* for runtime pm tracking */
  395. bool active;
  396. };
  397. struct pl330_dmac {
  398. /* DMA-Engine Device */
  399. struct dma_device ddma;
  400. /* Holds info about sg limitations */
  401. struct device_dma_parameters dma_parms;
  402. /* Pool of descriptors available for the DMAC's channels */
  403. struct list_head desc_pool;
  404. /* To protect desc_pool manipulation */
  405. spinlock_t pool_lock;
  406. /* Size of MicroCode buffers for each channel. */
  407. unsigned mcbufsz;
  408. /* ioremap'ed address of PL330 registers. */
  409. void __iomem *base;
  410. /* Populated by the PL330 core driver during pl330_add */
  411. struct pl330_config pcfg;
  412. spinlock_t lock;
  413. /* Maximum possible events/irqs */
  414. int events[32];
  415. /* BUS address of MicroCode buffer */
  416. dma_addr_t mcode_bus;
  417. /* CPU address of MicroCode buffer */
  418. void *mcode_cpu;
  419. /* List of all Channel threads */
  420. struct pl330_thread *channels;
  421. /* Pointer to the MANAGER thread */
  422. struct pl330_thread *manager;
  423. /* To handle bad news in interrupt */
  424. struct tasklet_struct tasks;
  425. struct _pl330_tbd dmac_tbd;
  426. /* State of DMAC operation */
  427. enum pl330_dmac_state state;
  428. /* Holds list of reqs with due callbacks */
  429. struct list_head req_done;
  430. /* Peripheral channels connected to this DMAC */
  431. unsigned int num_peripherals;
  432. struct dma_pl330_chan *peripherals; /* keep at end */
  433. int quirks;
  434. struct reset_control *rstc;
  435. struct reset_control *rstc_ocp;
  436. };
  437. static struct pl330_of_quirks {
  438. char *quirk;
  439. int id;
  440. } of_quirks[] = {
  441. {
  442. .quirk = "arm,pl330-broken-no-flushp",
  443. .id = PL330_QUIRK_BROKEN_NO_FLUSHP,
  444. }
  445. };
  446. struct dma_pl330_desc {
  447. /* To attach to a queue as child */
  448. struct list_head node;
  449. /* Descriptor for the DMA Engine API */
  450. struct dma_async_tx_descriptor txd;
  451. /* Xfer for PL330 core */
  452. struct pl330_xfer px;
  453. struct pl330_reqcfg rqcfg;
  454. enum desc_status status;
  455. int bytes_requested;
  456. bool last;
  457. /* The channel which currently holds this desc */
  458. struct dma_pl330_chan *pchan;
  459. enum dma_transfer_direction rqtype;
  460. /* Index of peripheral for the xfer. */
  461. unsigned peri:5;
  462. /* Hook to attach to DMAC's list of reqs with due callback */
  463. struct list_head rqd;
  464. };
  465. struct _xfer_spec {
  466. u32 ccr;
  467. struct dma_pl330_desc *desc;
  468. };
  469. static int pl330_config_write(struct dma_chan *chan,
  470. struct dma_slave_config *slave_config,
  471. enum dma_transfer_direction direction);
  472. static inline bool _queue_full(struct pl330_thread *thrd)
  473. {
  474. return thrd->req[0].desc != NULL && thrd->req[1].desc != NULL;
  475. }
  476. static inline bool is_manager(struct pl330_thread *thrd)
  477. {
  478. return thrd->dmac->manager == thrd;
  479. }
  480. /* If manager of the thread is in Non-Secure mode */
  481. static inline bool _manager_ns(struct pl330_thread *thrd)
  482. {
  483. return (thrd->dmac->pcfg.mode & DMAC_MODE_NS) ? true : false;
  484. }
  485. static inline u32 get_revision(u32 periph_id)
  486. {
  487. return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
  488. }
  489. static inline u32 _emit_END(unsigned dry_run, u8 buf[])
  490. {
  491. if (dry_run)
  492. return SZ_DMAEND;
  493. buf[0] = CMD_DMAEND;
  494. PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
  495. return SZ_DMAEND;
  496. }
  497. static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
  498. {
  499. if (dry_run)
  500. return SZ_DMAFLUSHP;
  501. buf[0] = CMD_DMAFLUSHP;
  502. peri &= 0x1f;
  503. peri <<= 3;
  504. buf[1] = peri;
  505. PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
  506. return SZ_DMAFLUSHP;
  507. }
  508. static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
  509. {
  510. if (dry_run)
  511. return SZ_DMALD;
  512. buf[0] = CMD_DMALD;
  513. if (cond == SINGLE)
  514. buf[0] |= (0 << 1) | (1 << 0);
  515. else if (cond == BURST)
  516. buf[0] |= (1 << 1) | (1 << 0);
  517. PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
  518. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
  519. return SZ_DMALD;
  520. }
  521. static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
  522. enum pl330_cond cond, u8 peri)
  523. {
  524. if (dry_run)
  525. return SZ_DMALDP;
  526. buf[0] = CMD_DMALDP;
  527. if (cond == BURST)
  528. buf[0] |= (1 << 1);
  529. peri &= 0x1f;
  530. peri <<= 3;
  531. buf[1] = peri;
  532. PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
  533. cond == SINGLE ? 'S' : 'B', peri >> 3);
  534. return SZ_DMALDP;
  535. }
  536. static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
  537. unsigned loop, u8 cnt)
  538. {
  539. if (dry_run)
  540. return SZ_DMALP;
  541. buf[0] = CMD_DMALP;
  542. if (loop)
  543. buf[0] |= (1 << 1);
  544. cnt--; /* DMAC increments by 1 internally */
  545. buf[1] = cnt;
  546. PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
  547. return SZ_DMALP;
  548. }
  549. struct _arg_LPEND {
  550. enum pl330_cond cond;
  551. bool forever;
  552. unsigned loop;
  553. u8 bjump;
  554. };
  555. static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
  556. const struct _arg_LPEND *arg)
  557. {
  558. enum pl330_cond cond = arg->cond;
  559. bool forever = arg->forever;
  560. unsigned loop = arg->loop;
  561. u8 bjump = arg->bjump;
  562. if (dry_run)
  563. return SZ_DMALPEND;
  564. buf[0] = CMD_DMALPEND;
  565. if (loop)
  566. buf[0] |= (1 << 2);
  567. if (!forever)
  568. buf[0] |= (1 << 4);
  569. if (cond == SINGLE)
  570. buf[0] |= (0 << 1) | (1 << 0);
  571. else if (cond == BURST)
  572. buf[0] |= (1 << 1) | (1 << 0);
  573. buf[1] = bjump;
  574. PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
  575. forever ? "FE" : "END",
  576. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
  577. loop ? '1' : '0',
  578. bjump);
  579. return SZ_DMALPEND;
  580. }
  581. static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
  582. {
  583. if (dry_run)
  584. return SZ_DMAKILL;
  585. buf[0] = CMD_DMAKILL;
  586. return SZ_DMAKILL;
  587. }
  588. static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
  589. enum dmamov_dst dst, u32 val)
  590. {
  591. if (dry_run)
  592. return SZ_DMAMOV;
  593. buf[0] = CMD_DMAMOV;
  594. buf[1] = dst;
  595. buf[2] = val;
  596. buf[3] = val >> 8;
  597. buf[4] = val >> 16;
  598. buf[5] = val >> 24;
  599. PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
  600. dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
  601. return SZ_DMAMOV;
  602. }
  603. static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
  604. {
  605. if (dry_run)
  606. return SZ_DMARMB;
  607. buf[0] = CMD_DMARMB;
  608. PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
  609. return SZ_DMARMB;
  610. }
  611. static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
  612. {
  613. if (dry_run)
  614. return SZ_DMASEV;
  615. buf[0] = CMD_DMASEV;
  616. ev &= 0x1f;
  617. ev <<= 3;
  618. buf[1] = ev;
  619. PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
  620. return SZ_DMASEV;
  621. }
  622. static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
  623. {
  624. if (dry_run)
  625. return SZ_DMAST;
  626. buf[0] = CMD_DMAST;
  627. if (cond == SINGLE)
  628. buf[0] |= (0 << 1) | (1 << 0);
  629. else if (cond == BURST)
  630. buf[0] |= (1 << 1) | (1 << 0);
  631. PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
  632. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
  633. return SZ_DMAST;
  634. }
  635. static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
  636. enum pl330_cond cond, u8 peri)
  637. {
  638. if (dry_run)
  639. return SZ_DMASTP;
  640. buf[0] = CMD_DMASTP;
  641. if (cond == BURST)
  642. buf[0] |= (1 << 1);
  643. peri &= 0x1f;
  644. peri <<= 3;
  645. buf[1] = peri;
  646. PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
  647. cond == SINGLE ? 'S' : 'B', peri >> 3);
  648. return SZ_DMASTP;
  649. }
  650. static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
  651. enum pl330_cond cond, u8 peri)
  652. {
  653. if (dry_run)
  654. return SZ_DMAWFP;
  655. buf[0] = CMD_DMAWFP;
  656. if (cond == SINGLE)
  657. buf[0] |= (0 << 1) | (0 << 0);
  658. else if (cond == BURST)
  659. buf[0] |= (1 << 1) | (0 << 0);
  660. else
  661. buf[0] |= (0 << 1) | (1 << 0);
  662. peri &= 0x1f;
  663. peri <<= 3;
  664. buf[1] = peri;
  665. PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
  666. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
  667. return SZ_DMAWFP;
  668. }
  669. static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
  670. {
  671. if (dry_run)
  672. return SZ_DMAWMB;
  673. buf[0] = CMD_DMAWMB;
  674. PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
  675. return SZ_DMAWMB;
  676. }
  677. struct _arg_GO {
  678. u8 chan;
  679. u32 addr;
  680. unsigned ns;
  681. };
  682. static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
  683. const struct _arg_GO *arg)
  684. {
  685. u8 chan = arg->chan;
  686. u32 addr = arg->addr;
  687. unsigned ns = arg->ns;
  688. if (dry_run)
  689. return SZ_DMAGO;
  690. buf[0] = CMD_DMAGO;
  691. buf[0] |= (ns << 1);
  692. buf[1] = chan & 0x7;
  693. buf[2] = addr;
  694. buf[3] = addr >> 8;
  695. buf[4] = addr >> 16;
  696. buf[5] = addr >> 24;
  697. return SZ_DMAGO;
  698. }
  699. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  700. /* Returns Time-Out */
  701. static bool _until_dmac_idle(struct pl330_thread *thrd)
  702. {
  703. void __iomem *regs = thrd->dmac->base;
  704. unsigned long loops = msecs_to_loops(5);
  705. do {
  706. /* Until Manager is Idle */
  707. if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
  708. break;
  709. cpu_relax();
  710. } while (--loops);
  711. if (!loops)
  712. return true;
  713. return false;
  714. }
  715. static inline void _execute_DBGINSN(struct pl330_thread *thrd,
  716. u8 insn[], bool as_manager)
  717. {
  718. void __iomem *regs = thrd->dmac->base;
  719. u32 val;
  720. val = (insn[0] << 16) | (insn[1] << 24);
  721. if (!as_manager) {
  722. val |= (1 << 0);
  723. val |= (thrd->id << 8); /* Channel Number */
  724. }
  725. writel(val, regs + DBGINST0);
  726. val = le32_to_cpu(*((__le32 *)&insn[2]));
  727. writel(val, regs + DBGINST1);
  728. /* If timed out due to halted state-machine */
  729. if (_until_dmac_idle(thrd)) {
  730. dev_err(thrd->dmac->ddma.dev, "DMAC halted!\n");
  731. return;
  732. }
  733. /* Get going */
  734. writel(0, regs + DBGCMD);
  735. }
  736. static inline u32 _state(struct pl330_thread *thrd)
  737. {
  738. void __iomem *regs = thrd->dmac->base;
  739. u32 val;
  740. if (is_manager(thrd))
  741. val = readl(regs + DS) & 0xf;
  742. else
  743. val = readl(regs + CS(thrd->id)) & 0xf;
  744. switch (val) {
  745. case DS_ST_STOP:
  746. return PL330_STATE_STOPPED;
  747. case DS_ST_EXEC:
  748. return PL330_STATE_EXECUTING;
  749. case DS_ST_CMISS:
  750. return PL330_STATE_CACHEMISS;
  751. case DS_ST_UPDTPC:
  752. return PL330_STATE_UPDTPC;
  753. case DS_ST_WFE:
  754. return PL330_STATE_WFE;
  755. case DS_ST_FAULT:
  756. return PL330_STATE_FAULTING;
  757. case DS_ST_ATBRR:
  758. if (is_manager(thrd))
  759. return PL330_STATE_INVALID;
  760. else
  761. return PL330_STATE_ATBARRIER;
  762. case DS_ST_QBUSY:
  763. if (is_manager(thrd))
  764. return PL330_STATE_INVALID;
  765. else
  766. return PL330_STATE_QUEUEBUSY;
  767. case DS_ST_WFP:
  768. if (is_manager(thrd))
  769. return PL330_STATE_INVALID;
  770. else
  771. return PL330_STATE_WFP;
  772. case DS_ST_KILL:
  773. if (is_manager(thrd))
  774. return PL330_STATE_INVALID;
  775. else
  776. return PL330_STATE_KILLING;
  777. case DS_ST_CMPLT:
  778. if (is_manager(thrd))
  779. return PL330_STATE_INVALID;
  780. else
  781. return PL330_STATE_COMPLETING;
  782. case DS_ST_FLTCMP:
  783. if (is_manager(thrd))
  784. return PL330_STATE_INVALID;
  785. else
  786. return PL330_STATE_FAULT_COMPLETING;
  787. default:
  788. return PL330_STATE_INVALID;
  789. }
  790. }
  791. static void _stop(struct pl330_thread *thrd)
  792. {
  793. void __iomem *regs = thrd->dmac->base;
  794. u8 insn[6] = {0, 0, 0, 0, 0, 0};
  795. u32 inten = readl(regs + INTEN);
  796. if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
  797. UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
  798. /* Return if nothing needs to be done */
  799. if (_state(thrd) == PL330_STATE_COMPLETING
  800. || _state(thrd) == PL330_STATE_KILLING
  801. || _state(thrd) == PL330_STATE_STOPPED)
  802. return;
  803. _emit_KILL(0, insn);
  804. _execute_DBGINSN(thrd, insn, is_manager(thrd));
  805. /* clear the event */
  806. if (inten & (1 << thrd->ev))
  807. writel(1 << thrd->ev, regs + INTCLR);
  808. /* Stop generating interrupts for SEV */
  809. writel(inten & ~(1 << thrd->ev), regs + INTEN);
  810. }
  811. /* Start doing req 'idx' of thread 'thrd' */
  812. static bool _trigger(struct pl330_thread *thrd)
  813. {
  814. void __iomem *regs = thrd->dmac->base;
  815. struct _pl330_req *req;
  816. struct dma_pl330_desc *desc;
  817. struct _arg_GO go;
  818. unsigned ns;
  819. u8 insn[6] = {0, 0, 0, 0, 0, 0};
  820. int idx;
  821. /* Return if already ACTIVE */
  822. if (_state(thrd) != PL330_STATE_STOPPED)
  823. return true;
  824. idx = 1 - thrd->lstenq;
  825. if (thrd->req[idx].desc != NULL) {
  826. req = &thrd->req[idx];
  827. } else {
  828. idx = thrd->lstenq;
  829. if (thrd->req[idx].desc != NULL)
  830. req = &thrd->req[idx];
  831. else
  832. req = NULL;
  833. }
  834. /* Return if no request */
  835. if (!req)
  836. return true;
  837. /* Return if req is running */
  838. if (idx == thrd->req_running)
  839. return true;
  840. desc = req->desc;
  841. ns = desc->rqcfg.nonsecure ? 1 : 0;
  842. /* See 'Abort Sources' point-4 at Page 2-25 */
  843. if (_manager_ns(thrd) && !ns)
  844. dev_info(thrd->dmac->ddma.dev, "%s:%d Recipe for ABORT!\n",
  845. __func__, __LINE__);
  846. go.chan = thrd->id;
  847. go.addr = req->mc_bus;
  848. go.ns = ns;
  849. _emit_GO(0, insn, &go);
  850. /* Set to generate interrupts for SEV */
  851. writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
  852. /* Only manager can execute GO */
  853. _execute_DBGINSN(thrd, insn, true);
  854. thrd->req_running = idx;
  855. return true;
  856. }
  857. static bool _start(struct pl330_thread *thrd)
  858. {
  859. switch (_state(thrd)) {
  860. case PL330_STATE_FAULT_COMPLETING:
  861. UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
  862. if (_state(thrd) == PL330_STATE_KILLING)
  863. UNTIL(thrd, PL330_STATE_STOPPED)
  864. /* fall through */
  865. case PL330_STATE_FAULTING:
  866. _stop(thrd);
  867. /* fall through */
  868. case PL330_STATE_KILLING:
  869. case PL330_STATE_COMPLETING:
  870. UNTIL(thrd, PL330_STATE_STOPPED)
  871. /* fall through */
  872. case PL330_STATE_STOPPED:
  873. return _trigger(thrd);
  874. case PL330_STATE_WFP:
  875. case PL330_STATE_QUEUEBUSY:
  876. case PL330_STATE_ATBARRIER:
  877. case PL330_STATE_UPDTPC:
  878. case PL330_STATE_CACHEMISS:
  879. case PL330_STATE_EXECUTING:
  880. return true;
  881. case PL330_STATE_WFE: /* For RESUME, nothing yet */
  882. default:
  883. return false;
  884. }
  885. }
  886. static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
  887. const struct _xfer_spec *pxs, int cyc)
  888. {
  889. int off = 0;
  890. struct pl330_config *pcfg = pxs->desc->rqcfg.pcfg;
  891. /* check lock-up free version */
  892. if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
  893. while (cyc--) {
  894. off += _emit_LD(dry_run, &buf[off], ALWAYS);
  895. off += _emit_ST(dry_run, &buf[off], ALWAYS);
  896. }
  897. } else {
  898. while (cyc--) {
  899. off += _emit_LD(dry_run, &buf[off], ALWAYS);
  900. off += _emit_RMB(dry_run, &buf[off]);
  901. off += _emit_ST(dry_run, &buf[off], ALWAYS);
  902. off += _emit_WMB(dry_run, &buf[off]);
  903. }
  904. }
  905. return off;
  906. }
  907. static u32 _emit_load(unsigned int dry_run, u8 buf[],
  908. enum pl330_cond cond, enum dma_transfer_direction direction,
  909. u8 peri)
  910. {
  911. int off = 0;
  912. switch (direction) {
  913. case DMA_MEM_TO_MEM:
  914. /* fall through */
  915. case DMA_MEM_TO_DEV:
  916. off += _emit_LD(dry_run, &buf[off], cond);
  917. break;
  918. case DMA_DEV_TO_MEM:
  919. if (cond == ALWAYS) {
  920. off += _emit_LDP(dry_run, &buf[off], SINGLE,
  921. peri);
  922. off += _emit_LDP(dry_run, &buf[off], BURST,
  923. peri);
  924. } else {
  925. off += _emit_LDP(dry_run, &buf[off], cond,
  926. peri);
  927. }
  928. break;
  929. default:
  930. /* this code should be unreachable */
  931. WARN_ON(1);
  932. break;
  933. }
  934. return off;
  935. }
  936. static inline u32 _emit_store(unsigned int dry_run, u8 buf[],
  937. enum pl330_cond cond, enum dma_transfer_direction direction,
  938. u8 peri)
  939. {
  940. int off = 0;
  941. switch (direction) {
  942. case DMA_MEM_TO_MEM:
  943. /* fall through */
  944. case DMA_DEV_TO_MEM:
  945. off += _emit_ST(dry_run, &buf[off], cond);
  946. break;
  947. case DMA_MEM_TO_DEV:
  948. if (cond == ALWAYS) {
  949. off += _emit_STP(dry_run, &buf[off], SINGLE,
  950. peri);
  951. off += _emit_STP(dry_run, &buf[off], BURST,
  952. peri);
  953. } else {
  954. off += _emit_STP(dry_run, &buf[off], cond,
  955. peri);
  956. }
  957. break;
  958. default:
  959. /* this code should be unreachable */
  960. WARN_ON(1);
  961. break;
  962. }
  963. return off;
  964. }
  965. static inline int _ldst_peripheral(struct pl330_dmac *pl330,
  966. unsigned dry_run, u8 buf[],
  967. const struct _xfer_spec *pxs, int cyc,
  968. enum pl330_cond cond)
  969. {
  970. int off = 0;
  971. if (pl330->quirks & PL330_QUIRK_BROKEN_NO_FLUSHP)
  972. cond = BURST;
  973. /*
  974. * do FLUSHP at beginning to clear any stale dma requests before the
  975. * first WFP.
  976. */
  977. if (!(pl330->quirks & PL330_QUIRK_BROKEN_NO_FLUSHP))
  978. off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
  979. while (cyc--) {
  980. off += _emit_WFP(dry_run, &buf[off], cond, pxs->desc->peri);
  981. off += _emit_load(dry_run, &buf[off], cond, pxs->desc->rqtype,
  982. pxs->desc->peri);
  983. off += _emit_store(dry_run, &buf[off], cond, pxs->desc->rqtype,
  984. pxs->desc->peri);
  985. }
  986. return off;
  987. }
  988. static int _bursts(struct pl330_dmac *pl330, unsigned dry_run, u8 buf[],
  989. const struct _xfer_spec *pxs, int cyc)
  990. {
  991. int off = 0;
  992. enum pl330_cond cond = BRST_LEN(pxs->ccr) > 1 ? BURST : SINGLE;
  993. switch (pxs->desc->rqtype) {
  994. case DMA_MEM_TO_DEV:
  995. /* fall through */
  996. case DMA_DEV_TO_MEM:
  997. off += _ldst_peripheral(pl330, dry_run, &buf[off], pxs, cyc,
  998. cond);
  999. break;
  1000. case DMA_MEM_TO_MEM:
  1001. off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
  1002. break;
  1003. default:
  1004. /* this code should be unreachable */
  1005. WARN_ON(1);
  1006. break;
  1007. }
  1008. return off;
  1009. }
  1010. /*
  1011. * transfer dregs with single transfers to peripheral, or a reduced size burst
  1012. * for mem-to-mem.
  1013. */
  1014. static int _dregs(struct pl330_dmac *pl330, unsigned int dry_run, u8 buf[],
  1015. const struct _xfer_spec *pxs, int transfer_length)
  1016. {
  1017. int off = 0;
  1018. int dregs_ccr;
  1019. if (transfer_length == 0)
  1020. return off;
  1021. switch (pxs->desc->rqtype) {
  1022. case DMA_MEM_TO_DEV:
  1023. /* fall through */
  1024. case DMA_DEV_TO_MEM:
  1025. off += _ldst_peripheral(pl330, dry_run, &buf[off], pxs,
  1026. transfer_length, SINGLE);
  1027. break;
  1028. case DMA_MEM_TO_MEM:
  1029. dregs_ccr = pxs->ccr;
  1030. dregs_ccr &= ~((0xf << CC_SRCBRSTLEN_SHFT) |
  1031. (0xf << CC_DSTBRSTLEN_SHFT));
  1032. dregs_ccr |= (((transfer_length - 1) & 0xf) <<
  1033. CC_SRCBRSTLEN_SHFT);
  1034. dregs_ccr |= (((transfer_length - 1) & 0xf) <<
  1035. CC_DSTBRSTLEN_SHFT);
  1036. off += _emit_MOV(dry_run, &buf[off], CCR, dregs_ccr);
  1037. off += _ldst_memtomem(dry_run, &buf[off], pxs, 1);
  1038. break;
  1039. default:
  1040. /* this code should be unreachable */
  1041. WARN_ON(1);
  1042. break;
  1043. }
  1044. return off;
  1045. }
  1046. /* Returns bytes consumed and updates bursts */
  1047. static inline int _loop(struct pl330_dmac *pl330, unsigned dry_run, u8 buf[],
  1048. unsigned long *bursts, const struct _xfer_spec *pxs)
  1049. {
  1050. int cyc, cycmax, szlp, szlpend, szbrst, off;
  1051. unsigned lcnt0, lcnt1, ljmp0, ljmp1;
  1052. struct _arg_LPEND lpend;
  1053. if (*bursts == 1)
  1054. return _bursts(pl330, dry_run, buf, pxs, 1);
  1055. /* Max iterations possible in DMALP is 256 */
  1056. if (*bursts >= 256*256) {
  1057. lcnt1 = 256;
  1058. lcnt0 = 256;
  1059. cyc = *bursts / lcnt1 / lcnt0;
  1060. } else if (*bursts > 256) {
  1061. lcnt1 = 256;
  1062. lcnt0 = *bursts / lcnt1;
  1063. cyc = 1;
  1064. } else {
  1065. lcnt1 = *bursts;
  1066. lcnt0 = 0;
  1067. cyc = 1;
  1068. }
  1069. szlp = _emit_LP(1, buf, 0, 0);
  1070. szbrst = _bursts(pl330, 1, buf, pxs, 1);
  1071. lpend.cond = ALWAYS;
  1072. lpend.forever = false;
  1073. lpend.loop = 0;
  1074. lpend.bjump = 0;
  1075. szlpend = _emit_LPEND(1, buf, &lpend);
  1076. if (lcnt0) {
  1077. szlp *= 2;
  1078. szlpend *= 2;
  1079. }
  1080. /*
  1081. * Max bursts that we can unroll due to limit on the
  1082. * size of backward jump that can be encoded in DMALPEND
  1083. * which is 8-bits and hence 255
  1084. */
  1085. cycmax = (255 - (szlp + szlpend)) / szbrst;
  1086. cyc = (cycmax < cyc) ? cycmax : cyc;
  1087. off = 0;
  1088. if (lcnt0) {
  1089. off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
  1090. ljmp0 = off;
  1091. }
  1092. off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
  1093. ljmp1 = off;
  1094. off += _bursts(pl330, dry_run, &buf[off], pxs, cyc);
  1095. lpend.cond = ALWAYS;
  1096. lpend.forever = false;
  1097. lpend.loop = 1;
  1098. lpend.bjump = off - ljmp1;
  1099. off += _emit_LPEND(dry_run, &buf[off], &lpend);
  1100. if (lcnt0) {
  1101. lpend.cond = ALWAYS;
  1102. lpend.forever = false;
  1103. lpend.loop = 0;
  1104. lpend.bjump = off - ljmp0;
  1105. off += _emit_LPEND(dry_run, &buf[off], &lpend);
  1106. }
  1107. *bursts = lcnt1 * cyc;
  1108. if (lcnt0)
  1109. *bursts *= lcnt0;
  1110. return off;
  1111. }
  1112. static inline int _setup_loops(struct pl330_dmac *pl330,
  1113. unsigned dry_run, u8 buf[],
  1114. const struct _xfer_spec *pxs)
  1115. {
  1116. struct pl330_xfer *x = &pxs->desc->px;
  1117. u32 ccr = pxs->ccr;
  1118. unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
  1119. int num_dregs = (x->bytes - BURST_TO_BYTE(bursts, ccr)) /
  1120. BRST_SIZE(ccr);
  1121. int off = 0;
  1122. while (bursts) {
  1123. c = bursts;
  1124. off += _loop(pl330, dry_run, &buf[off], &c, pxs);
  1125. bursts -= c;
  1126. }
  1127. off += _dregs(pl330, dry_run, &buf[off], pxs, num_dregs);
  1128. return off;
  1129. }
  1130. static inline int _setup_xfer(struct pl330_dmac *pl330,
  1131. unsigned dry_run, u8 buf[],
  1132. const struct _xfer_spec *pxs)
  1133. {
  1134. struct pl330_xfer *x = &pxs->desc->px;
  1135. int off = 0;
  1136. /* DMAMOV SAR, x->src_addr */
  1137. off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
  1138. /* DMAMOV DAR, x->dst_addr */
  1139. off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
  1140. /* Setup Loop(s) */
  1141. off += _setup_loops(pl330, dry_run, &buf[off], pxs);
  1142. return off;
  1143. }
  1144. /*
  1145. * A req is a sequence of one or more xfer units.
  1146. * Returns the number of bytes taken to setup the MC for the req.
  1147. */
  1148. static int _setup_req(struct pl330_dmac *pl330, unsigned dry_run,
  1149. struct pl330_thread *thrd, unsigned index,
  1150. struct _xfer_spec *pxs)
  1151. {
  1152. struct _pl330_req *req = &thrd->req[index];
  1153. u8 *buf = req->mc_cpu;
  1154. int off = 0;
  1155. PL330_DBGMC_START(req->mc_bus);
  1156. /* DMAMOV CCR, ccr */
  1157. off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
  1158. off += _setup_xfer(pl330, dry_run, &buf[off], pxs);
  1159. /* DMASEV peripheral/event */
  1160. off += _emit_SEV(dry_run, &buf[off], thrd->ev);
  1161. /* DMAEND */
  1162. off += _emit_END(dry_run, &buf[off]);
  1163. return off;
  1164. }
  1165. static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
  1166. {
  1167. u32 ccr = 0;
  1168. if (rqc->src_inc)
  1169. ccr |= CC_SRCINC;
  1170. if (rqc->dst_inc)
  1171. ccr |= CC_DSTINC;
  1172. /* We set same protection levels for Src and DST for now */
  1173. if (rqc->privileged)
  1174. ccr |= CC_SRCPRI | CC_DSTPRI;
  1175. if (rqc->nonsecure)
  1176. ccr |= CC_SRCNS | CC_DSTNS;
  1177. if (rqc->insnaccess)
  1178. ccr |= CC_SRCIA | CC_DSTIA;
  1179. ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
  1180. ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
  1181. ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
  1182. ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
  1183. ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
  1184. ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
  1185. ccr |= (rqc->swap << CC_SWAP_SHFT);
  1186. return ccr;
  1187. }
  1188. /*
  1189. * Submit a list of xfers after which the client wants notification.
  1190. * Client is not notified after each xfer unit, just once after all
  1191. * xfer units are done or some error occurs.
  1192. */
  1193. static int pl330_submit_req(struct pl330_thread *thrd,
  1194. struct dma_pl330_desc *desc)
  1195. {
  1196. struct pl330_dmac *pl330 = thrd->dmac;
  1197. struct _xfer_spec xs;
  1198. unsigned long flags;
  1199. unsigned idx;
  1200. u32 ccr;
  1201. int ret = 0;
  1202. switch (desc->rqtype) {
  1203. case DMA_MEM_TO_DEV:
  1204. break;
  1205. case DMA_DEV_TO_MEM:
  1206. break;
  1207. case DMA_MEM_TO_MEM:
  1208. break;
  1209. default:
  1210. return -ENOTSUPP;
  1211. }
  1212. if (pl330->state == DYING
  1213. || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
  1214. dev_info(thrd->dmac->ddma.dev, "%s:%d\n",
  1215. __func__, __LINE__);
  1216. return -EAGAIN;
  1217. }
  1218. /* If request for non-existing peripheral */
  1219. if (desc->rqtype != DMA_MEM_TO_MEM &&
  1220. desc->peri >= pl330->pcfg.num_peri) {
  1221. dev_info(thrd->dmac->ddma.dev,
  1222. "%s:%d Invalid peripheral(%u)!\n",
  1223. __func__, __LINE__, desc->peri);
  1224. return -EINVAL;
  1225. }
  1226. spin_lock_irqsave(&pl330->lock, flags);
  1227. if (_queue_full(thrd)) {
  1228. ret = -EAGAIN;
  1229. goto xfer_exit;
  1230. }
  1231. /* Prefer Secure Channel */
  1232. if (!_manager_ns(thrd))
  1233. desc->rqcfg.nonsecure = 0;
  1234. else
  1235. desc->rqcfg.nonsecure = 1;
  1236. ccr = _prepare_ccr(&desc->rqcfg);
  1237. idx = thrd->req[0].desc == NULL ? 0 : 1;
  1238. xs.ccr = ccr;
  1239. xs.desc = desc;
  1240. /* First dry run to check if req is acceptable */
  1241. ret = _setup_req(pl330, 1, thrd, idx, &xs);
  1242. if (ret < 0)
  1243. goto xfer_exit;
  1244. if (ret > pl330->mcbufsz / 2) {
  1245. dev_info(pl330->ddma.dev, "%s:%d Try increasing mcbufsz (%i/%i)\n",
  1246. __func__, __LINE__, ret, pl330->mcbufsz / 2);
  1247. ret = -ENOMEM;
  1248. goto xfer_exit;
  1249. }
  1250. /* Hook the request */
  1251. thrd->lstenq = idx;
  1252. thrd->req[idx].desc = desc;
  1253. _setup_req(pl330, 0, thrd, idx, &xs);
  1254. ret = 0;
  1255. xfer_exit:
  1256. spin_unlock_irqrestore(&pl330->lock, flags);
  1257. return ret;
  1258. }
  1259. static void dma_pl330_rqcb(struct dma_pl330_desc *desc, enum pl330_op_err err)
  1260. {
  1261. struct dma_pl330_chan *pch;
  1262. unsigned long flags;
  1263. if (!desc)
  1264. return;
  1265. pch = desc->pchan;
  1266. /* If desc aborted */
  1267. if (!pch)
  1268. return;
  1269. spin_lock_irqsave(&pch->lock, flags);
  1270. desc->status = DONE;
  1271. spin_unlock_irqrestore(&pch->lock, flags);
  1272. tasklet_schedule(&pch->task);
  1273. }
  1274. static void pl330_dotask(unsigned long data)
  1275. {
  1276. struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
  1277. unsigned long flags;
  1278. int i;
  1279. spin_lock_irqsave(&pl330->lock, flags);
  1280. /* The DMAC itself gone nuts */
  1281. if (pl330->dmac_tbd.reset_dmac) {
  1282. pl330->state = DYING;
  1283. /* Reset the manager too */
  1284. pl330->dmac_tbd.reset_mngr = true;
  1285. /* Clear the reset flag */
  1286. pl330->dmac_tbd.reset_dmac = false;
  1287. }
  1288. if (pl330->dmac_tbd.reset_mngr) {
  1289. _stop(pl330->manager);
  1290. /* Reset all channels */
  1291. pl330->dmac_tbd.reset_chan = (1 << pl330->pcfg.num_chan) - 1;
  1292. /* Clear the reset flag */
  1293. pl330->dmac_tbd.reset_mngr = false;
  1294. }
  1295. for (i = 0; i < pl330->pcfg.num_chan; i++) {
  1296. if (pl330->dmac_tbd.reset_chan & (1 << i)) {
  1297. struct pl330_thread *thrd = &pl330->channels[i];
  1298. void __iomem *regs = pl330->base;
  1299. enum pl330_op_err err;
  1300. _stop(thrd);
  1301. if (readl(regs + FSC) & (1 << thrd->id))
  1302. err = PL330_ERR_FAIL;
  1303. else
  1304. err = PL330_ERR_ABORT;
  1305. spin_unlock_irqrestore(&pl330->lock, flags);
  1306. dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, err);
  1307. dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, err);
  1308. spin_lock_irqsave(&pl330->lock, flags);
  1309. thrd->req[0].desc = NULL;
  1310. thrd->req[1].desc = NULL;
  1311. thrd->req_running = -1;
  1312. /* Clear the reset flag */
  1313. pl330->dmac_tbd.reset_chan &= ~(1 << i);
  1314. }
  1315. }
  1316. spin_unlock_irqrestore(&pl330->lock, flags);
  1317. return;
  1318. }
  1319. /* Returns 1 if state was updated, 0 otherwise */
  1320. static int pl330_update(struct pl330_dmac *pl330)
  1321. {
  1322. struct dma_pl330_desc *descdone;
  1323. unsigned long flags;
  1324. void __iomem *regs;
  1325. u32 val;
  1326. int id, ev, ret = 0;
  1327. regs = pl330->base;
  1328. spin_lock_irqsave(&pl330->lock, flags);
  1329. val = readl(regs + FSM) & 0x1;
  1330. if (val)
  1331. pl330->dmac_tbd.reset_mngr = true;
  1332. else
  1333. pl330->dmac_tbd.reset_mngr = false;
  1334. val = readl(regs + FSC) & ((1 << pl330->pcfg.num_chan) - 1);
  1335. pl330->dmac_tbd.reset_chan |= val;
  1336. if (val) {
  1337. int i = 0;
  1338. while (i < pl330->pcfg.num_chan) {
  1339. if (val & (1 << i)) {
  1340. dev_info(pl330->ddma.dev,
  1341. "Reset Channel-%d\t CS-%x FTC-%x\n",
  1342. i, readl(regs + CS(i)),
  1343. readl(regs + FTC(i)));
  1344. _stop(&pl330->channels[i]);
  1345. }
  1346. i++;
  1347. }
  1348. }
  1349. /* Check which event happened i.e, thread notified */
  1350. val = readl(regs + ES);
  1351. if (pl330->pcfg.num_events < 32
  1352. && val & ~((1 << pl330->pcfg.num_events) - 1)) {
  1353. pl330->dmac_tbd.reset_dmac = true;
  1354. dev_err(pl330->ddma.dev, "%s:%d Unexpected!\n", __func__,
  1355. __LINE__);
  1356. ret = 1;
  1357. goto updt_exit;
  1358. }
  1359. for (ev = 0; ev < pl330->pcfg.num_events; ev++) {
  1360. if (val & (1 << ev)) { /* Event occurred */
  1361. struct pl330_thread *thrd;
  1362. u32 inten = readl(regs + INTEN);
  1363. int active;
  1364. /* Clear the event */
  1365. if (inten & (1 << ev))
  1366. writel(1 << ev, regs + INTCLR);
  1367. ret = 1;
  1368. id = pl330->events[ev];
  1369. thrd = &pl330->channels[id];
  1370. active = thrd->req_running;
  1371. if (active == -1) /* Aborted */
  1372. continue;
  1373. /* Detach the req */
  1374. descdone = thrd->req[active].desc;
  1375. thrd->req[active].desc = NULL;
  1376. thrd->req_running = -1;
  1377. /* Get going again ASAP */
  1378. _start(thrd);
  1379. /* For now, just make a list of callbacks to be done */
  1380. list_add_tail(&descdone->rqd, &pl330->req_done);
  1381. }
  1382. }
  1383. /* Now that we are in no hurry, do the callbacks */
  1384. while (!list_empty(&pl330->req_done)) {
  1385. descdone = list_first_entry(&pl330->req_done,
  1386. struct dma_pl330_desc, rqd);
  1387. list_del(&descdone->rqd);
  1388. spin_unlock_irqrestore(&pl330->lock, flags);
  1389. dma_pl330_rqcb(descdone, PL330_ERR_NONE);
  1390. spin_lock_irqsave(&pl330->lock, flags);
  1391. }
  1392. updt_exit:
  1393. spin_unlock_irqrestore(&pl330->lock, flags);
  1394. if (pl330->dmac_tbd.reset_dmac
  1395. || pl330->dmac_tbd.reset_mngr
  1396. || pl330->dmac_tbd.reset_chan) {
  1397. ret = 1;
  1398. tasklet_schedule(&pl330->tasks);
  1399. }
  1400. return ret;
  1401. }
  1402. /* Reserve an event */
  1403. static inline int _alloc_event(struct pl330_thread *thrd)
  1404. {
  1405. struct pl330_dmac *pl330 = thrd->dmac;
  1406. int ev;
  1407. for (ev = 0; ev < pl330->pcfg.num_events; ev++)
  1408. if (pl330->events[ev] == -1) {
  1409. pl330->events[ev] = thrd->id;
  1410. return ev;
  1411. }
  1412. return -1;
  1413. }
  1414. static bool _chan_ns(const struct pl330_dmac *pl330, int i)
  1415. {
  1416. return pl330->pcfg.irq_ns & (1 << i);
  1417. }
  1418. /* Upon success, returns IdentityToken for the
  1419. * allocated channel, NULL otherwise.
  1420. */
  1421. static struct pl330_thread *pl330_request_channel(struct pl330_dmac *pl330)
  1422. {
  1423. struct pl330_thread *thrd = NULL;
  1424. int chans, i;
  1425. if (pl330->state == DYING)
  1426. return NULL;
  1427. chans = pl330->pcfg.num_chan;
  1428. for (i = 0; i < chans; i++) {
  1429. thrd = &pl330->channels[i];
  1430. if ((thrd->free) && (!_manager_ns(thrd) ||
  1431. _chan_ns(pl330, i))) {
  1432. thrd->ev = _alloc_event(thrd);
  1433. if (thrd->ev >= 0) {
  1434. thrd->free = false;
  1435. thrd->lstenq = 1;
  1436. thrd->req[0].desc = NULL;
  1437. thrd->req[1].desc = NULL;
  1438. thrd->req_running = -1;
  1439. break;
  1440. }
  1441. }
  1442. thrd = NULL;
  1443. }
  1444. return thrd;
  1445. }
  1446. /* Release an event */
  1447. static inline void _free_event(struct pl330_thread *thrd, int ev)
  1448. {
  1449. struct pl330_dmac *pl330 = thrd->dmac;
  1450. /* If the event is valid and was held by the thread */
  1451. if (ev >= 0 && ev < pl330->pcfg.num_events
  1452. && pl330->events[ev] == thrd->id)
  1453. pl330->events[ev] = -1;
  1454. }
  1455. static void pl330_release_channel(struct pl330_thread *thrd)
  1456. {
  1457. if (!thrd || thrd->free)
  1458. return;
  1459. _stop(thrd);
  1460. dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, PL330_ERR_ABORT);
  1461. dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, PL330_ERR_ABORT);
  1462. _free_event(thrd, thrd->ev);
  1463. thrd->free = true;
  1464. }
  1465. /* Initialize the structure for PL330 configuration, that can be used
  1466. * by the client driver the make best use of the DMAC
  1467. */
  1468. static void read_dmac_config(struct pl330_dmac *pl330)
  1469. {
  1470. void __iomem *regs = pl330->base;
  1471. u32 val;
  1472. val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
  1473. val &= CRD_DATA_WIDTH_MASK;
  1474. pl330->pcfg.data_bus_width = 8 * (1 << val);
  1475. val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
  1476. val &= CRD_DATA_BUFF_MASK;
  1477. pl330->pcfg.data_buf_dep = val + 1;
  1478. val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
  1479. val &= CR0_NUM_CHANS_MASK;
  1480. val += 1;
  1481. pl330->pcfg.num_chan = val;
  1482. val = readl(regs + CR0);
  1483. if (val & CR0_PERIPH_REQ_SET) {
  1484. val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
  1485. val += 1;
  1486. pl330->pcfg.num_peri = val;
  1487. pl330->pcfg.peri_ns = readl(regs + CR4);
  1488. } else {
  1489. pl330->pcfg.num_peri = 0;
  1490. }
  1491. val = readl(regs + CR0);
  1492. if (val & CR0_BOOT_MAN_NS)
  1493. pl330->pcfg.mode |= DMAC_MODE_NS;
  1494. else
  1495. pl330->pcfg.mode &= ~DMAC_MODE_NS;
  1496. val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
  1497. val &= CR0_NUM_EVENTS_MASK;
  1498. val += 1;
  1499. pl330->pcfg.num_events = val;
  1500. pl330->pcfg.irq_ns = readl(regs + CR3);
  1501. }
  1502. static inline void _reset_thread(struct pl330_thread *thrd)
  1503. {
  1504. struct pl330_dmac *pl330 = thrd->dmac;
  1505. thrd->req[0].mc_cpu = pl330->mcode_cpu
  1506. + (thrd->id * pl330->mcbufsz);
  1507. thrd->req[0].mc_bus = pl330->mcode_bus
  1508. + (thrd->id * pl330->mcbufsz);
  1509. thrd->req[0].desc = NULL;
  1510. thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
  1511. + pl330->mcbufsz / 2;
  1512. thrd->req[1].mc_bus = thrd->req[0].mc_bus
  1513. + pl330->mcbufsz / 2;
  1514. thrd->req[1].desc = NULL;
  1515. thrd->req_running = -1;
  1516. }
  1517. static int dmac_alloc_threads(struct pl330_dmac *pl330)
  1518. {
  1519. int chans = pl330->pcfg.num_chan;
  1520. struct pl330_thread *thrd;
  1521. int i;
  1522. /* Allocate 1 Manager and 'chans' Channel threads */
  1523. pl330->channels = kcalloc(1 + chans, sizeof(*thrd),
  1524. GFP_KERNEL);
  1525. if (!pl330->channels)
  1526. return -ENOMEM;
  1527. /* Init Channel threads */
  1528. for (i = 0; i < chans; i++) {
  1529. thrd = &pl330->channels[i];
  1530. thrd->id = i;
  1531. thrd->dmac = pl330;
  1532. _reset_thread(thrd);
  1533. thrd->free = true;
  1534. }
  1535. /* MANAGER is indexed at the end */
  1536. thrd = &pl330->channels[chans];
  1537. thrd->id = chans;
  1538. thrd->dmac = pl330;
  1539. thrd->free = false;
  1540. pl330->manager = thrd;
  1541. return 0;
  1542. }
  1543. static int dmac_alloc_resources(struct pl330_dmac *pl330)
  1544. {
  1545. int chans = pl330->pcfg.num_chan;
  1546. int ret;
  1547. /*
  1548. * Alloc MicroCode buffer for 'chans' Channel threads.
  1549. * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
  1550. */
  1551. pl330->mcode_cpu = dma_alloc_attrs(pl330->ddma.dev,
  1552. chans * pl330->mcbufsz,
  1553. &pl330->mcode_bus, GFP_KERNEL,
  1554. DMA_ATTR_PRIVILEGED);
  1555. if (!pl330->mcode_cpu) {
  1556. dev_err(pl330->ddma.dev, "%s:%d Can't allocate memory!\n",
  1557. __func__, __LINE__);
  1558. return -ENOMEM;
  1559. }
  1560. ret = dmac_alloc_threads(pl330);
  1561. if (ret) {
  1562. dev_err(pl330->ddma.dev, "%s:%d Can't to create channels for DMAC!\n",
  1563. __func__, __LINE__);
  1564. dma_free_attrs(pl330->ddma.dev,
  1565. chans * pl330->mcbufsz,
  1566. pl330->mcode_cpu, pl330->mcode_bus,
  1567. DMA_ATTR_PRIVILEGED);
  1568. return ret;
  1569. }
  1570. return 0;
  1571. }
  1572. static int pl330_add(struct pl330_dmac *pl330)
  1573. {
  1574. int i, ret;
  1575. /* Check if we can handle this DMAC */
  1576. if ((pl330->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
  1577. dev_err(pl330->ddma.dev, "PERIPH_ID 0x%x !\n",
  1578. pl330->pcfg.periph_id);
  1579. return -EINVAL;
  1580. }
  1581. /* Read the configuration of the DMAC */
  1582. read_dmac_config(pl330);
  1583. if (pl330->pcfg.num_events == 0) {
  1584. dev_err(pl330->ddma.dev, "%s:%d Can't work without events!\n",
  1585. __func__, __LINE__);
  1586. return -EINVAL;
  1587. }
  1588. spin_lock_init(&pl330->lock);
  1589. INIT_LIST_HEAD(&pl330->req_done);
  1590. /* Use default MC buffer size if not provided */
  1591. if (!pl330->mcbufsz)
  1592. pl330->mcbufsz = MCODE_BUFF_PER_REQ * 2;
  1593. /* Mark all events as free */
  1594. for (i = 0; i < pl330->pcfg.num_events; i++)
  1595. pl330->events[i] = -1;
  1596. /* Allocate resources needed by the DMAC */
  1597. ret = dmac_alloc_resources(pl330);
  1598. if (ret) {
  1599. dev_err(pl330->ddma.dev, "Unable to create channels for DMAC\n");
  1600. return ret;
  1601. }
  1602. tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
  1603. pl330->state = INIT;
  1604. return 0;
  1605. }
  1606. static int dmac_free_threads(struct pl330_dmac *pl330)
  1607. {
  1608. struct pl330_thread *thrd;
  1609. int i;
  1610. /* Release Channel threads */
  1611. for (i = 0; i < pl330->pcfg.num_chan; i++) {
  1612. thrd = &pl330->channels[i];
  1613. pl330_release_channel(thrd);
  1614. }
  1615. /* Free memory */
  1616. kfree(pl330->channels);
  1617. return 0;
  1618. }
  1619. static void pl330_del(struct pl330_dmac *pl330)
  1620. {
  1621. pl330->state = UNINIT;
  1622. tasklet_kill(&pl330->tasks);
  1623. /* Free DMAC resources */
  1624. dmac_free_threads(pl330);
  1625. dma_free_attrs(pl330->ddma.dev,
  1626. pl330->pcfg.num_chan * pl330->mcbufsz, pl330->mcode_cpu,
  1627. pl330->mcode_bus, DMA_ATTR_PRIVILEGED);
  1628. }
  1629. /* forward declaration */
  1630. static struct amba_driver pl330_driver;
  1631. static inline struct dma_pl330_chan *
  1632. to_pchan(struct dma_chan *ch)
  1633. {
  1634. if (!ch)
  1635. return NULL;
  1636. return container_of(ch, struct dma_pl330_chan, chan);
  1637. }
  1638. static inline struct dma_pl330_desc *
  1639. to_desc(struct dma_async_tx_descriptor *tx)
  1640. {
  1641. return container_of(tx, struct dma_pl330_desc, txd);
  1642. }
  1643. static inline void fill_queue(struct dma_pl330_chan *pch)
  1644. {
  1645. struct dma_pl330_desc *desc;
  1646. int ret;
  1647. list_for_each_entry(desc, &pch->work_list, node) {
  1648. /* If already submitted */
  1649. if (desc->status == BUSY)
  1650. continue;
  1651. ret = pl330_submit_req(pch->thread, desc);
  1652. if (!ret) {
  1653. desc->status = BUSY;
  1654. } else if (ret == -EAGAIN) {
  1655. /* QFull or DMAC Dying */
  1656. break;
  1657. } else {
  1658. /* Unacceptable request */
  1659. desc->status = DONE;
  1660. dev_err(pch->dmac->ddma.dev, "%s:%d Bad Desc(%d)\n",
  1661. __func__, __LINE__, desc->txd.cookie);
  1662. tasklet_schedule(&pch->task);
  1663. }
  1664. }
  1665. }
  1666. static void pl330_tasklet(unsigned long data)
  1667. {
  1668. struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
  1669. struct dma_pl330_desc *desc, *_dt;
  1670. unsigned long flags;
  1671. bool power_down = false;
  1672. spin_lock_irqsave(&pch->lock, flags);
  1673. /* Pick up ripe tomatoes */
  1674. list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
  1675. if (desc->status == DONE) {
  1676. if (!pch->cyclic)
  1677. dma_cookie_complete(&desc->txd);
  1678. list_move_tail(&desc->node, &pch->completed_list);
  1679. }
  1680. /* Try to submit a req imm. next to the last completed cookie */
  1681. fill_queue(pch);
  1682. if (list_empty(&pch->work_list)) {
  1683. spin_lock(&pch->thread->dmac->lock);
  1684. _stop(pch->thread);
  1685. spin_unlock(&pch->thread->dmac->lock);
  1686. power_down = true;
  1687. pch->active = false;
  1688. } else {
  1689. /* Make sure the PL330 Channel thread is active */
  1690. spin_lock(&pch->thread->dmac->lock);
  1691. _start(pch->thread);
  1692. spin_unlock(&pch->thread->dmac->lock);
  1693. }
  1694. while (!list_empty(&pch->completed_list)) {
  1695. struct dmaengine_desc_callback cb;
  1696. desc = list_first_entry(&pch->completed_list,
  1697. struct dma_pl330_desc, node);
  1698. dmaengine_desc_get_callback(&desc->txd, &cb);
  1699. if (pch->cyclic) {
  1700. desc->status = PREP;
  1701. list_move_tail(&desc->node, &pch->work_list);
  1702. if (power_down) {
  1703. pch->active = true;
  1704. spin_lock(&pch->thread->dmac->lock);
  1705. _start(pch->thread);
  1706. spin_unlock(&pch->thread->dmac->lock);
  1707. power_down = false;
  1708. }
  1709. } else {
  1710. desc->status = FREE;
  1711. list_move_tail(&desc->node, &pch->dmac->desc_pool);
  1712. }
  1713. dma_descriptor_unmap(&desc->txd);
  1714. if (dmaengine_desc_callback_valid(&cb)) {
  1715. spin_unlock_irqrestore(&pch->lock, flags);
  1716. dmaengine_desc_callback_invoke(&cb, NULL);
  1717. spin_lock_irqsave(&pch->lock, flags);
  1718. }
  1719. }
  1720. spin_unlock_irqrestore(&pch->lock, flags);
  1721. /* If work list empty, power down */
  1722. if (power_down) {
  1723. pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
  1724. pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
  1725. }
  1726. }
  1727. static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
  1728. struct of_dma *ofdma)
  1729. {
  1730. int count = dma_spec->args_count;
  1731. struct pl330_dmac *pl330 = ofdma->of_dma_data;
  1732. unsigned int chan_id;
  1733. if (!pl330)
  1734. return NULL;
  1735. if (count != 1)
  1736. return NULL;
  1737. chan_id = dma_spec->args[0];
  1738. if (chan_id >= pl330->num_peripherals)
  1739. return NULL;
  1740. return dma_get_slave_channel(&pl330->peripherals[chan_id].chan);
  1741. }
  1742. static int pl330_alloc_chan_resources(struct dma_chan *chan)
  1743. {
  1744. struct dma_pl330_chan *pch = to_pchan(chan);
  1745. struct pl330_dmac *pl330 = pch->dmac;
  1746. unsigned long flags;
  1747. spin_lock_irqsave(&pl330->lock, flags);
  1748. dma_cookie_init(chan);
  1749. pch->cyclic = false;
  1750. pch->thread = pl330_request_channel(pl330);
  1751. if (!pch->thread) {
  1752. spin_unlock_irqrestore(&pl330->lock, flags);
  1753. return -ENOMEM;
  1754. }
  1755. tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
  1756. spin_unlock_irqrestore(&pl330->lock, flags);
  1757. return 1;
  1758. }
  1759. /*
  1760. * We need the data direction between the DMAC (the dma-mapping "device") and
  1761. * the FIFO (the dmaengine "dev"), from the FIFO's point of view. Confusing!
  1762. */
  1763. static enum dma_data_direction
  1764. pl330_dma_slave_map_dir(enum dma_transfer_direction dir)
  1765. {
  1766. switch (dir) {
  1767. case DMA_MEM_TO_DEV:
  1768. return DMA_FROM_DEVICE;
  1769. case DMA_DEV_TO_MEM:
  1770. return DMA_TO_DEVICE;
  1771. case DMA_DEV_TO_DEV:
  1772. return DMA_BIDIRECTIONAL;
  1773. default:
  1774. return DMA_NONE;
  1775. }
  1776. }
  1777. static void pl330_unprep_slave_fifo(struct dma_pl330_chan *pch)
  1778. {
  1779. if (pch->dir != DMA_NONE)
  1780. dma_unmap_resource(pch->chan.device->dev, pch->fifo_dma,
  1781. 1 << pch->burst_sz, pch->dir, 0);
  1782. pch->dir = DMA_NONE;
  1783. }
  1784. static bool pl330_prep_slave_fifo(struct dma_pl330_chan *pch,
  1785. enum dma_transfer_direction dir)
  1786. {
  1787. struct device *dev = pch->chan.device->dev;
  1788. enum dma_data_direction dma_dir = pl330_dma_slave_map_dir(dir);
  1789. /* Already mapped for this config? */
  1790. if (pch->dir == dma_dir)
  1791. return true;
  1792. pl330_unprep_slave_fifo(pch);
  1793. pch->fifo_dma = dma_map_resource(dev, pch->fifo_addr,
  1794. 1 << pch->burst_sz, dma_dir, 0);
  1795. if (dma_mapping_error(dev, pch->fifo_dma))
  1796. return false;
  1797. pch->dir = dma_dir;
  1798. return true;
  1799. }
  1800. static int fixup_burst_len(int max_burst_len, int quirks)
  1801. {
  1802. if (quirks & PL330_QUIRK_BROKEN_NO_FLUSHP)
  1803. return 1;
  1804. else if (max_burst_len > PL330_MAX_BURST)
  1805. return PL330_MAX_BURST;
  1806. else if (max_burst_len < 1)
  1807. return 1;
  1808. else
  1809. return max_burst_len;
  1810. }
  1811. static int pl330_config_write(struct dma_chan *chan,
  1812. struct dma_slave_config *slave_config,
  1813. enum dma_transfer_direction direction)
  1814. {
  1815. struct dma_pl330_chan *pch = to_pchan(chan);
  1816. pl330_unprep_slave_fifo(pch);
  1817. if (direction == DMA_MEM_TO_DEV) {
  1818. if (slave_config->dst_addr)
  1819. pch->fifo_addr = slave_config->dst_addr;
  1820. if (slave_config->dst_addr_width)
  1821. pch->burst_sz = __ffs(slave_config->dst_addr_width);
  1822. pch->burst_len = fixup_burst_len(slave_config->dst_maxburst,
  1823. pch->dmac->quirks);
  1824. } else if (direction == DMA_DEV_TO_MEM) {
  1825. if (slave_config->src_addr)
  1826. pch->fifo_addr = slave_config->src_addr;
  1827. if (slave_config->src_addr_width)
  1828. pch->burst_sz = __ffs(slave_config->src_addr_width);
  1829. pch->burst_len = fixup_burst_len(slave_config->src_maxburst,
  1830. pch->dmac->quirks);
  1831. }
  1832. return 0;
  1833. }
  1834. static int pl330_config(struct dma_chan *chan,
  1835. struct dma_slave_config *slave_config)
  1836. {
  1837. struct dma_pl330_chan *pch = to_pchan(chan);
  1838. memcpy(&pch->slave_config, slave_config, sizeof(*slave_config));
  1839. return 0;
  1840. }
  1841. static int pl330_terminate_all(struct dma_chan *chan)
  1842. {
  1843. struct dma_pl330_chan *pch = to_pchan(chan);
  1844. struct dma_pl330_desc *desc;
  1845. unsigned long flags;
  1846. struct pl330_dmac *pl330 = pch->dmac;
  1847. bool power_down = false;
  1848. pm_runtime_get_sync(pl330->ddma.dev);
  1849. spin_lock_irqsave(&pch->lock, flags);
  1850. spin_lock(&pl330->lock);
  1851. _stop(pch->thread);
  1852. pch->thread->req[0].desc = NULL;
  1853. pch->thread->req[1].desc = NULL;
  1854. pch->thread->req_running = -1;
  1855. spin_unlock(&pl330->lock);
  1856. power_down = pch->active;
  1857. pch->active = false;
  1858. /* Mark all desc done */
  1859. list_for_each_entry(desc, &pch->submitted_list, node) {
  1860. desc->status = FREE;
  1861. dma_cookie_complete(&desc->txd);
  1862. }
  1863. list_for_each_entry(desc, &pch->work_list , node) {
  1864. desc->status = FREE;
  1865. dma_cookie_complete(&desc->txd);
  1866. }
  1867. list_splice_tail_init(&pch->submitted_list, &pl330->desc_pool);
  1868. list_splice_tail_init(&pch->work_list, &pl330->desc_pool);
  1869. list_splice_tail_init(&pch->completed_list, &pl330->desc_pool);
  1870. spin_unlock_irqrestore(&pch->lock, flags);
  1871. pm_runtime_mark_last_busy(pl330->ddma.dev);
  1872. if (power_down)
  1873. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1874. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1875. return 0;
  1876. }
  1877. /*
  1878. * We don't support DMA_RESUME command because of hardware
  1879. * limitations, so after pausing the channel we cannot restore
  1880. * it to active state. We have to terminate channel and setup
  1881. * DMA transfer again. This pause feature was implemented to
  1882. * allow safely read residue before channel termination.
  1883. */
  1884. static int pl330_pause(struct dma_chan *chan)
  1885. {
  1886. struct dma_pl330_chan *pch = to_pchan(chan);
  1887. struct pl330_dmac *pl330 = pch->dmac;
  1888. unsigned long flags;
  1889. pm_runtime_get_sync(pl330->ddma.dev);
  1890. spin_lock_irqsave(&pch->lock, flags);
  1891. spin_lock(&pl330->lock);
  1892. _stop(pch->thread);
  1893. spin_unlock(&pl330->lock);
  1894. spin_unlock_irqrestore(&pch->lock, flags);
  1895. pm_runtime_mark_last_busy(pl330->ddma.dev);
  1896. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1897. return 0;
  1898. }
  1899. static void pl330_free_chan_resources(struct dma_chan *chan)
  1900. {
  1901. struct dma_pl330_chan *pch = to_pchan(chan);
  1902. struct pl330_dmac *pl330 = pch->dmac;
  1903. unsigned long flags;
  1904. tasklet_kill(&pch->task);
  1905. pm_runtime_get_sync(pch->dmac->ddma.dev);
  1906. spin_lock_irqsave(&pl330->lock, flags);
  1907. pl330_release_channel(pch->thread);
  1908. pch->thread = NULL;
  1909. if (pch->cyclic)
  1910. list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
  1911. spin_unlock_irqrestore(&pl330->lock, flags);
  1912. pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
  1913. pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
  1914. pl330_unprep_slave_fifo(pch);
  1915. }
  1916. static int pl330_get_current_xferred_count(struct dma_pl330_chan *pch,
  1917. struct dma_pl330_desc *desc)
  1918. {
  1919. struct pl330_thread *thrd = pch->thread;
  1920. struct pl330_dmac *pl330 = pch->dmac;
  1921. void __iomem *regs = thrd->dmac->base;
  1922. u32 val, addr;
  1923. pm_runtime_get_sync(pl330->ddma.dev);
  1924. val = addr = 0;
  1925. if (desc->rqcfg.src_inc) {
  1926. val = readl(regs + SA(thrd->id));
  1927. addr = desc->px.src_addr;
  1928. } else {
  1929. val = readl(regs + DA(thrd->id));
  1930. addr = desc->px.dst_addr;
  1931. }
  1932. pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
  1933. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1934. /* If DMAMOV hasn't finished yet, SAR/DAR can be zero */
  1935. if (!val)
  1936. return 0;
  1937. return val - addr;
  1938. }
  1939. static enum dma_status
  1940. pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  1941. struct dma_tx_state *txstate)
  1942. {
  1943. enum dma_status ret;
  1944. unsigned long flags;
  1945. struct dma_pl330_desc *desc, *running = NULL, *last_enq = NULL;
  1946. struct dma_pl330_chan *pch = to_pchan(chan);
  1947. unsigned int transferred, residual = 0;
  1948. ret = dma_cookie_status(chan, cookie, txstate);
  1949. if (!txstate)
  1950. return ret;
  1951. if (ret == DMA_COMPLETE)
  1952. goto out;
  1953. spin_lock_irqsave(&pch->lock, flags);
  1954. spin_lock(&pch->thread->dmac->lock);
  1955. if (pch->thread->req_running != -1)
  1956. running = pch->thread->req[pch->thread->req_running].desc;
  1957. last_enq = pch->thread->req[pch->thread->lstenq].desc;
  1958. /* Check in pending list */
  1959. list_for_each_entry(desc, &pch->work_list, node) {
  1960. if (desc->status == DONE)
  1961. transferred = desc->bytes_requested;
  1962. else if (running && desc == running)
  1963. transferred =
  1964. pl330_get_current_xferred_count(pch, desc);
  1965. else if (desc->status == BUSY)
  1966. /*
  1967. * Busy but not running means either just enqueued,
  1968. * or finished and not yet marked done
  1969. */
  1970. if (desc == last_enq)
  1971. transferred = 0;
  1972. else
  1973. transferred = desc->bytes_requested;
  1974. else
  1975. transferred = 0;
  1976. residual += desc->bytes_requested - transferred;
  1977. if (desc->txd.cookie == cookie) {
  1978. switch (desc->status) {
  1979. case DONE:
  1980. ret = DMA_COMPLETE;
  1981. break;
  1982. case PREP:
  1983. case BUSY:
  1984. ret = DMA_IN_PROGRESS;
  1985. break;
  1986. default:
  1987. WARN_ON(1);
  1988. }
  1989. break;
  1990. }
  1991. if (desc->last)
  1992. residual = 0;
  1993. }
  1994. spin_unlock(&pch->thread->dmac->lock);
  1995. spin_unlock_irqrestore(&pch->lock, flags);
  1996. out:
  1997. dma_set_residue(txstate, residual);
  1998. return ret;
  1999. }
  2000. static void pl330_issue_pending(struct dma_chan *chan)
  2001. {
  2002. struct dma_pl330_chan *pch = to_pchan(chan);
  2003. unsigned long flags;
  2004. spin_lock_irqsave(&pch->lock, flags);
  2005. if (list_empty(&pch->work_list)) {
  2006. /*
  2007. * Warn on nothing pending. Empty submitted_list may
  2008. * break our pm_runtime usage counter as it is
  2009. * updated on work_list emptiness status.
  2010. */
  2011. WARN_ON(list_empty(&pch->submitted_list));
  2012. pch->active = true;
  2013. pm_runtime_get_sync(pch->dmac->ddma.dev);
  2014. }
  2015. list_splice_tail_init(&pch->submitted_list, &pch->work_list);
  2016. spin_unlock_irqrestore(&pch->lock, flags);
  2017. pl330_tasklet((unsigned long)pch);
  2018. }
  2019. /*
  2020. * We returned the last one of the circular list of descriptor(s)
  2021. * from prep_xxx, so the argument to submit corresponds to the last
  2022. * descriptor of the list.
  2023. */
  2024. static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
  2025. {
  2026. struct dma_pl330_desc *desc, *last = to_desc(tx);
  2027. struct dma_pl330_chan *pch = to_pchan(tx->chan);
  2028. dma_cookie_t cookie;
  2029. unsigned long flags;
  2030. spin_lock_irqsave(&pch->lock, flags);
  2031. /* Assign cookies to all nodes */
  2032. while (!list_empty(&last->node)) {
  2033. desc = list_entry(last->node.next, struct dma_pl330_desc, node);
  2034. if (pch->cyclic) {
  2035. desc->txd.callback = last->txd.callback;
  2036. desc->txd.callback_param = last->txd.callback_param;
  2037. }
  2038. desc->last = false;
  2039. dma_cookie_assign(&desc->txd);
  2040. list_move_tail(&desc->node, &pch->submitted_list);
  2041. }
  2042. last->last = true;
  2043. cookie = dma_cookie_assign(&last->txd);
  2044. list_add_tail(&last->node, &pch->submitted_list);
  2045. spin_unlock_irqrestore(&pch->lock, flags);
  2046. return cookie;
  2047. }
  2048. static inline void _init_desc(struct dma_pl330_desc *desc)
  2049. {
  2050. desc->rqcfg.swap = SWAP_NO;
  2051. desc->rqcfg.scctl = CCTRL0;
  2052. desc->rqcfg.dcctl = CCTRL0;
  2053. desc->txd.tx_submit = pl330_tx_submit;
  2054. INIT_LIST_HEAD(&desc->node);
  2055. }
  2056. /* Returns the number of descriptors added to the DMAC pool */
  2057. static int add_desc(struct list_head *pool, spinlock_t *lock,
  2058. gfp_t flg, int count)
  2059. {
  2060. struct dma_pl330_desc *desc;
  2061. unsigned long flags;
  2062. int i;
  2063. desc = kcalloc(count, sizeof(*desc), flg);
  2064. if (!desc)
  2065. return 0;
  2066. spin_lock_irqsave(lock, flags);
  2067. for (i = 0; i < count; i++) {
  2068. _init_desc(&desc[i]);
  2069. list_add_tail(&desc[i].node, pool);
  2070. }
  2071. spin_unlock_irqrestore(lock, flags);
  2072. return count;
  2073. }
  2074. static struct dma_pl330_desc *pluck_desc(struct list_head *pool,
  2075. spinlock_t *lock)
  2076. {
  2077. struct dma_pl330_desc *desc = NULL;
  2078. unsigned long flags;
  2079. spin_lock_irqsave(lock, flags);
  2080. if (!list_empty(pool)) {
  2081. desc = list_entry(pool->next,
  2082. struct dma_pl330_desc, node);
  2083. list_del_init(&desc->node);
  2084. desc->status = PREP;
  2085. desc->txd.callback = NULL;
  2086. }
  2087. spin_unlock_irqrestore(lock, flags);
  2088. return desc;
  2089. }
  2090. static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
  2091. {
  2092. struct pl330_dmac *pl330 = pch->dmac;
  2093. u8 *peri_id = pch->chan.private;
  2094. struct dma_pl330_desc *desc;
  2095. /* Pluck one desc from the pool of DMAC */
  2096. desc = pluck_desc(&pl330->desc_pool, &pl330->pool_lock);
  2097. /* If the DMAC pool is empty, alloc new */
  2098. if (!desc) {
  2099. DEFINE_SPINLOCK(lock);
  2100. LIST_HEAD(pool);
  2101. if (!add_desc(&pool, &lock, GFP_ATOMIC, 1))
  2102. return NULL;
  2103. desc = pluck_desc(&pool, &lock);
  2104. WARN_ON(!desc || !list_empty(&pool));
  2105. }
  2106. /* Initialize the descriptor */
  2107. desc->pchan = pch;
  2108. desc->txd.cookie = 0;
  2109. async_tx_ack(&desc->txd);
  2110. desc->peri = peri_id ? pch->chan.chan_id : 0;
  2111. desc->rqcfg.pcfg = &pch->dmac->pcfg;
  2112. dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
  2113. return desc;
  2114. }
  2115. static inline void fill_px(struct pl330_xfer *px,
  2116. dma_addr_t dst, dma_addr_t src, size_t len)
  2117. {
  2118. px->bytes = len;
  2119. px->dst_addr = dst;
  2120. px->src_addr = src;
  2121. }
  2122. static struct dma_pl330_desc *
  2123. __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
  2124. dma_addr_t src, size_t len)
  2125. {
  2126. struct dma_pl330_desc *desc = pl330_get_desc(pch);
  2127. if (!desc) {
  2128. dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
  2129. __func__, __LINE__);
  2130. return NULL;
  2131. }
  2132. /*
  2133. * Ideally we should lookout for reqs bigger than
  2134. * those that can be programmed with 256 bytes of
  2135. * MC buffer, but considering a req size is seldom
  2136. * going to be word-unaligned and more than 200MB,
  2137. * we take it easy.
  2138. * Also, should the limit is reached we'd rather
  2139. * have the platform increase MC buffer size than
  2140. * complicating this API driver.
  2141. */
  2142. fill_px(&desc->px, dst, src, len);
  2143. return desc;
  2144. }
  2145. /* Call after fixing burst size */
  2146. static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
  2147. {
  2148. struct dma_pl330_chan *pch = desc->pchan;
  2149. struct pl330_dmac *pl330 = pch->dmac;
  2150. int burst_len;
  2151. burst_len = pl330->pcfg.data_bus_width / 8;
  2152. burst_len *= pl330->pcfg.data_buf_dep / pl330->pcfg.num_chan;
  2153. burst_len >>= desc->rqcfg.brst_size;
  2154. /* src/dst_burst_len can't be more than 16 */
  2155. if (burst_len > PL330_MAX_BURST)
  2156. burst_len = PL330_MAX_BURST;
  2157. return burst_len;
  2158. }
  2159. static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
  2160. struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
  2161. size_t period_len, enum dma_transfer_direction direction,
  2162. unsigned long flags)
  2163. {
  2164. struct dma_pl330_desc *desc = NULL, *first = NULL;
  2165. struct dma_pl330_chan *pch = to_pchan(chan);
  2166. struct pl330_dmac *pl330 = pch->dmac;
  2167. unsigned int i;
  2168. dma_addr_t dst;
  2169. dma_addr_t src;
  2170. if (len % period_len != 0)
  2171. return NULL;
  2172. if (!is_slave_direction(direction)) {
  2173. dev_err(pch->dmac->ddma.dev, "%s:%d Invalid dma direction\n",
  2174. __func__, __LINE__);
  2175. return NULL;
  2176. }
  2177. pl330_config_write(chan, &pch->slave_config, direction);
  2178. if (!pl330_prep_slave_fifo(pch, direction))
  2179. return NULL;
  2180. for (i = 0; i < len / period_len; i++) {
  2181. desc = pl330_get_desc(pch);
  2182. if (!desc) {
  2183. dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
  2184. __func__, __LINE__);
  2185. if (!first)
  2186. return NULL;
  2187. spin_lock_irqsave(&pl330->pool_lock, flags);
  2188. while (!list_empty(&first->node)) {
  2189. desc = list_entry(first->node.next,
  2190. struct dma_pl330_desc, node);
  2191. list_move_tail(&desc->node, &pl330->desc_pool);
  2192. }
  2193. list_move_tail(&first->node, &pl330->desc_pool);
  2194. spin_unlock_irqrestore(&pl330->pool_lock, flags);
  2195. return NULL;
  2196. }
  2197. switch (direction) {
  2198. case DMA_MEM_TO_DEV:
  2199. desc->rqcfg.src_inc = 1;
  2200. desc->rqcfg.dst_inc = 0;
  2201. src = dma_addr;
  2202. dst = pch->fifo_dma;
  2203. break;
  2204. case DMA_DEV_TO_MEM:
  2205. desc->rqcfg.src_inc = 0;
  2206. desc->rqcfg.dst_inc = 1;
  2207. src = pch->fifo_dma;
  2208. dst = dma_addr;
  2209. break;
  2210. default:
  2211. break;
  2212. }
  2213. desc->rqtype = direction;
  2214. desc->rqcfg.brst_size = pch->burst_sz;
  2215. desc->rqcfg.brst_len = pch->burst_len;
  2216. desc->bytes_requested = period_len;
  2217. fill_px(&desc->px, dst, src, period_len);
  2218. if (!first)
  2219. first = desc;
  2220. else
  2221. list_add_tail(&desc->node, &first->node);
  2222. dma_addr += period_len;
  2223. }
  2224. if (!desc)
  2225. return NULL;
  2226. pch->cyclic = true;
  2227. desc->txd.flags = flags;
  2228. return &desc->txd;
  2229. }
  2230. static struct dma_async_tx_descriptor *
  2231. pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
  2232. dma_addr_t src, size_t len, unsigned long flags)
  2233. {
  2234. struct dma_pl330_desc *desc;
  2235. struct dma_pl330_chan *pch = to_pchan(chan);
  2236. struct pl330_dmac *pl330;
  2237. int burst;
  2238. if (unlikely(!pch || !len))
  2239. return NULL;
  2240. pl330 = pch->dmac;
  2241. desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
  2242. if (!desc)
  2243. return NULL;
  2244. desc->rqcfg.src_inc = 1;
  2245. desc->rqcfg.dst_inc = 1;
  2246. desc->rqtype = DMA_MEM_TO_MEM;
  2247. /* Select max possible burst size */
  2248. burst = pl330->pcfg.data_bus_width / 8;
  2249. /*
  2250. * Make sure we use a burst size that aligns with all the memcpy
  2251. * parameters because our DMA programming algorithm doesn't cope with
  2252. * transfers which straddle an entry in the DMA device's MFIFO.
  2253. */
  2254. while ((src | dst | len) & (burst - 1))
  2255. burst /= 2;
  2256. desc->rqcfg.brst_size = 0;
  2257. while (burst != (1 << desc->rqcfg.brst_size))
  2258. desc->rqcfg.brst_size++;
  2259. /*
  2260. * If burst size is smaller than bus width then make sure we only
  2261. * transfer one at a time to avoid a burst stradling an MFIFO entry.
  2262. */
  2263. if (desc->rqcfg.brst_size * 8 < pl330->pcfg.data_bus_width)
  2264. desc->rqcfg.brst_len = 1;
  2265. desc->rqcfg.brst_len = get_burst_len(desc, len);
  2266. desc->bytes_requested = len;
  2267. desc->txd.flags = flags;
  2268. return &desc->txd;
  2269. }
  2270. static void __pl330_giveback_desc(struct pl330_dmac *pl330,
  2271. struct dma_pl330_desc *first)
  2272. {
  2273. unsigned long flags;
  2274. struct dma_pl330_desc *desc;
  2275. if (!first)
  2276. return;
  2277. spin_lock_irqsave(&pl330->pool_lock, flags);
  2278. while (!list_empty(&first->node)) {
  2279. desc = list_entry(first->node.next,
  2280. struct dma_pl330_desc, node);
  2281. list_move_tail(&desc->node, &pl330->desc_pool);
  2282. }
  2283. list_move_tail(&first->node, &pl330->desc_pool);
  2284. spin_unlock_irqrestore(&pl330->pool_lock, flags);
  2285. }
  2286. static struct dma_async_tx_descriptor *
  2287. pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  2288. unsigned int sg_len, enum dma_transfer_direction direction,
  2289. unsigned long flg, void *context)
  2290. {
  2291. struct dma_pl330_desc *first, *desc = NULL;
  2292. struct dma_pl330_chan *pch = to_pchan(chan);
  2293. struct scatterlist *sg;
  2294. int i;
  2295. if (unlikely(!pch || !sgl || !sg_len))
  2296. return NULL;
  2297. pl330_config_write(chan, &pch->slave_config, direction);
  2298. if (!pl330_prep_slave_fifo(pch, direction))
  2299. return NULL;
  2300. first = NULL;
  2301. for_each_sg(sgl, sg, sg_len, i) {
  2302. desc = pl330_get_desc(pch);
  2303. if (!desc) {
  2304. struct pl330_dmac *pl330 = pch->dmac;
  2305. dev_err(pch->dmac->ddma.dev,
  2306. "%s:%d Unable to fetch desc\n",
  2307. __func__, __LINE__);
  2308. __pl330_giveback_desc(pl330, first);
  2309. return NULL;
  2310. }
  2311. if (!first)
  2312. first = desc;
  2313. else
  2314. list_add_tail(&desc->node, &first->node);
  2315. if (direction == DMA_MEM_TO_DEV) {
  2316. desc->rqcfg.src_inc = 1;
  2317. desc->rqcfg.dst_inc = 0;
  2318. fill_px(&desc->px, pch->fifo_dma, sg_dma_address(sg),
  2319. sg_dma_len(sg));
  2320. } else {
  2321. desc->rqcfg.src_inc = 0;
  2322. desc->rqcfg.dst_inc = 1;
  2323. fill_px(&desc->px, sg_dma_address(sg), pch->fifo_dma,
  2324. sg_dma_len(sg));
  2325. }
  2326. desc->rqcfg.brst_size = pch->burst_sz;
  2327. desc->rqcfg.brst_len = pch->burst_len;
  2328. desc->rqtype = direction;
  2329. desc->bytes_requested = sg_dma_len(sg);
  2330. }
  2331. /* Return the last desc in the chain */
  2332. desc->txd.flags = flg;
  2333. return &desc->txd;
  2334. }
  2335. static irqreturn_t pl330_irq_handler(int irq, void *data)
  2336. {
  2337. if (pl330_update(data))
  2338. return IRQ_HANDLED;
  2339. else
  2340. return IRQ_NONE;
  2341. }
  2342. #define PL330_DMA_BUSWIDTHS \
  2343. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  2344. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  2345. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  2346. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  2347. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
  2348. #ifdef CONFIG_DEBUG_FS
  2349. static int pl330_debugfs_show(struct seq_file *s, void *data)
  2350. {
  2351. struct pl330_dmac *pl330 = s->private;
  2352. int chans, pchs, ch, pr;
  2353. chans = pl330->pcfg.num_chan;
  2354. pchs = pl330->num_peripherals;
  2355. seq_puts(s, "PL330 physical channels:\n");
  2356. seq_puts(s, "THREAD:\t\tCHANNEL:\n");
  2357. seq_puts(s, "--------\t-----\n");
  2358. for (ch = 0; ch < chans; ch++) {
  2359. struct pl330_thread *thrd = &pl330->channels[ch];
  2360. int found = -1;
  2361. for (pr = 0; pr < pchs; pr++) {
  2362. struct dma_pl330_chan *pch = &pl330->peripherals[pr];
  2363. if (!pch->thread || thrd->id != pch->thread->id)
  2364. continue;
  2365. found = pr;
  2366. }
  2367. seq_printf(s, "%d\t\t", thrd->id);
  2368. if (found == -1)
  2369. seq_puts(s, "--\n");
  2370. else
  2371. seq_printf(s, "%d\n", found);
  2372. }
  2373. return 0;
  2374. }
  2375. DEFINE_SHOW_ATTRIBUTE(pl330_debugfs);
  2376. static inline void init_pl330_debugfs(struct pl330_dmac *pl330)
  2377. {
  2378. debugfs_create_file(dev_name(pl330->ddma.dev),
  2379. S_IFREG | 0444, NULL, pl330,
  2380. &pl330_debugfs_fops);
  2381. }
  2382. #else
  2383. static inline void init_pl330_debugfs(struct pl330_dmac *pl330)
  2384. {
  2385. }
  2386. #endif
  2387. /*
  2388. * Runtime PM callbacks are provided by amba/bus.c driver.
  2389. *
  2390. * It is assumed here that IRQ safe runtime PM is chosen in probe and amba
  2391. * bus driver will only disable/enable the clock in runtime PM callbacks.
  2392. */
  2393. static int __maybe_unused pl330_suspend(struct device *dev)
  2394. {
  2395. struct amba_device *pcdev = to_amba_device(dev);
  2396. pm_runtime_force_suspend(dev);
  2397. amba_pclk_unprepare(pcdev);
  2398. return 0;
  2399. }
  2400. static int __maybe_unused pl330_resume(struct device *dev)
  2401. {
  2402. struct amba_device *pcdev = to_amba_device(dev);
  2403. int ret;
  2404. ret = amba_pclk_prepare(pcdev);
  2405. if (ret)
  2406. return ret;
  2407. pm_runtime_force_resume(dev);
  2408. return ret;
  2409. }
  2410. static const struct dev_pm_ops pl330_pm = {
  2411. SET_LATE_SYSTEM_SLEEP_PM_OPS(pl330_suspend, pl330_resume)
  2412. };
  2413. static int
  2414. pl330_probe(struct amba_device *adev, const struct amba_id *id)
  2415. {
  2416. struct pl330_config *pcfg;
  2417. struct pl330_dmac *pl330;
  2418. struct dma_pl330_chan *pch, *_p;
  2419. struct dma_device *pd;
  2420. struct resource *res;
  2421. int i, ret, irq;
  2422. int num_chan;
  2423. struct device_node *np = adev->dev.of_node;
  2424. ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
  2425. if (ret)
  2426. return ret;
  2427. /* Allocate a new DMAC and its Channels */
  2428. pl330 = devm_kzalloc(&adev->dev, sizeof(*pl330), GFP_KERNEL);
  2429. if (!pl330)
  2430. return -ENOMEM;
  2431. pd = &pl330->ddma;
  2432. pd->dev = &adev->dev;
  2433. pl330->mcbufsz = 0;
  2434. /* get quirk */
  2435. for (i = 0; i < ARRAY_SIZE(of_quirks); i++)
  2436. if (of_property_read_bool(np, of_quirks[i].quirk))
  2437. pl330->quirks |= of_quirks[i].id;
  2438. res = &adev->res;
  2439. pl330->base = devm_ioremap_resource(&adev->dev, res);
  2440. if (IS_ERR(pl330->base))
  2441. return PTR_ERR(pl330->base);
  2442. amba_set_drvdata(adev, pl330);
  2443. pl330->rstc = devm_reset_control_get_optional(&adev->dev, "dma");
  2444. if (IS_ERR(pl330->rstc)) {
  2445. if (PTR_ERR(pl330->rstc) != -EPROBE_DEFER)
  2446. dev_err(&adev->dev, "Failed to get reset!\n");
  2447. return PTR_ERR(pl330->rstc);
  2448. } else {
  2449. ret = reset_control_deassert(pl330->rstc);
  2450. if (ret) {
  2451. dev_err(&adev->dev, "Couldn't deassert the device from reset!\n");
  2452. return ret;
  2453. }
  2454. }
  2455. pl330->rstc_ocp = devm_reset_control_get_optional(&adev->dev, "dma-ocp");
  2456. if (IS_ERR(pl330->rstc_ocp)) {
  2457. if (PTR_ERR(pl330->rstc_ocp) != -EPROBE_DEFER)
  2458. dev_err(&adev->dev, "Failed to get OCP reset!\n");
  2459. return PTR_ERR(pl330->rstc_ocp);
  2460. } else {
  2461. ret = reset_control_deassert(pl330->rstc_ocp);
  2462. if (ret) {
  2463. dev_err(&adev->dev, "Couldn't deassert the device from OCP reset!\n");
  2464. return ret;
  2465. }
  2466. }
  2467. for (i = 0; i < AMBA_NR_IRQS; i++) {
  2468. irq = adev->irq[i];
  2469. if (irq) {
  2470. ret = devm_request_irq(&adev->dev, irq,
  2471. pl330_irq_handler, 0,
  2472. dev_name(&adev->dev), pl330);
  2473. if (ret)
  2474. return ret;
  2475. } else {
  2476. break;
  2477. }
  2478. }
  2479. pcfg = &pl330->pcfg;
  2480. pcfg->periph_id = adev->periphid;
  2481. ret = pl330_add(pl330);
  2482. if (ret)
  2483. return ret;
  2484. INIT_LIST_HEAD(&pl330->desc_pool);
  2485. spin_lock_init(&pl330->pool_lock);
  2486. /* Create a descriptor pool of default size */
  2487. if (!add_desc(&pl330->desc_pool, &pl330->pool_lock,
  2488. GFP_KERNEL, NR_DEFAULT_DESC))
  2489. dev_warn(&adev->dev, "unable to allocate desc\n");
  2490. INIT_LIST_HEAD(&pd->channels);
  2491. /* Initialize channel parameters */
  2492. num_chan = max_t(int, pcfg->num_peri, pcfg->num_chan);
  2493. pl330->num_peripherals = num_chan;
  2494. pl330->peripherals = kcalloc(num_chan, sizeof(*pch), GFP_KERNEL);
  2495. if (!pl330->peripherals) {
  2496. ret = -ENOMEM;
  2497. goto probe_err2;
  2498. }
  2499. for (i = 0; i < num_chan; i++) {
  2500. pch = &pl330->peripherals[i];
  2501. pch->chan.private = adev->dev.of_node;
  2502. INIT_LIST_HEAD(&pch->submitted_list);
  2503. INIT_LIST_HEAD(&pch->work_list);
  2504. INIT_LIST_HEAD(&pch->completed_list);
  2505. spin_lock_init(&pch->lock);
  2506. pch->thread = NULL;
  2507. pch->chan.device = pd;
  2508. pch->dmac = pl330;
  2509. pch->dir = DMA_NONE;
  2510. /* Add the channel to the DMAC list */
  2511. list_add_tail(&pch->chan.device_node, &pd->channels);
  2512. }
  2513. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  2514. if (pcfg->num_peri) {
  2515. dma_cap_set(DMA_SLAVE, pd->cap_mask);
  2516. dma_cap_set(DMA_CYCLIC, pd->cap_mask);
  2517. dma_cap_set(DMA_PRIVATE, pd->cap_mask);
  2518. }
  2519. pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
  2520. pd->device_free_chan_resources = pl330_free_chan_resources;
  2521. pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
  2522. pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
  2523. pd->device_tx_status = pl330_tx_status;
  2524. pd->device_prep_slave_sg = pl330_prep_slave_sg;
  2525. pd->device_config = pl330_config;
  2526. pd->device_pause = pl330_pause;
  2527. pd->device_terminate_all = pl330_terminate_all;
  2528. pd->device_issue_pending = pl330_issue_pending;
  2529. pd->src_addr_widths = PL330_DMA_BUSWIDTHS;
  2530. pd->dst_addr_widths = PL330_DMA_BUSWIDTHS;
  2531. pd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  2532. pd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  2533. pd->max_burst = ((pl330->quirks & PL330_QUIRK_BROKEN_NO_FLUSHP) ?
  2534. 1 : PL330_MAX_BURST);
  2535. ret = dma_async_device_register(pd);
  2536. if (ret) {
  2537. dev_err(&adev->dev, "unable to register DMAC\n");
  2538. goto probe_err3;
  2539. }
  2540. if (adev->dev.of_node) {
  2541. ret = of_dma_controller_register(adev->dev.of_node,
  2542. of_dma_pl330_xlate, pl330);
  2543. if (ret) {
  2544. dev_err(&adev->dev,
  2545. "unable to register DMA to the generic DT DMA helpers\n");
  2546. }
  2547. }
  2548. adev->dev.dma_parms = &pl330->dma_parms;
  2549. /*
  2550. * This is the limit for transfers with a buswidth of 1, larger
  2551. * buswidths will have larger limits.
  2552. */
  2553. ret = dma_set_max_seg_size(&adev->dev, 1900800);
  2554. if (ret)
  2555. dev_err(&adev->dev, "unable to set the seg size\n");
  2556. init_pl330_debugfs(pl330);
  2557. dev_info(&adev->dev,
  2558. "Loaded driver for PL330 DMAC-%x\n", adev->periphid);
  2559. dev_info(&adev->dev,
  2560. "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
  2561. pcfg->data_buf_dep, pcfg->data_bus_width / 8, pcfg->num_chan,
  2562. pcfg->num_peri, pcfg->num_events);
  2563. pm_runtime_irq_safe(&adev->dev);
  2564. pm_runtime_use_autosuspend(&adev->dev);
  2565. pm_runtime_set_autosuspend_delay(&adev->dev, PL330_AUTOSUSPEND_DELAY);
  2566. pm_runtime_mark_last_busy(&adev->dev);
  2567. pm_runtime_put_autosuspend(&adev->dev);
  2568. return 0;
  2569. probe_err3:
  2570. /* Idle the DMAC */
  2571. list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
  2572. chan.device_node) {
  2573. /* Remove the channel */
  2574. list_del(&pch->chan.device_node);
  2575. /* Flush the channel */
  2576. if (pch->thread) {
  2577. pl330_terminate_all(&pch->chan);
  2578. pl330_free_chan_resources(&pch->chan);
  2579. }
  2580. }
  2581. probe_err2:
  2582. pl330_del(pl330);
  2583. if (pl330->rstc_ocp)
  2584. reset_control_assert(pl330->rstc_ocp);
  2585. if (pl330->rstc)
  2586. reset_control_assert(pl330->rstc);
  2587. return ret;
  2588. }
  2589. static int pl330_remove(struct amba_device *adev)
  2590. {
  2591. struct pl330_dmac *pl330 = amba_get_drvdata(adev);
  2592. struct dma_pl330_chan *pch, *_p;
  2593. int i, irq;
  2594. pm_runtime_get_noresume(pl330->ddma.dev);
  2595. if (adev->dev.of_node)
  2596. of_dma_controller_free(adev->dev.of_node);
  2597. for (i = 0; i < AMBA_NR_IRQS; i++) {
  2598. irq = adev->irq[i];
  2599. if (irq)
  2600. devm_free_irq(&adev->dev, irq, pl330);
  2601. }
  2602. dma_async_device_unregister(&pl330->ddma);
  2603. /* Idle the DMAC */
  2604. list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
  2605. chan.device_node) {
  2606. /* Remove the channel */
  2607. list_del(&pch->chan.device_node);
  2608. /* Flush the channel */
  2609. if (pch->thread) {
  2610. pl330_terminate_all(&pch->chan);
  2611. pl330_free_chan_resources(&pch->chan);
  2612. }
  2613. }
  2614. pl330_del(pl330);
  2615. if (pl330->rstc_ocp)
  2616. reset_control_assert(pl330->rstc_ocp);
  2617. if (pl330->rstc)
  2618. reset_control_assert(pl330->rstc);
  2619. return 0;
  2620. }
  2621. static const struct amba_id pl330_ids[] = {
  2622. {
  2623. .id = 0x00041330,
  2624. .mask = 0x000fffff,
  2625. },
  2626. { 0, 0 },
  2627. };
  2628. MODULE_DEVICE_TABLE(amba, pl330_ids);
  2629. static struct amba_driver pl330_driver = {
  2630. .drv = {
  2631. .owner = THIS_MODULE,
  2632. .name = "dma-pl330",
  2633. .pm = &pl330_pm,
  2634. },
  2635. .id_table = pl330_ids,
  2636. .probe = pl330_probe,
  2637. .remove = pl330_remove,
  2638. };
  2639. module_amba_driver(pl330_driver);
  2640. MODULE_AUTHOR("Jaswinder Singh <jassisinghbrar@gmail.com>");
  2641. MODULE_DESCRIPTION("API Driver for PL330 DMAC");
  2642. MODULE_LICENSE("GPL");