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