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