Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2016-2019 HabanaLabs, Ltd. |
| 5 | * All Rights Reserved. |
| 6 | */ |
| 7 | |
| 8 | #include "habanalabs.h" |
| 9 | |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | |
| 12 | /** |
Lee Jones | 9eea2a4 | 2020-06-26 14:05:24 +0100 | [diff] [blame] | 13 | * struct hl_eqe_work - This structure is used to schedule work of EQ |
| 14 | * entry and armcp_reset event |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 15 | * |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 16 | * @eq_work: workqueue object to run when EQ entry is received |
| 17 | * @hdev: pointer to device structure |
| 18 | * @eq_entry: copy of the EQ entry |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 19 | */ |
| 20 | struct hl_eqe_work { |
| 21 | struct work_struct eq_work; |
| 22 | struct hl_device *hdev; |
| 23 | struct hl_eq_entry eq_entry; |
| 24 | }; |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 25 | |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 26 | /** |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 27 | * hl_cq_inc_ptr - increment ci or pi of cq |
| 28 | * |
| 29 | * @ptr: the current ci or pi value of the completion queue |
| 30 | * |
| 31 | * Increment ptr by 1. If it reaches the number of completion queue |
| 32 | * entries, set it to 0 |
| 33 | */ |
| 34 | inline u32 hl_cq_inc_ptr(u32 ptr) |
| 35 | { |
| 36 | ptr++; |
| 37 | if (unlikely(ptr == HL_CQ_LENGTH)) |
| 38 | ptr = 0; |
| 39 | return ptr; |
| 40 | } |
| 41 | |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 42 | /** |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 43 | * hl_eq_inc_ptr - increment ci of eq |
| 44 | * |
| 45 | * @ptr: the current ci value of the event queue |
| 46 | * |
| 47 | * Increment ptr by 1. If it reaches the number of event queue |
| 48 | * entries, set it to 0 |
| 49 | */ |
| 50 | inline u32 hl_eq_inc_ptr(u32 ptr) |
| 51 | { |
| 52 | ptr++; |
| 53 | if (unlikely(ptr == HL_EQ_LENGTH)) |
| 54 | ptr = 0; |
| 55 | return ptr; |
| 56 | } |
| 57 | |
| 58 | static void irq_handle_eqe(struct work_struct *work) |
| 59 | { |
| 60 | struct hl_eqe_work *eqe_work = container_of(work, struct hl_eqe_work, |
| 61 | eq_work); |
| 62 | struct hl_device *hdev = eqe_work->hdev; |
| 63 | |
| 64 | hdev->asic_funcs->handle_eqe(hdev, &eqe_work->eq_entry); |
| 65 | |
| 66 | kfree(eqe_work); |
| 67 | } |
| 68 | |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 69 | /** |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 70 | * hl_irq_handler_cq - irq handler for completion queue |
| 71 | * |
| 72 | * @irq: irq number |
| 73 | * @arg: pointer to completion queue structure |
| 74 | * |
| 75 | */ |
| 76 | irqreturn_t hl_irq_handler_cq(int irq, void *arg) |
| 77 | { |
| 78 | struct hl_cq *cq = arg; |
| 79 | struct hl_device *hdev = cq->hdev; |
| 80 | struct hl_hw_queue *queue; |
| 81 | struct hl_cs_job *job; |
| 82 | bool shadow_index_valid; |
| 83 | u16 shadow_index; |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 84 | struct hl_cq_entry *cq_entry, *cq_base; |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 85 | |
| 86 | if (hdev->disabled) { |
| 87 | dev_dbg(hdev->dev, |
| 88 | "Device disabled but received IRQ %d for CQ %d\n", |
| 89 | irq, cq->hw_queue_id); |
| 90 | return IRQ_HANDLED; |
| 91 | } |
| 92 | |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 93 | cq_base = (struct hl_cq_entry *) (uintptr_t) cq->kernel_address; |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 94 | |
| 95 | while (1) { |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 96 | bool entry_ready = ((le32_to_cpu(cq_base[cq->ci].data) & |
| 97 | CQ_ENTRY_READY_MASK) |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 98 | >> CQ_ENTRY_READY_SHIFT); |
| 99 | |
| 100 | if (!entry_ready) |
| 101 | break; |
| 102 | |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 103 | cq_entry = (struct hl_cq_entry *) &cq_base[cq->ci]; |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 104 | |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 105 | /* Make sure we read CQ entry contents after we've |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 106 | * checked the ownership bit. |
| 107 | */ |
| 108 | dma_rmb(); |
| 109 | |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 110 | shadow_index_valid = ((le32_to_cpu(cq_entry->data) & |
| 111 | CQ_ENTRY_SHADOW_INDEX_VALID_MASK) |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 112 | >> CQ_ENTRY_SHADOW_INDEX_VALID_SHIFT); |
| 113 | |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 114 | shadow_index = (u16) ((le32_to_cpu(cq_entry->data) & |
| 115 | CQ_ENTRY_SHADOW_INDEX_MASK) |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 116 | >> CQ_ENTRY_SHADOW_INDEX_SHIFT); |
| 117 | |
| 118 | queue = &hdev->kernel_queues[cq->hw_queue_id]; |
| 119 | |
| 120 | if ((shadow_index_valid) && (!hdev->disabled)) { |
| 121 | job = queue->shadow_queue[hl_pi_2_offset(shadow_index)]; |
| 122 | queue_work(hdev->cq_wq, &job->finish_work); |
| 123 | } |
| 124 | |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 125 | /* Update ci of the context's queue. There is no |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 126 | * need to protect it with spinlock because this update is |
| 127 | * done only inside IRQ and there is a different IRQ per |
| 128 | * queue |
| 129 | */ |
| 130 | queue->ci = hl_queue_inc_ptr(queue->ci); |
| 131 | |
| 132 | /* Clear CQ entry ready bit */ |
Ben Segal | 4e87334 | 2019-08-01 23:22:20 +0000 | [diff] [blame] | 133 | cq_entry->data = cpu_to_le32(le32_to_cpu(cq_entry->data) & |
| 134 | ~CQ_ENTRY_READY_MASK); |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 135 | |
| 136 | cq->ci = hl_cq_inc_ptr(cq->ci); |
| 137 | |
| 138 | /* Increment free slots */ |
| 139 | atomic_inc(&cq->free_slots_cnt); |
| 140 | } |
| 141 | |
| 142 | return IRQ_HANDLED; |
| 143 | } |
| 144 | |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 145 | /** |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 146 | * hl_irq_handler_eq - irq handler for event queue |
| 147 | * |
| 148 | * @irq: irq number |
| 149 | * @arg: pointer to event queue structure |
| 150 | * |
| 151 | */ |
| 152 | irqreturn_t hl_irq_handler_eq(int irq, void *arg) |
| 153 | { |
| 154 | struct hl_eq *eq = arg; |
| 155 | struct hl_device *hdev = eq->hdev; |
| 156 | struct hl_eq_entry *eq_entry; |
| 157 | struct hl_eq_entry *eq_base; |
| 158 | struct hl_eqe_work *handle_eqe_work; |
| 159 | |
| 160 | eq_base = (struct hl_eq_entry *) (uintptr_t) eq->kernel_address; |
| 161 | |
| 162 | while (1) { |
| 163 | bool entry_ready = |
Oded Gabbay | fe9a52c | 2019-08-08 17:05:45 +0300 | [diff] [blame] | 164 | ((le32_to_cpu(eq_base[eq->ci].hdr.ctl) & |
Oded Gabbay | 8c84487 | 2019-02-28 10:46:24 +0200 | [diff] [blame] | 165 | EQ_CTL_READY_MASK) >> EQ_CTL_READY_SHIFT); |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 166 | |
| 167 | if (!entry_ready) |
| 168 | break; |
| 169 | |
| 170 | eq_entry = &eq_base[eq->ci]; |
| 171 | |
| 172 | /* |
| 173 | * Make sure we read EQ entry contents after we've |
| 174 | * checked the ownership bit. |
| 175 | */ |
| 176 | dma_rmb(); |
| 177 | |
| 178 | if (hdev->disabled) { |
| 179 | dev_warn(hdev->dev, |
| 180 | "Device disabled but received IRQ %d for EQ\n", |
| 181 | irq); |
| 182 | goto skip_irq; |
| 183 | } |
| 184 | |
| 185 | handle_eqe_work = kmalloc(sizeof(*handle_eqe_work), GFP_ATOMIC); |
| 186 | if (handle_eqe_work) { |
| 187 | INIT_WORK(&handle_eqe_work->eq_work, irq_handle_eqe); |
| 188 | handle_eqe_work->hdev = hdev; |
| 189 | |
| 190 | memcpy(&handle_eqe_work->eq_entry, eq_entry, |
| 191 | sizeof(*eq_entry)); |
| 192 | |
| 193 | queue_work(hdev->eq_wq, &handle_eqe_work->eq_work); |
| 194 | } |
| 195 | skip_irq: |
| 196 | /* Clear EQ entry ready bit */ |
Oded Gabbay | 8c84487 | 2019-02-28 10:46:24 +0200 | [diff] [blame] | 197 | eq_entry->hdr.ctl = |
Oded Gabbay | fe9a52c | 2019-08-08 17:05:45 +0300 | [diff] [blame] | 198 | cpu_to_le32(le32_to_cpu(eq_entry->hdr.ctl) & |
Oded Gabbay | 8c84487 | 2019-02-28 10:46:24 +0200 | [diff] [blame] | 199 | ~EQ_CTL_READY_MASK); |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 200 | |
| 201 | eq->ci = hl_eq_inc_ptr(eq->ci); |
| 202 | |
| 203 | hdev->asic_funcs->update_eq_ci(hdev, eq->ci); |
| 204 | } |
| 205 | |
| 206 | return IRQ_HANDLED; |
| 207 | } |
| 208 | |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 209 | /** |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 210 | * hl_cq_init - main initialization function for an cq object |
| 211 | * |
| 212 | * @hdev: pointer to device structure |
| 213 | * @q: pointer to cq structure |
| 214 | * @hw_queue_id: The H/W queue ID this completion queue belongs to |
| 215 | * |
| 216 | * Allocate dma-able memory for the completion queue and initialize fields |
| 217 | * Returns 0 on success |
| 218 | */ |
| 219 | int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id) |
| 220 | { |
| 221 | void *p; |
| 222 | |
Oded Gabbay | d9c3aa8 | 2019-05-01 11:47:04 +0300 | [diff] [blame] | 223 | p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, HL_CQ_SIZE_IN_BYTES, |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 224 | &q->bus_address, GFP_KERNEL | __GFP_ZERO); |
| 225 | if (!p) |
| 226 | return -ENOMEM; |
| 227 | |
| 228 | q->hdev = hdev; |
| 229 | q->kernel_address = (u64) (uintptr_t) p; |
| 230 | q->hw_queue_id = hw_queue_id; |
| 231 | q->ci = 0; |
| 232 | q->pi = 0; |
| 233 | |
| 234 | atomic_set(&q->free_slots_cnt, HL_CQ_LENGTH); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 239 | /** |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 240 | * hl_cq_fini - destroy completion queue |
| 241 | * |
| 242 | * @hdev: pointer to device structure |
| 243 | * @q: pointer to cq structure |
| 244 | * |
| 245 | * Free the completion queue memory |
| 246 | */ |
| 247 | void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q) |
| 248 | { |
Oded Gabbay | d9c3aa8 | 2019-05-01 11:47:04 +0300 | [diff] [blame] | 249 | hdev->asic_funcs->asic_dma_free_coherent(hdev, HL_CQ_SIZE_IN_BYTES, |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 250 | (void *) (uintptr_t) q->kernel_address, q->bus_address); |
| 251 | } |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 252 | |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 253 | void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q) |
| 254 | { |
| 255 | q->ci = 0; |
| 256 | q->pi = 0; |
| 257 | |
| 258 | atomic_set(&q->free_slots_cnt, HL_CQ_LENGTH); |
| 259 | |
| 260 | /* |
| 261 | * It's not enough to just reset the PI/CI because the H/W may have |
| 262 | * written valid completion entries before it was halted and therefore |
| 263 | * we need to clean the actual queues so we won't process old entries |
| 264 | * when the device is operational again |
| 265 | */ |
| 266 | |
| 267 | memset((void *) (uintptr_t) q->kernel_address, 0, HL_CQ_SIZE_IN_BYTES); |
| 268 | } |
| 269 | |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 270 | /** |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 271 | * hl_eq_init - main initialization function for an event queue object |
| 272 | * |
| 273 | * @hdev: pointer to device structure |
| 274 | * @q: pointer to eq structure |
| 275 | * |
| 276 | * Allocate dma-able memory for the event queue and initialize fields |
| 277 | * Returns 0 on success |
| 278 | */ |
| 279 | int hl_eq_init(struct hl_device *hdev, struct hl_eq *q) |
| 280 | { |
| 281 | void *p; |
| 282 | |
Tomer Tayar | 03d5f64 | 2019-04-28 19:17:38 +0300 | [diff] [blame] | 283 | p = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, |
| 284 | HL_EQ_SIZE_IN_BYTES, |
| 285 | &q->bus_address); |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 286 | if (!p) |
| 287 | return -ENOMEM; |
| 288 | |
| 289 | q->hdev = hdev; |
| 290 | q->kernel_address = (u64) (uintptr_t) p; |
| 291 | q->ci = 0; |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
Lee Jones | 3db99f0 | 2020-07-01 09:58:38 +0100 | [diff] [blame] | 296 | /** |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 297 | * hl_eq_fini - destroy event queue |
| 298 | * |
| 299 | * @hdev: pointer to device structure |
| 300 | * @q: pointer to eq structure |
| 301 | * |
| 302 | * Free the event queue memory |
| 303 | */ |
| 304 | void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q) |
| 305 | { |
| 306 | flush_workqueue(hdev->eq_wq); |
| 307 | |
Tomer Tayar | 03d5f64 | 2019-04-28 19:17:38 +0300 | [diff] [blame] | 308 | hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, |
| 309 | HL_EQ_SIZE_IN_BYTES, |
| 310 | (void *) (uintptr_t) q->kernel_address); |
Oded Gabbay | 1251f23 | 2019-02-16 00:39:18 +0200 | [diff] [blame] | 311 | } |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 312 | |
| 313 | void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q) |
| 314 | { |
| 315 | q->ci = 0; |
| 316 | |
| 317 | /* |
| 318 | * It's not enough to just reset the PI/CI because the H/W may have |
| 319 | * written valid completion entries before it was halted and therefore |
| 320 | * we need to clean the actual queues so we won't process old entries |
| 321 | * when the device is operational again |
| 322 | */ |
| 323 | |
| 324 | memset((void *) (uintptr_t) q->kernel_address, 0, HL_EQ_SIZE_IN_BYTES); |
| 325 | } |