1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Copyright 2016-2022 HabanaLabs, Ltd.
5  * All Rights Reserved.
6  */
7 
8 #include "habanalabs.h"
9 
10 #include <linux/slab.h>
11 
12 /**
13  * struct hl_eqe_work - This structure is used to schedule work of EQ
14  *                      entry and cpucp_reset event
15  *
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
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 };
25 
26 /**
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 
42 /**
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 static 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 
69 /**
70  * job_finish - queue job finish work
71  *
72  * @hdev: pointer to device structure
73  * @cs_seq: command submission sequence
74  * @cq: completion queue
75  *
76  */
77 static void job_finish(struct hl_device *hdev, u32 cs_seq, struct hl_cq *cq)
78 {
79 	struct hl_hw_queue *queue;
80 	struct hl_cs_job *job;
81 
82 	queue = &hdev->kernel_queues[cq->hw_queue_id];
83 	job = queue->shadow_queue[hl_pi_2_offset(cs_seq)];
84 	queue_work(hdev->cq_wq[cq->cq_idx], &job->finish_work);
85 
86 	atomic_inc(&queue->ci);
87 }
88 
89 /**
90  * cs_finish - queue all cs jobs finish work
91  *
92  * @hdev: pointer to device structure
93  * @cs_seq: command submission sequence
94  *
95  */
96 static void cs_finish(struct hl_device *hdev, u16 cs_seq)
97 {
98 	struct asic_fixed_properties *prop = &hdev->asic_prop;
99 	struct hl_hw_queue *queue;
100 	struct hl_cs *cs;
101 	struct hl_cs_job *job;
102 
103 	cs = hdev->shadow_cs_queue[cs_seq & (prop->max_pending_cs - 1)];
104 	if (!cs) {
105 		dev_warn(hdev->dev,
106 			"No pointer to CS in shadow array at index %d\n",
107 			cs_seq);
108 		return;
109 	}
110 
111 	list_for_each_entry(job, &cs->job_list, cs_node) {
112 		queue = &hdev->kernel_queues[job->hw_queue_id];
113 		atomic_inc(&queue->ci);
114 	}
115 
116 	queue_work(hdev->cs_cmplt_wq, &cs->finish_work);
117 }
118 
119 /**
120  * hl_irq_handler_cq - irq handler for completion queue
121  *
122  * @irq: irq number
123  * @arg: pointer to completion queue structure
124  *
125  */
126 irqreturn_t hl_irq_handler_cq(int irq, void *arg)
127 {
128 	struct hl_cq *cq = arg;
129 	struct hl_device *hdev = cq->hdev;
130 	bool shadow_index_valid, entry_ready;
131 	u16 shadow_index;
132 	struct hl_cq_entry *cq_entry, *cq_base;
133 
134 	if (hdev->disabled) {
135 		dev_dbg(hdev->dev,
136 			"Device disabled but received IRQ %d for CQ %d\n",
137 			irq, cq->hw_queue_id);
138 		return IRQ_HANDLED;
139 	}
140 
141 	cq_base = cq->kernel_address;
142 
143 	while (1) {
144 		cq_entry = (struct hl_cq_entry *) &cq_base[cq->ci];
145 
146 		entry_ready = !!FIELD_GET(CQ_ENTRY_READY_MASK,
147 				le32_to_cpu(cq_entry->data));
148 		if (!entry_ready)
149 			break;
150 
151 		/* Make sure we read CQ entry contents after we've
152 		 * checked the ownership bit.
153 		 */
154 		dma_rmb();
155 
156 		shadow_index_valid =
157 			!!FIELD_GET(CQ_ENTRY_SHADOW_INDEX_VALID_MASK,
158 					le32_to_cpu(cq_entry->data));
159 
160 		shadow_index = FIELD_GET(CQ_ENTRY_SHADOW_INDEX_MASK,
161 				le32_to_cpu(cq_entry->data));
162 
163 		/*
164 		 * CQ interrupt handler has 2 modes of operation:
165 		 * 1. Interrupt per CS completion: (Single CQ for all queues)
166 		 *    CQ entry represents a completed CS
167 		 *
168 		 * 2. Interrupt per CS job completion in queue: (CQ per queue)
169 		 *    CQ entry represents a completed job in a certain queue
170 		 */
171 		if (shadow_index_valid && !hdev->disabled) {
172 			if (hdev->asic_prop.completion_mode ==
173 					HL_COMPLETION_MODE_CS)
174 				cs_finish(hdev, shadow_index);
175 			else
176 				job_finish(hdev, shadow_index, cq);
177 		}
178 
179 		/* Clear CQ entry ready bit */
180 		cq_entry->data = cpu_to_le32(le32_to_cpu(cq_entry->data) &
181 						~CQ_ENTRY_READY_MASK);
182 
183 		cq->ci = hl_cq_inc_ptr(cq->ci);
184 
185 		/* Increment free slots */
186 		atomic_inc(&cq->free_slots_cnt);
187 	}
188 
189 	return IRQ_HANDLED;
190 }
191 
192 /*
193  * hl_ts_free_objects - handler of the free objects workqueue.
194  * This function should put refcount to objects that the registration node
195  * took refcount to them.
196  * @work: workqueue object pointer
197  */
198 static void hl_ts_free_objects(struct work_struct *work)
199 {
200 	struct timestamp_reg_work_obj *job =
201 			container_of(work, struct timestamp_reg_work_obj, free_obj);
202 	struct timestamp_reg_free_node *free_obj, *temp_free_obj;
203 	struct list_head *free_list_head = job->free_obj_head;
204 	struct hl_device *hdev = job->hdev;
205 
206 	list_for_each_entry_safe(free_obj, temp_free_obj, free_list_head, free_objects_node) {
207 		dev_dbg(hdev->dev, "About to put refcount to buf (%p) cq_cb(%p)\n",
208 					free_obj->buf,
209 					free_obj->cq_cb);
210 
211 		hl_mmap_mem_buf_put(free_obj->buf);
212 		hl_cb_put(free_obj->cq_cb);
213 		kfree(free_obj);
214 	}
215 
216 	kfree(free_list_head);
217 	kfree(job);
218 }
219 
220 /*
221  * This function called with spin_lock of wait_list_lock taken
222  * This function will set timestamp and delete the registration node from the
223  * wait_list_lock.
224  * and since we're protected with spin_lock here, so we cannot just put the refcount
225  * for the objects here, since the release function may be called and it's also a long
226  * logic (which might sleep also) that cannot be handled in irq context.
227  * so here we'll be filling a list with nodes of "put" jobs and then will send this
228  * list to a dedicated workqueue to do the actual put.
229  */
230 static int handle_registration_node(struct hl_device *hdev, struct hl_user_pending_interrupt *pend,
231 						struct list_head **free_list, ktime_t now)
232 {
233 	struct timestamp_reg_free_node *free_node;
234 	u64 timestamp;
235 
236 	if (!(*free_list)) {
237 		/* Alloc/Init the timestamp registration free objects list */
238 		*free_list = kmalloc(sizeof(struct list_head), GFP_ATOMIC);
239 		if (!(*free_list))
240 			return -ENOMEM;
241 
242 		INIT_LIST_HEAD(*free_list);
243 	}
244 
245 	free_node = kmalloc(sizeof(*free_node), GFP_ATOMIC);
246 	if (!free_node)
247 		return -ENOMEM;
248 
249 	timestamp = ktime_to_ns(now);
250 
251 	*pend->ts_reg_info.timestamp_kernel_addr = timestamp;
252 
253 	dev_dbg(hdev->dev, "Timestamp is set to ts cb address (%p), ts: 0x%llx\n",
254 			pend->ts_reg_info.timestamp_kernel_addr,
255 			*(u64 *)pend->ts_reg_info.timestamp_kernel_addr);
256 
257 	list_del(&pend->wait_list_node);
258 
259 	/* Mark kernel CB node as free */
260 	pend->ts_reg_info.in_use = 0;
261 
262 	/* Putting the refcount for ts_buff and cq_cb objects will be handled
263 	 * in workqueue context, just add job to free_list.
264 	 */
265 	free_node->buf = pend->ts_reg_info.buf;
266 	free_node->cq_cb = pend->ts_reg_info.cq_cb;
267 	list_add(&free_node->free_objects_node, *free_list);
268 
269 	return 0;
270 }
271 
272 static void handle_user_interrupt(struct hl_device *hdev, struct hl_user_interrupt *intr)
273 {
274 	struct hl_user_pending_interrupt *pend, *temp_pend;
275 	struct list_head *ts_reg_free_list_head = NULL;
276 	struct timestamp_reg_work_obj *job;
277 	bool reg_node_handle_fail = false;
278 	ktime_t now = ktime_get();
279 	int rc;
280 
281 	/* For registration nodes:
282 	 * As part of handling the registration nodes, we should put refcount to
283 	 * some objects. the problem is that we cannot do that under spinlock
284 	 * or in irq handler context at all (since release functions are long and
285 	 * might sleep), so we will need to handle that part in workqueue context.
286 	 * To avoid handling kmalloc failure which compels us rolling back actions
287 	 * and move nodes hanged on the free list back to the interrupt wait list
288 	 * we always alloc the job of the WQ at the beginning.
289 	 */
290 	job = kmalloc(sizeof(*job), GFP_ATOMIC);
291 	if (!job)
292 		return;
293 
294 	spin_lock(&intr->wait_list_lock);
295 	list_for_each_entry_safe(pend, temp_pend, &intr->wait_list_head, wait_list_node) {
296 		if ((pend->cq_kernel_addr && *(pend->cq_kernel_addr) >= pend->cq_target_value) ||
297 				!pend->cq_kernel_addr) {
298 			if (pend->ts_reg_info.buf) {
299 				if (!reg_node_handle_fail) {
300 					rc = handle_registration_node(hdev, pend,
301 								&ts_reg_free_list_head, now);
302 					if (rc)
303 						reg_node_handle_fail = true;
304 				}
305 			} else {
306 				/* Handle wait target value node */
307 				pend->fence.timestamp = now;
308 				complete_all(&pend->fence.completion);
309 			}
310 		}
311 	}
312 	spin_unlock(&intr->wait_list_lock);
313 
314 	if (ts_reg_free_list_head) {
315 		INIT_WORK(&job->free_obj, hl_ts_free_objects);
316 		job->free_obj_head = ts_reg_free_list_head;
317 		job->hdev = hdev;
318 		queue_work(hdev->ts_free_obj_wq, &job->free_obj);
319 	} else {
320 		kfree(job);
321 	}
322 }
323 
324 /**
325  * hl_irq_handler_user_interrupt - irq handler for user interrupts
326  *
327  * @irq: irq number
328  * @arg: pointer to user interrupt structure
329  *
330  */
331 irqreturn_t hl_irq_handler_user_interrupt(int irq, void *arg)
332 {
333 	struct hl_user_interrupt *user_int = arg;
334 	struct hl_device *hdev = user_int->hdev;
335 
336 	switch (user_int->type) {
337 	case HL_USR_INTERRUPT_CQ:
338 		handle_user_interrupt(hdev, &hdev->common_user_cq_interrupt);
339 
340 		/* Handle user cq interrupt registered on this specific irq */
341 		handle_user_interrupt(hdev, user_int);
342 		break;
343 	case HL_USR_INTERRUPT_DECODER:
344 		handle_user_interrupt(hdev, &hdev->common_decoder_interrupt);
345 
346 		/* Handle decoder interrupt registered on this specific irq */
347 		handle_user_interrupt(hdev, user_int);
348 		break;
349 	default:
350 		break;
351 	}
352 
353 	return IRQ_HANDLED;
354 }
355 
356 /**
357  * hl_irq_handler_default - default irq handler
358  *
359  * @irq: irq number
360  * @arg: pointer to user interrupt structure
361  *
362  */
363 irqreturn_t hl_irq_handler_default(int irq, void *arg)
364 {
365 	struct hl_user_interrupt *user_interrupt = arg;
366 	struct hl_device *hdev = user_interrupt->hdev;
367 	u32 interrupt_id = user_interrupt->interrupt_id;
368 
369 	dev_err(hdev->dev, "got invalid user interrupt %u", interrupt_id);
370 
371 	return IRQ_HANDLED;
372 }
373 
374 /**
375  * hl_irq_handler_eq - irq handler for event queue
376  *
377  * @irq: irq number
378  * @arg: pointer to event queue structure
379  *
380  */
381 irqreturn_t hl_irq_handler_eq(int irq, void *arg)
382 {
383 	struct hl_eq *eq = arg;
384 	struct hl_device *hdev = eq->hdev;
385 	struct hl_eq_entry *eq_entry;
386 	struct hl_eq_entry *eq_base;
387 	struct hl_eqe_work *handle_eqe_work;
388 	bool entry_ready;
389 	u32 cur_eqe;
390 	u16 cur_eqe_index;
391 
392 	eq_base = eq->kernel_address;
393 
394 	while (1) {
395 		cur_eqe = le32_to_cpu(eq_base[eq->ci].hdr.ctl);
396 		entry_ready = !!FIELD_GET(EQ_CTL_READY_MASK, cur_eqe);
397 
398 		if (!entry_ready)
399 			break;
400 
401 		cur_eqe_index = FIELD_GET(EQ_CTL_INDEX_MASK, cur_eqe);
402 		if ((hdev->event_queue.check_eqe_index) &&
403 				(((eq->prev_eqe_index + 1) & EQ_CTL_INDEX_MASK)
404 							!= cur_eqe_index)) {
405 			dev_dbg(hdev->dev,
406 				"EQE 0x%x in queue is ready but index does not match %d!=%d",
407 				eq_base[eq->ci].hdr.ctl,
408 				((eq->prev_eqe_index + 1) & EQ_CTL_INDEX_MASK),
409 				cur_eqe_index);
410 			break;
411 		}
412 
413 		eq->prev_eqe_index++;
414 
415 		eq_entry = &eq_base[eq->ci];
416 
417 		/*
418 		 * Make sure we read EQ entry contents after we've
419 		 * checked the ownership bit.
420 		 */
421 		dma_rmb();
422 
423 		if (hdev->disabled && !hdev->reset_info.in_compute_reset) {
424 			dev_warn(hdev->dev, "Device disabled but received an EQ event\n");
425 			goto skip_irq;
426 		}
427 
428 		handle_eqe_work = kmalloc(sizeof(*handle_eqe_work), GFP_ATOMIC);
429 		if (handle_eqe_work) {
430 			INIT_WORK(&handle_eqe_work->eq_work, irq_handle_eqe);
431 			handle_eqe_work->hdev = hdev;
432 
433 			memcpy(&handle_eqe_work->eq_entry, eq_entry,
434 					sizeof(*eq_entry));
435 
436 			queue_work(hdev->eq_wq, &handle_eqe_work->eq_work);
437 		}
438 skip_irq:
439 		/* Clear EQ entry ready bit */
440 		eq_entry->hdr.ctl =
441 			cpu_to_le32(le32_to_cpu(eq_entry->hdr.ctl) &
442 							~EQ_CTL_READY_MASK);
443 
444 		eq->ci = hl_eq_inc_ptr(eq->ci);
445 
446 		hdev->asic_funcs->update_eq_ci(hdev, eq->ci);
447 	}
448 
449 	return IRQ_HANDLED;
450 }
451 
452 /**
453  * hl_irq_handler_dec_abnrm - Decoder error interrupt handler
454  * @irq: IRQ number
455  * @arg: pointer to decoder structure.
456  */
457 irqreturn_t hl_irq_handler_dec_abnrm(int irq, void *arg)
458 {
459 	struct hl_dec *dec = arg;
460 
461 	schedule_work(&dec->completion_abnrm_work);
462 
463 	return IRQ_HANDLED;
464 }
465 
466 /**
467  * hl_cq_init - main initialization function for an cq object
468  *
469  * @hdev: pointer to device structure
470  * @q: pointer to cq structure
471  * @hw_queue_id: The H/W queue ID this completion queue belongs to
472  *               HL_INVALID_QUEUE if cq is not attached to any specific queue
473  *
474  * Allocate dma-able memory for the completion queue and initialize fields
475  * Returns 0 on success
476  */
477 int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id)
478 {
479 	void *p;
480 
481 	p = hl_asic_dma_alloc_coherent(hdev, HL_CQ_SIZE_IN_BYTES, &q->bus_address,
482 					GFP_KERNEL | __GFP_ZERO);
483 	if (!p)
484 		return -ENOMEM;
485 
486 	q->hdev = hdev;
487 	q->kernel_address = p;
488 	q->hw_queue_id = hw_queue_id;
489 	q->ci = 0;
490 	q->pi = 0;
491 
492 	atomic_set(&q->free_slots_cnt, HL_CQ_LENGTH);
493 
494 	return 0;
495 }
496 
497 /**
498  * hl_cq_fini - destroy completion queue
499  *
500  * @hdev: pointer to device structure
501  * @q: pointer to cq structure
502  *
503  * Free the completion queue memory
504  */
505 void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q)
506 {
507 	hl_asic_dma_free_coherent(hdev, HL_CQ_SIZE_IN_BYTES, q->kernel_address, q->bus_address);
508 }
509 
510 void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q)
511 {
512 	q->ci = 0;
513 	q->pi = 0;
514 
515 	atomic_set(&q->free_slots_cnt, HL_CQ_LENGTH);
516 
517 	/*
518 	 * It's not enough to just reset the PI/CI because the H/W may have
519 	 * written valid completion entries before it was halted and therefore
520 	 * we need to clean the actual queues so we won't process old entries
521 	 * when the device is operational again
522 	 */
523 
524 	memset(q->kernel_address, 0, HL_CQ_SIZE_IN_BYTES);
525 }
526 
527 /**
528  * hl_eq_init - main initialization function for an event queue object
529  *
530  * @hdev: pointer to device structure
531  * @q: pointer to eq structure
532  *
533  * Allocate dma-able memory for the event queue and initialize fields
534  * Returns 0 on success
535  */
536 int hl_eq_init(struct hl_device *hdev, struct hl_eq *q)
537 {
538 	void *p;
539 
540 	p = hl_cpu_accessible_dma_pool_alloc(hdev, HL_EQ_SIZE_IN_BYTES, &q->bus_address);
541 	if (!p)
542 		return -ENOMEM;
543 
544 	q->hdev = hdev;
545 	q->kernel_address = p;
546 	q->ci = 0;
547 	q->prev_eqe_index = 0;
548 
549 	return 0;
550 }
551 
552 /**
553  * hl_eq_fini - destroy event queue
554  *
555  * @hdev: pointer to device structure
556  * @q: pointer to eq structure
557  *
558  * Free the event queue memory
559  */
560 void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q)
561 {
562 	flush_workqueue(hdev->eq_wq);
563 
564 	hl_cpu_accessible_dma_pool_free(hdev, HL_EQ_SIZE_IN_BYTES, q->kernel_address);
565 }
566 
567 void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q)
568 {
569 	q->ci = 0;
570 	q->prev_eqe_index = 0;
571 
572 	/*
573 	 * It's not enough to just reset the PI/CI because the H/W may have
574 	 * written valid completion entries before it was halted and therefore
575 	 * we need to clean the actual queues so we won't process old entries
576 	 * when the device is operational again
577 	 */
578 
579 	memset(q->kernel_address, 0, HL_EQ_SIZE_IN_BYTES);
580 }
581