1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include <uapi/misc/habanalabs.h>
9 #include "habanalabs.h"
11 #include <linux/uaccess.h>
12 #include <linux/slab.h>
14 #define HL_CS_FLAGS_TYPE_MASK (HL_CS_FLAGS_SIGNAL | HL_CS_FLAGS_WAIT | \
15 HL_CS_FLAGS_COLLECTIVE_WAIT)
18 * enum hl_cs_wait_status - cs wait status
19 * @CS_WAIT_STATUS_BUSY: cs was not completed yet
20 * @CS_WAIT_STATUS_COMPLETED: cs completed
21 * @CS_WAIT_STATUS_GONE: cs completed but fence is already gone
23 enum hl_cs_wait_status {
25 CS_WAIT_STATUS_COMPLETED,
29 static void job_wq_completion(struct work_struct *work);
30 static int _hl_cs_wait_ioctl(struct hl_device *hdev, struct hl_ctx *ctx,
31 u64 timeout_us, u64 seq,
32 enum hl_cs_wait_status *status, s64 *timestamp);
33 static void cs_do_release(struct kref *ref);
35 static void hl_sob_reset(struct kref *ref)
37 struct hl_hw_sob *hw_sob = container_of(ref, struct hl_hw_sob,
39 struct hl_device *hdev = hw_sob->hdev;
41 hdev->asic_funcs->reset_sob(hdev, hw_sob);
44 void hl_sob_reset_error(struct kref *ref)
46 struct hl_hw_sob *hw_sob = container_of(ref, struct hl_hw_sob,
48 struct hl_device *hdev = hw_sob->hdev;
51 "SOB release shouldn't be called here, q_idx: %d, sob_id: %d\n",
52 hw_sob->q_idx, hw_sob->sob_id);
56 * hl_gen_sob_mask() - Generates a sob mask to be used in a monitor arm packet
57 * @sob_base: sob base id
58 * @sob_mask: sob user mask, each bit represents a sob offset from sob base
59 * @mask: generated mask
61 * Return: 0 if given parameters are valid
63 int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask)
70 if (sob_mask == 0x1) {
71 *mask = ~(1 << (sob_base & 0x7));
73 /* find msb in order to verify sob range is valid */
74 for (i = BITS_PER_BYTE - 1 ; i >= 0 ; i--)
75 if (BIT(i) & sob_mask)
78 if (i > (HL_MAX_SOBS_PER_MONITOR - (sob_base & 0x7) - 1))
87 static void sob_reset_work(struct work_struct *work)
89 struct hl_cs_compl *hl_cs_cmpl =
90 container_of(work, struct hl_cs_compl, sob_reset_work);
91 struct hl_device *hdev = hl_cs_cmpl->hdev;
94 * A signal CS can get completion while the corresponding wait
95 * for signal CS is on its way to the PQ. The wait for signal CS
96 * will get stuck if the signal CS incremented the SOB to its
97 * max value and there are no pending (submitted) waits on this
99 * We do the following to void this situation:
100 * 1. The wait for signal CS must get a ref for the signal CS as
101 * soon as possible in cs_ioctl_signal_wait() and put it
102 * before being submitted to the PQ but after it incremented
103 * the SOB refcnt in init_signal_wait_cs().
104 * 2. Signal/Wait for signal CS will decrement the SOB refcnt
106 * These two measures guarantee that the wait for signal CS will
107 * reset the SOB upon completion rather than the signal CS and
108 * hence the above scenario is avoided.
110 kref_put(&hl_cs_cmpl->hw_sob->kref, hl_sob_reset);
112 if (hl_cs_cmpl->type == CS_TYPE_COLLECTIVE_WAIT)
113 hdev->asic_funcs->reset_sob_group(hdev,
114 hl_cs_cmpl->sob_group);
119 static void hl_fence_release(struct kref *kref)
121 struct hl_fence *fence =
122 container_of(kref, struct hl_fence, refcount);
123 struct hl_cs_compl *hl_cs_cmpl =
124 container_of(fence, struct hl_cs_compl, base_fence);
125 struct hl_device *hdev = hl_cs_cmpl->hdev;
127 /* EBUSY means the CS was never submitted and hence we don't have
128 * an attached hw_sob object that we should handle here
130 if (fence->error == -EBUSY)
133 if ((hl_cs_cmpl->type == CS_TYPE_SIGNAL) ||
134 (hl_cs_cmpl->type == CS_TYPE_WAIT) ||
135 (hl_cs_cmpl->type == CS_TYPE_COLLECTIVE_WAIT)) {
138 "CS 0x%llx type %d finished, sob_id: %d, sob_val: 0x%x\n",
141 hl_cs_cmpl->hw_sob->sob_id,
142 hl_cs_cmpl->sob_val);
144 queue_work(hdev->sob_reset_wq, &hl_cs_cmpl->sob_reset_work);
153 void hl_fence_put(struct hl_fence *fence)
156 kref_put(&fence->refcount, hl_fence_release);
159 void hl_fence_get(struct hl_fence *fence)
162 kref_get(&fence->refcount);
165 static void hl_fence_init(struct hl_fence *fence, u64 sequence)
167 kref_init(&fence->refcount);
168 fence->cs_sequence = sequence;
170 fence->timestamp = ktime_set(0, 0);
171 init_completion(&fence->completion);
174 void cs_get(struct hl_cs *cs)
176 kref_get(&cs->refcount);
179 static int cs_get_unless_zero(struct hl_cs *cs)
181 return kref_get_unless_zero(&cs->refcount);
184 static void cs_put(struct hl_cs *cs)
186 kref_put(&cs->refcount, cs_do_release);
189 static void cs_job_do_release(struct kref *ref)
191 struct hl_cs_job *job = container_of(ref, struct hl_cs_job, refcount);
196 static void cs_job_put(struct hl_cs_job *job)
198 kref_put(&job->refcount, cs_job_do_release);
201 bool cs_needs_completion(struct hl_cs *cs)
203 /* In case this is a staged CS, only the last CS in sequence should
204 * get a completion, any non staged CS will always get a completion
206 if (cs->staged_cs && !cs->staged_last)
212 bool cs_needs_timeout(struct hl_cs *cs)
214 /* In case this is a staged CS, only the first CS in sequence should
215 * get a timeout, any non staged CS will always get a timeout
217 if (cs->staged_cs && !cs->staged_first)
223 static bool is_cb_patched(struct hl_device *hdev, struct hl_cs_job *job)
226 * Patched CB is created for external queues jobs, and for H/W queues
227 * jobs if the user CB was allocated by driver and MMU is disabled.
229 return (job->queue_type == QUEUE_TYPE_EXT ||
230 (job->queue_type == QUEUE_TYPE_HW &&
231 job->is_kernel_allocated_cb &&
236 * cs_parser - parse the user command submission
238 * @hpriv : pointer to the private data of the fd
239 * @job : pointer to the job that holds the command submission info
241 * The function parses the command submission of the user. It calls the
242 * ASIC specific parser, which returns a list of memory blocks to send
243 * to the device as different command buffers
246 static int cs_parser(struct hl_fpriv *hpriv, struct hl_cs_job *job)
248 struct hl_device *hdev = hpriv->hdev;
249 struct hl_cs_parser parser;
252 parser.ctx_id = job->cs->ctx->asid;
253 parser.cs_sequence = job->cs->sequence;
254 parser.job_id = job->id;
256 parser.hw_queue_id = job->hw_queue_id;
257 parser.job_userptr_list = &job->userptr_list;
258 parser.patched_cb = NULL;
259 parser.user_cb = job->user_cb;
260 parser.user_cb_size = job->user_cb_size;
261 parser.queue_type = job->queue_type;
262 parser.is_kernel_allocated_cb = job->is_kernel_allocated_cb;
263 job->patched_cb = NULL;
264 parser.completion = cs_needs_completion(job->cs);
266 rc = hdev->asic_funcs->cs_parser(hdev, &parser);
268 if (is_cb_patched(hdev, job)) {
270 job->patched_cb = parser.patched_cb;
271 job->job_cb_size = parser.patched_cb_size;
272 job->contains_dma_pkt = parser.contains_dma_pkt;
273 atomic_inc(&job->patched_cb->cs_cnt);
277 * Whether the parsing worked or not, we don't need the
278 * original CB anymore because it was already parsed and
279 * won't be accessed again for this CS
281 atomic_dec(&job->user_cb->cs_cnt);
282 hl_cb_put(job->user_cb);
285 job->job_cb_size = job->user_cb_size;
291 static void complete_job(struct hl_device *hdev, struct hl_cs_job *job)
293 struct hl_cs *cs = job->cs;
295 if (is_cb_patched(hdev, job)) {
296 hl_userptr_delete_list(hdev, &job->userptr_list);
299 * We might arrive here from rollback and patched CB wasn't
300 * created, so we need to check it's not NULL
302 if (job->patched_cb) {
303 atomic_dec(&job->patched_cb->cs_cnt);
304 hl_cb_put(job->patched_cb);
308 /* For H/W queue jobs, if a user CB was allocated by driver and MMU is
309 * enabled, the user CB isn't released in cs_parser() and thus should be
311 * This is also true for INT queues jobs which were allocated by driver
313 if (job->is_kernel_allocated_cb &&
314 ((job->queue_type == QUEUE_TYPE_HW && hdev->mmu_enable) ||
315 job->queue_type == QUEUE_TYPE_INT)) {
316 atomic_dec(&job->user_cb->cs_cnt);
317 hl_cb_put(job->user_cb);
321 * This is the only place where there can be multiple threads
322 * modifying the list at the same time
324 spin_lock(&cs->job_lock);
325 list_del(&job->cs_node);
326 spin_unlock(&cs->job_lock);
328 hl_debugfs_remove_job(hdev, job);
330 /* We decrement reference only for a CS that gets completion
331 * because the reference was incremented only for this kind of CS
332 * right before it was scheduled.
334 * In staged submission, only the last CS marked as 'staged_last'
335 * gets completion, hence its release function will be called from here.
336 * As for all the rest CS's in the staged submission which do not get
337 * completion, their CS reference will be decremented by the
338 * 'staged_last' CS during the CS release flow.
339 * All relevant PQ CI counters will be incremented during the CS release
340 * flow by calling 'hl_hw_queue_update_ci'.
342 if (cs_needs_completion(cs) &&
343 (job->queue_type == QUEUE_TYPE_EXT ||
344 job->queue_type == QUEUE_TYPE_HW))
351 * hl_staged_cs_find_first - locate the first CS in this staged submission
353 * @hdev: pointer to device structure
354 * @cs_seq: staged submission sequence number
356 * @note: This function must be called under 'hdev->cs_mirror_lock'
358 * Find and return a CS pointer with the given sequence
360 struct hl_cs *hl_staged_cs_find_first(struct hl_device *hdev, u64 cs_seq)
364 list_for_each_entry_reverse(cs, &hdev->cs_mirror_list, mirror_node)
365 if (cs->staged_cs && cs->staged_first &&
366 cs->sequence == cs_seq)
373 * is_staged_cs_last_exists - returns true if the last CS in sequence exists
375 * @hdev: pointer to device structure
376 * @cs: staged submission member
379 bool is_staged_cs_last_exists(struct hl_device *hdev, struct hl_cs *cs)
381 struct hl_cs *last_entry;
383 last_entry = list_last_entry(&cs->staged_cs_node, struct hl_cs,
386 if (last_entry->staged_last)
393 * staged_cs_get - get CS reference if this CS is a part of a staged CS
395 * @hdev: pointer to device structure
397 * @cs_seq: staged submission sequence number
399 * Increment CS reference for every CS in this staged submission except for
400 * the CS which get completion.
402 static void staged_cs_get(struct hl_device *hdev, struct hl_cs *cs)
404 /* Only the last CS in this staged submission will get a completion.
405 * We must increment the reference for all other CS's in this
407 * Once we get a completion we will release the whole staged submission.
409 if (!cs->staged_last)
414 * staged_cs_put - put a CS in case it is part of staged submission
416 * @hdev: pointer to device structure
419 * This function decrements a CS reference (for a non completion CS)
421 static void staged_cs_put(struct hl_device *hdev, struct hl_cs *cs)
423 /* We release all CS's in a staged submission except the last
424 * CS which we have never incremented its reference.
426 if (!cs_needs_completion(cs))
430 static void cs_handle_tdr(struct hl_device *hdev, struct hl_cs *cs)
432 bool next_entry_found = false;
435 if (!cs_needs_timeout(cs))
438 spin_lock(&hdev->cs_mirror_lock);
440 /* We need to handle tdr only once for the complete staged submission.
441 * Hence, we choose the CS that reaches this function first which is
442 * the CS marked as 'staged_last'.
444 if (cs->staged_cs && cs->staged_last)
445 cs = hl_staged_cs_find_first(hdev, cs->staged_sequence);
447 spin_unlock(&hdev->cs_mirror_lock);
449 /* Don't cancel TDR in case this CS was timedout because we might be
450 * running from the TDR context
452 if (cs && (cs->timedout ||
453 hdev->timeout_jiffies == MAX_SCHEDULE_TIMEOUT))
456 if (cs && cs->tdr_active)
457 cancel_delayed_work_sync(&cs->work_tdr);
459 spin_lock(&hdev->cs_mirror_lock);
461 /* queue TDR for next CS */
462 list_for_each_entry(next, &hdev->cs_mirror_list, mirror_node)
463 if (cs_needs_timeout(next)) {
464 next_entry_found = true;
468 if (next_entry_found && !next->tdr_active) {
469 next->tdr_active = true;
470 schedule_delayed_work(&next->work_tdr, next->timeout_jiffies);
473 spin_unlock(&hdev->cs_mirror_lock);
476 static void cs_do_release(struct kref *ref)
478 struct hl_cs *cs = container_of(ref, struct hl_cs, refcount);
479 struct hl_device *hdev = cs->ctx->hdev;
480 struct hl_cs_job *job, *tmp;
482 cs->completed = true;
485 * Although if we reached here it means that all external jobs have
486 * finished, because each one of them took refcnt to CS, we still
487 * need to go over the internal jobs and complete them. Otherwise, we
488 * will have leaked memory and what's worse, the CS object (and
489 * potentially the CTX object) could be released, while the JOB
490 * still holds a pointer to them (but no reference).
492 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
493 complete_job(hdev, job);
495 if (!cs->submitted) {
496 /* In case the wait for signal CS was submitted, the put occurs
497 * in init_signal_wait_cs() or collective_wait_init_cs()
498 * right before hanging on the PQ.
500 if (cs->type == CS_TYPE_WAIT ||
501 cs->type == CS_TYPE_COLLECTIVE_WAIT)
502 hl_fence_put(cs->signal_fence);
507 /* Need to update CI for all queue jobs that does not get completion */
508 hl_hw_queue_update_ci(cs);
510 /* remove CS from CS mirror list */
511 spin_lock(&hdev->cs_mirror_lock);
512 list_del_init(&cs->mirror_node);
513 spin_unlock(&hdev->cs_mirror_lock);
515 cs_handle_tdr(hdev, cs);
518 /* the completion CS decrements reference for the entire
521 if (cs->staged_last) {
522 struct hl_cs *staged_cs, *tmp;
524 list_for_each_entry_safe(staged_cs, tmp,
525 &cs->staged_cs_node, staged_cs_node)
526 staged_cs_put(hdev, staged_cs);
529 /* A staged CS will be a member in the list only after it
530 * was submitted. We used 'cs_mirror_lock' when inserting
531 * it to list so we will use it again when removing it
534 spin_lock(&hdev->cs_mirror_lock);
535 list_del(&cs->staged_cs_node);
536 spin_unlock(&hdev->cs_mirror_lock);
541 /* Must be called before hl_ctx_put because inside we use ctx to get
544 hl_debugfs_remove_cs(cs);
548 /* We need to mark an error for not submitted because in that case
549 * the hl fence release flow is different. Mainly, we don't need
550 * to handle hw_sob for signal/wait
553 cs->fence->error = -ETIMEDOUT;
554 else if (cs->aborted)
555 cs->fence->error = -EIO;
556 else if (!cs->submitted)
557 cs->fence->error = -EBUSY;
560 cs->fence->timestamp = ktime_get();
561 complete_all(&cs->fence->completion);
562 hl_fence_put(cs->fence);
564 kfree(cs->jobs_in_queue_cnt);
568 static void cs_timedout(struct work_struct *work)
570 struct hl_device *hdev;
572 struct hl_cs *cs = container_of(work, struct hl_cs,
574 rc = cs_get_unless_zero(cs);
578 if ((!cs->submitted) || (cs->completed)) {
583 /* Mark the CS is timed out so we won't try to cancel its TDR */
586 hdev = cs->ctx->hdev;
591 "Signal command submission %llu has not finished in time!\n",
597 "Wait command submission %llu has not finished in time!\n",
601 case CS_TYPE_COLLECTIVE_WAIT:
603 "Collective Wait command submission %llu has not finished in time!\n",
609 "Command submission %llu has not finished in time!\n",
616 if (hdev->reset_on_lockup)
617 hl_device_reset(hdev, 0);
619 hdev->needs_reset = true;
622 static int allocate_cs(struct hl_device *hdev, struct hl_ctx *ctx,
623 enum hl_cs_type cs_type, u64 user_sequence,
624 struct hl_cs **cs_new, u32 flags, u32 timeout)
626 struct hl_cs_counters_atomic *cntr;
627 struct hl_fence *other = NULL;
628 struct hl_cs_compl *cs_cmpl;
632 cntr = &hdev->aggregated_cs_counters;
634 cs = kzalloc(sizeof(*cs), GFP_ATOMIC);
636 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
639 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
640 atomic64_inc(&cntr->out_of_mem_drop_cnt);
644 /* increment refcnt for context */
645 hl_ctx_get(hdev, ctx);
648 cs->submitted = false;
649 cs->completed = false;
651 cs->timestamp = !!(flags & HL_CS_FLAGS_TIMESTAMP);
652 cs->timeout_jiffies = timeout;
653 INIT_LIST_HEAD(&cs->job_list);
654 INIT_DELAYED_WORK(&cs->work_tdr, cs_timedout);
655 kref_init(&cs->refcount);
656 spin_lock_init(&cs->job_lock);
658 cs_cmpl = kmalloc(sizeof(*cs_cmpl), GFP_ATOMIC);
660 cs_cmpl = kmalloc(sizeof(*cs_cmpl), GFP_KERNEL);
663 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
664 atomic64_inc(&cntr->out_of_mem_drop_cnt);
669 cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
670 sizeof(*cs->jobs_in_queue_cnt), GFP_ATOMIC);
671 if (!cs->jobs_in_queue_cnt)
672 cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
673 sizeof(*cs->jobs_in_queue_cnt), GFP_KERNEL);
675 if (!cs->jobs_in_queue_cnt) {
676 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
677 atomic64_inc(&cntr->out_of_mem_drop_cnt);
682 cs_cmpl->hdev = hdev;
683 cs_cmpl->type = cs->type;
684 spin_lock_init(&cs_cmpl->lock);
685 INIT_WORK(&cs_cmpl->sob_reset_work, sob_reset_work);
686 cs->fence = &cs_cmpl->base_fence;
688 spin_lock(&ctx->cs_lock);
690 cs_cmpl->cs_seq = ctx->cs_sequence;
691 other = ctx->cs_pending[cs_cmpl->cs_seq &
692 (hdev->asic_prop.max_pending_cs - 1)];
694 if (other && !completion_done(&other->completion)) {
695 /* If the following statement is true, it means we have reached
696 * a point in which only part of the staged submission was
697 * submitted and we don't have enough room in the 'cs_pending'
698 * array for the rest of the submission.
699 * This causes a deadlock because this CS will never be
700 * completed as it depends on future CS's for completion.
702 if (other->cs_sequence == user_sequence)
703 dev_crit_ratelimited(hdev->dev,
704 "Staged CS %llu deadlock due to lack of resources",
707 dev_dbg_ratelimited(hdev->dev,
708 "Rejecting CS because of too many in-flights CS\n");
709 atomic64_inc(&ctx->cs_counters.max_cs_in_flight_drop_cnt);
710 atomic64_inc(&cntr->max_cs_in_flight_drop_cnt);
716 hl_fence_init(&cs_cmpl->base_fence, cs_cmpl->cs_seq);
718 cs->sequence = cs_cmpl->cs_seq;
720 ctx->cs_pending[cs_cmpl->cs_seq &
721 (hdev->asic_prop.max_pending_cs - 1)] =
722 &cs_cmpl->base_fence;
725 hl_fence_get(&cs_cmpl->base_fence);
729 spin_unlock(&ctx->cs_lock);
736 spin_unlock(&ctx->cs_lock);
737 kfree(cs->jobs_in_queue_cnt);
746 static void cs_rollback(struct hl_device *hdev, struct hl_cs *cs)
748 struct hl_cs_job *job, *tmp;
750 staged_cs_put(hdev, cs);
752 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
753 complete_job(hdev, job);
756 void hl_cs_rollback_all(struct hl_device *hdev)
759 struct hl_cs *cs, *tmp;
761 flush_workqueue(hdev->sob_reset_wq);
763 /* flush all completions before iterating over the CS mirror list in
764 * order to avoid a race with the release functions
766 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
767 flush_workqueue(hdev->cq_wq[i]);
769 /* Make sure we don't have leftovers in the CS mirror list */
770 list_for_each_entry_safe(cs, tmp, &hdev->cs_mirror_list, mirror_node) {
773 dev_warn_ratelimited(hdev->dev, "Killing CS %d.%llu\n",
774 cs->ctx->asid, cs->sequence);
775 cs_rollback(hdev, cs);
780 void hl_pending_cb_list_flush(struct hl_ctx *ctx)
782 struct hl_pending_cb *pending_cb, *tmp;
784 list_for_each_entry_safe(pending_cb, tmp,
785 &ctx->pending_cb_list, cb_node) {
786 list_del(&pending_cb->cb_node);
787 hl_cb_put(pending_cb->cb);
793 wake_pending_user_interrupt_threads(struct hl_user_interrupt *interrupt)
795 struct hl_user_pending_interrupt *pend;
797 spin_lock(&interrupt->wait_list_lock);
798 list_for_each_entry(pend, &interrupt->wait_list_head, wait_list_node) {
799 pend->fence.error = -EIO;
800 complete_all(&pend->fence.completion);
802 spin_unlock(&interrupt->wait_list_lock);
805 void hl_release_pending_user_interrupts(struct hl_device *hdev)
807 struct asic_fixed_properties *prop = &hdev->asic_prop;
808 struct hl_user_interrupt *interrupt;
811 if (!prop->user_interrupt_count)
814 /* We iterate through the user interrupt requests and waking up all
815 * user threads waiting for interrupt completion. We iterate the
816 * list under a lock, this is why all user threads, once awake,
817 * will wait on the same lock and will release the waiting object upon
821 for (i = 0 ; i < prop->user_interrupt_count ; i++) {
822 interrupt = &hdev->user_interrupt[i];
823 wake_pending_user_interrupt_threads(interrupt);
826 interrupt = &hdev->common_user_interrupt;
827 wake_pending_user_interrupt_threads(interrupt);
830 static void job_wq_completion(struct work_struct *work)
832 struct hl_cs_job *job = container_of(work, struct hl_cs_job,
834 struct hl_cs *cs = job->cs;
835 struct hl_device *hdev = cs->ctx->hdev;
837 /* job is no longer needed */
838 complete_job(hdev, job);
841 static int validate_queue_index(struct hl_device *hdev,
842 struct hl_cs_chunk *chunk,
843 enum hl_queue_type *queue_type,
844 bool *is_kernel_allocated_cb)
846 struct asic_fixed_properties *asic = &hdev->asic_prop;
847 struct hw_queue_properties *hw_queue_prop;
849 /* This must be checked here to prevent out-of-bounds access to
850 * hw_queues_props array
852 if (chunk->queue_index >= asic->max_queues) {
853 dev_err(hdev->dev, "Queue index %d is invalid\n",
858 hw_queue_prop = &asic->hw_queues_props[chunk->queue_index];
860 if (hw_queue_prop->type == QUEUE_TYPE_NA) {
861 dev_err(hdev->dev, "Queue index %d is invalid\n",
866 if (hw_queue_prop->driver_only) {
868 "Queue index %d is restricted for the kernel driver\n",
873 /* When hw queue type isn't QUEUE_TYPE_HW,
874 * USER_ALLOC_CB flag shall be referred as "don't care".
876 if (hw_queue_prop->type == QUEUE_TYPE_HW) {
877 if (chunk->cs_chunk_flags & HL_CS_CHUNK_FLAGS_USER_ALLOC_CB) {
878 if (!(hw_queue_prop->cb_alloc_flags & CB_ALLOC_USER)) {
880 "Queue index %d doesn't support user CB\n",
885 *is_kernel_allocated_cb = false;
887 if (!(hw_queue_prop->cb_alloc_flags &
890 "Queue index %d doesn't support kernel CB\n",
895 *is_kernel_allocated_cb = true;
898 *is_kernel_allocated_cb = !!(hw_queue_prop->cb_alloc_flags
902 *queue_type = hw_queue_prop->type;
906 static struct hl_cb *get_cb_from_cs_chunk(struct hl_device *hdev,
907 struct hl_cb_mgr *cb_mgr,
908 struct hl_cs_chunk *chunk)
913 cb_handle = (u32) (chunk->cb_handle >> PAGE_SHIFT);
915 cb = hl_cb_get(hdev, cb_mgr, cb_handle);
917 dev_err(hdev->dev, "CB handle 0x%x invalid\n", cb_handle);
921 if ((chunk->cb_size < 8) || (chunk->cb_size > cb->size)) {
922 dev_err(hdev->dev, "CB size %u invalid\n", chunk->cb_size);
926 atomic_inc(&cb->cs_cnt);
935 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
936 enum hl_queue_type queue_type, bool is_kernel_allocated_cb)
938 struct hl_cs_job *job;
940 job = kzalloc(sizeof(*job), GFP_ATOMIC);
942 job = kzalloc(sizeof(*job), GFP_KERNEL);
947 kref_init(&job->refcount);
948 job->queue_type = queue_type;
949 job->is_kernel_allocated_cb = is_kernel_allocated_cb;
951 if (is_cb_patched(hdev, job))
952 INIT_LIST_HEAD(&job->userptr_list);
954 if (job->queue_type == QUEUE_TYPE_EXT)
955 INIT_WORK(&job->finish_work, job_wq_completion);
960 static enum hl_cs_type hl_cs_get_cs_type(u32 cs_type_flags)
962 if (cs_type_flags & HL_CS_FLAGS_SIGNAL)
963 return CS_TYPE_SIGNAL;
964 else if (cs_type_flags & HL_CS_FLAGS_WAIT)
966 else if (cs_type_flags & HL_CS_FLAGS_COLLECTIVE_WAIT)
967 return CS_TYPE_COLLECTIVE_WAIT;
969 return CS_TYPE_DEFAULT;
972 static int hl_cs_sanity_checks(struct hl_fpriv *hpriv, union hl_cs_args *args)
974 struct hl_device *hdev = hpriv->hdev;
975 struct hl_ctx *ctx = hpriv->ctx;
976 u32 cs_type_flags, num_chunks;
977 enum hl_device_status status;
978 enum hl_cs_type cs_type;
980 if (!hl_device_operational(hdev, &status)) {
981 dev_warn_ratelimited(hdev->dev,
982 "Device is %s. Can't submit new CS\n",
983 hdev->status[status]);
987 if ((args->in.cs_flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
988 !hdev->supports_staged_submission) {
989 dev_err(hdev->dev, "staged submission not supported");
993 cs_type_flags = args->in.cs_flags & HL_CS_FLAGS_TYPE_MASK;
995 if (unlikely(cs_type_flags && !is_power_of_2(cs_type_flags))) {
997 "CS type flags are mutually exclusive, context %d\n",
1002 cs_type = hl_cs_get_cs_type(cs_type_flags);
1003 num_chunks = args->in.num_chunks_execute;
1005 if (unlikely((cs_type != CS_TYPE_DEFAULT) &&
1006 !hdev->supports_sync_stream)) {
1007 dev_err(hdev->dev, "Sync stream CS is not supported\n");
1011 if (cs_type == CS_TYPE_DEFAULT) {
1014 "Got execute CS with 0 chunks, context %d\n",
1018 } else if (num_chunks != 1) {
1020 "Sync stream CS mandates one chunk only, context %d\n",
1028 static int hl_cs_copy_chunk_array(struct hl_device *hdev,
1029 struct hl_cs_chunk **cs_chunk_array,
1030 void __user *chunks, u32 num_chunks,
1035 if (num_chunks > HL_MAX_JOBS_PER_CS) {
1036 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1037 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1039 "Number of chunks can NOT be larger than %d\n",
1040 HL_MAX_JOBS_PER_CS);
1044 *cs_chunk_array = kmalloc_array(num_chunks, sizeof(**cs_chunk_array),
1046 if (!*cs_chunk_array)
1047 *cs_chunk_array = kmalloc_array(num_chunks,
1048 sizeof(**cs_chunk_array), GFP_KERNEL);
1049 if (!*cs_chunk_array) {
1050 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1051 atomic64_inc(&hdev->aggregated_cs_counters.out_of_mem_drop_cnt);
1055 size_to_copy = num_chunks * sizeof(struct hl_cs_chunk);
1056 if (copy_from_user(*cs_chunk_array, chunks, size_to_copy)) {
1057 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1058 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1059 dev_err(hdev->dev, "Failed to copy cs chunk array from user\n");
1060 kfree(*cs_chunk_array);
1067 static int cs_staged_submission(struct hl_device *hdev, struct hl_cs *cs,
1068 u64 sequence, u32 flags)
1070 if (!(flags & HL_CS_FLAGS_STAGED_SUBMISSION))
1073 cs->staged_last = !!(flags & HL_CS_FLAGS_STAGED_SUBMISSION_LAST);
1074 cs->staged_first = !!(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST);
1076 if (cs->staged_first) {
1077 /* Staged CS sequence is the first CS sequence */
1078 INIT_LIST_HEAD(&cs->staged_cs_node);
1079 cs->staged_sequence = cs->sequence;
1081 /* User sequence will be validated in 'hl_hw_queue_schedule_cs'
1082 * under the cs_mirror_lock
1084 cs->staged_sequence = sequence;
1087 /* Increment CS reference if needed */
1088 staged_cs_get(hdev, cs);
1090 cs->staged_cs = true;
1095 static int cs_ioctl_default(struct hl_fpriv *hpriv, void __user *chunks,
1096 u32 num_chunks, u64 *cs_seq, u32 flags,
1099 bool staged_mid, int_queues_only = true;
1100 struct hl_device *hdev = hpriv->hdev;
1101 struct hl_cs_chunk *cs_chunk_array;
1102 struct hl_cs_counters_atomic *cntr;
1103 struct hl_ctx *ctx = hpriv->ctx;
1104 struct hl_cs_job *job;
1110 cntr = &hdev->aggregated_cs_counters;
1111 user_sequence = *cs_seq;
1112 *cs_seq = ULLONG_MAX;
1114 rc = hl_cs_copy_chunk_array(hdev, &cs_chunk_array, chunks, num_chunks,
1119 if ((flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
1120 !(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST))
1125 rc = allocate_cs(hdev, hpriv->ctx, CS_TYPE_DEFAULT,
1126 staged_mid ? user_sequence : ULLONG_MAX, &cs, flags,
1129 goto free_cs_chunk_array;
1131 *cs_seq = cs->sequence;
1133 hl_debugfs_add_cs(cs);
1135 rc = cs_staged_submission(hdev, cs, user_sequence, flags);
1137 goto free_cs_object;
1139 /* Validate ALL the CS chunks before submitting the CS */
1140 for (i = 0 ; i < num_chunks ; i++) {
1141 struct hl_cs_chunk *chunk = &cs_chunk_array[i];
1142 enum hl_queue_type queue_type;
1143 bool is_kernel_allocated_cb;
1145 rc = validate_queue_index(hdev, chunk, &queue_type,
1146 &is_kernel_allocated_cb);
1148 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1149 atomic64_inc(&cntr->validation_drop_cnt);
1150 goto free_cs_object;
1153 if (is_kernel_allocated_cb) {
1154 cb = get_cb_from_cs_chunk(hdev, &hpriv->cb_mgr, chunk);
1157 &ctx->cs_counters.validation_drop_cnt);
1158 atomic64_inc(&cntr->validation_drop_cnt);
1160 goto free_cs_object;
1163 cb = (struct hl_cb *) (uintptr_t) chunk->cb_handle;
1166 if (queue_type == QUEUE_TYPE_EXT || queue_type == QUEUE_TYPE_HW)
1167 int_queues_only = false;
1169 job = hl_cs_allocate_job(hdev, queue_type,
1170 is_kernel_allocated_cb);
1172 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1173 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1174 dev_err(hdev->dev, "Failed to allocate a new job\n");
1176 if (is_kernel_allocated_cb)
1179 goto free_cs_object;
1185 job->user_cb_size = chunk->cb_size;
1186 job->hw_queue_id = chunk->queue_index;
1188 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1190 list_add_tail(&job->cs_node, &cs->job_list);
1193 * Increment CS reference. When CS reference is 0, CS is
1194 * done and can be signaled to user and free all its resources
1195 * Only increment for JOB on external or H/W queues, because
1196 * only for those JOBs we get completion
1198 if (cs_needs_completion(cs) &&
1199 (job->queue_type == QUEUE_TYPE_EXT ||
1200 job->queue_type == QUEUE_TYPE_HW))
1203 hl_debugfs_add_job(hdev, job);
1205 rc = cs_parser(hpriv, job);
1207 atomic64_inc(&ctx->cs_counters.parsing_drop_cnt);
1208 atomic64_inc(&cntr->parsing_drop_cnt);
1210 "Failed to parse JOB %d.%llu.%d, err %d, rejecting the CS\n",
1211 cs->ctx->asid, cs->sequence, job->id, rc);
1212 goto free_cs_object;
1216 /* We allow a CS with any queue type combination as long as it does
1217 * not get a completion
1219 if (int_queues_only && cs_needs_completion(cs)) {
1220 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1221 atomic64_inc(&cntr->validation_drop_cnt);
1223 "Reject CS %d.%llu since it contains only internal queues jobs and needs completion\n",
1224 cs->ctx->asid, cs->sequence);
1226 goto free_cs_object;
1229 rc = hl_hw_queue_schedule_cs(cs);
1233 "Failed to submit CS %d.%llu to H/W queues, error %d\n",
1234 cs->ctx->asid, cs->sequence, rc);
1235 goto free_cs_object;
1238 rc = HL_CS_STATUS_SUCCESS;
1242 atomic_dec(&cb->cs_cnt);
1245 cs_rollback(hdev, cs);
1246 *cs_seq = ULLONG_MAX;
1247 /* The path below is both for good and erroneous exits */
1249 /* We finished with the CS in this function, so put the ref */
1251 free_cs_chunk_array:
1252 kfree(cs_chunk_array);
1257 static int pending_cb_create_job(struct hl_device *hdev, struct hl_ctx *ctx,
1258 struct hl_cs *cs, struct hl_cb *cb, u32 size, u32 hw_queue_id)
1260 struct hw_queue_properties *hw_queue_prop;
1261 struct hl_cs_counters_atomic *cntr;
1262 struct hl_cs_job *job;
1264 hw_queue_prop = &hdev->asic_prop.hw_queues_props[hw_queue_id];
1265 cntr = &hdev->aggregated_cs_counters;
1267 job = hl_cs_allocate_job(hdev, hw_queue_prop->type, true);
1269 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1270 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1271 dev_err(hdev->dev, "Failed to allocate a new job\n");
1278 atomic_inc(&job->user_cb->cs_cnt);
1279 job->user_cb_size = size;
1280 job->hw_queue_id = hw_queue_id;
1281 job->patched_cb = job->user_cb;
1282 job->job_cb_size = job->user_cb_size;
1284 /* increment refcount as for external queues we get completion */
1287 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1289 list_add_tail(&job->cs_node, &cs->job_list);
1291 hl_debugfs_add_job(hdev, job);
1296 static int hl_submit_pending_cb(struct hl_fpriv *hpriv)
1298 struct hl_device *hdev = hpriv->hdev;
1299 struct hl_ctx *ctx = hpriv->ctx;
1300 struct hl_pending_cb *pending_cb, *tmp;
1301 struct list_head local_cb_list;
1306 int process_list, rc = 0;
1308 if (list_empty(&ctx->pending_cb_list))
1311 process_list = atomic_cmpxchg(&ctx->thread_pending_cb_token, 1, 0);
1313 /* Only a single thread is allowed to process the list */
1317 if (list_empty(&ctx->pending_cb_list))
1318 goto free_pending_cb_token;
1320 /* move all list elements to a local list */
1321 INIT_LIST_HEAD(&local_cb_list);
1322 spin_lock(&ctx->pending_cb_lock);
1323 list_for_each_entry_safe(pending_cb, tmp, &ctx->pending_cb_list,
1325 list_move_tail(&pending_cb->cb_node, &local_cb_list);
1326 spin_unlock(&ctx->pending_cb_lock);
1328 rc = allocate_cs(hdev, ctx, CS_TYPE_DEFAULT, ULLONG_MAX, &cs, 0,
1329 hdev->timeout_jiffies);
1331 goto add_list_elements;
1333 hl_debugfs_add_cs(cs);
1335 /* Iterate through pending cb list, create jobs and add to CS */
1336 list_for_each_entry(pending_cb, &local_cb_list, cb_node) {
1337 cb = pending_cb->cb;
1338 cb_size = pending_cb->cb_size;
1339 hw_queue_id = pending_cb->hw_queue_id;
1341 rc = pending_cb_create_job(hdev, ctx, cs, cb, cb_size,
1344 goto free_cs_object;
1347 rc = hl_hw_queue_schedule_cs(cs);
1351 "Failed to submit CS %d.%llu (%d)\n",
1352 ctx->asid, cs->sequence, rc);
1353 goto free_cs_object;
1356 /* pending cb was scheduled successfully */
1357 list_for_each_entry_safe(pending_cb, tmp, &local_cb_list, cb_node) {
1358 list_del(&pending_cb->cb_node);
1364 goto free_pending_cb_token;
1367 cs_rollback(hdev, cs);
1370 spin_lock(&ctx->pending_cb_lock);
1371 list_for_each_entry_safe_reverse(pending_cb, tmp, &local_cb_list,
1373 list_move(&pending_cb->cb_node, &ctx->pending_cb_list);
1374 spin_unlock(&ctx->pending_cb_lock);
1375 free_pending_cb_token:
1376 atomic_set(&ctx->thread_pending_cb_token, 1);
1381 static int hl_cs_ctx_switch(struct hl_fpriv *hpriv, union hl_cs_args *args,
1384 struct hl_device *hdev = hpriv->hdev;
1385 struct hl_ctx *ctx = hpriv->ctx;
1386 bool need_soft_reset = false;
1387 int rc = 0, do_ctx_switch;
1388 void __user *chunks;
1389 u32 num_chunks, tmp;
1392 do_ctx_switch = atomic_cmpxchg(&ctx->thread_ctx_switch_token, 1, 0);
1394 if (do_ctx_switch || (args->in.cs_flags & HL_CS_FLAGS_FORCE_RESTORE)) {
1395 mutex_lock(&hpriv->restore_phase_mutex);
1397 if (do_ctx_switch) {
1398 rc = hdev->asic_funcs->context_switch(hdev, ctx->asid);
1400 dev_err_ratelimited(hdev->dev,
1401 "Failed to switch to context %d, rejecting CS! %d\n",
1404 * If we timedout, or if the device is not IDLE
1405 * while we want to do context-switch (-EBUSY),
1406 * we need to soft-reset because QMAN is
1407 * probably stuck. However, we can't call to
1408 * reset here directly because of deadlock, so
1409 * need to do it at the very end of this
1412 if ((rc == -ETIMEDOUT) || (rc == -EBUSY))
1413 need_soft_reset = true;
1414 mutex_unlock(&hpriv->restore_phase_mutex);
1419 hdev->asic_funcs->restore_phase_topology(hdev);
1421 chunks = (void __user *) (uintptr_t) args->in.chunks_restore;
1422 num_chunks = args->in.num_chunks_restore;
1426 "Need to run restore phase but restore CS is empty\n");
1429 rc = cs_ioctl_default(hpriv, chunks, num_chunks,
1430 cs_seq, 0, hdev->timeout_jiffies);
1433 mutex_unlock(&hpriv->restore_phase_mutex);
1437 "Failed to submit restore CS for context %d (%d)\n",
1442 /* Need to wait for restore completion before execution phase */
1444 enum hl_cs_wait_status status;
1446 ret = _hl_cs_wait_ioctl(hdev, ctx,
1447 jiffies_to_usecs(hdev->timeout_jiffies),
1448 *cs_seq, &status, NULL);
1450 if (ret == -ERESTARTSYS) {
1451 usleep_range(100, 200);
1456 "Restore CS for context %d failed to complete %d\n",
1463 ctx->thread_ctx_switch_wait_token = 1;
1465 } else if (!ctx->thread_ctx_switch_wait_token) {
1466 rc = hl_poll_timeout_memory(hdev,
1467 &ctx->thread_ctx_switch_wait_token, tmp, (tmp == 1),
1468 100, jiffies_to_usecs(hdev->timeout_jiffies), false);
1470 if (rc == -ETIMEDOUT) {
1472 "context switch phase timeout (%d)\n", tmp);
1478 if ((rc == -ETIMEDOUT || rc == -EBUSY) && (need_soft_reset))
1479 hl_device_reset(hdev, 0);
1484 static int cs_ioctl_extract_signal_seq(struct hl_device *hdev,
1485 struct hl_cs_chunk *chunk, u64 *signal_seq, struct hl_ctx *ctx)
1487 u64 *signal_seq_arr = NULL;
1488 u32 size_to_copy, signal_seq_arr_len;
1491 signal_seq_arr_len = chunk->num_signal_seq_arr;
1493 /* currently only one signal seq is supported */
1494 if (signal_seq_arr_len != 1) {
1495 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1496 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1498 "Wait for signal CS supports only one signal CS seq\n");
1502 signal_seq_arr = kmalloc_array(signal_seq_arr_len,
1503 sizeof(*signal_seq_arr),
1505 if (!signal_seq_arr)
1506 signal_seq_arr = kmalloc_array(signal_seq_arr_len,
1507 sizeof(*signal_seq_arr),
1509 if (!signal_seq_arr) {
1510 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1511 atomic64_inc(&hdev->aggregated_cs_counters.out_of_mem_drop_cnt);
1515 size_to_copy = chunk->num_signal_seq_arr * sizeof(*signal_seq_arr);
1516 if (copy_from_user(signal_seq_arr,
1517 u64_to_user_ptr(chunk->signal_seq_arr),
1519 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1520 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1522 "Failed to copy signal seq array from user\n");
1527 /* currently it is guaranteed to have only one signal seq */
1528 *signal_seq = signal_seq_arr[0];
1531 kfree(signal_seq_arr);
1536 static int cs_ioctl_signal_wait_create_jobs(struct hl_device *hdev,
1537 struct hl_ctx *ctx, struct hl_cs *cs, enum hl_queue_type q_type,
1540 struct hl_cs_counters_atomic *cntr;
1541 struct hl_cs_job *job;
1545 cntr = &hdev->aggregated_cs_counters;
1547 job = hl_cs_allocate_job(hdev, q_type, true);
1549 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1550 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1551 dev_err(hdev->dev, "Failed to allocate a new job\n");
1555 if (cs->type == CS_TYPE_WAIT)
1556 cb_size = hdev->asic_funcs->get_wait_cb_size(hdev);
1558 cb_size = hdev->asic_funcs->get_signal_cb_size(hdev);
1560 cb = hl_cb_kernel_create(hdev, cb_size,
1561 q_type == QUEUE_TYPE_HW && hdev->mmu_enable);
1563 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1564 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1572 atomic_inc(&job->user_cb->cs_cnt);
1573 job->user_cb_size = cb_size;
1574 job->hw_queue_id = q_idx;
1577 * No need in parsing, user CB is the patched CB.
1578 * We call hl_cb_destroy() out of two reasons - we don't need the CB in
1579 * the CB idr anymore and to decrement its refcount as it was
1580 * incremented inside hl_cb_kernel_create().
1582 job->patched_cb = job->user_cb;
1583 job->job_cb_size = job->user_cb_size;
1584 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
1586 /* increment refcount as for external queues we get completion */
1589 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1591 list_add_tail(&job->cs_node, &cs->job_list);
1593 hl_debugfs_add_job(hdev, job);
1598 static int cs_ioctl_signal_wait(struct hl_fpriv *hpriv, enum hl_cs_type cs_type,
1599 void __user *chunks, u32 num_chunks,
1600 u64 *cs_seq, u32 flags, u32 timeout)
1602 struct hl_cs_chunk *cs_chunk_array, *chunk;
1603 struct hw_queue_properties *hw_queue_prop;
1604 struct hl_device *hdev = hpriv->hdev;
1605 struct hl_cs_compl *sig_waitcs_cmpl;
1606 u32 q_idx, collective_engine_id = 0;
1607 struct hl_cs_counters_atomic *cntr;
1608 struct hl_fence *sig_fence = NULL;
1609 struct hl_ctx *ctx = hpriv->ctx;
1610 enum hl_queue_type q_type;
1615 cntr = &hdev->aggregated_cs_counters;
1616 *cs_seq = ULLONG_MAX;
1618 rc = hl_cs_copy_chunk_array(hdev, &cs_chunk_array, chunks, num_chunks,
1623 /* currently it is guaranteed to have only one chunk */
1624 chunk = &cs_chunk_array[0];
1626 if (chunk->queue_index >= hdev->asic_prop.max_queues) {
1627 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1628 atomic64_inc(&cntr->validation_drop_cnt);
1629 dev_err(hdev->dev, "Queue index %d is invalid\n",
1630 chunk->queue_index);
1632 goto free_cs_chunk_array;
1635 q_idx = chunk->queue_index;
1636 hw_queue_prop = &hdev->asic_prop.hw_queues_props[q_idx];
1637 q_type = hw_queue_prop->type;
1639 if (!hw_queue_prop->supports_sync_stream) {
1640 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1641 atomic64_inc(&cntr->validation_drop_cnt);
1643 "Queue index %d does not support sync stream operations\n",
1646 goto free_cs_chunk_array;
1649 if (cs_type == CS_TYPE_COLLECTIVE_WAIT) {
1650 if (!(hw_queue_prop->collective_mode == HL_COLLECTIVE_MASTER)) {
1651 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1652 atomic64_inc(&cntr->validation_drop_cnt);
1654 "Queue index %d is invalid\n", q_idx);
1656 goto free_cs_chunk_array;
1659 collective_engine_id = chunk->collective_engine_id;
1662 if (cs_type == CS_TYPE_WAIT || cs_type == CS_TYPE_COLLECTIVE_WAIT) {
1663 rc = cs_ioctl_extract_signal_seq(hdev, chunk, &signal_seq, ctx);
1665 goto free_cs_chunk_array;
1667 sig_fence = hl_ctx_get_fence(ctx, signal_seq);
1668 if (IS_ERR(sig_fence)) {
1669 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1670 atomic64_inc(&cntr->validation_drop_cnt);
1672 "Failed to get signal CS with seq 0x%llx\n",
1674 rc = PTR_ERR(sig_fence);
1675 goto free_cs_chunk_array;
1679 /* signal CS already finished */
1681 goto free_cs_chunk_array;
1685 container_of(sig_fence, struct hl_cs_compl, base_fence);
1687 if (sig_waitcs_cmpl->type != CS_TYPE_SIGNAL) {
1688 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1689 atomic64_inc(&cntr->validation_drop_cnt);
1691 "CS seq 0x%llx is not of a signal CS\n",
1693 hl_fence_put(sig_fence);
1695 goto free_cs_chunk_array;
1698 if (completion_done(&sig_fence->completion)) {
1699 /* signal CS already finished */
1700 hl_fence_put(sig_fence);
1702 goto free_cs_chunk_array;
1706 rc = allocate_cs(hdev, ctx, cs_type, ULLONG_MAX, &cs, flags, timeout);
1708 if (cs_type == CS_TYPE_WAIT ||
1709 cs_type == CS_TYPE_COLLECTIVE_WAIT)
1710 hl_fence_put(sig_fence);
1711 goto free_cs_chunk_array;
1715 * Save the signal CS fence for later initialization right before
1716 * hanging the wait CS on the queue.
1718 if (cs_type == CS_TYPE_WAIT || cs_type == CS_TYPE_COLLECTIVE_WAIT)
1719 cs->signal_fence = sig_fence;
1721 hl_debugfs_add_cs(cs);
1723 *cs_seq = cs->sequence;
1725 if (cs_type == CS_TYPE_WAIT || cs_type == CS_TYPE_SIGNAL)
1726 rc = cs_ioctl_signal_wait_create_jobs(hdev, ctx, cs, q_type,
1728 else if (cs_type == CS_TYPE_COLLECTIVE_WAIT)
1729 rc = hdev->asic_funcs->collective_wait_create_jobs(hdev, ctx,
1730 cs, q_idx, collective_engine_id);
1732 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1733 atomic64_inc(&cntr->validation_drop_cnt);
1738 goto free_cs_object;
1740 rc = hl_hw_queue_schedule_cs(cs);
1744 "Failed to submit CS %d.%llu to H/W queues, error %d\n",
1745 ctx->asid, cs->sequence, rc);
1746 goto free_cs_object;
1749 rc = HL_CS_STATUS_SUCCESS;
1753 cs_rollback(hdev, cs);
1754 *cs_seq = ULLONG_MAX;
1755 /* The path below is both for good and erroneous exits */
1757 /* We finished with the CS in this function, so put the ref */
1759 free_cs_chunk_array:
1760 kfree(cs_chunk_array);
1765 int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data)
1767 union hl_cs_args *args = data;
1768 enum hl_cs_type cs_type;
1769 u64 cs_seq = ULONG_MAX;
1770 void __user *chunks;
1771 u32 num_chunks, flags, timeout;
1774 rc = hl_cs_sanity_checks(hpriv, args);
1778 rc = hl_cs_ctx_switch(hpriv, args, &cs_seq);
1782 rc = hl_submit_pending_cb(hpriv);
1786 cs_type = hl_cs_get_cs_type(args->in.cs_flags &
1787 ~HL_CS_FLAGS_FORCE_RESTORE);
1788 chunks = (void __user *) (uintptr_t) args->in.chunks_execute;
1789 num_chunks = args->in.num_chunks_execute;
1790 flags = args->in.cs_flags;
1792 /* In case this is a staged CS, user should supply the CS sequence */
1793 if ((flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
1794 !(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST))
1795 cs_seq = args->in.seq;
1797 timeout = flags & HL_CS_FLAGS_CUSTOM_TIMEOUT
1798 ? msecs_to_jiffies(args->in.timeout * 1000)
1799 : hpriv->hdev->timeout_jiffies;
1802 case CS_TYPE_SIGNAL:
1804 case CS_TYPE_COLLECTIVE_WAIT:
1805 rc = cs_ioctl_signal_wait(hpriv, cs_type, chunks, num_chunks,
1806 &cs_seq, args->in.cs_flags, timeout);
1809 rc = cs_ioctl_default(hpriv, chunks, num_chunks, &cs_seq,
1810 args->in.cs_flags, timeout);
1815 if (rc != -EAGAIN) {
1816 memset(args, 0, sizeof(*args));
1817 args->out.status = rc;
1818 args->out.seq = cs_seq;
1824 static int _hl_cs_wait_ioctl(struct hl_device *hdev, struct hl_ctx *ctx,
1825 u64 timeout_us, u64 seq,
1826 enum hl_cs_wait_status *status, s64 *timestamp)
1828 struct hl_fence *fence;
1829 unsigned long timeout;
1836 if (timeout_us == MAX_SCHEDULE_TIMEOUT)
1837 timeout = timeout_us;
1839 timeout = usecs_to_jiffies(timeout_us);
1841 hl_ctx_get(hdev, ctx);
1843 fence = hl_ctx_get_fence(ctx, seq);
1844 if (IS_ERR(fence)) {
1845 rc = PTR_ERR(fence);
1847 dev_notice_ratelimited(hdev->dev,
1848 "Can't wait on CS %llu because current CS is at seq %llu\n",
1849 seq, ctx->cs_sequence);
1852 completion_rc = completion_done(&fence->completion);
1855 wait_for_completion_interruptible_timeout(
1856 &fence->completion, timeout);
1858 if (completion_rc > 0) {
1859 *status = CS_WAIT_STATUS_COMPLETED;
1861 *timestamp = ktime_to_ns(fence->timestamp);
1863 *status = CS_WAIT_STATUS_BUSY;
1866 if (fence->error == -ETIMEDOUT)
1868 else if (fence->error == -EIO)
1871 hl_fence_put(fence);
1874 "Can't wait on seq %llu because current CS is at seq %llu (Fence is gone)\n",
1875 seq, ctx->cs_sequence);
1876 *status = CS_WAIT_STATUS_GONE;
1884 static int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data)
1886 struct hl_device *hdev = hpriv->hdev;
1887 union hl_wait_cs_args *args = data;
1888 enum hl_cs_wait_status status;
1889 u64 seq = args->in.seq;
1893 rc = _hl_cs_wait_ioctl(hdev, hpriv->ctx, args->in.timeout_us, seq,
1894 &status, ×tamp);
1896 memset(args, 0, sizeof(*args));
1899 if (rc == -ERESTARTSYS) {
1900 dev_err_ratelimited(hdev->dev,
1901 "user process got signal while waiting for CS handle %llu\n",
1903 args->out.status = HL_WAIT_CS_STATUS_INTERRUPTED;
1905 } else if (rc == -ETIMEDOUT) {
1906 dev_err_ratelimited(hdev->dev,
1907 "CS %llu has timed-out while user process is waiting for it\n",
1909 args->out.status = HL_WAIT_CS_STATUS_TIMEDOUT;
1910 } else if (rc == -EIO) {
1911 dev_err_ratelimited(hdev->dev,
1912 "CS %llu has been aborted while user process is waiting for it\n",
1914 args->out.status = HL_WAIT_CS_STATUS_ABORTED;
1920 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_TIMESTAMP_VLD;
1921 args->out.timestamp_nsec = timestamp;
1925 case CS_WAIT_STATUS_GONE:
1926 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_GONE;
1928 case CS_WAIT_STATUS_COMPLETED:
1929 args->out.status = HL_WAIT_CS_STATUS_COMPLETED;
1931 case CS_WAIT_STATUS_BUSY:
1933 args->out.status = HL_WAIT_CS_STATUS_BUSY;
1940 static int _hl_interrupt_wait_ioctl(struct hl_device *hdev, struct hl_ctx *ctx,
1941 u32 timeout_us, u64 user_address,
1942 u32 target_value, u16 interrupt_offset,
1943 enum hl_cs_wait_status *status)
1945 struct hl_user_pending_interrupt *pend;
1946 struct hl_user_interrupt *interrupt;
1947 unsigned long timeout;
1949 u32 completion_value;
1952 if (timeout_us == U32_MAX)
1953 timeout = timeout_us;
1955 timeout = usecs_to_jiffies(timeout_us);
1957 hl_ctx_get(hdev, ctx);
1959 pend = kmalloc(sizeof(*pend), GFP_KERNEL);
1965 hl_fence_init(&pend->fence, ULONG_MAX);
1967 if (interrupt_offset == HL_COMMON_USER_INTERRUPT_ID)
1968 interrupt = &hdev->common_user_interrupt;
1970 interrupt = &hdev->user_interrupt[interrupt_offset];
1972 spin_lock(&interrupt->wait_list_lock);
1973 if (!hl_device_operational(hdev, NULL)) {
1975 goto unlock_and_free_fence;
1978 if (copy_from_user(&completion_value, u64_to_user_ptr(user_address), 4)) {
1980 "Failed to copy completion value from user\n");
1982 goto unlock_and_free_fence;
1985 if (completion_value >= target_value)
1986 *status = CS_WAIT_STATUS_COMPLETED;
1988 *status = CS_WAIT_STATUS_BUSY;
1990 if (!timeout_us || (*status == CS_WAIT_STATUS_COMPLETED))
1991 goto unlock_and_free_fence;
1993 /* Add pending user interrupt to relevant list for the interrupt
1994 * handler to monitor
1996 list_add_tail(&pend->wait_list_node, &interrupt->wait_list_head);
1997 spin_unlock(&interrupt->wait_list_lock);
2000 /* Wait for interrupt handler to signal completion */
2002 wait_for_completion_interruptible_timeout(
2003 &pend->fence.completion, timeout);
2005 /* If timeout did not expire we need to perform the comparison.
2006 * If comparison fails, keep waiting until timeout expires
2008 if (completion_rc > 0) {
2009 if (copy_from_user(&completion_value,
2010 u64_to_user_ptr(user_address), 4)) {
2012 "Failed to copy completion value from user\n");
2014 goto remove_pending_user_interrupt;
2017 if (completion_value >= target_value) {
2018 *status = CS_WAIT_STATUS_COMPLETED;
2020 timeout = completion_rc;
2024 *status = CS_WAIT_STATUS_BUSY;
2027 remove_pending_user_interrupt:
2028 spin_lock(&interrupt->wait_list_lock);
2029 list_del(&pend->wait_list_node);
2031 unlock_and_free_fence:
2032 spin_unlock(&interrupt->wait_list_lock);
2039 static int hl_interrupt_wait_ioctl(struct hl_fpriv *hpriv, void *data)
2041 u16 interrupt_id, interrupt_offset, first_interrupt, last_interrupt;
2042 struct hl_device *hdev = hpriv->hdev;
2043 struct asic_fixed_properties *prop;
2044 union hl_wait_cs_args *args = data;
2045 enum hl_cs_wait_status status;
2048 prop = &hdev->asic_prop;
2050 if (!prop->user_interrupt_count) {
2051 dev_err(hdev->dev, "no user interrupts allowed");
2056 FIELD_GET(HL_WAIT_CS_FLAGS_INTERRUPT_MASK, args->in.flags);
2058 first_interrupt = prop->first_available_user_msix_interrupt;
2059 last_interrupt = prop->first_available_user_msix_interrupt +
2060 prop->user_interrupt_count - 1;
2062 if ((interrupt_id < first_interrupt || interrupt_id > last_interrupt) &&
2063 interrupt_id != HL_COMMON_USER_INTERRUPT_ID) {
2064 dev_err(hdev->dev, "invalid user interrupt %u", interrupt_id);
2068 if (interrupt_id == HL_COMMON_USER_INTERRUPT_ID)
2069 interrupt_offset = HL_COMMON_USER_INTERRUPT_ID;
2071 interrupt_offset = interrupt_id - first_interrupt;
2073 rc = _hl_interrupt_wait_ioctl(hdev, hpriv->ctx,
2074 args->in.interrupt_timeout_us, args->in.addr,
2075 args->in.target, interrupt_offset, &status);
2077 memset(args, 0, sizeof(*args));
2080 dev_err_ratelimited(hdev->dev,
2081 "interrupt_wait_ioctl failed (%d)\n", rc);
2087 case CS_WAIT_STATUS_COMPLETED:
2088 args->out.status = HL_WAIT_CS_STATUS_COMPLETED;
2090 case CS_WAIT_STATUS_BUSY:
2092 args->out.status = HL_WAIT_CS_STATUS_BUSY;
2099 int hl_wait_ioctl(struct hl_fpriv *hpriv, void *data)
2101 union hl_wait_cs_args *args = data;
2102 u32 flags = args->in.flags;
2105 if (flags & HL_WAIT_CS_FLAGS_INTERRUPT)
2106 rc = hl_interrupt_wait_ioctl(hpriv, data);
2108 rc = hl_cs_wait_ioctl(hpriv, data);