Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1 | /* Broadcom FlexRM Mailbox Driver |
| 2 | * |
| 3 | * Copyright (C) 2017 Broadcom |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Each Broadcom FlexSparx4 offload engine is implemented as an |
| 10 | * extension to Broadcom FlexRM ring manager. The FlexRM ring |
| 11 | * manager provides a set of rings which can be used to submit |
| 12 | * work to a FlexSparx4 offload engine. |
| 13 | * |
| 14 | * This driver creates a mailbox controller using a set of FlexRM |
| 15 | * rings where each mailbox channel represents a separate FlexRM ring. |
| 16 | */ |
| 17 | |
| 18 | #include <asm/barrier.h> |
| 19 | #include <asm/byteorder.h> |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 20 | #include <linux/atomic.h> |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 21 | #include <linux/bitmap.h> |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 22 | #include <linux/debugfs.h> |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/device.h> |
| 25 | #include <linux/dma-mapping.h> |
| 26 | #include <linux/dmapool.h> |
| 27 | #include <linux/err.h> |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/mailbox_controller.h> |
| 31 | #include <linux/mailbox_client.h> |
| 32 | #include <linux/mailbox/brcm-message.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/msi.h> |
| 35 | #include <linux/of_address.h> |
| 36 | #include <linux/of_irq.h> |
| 37 | #include <linux/platform_device.h> |
| 38 | #include <linux/spinlock.h> |
| 39 | |
| 40 | /* ====== FlexRM register defines ===== */ |
| 41 | |
| 42 | /* FlexRM configuration */ |
| 43 | #define RING_REGS_SIZE 0x10000 |
| 44 | #define RING_DESC_SIZE 8 |
| 45 | #define RING_DESC_INDEX(offset) \ |
| 46 | ((offset) / RING_DESC_SIZE) |
| 47 | #define RING_DESC_OFFSET(index) \ |
| 48 | ((index) * RING_DESC_SIZE) |
| 49 | #define RING_MAX_REQ_COUNT 1024 |
| 50 | #define RING_BD_ALIGN_ORDER 12 |
| 51 | #define RING_BD_ALIGN_CHECK(addr) \ |
| 52 | (!((addr) & ((0x1 << RING_BD_ALIGN_ORDER) - 1))) |
| 53 | #define RING_BD_TOGGLE_INVALID(offset) \ |
| 54 | (((offset) >> RING_BD_ALIGN_ORDER) & 0x1) |
| 55 | #define RING_BD_TOGGLE_VALID(offset) \ |
| 56 | (!RING_BD_TOGGLE_INVALID(offset)) |
| 57 | #define RING_BD_DESC_PER_REQ 32 |
| 58 | #define RING_BD_DESC_COUNT \ |
| 59 | (RING_MAX_REQ_COUNT * RING_BD_DESC_PER_REQ) |
| 60 | #define RING_BD_SIZE \ |
| 61 | (RING_BD_DESC_COUNT * RING_DESC_SIZE) |
| 62 | #define RING_CMPL_ALIGN_ORDER 13 |
| 63 | #define RING_CMPL_DESC_COUNT RING_MAX_REQ_COUNT |
| 64 | #define RING_CMPL_SIZE \ |
| 65 | (RING_CMPL_DESC_COUNT * RING_DESC_SIZE) |
| 66 | #define RING_VER_MAGIC 0x76303031 |
| 67 | |
| 68 | /* Per-Ring register offsets */ |
| 69 | #define RING_VER 0x000 |
| 70 | #define RING_BD_START_ADDR 0x004 |
| 71 | #define RING_BD_READ_PTR 0x008 |
| 72 | #define RING_BD_WRITE_PTR 0x00c |
| 73 | #define RING_BD_READ_PTR_DDR_LS 0x010 |
| 74 | #define RING_BD_READ_PTR_DDR_MS 0x014 |
| 75 | #define RING_CMPL_START_ADDR 0x018 |
| 76 | #define RING_CMPL_WRITE_PTR 0x01c |
| 77 | #define RING_NUM_REQ_RECV_LS 0x020 |
| 78 | #define RING_NUM_REQ_RECV_MS 0x024 |
| 79 | #define RING_NUM_REQ_TRANS_LS 0x028 |
| 80 | #define RING_NUM_REQ_TRANS_MS 0x02c |
| 81 | #define RING_NUM_REQ_OUTSTAND 0x030 |
| 82 | #define RING_CONTROL 0x034 |
| 83 | #define RING_FLUSH_DONE 0x038 |
| 84 | #define RING_MSI_ADDR_LS 0x03c |
| 85 | #define RING_MSI_ADDR_MS 0x040 |
| 86 | #define RING_MSI_CONTROL 0x048 |
| 87 | #define RING_BD_READ_PTR_DDR_CONTROL 0x04c |
| 88 | #define RING_MSI_DATA_VALUE 0x064 |
| 89 | |
| 90 | /* Register RING_BD_START_ADDR fields */ |
| 91 | #define BD_LAST_UPDATE_HW_SHIFT 28 |
| 92 | #define BD_LAST_UPDATE_HW_MASK 0x1 |
| 93 | #define BD_START_ADDR_VALUE(pa) \ |
| 94 | ((u32)((((dma_addr_t)(pa)) >> RING_BD_ALIGN_ORDER) & 0x0fffffff)) |
| 95 | #define BD_START_ADDR_DECODE(val) \ |
| 96 | ((dma_addr_t)((val) & 0x0fffffff) << RING_BD_ALIGN_ORDER) |
| 97 | |
| 98 | /* Register RING_CMPL_START_ADDR fields */ |
| 99 | #define CMPL_START_ADDR_VALUE(pa) \ |
Anup Patel | 6d2061b | 2017-08-01 16:05:52 +0530 | [diff] [blame] | 100 | ((u32)((((u64)(pa)) >> RING_CMPL_ALIGN_ORDER) & 0x07ffffff)) |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 101 | |
| 102 | /* Register RING_CONTROL fields */ |
| 103 | #define CONTROL_MASK_DISABLE_CONTROL 12 |
| 104 | #define CONTROL_FLUSH_SHIFT 5 |
| 105 | #define CONTROL_ACTIVE_SHIFT 4 |
| 106 | #define CONTROL_RATE_ADAPT_MASK 0xf |
| 107 | #define CONTROL_RATE_DYNAMIC 0x0 |
| 108 | #define CONTROL_RATE_FAST 0x8 |
| 109 | #define CONTROL_RATE_MEDIUM 0x9 |
| 110 | #define CONTROL_RATE_SLOW 0xa |
| 111 | #define CONTROL_RATE_IDLE 0xb |
| 112 | |
| 113 | /* Register RING_FLUSH_DONE fields */ |
| 114 | #define FLUSH_DONE_MASK 0x1 |
| 115 | |
| 116 | /* Register RING_MSI_CONTROL fields */ |
| 117 | #define MSI_TIMER_VAL_SHIFT 16 |
| 118 | #define MSI_TIMER_VAL_MASK 0xffff |
| 119 | #define MSI_ENABLE_SHIFT 15 |
| 120 | #define MSI_ENABLE_MASK 0x1 |
| 121 | #define MSI_COUNT_SHIFT 0 |
| 122 | #define MSI_COUNT_MASK 0x3ff |
| 123 | |
| 124 | /* Register RING_BD_READ_PTR_DDR_CONTROL fields */ |
| 125 | #define BD_READ_PTR_DDR_TIMER_VAL_SHIFT 16 |
| 126 | #define BD_READ_PTR_DDR_TIMER_VAL_MASK 0xffff |
| 127 | #define BD_READ_PTR_DDR_ENABLE_SHIFT 15 |
| 128 | #define BD_READ_PTR_DDR_ENABLE_MASK 0x1 |
| 129 | |
| 130 | /* ====== FlexRM ring descriptor defines ===== */ |
| 131 | |
| 132 | /* Completion descriptor format */ |
| 133 | #define CMPL_OPAQUE_SHIFT 0 |
| 134 | #define CMPL_OPAQUE_MASK 0xffff |
| 135 | #define CMPL_ENGINE_STATUS_SHIFT 16 |
| 136 | #define CMPL_ENGINE_STATUS_MASK 0xffff |
| 137 | #define CMPL_DME_STATUS_SHIFT 32 |
| 138 | #define CMPL_DME_STATUS_MASK 0xffff |
| 139 | #define CMPL_RM_STATUS_SHIFT 48 |
| 140 | #define CMPL_RM_STATUS_MASK 0xffff |
| 141 | |
| 142 | /* Completion DME status code */ |
| 143 | #define DME_STATUS_MEM_COR_ERR BIT(0) |
| 144 | #define DME_STATUS_MEM_UCOR_ERR BIT(1) |
| 145 | #define DME_STATUS_FIFO_UNDERFLOW BIT(2) |
| 146 | #define DME_STATUS_FIFO_OVERFLOW BIT(3) |
| 147 | #define DME_STATUS_RRESP_ERR BIT(4) |
| 148 | #define DME_STATUS_BRESP_ERR BIT(5) |
| 149 | #define DME_STATUS_ERROR_MASK (DME_STATUS_MEM_COR_ERR | \ |
| 150 | DME_STATUS_MEM_UCOR_ERR | \ |
| 151 | DME_STATUS_FIFO_UNDERFLOW | \ |
| 152 | DME_STATUS_FIFO_OVERFLOW | \ |
| 153 | DME_STATUS_RRESP_ERR | \ |
| 154 | DME_STATUS_BRESP_ERR) |
| 155 | |
| 156 | /* Completion RM status code */ |
| 157 | #define RM_STATUS_CODE_SHIFT 0 |
| 158 | #define RM_STATUS_CODE_MASK 0x3ff |
| 159 | #define RM_STATUS_CODE_GOOD 0x0 |
| 160 | #define RM_STATUS_CODE_AE_TIMEOUT 0x3ff |
| 161 | |
| 162 | /* General descriptor format */ |
| 163 | #define DESC_TYPE_SHIFT 60 |
| 164 | #define DESC_TYPE_MASK 0xf |
| 165 | #define DESC_PAYLOAD_SHIFT 0 |
| 166 | #define DESC_PAYLOAD_MASK 0x0fffffffffffffff |
| 167 | |
| 168 | /* Null descriptor format */ |
| 169 | #define NULL_TYPE 0 |
| 170 | #define NULL_TOGGLE_SHIFT 58 |
| 171 | #define NULL_TOGGLE_MASK 0x1 |
| 172 | |
| 173 | /* Header descriptor format */ |
| 174 | #define HEADER_TYPE 1 |
| 175 | #define HEADER_TOGGLE_SHIFT 58 |
| 176 | #define HEADER_TOGGLE_MASK 0x1 |
| 177 | #define HEADER_ENDPKT_SHIFT 57 |
| 178 | #define HEADER_ENDPKT_MASK 0x1 |
| 179 | #define HEADER_STARTPKT_SHIFT 56 |
| 180 | #define HEADER_STARTPKT_MASK 0x1 |
| 181 | #define HEADER_BDCOUNT_SHIFT 36 |
| 182 | #define HEADER_BDCOUNT_MASK 0x1f |
| 183 | #define HEADER_BDCOUNT_MAX HEADER_BDCOUNT_MASK |
| 184 | #define HEADER_FLAGS_SHIFT 16 |
| 185 | #define HEADER_FLAGS_MASK 0xffff |
| 186 | #define HEADER_OPAQUE_SHIFT 0 |
| 187 | #define HEADER_OPAQUE_MASK 0xffff |
| 188 | |
| 189 | /* Source (SRC) descriptor format */ |
| 190 | #define SRC_TYPE 2 |
| 191 | #define SRC_LENGTH_SHIFT 44 |
| 192 | #define SRC_LENGTH_MASK 0xffff |
| 193 | #define SRC_ADDR_SHIFT 0 |
| 194 | #define SRC_ADDR_MASK 0x00000fffffffffff |
| 195 | |
| 196 | /* Destination (DST) descriptor format */ |
| 197 | #define DST_TYPE 3 |
| 198 | #define DST_LENGTH_SHIFT 44 |
| 199 | #define DST_LENGTH_MASK 0xffff |
| 200 | #define DST_ADDR_SHIFT 0 |
| 201 | #define DST_ADDR_MASK 0x00000fffffffffff |
| 202 | |
| 203 | /* Immediate (IMM) descriptor format */ |
| 204 | #define IMM_TYPE 4 |
| 205 | #define IMM_DATA_SHIFT 0 |
| 206 | #define IMM_DATA_MASK 0x0fffffffffffffff |
| 207 | |
| 208 | /* Next pointer (NPTR) descriptor format */ |
| 209 | #define NPTR_TYPE 5 |
| 210 | #define NPTR_TOGGLE_SHIFT 58 |
| 211 | #define NPTR_TOGGLE_MASK 0x1 |
| 212 | #define NPTR_ADDR_SHIFT 0 |
| 213 | #define NPTR_ADDR_MASK 0x00000fffffffffff |
| 214 | |
| 215 | /* Mega source (MSRC) descriptor format */ |
| 216 | #define MSRC_TYPE 6 |
| 217 | #define MSRC_LENGTH_SHIFT 44 |
| 218 | #define MSRC_LENGTH_MASK 0xffff |
| 219 | #define MSRC_ADDR_SHIFT 0 |
| 220 | #define MSRC_ADDR_MASK 0x00000fffffffffff |
| 221 | |
| 222 | /* Mega destination (MDST) descriptor format */ |
| 223 | #define MDST_TYPE 7 |
| 224 | #define MDST_LENGTH_SHIFT 44 |
| 225 | #define MDST_LENGTH_MASK 0xffff |
| 226 | #define MDST_ADDR_SHIFT 0 |
| 227 | #define MDST_ADDR_MASK 0x00000fffffffffff |
| 228 | |
| 229 | /* Source with tlast (SRCT) descriptor format */ |
| 230 | #define SRCT_TYPE 8 |
| 231 | #define SRCT_LENGTH_SHIFT 44 |
| 232 | #define SRCT_LENGTH_MASK 0xffff |
| 233 | #define SRCT_ADDR_SHIFT 0 |
| 234 | #define SRCT_ADDR_MASK 0x00000fffffffffff |
| 235 | |
| 236 | /* Destination with tlast (DSTT) descriptor format */ |
| 237 | #define DSTT_TYPE 9 |
| 238 | #define DSTT_LENGTH_SHIFT 44 |
| 239 | #define DSTT_LENGTH_MASK 0xffff |
| 240 | #define DSTT_ADDR_SHIFT 0 |
| 241 | #define DSTT_ADDR_MASK 0x00000fffffffffff |
| 242 | |
| 243 | /* Immediate with tlast (IMMT) descriptor format */ |
| 244 | #define IMMT_TYPE 10 |
| 245 | #define IMMT_DATA_SHIFT 0 |
| 246 | #define IMMT_DATA_MASK 0x0fffffffffffffff |
| 247 | |
| 248 | /* Descriptor helper macros */ |
| 249 | #define DESC_DEC(_d, _s, _m) (((_d) >> (_s)) & (_m)) |
| 250 | #define DESC_ENC(_d, _v, _s, _m) \ |
| 251 | do { \ |
| 252 | (_d) &= ~((u64)(_m) << (_s)); \ |
| 253 | (_d) |= (((u64)(_v) & (_m)) << (_s)); \ |
| 254 | } while (0) |
| 255 | |
| 256 | /* ====== FlexRM data structures ===== */ |
| 257 | |
| 258 | struct flexrm_ring { |
| 259 | /* Unprotected members */ |
| 260 | int num; |
| 261 | struct flexrm_mbox *mbox; |
| 262 | void __iomem *regs; |
| 263 | bool irq_requested; |
| 264 | unsigned int irq; |
Anup Patel | 6ac17fe | 2017-08-01 16:05:50 +0530 | [diff] [blame] | 265 | cpumask_t irq_aff_hint; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 266 | unsigned int msi_timer_val; |
| 267 | unsigned int msi_count_threshold; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 268 | struct brcm_message *requests[RING_MAX_REQ_COUNT]; |
| 269 | void *bd_base; |
| 270 | dma_addr_t bd_dma_base; |
| 271 | u32 bd_write_offset; |
| 272 | void *cmpl_base; |
| 273 | dma_addr_t cmpl_dma_base; |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 274 | /* Atomic stats */ |
| 275 | atomic_t msg_send_count; |
| 276 | atomic_t msg_cmpl_count; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 277 | /* Protected members */ |
| 278 | spinlock_t lock; |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 279 | DECLARE_BITMAP(requests_bmap, RING_MAX_REQ_COUNT); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 280 | struct brcm_message *last_pending_msg; |
| 281 | u32 cmpl_read_offset; |
| 282 | }; |
| 283 | |
| 284 | struct flexrm_mbox { |
| 285 | struct device *dev; |
| 286 | void __iomem *regs; |
| 287 | u32 num_rings; |
| 288 | struct flexrm_ring *rings; |
| 289 | struct dma_pool *bd_pool; |
| 290 | struct dma_pool *cmpl_pool; |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 291 | struct dentry *root; |
| 292 | struct dentry *config; |
| 293 | struct dentry *stats; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 294 | struct mbox_controller controller; |
| 295 | }; |
| 296 | |
| 297 | /* ====== FlexRM ring descriptor helper routines ===== */ |
| 298 | |
| 299 | static u64 flexrm_read_desc(void *desc_ptr) |
| 300 | { |
| 301 | return le64_to_cpu(*((u64 *)desc_ptr)); |
| 302 | } |
| 303 | |
| 304 | static void flexrm_write_desc(void *desc_ptr, u64 desc) |
| 305 | { |
| 306 | *((u64 *)desc_ptr) = cpu_to_le64(desc); |
| 307 | } |
| 308 | |
| 309 | static u32 flexrm_cmpl_desc_to_reqid(u64 cmpl_desc) |
| 310 | { |
| 311 | return (u32)(cmpl_desc & CMPL_OPAQUE_MASK); |
| 312 | } |
| 313 | |
| 314 | static int flexrm_cmpl_desc_to_error(u64 cmpl_desc) |
| 315 | { |
| 316 | u32 status; |
| 317 | |
| 318 | status = DESC_DEC(cmpl_desc, CMPL_DME_STATUS_SHIFT, |
| 319 | CMPL_DME_STATUS_MASK); |
| 320 | if (status & DME_STATUS_ERROR_MASK) |
| 321 | return -EIO; |
| 322 | |
| 323 | status = DESC_DEC(cmpl_desc, CMPL_RM_STATUS_SHIFT, |
| 324 | CMPL_RM_STATUS_MASK); |
| 325 | status &= RM_STATUS_CODE_MASK; |
| 326 | if (status == RM_STATUS_CODE_AE_TIMEOUT) |
| 327 | return -ETIMEDOUT; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static bool flexrm_is_next_table_desc(void *desc_ptr) |
| 333 | { |
| 334 | u64 desc = flexrm_read_desc(desc_ptr); |
| 335 | u32 type = DESC_DEC(desc, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 336 | |
| 337 | return (type == NPTR_TYPE) ? true : false; |
| 338 | } |
| 339 | |
| 340 | static u64 flexrm_next_table_desc(u32 toggle, dma_addr_t next_addr) |
| 341 | { |
| 342 | u64 desc = 0; |
| 343 | |
| 344 | DESC_ENC(desc, NPTR_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 345 | DESC_ENC(desc, toggle, NPTR_TOGGLE_SHIFT, NPTR_TOGGLE_MASK); |
| 346 | DESC_ENC(desc, next_addr, NPTR_ADDR_SHIFT, NPTR_ADDR_MASK); |
| 347 | |
| 348 | return desc; |
| 349 | } |
| 350 | |
| 351 | static u64 flexrm_null_desc(u32 toggle) |
| 352 | { |
| 353 | u64 desc = 0; |
| 354 | |
| 355 | DESC_ENC(desc, NULL_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 356 | DESC_ENC(desc, toggle, NULL_TOGGLE_SHIFT, NULL_TOGGLE_MASK); |
| 357 | |
| 358 | return desc; |
| 359 | } |
| 360 | |
| 361 | static u32 flexrm_estimate_header_desc_count(u32 nhcnt) |
| 362 | { |
| 363 | u32 hcnt = nhcnt / HEADER_BDCOUNT_MAX; |
| 364 | |
| 365 | if (!(nhcnt % HEADER_BDCOUNT_MAX)) |
| 366 | hcnt += 1; |
| 367 | |
| 368 | return hcnt; |
| 369 | } |
| 370 | |
| 371 | static void flexrm_flip_header_toogle(void *desc_ptr) |
| 372 | { |
| 373 | u64 desc = flexrm_read_desc(desc_ptr); |
| 374 | |
| 375 | if (desc & ((u64)0x1 << HEADER_TOGGLE_SHIFT)) |
| 376 | desc &= ~((u64)0x1 << HEADER_TOGGLE_SHIFT); |
| 377 | else |
| 378 | desc |= ((u64)0x1 << HEADER_TOGGLE_SHIFT); |
| 379 | |
| 380 | flexrm_write_desc(desc_ptr, desc); |
| 381 | } |
| 382 | |
| 383 | static u64 flexrm_header_desc(u32 toggle, u32 startpkt, u32 endpkt, |
| 384 | u32 bdcount, u32 flags, u32 opaque) |
| 385 | { |
| 386 | u64 desc = 0; |
| 387 | |
| 388 | DESC_ENC(desc, HEADER_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 389 | DESC_ENC(desc, toggle, HEADER_TOGGLE_SHIFT, HEADER_TOGGLE_MASK); |
| 390 | DESC_ENC(desc, startpkt, HEADER_STARTPKT_SHIFT, HEADER_STARTPKT_MASK); |
| 391 | DESC_ENC(desc, endpkt, HEADER_ENDPKT_SHIFT, HEADER_ENDPKT_MASK); |
| 392 | DESC_ENC(desc, bdcount, HEADER_BDCOUNT_SHIFT, HEADER_BDCOUNT_MASK); |
| 393 | DESC_ENC(desc, flags, HEADER_FLAGS_SHIFT, HEADER_FLAGS_MASK); |
| 394 | DESC_ENC(desc, opaque, HEADER_OPAQUE_SHIFT, HEADER_OPAQUE_MASK); |
| 395 | |
| 396 | return desc; |
| 397 | } |
| 398 | |
| 399 | static void flexrm_enqueue_desc(u32 nhpos, u32 nhcnt, u32 reqid, |
| 400 | u64 desc, void **desc_ptr, u32 *toggle, |
| 401 | void *start_desc, void *end_desc) |
| 402 | { |
| 403 | u64 d; |
| 404 | u32 nhavail, _toggle, _startpkt, _endpkt, _bdcount; |
| 405 | |
| 406 | /* Sanity check */ |
| 407 | if (nhcnt <= nhpos) |
| 408 | return; |
| 409 | |
| 410 | /* |
| 411 | * Each request or packet start with a HEADER descriptor followed |
| 412 | * by one or more non-HEADER descriptors (SRC, SRCT, MSRC, DST, |
| 413 | * DSTT, MDST, IMM, and IMMT). The number of non-HEADER descriptors |
| 414 | * following a HEADER descriptor is represented by BDCOUNT field |
| 415 | * of HEADER descriptor. The max value of BDCOUNT field is 31 which |
| 416 | * means we can only have 31 non-HEADER descriptors following one |
| 417 | * HEADER descriptor. |
| 418 | * |
| 419 | * In general use, number of non-HEADER descriptors can easily go |
| 420 | * beyond 31. To tackle this situation, we have packet (or request) |
| 421 | * extenstion bits (STARTPKT and ENDPKT) in the HEADER descriptor. |
| 422 | * |
| 423 | * To use packet extension, the first HEADER descriptor of request |
| 424 | * (or packet) will have STARTPKT=1 and ENDPKT=0. The intermediate |
| 425 | * HEADER descriptors will have STARTPKT=0 and ENDPKT=0. The last |
| 426 | * HEADER descriptor will have STARTPKT=0 and ENDPKT=1. Also, the |
| 427 | * TOGGLE bit of the first HEADER will be set to invalid state to |
| 428 | * ensure that FlexRM does not start fetching descriptors till all |
| 429 | * descriptors are enqueued. The user of this function will flip |
| 430 | * the TOGGLE bit of first HEADER after all descriptors are |
| 431 | * enqueued. |
| 432 | */ |
| 433 | |
| 434 | if ((nhpos % HEADER_BDCOUNT_MAX == 0) && (nhcnt - nhpos)) { |
| 435 | /* Prepare the header descriptor */ |
| 436 | nhavail = (nhcnt - nhpos); |
| 437 | _toggle = (nhpos == 0) ? !(*toggle) : (*toggle); |
| 438 | _startpkt = (nhpos == 0) ? 0x1 : 0x0; |
| 439 | _endpkt = (nhavail <= HEADER_BDCOUNT_MAX) ? 0x1 : 0x0; |
| 440 | _bdcount = (nhavail <= HEADER_BDCOUNT_MAX) ? |
| 441 | nhavail : HEADER_BDCOUNT_MAX; |
| 442 | if (nhavail <= HEADER_BDCOUNT_MAX) |
| 443 | _bdcount = nhavail; |
| 444 | else |
| 445 | _bdcount = HEADER_BDCOUNT_MAX; |
| 446 | d = flexrm_header_desc(_toggle, _startpkt, _endpkt, |
| 447 | _bdcount, 0x0, reqid); |
| 448 | |
| 449 | /* Write header descriptor */ |
| 450 | flexrm_write_desc(*desc_ptr, d); |
| 451 | |
| 452 | /* Point to next descriptor */ |
| 453 | *desc_ptr += sizeof(desc); |
| 454 | if (*desc_ptr == end_desc) |
| 455 | *desc_ptr = start_desc; |
| 456 | |
| 457 | /* Skip next pointer descriptors */ |
| 458 | while (flexrm_is_next_table_desc(*desc_ptr)) { |
| 459 | *toggle = (*toggle) ? 0 : 1; |
| 460 | *desc_ptr += sizeof(desc); |
| 461 | if (*desc_ptr == end_desc) |
| 462 | *desc_ptr = start_desc; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /* Write desired descriptor */ |
| 467 | flexrm_write_desc(*desc_ptr, desc); |
| 468 | |
| 469 | /* Point to next descriptor */ |
| 470 | *desc_ptr += sizeof(desc); |
| 471 | if (*desc_ptr == end_desc) |
| 472 | *desc_ptr = start_desc; |
| 473 | |
| 474 | /* Skip next pointer descriptors */ |
| 475 | while (flexrm_is_next_table_desc(*desc_ptr)) { |
| 476 | *toggle = (*toggle) ? 0 : 1; |
| 477 | *desc_ptr += sizeof(desc); |
| 478 | if (*desc_ptr == end_desc) |
| 479 | *desc_ptr = start_desc; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static u64 flexrm_src_desc(dma_addr_t addr, unsigned int length) |
| 484 | { |
| 485 | u64 desc = 0; |
| 486 | |
| 487 | DESC_ENC(desc, SRC_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 488 | DESC_ENC(desc, length, SRC_LENGTH_SHIFT, SRC_LENGTH_MASK); |
| 489 | DESC_ENC(desc, addr, SRC_ADDR_SHIFT, SRC_ADDR_MASK); |
| 490 | |
| 491 | return desc; |
| 492 | } |
| 493 | |
| 494 | static u64 flexrm_msrc_desc(dma_addr_t addr, unsigned int length_div_16) |
| 495 | { |
| 496 | u64 desc = 0; |
| 497 | |
| 498 | DESC_ENC(desc, MSRC_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 499 | DESC_ENC(desc, length_div_16, MSRC_LENGTH_SHIFT, MSRC_LENGTH_MASK); |
| 500 | DESC_ENC(desc, addr, MSRC_ADDR_SHIFT, MSRC_ADDR_MASK); |
| 501 | |
| 502 | return desc; |
| 503 | } |
| 504 | |
| 505 | static u64 flexrm_dst_desc(dma_addr_t addr, unsigned int length) |
| 506 | { |
| 507 | u64 desc = 0; |
| 508 | |
| 509 | DESC_ENC(desc, DST_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 510 | DESC_ENC(desc, length, DST_LENGTH_SHIFT, DST_LENGTH_MASK); |
| 511 | DESC_ENC(desc, addr, DST_ADDR_SHIFT, DST_ADDR_MASK); |
| 512 | |
| 513 | return desc; |
| 514 | } |
| 515 | |
| 516 | static u64 flexrm_mdst_desc(dma_addr_t addr, unsigned int length_div_16) |
| 517 | { |
| 518 | u64 desc = 0; |
| 519 | |
| 520 | DESC_ENC(desc, MDST_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 521 | DESC_ENC(desc, length_div_16, MDST_LENGTH_SHIFT, MDST_LENGTH_MASK); |
| 522 | DESC_ENC(desc, addr, MDST_ADDR_SHIFT, MDST_ADDR_MASK); |
| 523 | |
| 524 | return desc; |
| 525 | } |
| 526 | |
| 527 | static u64 flexrm_imm_desc(u64 data) |
| 528 | { |
| 529 | u64 desc = 0; |
| 530 | |
| 531 | DESC_ENC(desc, IMM_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 532 | DESC_ENC(desc, data, IMM_DATA_SHIFT, IMM_DATA_MASK); |
| 533 | |
| 534 | return desc; |
| 535 | } |
| 536 | |
| 537 | static u64 flexrm_srct_desc(dma_addr_t addr, unsigned int length) |
| 538 | { |
| 539 | u64 desc = 0; |
| 540 | |
| 541 | DESC_ENC(desc, SRCT_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 542 | DESC_ENC(desc, length, SRCT_LENGTH_SHIFT, SRCT_LENGTH_MASK); |
| 543 | DESC_ENC(desc, addr, SRCT_ADDR_SHIFT, SRCT_ADDR_MASK); |
| 544 | |
| 545 | return desc; |
| 546 | } |
| 547 | |
| 548 | static u64 flexrm_dstt_desc(dma_addr_t addr, unsigned int length) |
| 549 | { |
| 550 | u64 desc = 0; |
| 551 | |
| 552 | DESC_ENC(desc, DSTT_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 553 | DESC_ENC(desc, length, DSTT_LENGTH_SHIFT, DSTT_LENGTH_MASK); |
| 554 | DESC_ENC(desc, addr, DSTT_ADDR_SHIFT, DSTT_ADDR_MASK); |
| 555 | |
| 556 | return desc; |
| 557 | } |
| 558 | |
| 559 | static u64 flexrm_immt_desc(u64 data) |
| 560 | { |
| 561 | u64 desc = 0; |
| 562 | |
| 563 | DESC_ENC(desc, IMMT_TYPE, DESC_TYPE_SHIFT, DESC_TYPE_MASK); |
| 564 | DESC_ENC(desc, data, IMMT_DATA_SHIFT, IMMT_DATA_MASK); |
| 565 | |
| 566 | return desc; |
| 567 | } |
| 568 | |
| 569 | static bool flexrm_spu_sanity_check(struct brcm_message *msg) |
| 570 | { |
| 571 | struct scatterlist *sg; |
| 572 | |
| 573 | if (!msg->spu.src || !msg->spu.dst) |
| 574 | return false; |
| 575 | for (sg = msg->spu.src; sg; sg = sg_next(sg)) { |
| 576 | if (sg->length & 0xf) { |
| 577 | if (sg->length > SRC_LENGTH_MASK) |
| 578 | return false; |
| 579 | } else { |
| 580 | if (sg->length > (MSRC_LENGTH_MASK * 16)) |
| 581 | return false; |
| 582 | } |
| 583 | } |
| 584 | for (sg = msg->spu.dst; sg; sg = sg_next(sg)) { |
| 585 | if (sg->length & 0xf) { |
| 586 | if (sg->length > DST_LENGTH_MASK) |
| 587 | return false; |
| 588 | } else { |
| 589 | if (sg->length > (MDST_LENGTH_MASK * 16)) |
| 590 | return false; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | static u32 flexrm_spu_estimate_nonheader_desc_count(struct brcm_message *msg) |
| 598 | { |
| 599 | u32 cnt = 0; |
| 600 | unsigned int dst_target = 0; |
| 601 | struct scatterlist *src_sg = msg->spu.src, *dst_sg = msg->spu.dst; |
| 602 | |
| 603 | while (src_sg || dst_sg) { |
| 604 | if (src_sg) { |
| 605 | cnt++; |
| 606 | dst_target = src_sg->length; |
| 607 | src_sg = sg_next(src_sg); |
| 608 | } else |
| 609 | dst_target = UINT_MAX; |
| 610 | |
| 611 | while (dst_target && dst_sg) { |
| 612 | cnt++; |
| 613 | if (dst_sg->length < dst_target) |
| 614 | dst_target -= dst_sg->length; |
| 615 | else |
| 616 | dst_target = 0; |
| 617 | dst_sg = sg_next(dst_sg); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | return cnt; |
| 622 | } |
| 623 | |
| 624 | static int flexrm_spu_dma_map(struct device *dev, struct brcm_message *msg) |
| 625 | { |
| 626 | int rc; |
| 627 | |
| 628 | rc = dma_map_sg(dev, msg->spu.src, sg_nents(msg->spu.src), |
| 629 | DMA_TO_DEVICE); |
| 630 | if (rc < 0) |
| 631 | return rc; |
| 632 | |
| 633 | rc = dma_map_sg(dev, msg->spu.dst, sg_nents(msg->spu.dst), |
| 634 | DMA_FROM_DEVICE); |
| 635 | if (rc < 0) { |
| 636 | dma_unmap_sg(dev, msg->spu.src, sg_nents(msg->spu.src), |
| 637 | DMA_TO_DEVICE); |
| 638 | return rc; |
| 639 | } |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static void flexrm_spu_dma_unmap(struct device *dev, struct brcm_message *msg) |
| 645 | { |
| 646 | dma_unmap_sg(dev, msg->spu.dst, sg_nents(msg->spu.dst), |
| 647 | DMA_FROM_DEVICE); |
| 648 | dma_unmap_sg(dev, msg->spu.src, sg_nents(msg->spu.src), |
| 649 | DMA_TO_DEVICE); |
| 650 | } |
| 651 | |
| 652 | static void *flexrm_spu_write_descs(struct brcm_message *msg, u32 nhcnt, |
| 653 | u32 reqid, void *desc_ptr, u32 toggle, |
| 654 | void *start_desc, void *end_desc) |
| 655 | { |
| 656 | u64 d; |
| 657 | u32 nhpos = 0; |
| 658 | void *orig_desc_ptr = desc_ptr; |
| 659 | unsigned int dst_target = 0; |
| 660 | struct scatterlist *src_sg = msg->spu.src, *dst_sg = msg->spu.dst; |
| 661 | |
| 662 | while (src_sg || dst_sg) { |
| 663 | if (src_sg) { |
| 664 | if (sg_dma_len(src_sg) & 0xf) |
| 665 | d = flexrm_src_desc(sg_dma_address(src_sg), |
| 666 | sg_dma_len(src_sg)); |
| 667 | else |
| 668 | d = flexrm_msrc_desc(sg_dma_address(src_sg), |
| 669 | sg_dma_len(src_sg)/16); |
| 670 | flexrm_enqueue_desc(nhpos, nhcnt, reqid, |
| 671 | d, &desc_ptr, &toggle, |
| 672 | start_desc, end_desc); |
| 673 | nhpos++; |
| 674 | dst_target = sg_dma_len(src_sg); |
| 675 | src_sg = sg_next(src_sg); |
| 676 | } else |
| 677 | dst_target = UINT_MAX; |
| 678 | |
| 679 | while (dst_target && dst_sg) { |
| 680 | if (sg_dma_len(dst_sg) & 0xf) |
| 681 | d = flexrm_dst_desc(sg_dma_address(dst_sg), |
| 682 | sg_dma_len(dst_sg)); |
| 683 | else |
| 684 | d = flexrm_mdst_desc(sg_dma_address(dst_sg), |
| 685 | sg_dma_len(dst_sg)/16); |
| 686 | flexrm_enqueue_desc(nhpos, nhcnt, reqid, |
| 687 | d, &desc_ptr, &toggle, |
| 688 | start_desc, end_desc); |
| 689 | nhpos++; |
| 690 | if (sg_dma_len(dst_sg) < dst_target) |
| 691 | dst_target -= sg_dma_len(dst_sg); |
| 692 | else |
| 693 | dst_target = 0; |
| 694 | dst_sg = sg_next(dst_sg); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /* Null descriptor with invalid toggle bit */ |
| 699 | flexrm_write_desc(desc_ptr, flexrm_null_desc(!toggle)); |
| 700 | |
| 701 | /* Ensure that descriptors have been written to memory */ |
| 702 | wmb(); |
| 703 | |
| 704 | /* Flip toggle bit in header */ |
| 705 | flexrm_flip_header_toogle(orig_desc_ptr); |
| 706 | |
| 707 | return desc_ptr; |
| 708 | } |
| 709 | |
| 710 | static bool flexrm_sba_sanity_check(struct brcm_message *msg) |
| 711 | { |
| 712 | u32 i; |
| 713 | |
| 714 | if (!msg->sba.cmds || !msg->sba.cmds_count) |
| 715 | return false; |
| 716 | |
| 717 | for (i = 0; i < msg->sba.cmds_count; i++) { |
| 718 | if (((msg->sba.cmds[i].flags & BRCM_SBA_CMD_TYPE_B) || |
| 719 | (msg->sba.cmds[i].flags & BRCM_SBA_CMD_TYPE_C)) && |
| 720 | (msg->sba.cmds[i].flags & BRCM_SBA_CMD_HAS_OUTPUT)) |
| 721 | return false; |
| 722 | if ((msg->sba.cmds[i].flags & BRCM_SBA_CMD_TYPE_B) && |
| 723 | (msg->sba.cmds[i].data_len > SRCT_LENGTH_MASK)) |
| 724 | return false; |
| 725 | if ((msg->sba.cmds[i].flags & BRCM_SBA_CMD_TYPE_C) && |
| 726 | (msg->sba.cmds[i].data_len > SRCT_LENGTH_MASK)) |
| 727 | return false; |
| 728 | if ((msg->sba.cmds[i].flags & BRCM_SBA_CMD_HAS_RESP) && |
| 729 | (msg->sba.cmds[i].resp_len > DSTT_LENGTH_MASK)) |
| 730 | return false; |
| 731 | if ((msg->sba.cmds[i].flags & BRCM_SBA_CMD_HAS_OUTPUT) && |
| 732 | (msg->sba.cmds[i].data_len > DSTT_LENGTH_MASK)) |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | return true; |
| 737 | } |
| 738 | |
| 739 | static u32 flexrm_sba_estimate_nonheader_desc_count(struct brcm_message *msg) |
| 740 | { |
| 741 | u32 i, cnt; |
| 742 | |
| 743 | cnt = 0; |
| 744 | for (i = 0; i < msg->sba.cmds_count; i++) { |
| 745 | cnt++; |
| 746 | |
| 747 | if ((msg->sba.cmds[i].flags & BRCM_SBA_CMD_TYPE_B) || |
| 748 | (msg->sba.cmds[i].flags & BRCM_SBA_CMD_TYPE_C)) |
| 749 | cnt++; |
| 750 | |
| 751 | if (msg->sba.cmds[i].flags & BRCM_SBA_CMD_HAS_RESP) |
| 752 | cnt++; |
| 753 | |
| 754 | if (msg->sba.cmds[i].flags & BRCM_SBA_CMD_HAS_OUTPUT) |
| 755 | cnt++; |
| 756 | } |
| 757 | |
| 758 | return cnt; |
| 759 | } |
| 760 | |
| 761 | static void *flexrm_sba_write_descs(struct brcm_message *msg, u32 nhcnt, |
| 762 | u32 reqid, void *desc_ptr, u32 toggle, |
| 763 | void *start_desc, void *end_desc) |
| 764 | { |
| 765 | u64 d; |
| 766 | u32 i, nhpos = 0; |
| 767 | struct brcm_sba_command *c; |
| 768 | void *orig_desc_ptr = desc_ptr; |
| 769 | |
| 770 | /* Convert SBA commands into descriptors */ |
| 771 | for (i = 0; i < msg->sba.cmds_count; i++) { |
| 772 | c = &msg->sba.cmds[i]; |
| 773 | |
| 774 | if ((c->flags & BRCM_SBA_CMD_HAS_RESP) && |
| 775 | (c->flags & BRCM_SBA_CMD_HAS_OUTPUT)) { |
| 776 | /* Destination response descriptor */ |
| 777 | d = flexrm_dst_desc(c->resp, c->resp_len); |
| 778 | flexrm_enqueue_desc(nhpos, nhcnt, reqid, |
| 779 | d, &desc_ptr, &toggle, |
| 780 | start_desc, end_desc); |
| 781 | nhpos++; |
| 782 | } else if (c->flags & BRCM_SBA_CMD_HAS_RESP) { |
| 783 | /* Destination response with tlast descriptor */ |
| 784 | d = flexrm_dstt_desc(c->resp, c->resp_len); |
| 785 | flexrm_enqueue_desc(nhpos, nhcnt, reqid, |
| 786 | d, &desc_ptr, &toggle, |
| 787 | start_desc, end_desc); |
| 788 | nhpos++; |
| 789 | } |
| 790 | |
| 791 | if (c->flags & BRCM_SBA_CMD_HAS_OUTPUT) { |
| 792 | /* Destination with tlast descriptor */ |
| 793 | d = flexrm_dstt_desc(c->data, c->data_len); |
| 794 | flexrm_enqueue_desc(nhpos, nhcnt, reqid, |
| 795 | d, &desc_ptr, &toggle, |
| 796 | start_desc, end_desc); |
| 797 | nhpos++; |
| 798 | } |
| 799 | |
| 800 | if (c->flags & BRCM_SBA_CMD_TYPE_B) { |
| 801 | /* Command as immediate descriptor */ |
| 802 | d = flexrm_imm_desc(c->cmd); |
| 803 | flexrm_enqueue_desc(nhpos, nhcnt, reqid, |
| 804 | d, &desc_ptr, &toggle, |
| 805 | start_desc, end_desc); |
| 806 | nhpos++; |
| 807 | } else { |
| 808 | /* Command as immediate descriptor with tlast */ |
| 809 | d = flexrm_immt_desc(c->cmd); |
| 810 | flexrm_enqueue_desc(nhpos, nhcnt, reqid, |
| 811 | d, &desc_ptr, &toggle, |
| 812 | start_desc, end_desc); |
| 813 | nhpos++; |
| 814 | } |
| 815 | |
| 816 | if ((c->flags & BRCM_SBA_CMD_TYPE_B) || |
| 817 | (c->flags & BRCM_SBA_CMD_TYPE_C)) { |
| 818 | /* Source with tlast descriptor */ |
| 819 | d = flexrm_srct_desc(c->data, c->data_len); |
| 820 | flexrm_enqueue_desc(nhpos, nhcnt, reqid, |
| 821 | d, &desc_ptr, &toggle, |
| 822 | start_desc, end_desc); |
| 823 | nhpos++; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /* Null descriptor with invalid toggle bit */ |
| 828 | flexrm_write_desc(desc_ptr, flexrm_null_desc(!toggle)); |
| 829 | |
| 830 | /* Ensure that descriptors have been written to memory */ |
| 831 | wmb(); |
| 832 | |
| 833 | /* Flip toggle bit in header */ |
| 834 | flexrm_flip_header_toogle(orig_desc_ptr); |
| 835 | |
| 836 | return desc_ptr; |
| 837 | } |
| 838 | |
| 839 | static bool flexrm_sanity_check(struct brcm_message *msg) |
| 840 | { |
| 841 | if (!msg) |
| 842 | return false; |
| 843 | |
| 844 | switch (msg->type) { |
| 845 | case BRCM_MESSAGE_SPU: |
| 846 | return flexrm_spu_sanity_check(msg); |
| 847 | case BRCM_MESSAGE_SBA: |
| 848 | return flexrm_sba_sanity_check(msg); |
| 849 | default: |
| 850 | return false; |
| 851 | }; |
| 852 | } |
| 853 | |
| 854 | static u32 flexrm_estimate_nonheader_desc_count(struct brcm_message *msg) |
| 855 | { |
| 856 | if (!msg) |
| 857 | return 0; |
| 858 | |
| 859 | switch (msg->type) { |
| 860 | case BRCM_MESSAGE_SPU: |
| 861 | return flexrm_spu_estimate_nonheader_desc_count(msg); |
| 862 | case BRCM_MESSAGE_SBA: |
| 863 | return flexrm_sba_estimate_nonheader_desc_count(msg); |
| 864 | default: |
| 865 | return 0; |
| 866 | }; |
| 867 | } |
| 868 | |
| 869 | static int flexrm_dma_map(struct device *dev, struct brcm_message *msg) |
| 870 | { |
| 871 | if (!dev || !msg) |
| 872 | return -EINVAL; |
| 873 | |
| 874 | switch (msg->type) { |
| 875 | case BRCM_MESSAGE_SPU: |
| 876 | return flexrm_spu_dma_map(dev, msg); |
| 877 | default: |
| 878 | break; |
| 879 | } |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static void flexrm_dma_unmap(struct device *dev, struct brcm_message *msg) |
| 885 | { |
| 886 | if (!dev || !msg) |
| 887 | return; |
| 888 | |
| 889 | switch (msg->type) { |
| 890 | case BRCM_MESSAGE_SPU: |
| 891 | flexrm_spu_dma_unmap(dev, msg); |
| 892 | break; |
| 893 | default: |
| 894 | break; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | static void *flexrm_write_descs(struct brcm_message *msg, u32 nhcnt, |
| 899 | u32 reqid, void *desc_ptr, u32 toggle, |
| 900 | void *start_desc, void *end_desc) |
| 901 | { |
| 902 | if (!msg || !desc_ptr || !start_desc || !end_desc) |
| 903 | return ERR_PTR(-ENOTSUPP); |
| 904 | |
| 905 | if ((desc_ptr < start_desc) || (end_desc <= desc_ptr)) |
| 906 | return ERR_PTR(-ERANGE); |
| 907 | |
| 908 | switch (msg->type) { |
| 909 | case BRCM_MESSAGE_SPU: |
| 910 | return flexrm_spu_write_descs(msg, nhcnt, reqid, |
| 911 | desc_ptr, toggle, |
| 912 | start_desc, end_desc); |
| 913 | case BRCM_MESSAGE_SBA: |
| 914 | return flexrm_sba_write_descs(msg, nhcnt, reqid, |
| 915 | desc_ptr, toggle, |
| 916 | start_desc, end_desc); |
| 917 | default: |
| 918 | return ERR_PTR(-ENOTSUPP); |
| 919 | }; |
| 920 | } |
| 921 | |
| 922 | /* ====== FlexRM driver helper routines ===== */ |
| 923 | |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 924 | static void flexrm_write_config_in_seqfile(struct flexrm_mbox *mbox, |
| 925 | struct seq_file *file) |
| 926 | { |
| 927 | int i; |
| 928 | const char *state; |
| 929 | struct flexrm_ring *ring; |
| 930 | |
| 931 | seq_printf(file, "%-5s %-9s %-18s %-10s %-18s %-10s\n", |
| 932 | "Ring#", "State", "BD_Addr", "BD_Size", |
| 933 | "Cmpl_Addr", "Cmpl_Size"); |
| 934 | |
| 935 | for (i = 0; i < mbox->num_rings; i++) { |
| 936 | ring = &mbox->rings[i]; |
| 937 | if (readl(ring->regs + RING_CONTROL) & |
| 938 | BIT(CONTROL_ACTIVE_SHIFT)) |
| 939 | state = "active"; |
| 940 | else |
| 941 | state = "inactive"; |
| 942 | seq_printf(file, |
| 943 | "%-5d %-9s 0x%016llx 0x%08x 0x%016llx 0x%08x\n", |
| 944 | ring->num, state, |
| 945 | (unsigned long long)ring->bd_dma_base, |
| 946 | (u32)RING_BD_SIZE, |
| 947 | (unsigned long long)ring->cmpl_dma_base, |
| 948 | (u32)RING_CMPL_SIZE); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | static void flexrm_write_stats_in_seqfile(struct flexrm_mbox *mbox, |
| 953 | struct seq_file *file) |
| 954 | { |
| 955 | int i; |
| 956 | u32 val, bd_read_offset; |
| 957 | struct flexrm_ring *ring; |
| 958 | |
| 959 | seq_printf(file, "%-5s %-10s %-10s %-10s %-11s %-11s\n", |
| 960 | "Ring#", "BD_Read", "BD_Write", |
| 961 | "Cmpl_Read", "Submitted", "Completed"); |
| 962 | |
| 963 | for (i = 0; i < mbox->num_rings; i++) { |
| 964 | ring = &mbox->rings[i]; |
| 965 | bd_read_offset = readl_relaxed(ring->regs + RING_BD_READ_PTR); |
| 966 | val = readl_relaxed(ring->regs + RING_BD_START_ADDR); |
| 967 | bd_read_offset *= RING_DESC_SIZE; |
| 968 | bd_read_offset += (u32)(BD_START_ADDR_DECODE(val) - |
| 969 | ring->bd_dma_base); |
| 970 | seq_printf(file, "%-5d 0x%08x 0x%08x 0x%08x %-11d %-11d\n", |
| 971 | ring->num, |
| 972 | (u32)bd_read_offset, |
| 973 | (u32)ring->bd_write_offset, |
| 974 | (u32)ring->cmpl_read_offset, |
| 975 | (u32)atomic_read(&ring->msg_send_count), |
| 976 | (u32)atomic_read(&ring->msg_cmpl_count)); |
| 977 | } |
| 978 | } |
| 979 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 980 | static int flexrm_new_request(struct flexrm_ring *ring, |
| 981 | struct brcm_message *batch_msg, |
| 982 | struct brcm_message *msg) |
| 983 | { |
| 984 | void *next; |
| 985 | unsigned long flags; |
| 986 | u32 val, count, nhcnt; |
| 987 | u32 read_offset, write_offset; |
| 988 | bool exit_cleanup = false; |
| 989 | int ret = 0, reqid; |
| 990 | |
| 991 | /* Do sanity check on message */ |
| 992 | if (!flexrm_sanity_check(msg)) |
| 993 | return -EIO; |
| 994 | msg->error = 0; |
| 995 | |
| 996 | /* If no requests possible then save data pointer and goto done. */ |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 997 | spin_lock_irqsave(&ring->lock, flags); |
| 998 | reqid = bitmap_find_free_region(ring->requests_bmap, |
| 999 | RING_MAX_REQ_COUNT, 0); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1000 | if (reqid < 0) { |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1001 | if (batch_msg) |
| 1002 | ring->last_pending_msg = batch_msg; |
| 1003 | else |
| 1004 | ring->last_pending_msg = msg; |
| 1005 | spin_unlock_irqrestore(&ring->lock, flags); |
| 1006 | return 0; |
| 1007 | } |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 1008 | spin_unlock_irqrestore(&ring->lock, flags); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1009 | ring->requests[reqid] = msg; |
| 1010 | |
| 1011 | /* Do DMA mappings for the message */ |
| 1012 | ret = flexrm_dma_map(ring->mbox->dev, msg); |
| 1013 | if (ret < 0) { |
| 1014 | ring->requests[reqid] = NULL; |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 1015 | spin_lock_irqsave(&ring->lock, flags); |
| 1016 | bitmap_release_region(ring->requests_bmap, reqid, 0); |
| 1017 | spin_unlock_irqrestore(&ring->lock, flags); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1018 | return ret; |
| 1019 | } |
| 1020 | |
| 1021 | /* If last_pending_msg is already set then goto done with error */ |
| 1022 | spin_lock_irqsave(&ring->lock, flags); |
| 1023 | if (ring->last_pending_msg) |
| 1024 | ret = -ENOSPC; |
| 1025 | spin_unlock_irqrestore(&ring->lock, flags); |
| 1026 | if (ret < 0) { |
| 1027 | dev_warn(ring->mbox->dev, "no space in ring %d\n", ring->num); |
| 1028 | exit_cleanup = true; |
| 1029 | goto exit; |
| 1030 | } |
| 1031 | |
| 1032 | /* Determine current HW BD read offset */ |
| 1033 | read_offset = readl_relaxed(ring->regs + RING_BD_READ_PTR); |
| 1034 | val = readl_relaxed(ring->regs + RING_BD_START_ADDR); |
| 1035 | read_offset *= RING_DESC_SIZE; |
| 1036 | read_offset += (u32)(BD_START_ADDR_DECODE(val) - ring->bd_dma_base); |
| 1037 | |
| 1038 | /* |
| 1039 | * Number required descriptors = number of non-header descriptors + |
| 1040 | * number of header descriptors + |
| 1041 | * 1x null descriptor |
| 1042 | */ |
| 1043 | nhcnt = flexrm_estimate_nonheader_desc_count(msg); |
| 1044 | count = flexrm_estimate_header_desc_count(nhcnt) + nhcnt + 1; |
| 1045 | |
| 1046 | /* Check for available descriptor space. */ |
| 1047 | write_offset = ring->bd_write_offset; |
| 1048 | while (count) { |
| 1049 | if (!flexrm_is_next_table_desc(ring->bd_base + write_offset)) |
| 1050 | count--; |
| 1051 | write_offset += RING_DESC_SIZE; |
| 1052 | if (write_offset == RING_BD_SIZE) |
| 1053 | write_offset = 0x0; |
| 1054 | if (write_offset == read_offset) |
| 1055 | break; |
| 1056 | } |
| 1057 | if (count) { |
| 1058 | spin_lock_irqsave(&ring->lock, flags); |
| 1059 | if (batch_msg) |
| 1060 | ring->last_pending_msg = batch_msg; |
| 1061 | else |
| 1062 | ring->last_pending_msg = msg; |
| 1063 | spin_unlock_irqrestore(&ring->lock, flags); |
| 1064 | ret = 0; |
| 1065 | exit_cleanup = true; |
| 1066 | goto exit; |
| 1067 | } |
| 1068 | |
| 1069 | /* Write descriptors to ring */ |
| 1070 | next = flexrm_write_descs(msg, nhcnt, reqid, |
| 1071 | ring->bd_base + ring->bd_write_offset, |
| 1072 | RING_BD_TOGGLE_VALID(ring->bd_write_offset), |
| 1073 | ring->bd_base, ring->bd_base + RING_BD_SIZE); |
| 1074 | if (IS_ERR(next)) { |
| 1075 | ret = PTR_ERR(next); |
| 1076 | exit_cleanup = true; |
| 1077 | goto exit; |
| 1078 | } |
| 1079 | |
| 1080 | /* Save ring BD write offset */ |
| 1081 | ring->bd_write_offset = (unsigned long)(next - ring->bd_base); |
| 1082 | |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1083 | /* Increment number of messages sent */ |
| 1084 | atomic_inc_return(&ring->msg_send_count); |
| 1085 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1086 | exit: |
| 1087 | /* Update error status in message */ |
| 1088 | msg->error = ret; |
| 1089 | |
| 1090 | /* Cleanup if we failed */ |
| 1091 | if (exit_cleanup) { |
| 1092 | flexrm_dma_unmap(ring->mbox->dev, msg); |
| 1093 | ring->requests[reqid] = NULL; |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 1094 | spin_lock_irqsave(&ring->lock, flags); |
| 1095 | bitmap_release_region(ring->requests_bmap, reqid, 0); |
| 1096 | spin_unlock_irqrestore(&ring->lock, flags); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
| 1102 | static int flexrm_process_completions(struct flexrm_ring *ring) |
| 1103 | { |
| 1104 | u64 desc; |
| 1105 | int err, count = 0; |
| 1106 | unsigned long flags; |
| 1107 | struct brcm_message *msg = NULL; |
| 1108 | u32 reqid, cmpl_read_offset, cmpl_write_offset; |
| 1109 | struct mbox_chan *chan = &ring->mbox->controller.chans[ring->num]; |
| 1110 | |
| 1111 | spin_lock_irqsave(&ring->lock, flags); |
| 1112 | |
| 1113 | /* Check last_pending_msg */ |
| 1114 | if (ring->last_pending_msg) { |
| 1115 | msg = ring->last_pending_msg; |
| 1116 | ring->last_pending_msg = NULL; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * Get current completion read and write offset |
| 1121 | * |
| 1122 | * Note: We should read completion write pointer atleast once |
| 1123 | * after we get a MSI interrupt because HW maintains internal |
| 1124 | * MSI status which will allow next MSI interrupt only after |
| 1125 | * completion write pointer is read. |
| 1126 | */ |
| 1127 | cmpl_write_offset = readl_relaxed(ring->regs + RING_CMPL_WRITE_PTR); |
| 1128 | cmpl_write_offset *= RING_DESC_SIZE; |
| 1129 | cmpl_read_offset = ring->cmpl_read_offset; |
| 1130 | ring->cmpl_read_offset = cmpl_write_offset; |
| 1131 | |
| 1132 | spin_unlock_irqrestore(&ring->lock, flags); |
| 1133 | |
| 1134 | /* If last_pending_msg was set then queue it back */ |
| 1135 | if (msg) |
| 1136 | mbox_send_message(chan, msg); |
| 1137 | |
| 1138 | /* For each completed request notify mailbox clients */ |
| 1139 | reqid = 0; |
| 1140 | while (cmpl_read_offset != cmpl_write_offset) { |
| 1141 | /* Dequeue next completion descriptor */ |
| 1142 | desc = *((u64 *)(ring->cmpl_base + cmpl_read_offset)); |
| 1143 | |
| 1144 | /* Next read offset */ |
| 1145 | cmpl_read_offset += RING_DESC_SIZE; |
| 1146 | if (cmpl_read_offset == RING_CMPL_SIZE) |
| 1147 | cmpl_read_offset = 0; |
| 1148 | |
| 1149 | /* Decode error from completion descriptor */ |
| 1150 | err = flexrm_cmpl_desc_to_error(desc); |
| 1151 | if (err < 0) { |
| 1152 | dev_warn(ring->mbox->dev, |
| 1153 | "got completion desc=0x%lx with error %d", |
| 1154 | (unsigned long)desc, err); |
| 1155 | } |
| 1156 | |
| 1157 | /* Determine request id from completion descriptor */ |
| 1158 | reqid = flexrm_cmpl_desc_to_reqid(desc); |
| 1159 | |
| 1160 | /* Determine message pointer based on reqid */ |
| 1161 | msg = ring->requests[reqid]; |
| 1162 | if (!msg) { |
| 1163 | dev_warn(ring->mbox->dev, |
| 1164 | "null msg pointer for completion desc=0x%lx", |
| 1165 | (unsigned long)desc); |
| 1166 | continue; |
| 1167 | } |
| 1168 | |
| 1169 | /* Release reqid for recycling */ |
| 1170 | ring->requests[reqid] = NULL; |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 1171 | spin_lock_irqsave(&ring->lock, flags); |
| 1172 | bitmap_release_region(ring->requests_bmap, reqid, 0); |
| 1173 | spin_unlock_irqrestore(&ring->lock, flags); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1174 | |
| 1175 | /* Unmap DMA mappings */ |
| 1176 | flexrm_dma_unmap(ring->mbox->dev, msg); |
| 1177 | |
| 1178 | /* Give-back message to mailbox client */ |
| 1179 | msg->error = err; |
| 1180 | mbox_chan_received_data(chan, msg); |
| 1181 | |
| 1182 | /* Increment number of completions processed */ |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1183 | atomic_inc_return(&ring->msg_cmpl_count); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1184 | count++; |
| 1185 | } |
| 1186 | |
| 1187 | return count; |
| 1188 | } |
| 1189 | |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1190 | /* ====== FlexRM Debugfs callbacks ====== */ |
| 1191 | |
| 1192 | static int flexrm_debugfs_conf_show(struct seq_file *file, void *offset) |
| 1193 | { |
| 1194 | struct platform_device *pdev = to_platform_device(file->private); |
| 1195 | struct flexrm_mbox *mbox = platform_get_drvdata(pdev); |
| 1196 | |
| 1197 | /* Write config in file */ |
| 1198 | flexrm_write_config_in_seqfile(mbox, file); |
| 1199 | |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static int flexrm_debugfs_stats_show(struct seq_file *file, void *offset) |
| 1204 | { |
| 1205 | struct platform_device *pdev = to_platform_device(file->private); |
| 1206 | struct flexrm_mbox *mbox = platform_get_drvdata(pdev); |
| 1207 | |
| 1208 | /* Write stats in file */ |
| 1209 | flexrm_write_stats_in_seqfile(mbox, file); |
| 1210 | |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1214 | /* ====== FlexRM interrupt handler ===== */ |
| 1215 | |
| 1216 | static irqreturn_t flexrm_irq_event(int irq, void *dev_id) |
| 1217 | { |
| 1218 | /* We only have MSI for completions so just wakeup IRQ thread */ |
| 1219 | /* Ring related errors will be informed via completion descriptors */ |
| 1220 | |
| 1221 | return IRQ_WAKE_THREAD; |
| 1222 | } |
| 1223 | |
| 1224 | static irqreturn_t flexrm_irq_thread(int irq, void *dev_id) |
| 1225 | { |
| 1226 | flexrm_process_completions(dev_id); |
| 1227 | |
| 1228 | return IRQ_HANDLED; |
| 1229 | } |
| 1230 | |
| 1231 | /* ====== FlexRM mailbox callbacks ===== */ |
| 1232 | |
| 1233 | static int flexrm_send_data(struct mbox_chan *chan, void *data) |
| 1234 | { |
| 1235 | int i, rc; |
| 1236 | struct flexrm_ring *ring = chan->con_priv; |
| 1237 | struct brcm_message *msg = data; |
| 1238 | |
| 1239 | if (msg->type == BRCM_MESSAGE_BATCH) { |
| 1240 | for (i = msg->batch.msgs_queued; |
| 1241 | i < msg->batch.msgs_count; i++) { |
| 1242 | rc = flexrm_new_request(ring, msg, |
| 1243 | &msg->batch.msgs[i]); |
| 1244 | if (rc) { |
| 1245 | msg->error = rc; |
| 1246 | return rc; |
| 1247 | } |
| 1248 | msg->batch.msgs_queued++; |
| 1249 | } |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | return flexrm_new_request(ring, NULL, data); |
| 1254 | } |
| 1255 | |
| 1256 | static bool flexrm_peek_data(struct mbox_chan *chan) |
| 1257 | { |
| 1258 | int cnt = flexrm_process_completions(chan->con_priv); |
| 1259 | |
| 1260 | return (cnt > 0) ? true : false; |
| 1261 | } |
| 1262 | |
| 1263 | static int flexrm_startup(struct mbox_chan *chan) |
| 1264 | { |
| 1265 | u64 d; |
| 1266 | u32 val, off; |
| 1267 | int ret = 0; |
| 1268 | dma_addr_t next_addr; |
| 1269 | struct flexrm_ring *ring = chan->con_priv; |
| 1270 | |
| 1271 | /* Allocate BD memory */ |
| 1272 | ring->bd_base = dma_pool_alloc(ring->mbox->bd_pool, |
| 1273 | GFP_KERNEL, &ring->bd_dma_base); |
| 1274 | if (!ring->bd_base) { |
| 1275 | dev_err(ring->mbox->dev, "can't allocate BD memory\n"); |
| 1276 | ret = -ENOMEM; |
| 1277 | goto fail; |
| 1278 | } |
| 1279 | |
| 1280 | /* Configure next table pointer entries in BD memory */ |
| 1281 | for (off = 0; off < RING_BD_SIZE; off += RING_DESC_SIZE) { |
| 1282 | next_addr = off + RING_DESC_SIZE; |
| 1283 | if (next_addr == RING_BD_SIZE) |
| 1284 | next_addr = 0; |
| 1285 | next_addr += ring->bd_dma_base; |
| 1286 | if (RING_BD_ALIGN_CHECK(next_addr)) |
| 1287 | d = flexrm_next_table_desc(RING_BD_TOGGLE_VALID(off), |
| 1288 | next_addr); |
| 1289 | else |
| 1290 | d = flexrm_null_desc(RING_BD_TOGGLE_INVALID(off)); |
| 1291 | flexrm_write_desc(ring->bd_base + off, d); |
| 1292 | } |
| 1293 | |
| 1294 | /* Allocate completion memory */ |
| 1295 | ring->cmpl_base = dma_pool_alloc(ring->mbox->cmpl_pool, |
| 1296 | GFP_KERNEL, &ring->cmpl_dma_base); |
| 1297 | if (!ring->cmpl_base) { |
| 1298 | dev_err(ring->mbox->dev, "can't allocate completion memory\n"); |
| 1299 | ret = -ENOMEM; |
| 1300 | goto fail_free_bd_memory; |
| 1301 | } |
| 1302 | memset(ring->cmpl_base, 0, RING_CMPL_SIZE); |
| 1303 | |
| 1304 | /* Request IRQ */ |
| 1305 | if (ring->irq == UINT_MAX) { |
| 1306 | dev_err(ring->mbox->dev, "ring IRQ not available\n"); |
| 1307 | ret = -ENODEV; |
| 1308 | goto fail_free_cmpl_memory; |
| 1309 | } |
| 1310 | ret = request_threaded_irq(ring->irq, |
| 1311 | flexrm_irq_event, |
| 1312 | flexrm_irq_thread, |
| 1313 | 0, dev_name(ring->mbox->dev), ring); |
| 1314 | if (ret) { |
| 1315 | dev_err(ring->mbox->dev, "failed to request ring IRQ\n"); |
| 1316 | goto fail_free_cmpl_memory; |
| 1317 | } |
| 1318 | ring->irq_requested = true; |
| 1319 | |
Anup Patel | 6ac17fe | 2017-08-01 16:05:50 +0530 | [diff] [blame] | 1320 | /* Set IRQ affinity hint */ |
| 1321 | ring->irq_aff_hint = CPU_MASK_NONE; |
| 1322 | val = ring->mbox->num_rings; |
| 1323 | val = (num_online_cpus() < val) ? val / num_online_cpus() : 1; |
| 1324 | cpumask_set_cpu((ring->num / val) % num_online_cpus(), |
| 1325 | &ring->irq_aff_hint); |
| 1326 | ret = irq_set_affinity_hint(ring->irq, &ring->irq_aff_hint); |
| 1327 | if (ret) { |
| 1328 | dev_err(ring->mbox->dev, "failed to set IRQ affinity hint\n"); |
| 1329 | goto fail_free_irq; |
| 1330 | } |
| 1331 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1332 | /* Disable/inactivate ring */ |
| 1333 | writel_relaxed(0x0, ring->regs + RING_CONTROL); |
| 1334 | |
| 1335 | /* Program BD start address */ |
| 1336 | val = BD_START_ADDR_VALUE(ring->bd_dma_base); |
| 1337 | writel_relaxed(val, ring->regs + RING_BD_START_ADDR); |
| 1338 | |
| 1339 | /* BD write pointer will be same as HW write pointer */ |
| 1340 | ring->bd_write_offset = |
| 1341 | readl_relaxed(ring->regs + RING_BD_WRITE_PTR); |
| 1342 | ring->bd_write_offset *= RING_DESC_SIZE; |
| 1343 | |
| 1344 | /* Program completion start address */ |
| 1345 | val = CMPL_START_ADDR_VALUE(ring->cmpl_dma_base); |
| 1346 | writel_relaxed(val, ring->regs + RING_CMPL_START_ADDR); |
| 1347 | |
| 1348 | /* Ensure last pending message is cleared */ |
| 1349 | ring->last_pending_msg = NULL; |
| 1350 | |
| 1351 | /* Completion read pointer will be same as HW write pointer */ |
| 1352 | ring->cmpl_read_offset = |
| 1353 | readl_relaxed(ring->regs + RING_CMPL_WRITE_PTR); |
| 1354 | ring->cmpl_read_offset *= RING_DESC_SIZE; |
| 1355 | |
| 1356 | /* Read ring Tx, Rx, and Outstanding counts to clear */ |
| 1357 | readl_relaxed(ring->regs + RING_NUM_REQ_RECV_LS); |
| 1358 | readl_relaxed(ring->regs + RING_NUM_REQ_RECV_MS); |
| 1359 | readl_relaxed(ring->regs + RING_NUM_REQ_TRANS_LS); |
| 1360 | readl_relaxed(ring->regs + RING_NUM_REQ_TRANS_MS); |
| 1361 | readl_relaxed(ring->regs + RING_NUM_REQ_OUTSTAND); |
| 1362 | |
| 1363 | /* Configure RING_MSI_CONTROL */ |
| 1364 | val = 0; |
| 1365 | val |= (ring->msi_timer_val << MSI_TIMER_VAL_SHIFT); |
| 1366 | val |= BIT(MSI_ENABLE_SHIFT); |
| 1367 | val |= (ring->msi_count_threshold & MSI_COUNT_MASK) << MSI_COUNT_SHIFT; |
| 1368 | writel_relaxed(val, ring->regs + RING_MSI_CONTROL); |
| 1369 | |
| 1370 | /* Enable/activate ring */ |
| 1371 | val = BIT(CONTROL_ACTIVE_SHIFT); |
| 1372 | writel_relaxed(val, ring->regs + RING_CONTROL); |
| 1373 | |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1374 | /* Reset stats to zero */ |
| 1375 | atomic_set(&ring->msg_send_count, 0); |
| 1376 | atomic_set(&ring->msg_cmpl_count, 0); |
| 1377 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1378 | return 0; |
| 1379 | |
Anup Patel | 6ac17fe | 2017-08-01 16:05:50 +0530 | [diff] [blame] | 1380 | fail_free_irq: |
| 1381 | free_irq(ring->irq, ring); |
| 1382 | ring->irq_requested = false; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1383 | fail_free_cmpl_memory: |
| 1384 | dma_pool_free(ring->mbox->cmpl_pool, |
| 1385 | ring->cmpl_base, ring->cmpl_dma_base); |
| 1386 | ring->cmpl_base = NULL; |
| 1387 | fail_free_bd_memory: |
| 1388 | dma_pool_free(ring->mbox->bd_pool, |
| 1389 | ring->bd_base, ring->bd_dma_base); |
| 1390 | ring->bd_base = NULL; |
| 1391 | fail: |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
| 1395 | static void flexrm_shutdown(struct mbox_chan *chan) |
| 1396 | { |
| 1397 | u32 reqid; |
| 1398 | unsigned int timeout; |
| 1399 | struct brcm_message *msg; |
| 1400 | struct flexrm_ring *ring = chan->con_priv; |
| 1401 | |
| 1402 | /* Disable/inactivate ring */ |
| 1403 | writel_relaxed(0x0, ring->regs + RING_CONTROL); |
| 1404 | |
| 1405 | /* Flush ring with timeout of 1s */ |
| 1406 | timeout = 1000; |
| 1407 | writel_relaxed(BIT(CONTROL_FLUSH_SHIFT), |
| 1408 | ring->regs + RING_CONTROL); |
| 1409 | do { |
| 1410 | if (readl_relaxed(ring->regs + RING_FLUSH_DONE) & |
| 1411 | FLUSH_DONE_MASK) |
| 1412 | break; |
| 1413 | mdelay(1); |
| 1414 | } while (timeout--); |
| 1415 | |
| 1416 | /* Abort all in-flight requests */ |
| 1417 | for (reqid = 0; reqid < RING_MAX_REQ_COUNT; reqid++) { |
| 1418 | msg = ring->requests[reqid]; |
| 1419 | if (!msg) |
| 1420 | continue; |
| 1421 | |
| 1422 | /* Release reqid for recycling */ |
| 1423 | ring->requests[reqid] = NULL; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1424 | |
| 1425 | /* Unmap DMA mappings */ |
| 1426 | flexrm_dma_unmap(ring->mbox->dev, msg); |
| 1427 | |
| 1428 | /* Give-back message to mailbox client */ |
| 1429 | msg->error = -EIO; |
| 1430 | mbox_chan_received_data(chan, msg); |
| 1431 | } |
| 1432 | |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 1433 | /* Clear requests bitmap */ |
| 1434 | bitmap_zero(ring->requests_bmap, RING_MAX_REQ_COUNT); |
| 1435 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1436 | /* Release IRQ */ |
| 1437 | if (ring->irq_requested) { |
Anup Patel | 6ac17fe | 2017-08-01 16:05:50 +0530 | [diff] [blame] | 1438 | irq_set_affinity_hint(ring->irq, NULL); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1439 | free_irq(ring->irq, ring); |
| 1440 | ring->irq_requested = false; |
| 1441 | } |
| 1442 | |
| 1443 | /* Free-up completion descriptor ring */ |
| 1444 | if (ring->cmpl_base) { |
| 1445 | dma_pool_free(ring->mbox->cmpl_pool, |
| 1446 | ring->cmpl_base, ring->cmpl_dma_base); |
| 1447 | ring->cmpl_base = NULL; |
| 1448 | } |
| 1449 | |
| 1450 | /* Free-up BD descriptor ring */ |
| 1451 | if (ring->bd_base) { |
| 1452 | dma_pool_free(ring->mbox->bd_pool, |
| 1453 | ring->bd_base, ring->bd_dma_base); |
| 1454 | ring->bd_base = NULL; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | static bool flexrm_last_tx_done(struct mbox_chan *chan) |
| 1459 | { |
| 1460 | bool ret; |
| 1461 | unsigned long flags; |
| 1462 | struct flexrm_ring *ring = chan->con_priv; |
| 1463 | |
| 1464 | spin_lock_irqsave(&ring->lock, flags); |
| 1465 | ret = (ring->last_pending_msg) ? false : true; |
| 1466 | spin_unlock_irqrestore(&ring->lock, flags); |
| 1467 | |
| 1468 | return ret; |
| 1469 | } |
| 1470 | |
| 1471 | static const struct mbox_chan_ops flexrm_mbox_chan_ops = { |
| 1472 | .send_data = flexrm_send_data, |
| 1473 | .startup = flexrm_startup, |
| 1474 | .shutdown = flexrm_shutdown, |
| 1475 | .last_tx_done = flexrm_last_tx_done, |
| 1476 | .peek_data = flexrm_peek_data, |
| 1477 | }; |
| 1478 | |
| 1479 | static struct mbox_chan *flexrm_mbox_of_xlate(struct mbox_controller *cntlr, |
| 1480 | const struct of_phandle_args *pa) |
| 1481 | { |
| 1482 | struct mbox_chan *chan; |
| 1483 | struct flexrm_ring *ring; |
| 1484 | |
| 1485 | if (pa->args_count < 3) |
| 1486 | return ERR_PTR(-EINVAL); |
| 1487 | |
| 1488 | if (pa->args[0] >= cntlr->num_chans) |
| 1489 | return ERR_PTR(-ENOENT); |
| 1490 | |
| 1491 | if (pa->args[1] > MSI_COUNT_MASK) |
| 1492 | return ERR_PTR(-EINVAL); |
| 1493 | |
| 1494 | if (pa->args[2] > MSI_TIMER_VAL_MASK) |
| 1495 | return ERR_PTR(-EINVAL); |
| 1496 | |
| 1497 | chan = &cntlr->chans[pa->args[0]]; |
| 1498 | ring = chan->con_priv; |
| 1499 | ring->msi_count_threshold = pa->args[1]; |
| 1500 | ring->msi_timer_val = pa->args[2]; |
| 1501 | |
| 1502 | return chan; |
| 1503 | } |
| 1504 | |
| 1505 | /* ====== FlexRM platform driver ===== */ |
| 1506 | |
| 1507 | static void flexrm_mbox_msi_write(struct msi_desc *desc, struct msi_msg *msg) |
| 1508 | { |
| 1509 | struct device *dev = msi_desc_to_dev(desc); |
| 1510 | struct flexrm_mbox *mbox = dev_get_drvdata(dev); |
| 1511 | struct flexrm_ring *ring = &mbox->rings[desc->platform.msi_index]; |
| 1512 | |
| 1513 | /* Configure per-Ring MSI registers */ |
| 1514 | writel_relaxed(msg->address_lo, ring->regs + RING_MSI_ADDR_LS); |
| 1515 | writel_relaxed(msg->address_hi, ring->regs + RING_MSI_ADDR_MS); |
| 1516 | writel_relaxed(msg->data, ring->regs + RING_MSI_DATA_VALUE); |
| 1517 | } |
| 1518 | |
| 1519 | static int flexrm_mbox_probe(struct platform_device *pdev) |
| 1520 | { |
| 1521 | int index, ret = 0; |
| 1522 | void __iomem *regs; |
| 1523 | void __iomem *regs_end; |
| 1524 | struct msi_desc *desc; |
| 1525 | struct resource *iomem; |
| 1526 | struct flexrm_ring *ring; |
| 1527 | struct flexrm_mbox *mbox; |
| 1528 | struct device *dev = &pdev->dev; |
| 1529 | |
| 1530 | /* Allocate driver mailbox struct */ |
| 1531 | mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL); |
| 1532 | if (!mbox) { |
| 1533 | ret = -ENOMEM; |
| 1534 | goto fail; |
| 1535 | } |
| 1536 | mbox->dev = dev; |
| 1537 | platform_set_drvdata(pdev, mbox); |
| 1538 | |
| 1539 | /* Get resource for registers */ |
| 1540 | iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1541 | if (!iomem || (resource_size(iomem) < RING_REGS_SIZE)) { |
| 1542 | ret = -ENODEV; |
| 1543 | goto fail; |
| 1544 | } |
| 1545 | |
| 1546 | /* Map registers of all rings */ |
| 1547 | mbox->regs = devm_ioremap_resource(&pdev->dev, iomem); |
| 1548 | if (IS_ERR(mbox->regs)) { |
| 1549 | ret = PTR_ERR(mbox->regs); |
| 1550 | dev_err(&pdev->dev, "Failed to remap mailbox regs: %d\n", ret); |
| 1551 | goto fail; |
| 1552 | } |
| 1553 | regs_end = mbox->regs + resource_size(iomem); |
| 1554 | |
| 1555 | /* Scan and count available rings */ |
| 1556 | mbox->num_rings = 0; |
| 1557 | for (regs = mbox->regs; regs < regs_end; regs += RING_REGS_SIZE) { |
| 1558 | if (readl_relaxed(regs + RING_VER) == RING_VER_MAGIC) |
| 1559 | mbox->num_rings++; |
| 1560 | } |
| 1561 | if (!mbox->num_rings) { |
| 1562 | ret = -ENODEV; |
| 1563 | goto fail; |
| 1564 | } |
| 1565 | |
| 1566 | /* Allocate driver ring structs */ |
| 1567 | ring = devm_kcalloc(dev, mbox->num_rings, sizeof(*ring), GFP_KERNEL); |
| 1568 | if (!ring) { |
| 1569 | ret = -ENOMEM; |
| 1570 | goto fail; |
| 1571 | } |
| 1572 | mbox->rings = ring; |
| 1573 | |
| 1574 | /* Initialize members of driver ring structs */ |
| 1575 | regs = mbox->regs; |
| 1576 | for (index = 0; index < mbox->num_rings; index++) { |
| 1577 | ring = &mbox->rings[index]; |
| 1578 | ring->num = index; |
| 1579 | ring->mbox = mbox; |
| 1580 | while ((regs < regs_end) && |
| 1581 | (readl_relaxed(regs + RING_VER) != RING_VER_MAGIC)) |
| 1582 | regs += RING_REGS_SIZE; |
| 1583 | if (regs_end <= regs) { |
| 1584 | ret = -ENODEV; |
| 1585 | goto fail; |
| 1586 | } |
| 1587 | ring->regs = regs; |
| 1588 | regs += RING_REGS_SIZE; |
| 1589 | ring->irq = UINT_MAX; |
| 1590 | ring->irq_requested = false; |
| 1591 | ring->msi_timer_val = MSI_TIMER_VAL_MASK; |
| 1592 | ring->msi_count_threshold = 0x1; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1593 | memset(ring->requests, 0, sizeof(ring->requests)); |
| 1594 | ring->bd_base = NULL; |
| 1595 | ring->bd_dma_base = 0; |
| 1596 | ring->cmpl_base = NULL; |
| 1597 | ring->cmpl_dma_base = 0; |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1598 | atomic_set(&ring->msg_send_count, 0); |
| 1599 | atomic_set(&ring->msg_cmpl_count, 0); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1600 | spin_lock_init(&ring->lock); |
Anup Patel | 1f7466c | 2017-08-01 16:05:53 +0530 | [diff] [blame^] | 1601 | bitmap_zero(ring->requests_bmap, RING_MAX_REQ_COUNT); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1602 | ring->last_pending_msg = NULL; |
| 1603 | ring->cmpl_read_offset = 0; |
| 1604 | } |
| 1605 | |
| 1606 | /* FlexRM is capable of 40-bit physical addresses only */ |
| 1607 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40)); |
| 1608 | if (ret) { |
| 1609 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); |
| 1610 | if (ret) |
| 1611 | goto fail; |
| 1612 | } |
| 1613 | |
| 1614 | /* Create DMA pool for ring BD memory */ |
| 1615 | mbox->bd_pool = dma_pool_create("bd", dev, RING_BD_SIZE, |
| 1616 | 1 << RING_BD_ALIGN_ORDER, 0); |
| 1617 | if (!mbox->bd_pool) { |
| 1618 | ret = -ENOMEM; |
| 1619 | goto fail; |
| 1620 | } |
| 1621 | |
| 1622 | /* Create DMA pool for ring completion memory */ |
| 1623 | mbox->cmpl_pool = dma_pool_create("cmpl", dev, RING_CMPL_SIZE, |
| 1624 | 1 << RING_CMPL_ALIGN_ORDER, 0); |
| 1625 | if (!mbox->cmpl_pool) { |
| 1626 | ret = -ENOMEM; |
| 1627 | goto fail_destroy_bd_pool; |
| 1628 | } |
| 1629 | |
| 1630 | /* Allocate platform MSIs for each ring */ |
| 1631 | ret = platform_msi_domain_alloc_irqs(dev, mbox->num_rings, |
| 1632 | flexrm_mbox_msi_write); |
| 1633 | if (ret) |
| 1634 | goto fail_destroy_cmpl_pool; |
| 1635 | |
| 1636 | /* Save alloced IRQ numbers for each ring */ |
| 1637 | for_each_msi_entry(desc, dev) { |
| 1638 | ring = &mbox->rings[desc->platform.msi_index]; |
| 1639 | ring->irq = desc->irq; |
| 1640 | } |
| 1641 | |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1642 | /* Check availability of debugfs */ |
| 1643 | if (!debugfs_initialized()) |
| 1644 | goto skip_debugfs; |
| 1645 | |
| 1646 | /* Create debugfs root entry */ |
| 1647 | mbox->root = debugfs_create_dir(dev_name(mbox->dev), NULL); |
| 1648 | if (IS_ERR_OR_NULL(mbox->root)) { |
| 1649 | ret = PTR_ERR_OR_ZERO(mbox->root); |
| 1650 | goto fail_free_msis; |
| 1651 | } |
| 1652 | |
| 1653 | /* Create debugfs config entry */ |
| 1654 | mbox->config = debugfs_create_devm_seqfile(mbox->dev, |
| 1655 | "config", mbox->root, |
| 1656 | flexrm_debugfs_conf_show); |
| 1657 | if (IS_ERR_OR_NULL(mbox->config)) { |
| 1658 | ret = PTR_ERR_OR_ZERO(mbox->config); |
| 1659 | goto fail_free_debugfs_root; |
| 1660 | } |
| 1661 | |
| 1662 | /* Create debugfs stats entry */ |
| 1663 | mbox->stats = debugfs_create_devm_seqfile(mbox->dev, |
| 1664 | "stats", mbox->root, |
| 1665 | flexrm_debugfs_stats_show); |
| 1666 | if (IS_ERR_OR_NULL(mbox->stats)) { |
| 1667 | ret = PTR_ERR_OR_ZERO(mbox->stats); |
| 1668 | goto fail_free_debugfs_root; |
| 1669 | } |
| 1670 | skip_debugfs: |
| 1671 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1672 | /* Initialize mailbox controller */ |
| 1673 | mbox->controller.txdone_irq = false; |
| 1674 | mbox->controller.txdone_poll = true; |
| 1675 | mbox->controller.txpoll_period = 1; |
| 1676 | mbox->controller.ops = &flexrm_mbox_chan_ops; |
| 1677 | mbox->controller.dev = dev; |
| 1678 | mbox->controller.num_chans = mbox->num_rings; |
| 1679 | mbox->controller.of_xlate = flexrm_mbox_of_xlate; |
| 1680 | mbox->controller.chans = devm_kcalloc(dev, mbox->num_rings, |
| 1681 | sizeof(*mbox->controller.chans), GFP_KERNEL); |
| 1682 | if (!mbox->controller.chans) { |
| 1683 | ret = -ENOMEM; |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1684 | goto fail_free_debugfs_root; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1685 | } |
| 1686 | for (index = 0; index < mbox->num_rings; index++) |
| 1687 | mbox->controller.chans[index].con_priv = &mbox->rings[index]; |
| 1688 | |
| 1689 | /* Register mailbox controller */ |
| 1690 | ret = mbox_controller_register(&mbox->controller); |
| 1691 | if (ret) |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1692 | goto fail_free_debugfs_root; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1693 | |
| 1694 | dev_info(dev, "registered flexrm mailbox with %d channels\n", |
| 1695 | mbox->controller.num_chans); |
| 1696 | |
| 1697 | return 0; |
| 1698 | |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1699 | fail_free_debugfs_root: |
| 1700 | debugfs_remove_recursive(mbox->root); |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1701 | fail_free_msis: |
| 1702 | platform_msi_domain_free_irqs(dev); |
| 1703 | fail_destroy_cmpl_pool: |
| 1704 | dma_pool_destroy(mbox->cmpl_pool); |
| 1705 | fail_destroy_bd_pool: |
| 1706 | dma_pool_destroy(mbox->bd_pool); |
| 1707 | fail: |
| 1708 | return ret; |
| 1709 | } |
| 1710 | |
| 1711 | static int flexrm_mbox_remove(struct platform_device *pdev) |
| 1712 | { |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1713 | struct device *dev = &pdev->dev; |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1714 | struct flexrm_mbox *mbox = platform_get_drvdata(pdev); |
| 1715 | |
| 1716 | mbox_controller_unregister(&mbox->controller); |
| 1717 | |
Anup Patel | acf7e50 | 2017-08-01 16:05:51 +0530 | [diff] [blame] | 1718 | debugfs_remove_recursive(mbox->root); |
| 1719 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1720 | platform_msi_domain_free_irqs(dev); |
| 1721 | |
| 1722 | dma_pool_destroy(mbox->cmpl_pool); |
| 1723 | dma_pool_destroy(mbox->bd_pool); |
| 1724 | |
Anup Patel | dbc049e | 2017-03-15 12:10:00 +0530 | [diff] [blame] | 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | static const struct of_device_id flexrm_mbox_of_match[] = { |
| 1729 | { .compatible = "brcm,iproc-flexrm-mbox", }, |
| 1730 | {}, |
| 1731 | }; |
| 1732 | MODULE_DEVICE_TABLE(of, flexrm_mbox_of_match); |
| 1733 | |
| 1734 | static struct platform_driver flexrm_mbox_driver = { |
| 1735 | .driver = { |
| 1736 | .name = "brcm-flexrm-mbox", |
| 1737 | .of_match_table = flexrm_mbox_of_match, |
| 1738 | }, |
| 1739 | .probe = flexrm_mbox_probe, |
| 1740 | .remove = flexrm_mbox_remove, |
| 1741 | }; |
| 1742 | module_platform_driver(flexrm_mbox_driver); |
| 1743 | |
| 1744 | MODULE_AUTHOR("Anup Patel <anup.patel@broadcom.com>"); |
| 1745 | MODULE_DESCRIPTION("Broadcom FlexRM mailbox driver"); |
| 1746 | MODULE_LICENSE("GPL v2"); |