bcm-pdc-mailbox.c 48 KB

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