blob: d4c2077a3718c4750137ea25e5653e15d4c3dc72 [file] [log] [blame]
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001// 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 Gabbay1251f232019-02-16 00:39:18 +020010#include <linux/slab.h>
11
12/**
13 * This structure is used to schedule work of EQ entry and armcp_reset event
14 *
15 * @eq_work - workqueue object to run when EQ entry is received
16 * @hdev - pointer to device structure
17 * @eq_entry - copy of the EQ entry
18 */
19struct hl_eqe_work {
20 struct work_struct eq_work;
21 struct hl_device *hdev;
22 struct hl_eq_entry eq_entry;
23};
Oded Gabbay9494a8d2019-02-16 00:39:17 +020024
25/*
26 * hl_cq_inc_ptr - increment ci or pi of cq
27 *
28 * @ptr: the current ci or pi value of the completion queue
29 *
30 * Increment ptr by 1. If it reaches the number of completion queue
31 * entries, set it to 0
32 */
33inline u32 hl_cq_inc_ptr(u32 ptr)
34{
35 ptr++;
36 if (unlikely(ptr == HL_CQ_LENGTH))
37 ptr = 0;
38 return ptr;
39}
40
41/*
Oded Gabbay1251f232019-02-16 00:39:18 +020042 * hl_eq_inc_ptr - increment ci of eq
43 *
44 * @ptr: the current ci value of the event queue
45 *
46 * Increment ptr by 1. If it reaches the number of event queue
47 * entries, set it to 0
48 */
49inline u32 hl_eq_inc_ptr(u32 ptr)
50{
51 ptr++;
52 if (unlikely(ptr == HL_EQ_LENGTH))
53 ptr = 0;
54 return ptr;
55}
56
57static void irq_handle_eqe(struct work_struct *work)
58{
59 struct hl_eqe_work *eqe_work = container_of(work, struct hl_eqe_work,
60 eq_work);
61 struct hl_device *hdev = eqe_work->hdev;
62
63 hdev->asic_funcs->handle_eqe(hdev, &eqe_work->eq_entry);
64
65 kfree(eqe_work);
66}
67
68/*
Oded Gabbay9494a8d2019-02-16 00:39:17 +020069 * hl_irq_handler_cq - irq handler for completion queue
70 *
71 * @irq: irq number
72 * @arg: pointer to completion queue structure
73 *
74 */
75irqreturn_t hl_irq_handler_cq(int irq, void *arg)
76{
77 struct hl_cq *cq = arg;
78 struct hl_device *hdev = cq->hdev;
79 struct hl_hw_queue *queue;
80 struct hl_cs_job *job;
81 bool shadow_index_valid;
82 u16 shadow_index;
83 u32 *cq_entry;
84 u32 *cq_base;
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
93 cq_base = (u32 *) (uintptr_t) cq->kernel_address;
94
95 while (1) {
96 bool entry_ready = ((cq_base[cq->ci] & CQ_ENTRY_READY_MASK)
97 >> CQ_ENTRY_READY_SHIFT);
98
99 if (!entry_ready)
100 break;
101
102 cq_entry = (u32 *) &cq_base[cq->ci];
103
104 /*
105 * Make sure we read CQ entry contents after we've
106 * checked the ownership bit.
107 */
108 dma_rmb();
109
110 shadow_index_valid =
111 ((*cq_entry & CQ_ENTRY_SHADOW_INDEX_VALID_MASK)
112 >> CQ_ENTRY_SHADOW_INDEX_VALID_SHIFT);
113
114 shadow_index = (u16)
115 ((*cq_entry & CQ_ENTRY_SHADOW_INDEX_MASK)
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
125 /*
126 * Update ci of the context's queue. There is no
127 * need to protect it with spinlock because this update is
128 * done only inside IRQ and there is a different IRQ per
129 * queue
130 */
131 queue->ci = hl_queue_inc_ptr(queue->ci);
132
133 /* Clear CQ entry ready bit */
134 cq_base[cq->ci] &= ~CQ_ENTRY_READY_MASK;
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
145/*
Oded Gabbay1251f232019-02-16 00:39:18 +0200146 * hl_irq_handler_eq - irq handler for event queue
147 *
148 * @irq: irq number
149 * @arg: pointer to event queue structure
150 *
151 */
152irqreturn_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 =
164 ((eq_base[eq->ci].hdr.ctl & EQ_CTL_READY_MASK)
165 >> EQ_CTL_READY_SHIFT);
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 }
195skip_irq:
196 /* Clear EQ entry ready bit */
197 eq_entry->hdr.ctl &= ~EQ_CTL_READY_MASK;
198
199 eq->ci = hl_eq_inc_ptr(eq->ci);
200
201 hdev->asic_funcs->update_eq_ci(hdev, eq->ci);
202 }
203
204 return IRQ_HANDLED;
205}
206
207/*
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200208 * hl_cq_init - main initialization function for an cq object
209 *
210 * @hdev: pointer to device structure
211 * @q: pointer to cq structure
212 * @hw_queue_id: The H/W queue ID this completion queue belongs to
213 *
214 * Allocate dma-able memory for the completion queue and initialize fields
215 * Returns 0 on success
216 */
217int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id)
218{
219 void *p;
220
221 BUILD_BUG_ON(HL_CQ_SIZE_IN_BYTES > HL_PAGE_SIZE);
222
223 p = hdev->asic_funcs->dma_alloc_coherent(hdev, HL_CQ_SIZE_IN_BYTES,
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
239/*
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 */
247void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q)
248{
249 hdev->asic_funcs->dma_free_coherent(hdev, HL_CQ_SIZE_IN_BYTES,
250 (void *) (uintptr_t) q->kernel_address, q->bus_address);
251}
Oded Gabbay1251f232019-02-16 00:39:18 +0200252
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200253void 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
Oded Gabbay1251f232019-02-16 00:39:18 +0200270/*
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 */
279int hl_eq_init(struct hl_device *hdev, struct hl_eq *q)
280{
281 void *p;
282
283 BUILD_BUG_ON(HL_EQ_SIZE_IN_BYTES > HL_PAGE_SIZE);
284
285 p = hdev->asic_funcs->dma_alloc_coherent(hdev, HL_EQ_SIZE_IN_BYTES,
286 &q->bus_address, GFP_KERNEL | __GFP_ZERO);
287 if (!p)
288 return -ENOMEM;
289
290 q->hdev = hdev;
291 q->kernel_address = (u64) (uintptr_t) p;
292 q->ci = 0;
293
294 return 0;
295}
296
297/*
298 * hl_eq_fini - destroy event queue
299 *
300 * @hdev: pointer to device structure
301 * @q: pointer to eq structure
302 *
303 * Free the event queue memory
304 */
305void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q)
306{
307 flush_workqueue(hdev->eq_wq);
308
309 hdev->asic_funcs->dma_free_coherent(hdev, HL_EQ_SIZE_IN_BYTES,
310 (void *) (uintptr_t) q->kernel_address, q->bus_address);
311}
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200312
313void 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}