bcm-pdc-mailbox.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /*
  2. * Copyright 2016 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2, as
  6. * published by the Free Software Foundation (the "GPL").
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License version 2 (GPLv2) for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * version 2 (GPLv2) along with this source code.
  15. */
  16. /*
  17. * Broadcom PDC Mailbox Driver
  18. * The PDC provides a ring based programming interface to one or more hardware
  19. * offload engines. For example, the PDC driver works with both SPU-M and SPU2
  20. * cryptographic offload hardware. In some chips the PDC is referred to as MDE.
  21. *
  22. * The PDC driver registers with the Linux mailbox framework as a mailbox
  23. * controller, once for each PDC instance. Ring 0 for each PDC is registered as
  24. * a mailbox channel. The PDC driver uses interrupts to determine when data
  25. * transfers to and from an offload engine are complete. The PDC driver uses
  26. * threaded IRQs so that response messages are handled outside of interrupt
  27. * context.
  28. *
  29. * The PDC driver allows multiple messages to be pending in the descriptor
  30. * rings. The tx_msg_start descriptor index indicates where the last message
  31. * starts. The txin_numd value at this index indicates how many descriptor
  32. * indexes make up the message. Similar state is kept on the receive side. When
  33. * an rx interrupt indicates a response is ready, the PDC driver processes numd
  34. * descriptors from the tx and rx ring, thus processing one response at a time.
  35. */
  36. #include <linux/errno.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <linux/debugfs.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/wait.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/io.h>
  45. #include <linux/of.h>
  46. #include <linux/of_device.h>
  47. #include <linux/of_address.h>
  48. #include <linux/of_irq.h>
  49. #include <linux/mailbox_controller.h>
  50. #include <linux/mailbox/brcm-message.h>
  51. #include <linux/scatterlist.h>
  52. #include <linux/dma-direction.h>
  53. #include <linux/dma-mapping.h>
  54. #include <linux/dmapool.h>
  55. #define PDC_SUCCESS 0
  56. #define RING_ENTRY_SIZE sizeof(struct dma64dd)
  57. /* # entries in PDC dma ring */
  58. #define PDC_RING_ENTRIES 128
  59. #define PDC_RING_SIZE (PDC_RING_ENTRIES * RING_ENTRY_SIZE)
  60. /* Rings are 8k aligned */
  61. #define RING_ALIGN_ORDER 13
  62. #define RING_ALIGN BIT(RING_ALIGN_ORDER)
  63. #define RX_BUF_ALIGN_ORDER 5
  64. #define RX_BUF_ALIGN BIT(RX_BUF_ALIGN_ORDER)
  65. /* descriptor bumping macros */
  66. #define XXD(x, max_mask) ((x) & (max_mask))
  67. #define TXD(x, max_mask) XXD((x), (max_mask))
  68. #define RXD(x, max_mask) XXD((x), (max_mask))
  69. #define NEXTTXD(i, max_mask) TXD((i) + 1, (max_mask))
  70. #define PREVTXD(i, max_mask) TXD((i) - 1, (max_mask))
  71. #define NEXTRXD(i, max_mask) RXD((i) + 1, (max_mask))
  72. #define PREVRXD(i, max_mask) RXD((i) - 1, (max_mask))
  73. #define NTXDACTIVE(h, t, max_mask) TXD((t) - (h), (max_mask))
  74. #define NRXDACTIVE(h, t, max_mask) RXD((t) - (h), (max_mask))
  75. /* Length of BCM header at start of SPU msg, in bytes */
  76. #define BCM_HDR_LEN 8
  77. /*
  78. * PDC driver reserves ringset 0 on each SPU for its own use. The driver does
  79. * not currently support use of multiple ringsets on a single PDC engine.
  80. */
  81. #define PDC_RINGSET 0
  82. /*
  83. * Interrupt mask and status definitions. Enable interrupts for tx and rx on
  84. * ring 0
  85. */
  86. #define PDC_XMTINT_0 (24 + PDC_RINGSET)
  87. #define PDC_RCVINT_0 (16 + PDC_RINGSET)
  88. #define PDC_XMTINTEN_0 BIT(PDC_XMTINT_0)
  89. #define PDC_RCVINTEN_0 BIT(PDC_RCVINT_0)
  90. #define PDC_INTMASK (PDC_XMTINTEN_0 | PDC_RCVINTEN_0)
  91. #define PDC_LAZY_FRAMECOUNT 1
  92. #define PDC_LAZY_TIMEOUT 10000
  93. #define PDC_LAZY_INT (PDC_LAZY_TIMEOUT | (PDC_LAZY_FRAMECOUNT << 24))
  94. #define PDC_INTMASK_OFFSET 0x24
  95. #define PDC_INTSTATUS_OFFSET 0x20
  96. #define PDC_RCVLAZY0_OFFSET (0x30 + 4 * PDC_RINGSET)
  97. /*
  98. * For SPU2, configure MDE_CKSUM_CONTROL to write 17 bytes of metadata
  99. * before frame
  100. */
  101. #define PDC_SPU2_RESP_HDR_LEN 17
  102. #define PDC_CKSUM_CTRL BIT(27)
  103. #define PDC_CKSUM_CTRL_OFFSET 0x400
  104. #define PDC_SPUM_RESP_HDR_LEN 32
  105. /*
  106. * Sets the following bits for write to transmit control reg:
  107. * 0 - XmtEn - enable activity on the tx channel
  108. * 11 - PtyChkDisable - parity check is disabled
  109. * 20:18 - BurstLen = 3 -> 2^7 = 128 byte data reads from memory
  110. */
  111. #define PDC_TX_CTL 0x000C0801
  112. /*
  113. * Sets the following bits for write to receive control reg:
  114. * 0 - RcvEn - enable activity on the rx channel
  115. * 7:1 - RcvOffset - size in bytes of status region at start of rx frame buf
  116. * 9 - SepRxHdrDescEn - place start of new frames only in descriptors
  117. * that have StartOfFrame set
  118. * 10 - OflowContinue - on rx FIFO overflow, clear rx fifo, discard all
  119. * remaining bytes in current frame, report error
  120. * in rx frame status for current frame
  121. * 11 - PtyChkDisable - parity check is disabled
  122. * 20:18 - BurstLen = 3 -> 2^7 = 128 byte data reads from memory
  123. */
  124. #define PDC_RX_CTL 0x000C0E01
  125. #define CRYPTO_D64_RS0_CD_MASK ((PDC_RING_ENTRIES * RING_ENTRY_SIZE) - 1)
  126. /* descriptor flags */
  127. #define D64_CTRL1_EOT BIT(28) /* end of descriptor table */
  128. #define D64_CTRL1_IOC BIT(29) /* interrupt on complete */
  129. #define D64_CTRL1_EOF BIT(30) /* end of frame */
  130. #define D64_CTRL1_SOF BIT(31) /* start of frame */
  131. #define RX_STATUS_OVERFLOW 0x00800000
  132. #define RX_STATUS_LEN 0x0000FFFF
  133. #define PDC_TXREGS_OFFSET 0x200
  134. #define PDC_RXREGS_OFFSET 0x220
  135. /* Maximum size buffer the DMA engine can handle */
  136. #define PDC_DMA_BUF_MAX 16384
  137. struct pdc_dma_map {
  138. void *ctx; /* opaque context associated with frame */
  139. };
  140. /* dma descriptor */
  141. struct dma64dd {
  142. u32 ctrl1; /* misc control bits */
  143. u32 ctrl2; /* buffer count and address extension */
  144. u32 addrlow; /* memory address of the date buffer, bits 31:0 */
  145. u32 addrhigh; /* memory address of the date buffer, bits 63:32 */
  146. };
  147. /* dma registers per channel(xmt or rcv) */
  148. struct dma64_regs {
  149. u32 control; /* enable, et al */
  150. u32 ptr; /* last descriptor posted to chip */
  151. u32 addrlow; /* descriptor ring base address low 32-bits */
  152. u32 addrhigh; /* descriptor ring base address bits 63:32 */
  153. u32 status0; /* last rx descriptor written by hw */
  154. u32 status1; /* driver does not use */
  155. };
  156. /* cpp contortions to concatenate w/arg prescan */
  157. #ifndef PAD
  158. #define _PADLINE(line) pad ## line
  159. #define _XSTR(line) _PADLINE(line)
  160. #define PAD _XSTR(__LINE__)
  161. #endif /* PAD */
  162. /* dma registers. matches hw layout. */
  163. struct dma64 {
  164. struct dma64_regs dmaxmt; /* dma tx */
  165. u32 PAD[2];
  166. struct dma64_regs dmarcv; /* dma rx */
  167. u32 PAD[2];
  168. };
  169. /* PDC registers */
  170. struct pdc_regs {
  171. u32 devcontrol; /* 0x000 */
  172. u32 devstatus; /* 0x004 */
  173. u32 PAD;
  174. u32 biststatus; /* 0x00c */
  175. u32 PAD[4];
  176. u32 intstatus; /* 0x020 */
  177. u32 intmask; /* 0x024 */
  178. u32 gptimer; /* 0x028 */
  179. u32 PAD;
  180. u32 intrcvlazy_0; /* 0x030 */
  181. u32 intrcvlazy_1; /* 0x034 */
  182. u32 intrcvlazy_2; /* 0x038 */
  183. u32 intrcvlazy_3; /* 0x03c */
  184. u32 PAD[48];
  185. u32 removed_intrecvlazy; /* 0x100 */
  186. u32 flowctlthresh; /* 0x104 */
  187. u32 wrrthresh; /* 0x108 */
  188. u32 gmac_idle_cnt_thresh; /* 0x10c */
  189. u32 PAD[4];
  190. u32 ifioaccessaddr; /* 0x120 */
  191. u32 ifioaccessbyte; /* 0x124 */
  192. u32 ifioaccessdata; /* 0x128 */
  193. u32 PAD[21];
  194. u32 phyaccess; /* 0x180 */
  195. u32 PAD;
  196. u32 phycontrol; /* 0x188 */
  197. u32 txqctl; /* 0x18c */
  198. u32 rxqctl; /* 0x190 */
  199. u32 gpioselect; /* 0x194 */
  200. u32 gpio_output_en; /* 0x198 */
  201. u32 PAD; /* 0x19c */
  202. u32 txq_rxq_mem_ctl; /* 0x1a0 */
  203. u32 memory_ecc_status; /* 0x1a4 */
  204. u32 serdes_ctl; /* 0x1a8 */
  205. u32 serdes_status0; /* 0x1ac */
  206. u32 serdes_status1; /* 0x1b0 */
  207. u32 PAD[11]; /* 0x1b4-1dc */
  208. u32 clk_ctl_st; /* 0x1e0 */
  209. u32 hw_war; /* 0x1e4 */
  210. u32 pwrctl; /* 0x1e8 */
  211. u32 PAD[5];
  212. #define PDC_NUM_DMA_RINGS 4
  213. struct dma64 dmaregs[PDC_NUM_DMA_RINGS]; /* 0x0200 - 0x2fc */
  214. /* more registers follow, but we don't use them */
  215. };
  216. /* structure for allocating/freeing DMA rings */
  217. struct pdc_ring_alloc {
  218. dma_addr_t dmabase; /* DMA address of start of ring */
  219. void *vbase; /* base kernel virtual address of ring */
  220. u32 size; /* ring allocation size in bytes */
  221. };
  222. /* PDC state structure */
  223. struct pdc_state {
  224. /* synchronize access to this PDC state structure */
  225. spinlock_t pdc_lock;
  226. /* Index of the PDC whose state is in this structure instance */
  227. u8 pdc_idx;
  228. /* Platform device for this PDC instance */
  229. struct platform_device *pdev;
  230. /*
  231. * Each PDC instance has a mailbox controller. PDC receives request
  232. * messages through mailboxes, and sends response messages through the
  233. * mailbox framework.
  234. */
  235. struct mbox_controller mbc;
  236. unsigned int pdc_irq;
  237. /*
  238. * Last interrupt status read from PDC device. Saved in interrupt
  239. * handler so the handler can clear the interrupt in the device,
  240. * and the interrupt thread called later can know which interrupt
  241. * bits are active.
  242. */
  243. unsigned long intstatus;
  244. /* Number of bytes of receive status prior to each rx frame */
  245. u32 rx_status_len;
  246. /* Whether a BCM header is prepended to each frame */
  247. bool use_bcm_hdr;
  248. /* Sum of length of BCM header and rx status header */
  249. u32 pdc_resp_hdr_len;
  250. /* The base virtual address of DMA hw registers */
  251. void __iomem *pdc_reg_vbase;
  252. /* Pool for allocation of DMA rings */
  253. struct dma_pool *ring_pool;
  254. /* Pool for allocation of metadata buffers for response messages */
  255. struct dma_pool *rx_buf_pool;
  256. /*
  257. * The base virtual address of DMA tx/rx descriptor rings. Corresponding
  258. * DMA address and size of ring allocation.
  259. */
  260. struct pdc_ring_alloc tx_ring_alloc;
  261. struct pdc_ring_alloc rx_ring_alloc;
  262. struct pdc_regs *regs; /* start of PDC registers */
  263. struct dma64_regs *txregs_64; /* dma tx engine registers */
  264. struct dma64_regs *rxregs_64; /* dma rx engine registers */
  265. /*
  266. * Arrays of PDC_RING_ENTRIES descriptors
  267. * To use multiple ringsets, this needs to be extended
  268. */
  269. struct dma64dd *txd_64; /* tx descriptor ring */
  270. struct dma64dd *rxd_64; /* rx descriptor ring */
  271. /* descriptor ring sizes */
  272. u32 ntxd; /* # tx descriptors */
  273. u32 nrxd; /* # rx descriptors */
  274. u32 nrxpost; /* # rx buffers to keep posted */
  275. u32 ntxpost; /* max number of tx buffers that can be posted */
  276. /*
  277. * Index of next tx descriptor to reclaim. That is, the descriptor
  278. * index of the oldest tx buffer for which the host has yet to process
  279. * the corresponding response.
  280. */
  281. u32 txin;
  282. /*
  283. * Index of the first receive descriptor for the sequence of
  284. * message fragments currently under construction. Used to build up
  285. * the rxin_numd count for a message. Updated to rxout when the host
  286. * starts a new sequence of rx buffers for a new message.
  287. */
  288. u32 tx_msg_start;
  289. /* Index of next tx descriptor to post. */
  290. u32 txout;
  291. /*
  292. * Number of tx descriptors associated with the message that starts
  293. * at this tx descriptor index.
  294. */
  295. u32 txin_numd[PDC_RING_ENTRIES];
  296. /*
  297. * Index of next rx descriptor to reclaim. This is the index of
  298. * the next descriptor whose data has yet to be processed by the host.
  299. */
  300. u32 rxin;
  301. /*
  302. * Index of the first receive descriptor for the sequence of
  303. * message fragments currently under construction. Used to build up
  304. * the rxin_numd count for a message. Updated to rxout when the host
  305. * starts a new sequence of rx buffers for a new message.
  306. */
  307. u32 rx_msg_start;
  308. /*
  309. * Saved value of current hardware rx descriptor index.
  310. * The last rx buffer written by the hw is the index previous to
  311. * this one.
  312. */
  313. u32 last_rx_curr;
  314. /* Index of next rx descriptor to post. */
  315. u32 rxout;
  316. /*
  317. * opaque context associated with frame that starts at each
  318. * rx ring index.
  319. */
  320. void *rxp_ctx[PDC_RING_ENTRIES];
  321. /*
  322. * Scatterlists used to form request and reply frames beginning at a
  323. * given ring index. Retained in order to unmap each sg after reply
  324. * is processed
  325. */
  326. struct scatterlist *src_sg[PDC_RING_ENTRIES];
  327. struct scatterlist *dst_sg[PDC_RING_ENTRIES];
  328. /*
  329. * Number of rx descriptors associated with the message that starts
  330. * at this descriptor index. Not set for every index. For example,
  331. * if descriptor index i points to a scatterlist with 4 entries, then
  332. * the next three descriptor indexes don't have a value set.
  333. */
  334. u32 rxin_numd[PDC_RING_ENTRIES];
  335. void *resp_hdr[PDC_RING_ENTRIES];
  336. dma_addr_t resp_hdr_daddr[PDC_RING_ENTRIES];
  337. struct dentry *debugfs_stats; /* debug FS stats file for this PDC */
  338. /* counters */
  339. u32 pdc_requests; /* number of request messages submitted */
  340. u32 pdc_replies; /* number of reply messages received */
  341. u32 txnobuf; /* count of tx ring full */
  342. u32 rxnobuf; /* count of rx ring full */
  343. u32 rx_oflow; /* count of rx overflows */
  344. };
  345. /* Global variables */
  346. struct pdc_globals {
  347. /* Actual number of SPUs in hardware, as reported by device tree */
  348. u32 num_spu;
  349. };
  350. static struct pdc_globals pdcg;
  351. /* top level debug FS directory for PDC driver */
  352. static struct dentry *debugfs_dir;
  353. static ssize_t pdc_debugfs_read(struct file *filp, char __user *ubuf,
  354. size_t count, loff_t *offp)
  355. {
  356. struct pdc_state *pdcs;
  357. char *buf;
  358. ssize_t ret, out_offset, out_count;
  359. out_count = 512;
  360. buf = kmalloc(out_count, GFP_KERNEL);
  361. if (!buf)
  362. return -ENOMEM;
  363. pdcs = filp->private_data;
  364. out_offset = 0;
  365. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  366. "SPU %u stats:\n", pdcs->pdc_idx);
  367. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  368. "PDC requests............%u\n",
  369. pdcs->pdc_requests);
  370. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  371. "PDC responses...........%u\n",
  372. pdcs->pdc_replies);
  373. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  374. "Tx err ring full........%u\n",
  375. pdcs->txnobuf);
  376. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  377. "Rx err ring full........%u\n",
  378. pdcs->rxnobuf);
  379. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  380. "Receive overflow........%u\n",
  381. pdcs->rx_oflow);
  382. if (out_offset > out_count)
  383. out_offset = out_count;
  384. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  385. kfree(buf);
  386. return ret;
  387. }
  388. static const struct file_operations pdc_debugfs_stats = {
  389. .owner = THIS_MODULE,
  390. .open = simple_open,
  391. .read = pdc_debugfs_read,
  392. };
  393. /**
  394. * pdc_setup_debugfs() - Create the debug FS directories. If the top-level
  395. * directory has not yet been created, create it now. Create a stats file in
  396. * this directory for a SPU.
  397. * @pdcs: PDC state structure
  398. */
  399. static void pdc_setup_debugfs(struct pdc_state *pdcs)
  400. {
  401. char spu_stats_name[16];
  402. if (!debugfs_initialized())
  403. return;
  404. snprintf(spu_stats_name, 16, "pdc%d_stats", pdcs->pdc_idx);
  405. if (!debugfs_dir)
  406. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  407. pdcs->debugfs_stats = debugfs_create_file(spu_stats_name, S_IRUSR,
  408. debugfs_dir, pdcs,
  409. &pdc_debugfs_stats);
  410. }
  411. static void pdc_free_debugfs(void)
  412. {
  413. if (debugfs_dir && simple_empty(debugfs_dir)) {
  414. debugfs_remove_recursive(debugfs_dir);
  415. debugfs_dir = NULL;
  416. }
  417. }
  418. /**
  419. * pdc_build_rxd() - Build DMA descriptor to receive SPU result.
  420. * @pdcs: PDC state for SPU that will generate result
  421. * @dma_addr: DMA address of buffer that descriptor is being built for
  422. * @buf_len: Length of the receive buffer, in bytes
  423. * @flags: Flags to be stored in descriptor
  424. */
  425. static inline void
  426. pdc_build_rxd(struct pdc_state *pdcs, dma_addr_t dma_addr,
  427. u32 buf_len, u32 flags)
  428. {
  429. struct device *dev = &pdcs->pdev->dev;
  430. dev_dbg(dev,
  431. "Writing rx descriptor for PDC %u at index %u with length %u. flags %#x\n",
  432. pdcs->pdc_idx, pdcs->rxout, buf_len, flags);
  433. iowrite32(lower_32_bits(dma_addr),
  434. (void *)&pdcs->rxd_64[pdcs->rxout].addrlow);
  435. iowrite32(upper_32_bits(dma_addr),
  436. (void *)&pdcs->rxd_64[pdcs->rxout].addrhigh);
  437. iowrite32(flags, (void *)&pdcs->rxd_64[pdcs->rxout].ctrl1);
  438. iowrite32(buf_len, (void *)&pdcs->rxd_64[pdcs->rxout].ctrl2);
  439. /* bump ring index and return */
  440. pdcs->rxout = NEXTRXD(pdcs->rxout, pdcs->nrxpost);
  441. }
  442. /**
  443. * pdc_build_txd() - Build a DMA descriptor to transmit a SPU request to
  444. * hardware.
  445. * @pdcs: PDC state for the SPU that will process this request
  446. * @dma_addr: DMA address of packet to be transmitted
  447. * @buf_len: Length of tx buffer, in bytes
  448. * @flags: Flags to be stored in descriptor
  449. */
  450. static inline void
  451. pdc_build_txd(struct pdc_state *pdcs, dma_addr_t dma_addr, u32 buf_len,
  452. u32 flags)
  453. {
  454. struct device *dev = &pdcs->pdev->dev;
  455. dev_dbg(dev,
  456. "Writing tx descriptor for PDC %u at index %u with length %u, flags %#x\n",
  457. pdcs->pdc_idx, pdcs->txout, buf_len, flags);
  458. iowrite32(lower_32_bits(dma_addr),
  459. (void *)&pdcs->txd_64[pdcs->txout].addrlow);
  460. iowrite32(upper_32_bits(dma_addr),
  461. (void *)&pdcs->txd_64[pdcs->txout].addrhigh);
  462. iowrite32(flags, (void *)&pdcs->txd_64[pdcs->txout].ctrl1);
  463. iowrite32(buf_len, (void *)&pdcs->txd_64[pdcs->txout].ctrl2);
  464. /* bump ring index and return */
  465. pdcs->txout = NEXTTXD(pdcs->txout, pdcs->ntxpost);
  466. }
  467. /**
  468. * pdc_receive() - Receive a response message from a given SPU.
  469. * @pdcs: PDC state for the SPU to receive from
  470. * @mssg: mailbox message to be returned to client
  471. *
  472. * When the return code indicates success, the response message is available in
  473. * the receive buffers provided prior to submission of the request.
  474. *
  475. * Input:
  476. * pdcs - PDC state structure for the SPU to be polled
  477. * mssg - mailbox message to be returned to client. This function sets the
  478. * context pointer on the message to help the client associate the
  479. * response with a request.
  480. *
  481. * Return: PDC_SUCCESS if one or more receive descriptors was processed
  482. * -EAGAIN indicates that no response message is available
  483. * -EIO an error occurred
  484. */
  485. static int
  486. pdc_receive(struct pdc_state *pdcs, struct brcm_message *mssg)
  487. {
  488. struct device *dev = &pdcs->pdev->dev;
  489. u32 len, rx_status;
  490. u32 num_frags;
  491. int i;
  492. u8 *resp_hdr; /* virtual addr of start of resp message DMA header */
  493. u32 frags_rdy; /* number of fragments ready to read */
  494. u32 rx_idx; /* ring index of start of receive frame */
  495. dma_addr_t resp_hdr_daddr;
  496. spin_lock(&pdcs->pdc_lock);
  497. /*
  498. * return if a complete response message is not yet ready.
  499. * rxin_numd[rxin] is the number of fragments in the next msg
  500. * to read.
  501. */
  502. frags_rdy = NRXDACTIVE(pdcs->rxin, pdcs->last_rx_curr, pdcs->nrxpost);
  503. if ((frags_rdy == 0) || (frags_rdy < pdcs->rxin_numd[pdcs->rxin])) {
  504. /* See if the hw has written more fragments than we know */
  505. pdcs->last_rx_curr =
  506. (ioread32((void *)&pdcs->rxregs_64->status0) &
  507. CRYPTO_D64_RS0_CD_MASK) / RING_ENTRY_SIZE;
  508. frags_rdy = NRXDACTIVE(pdcs->rxin, pdcs->last_rx_curr,
  509. pdcs->nrxpost);
  510. if ((frags_rdy == 0) ||
  511. (frags_rdy < pdcs->rxin_numd[pdcs->rxin])) {
  512. /* No response ready */
  513. spin_unlock(&pdcs->pdc_lock);
  514. return -EAGAIN;
  515. }
  516. /* can't read descriptors/data until write index is read */
  517. rmb();
  518. }
  519. num_frags = pdcs->txin_numd[pdcs->txin];
  520. dma_unmap_sg(dev, pdcs->src_sg[pdcs->txin],
  521. sg_nents(pdcs->src_sg[pdcs->txin]), DMA_TO_DEVICE);
  522. for (i = 0; i < num_frags; i++)
  523. pdcs->txin = NEXTTXD(pdcs->txin, pdcs->ntxpost);
  524. dev_dbg(dev, "PDC %u reclaimed %d tx descriptors",
  525. pdcs->pdc_idx, num_frags);
  526. rx_idx = pdcs->rxin;
  527. num_frags = pdcs->rxin_numd[rx_idx];
  528. /* Return opaque context with result */
  529. mssg->ctx = pdcs->rxp_ctx[rx_idx];
  530. pdcs->rxp_ctx[rx_idx] = NULL;
  531. resp_hdr = pdcs->resp_hdr[rx_idx];
  532. resp_hdr_daddr = pdcs->resp_hdr_daddr[rx_idx];
  533. dma_unmap_sg(dev, pdcs->dst_sg[rx_idx],
  534. sg_nents(pdcs->dst_sg[rx_idx]), DMA_FROM_DEVICE);
  535. for (i = 0; i < num_frags; i++)
  536. pdcs->rxin = NEXTRXD(pdcs->rxin, pdcs->nrxpost);
  537. spin_unlock(&pdcs->pdc_lock);
  538. dev_dbg(dev, "PDC %u reclaimed %d rx descriptors",
  539. pdcs->pdc_idx, num_frags);
  540. dev_dbg(dev,
  541. "PDC %u txin %u, txout %u, rxin %u, rxout %u, last_rx_curr %u\n",
  542. pdcs->pdc_idx, pdcs->txin, pdcs->txout, pdcs->rxin,
  543. pdcs->rxout, pdcs->last_rx_curr);
  544. if (pdcs->pdc_resp_hdr_len == PDC_SPUM_RESP_HDR_LEN) {
  545. /*
  546. * For SPU-M, get length of response msg and rx overflow status.
  547. */
  548. rx_status = *((u32 *)resp_hdr);
  549. len = rx_status & RX_STATUS_LEN;
  550. dev_dbg(dev,
  551. "SPU response length %u bytes", len);
  552. if (unlikely(((rx_status & RX_STATUS_OVERFLOW) || (!len)))) {
  553. if (rx_status & RX_STATUS_OVERFLOW) {
  554. dev_err_ratelimited(dev,
  555. "crypto receive overflow");
  556. pdcs->rx_oflow++;
  557. } else {
  558. dev_info_ratelimited(dev, "crypto rx len = 0");
  559. }
  560. return -EIO;
  561. }
  562. }
  563. dma_pool_free(pdcs->rx_buf_pool, resp_hdr, resp_hdr_daddr);
  564. pdcs->pdc_replies++;
  565. /* if we read one or more rx descriptors, claim success */
  566. if (num_frags > 0)
  567. return PDC_SUCCESS;
  568. else
  569. return -EIO;
  570. }
  571. /**
  572. * pdc_tx_list_sg_add() - Add the buffers in a scatterlist to the transmit
  573. * descriptors for a given SPU. The scatterlist buffers contain the data for a
  574. * SPU request message.
  575. * @spu_idx: The index of the SPU to submit the request to, [0, max_spu)
  576. * @sg: Scatterlist whose buffers contain part of the SPU request
  577. *
  578. * If a scatterlist buffer is larger than PDC_DMA_BUF_MAX, multiple descriptors
  579. * are written for that buffer, each <= PDC_DMA_BUF_MAX byte in length.
  580. *
  581. * Return: PDC_SUCCESS if successful
  582. * < 0 otherwise
  583. */
  584. static int pdc_tx_list_sg_add(struct pdc_state *pdcs, struct scatterlist *sg)
  585. {
  586. u32 flags = 0;
  587. u32 eot;
  588. u32 tx_avail;
  589. /*
  590. * Num descriptors needed. Conservatively assume we need a descriptor
  591. * for every entry in sg.
  592. */
  593. u32 num_desc;
  594. u32 desc_w = 0; /* Number of tx descriptors written */
  595. u32 bufcnt; /* Number of bytes of buffer pointed to by descriptor */
  596. dma_addr_t databufptr; /* DMA address to put in descriptor */
  597. num_desc = (u32)sg_nents(sg);
  598. /* check whether enough tx descriptors are available */
  599. tx_avail = pdcs->ntxpost - NTXDACTIVE(pdcs->txin, pdcs->txout,
  600. pdcs->ntxpost);
  601. if (unlikely(num_desc > tx_avail)) {
  602. pdcs->txnobuf++;
  603. return -ENOSPC;
  604. }
  605. /* build tx descriptors */
  606. if (pdcs->tx_msg_start == pdcs->txout) {
  607. /* Start of frame */
  608. pdcs->txin_numd[pdcs->tx_msg_start] = 0;
  609. pdcs->src_sg[pdcs->txout] = sg;
  610. flags = D64_CTRL1_SOF;
  611. }
  612. while (sg) {
  613. if (unlikely(pdcs->txout == (pdcs->ntxd - 1)))
  614. eot = D64_CTRL1_EOT;
  615. else
  616. eot = 0;
  617. /*
  618. * If sg buffer larger than PDC limit, split across
  619. * multiple descriptors
  620. */
  621. bufcnt = sg_dma_len(sg);
  622. databufptr = sg_dma_address(sg);
  623. while (bufcnt > PDC_DMA_BUF_MAX) {
  624. pdc_build_txd(pdcs, databufptr, PDC_DMA_BUF_MAX,
  625. flags | eot);
  626. desc_w++;
  627. bufcnt -= PDC_DMA_BUF_MAX;
  628. databufptr += PDC_DMA_BUF_MAX;
  629. if (unlikely(pdcs->txout == (pdcs->ntxd - 1)))
  630. eot = D64_CTRL1_EOT;
  631. else
  632. eot = 0;
  633. }
  634. sg = sg_next(sg);
  635. if (!sg)
  636. /* Writing last descriptor for frame */
  637. flags |= (D64_CTRL1_EOF | D64_CTRL1_IOC);
  638. pdc_build_txd(pdcs, databufptr, bufcnt, flags | eot);
  639. desc_w++;
  640. /* Clear start of frame after first descriptor */
  641. flags &= ~D64_CTRL1_SOF;
  642. }
  643. pdcs->txin_numd[pdcs->tx_msg_start] += desc_w;
  644. return PDC_SUCCESS;
  645. }
  646. /**
  647. * pdc_tx_list_final() - Initiate DMA transfer of last frame written to tx
  648. * ring.
  649. * @pdcs: PDC state for SPU to process the request
  650. *
  651. * Sets the index of the last descriptor written in both the rx and tx ring.
  652. *
  653. * Return: PDC_SUCCESS
  654. */
  655. static int pdc_tx_list_final(struct pdc_state *pdcs)
  656. {
  657. /*
  658. * write barrier to ensure all register writes are complete
  659. * before chip starts to process new request
  660. */
  661. wmb();
  662. iowrite32(pdcs->rxout << 4, (void *)&pdcs->rxregs_64->ptr);
  663. iowrite32(pdcs->txout << 4, (void *)&pdcs->txregs_64->ptr);
  664. pdcs->pdc_requests++;
  665. return PDC_SUCCESS;
  666. }
  667. /**
  668. * pdc_rx_list_init() - Start a new receive descriptor list for a given PDC.
  669. * @pdcs: PDC state for SPU handling request
  670. * @dst_sg: scatterlist providing rx buffers for response to be returned to
  671. * mailbox client
  672. * @ctx: Opaque context for this request
  673. *
  674. * Posts a single receive descriptor to hold the metadata that precedes a
  675. * response. For example, with SPU-M, the metadata is a 32-byte DMA header and
  676. * an 8-byte BCM header. Moves the msg_start descriptor indexes for both tx and
  677. * rx to indicate the start of a new message.
  678. *
  679. * Return: PDC_SUCCESS if successful
  680. * < 0 if an error (e.g., rx ring is full)
  681. */
  682. static int pdc_rx_list_init(struct pdc_state *pdcs, struct scatterlist *dst_sg,
  683. void *ctx)
  684. {
  685. u32 flags = 0;
  686. u32 rx_avail;
  687. u32 rx_pkt_cnt = 1; /* Adding a single rx buffer */
  688. dma_addr_t daddr;
  689. void *vaddr;
  690. rx_avail = pdcs->nrxpost - NRXDACTIVE(pdcs->rxin, pdcs->rxout,
  691. pdcs->nrxpost);
  692. if (unlikely(rx_pkt_cnt > rx_avail)) {
  693. pdcs->rxnobuf++;
  694. return -ENOSPC;
  695. }
  696. /* allocate a buffer for the dma rx status */
  697. vaddr = dma_pool_zalloc(pdcs->rx_buf_pool, GFP_ATOMIC, &daddr);
  698. if (!vaddr)
  699. return -ENOMEM;
  700. /*
  701. * Update msg_start indexes for both tx and rx to indicate the start
  702. * of a new sequence of descriptor indexes that contain the fragments
  703. * of the same message.
  704. */
  705. pdcs->rx_msg_start = pdcs->rxout;
  706. pdcs->tx_msg_start = pdcs->txout;
  707. /* This is always the first descriptor in the receive sequence */
  708. flags = D64_CTRL1_SOF;
  709. pdcs->rxin_numd[pdcs->rx_msg_start] = 1;
  710. if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
  711. flags |= D64_CTRL1_EOT;
  712. pdcs->rxp_ctx[pdcs->rxout] = ctx;
  713. pdcs->dst_sg[pdcs->rxout] = dst_sg;
  714. pdcs->resp_hdr[pdcs->rxout] = vaddr;
  715. pdcs->resp_hdr_daddr[pdcs->rxout] = daddr;
  716. pdc_build_rxd(pdcs, daddr, pdcs->pdc_resp_hdr_len, flags);
  717. return PDC_SUCCESS;
  718. }
  719. /**
  720. * pdc_rx_list_sg_add() - Add the buffers in a scatterlist to the receive
  721. * descriptors for a given SPU. The caller must have already DMA mapped the
  722. * scatterlist.
  723. * @spu_idx: Indicates which SPU the buffers are for
  724. * @sg: Scatterlist whose buffers are added to the receive ring
  725. *
  726. * If a receive buffer in the scatterlist is larger than PDC_DMA_BUF_MAX,
  727. * multiple receive descriptors are written, each with a buffer <=
  728. * PDC_DMA_BUF_MAX.
  729. *
  730. * Return: PDC_SUCCESS if successful
  731. * < 0 otherwise (e.g., receive ring is full)
  732. */
  733. static int pdc_rx_list_sg_add(struct pdc_state *pdcs, struct scatterlist *sg)
  734. {
  735. u32 flags = 0;
  736. u32 rx_avail;
  737. /*
  738. * Num descriptors needed. Conservatively assume we need a descriptor
  739. * for every entry from our starting point in the scatterlist.
  740. */
  741. u32 num_desc;
  742. u32 desc_w = 0; /* Number of tx descriptors written */
  743. u32 bufcnt; /* Number of bytes of buffer pointed to by descriptor */
  744. dma_addr_t databufptr; /* DMA address to put in descriptor */
  745. num_desc = (u32)sg_nents(sg);
  746. rx_avail = pdcs->nrxpost - NRXDACTIVE(pdcs->rxin, pdcs->rxout,
  747. pdcs->nrxpost);
  748. if (unlikely(num_desc > rx_avail)) {
  749. pdcs->rxnobuf++;
  750. return -ENOSPC;
  751. }
  752. while (sg) {
  753. if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
  754. flags = D64_CTRL1_EOT;
  755. else
  756. flags = 0;
  757. /*
  758. * If sg buffer larger than PDC limit, split across
  759. * multiple descriptors
  760. */
  761. bufcnt = sg_dma_len(sg);
  762. databufptr = sg_dma_address(sg);
  763. while (bufcnt > PDC_DMA_BUF_MAX) {
  764. pdc_build_rxd(pdcs, databufptr, PDC_DMA_BUF_MAX, flags);
  765. desc_w++;
  766. bufcnt -= PDC_DMA_BUF_MAX;
  767. databufptr += PDC_DMA_BUF_MAX;
  768. if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
  769. flags = D64_CTRL1_EOT;
  770. else
  771. flags = 0;
  772. }
  773. pdc_build_rxd(pdcs, databufptr, bufcnt, flags);
  774. desc_w++;
  775. sg = sg_next(sg);
  776. }
  777. pdcs->rxin_numd[pdcs->rx_msg_start] += desc_w;
  778. return PDC_SUCCESS;
  779. }
  780. /**
  781. * pdc_irq_handler() - Interrupt handler called in interrupt context.
  782. * @irq: Interrupt number that has fired
  783. * @cookie: PDC state for DMA engine that generated the interrupt
  784. *
  785. * We have to clear the device interrupt status flags here. So cache the
  786. * status for later use in the thread function. Other than that, just return
  787. * WAKE_THREAD to invoke the thread function.
  788. *
  789. * Return: IRQ_WAKE_THREAD if interrupt is ours
  790. * IRQ_NONE otherwise
  791. */
  792. static irqreturn_t pdc_irq_handler(int irq, void *cookie)
  793. {
  794. struct pdc_state *pdcs = cookie;
  795. u32 intstatus = ioread32(pdcs->pdc_reg_vbase + PDC_INTSTATUS_OFFSET);
  796. if (intstatus & PDC_XMTINTEN_0)
  797. set_bit(PDC_XMTINT_0, &pdcs->intstatus);
  798. if (intstatus & PDC_RCVINTEN_0)
  799. set_bit(PDC_RCVINT_0, &pdcs->intstatus);
  800. /* Clear interrupt flags in device */
  801. iowrite32(intstatus, pdcs->pdc_reg_vbase + PDC_INTSTATUS_OFFSET);
  802. /* Wakeup IRQ thread */
  803. if (pdcs && (irq == pdcs->pdc_irq) && (intstatus & PDC_INTMASK))
  804. return IRQ_WAKE_THREAD;
  805. return IRQ_NONE;
  806. }
  807. /**
  808. * pdc_irq_thread() - Function invoked on deferred thread when a DMA tx has
  809. * completed or data is available to receive.
  810. * @irq: Interrupt number
  811. * @cookie: PDC state for PDC that generated the interrupt
  812. *
  813. * On DMA tx complete, notify the mailbox client. On DMA rx complete, process
  814. * as many SPU response messages as are available and send each to the mailbox
  815. * client.
  816. *
  817. * Return: IRQ_HANDLED if we recognized and handled the interrupt
  818. * IRQ_NONE otherwise
  819. */
  820. static irqreturn_t pdc_irq_thread(int irq, void *cookie)
  821. {
  822. struct pdc_state *pdcs = cookie;
  823. struct mbox_controller *mbc;
  824. struct mbox_chan *chan;
  825. bool tx_int;
  826. bool rx_int;
  827. int rx_status;
  828. struct brcm_message mssg;
  829. tx_int = test_and_clear_bit(PDC_XMTINT_0, &pdcs->intstatus);
  830. rx_int = test_and_clear_bit(PDC_RCVINT_0, &pdcs->intstatus);
  831. if (pdcs && (tx_int || rx_int)) {
  832. dev_dbg(&pdcs->pdev->dev,
  833. "%s() got irq %d with tx_int %s, rx_int %s",
  834. __func__, irq,
  835. tx_int ? "set" : "clear", rx_int ? "set" : "clear");
  836. mbc = &pdcs->mbc;
  837. chan = &mbc->chans[0];
  838. if (tx_int) {
  839. dev_dbg(&pdcs->pdev->dev, "%s(): tx done", __func__);
  840. /* only one frame in flight at a time */
  841. mbox_chan_txdone(chan, PDC_SUCCESS);
  842. }
  843. if (rx_int) {
  844. while (1) {
  845. /* Could be many frames ready */
  846. memset(&mssg, 0, sizeof(mssg));
  847. mssg.type = BRCM_MESSAGE_SPU;
  848. rx_status = pdc_receive(pdcs, &mssg);
  849. if (rx_status >= 0) {
  850. dev_dbg(&pdcs->pdev->dev,
  851. "%s(): invoking client rx cb",
  852. __func__);
  853. mbox_chan_received_data(chan, &mssg);
  854. } else {
  855. dev_dbg(&pdcs->pdev->dev,
  856. "%s(): no SPU response available",
  857. __func__);
  858. break;
  859. }
  860. }
  861. }
  862. return IRQ_HANDLED;
  863. }
  864. return IRQ_NONE;
  865. }
  866. /**
  867. * pdc_ring_init() - Allocate DMA rings and initialize constant fields of
  868. * descriptors in one ringset.
  869. * @pdcs: PDC instance state
  870. * @ringset: index of ringset being used
  871. *
  872. * Return: PDC_SUCCESS if ring initialized
  873. * < 0 otherwise
  874. */
  875. static int pdc_ring_init(struct pdc_state *pdcs, int ringset)
  876. {
  877. int i;
  878. int err = PDC_SUCCESS;
  879. struct dma64 *dma_reg;
  880. struct device *dev = &pdcs->pdev->dev;
  881. struct pdc_ring_alloc tx;
  882. struct pdc_ring_alloc rx;
  883. /* Allocate tx ring */
  884. tx.vbase = dma_pool_zalloc(pdcs->ring_pool, GFP_KERNEL, &tx.dmabase);
  885. if (!tx.vbase) {
  886. err = -ENOMEM;
  887. goto done;
  888. }
  889. /* Allocate rx ring */
  890. rx.vbase = dma_pool_zalloc(pdcs->ring_pool, GFP_KERNEL, &rx.dmabase);
  891. if (!rx.vbase) {
  892. err = -ENOMEM;
  893. goto fail_dealloc;
  894. }
  895. dev_dbg(dev, " - base DMA addr of tx ring %pad", &tx.dmabase);
  896. dev_dbg(dev, " - base virtual addr of tx ring %p", tx.vbase);
  897. dev_dbg(dev, " - base DMA addr of rx ring %pad", &rx.dmabase);
  898. dev_dbg(dev, " - base virtual addr of rx ring %p", rx.vbase);
  899. /* lock after ring allocation to avoid scheduling while atomic */
  900. spin_lock(&pdcs->pdc_lock);
  901. memcpy(&pdcs->tx_ring_alloc, &tx, sizeof(tx));
  902. memcpy(&pdcs->rx_ring_alloc, &rx, sizeof(rx));
  903. pdcs->rxin = 0;
  904. pdcs->rx_msg_start = 0;
  905. pdcs->last_rx_curr = 0;
  906. pdcs->rxout = 0;
  907. pdcs->txin = 0;
  908. pdcs->tx_msg_start = 0;
  909. pdcs->txout = 0;
  910. /* Set descriptor array base addresses */
  911. pdcs->txd_64 = (struct dma64dd *)pdcs->tx_ring_alloc.vbase;
  912. pdcs->rxd_64 = (struct dma64dd *)pdcs->rx_ring_alloc.vbase;
  913. /* Tell device the base DMA address of each ring */
  914. dma_reg = &pdcs->regs->dmaregs[ringset];
  915. iowrite32(lower_32_bits(pdcs->tx_ring_alloc.dmabase),
  916. (void *)&dma_reg->dmaxmt.addrlow);
  917. iowrite32(upper_32_bits(pdcs->tx_ring_alloc.dmabase),
  918. (void *)&dma_reg->dmaxmt.addrhigh);
  919. iowrite32(lower_32_bits(pdcs->rx_ring_alloc.dmabase),
  920. (void *)&dma_reg->dmarcv.addrlow);
  921. iowrite32(upper_32_bits(pdcs->rx_ring_alloc.dmabase),
  922. (void *)&dma_reg->dmarcv.addrhigh);
  923. /* Initialize descriptors */
  924. for (i = 0; i < PDC_RING_ENTRIES; i++) {
  925. /* Every tx descriptor can be used for start of frame. */
  926. if (i != pdcs->ntxpost) {
  927. iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOF,
  928. (void *)&pdcs->txd_64[i].ctrl1);
  929. } else {
  930. /* Last descriptor in ringset. Set End of Table. */
  931. iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOF |
  932. D64_CTRL1_EOT,
  933. (void *)&pdcs->txd_64[i].ctrl1);
  934. }
  935. /* Every rx descriptor can be used for start of frame */
  936. if (i != pdcs->nrxpost) {
  937. iowrite32(D64_CTRL1_SOF,
  938. (void *)&pdcs->rxd_64[i].ctrl1);
  939. } else {
  940. /* Last descriptor in ringset. Set End of Table. */
  941. iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOT,
  942. (void *)&pdcs->rxd_64[i].ctrl1);
  943. }
  944. }
  945. spin_unlock(&pdcs->pdc_lock);
  946. return PDC_SUCCESS;
  947. fail_dealloc:
  948. dma_pool_free(pdcs->ring_pool, tx.vbase, tx.dmabase);
  949. done:
  950. return err;
  951. }
  952. static void pdc_ring_free(struct pdc_state *pdcs)
  953. {
  954. if (pdcs->tx_ring_alloc.vbase) {
  955. dma_pool_free(pdcs->ring_pool, pdcs->tx_ring_alloc.vbase,
  956. pdcs->tx_ring_alloc.dmabase);
  957. pdcs->tx_ring_alloc.vbase = NULL;
  958. }
  959. if (pdcs->rx_ring_alloc.vbase) {
  960. dma_pool_free(pdcs->ring_pool, pdcs->rx_ring_alloc.vbase,
  961. pdcs->rx_ring_alloc.dmabase);
  962. pdcs->rx_ring_alloc.vbase = NULL;
  963. }
  964. }
  965. /**
  966. * pdc_send_data() - mailbox send_data function
  967. * @chan: The mailbox channel on which the data is sent. The channel
  968. * corresponds to a DMA ringset.
  969. * @data: The mailbox message to be sent. The message must be a
  970. * brcm_message structure.
  971. *
  972. * This function is registered as the send_data function for the mailbox
  973. * controller. From the destination scatterlist in the mailbox message, it
  974. * creates a sequence of receive descriptors in the rx ring. From the source
  975. * scatterlist, it creates a sequence of transmit descriptors in the tx ring.
  976. * After creating the descriptors, it writes the rx ptr and tx ptr registers to
  977. * initiate the DMA transfer.
  978. *
  979. * This function does the DMA map and unmap of the src and dst scatterlists in
  980. * the mailbox message.
  981. *
  982. * Return: 0 if successful
  983. * -ENOTSUPP if the mailbox message is a type this driver does not
  984. * support
  985. * < 0 if an error
  986. */
  987. static int pdc_send_data(struct mbox_chan *chan, void *data)
  988. {
  989. struct pdc_state *pdcs = chan->con_priv;
  990. struct device *dev = &pdcs->pdev->dev;
  991. struct brcm_message *mssg = data;
  992. int err = PDC_SUCCESS;
  993. int src_nent;
  994. int dst_nent;
  995. int nent;
  996. if (mssg->type != BRCM_MESSAGE_SPU)
  997. return -ENOTSUPP;
  998. src_nent = sg_nents(mssg->spu.src);
  999. if (src_nent) {
  1000. nent = dma_map_sg(dev, mssg->spu.src, src_nent, DMA_TO_DEVICE);
  1001. if (nent == 0)
  1002. return -EIO;
  1003. }
  1004. dst_nent = sg_nents(mssg->spu.dst);
  1005. if (dst_nent) {
  1006. nent = dma_map_sg(dev, mssg->spu.dst, dst_nent,
  1007. DMA_FROM_DEVICE);
  1008. if (nent == 0) {
  1009. dma_unmap_sg(dev, mssg->spu.src, src_nent,
  1010. DMA_TO_DEVICE);
  1011. return -EIO;
  1012. }
  1013. }
  1014. spin_lock(&pdcs->pdc_lock);
  1015. /* Create rx descriptors to SPU catch response */
  1016. err = pdc_rx_list_init(pdcs, mssg->spu.dst, mssg->ctx);
  1017. err |= pdc_rx_list_sg_add(pdcs, mssg->spu.dst);
  1018. /* Create tx descriptors to submit SPU request */
  1019. err |= pdc_tx_list_sg_add(pdcs, mssg->spu.src);
  1020. err |= pdc_tx_list_final(pdcs); /* initiate transfer */
  1021. spin_unlock(&pdcs->pdc_lock);
  1022. if (err)
  1023. dev_err(&pdcs->pdev->dev,
  1024. "%s failed with error %d", __func__, err);
  1025. return err;
  1026. }
  1027. static int pdc_startup(struct mbox_chan *chan)
  1028. {
  1029. return pdc_ring_init(chan->con_priv, PDC_RINGSET);
  1030. }
  1031. static void pdc_shutdown(struct mbox_chan *chan)
  1032. {
  1033. struct pdc_state *pdcs = chan->con_priv;
  1034. if (!pdcs)
  1035. return;
  1036. dev_dbg(&pdcs->pdev->dev,
  1037. "Shutdown mailbox channel for PDC %u", pdcs->pdc_idx);
  1038. pdc_ring_free(pdcs);
  1039. }
  1040. /**
  1041. * pdc_hw_init() - Use the given initialization parameters to initialize the
  1042. * state for one of the PDCs.
  1043. * @pdcs: state of the PDC
  1044. */
  1045. static
  1046. void pdc_hw_init(struct pdc_state *pdcs)
  1047. {
  1048. struct platform_device *pdev;
  1049. struct device *dev;
  1050. struct dma64 *dma_reg;
  1051. int ringset = PDC_RINGSET;
  1052. pdev = pdcs->pdev;
  1053. dev = &pdev->dev;
  1054. dev_dbg(dev, "PDC %u initial values:", pdcs->pdc_idx);
  1055. dev_dbg(dev, "state structure: %p",
  1056. pdcs);
  1057. dev_dbg(dev, " - base virtual addr of hw regs %p",
  1058. pdcs->pdc_reg_vbase);
  1059. /* initialize data structures */
  1060. pdcs->regs = (struct pdc_regs *)pdcs->pdc_reg_vbase;
  1061. pdcs->txregs_64 = (struct dma64_regs *)
  1062. (void *)(((u8 *)pdcs->pdc_reg_vbase) +
  1063. PDC_TXREGS_OFFSET + (sizeof(struct dma64) * ringset));
  1064. pdcs->rxregs_64 = (struct dma64_regs *)
  1065. (void *)(((u8 *)pdcs->pdc_reg_vbase) +
  1066. PDC_RXREGS_OFFSET + (sizeof(struct dma64) * ringset));
  1067. pdcs->ntxd = PDC_RING_ENTRIES;
  1068. pdcs->nrxd = PDC_RING_ENTRIES;
  1069. pdcs->ntxpost = PDC_RING_ENTRIES - 1;
  1070. pdcs->nrxpost = PDC_RING_ENTRIES - 1;
  1071. pdcs->regs->intmask = 0;
  1072. dma_reg = &pdcs->regs->dmaregs[ringset];
  1073. iowrite32(0, (void *)&dma_reg->dmaxmt.ptr);
  1074. iowrite32(0, (void *)&dma_reg->dmarcv.ptr);
  1075. iowrite32(PDC_TX_CTL, (void *)&dma_reg->dmaxmt.control);
  1076. iowrite32(PDC_RX_CTL + (pdcs->rx_status_len << 1),
  1077. (void *)&dma_reg->dmarcv.control);
  1078. if (pdcs->pdc_resp_hdr_len == PDC_SPU2_RESP_HDR_LEN)
  1079. iowrite32(PDC_CKSUM_CTRL,
  1080. pdcs->pdc_reg_vbase + PDC_CKSUM_CTRL_OFFSET);
  1081. }
  1082. /**
  1083. * pdc_rx_buf_pool_create() - Pool of receive buffers used to catch the metadata
  1084. * header returned with each response message.
  1085. * @pdcs: PDC state structure
  1086. *
  1087. * The metadata is not returned to the mailbox client. So the PDC driver
  1088. * manages these buffers.
  1089. *
  1090. * Return: PDC_SUCCESS
  1091. * -ENOMEM if pool creation fails
  1092. */
  1093. static int pdc_rx_buf_pool_create(struct pdc_state *pdcs)
  1094. {
  1095. struct platform_device *pdev;
  1096. struct device *dev;
  1097. pdev = pdcs->pdev;
  1098. dev = &pdev->dev;
  1099. pdcs->pdc_resp_hdr_len = pdcs->rx_status_len;
  1100. if (pdcs->use_bcm_hdr)
  1101. pdcs->pdc_resp_hdr_len += BCM_HDR_LEN;
  1102. pdcs->rx_buf_pool = dma_pool_create("pdc rx bufs", dev,
  1103. pdcs->pdc_resp_hdr_len,
  1104. RX_BUF_ALIGN, 0);
  1105. if (!pdcs->rx_buf_pool)
  1106. return -ENOMEM;
  1107. return PDC_SUCCESS;
  1108. }
  1109. /**
  1110. * pdc_interrupts_init() - Initialize the interrupt configuration for a PDC and
  1111. * specify a threaded IRQ handler for deferred handling of interrupts outside of
  1112. * interrupt context.
  1113. * @pdcs: PDC state
  1114. *
  1115. * Set the interrupt mask for transmit and receive done.
  1116. * Set the lazy interrupt frame count to generate an interrupt for just one pkt.
  1117. *
  1118. * Return: PDC_SUCCESS
  1119. * <0 if threaded irq request fails
  1120. */
  1121. static int pdc_interrupts_init(struct pdc_state *pdcs)
  1122. {
  1123. struct platform_device *pdev = pdcs->pdev;
  1124. struct device *dev = &pdev->dev;
  1125. struct device_node *dn = pdev->dev.of_node;
  1126. int err;
  1127. pdcs->intstatus = 0;
  1128. /* interrupt configuration */
  1129. iowrite32(PDC_INTMASK, pdcs->pdc_reg_vbase + PDC_INTMASK_OFFSET);
  1130. iowrite32(PDC_LAZY_INT, pdcs->pdc_reg_vbase + PDC_RCVLAZY0_OFFSET);
  1131. /* read irq from device tree */
  1132. pdcs->pdc_irq = irq_of_parse_and_map(dn, 0);
  1133. dev_dbg(dev, "pdc device %s irq %u for pdcs %p",
  1134. dev_name(dev), pdcs->pdc_irq, pdcs);
  1135. err = devm_request_threaded_irq(dev, pdcs->pdc_irq,
  1136. pdc_irq_handler,
  1137. pdc_irq_thread, 0, dev_name(dev), pdcs);
  1138. if (err) {
  1139. dev_err(dev, "threaded tx IRQ %u request failed with err %d\n",
  1140. pdcs->pdc_irq, err);
  1141. return err;
  1142. }
  1143. return PDC_SUCCESS;
  1144. }
  1145. static const struct mbox_chan_ops pdc_mbox_chan_ops = {
  1146. .send_data = pdc_send_data,
  1147. .startup = pdc_startup,
  1148. .shutdown = pdc_shutdown
  1149. };
  1150. /**
  1151. * pdc_mb_init() - Initialize the mailbox controller.
  1152. * @pdcs: PDC state
  1153. *
  1154. * Each PDC is a mailbox controller. Each ringset is a mailbox channel. Kernel
  1155. * driver only uses one ringset and thus one mb channel. PDC uses the transmit
  1156. * complete interrupt to determine when a mailbox message has successfully been
  1157. * transmitted.
  1158. *
  1159. * Return: 0 on success
  1160. * < 0 if there is an allocation or registration failure
  1161. */
  1162. static int pdc_mb_init(struct pdc_state *pdcs)
  1163. {
  1164. struct device *dev = &pdcs->pdev->dev;
  1165. struct mbox_controller *mbc;
  1166. int chan_index;
  1167. int err;
  1168. mbc = &pdcs->mbc;
  1169. mbc->dev = dev;
  1170. mbc->ops = &pdc_mbox_chan_ops;
  1171. mbc->num_chans = 1;
  1172. mbc->chans = devm_kcalloc(dev, mbc->num_chans, sizeof(*mbc->chans),
  1173. GFP_KERNEL);
  1174. if (!mbc->chans)
  1175. return -ENOMEM;
  1176. mbc->txdone_irq = true;
  1177. mbc->txdone_poll = false;
  1178. for (chan_index = 0; chan_index < mbc->num_chans; chan_index++)
  1179. mbc->chans[chan_index].con_priv = pdcs;
  1180. /* Register mailbox controller */
  1181. err = mbox_controller_register(mbc);
  1182. if (err) {
  1183. dev_crit(dev,
  1184. "Failed to register PDC mailbox controller. Error %d.",
  1185. err);
  1186. return err;
  1187. }
  1188. return 0;
  1189. }
  1190. /**
  1191. * pdc_dt_read() - Read application-specific data from device tree.
  1192. * @pdev: Platform device
  1193. * @pdcs: PDC state
  1194. *
  1195. * Reads the number of bytes of receive status that precede each received frame.
  1196. * Reads whether transmit and received frames should be preceded by an 8-byte
  1197. * BCM header.
  1198. *
  1199. * Return: 0 if successful
  1200. * -ENODEV if device not available
  1201. */
  1202. static int pdc_dt_read(struct platform_device *pdev, struct pdc_state *pdcs)
  1203. {
  1204. struct device *dev = &pdev->dev;
  1205. struct device_node *dn = pdev->dev.of_node;
  1206. int err;
  1207. err = of_property_read_u32(dn, "brcm,rx-status-len",
  1208. &pdcs->rx_status_len);
  1209. if (err < 0)
  1210. dev_err(dev,
  1211. "%s failed to get DMA receive status length from device tree",
  1212. __func__);
  1213. pdcs->use_bcm_hdr = of_property_read_bool(dn, "brcm,use-bcm-hdr");
  1214. return 0;
  1215. }
  1216. /**
  1217. * pdc_probe() - Probe function for PDC driver.
  1218. * @pdev: PDC platform device
  1219. *
  1220. * Reserve and map register regions defined in device tree.
  1221. * Allocate and initialize tx and rx DMA rings.
  1222. * Initialize a mailbox controller for each PDC.
  1223. *
  1224. * Return: 0 if successful
  1225. * < 0 if an error
  1226. */
  1227. static int pdc_probe(struct platform_device *pdev)
  1228. {
  1229. int err = 0;
  1230. struct device *dev = &pdev->dev;
  1231. struct resource *pdc_regs;
  1232. struct pdc_state *pdcs;
  1233. /* PDC state for one SPU */
  1234. pdcs = devm_kzalloc(dev, sizeof(*pdcs), GFP_KERNEL);
  1235. if (!pdcs) {
  1236. err = -ENOMEM;
  1237. goto cleanup;
  1238. }
  1239. spin_lock_init(&pdcs->pdc_lock);
  1240. pdcs->pdev = pdev;
  1241. platform_set_drvdata(pdev, pdcs);
  1242. pdcs->pdc_idx = pdcg.num_spu;
  1243. pdcg.num_spu++;
  1244. err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  1245. if (err) {
  1246. dev_warn(dev, "PDC device cannot perform DMA. Error %d.", err);
  1247. goto cleanup;
  1248. }
  1249. /* Create DMA pool for tx ring */
  1250. pdcs->ring_pool = dma_pool_create("pdc rings", dev, PDC_RING_SIZE,
  1251. RING_ALIGN, 0);
  1252. if (!pdcs->ring_pool) {
  1253. err = -ENOMEM;
  1254. goto cleanup;
  1255. }
  1256. err = pdc_dt_read(pdev, pdcs);
  1257. if (err)
  1258. goto cleanup_ring_pool;
  1259. pdc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1260. if (!pdc_regs) {
  1261. err = -ENODEV;
  1262. goto cleanup_ring_pool;
  1263. }
  1264. dev_dbg(dev, "PDC register region res.start = %pa, res.end = %pa",
  1265. &pdc_regs->start, &pdc_regs->end);
  1266. pdcs->pdc_reg_vbase = devm_ioremap_resource(&pdev->dev, pdc_regs);
  1267. if (IS_ERR(pdcs->pdc_reg_vbase)) {
  1268. err = PTR_ERR(pdcs->pdc_reg_vbase);
  1269. dev_err(&pdev->dev, "Failed to map registers: %d\n", err);
  1270. goto cleanup_ring_pool;
  1271. }
  1272. /* create rx buffer pool after dt read to know how big buffers are */
  1273. err = pdc_rx_buf_pool_create(pdcs);
  1274. if (err)
  1275. goto cleanup_ring_pool;
  1276. pdc_hw_init(pdcs);
  1277. err = pdc_interrupts_init(pdcs);
  1278. if (err)
  1279. goto cleanup_buf_pool;
  1280. /* Initialize mailbox controller */
  1281. err = pdc_mb_init(pdcs);
  1282. if (err)
  1283. goto cleanup_buf_pool;
  1284. pdcs->debugfs_stats = NULL;
  1285. pdc_setup_debugfs(pdcs);
  1286. dev_dbg(dev, "pdc_probe() successful");
  1287. return PDC_SUCCESS;
  1288. cleanup_buf_pool:
  1289. dma_pool_destroy(pdcs->rx_buf_pool);
  1290. cleanup_ring_pool:
  1291. dma_pool_destroy(pdcs->ring_pool);
  1292. cleanup:
  1293. return err;
  1294. }
  1295. static int pdc_remove(struct platform_device *pdev)
  1296. {
  1297. struct pdc_state *pdcs = platform_get_drvdata(pdev);
  1298. pdc_free_debugfs();
  1299. mbox_controller_unregister(&pdcs->mbc);
  1300. dma_pool_destroy(pdcs->rx_buf_pool);
  1301. dma_pool_destroy(pdcs->ring_pool);
  1302. return 0;
  1303. }
  1304. static const struct of_device_id pdc_mbox_of_match[] = {
  1305. {.compatible = "brcm,iproc-pdc-mbox"},
  1306. { /* sentinel */ }
  1307. };
  1308. MODULE_DEVICE_TABLE(of, pdc_mbox_of_match);
  1309. static struct platform_driver pdc_mbox_driver = {
  1310. .probe = pdc_probe,
  1311. .remove = pdc_remove,
  1312. .driver = {
  1313. .name = "brcm-iproc-pdc-mbox",
  1314. .of_match_table = of_match_ptr(pdc_mbox_of_match),
  1315. },
  1316. };
  1317. module_platform_driver(pdc_mbox_driver);
  1318. MODULE_AUTHOR("Rob Rice <rob.rice@broadcom.com>");
  1319. MODULE_DESCRIPTION("Broadcom PDC mailbox driver");
  1320. MODULE_LICENSE("GPL v2");