|
| 1 | +/* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | +#include <linux/init.h> |
| 3 | +#include <linux/module.h> |
| 4 | +#include <linux/types.h> |
| 5 | +#include <asm/io.h> |
| 6 | +#include <linux/export.h> |
| 7 | +#include <linux/slab.h> |
| 8 | +#include <linux/platform_device.h> |
| 9 | +#include <linux/fs.h> |
| 10 | +#include <linux/rwsem.h> |
| 11 | +#include "kpc_dma_driver.h" |
| 12 | + |
| 13 | +/********** IRQ Handlers **********/ |
| 14 | +static |
| 15 | +irqreturn_t ndd_irq_handler(int irq, void *dev_id) |
| 16 | +{ |
| 17 | + struct kpc_dma_device *ldev = (struct kpc_dma_device*)dev_id; |
| 18 | + |
| 19 | + if ((GetEngineControl(ldev) & ENG_CTL_IRQ_ACTIVE) || (ldev->desc_completed->MyDMAAddr != GetEngineCompletePtr(ldev))) |
| 20 | + schedule_work(&ldev->irq_work); |
| 21 | + |
| 22 | + return IRQ_HANDLED; |
| 23 | +} |
| 24 | + |
| 25 | +static |
| 26 | +void ndd_irq_worker(struct work_struct *ws) |
| 27 | +{ |
| 28 | + struct kpc_dma_descriptor *cur; |
| 29 | + struct kpc_dma_device *eng = container_of(ws, struct kpc_dma_device, irq_work); |
| 30 | + lock_engine(eng); |
| 31 | + |
| 32 | + if (GetEngineCompletePtr(eng) == 0) |
| 33 | + goto out; |
| 34 | + |
| 35 | + if (eng->desc_completed->MyDMAAddr == GetEngineCompletePtr(eng)) |
| 36 | + goto out; |
| 37 | + |
| 38 | + cur = eng->desc_completed; |
| 39 | + do { |
| 40 | + cur = cur->Next; |
| 41 | + dev_dbg(&eng->pldev->dev, "Handling completed descriptor %p (acd = %p)\n", cur, cur->acd); |
| 42 | + BUG_ON(cur == eng->desc_next); // Ordering failure. |
| 43 | + |
| 44 | + if (cur->DescControlFlags & DMA_DESC_CTL_SOP){ |
| 45 | + eng->accumulated_bytes = 0; |
| 46 | + eng->accumulated_flags = 0; |
| 47 | + } |
| 48 | + |
| 49 | + eng->accumulated_bytes += cur->DescByteCount; |
| 50 | + if (cur->DescStatusFlags & DMA_DESC_STS_ERROR) |
| 51 | + eng->accumulated_flags |= ACD_FLAG_ENG_ACCUM_ERROR; |
| 52 | + |
| 53 | + if (cur->DescStatusFlags & DMA_DESC_STS_SHORT) |
| 54 | + eng->accumulated_flags |= ACD_FLAG_ENG_ACCUM_SHORT; |
| 55 | + |
| 56 | + if (cur->DescControlFlags & DMA_DESC_CTL_EOP){ |
| 57 | + if (cur->acd) |
| 58 | + transfer_complete_cb(cur->acd, eng->accumulated_bytes, eng->accumulated_flags | ACD_FLAG_DONE); |
| 59 | + } |
| 60 | + |
| 61 | + eng->desc_completed = cur; |
| 62 | + } while (cur->MyDMAAddr != GetEngineCompletePtr(eng)); |
| 63 | + |
| 64 | + out: |
| 65 | + SetClearEngineControl(eng, ENG_CTL_IRQ_ACTIVE, 0); |
| 66 | + |
| 67 | + unlock_engine(eng); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +/********** DMA Engine Init/Teardown **********/ |
| 72 | +void start_dma_engine(struct kpc_dma_device *eng) |
| 73 | +{ |
| 74 | + eng->desc_next = eng->desc_pool_first; |
| 75 | + eng->desc_completed = eng->desc_pool_last; |
| 76 | + |
| 77 | + // Setup the engine pointer registers |
| 78 | + SetEngineNextPtr(eng, eng->desc_pool_first); |
| 79 | + SetEngineSWPtr(eng, eng->desc_pool_first); |
| 80 | + ClearEngineCompletePtr(eng); |
| 81 | + |
| 82 | + WriteEngineControl(eng, ENG_CTL_DMA_ENABLE | ENG_CTL_IRQ_ENABLE); |
| 83 | +} |
| 84 | + |
| 85 | +int setup_dma_engine(struct kpc_dma_device *eng, u32 desc_cnt) |
| 86 | +{ |
| 87 | + u32 caps; |
| 88 | + struct kpc_dma_descriptor * cur; |
| 89 | + struct kpc_dma_descriptor * next; |
| 90 | + dma_addr_t next_handle; |
| 91 | + dma_addr_t head_handle; |
| 92 | + unsigned int i; |
| 93 | + int rv; |
| 94 | + dev_dbg(&eng->pldev->dev, "Setting up DMA engine [%p]\n", eng); |
| 95 | + |
| 96 | + caps = GetEngineCapabilities(eng); |
| 97 | + |
| 98 | + if (WARN(!(caps & ENG_CAP_PRESENT), "setup_dma_engine() called for DMA Engine at %p which isn't present in hardware!\n", eng)) |
| 99 | + return -ENXIO; |
| 100 | + |
| 101 | + if (caps & ENG_CAP_DIRECTION){ |
| 102 | + eng->dir = DMA_FROM_DEVICE; |
| 103 | + } else { |
| 104 | + eng->dir = DMA_TO_DEVICE; |
| 105 | + } |
| 106 | + |
| 107 | + eng->desc_pool_cnt = desc_cnt; |
| 108 | + eng->desc_pool = dma_pool_create("KPC DMA Descriptors", &eng->pldev->dev, sizeof(struct kpc_dma_descriptor), DMA_DESC_ALIGNMENT, 4096); |
| 109 | + |
| 110 | + eng->desc_pool_first = dma_pool_alloc(eng->desc_pool, GFP_KERNEL | GFP_DMA, &head_handle); |
| 111 | + if (!eng->desc_pool_first){ |
| 112 | + dev_err(&eng->pldev->dev, "setup_dma_engine: couldn't allocate desc_pool_first!\n"); |
| 113 | + dma_pool_destroy(eng->desc_pool); |
| 114 | + return -ENOMEM; |
| 115 | + } |
| 116 | + |
| 117 | + eng->desc_pool_first->MyDMAAddr = head_handle; |
| 118 | + clear_desc(eng->desc_pool_first); |
| 119 | + |
| 120 | + cur = eng->desc_pool_first; |
| 121 | + for (i = 1 ; i < eng->desc_pool_cnt ; i++){ |
| 122 | + next = dma_pool_alloc(eng->desc_pool, GFP_KERNEL | GFP_DMA, &next_handle); |
| 123 | + if (next == NULL) |
| 124 | + goto done_alloc; |
| 125 | + |
| 126 | + clear_desc(next); |
| 127 | + next->MyDMAAddr = next_handle; |
| 128 | + |
| 129 | + cur->DescNextDescPtr = next_handle; |
| 130 | + cur->Next = next; |
| 131 | + cur = next; |
| 132 | + } |
| 133 | + |
| 134 | + done_alloc: |
| 135 | + // Link the last descriptor back to the first, so it's a circular linked list |
| 136 | + cur->Next = eng->desc_pool_first; |
| 137 | + cur->DescNextDescPtr = eng->desc_pool_first->MyDMAAddr; |
| 138 | + |
| 139 | + eng->desc_pool_last = cur; |
| 140 | + eng->desc_completed = eng->desc_pool_last; |
| 141 | + |
| 142 | + // Setup work queue |
| 143 | + INIT_WORK(&eng->irq_work, ndd_irq_worker); |
| 144 | + |
| 145 | + // Grab IRQ line |
| 146 | + rv = request_irq(eng->irq, ndd_irq_handler, IRQF_SHARED, KP_DRIVER_NAME_DMA_CONTROLLER, eng); |
| 147 | + if (rv){ |
| 148 | + dev_err(&eng->pldev->dev, "setup_dma_engine: failed to request_irq: %d\n", rv); |
| 149 | + return rv; |
| 150 | + } |
| 151 | + |
| 152 | + // Turn on the engine! |
| 153 | + start_dma_engine(eng); |
| 154 | + unlock_engine(eng); |
| 155 | + |
| 156 | + return 0; |
| 157 | +} |
| 158 | + |
| 159 | +void stop_dma_engine(struct kpc_dma_device *eng) |
| 160 | +{ |
| 161 | + unsigned long timeout; |
| 162 | + dev_dbg(&eng->pldev->dev, "Destroying DMA engine [%p]\n", eng); |
| 163 | + |
| 164 | + // Disable the descriptor engine |
| 165 | + WriteEngineControl(eng, 0); |
| 166 | + |
| 167 | + // Wait for descriptor engine to finish current operaion |
| 168 | + timeout = jiffies + (HZ / 2); |
| 169 | + while (GetEngineControl(eng) & ENG_CTL_DMA_RUNNING){ |
| 170 | + if (time_after(jiffies, timeout)){ |
| 171 | + dev_crit(&eng->pldev->dev, "DMA_RUNNING still asserted!\n"); |
| 172 | + break; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Request a reset |
| 177 | + WriteEngineControl(eng, ENG_CTL_DMA_RESET_REQUEST); |
| 178 | + |
| 179 | + // Wait for reset request to be processed |
| 180 | + timeout = jiffies + (HZ / 2); |
| 181 | + while (GetEngineControl(eng) & (ENG_CTL_DMA_RUNNING | ENG_CTL_DMA_RESET_REQUEST)){ |
| 182 | + if (time_after(jiffies, timeout)){ |
| 183 | + dev_crit(&eng->pldev->dev, "ENG_CTL_DMA_RESET_REQUEST still asserted!\n"); |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // Request a reset |
| 189 | + WriteEngineControl(eng, ENG_CTL_DMA_RESET); |
| 190 | + |
| 191 | + // And wait for reset to complete |
| 192 | + timeout = jiffies + (HZ / 2); |
| 193 | + while (GetEngineControl(eng) & ENG_CTL_DMA_RESET){ |
| 194 | + if (time_after(jiffies, timeout)){ |
| 195 | + dev_crit(&eng->pldev->dev, "DMA_RESET still asserted!\n"); |
| 196 | + break; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + // Clear any persistent bits just to make sure there is no residue from the reset |
| 201 | + SetClearEngineControl(eng, (ENG_CTL_IRQ_ACTIVE | ENG_CTL_DESC_COMPLETE | ENG_CTL_DESC_ALIGN_ERR | ENG_CTL_DESC_FETCH_ERR | ENG_CTL_SW_ABORT_ERR | ENG_CTL_DESC_CHAIN_END | ENG_CTL_DMA_WAITING_PERSIST), 0); |
| 202 | + |
| 203 | + // Reset performance counters |
| 204 | + |
| 205 | + // Completely disable the engine |
| 206 | + WriteEngineControl(eng, 0); |
| 207 | +} |
| 208 | + |
| 209 | +void destroy_dma_engine(struct kpc_dma_device *eng) |
| 210 | +{ |
| 211 | + struct kpc_dma_descriptor * cur; |
| 212 | + dma_addr_t cur_handle; |
| 213 | + unsigned int i; |
| 214 | + |
| 215 | + stop_dma_engine(eng); |
| 216 | + |
| 217 | + cur = eng->desc_pool_first; |
| 218 | + cur_handle = eng->desc_pool_first->MyDMAAddr; |
| 219 | + |
| 220 | + for (i = 0 ; i < eng->desc_pool_cnt ; i++){ |
| 221 | + struct kpc_dma_descriptor *next = cur->Next; |
| 222 | + dma_addr_t next_handle = cur->DescNextDescPtr; |
| 223 | + dma_pool_free(eng->desc_pool, cur, cur_handle); |
| 224 | + cur_handle = next_handle; |
| 225 | + cur = next; |
| 226 | + } |
| 227 | + |
| 228 | + dma_pool_destroy(eng->desc_pool); |
| 229 | + |
| 230 | + free_irq(eng->irq, eng); |
| 231 | +} |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | +/********** Helper Functions **********/ |
| 236 | +int count_descriptors_available(struct kpc_dma_device *eng) |
| 237 | +{ |
| 238 | + u32 count = 0; |
| 239 | + struct kpc_dma_descriptor *cur = eng->desc_next; |
| 240 | + while (cur != eng->desc_completed){ |
| 241 | + BUG_ON(cur == NULL); |
| 242 | + count++; |
| 243 | + cur = cur->Next; |
| 244 | + } |
| 245 | + return count; |
| 246 | +} |
| 247 | + |
| 248 | +void clear_desc(struct kpc_dma_descriptor *desc) |
| 249 | +{ |
| 250 | + if (desc == NULL) |
| 251 | + return; |
| 252 | + desc->DescByteCount = 0; |
| 253 | + desc->DescStatusErrorFlags = 0; |
| 254 | + desc->DescStatusFlags = 0; |
| 255 | + desc->DescUserControlLS = 0; |
| 256 | + desc->DescUserControlMS = 0; |
| 257 | + desc->DescCardAddrLS = 0; |
| 258 | + desc->DescBufferByteCount = 0; |
| 259 | + desc->DescCardAddrMS = 0; |
| 260 | + desc->DescControlFlags = 0; |
| 261 | + desc->DescSystemAddrLS = 0; |
| 262 | + desc->DescSystemAddrMS = 0; |
| 263 | + desc->acd = NULL; |
| 264 | +} |
0 commit comments