bcm-pdc-mailbox.c 47 KB

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