Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Qualcomm Technologies HIDMA DMA engine low level code |
| 3 | * |
| 4 | * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 and |
| 8 | * only version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/dmaengine.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/highmem.h> |
| 21 | #include <linux/dma-mapping.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/atomic.h> |
| 24 | #include <linux/iopoll.h> |
| 25 | #include <linux/kfifo.h> |
| 26 | #include <linux/bitops.h> |
| 27 | |
| 28 | #include "hidma.h" |
| 29 | |
| 30 | #define HIDMA_EVRE_SIZE 16 /* each EVRE is 16 bytes */ |
| 31 | |
| 32 | #define HIDMA_TRCA_CTRLSTS_REG 0x000 |
| 33 | #define HIDMA_TRCA_RING_LOW_REG 0x008 |
| 34 | #define HIDMA_TRCA_RING_HIGH_REG 0x00C |
| 35 | #define HIDMA_TRCA_RING_LEN_REG 0x010 |
| 36 | #define HIDMA_TRCA_DOORBELL_REG 0x400 |
| 37 | |
| 38 | #define HIDMA_EVCA_CTRLSTS_REG 0x000 |
| 39 | #define HIDMA_EVCA_INTCTRL_REG 0x004 |
| 40 | #define HIDMA_EVCA_RING_LOW_REG 0x008 |
| 41 | #define HIDMA_EVCA_RING_HIGH_REG 0x00C |
| 42 | #define HIDMA_EVCA_RING_LEN_REG 0x010 |
| 43 | #define HIDMA_EVCA_WRITE_PTR_REG 0x020 |
| 44 | #define HIDMA_EVCA_DOORBELL_REG 0x400 |
| 45 | |
| 46 | #define HIDMA_EVCA_IRQ_STAT_REG 0x100 |
| 47 | #define HIDMA_EVCA_IRQ_CLR_REG 0x108 |
| 48 | #define HIDMA_EVCA_IRQ_EN_REG 0x110 |
| 49 | |
| 50 | #define HIDMA_EVRE_CFG_IDX 0 |
| 51 | |
| 52 | #define HIDMA_EVRE_ERRINFO_BIT_POS 24 |
| 53 | #define HIDMA_EVRE_CODE_BIT_POS 28 |
| 54 | |
| 55 | #define HIDMA_EVRE_ERRINFO_MASK GENMASK(3, 0) |
| 56 | #define HIDMA_EVRE_CODE_MASK GENMASK(3, 0) |
| 57 | |
| 58 | #define HIDMA_CH_CONTROL_MASK GENMASK(7, 0) |
| 59 | #define HIDMA_CH_STATE_MASK GENMASK(7, 0) |
| 60 | #define HIDMA_CH_STATE_BIT_POS 0x8 |
| 61 | |
| 62 | #define HIDMA_IRQ_EV_CH_EOB_IRQ_BIT_POS 0 |
| 63 | #define HIDMA_IRQ_EV_CH_WR_RESP_BIT_POS 1 |
| 64 | #define HIDMA_IRQ_TR_CH_TRE_RD_RSP_ER_BIT_POS 9 |
| 65 | #define HIDMA_IRQ_TR_CH_DATA_RD_ER_BIT_POS 10 |
| 66 | #define HIDMA_IRQ_TR_CH_DATA_WR_ER_BIT_POS 11 |
| 67 | #define HIDMA_IRQ_TR_CH_INVALID_TRE_BIT_POS 14 |
| 68 | |
| 69 | #define ENABLE_IRQS (BIT(HIDMA_IRQ_EV_CH_EOB_IRQ_BIT_POS) | \ |
| 70 | BIT(HIDMA_IRQ_EV_CH_WR_RESP_BIT_POS) | \ |
| 71 | BIT(HIDMA_IRQ_TR_CH_TRE_RD_RSP_ER_BIT_POS) | \ |
| 72 | BIT(HIDMA_IRQ_TR_CH_DATA_RD_ER_BIT_POS) | \ |
| 73 | BIT(HIDMA_IRQ_TR_CH_DATA_WR_ER_BIT_POS) | \ |
| 74 | BIT(HIDMA_IRQ_TR_CH_INVALID_TRE_BIT_POS)) |
| 75 | |
| 76 | #define HIDMA_INCREMENT_ITERATOR(iter, size, ring_size) \ |
| 77 | do { \ |
| 78 | iter += size; \ |
| 79 | if (iter >= ring_size) \ |
| 80 | iter -= ring_size; \ |
| 81 | } while (0) |
| 82 | |
| 83 | #define HIDMA_CH_STATE(val) \ |
| 84 | ((val >> HIDMA_CH_STATE_BIT_POS) & HIDMA_CH_STATE_MASK) |
| 85 | |
| 86 | #define HIDMA_ERR_INT_MASK \ |
| 87 | (BIT(HIDMA_IRQ_TR_CH_INVALID_TRE_BIT_POS) | \ |
| 88 | BIT(HIDMA_IRQ_TR_CH_TRE_RD_RSP_ER_BIT_POS) | \ |
| 89 | BIT(HIDMA_IRQ_EV_CH_WR_RESP_BIT_POS) | \ |
| 90 | BIT(HIDMA_IRQ_TR_CH_DATA_RD_ER_BIT_POS) | \ |
| 91 | BIT(HIDMA_IRQ_TR_CH_DATA_WR_ER_BIT_POS)) |
| 92 | |
| 93 | enum ch_command { |
| 94 | HIDMA_CH_DISABLE = 0, |
| 95 | HIDMA_CH_ENABLE = 1, |
| 96 | HIDMA_CH_SUSPEND = 2, |
| 97 | HIDMA_CH_RESET = 9, |
| 98 | }; |
| 99 | |
| 100 | enum ch_state { |
| 101 | HIDMA_CH_DISABLED = 0, |
| 102 | HIDMA_CH_ENABLED = 1, |
| 103 | HIDMA_CH_RUNNING = 2, |
| 104 | HIDMA_CH_SUSPENDED = 3, |
| 105 | HIDMA_CH_STOPPED = 4, |
| 106 | }; |
| 107 | |
| 108 | enum tre_type { |
| 109 | HIDMA_TRE_MEMCPY = 3, |
| 110 | }; |
| 111 | |
| 112 | enum err_code { |
| 113 | HIDMA_EVRE_STATUS_COMPLETE = 1, |
| 114 | HIDMA_EVRE_STATUS_ERROR = 4, |
| 115 | }; |
| 116 | |
| 117 | static int hidma_is_chan_enabled(int state) |
| 118 | { |
| 119 | switch (state) { |
| 120 | case HIDMA_CH_ENABLED: |
| 121 | case HIDMA_CH_RUNNING: |
| 122 | return true; |
| 123 | default: |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void hidma_ll_free(struct hidma_lldev *lldev, u32 tre_ch) |
| 129 | { |
| 130 | struct hidma_tre *tre; |
| 131 | |
| 132 | if (tre_ch >= lldev->nr_tres) { |
| 133 | dev_err(lldev->dev, "invalid TRE number in free:%d", tre_ch); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | tre = &lldev->trepool[tre_ch]; |
| 138 | if (atomic_read(&tre->allocated) != true) { |
| 139 | dev_err(lldev->dev, "trying to free an unused TRE:%d", tre_ch); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | atomic_set(&tre->allocated, 0); |
| 144 | } |
| 145 | |
| 146 | int hidma_ll_request(struct hidma_lldev *lldev, u32 sig, const char *dev_name, |
| 147 | void (*callback)(void *data), void *data, u32 *tre_ch) |
| 148 | { |
| 149 | unsigned int i; |
| 150 | struct hidma_tre *tre; |
| 151 | u32 *tre_local; |
| 152 | |
| 153 | if (!tre_ch || !lldev) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | /* need to have at least one empty spot in the queue */ |
| 157 | for (i = 0; i < lldev->nr_tres - 1; i++) { |
| 158 | if (atomic_add_unless(&lldev->trepool[i].allocated, 1, 1)) |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | if (i == (lldev->nr_tres - 1)) |
| 163 | return -ENOMEM; |
| 164 | |
| 165 | tre = &lldev->trepool[i]; |
| 166 | tre->dma_sig = sig; |
| 167 | tre->dev_name = dev_name; |
| 168 | tre->callback = callback; |
| 169 | tre->data = data; |
| 170 | tre->idx = i; |
| 171 | tre->status = 0; |
| 172 | tre->queued = 0; |
| 173 | tre->err_code = 0; |
| 174 | tre->err_info = 0; |
| 175 | tre->lldev = lldev; |
| 176 | tre_local = &tre->tre_local[0]; |
| 177 | tre_local[HIDMA_TRE_CFG_IDX] = HIDMA_TRE_MEMCPY; |
| 178 | tre_local[HIDMA_TRE_CFG_IDX] |= (lldev->chidx & 0xFF) << 8; |
| 179 | tre_local[HIDMA_TRE_CFG_IDX] |= BIT(16); /* set IEOB */ |
| 180 | *tre_ch = i; |
| 181 | if (callback) |
| 182 | callback(data); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Multiple TREs may be queued and waiting in the pending queue. |
| 188 | */ |
| 189 | static void hidma_ll_tre_complete(unsigned long arg) |
| 190 | { |
| 191 | struct hidma_lldev *lldev = (struct hidma_lldev *)arg; |
| 192 | struct hidma_tre *tre; |
| 193 | |
| 194 | while (kfifo_out(&lldev->handoff_fifo, &tre, 1)) { |
| 195 | /* call the user if it has been read by the hardware */ |
| 196 | if (tre->callback) |
| 197 | tre->callback(tre->data); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static int hidma_post_completed(struct hidma_lldev *lldev, int tre_iterator, |
| 202 | u8 err_info, u8 err_code) |
| 203 | { |
| 204 | struct hidma_tre *tre; |
| 205 | unsigned long flags; |
| 206 | |
| 207 | spin_lock_irqsave(&lldev->lock, flags); |
| 208 | tre = lldev->pending_tre_list[tre_iterator / HIDMA_TRE_SIZE]; |
| 209 | if (!tre) { |
| 210 | spin_unlock_irqrestore(&lldev->lock, flags); |
| 211 | dev_warn(lldev->dev, "tre_index [%d] and tre out of sync\n", |
| 212 | tre_iterator / HIDMA_TRE_SIZE); |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | lldev->pending_tre_list[tre->tre_index] = NULL; |
| 216 | |
| 217 | /* |
| 218 | * Keep track of pending TREs that SW is expecting to receive |
| 219 | * from HW. We got one now. Decrement our counter. |
| 220 | */ |
Sinan Kaya | bdcfddf | 2016-10-21 12:37:56 -0400 | [diff] [blame^] | 221 | if (atomic_dec_return(&lldev->pending_tre_count) < 0) { |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 222 | dev_warn(lldev->dev, "tre count mismatch on completion"); |
Sinan Kaya | bdcfddf | 2016-10-21 12:37:56 -0400 | [diff] [blame^] | 223 | atomic_set(&lldev->pending_tre_count, 0); |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | spin_unlock_irqrestore(&lldev->lock, flags); |
| 227 | |
| 228 | tre->err_info = err_info; |
| 229 | tre->err_code = err_code; |
| 230 | tre->queued = 0; |
| 231 | |
| 232 | kfifo_put(&lldev->handoff_fifo, tre); |
| 233 | tasklet_schedule(&lldev->task); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Called to handle the interrupt for the channel. |
| 240 | * Return a positive number if TRE or EVRE were consumed on this run. |
| 241 | * Return a positive number if there are pending TREs or EVREs. |
| 242 | * Return 0 if there is nothing to consume or no pending TREs/EVREs found. |
| 243 | */ |
| 244 | static int hidma_handle_tre_completion(struct hidma_lldev *lldev) |
| 245 | { |
| 246 | u32 evre_ring_size = lldev->evre_ring_size; |
| 247 | u32 tre_ring_size = lldev->tre_ring_size; |
| 248 | u32 err_info, err_code, evre_write_off; |
| 249 | u32 tre_iterator, evre_iterator; |
| 250 | u32 num_completed = 0; |
| 251 | |
| 252 | evre_write_off = readl_relaxed(lldev->evca + HIDMA_EVCA_WRITE_PTR_REG); |
| 253 | tre_iterator = lldev->tre_processed_off; |
| 254 | evre_iterator = lldev->evre_processed_off; |
| 255 | |
| 256 | if ((evre_write_off > evre_ring_size) || |
| 257 | (evre_write_off % HIDMA_EVRE_SIZE)) { |
| 258 | dev_err(lldev->dev, "HW reports invalid EVRE write offset\n"); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * By the time control reaches here the number of EVREs and TREs |
| 264 | * may not match. Only consume the ones that hardware told us. |
| 265 | */ |
| 266 | while ((evre_iterator != evre_write_off)) { |
| 267 | u32 *current_evre = lldev->evre_ring + evre_iterator; |
| 268 | u32 cfg; |
| 269 | |
| 270 | cfg = current_evre[HIDMA_EVRE_CFG_IDX]; |
| 271 | err_info = cfg >> HIDMA_EVRE_ERRINFO_BIT_POS; |
| 272 | err_info &= HIDMA_EVRE_ERRINFO_MASK; |
| 273 | err_code = |
| 274 | (cfg >> HIDMA_EVRE_CODE_BIT_POS) & HIDMA_EVRE_CODE_MASK; |
| 275 | |
| 276 | if (hidma_post_completed(lldev, tre_iterator, err_info, |
| 277 | err_code)) |
| 278 | break; |
| 279 | |
| 280 | HIDMA_INCREMENT_ITERATOR(tre_iterator, HIDMA_TRE_SIZE, |
| 281 | tre_ring_size); |
| 282 | HIDMA_INCREMENT_ITERATOR(evre_iterator, HIDMA_EVRE_SIZE, |
| 283 | evre_ring_size); |
| 284 | |
| 285 | /* |
| 286 | * Read the new event descriptor written by the HW. |
| 287 | * As we are processing the delivered events, other events |
| 288 | * get queued to the SW for processing. |
| 289 | */ |
| 290 | evre_write_off = |
| 291 | readl_relaxed(lldev->evca + HIDMA_EVCA_WRITE_PTR_REG); |
| 292 | num_completed++; |
Sinan Kaya | fc73796 | 2016-10-07 01:25:14 -0400 | [diff] [blame] | 293 | |
| 294 | /* |
| 295 | * An error interrupt might have arrived while we are processing |
| 296 | * the completed interrupt. |
| 297 | */ |
| 298 | if (!hidma_ll_isenabled(lldev)) |
| 299 | break; |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | if (num_completed) { |
| 303 | u32 evre_read_off = (lldev->evre_processed_off + |
| 304 | HIDMA_EVRE_SIZE * num_completed); |
| 305 | u32 tre_read_off = (lldev->tre_processed_off + |
| 306 | HIDMA_TRE_SIZE * num_completed); |
| 307 | |
| 308 | evre_read_off = evre_read_off % evre_ring_size; |
| 309 | tre_read_off = tre_read_off % tre_ring_size; |
| 310 | |
| 311 | writel(evre_read_off, lldev->evca + HIDMA_EVCA_DOORBELL_REG); |
| 312 | |
| 313 | /* record the last processed tre offset */ |
| 314 | lldev->tre_processed_off = tre_read_off; |
| 315 | lldev->evre_processed_off = evre_read_off; |
| 316 | } |
| 317 | |
| 318 | return num_completed; |
| 319 | } |
| 320 | |
| 321 | void hidma_cleanup_pending_tre(struct hidma_lldev *lldev, u8 err_info, |
| 322 | u8 err_code) |
| 323 | { |
| 324 | u32 tre_iterator; |
| 325 | u32 tre_ring_size = lldev->tre_ring_size; |
| 326 | int num_completed = 0; |
| 327 | u32 tre_read_off; |
| 328 | |
| 329 | tre_iterator = lldev->tre_processed_off; |
Sinan Kaya | bdcfddf | 2016-10-21 12:37:56 -0400 | [diff] [blame^] | 330 | while (atomic_read(&lldev->pending_tre_count)) { |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 331 | if (hidma_post_completed(lldev, tre_iterator, err_info, |
| 332 | err_code)) |
| 333 | break; |
| 334 | HIDMA_INCREMENT_ITERATOR(tre_iterator, HIDMA_TRE_SIZE, |
| 335 | tre_ring_size); |
| 336 | num_completed++; |
| 337 | } |
| 338 | tre_read_off = (lldev->tre_processed_off + |
| 339 | HIDMA_TRE_SIZE * num_completed); |
| 340 | |
| 341 | tre_read_off = tre_read_off % tre_ring_size; |
| 342 | |
| 343 | /* record the last processed tre offset */ |
| 344 | lldev->tre_processed_off = tre_read_off; |
| 345 | } |
| 346 | |
| 347 | static int hidma_ll_reset(struct hidma_lldev *lldev) |
| 348 | { |
| 349 | u32 val; |
| 350 | int ret; |
| 351 | |
| 352 | val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG); |
| 353 | val &= ~(HIDMA_CH_CONTROL_MASK << 16); |
| 354 | val |= HIDMA_CH_RESET << 16; |
| 355 | writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG); |
| 356 | |
| 357 | /* |
| 358 | * Delay 10ms after reset to allow DMA logic to quiesce. |
| 359 | * Do a polled read up to 1ms and 10ms maximum. |
| 360 | */ |
| 361 | ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val, |
| 362 | HIDMA_CH_STATE(val) == HIDMA_CH_DISABLED, |
| 363 | 1000, 10000); |
| 364 | if (ret) { |
| 365 | dev_err(lldev->dev, "transfer channel did not reset\n"); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG); |
| 370 | val &= ~(HIDMA_CH_CONTROL_MASK << 16); |
| 371 | val |= HIDMA_CH_RESET << 16; |
| 372 | writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG); |
| 373 | |
| 374 | /* |
| 375 | * Delay 10ms after reset to allow DMA logic to quiesce. |
| 376 | * Do a polled read up to 1ms and 10ms maximum. |
| 377 | */ |
| 378 | ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val, |
| 379 | HIDMA_CH_STATE(val) == HIDMA_CH_DISABLED, |
| 380 | 1000, 10000); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | |
| 384 | lldev->trch_state = HIDMA_CH_DISABLED; |
| 385 | lldev->evch_state = HIDMA_CH_DISABLED; |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /* |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 390 | * The interrupt handler for HIDMA will try to consume as many pending |
| 391 | * EVRE from the event queue as possible. Each EVRE has an associated |
| 392 | * TRE that holds the user interface parameters. EVRE reports the |
| 393 | * result of the transaction. Hardware guarantees ordering between EVREs |
| 394 | * and TREs. We use last processed offset to figure out which TRE is |
| 395 | * associated with which EVRE. If two TREs are consumed by HW, the EVREs |
| 396 | * are in order in the event ring. |
| 397 | * |
| 398 | * This handler will do a one pass for consuming EVREs. Other EVREs may |
| 399 | * be delivered while we are working. It will try to consume incoming |
| 400 | * EVREs one more time and return. |
| 401 | * |
| 402 | * For unprocessed EVREs, hardware will trigger another interrupt until |
| 403 | * all the interrupt bits are cleared. |
| 404 | * |
| 405 | * Hardware guarantees that by the time interrupt is observed, all data |
| 406 | * transactions in flight are delivered to their respective places and |
| 407 | * are visible to the CPU. |
| 408 | * |
| 409 | * On demand paging for IOMMU is only supported for PCIe via PRI |
| 410 | * (Page Request Interface) not for HIDMA. All other hardware instances |
| 411 | * including HIDMA work on pinned DMA addresses. |
| 412 | * |
| 413 | * HIDMA is not aware of IOMMU presence since it follows the DMA API. All |
| 414 | * IOMMU latency will be built into the data movement time. By the time |
| 415 | * interrupt happens, IOMMU lookups + data movement has already taken place. |
| 416 | * |
| 417 | * While the first read in a typical PCI endpoint ISR flushes all outstanding |
| 418 | * requests traditionally to the destination, this concept does not apply |
| 419 | * here for this HW. |
| 420 | */ |
| 421 | irqreturn_t hidma_ll_inthandler(int chirq, void *arg) |
| 422 | { |
| 423 | struct hidma_lldev *lldev = arg; |
| 424 | u32 status; |
| 425 | u32 enable; |
| 426 | u32 cause; |
| 427 | |
| 428 | /* |
| 429 | * Fine tuned for this HW... |
| 430 | * |
| 431 | * This ISR has been designed for this particular hardware. Relaxed |
| 432 | * read and write accessors are used for performance reasons due to |
| 433 | * interrupt delivery guarantees. Do not copy this code blindly and |
| 434 | * expect that to work. |
| 435 | */ |
| 436 | status = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG); |
| 437 | enable = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_EN_REG); |
| 438 | cause = status & enable; |
| 439 | |
| 440 | while (cause) { |
| 441 | if (cause & HIDMA_ERR_INT_MASK) { |
Sinan Kaya | 793ae66 | 2016-08-31 11:10:29 -0400 | [diff] [blame] | 442 | dev_err(lldev->dev, "error 0x%x, disabling...\n", |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 443 | cause); |
| 444 | |
| 445 | /* Clear out pending interrupts */ |
| 446 | writel(cause, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG); |
| 447 | |
Sinan Kaya | 793ae66 | 2016-08-31 11:10:29 -0400 | [diff] [blame] | 448 | /* No further submissions. */ |
| 449 | hidma_ll_disable(lldev); |
| 450 | |
| 451 | /* Driver completes the txn and intimates the client.*/ |
| 452 | hidma_cleanup_pending_tre(lldev, 0xFF, |
| 453 | HIDMA_EVRE_STATUS_ERROR); |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 454 | goto out; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Try to consume as many EVREs as possible. |
| 459 | */ |
| 460 | hidma_handle_tre_completion(lldev); |
| 461 | |
| 462 | /* We consumed TREs or there are pending TREs or EVREs. */ |
| 463 | writel_relaxed(cause, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG); |
| 464 | |
| 465 | /* |
| 466 | * Another interrupt might have arrived while we are |
| 467 | * processing this one. Read the new cause. |
| 468 | */ |
| 469 | status = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG); |
| 470 | enable = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_EN_REG); |
| 471 | cause = status & enable; |
| 472 | } |
| 473 | |
| 474 | out: |
| 475 | return IRQ_HANDLED; |
| 476 | } |
| 477 | |
| 478 | int hidma_ll_enable(struct hidma_lldev *lldev) |
| 479 | { |
| 480 | u32 val; |
| 481 | int ret; |
| 482 | |
| 483 | val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG); |
| 484 | val &= ~(HIDMA_CH_CONTROL_MASK << 16); |
| 485 | val |= HIDMA_CH_ENABLE << 16; |
| 486 | writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG); |
| 487 | |
| 488 | ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val, |
| 489 | hidma_is_chan_enabled(HIDMA_CH_STATE(val)), |
| 490 | 1000, 10000); |
| 491 | if (ret) { |
| 492 | dev_err(lldev->dev, "event channel did not get enabled\n"); |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG); |
| 497 | val &= ~(HIDMA_CH_CONTROL_MASK << 16); |
| 498 | val |= HIDMA_CH_ENABLE << 16; |
| 499 | writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG); |
| 500 | |
| 501 | ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val, |
| 502 | hidma_is_chan_enabled(HIDMA_CH_STATE(val)), |
| 503 | 1000, 10000); |
| 504 | if (ret) { |
| 505 | dev_err(lldev->dev, "transfer channel did not get enabled\n"); |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | lldev->trch_state = HIDMA_CH_ENABLED; |
| 510 | lldev->evch_state = HIDMA_CH_ENABLED; |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | void hidma_ll_start(struct hidma_lldev *lldev) |
| 516 | { |
| 517 | unsigned long irqflags; |
| 518 | |
| 519 | spin_lock_irqsave(&lldev->lock, irqflags); |
| 520 | writel(lldev->tre_write_offset, lldev->trca + HIDMA_TRCA_DOORBELL_REG); |
| 521 | spin_unlock_irqrestore(&lldev->lock, irqflags); |
| 522 | } |
| 523 | |
| 524 | bool hidma_ll_isenabled(struct hidma_lldev *lldev) |
| 525 | { |
| 526 | u32 val; |
| 527 | |
| 528 | val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG); |
| 529 | lldev->trch_state = HIDMA_CH_STATE(val); |
| 530 | val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG); |
| 531 | lldev->evch_state = HIDMA_CH_STATE(val); |
| 532 | |
| 533 | /* both channels have to be enabled before calling this function */ |
| 534 | if (hidma_is_chan_enabled(lldev->trch_state) && |
| 535 | hidma_is_chan_enabled(lldev->evch_state)) |
| 536 | return true; |
| 537 | |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | void hidma_ll_queue_request(struct hidma_lldev *lldev, u32 tre_ch) |
| 542 | { |
| 543 | struct hidma_tre *tre; |
| 544 | unsigned long flags; |
| 545 | |
| 546 | tre = &lldev->trepool[tre_ch]; |
| 547 | |
| 548 | /* copy the TRE into its location in the TRE ring */ |
| 549 | spin_lock_irqsave(&lldev->lock, flags); |
| 550 | tre->tre_index = lldev->tre_write_offset / HIDMA_TRE_SIZE; |
| 551 | lldev->pending_tre_list[tre->tre_index] = tre; |
| 552 | memcpy(lldev->tre_ring + lldev->tre_write_offset, |
| 553 | &tre->tre_local[0], HIDMA_TRE_SIZE); |
| 554 | tre->err_code = 0; |
| 555 | tre->err_info = 0; |
| 556 | tre->queued = 1; |
Sinan Kaya | bdcfddf | 2016-10-21 12:37:56 -0400 | [diff] [blame^] | 557 | atomic_inc(&lldev->pending_tre_count); |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 558 | lldev->tre_write_offset = (lldev->tre_write_offset + HIDMA_TRE_SIZE) |
| 559 | % lldev->tre_ring_size; |
| 560 | spin_unlock_irqrestore(&lldev->lock, flags); |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Note that even though we stop this channel if there is a pending transaction |
| 565 | * in flight it will complete and follow the callback. This request will |
| 566 | * prevent further requests to be made. |
| 567 | */ |
| 568 | int hidma_ll_disable(struct hidma_lldev *lldev) |
| 569 | { |
| 570 | u32 val; |
| 571 | int ret; |
| 572 | |
Sinan Kaya | 7dcec75 | 2016-10-06 15:33:14 -0400 | [diff] [blame] | 573 | /* The channel needs to be in working state */ |
| 574 | if (!hidma_ll_isenabled(lldev)) |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 575 | return 0; |
| 576 | |
| 577 | val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG); |
| 578 | val &= ~(HIDMA_CH_CONTROL_MASK << 16); |
| 579 | val |= HIDMA_CH_SUSPEND << 16; |
| 580 | writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG); |
| 581 | |
| 582 | /* |
| 583 | * Start the wait right after the suspend is confirmed. |
| 584 | * Do a polled read up to 1ms and 10ms maximum. |
| 585 | */ |
| 586 | ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val, |
| 587 | HIDMA_CH_STATE(val) == HIDMA_CH_SUSPENDED, |
| 588 | 1000, 10000); |
| 589 | if (ret) |
| 590 | return ret; |
| 591 | |
| 592 | val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG); |
| 593 | val &= ~(HIDMA_CH_CONTROL_MASK << 16); |
| 594 | val |= HIDMA_CH_SUSPEND << 16; |
| 595 | writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG); |
| 596 | |
| 597 | /* |
| 598 | * Start the wait right after the suspend is confirmed |
| 599 | * Delay up to 10ms after reset to allow DMA logic to quiesce. |
| 600 | */ |
| 601 | ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val, |
| 602 | HIDMA_CH_STATE(val) == HIDMA_CH_SUSPENDED, |
| 603 | 1000, 10000); |
| 604 | if (ret) |
| 605 | return ret; |
| 606 | |
| 607 | lldev->trch_state = HIDMA_CH_SUSPENDED; |
| 608 | lldev->evch_state = HIDMA_CH_SUSPENDED; |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | void hidma_ll_set_transfer_params(struct hidma_lldev *lldev, u32 tre_ch, |
| 613 | dma_addr_t src, dma_addr_t dest, u32 len, |
| 614 | u32 flags) |
| 615 | { |
| 616 | struct hidma_tre *tre; |
| 617 | u32 *tre_local; |
| 618 | |
| 619 | if (tre_ch >= lldev->nr_tres) { |
| 620 | dev_err(lldev->dev, "invalid TRE number in transfer params:%d", |
| 621 | tre_ch); |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | tre = &lldev->trepool[tre_ch]; |
| 626 | if (atomic_read(&tre->allocated) != true) { |
| 627 | dev_err(lldev->dev, "trying to set params on an unused TRE:%d", |
| 628 | tre_ch); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | tre_local = &tre->tre_local[0]; |
| 633 | tre_local[HIDMA_TRE_LEN_IDX] = len; |
| 634 | tre_local[HIDMA_TRE_SRC_LOW_IDX] = lower_32_bits(src); |
| 635 | tre_local[HIDMA_TRE_SRC_HI_IDX] = upper_32_bits(src); |
| 636 | tre_local[HIDMA_TRE_DEST_LOW_IDX] = lower_32_bits(dest); |
| 637 | tre_local[HIDMA_TRE_DEST_HI_IDX] = upper_32_bits(dest); |
| 638 | tre->int_flags = flags; |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Called during initialization and after an error condition |
| 643 | * to restore hardware state. |
| 644 | */ |
| 645 | int hidma_ll_setup(struct hidma_lldev *lldev) |
| 646 | { |
| 647 | int rc; |
| 648 | u64 addr; |
| 649 | u32 val; |
| 650 | u32 nr_tres = lldev->nr_tres; |
| 651 | |
Sinan Kaya | bdcfddf | 2016-10-21 12:37:56 -0400 | [diff] [blame^] | 652 | atomic_set(&lldev->pending_tre_count, 0); |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 653 | lldev->tre_processed_off = 0; |
| 654 | lldev->evre_processed_off = 0; |
| 655 | lldev->tre_write_offset = 0; |
| 656 | |
| 657 | /* disable interrupts */ |
| 658 | writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG); |
| 659 | |
| 660 | /* clear all pending interrupts */ |
| 661 | val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG); |
| 662 | writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG); |
| 663 | |
| 664 | rc = hidma_ll_reset(lldev); |
| 665 | if (rc) |
| 666 | return rc; |
| 667 | |
| 668 | /* |
| 669 | * Clear all pending interrupts again. |
| 670 | * Otherwise, we observe reset complete interrupts. |
| 671 | */ |
| 672 | val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG); |
| 673 | writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG); |
| 674 | |
| 675 | /* disable interrupts again after reset */ |
| 676 | writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG); |
| 677 | |
| 678 | addr = lldev->tre_dma; |
| 679 | writel(lower_32_bits(addr), lldev->trca + HIDMA_TRCA_RING_LOW_REG); |
| 680 | writel(upper_32_bits(addr), lldev->trca + HIDMA_TRCA_RING_HIGH_REG); |
| 681 | writel(lldev->tre_ring_size, lldev->trca + HIDMA_TRCA_RING_LEN_REG); |
| 682 | |
| 683 | addr = lldev->evre_dma; |
| 684 | writel(lower_32_bits(addr), lldev->evca + HIDMA_EVCA_RING_LOW_REG); |
| 685 | writel(upper_32_bits(addr), lldev->evca + HIDMA_EVCA_RING_HIGH_REG); |
| 686 | writel(HIDMA_EVRE_SIZE * nr_tres, |
| 687 | lldev->evca + HIDMA_EVCA_RING_LEN_REG); |
| 688 | |
Sinan Kaya | d3eab50 | 2016-10-07 01:25:12 -0400 | [diff] [blame] | 689 | /* configure interrupts */ |
| 690 | hidma_ll_setup_irq(lldev, lldev->msi_support); |
| 691 | |
| 692 | rc = hidma_ll_enable(lldev); |
| 693 | if (rc) |
| 694 | return rc; |
| 695 | |
| 696 | return rc; |
| 697 | } |
| 698 | |
| 699 | void hidma_ll_setup_irq(struct hidma_lldev *lldev, bool msi) |
| 700 | { |
| 701 | u32 val; |
| 702 | |
| 703 | lldev->msi_support = msi; |
| 704 | |
| 705 | /* disable interrupts again after reset */ |
| 706 | writel(0, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG); |
| 707 | writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG); |
| 708 | |
| 709 | /* support IRQ by default */ |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 710 | val = readl(lldev->evca + HIDMA_EVCA_INTCTRL_REG); |
| 711 | val &= ~0xF; |
Sinan Kaya | d3eab50 | 2016-10-07 01:25:12 -0400 | [diff] [blame] | 712 | if (!lldev->msi_support) |
| 713 | val = val | 0x1; |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 714 | writel(val, lldev->evca + HIDMA_EVCA_INTCTRL_REG); |
| 715 | |
| 716 | /* clear all pending interrupts and enable them */ |
| 717 | writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG); |
| 718 | writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_EN_REG); |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | struct hidma_lldev *hidma_ll_init(struct device *dev, u32 nr_tres, |
| 722 | void __iomem *trca, void __iomem *evca, |
| 723 | u8 chidx) |
| 724 | { |
| 725 | u32 required_bytes; |
| 726 | struct hidma_lldev *lldev; |
| 727 | int rc; |
| 728 | size_t sz; |
| 729 | |
| 730 | if (!trca || !evca || !dev || !nr_tres) |
| 731 | return NULL; |
| 732 | |
| 733 | /* need at least four TREs */ |
| 734 | if (nr_tres < 4) |
| 735 | return NULL; |
| 736 | |
| 737 | /* need an extra space */ |
| 738 | nr_tres += 1; |
| 739 | |
| 740 | lldev = devm_kzalloc(dev, sizeof(struct hidma_lldev), GFP_KERNEL); |
| 741 | if (!lldev) |
| 742 | return NULL; |
| 743 | |
| 744 | lldev->evca = evca; |
| 745 | lldev->trca = trca; |
| 746 | lldev->dev = dev; |
| 747 | sz = sizeof(struct hidma_tre); |
| 748 | lldev->trepool = devm_kcalloc(lldev->dev, nr_tres, sz, GFP_KERNEL); |
| 749 | if (!lldev->trepool) |
| 750 | return NULL; |
| 751 | |
| 752 | required_bytes = sizeof(lldev->pending_tre_list[0]); |
| 753 | lldev->pending_tre_list = devm_kcalloc(dev, nr_tres, required_bytes, |
| 754 | GFP_KERNEL); |
| 755 | if (!lldev->pending_tre_list) |
| 756 | return NULL; |
| 757 | |
| 758 | sz = (HIDMA_TRE_SIZE + 1) * nr_tres; |
| 759 | lldev->tre_ring = dmam_alloc_coherent(dev, sz, &lldev->tre_dma, |
| 760 | GFP_KERNEL); |
| 761 | if (!lldev->tre_ring) |
| 762 | return NULL; |
| 763 | |
| 764 | memset(lldev->tre_ring, 0, (HIDMA_TRE_SIZE + 1) * nr_tres); |
| 765 | lldev->tre_ring_size = HIDMA_TRE_SIZE * nr_tres; |
| 766 | lldev->nr_tres = nr_tres; |
| 767 | |
| 768 | /* the TRE ring has to be TRE_SIZE aligned */ |
| 769 | if (!IS_ALIGNED(lldev->tre_dma, HIDMA_TRE_SIZE)) { |
| 770 | u8 tre_ring_shift; |
| 771 | |
| 772 | tre_ring_shift = lldev->tre_dma % HIDMA_TRE_SIZE; |
| 773 | tre_ring_shift = HIDMA_TRE_SIZE - tre_ring_shift; |
| 774 | lldev->tre_dma += tre_ring_shift; |
| 775 | lldev->tre_ring += tre_ring_shift; |
| 776 | } |
| 777 | |
| 778 | sz = (HIDMA_EVRE_SIZE + 1) * nr_tres; |
| 779 | lldev->evre_ring = dmam_alloc_coherent(dev, sz, &lldev->evre_dma, |
| 780 | GFP_KERNEL); |
| 781 | if (!lldev->evre_ring) |
| 782 | return NULL; |
| 783 | |
| 784 | memset(lldev->evre_ring, 0, (HIDMA_EVRE_SIZE + 1) * nr_tres); |
| 785 | lldev->evre_ring_size = HIDMA_EVRE_SIZE * nr_tres; |
| 786 | |
| 787 | /* the EVRE ring has to be EVRE_SIZE aligned */ |
| 788 | if (!IS_ALIGNED(lldev->evre_dma, HIDMA_EVRE_SIZE)) { |
| 789 | u8 evre_ring_shift; |
| 790 | |
| 791 | evre_ring_shift = lldev->evre_dma % HIDMA_EVRE_SIZE; |
| 792 | evre_ring_shift = HIDMA_EVRE_SIZE - evre_ring_shift; |
| 793 | lldev->evre_dma += evre_ring_shift; |
| 794 | lldev->evre_ring += evre_ring_shift; |
| 795 | } |
| 796 | lldev->nr_tres = nr_tres; |
| 797 | lldev->chidx = chidx; |
| 798 | |
| 799 | sz = nr_tres * sizeof(struct hidma_tre *); |
| 800 | rc = kfifo_alloc(&lldev->handoff_fifo, sz, GFP_KERNEL); |
| 801 | if (rc) |
| 802 | return NULL; |
| 803 | |
| 804 | rc = hidma_ll_setup(lldev); |
| 805 | if (rc) |
| 806 | return NULL; |
| 807 | |
| 808 | spin_lock_init(&lldev->lock); |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 809 | tasklet_init(&lldev->task, hidma_ll_tre_complete, (unsigned long)lldev); |
| 810 | lldev->initialized = 1; |
| 811 | writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_EN_REG); |
| 812 | return lldev; |
| 813 | } |
| 814 | |
| 815 | int hidma_ll_uninit(struct hidma_lldev *lldev) |
| 816 | { |
| 817 | u32 required_bytes; |
| 818 | int rc = 0; |
| 819 | u32 val; |
| 820 | |
| 821 | if (!lldev) |
| 822 | return -ENODEV; |
| 823 | |
| 824 | if (!lldev->initialized) |
| 825 | return 0; |
| 826 | |
| 827 | lldev->initialized = 0; |
| 828 | |
| 829 | required_bytes = sizeof(struct hidma_tre) * lldev->nr_tres; |
| 830 | tasklet_kill(&lldev->task); |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 831 | memset(lldev->trepool, 0, required_bytes); |
| 832 | lldev->trepool = NULL; |
Sinan Kaya | bdcfddf | 2016-10-21 12:37:56 -0400 | [diff] [blame^] | 833 | atomic_set(&lldev->pending_tre_count, 0); |
Sinan Kaya | d1615ca | 2016-05-01 00:25:26 -0400 | [diff] [blame] | 834 | lldev->tre_write_offset = 0; |
| 835 | |
| 836 | rc = hidma_ll_reset(lldev); |
| 837 | |
| 838 | /* |
| 839 | * Clear all pending interrupts again. |
| 840 | * Otherwise, we observe reset complete interrupts. |
| 841 | */ |
| 842 | val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG); |
| 843 | writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG); |
| 844 | writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG); |
| 845 | return rc; |
| 846 | } |
| 847 | |
| 848 | enum dma_status hidma_ll_status(struct hidma_lldev *lldev, u32 tre_ch) |
| 849 | { |
| 850 | enum dma_status ret = DMA_ERROR; |
| 851 | struct hidma_tre *tre; |
| 852 | unsigned long flags; |
| 853 | u8 err_code; |
| 854 | |
| 855 | spin_lock_irqsave(&lldev->lock, flags); |
| 856 | |
| 857 | tre = &lldev->trepool[tre_ch]; |
| 858 | err_code = tre->err_code; |
| 859 | |
| 860 | if (err_code & HIDMA_EVRE_STATUS_COMPLETE) |
| 861 | ret = DMA_COMPLETE; |
| 862 | else if (err_code & HIDMA_EVRE_STATUS_ERROR) |
| 863 | ret = DMA_ERROR; |
| 864 | else |
| 865 | ret = DMA_IN_PROGRESS; |
| 866 | spin_unlock_irqrestore(&lldev->lock, flags); |
| 867 | |
| 868 | return ret; |
| 869 | } |