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 dev_dbg(hdev->dev, "reset sob id %u\n", hw_sob->sob_id);
43 hdev->asic_funcs->reset_sob(hdev, hw_sob);
45 hw_sob->need_reset = false;
48 void hl_sob_reset_error(struct kref *ref)
50 struct hl_hw_sob *hw_sob = container_of(ref, struct hl_hw_sob,
52 struct hl_device *hdev = hw_sob->hdev;
55 "SOB release shouldn't be called here, q_idx: %d, sob_id: %d\n",
56 hw_sob->q_idx, hw_sob->sob_id);
59 void hw_sob_put(struct hl_hw_sob *hw_sob)
62 kref_put(&hw_sob->kref, hl_sob_reset);
65 static void hw_sob_put_err(struct hl_hw_sob *hw_sob)
68 kref_put(&hw_sob->kref, hl_sob_reset_error);
71 void hw_sob_get(struct hl_hw_sob *hw_sob)
74 kref_get(&hw_sob->kref);
78 * hl_gen_sob_mask() - Generates a sob mask to be used in a monitor arm packet
79 * @sob_base: sob base id
80 * @sob_mask: sob user mask, each bit represents a sob offset from sob base
81 * @mask: generated mask
83 * Return: 0 if given parameters are valid
85 int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask)
92 if (sob_mask == 0x1) {
93 *mask = ~(1 << (sob_base & 0x7));
95 /* find msb in order to verify sob range is valid */
96 for (i = BITS_PER_BYTE - 1 ; i >= 0 ; i--)
97 if (BIT(i) & sob_mask)
100 if (i > (HL_MAX_SOBS_PER_MONITOR - (sob_base & 0x7) - 1))
109 static void hl_fence_release(struct kref *kref)
111 struct hl_fence *fence =
112 container_of(kref, struct hl_fence, refcount);
113 struct hl_cs_compl *hl_cs_cmpl =
114 container_of(fence, struct hl_cs_compl, base_fence);
119 void hl_fence_put(struct hl_fence *fence)
121 if (IS_ERR_OR_NULL(fence))
123 kref_put(&fence->refcount, hl_fence_release);
126 void hl_fences_put(struct hl_fence **fence, int len)
130 for (i = 0; i < len; i++, fence++)
131 hl_fence_put(*fence);
134 void hl_fence_get(struct hl_fence *fence)
137 kref_get(&fence->refcount);
140 static void hl_fence_init(struct hl_fence *fence, u64 sequence)
142 kref_init(&fence->refcount);
143 fence->cs_sequence = sequence;
145 fence->timestamp = ktime_set(0, 0);
146 init_completion(&fence->completion);
149 void cs_get(struct hl_cs *cs)
151 kref_get(&cs->refcount);
154 static int cs_get_unless_zero(struct hl_cs *cs)
156 return kref_get_unless_zero(&cs->refcount);
159 static void cs_put(struct hl_cs *cs)
161 kref_put(&cs->refcount, cs_do_release);
164 static void cs_job_do_release(struct kref *ref)
166 struct hl_cs_job *job = container_of(ref, struct hl_cs_job, refcount);
171 static void cs_job_put(struct hl_cs_job *job)
173 kref_put(&job->refcount, cs_job_do_release);
176 bool cs_needs_completion(struct hl_cs *cs)
178 /* In case this is a staged CS, only the last CS in sequence should
179 * get a completion, any non staged CS will always get a completion
181 if (cs->staged_cs && !cs->staged_last)
187 bool cs_needs_timeout(struct hl_cs *cs)
189 /* In case this is a staged CS, only the first CS in sequence should
190 * get a timeout, any non staged CS will always get a timeout
192 if (cs->staged_cs && !cs->staged_first)
198 static bool is_cb_patched(struct hl_device *hdev, struct hl_cs_job *job)
201 * Patched CB is created for external queues jobs, and for H/W queues
202 * jobs if the user CB was allocated by driver and MMU is disabled.
204 return (job->queue_type == QUEUE_TYPE_EXT ||
205 (job->queue_type == QUEUE_TYPE_HW &&
206 job->is_kernel_allocated_cb &&
211 * cs_parser - parse the user command submission
213 * @hpriv : pointer to the private data of the fd
214 * @job : pointer to the job that holds the command submission info
216 * The function parses the command submission of the user. It calls the
217 * ASIC specific parser, which returns a list of memory blocks to send
218 * to the device as different command buffers
221 static int cs_parser(struct hl_fpriv *hpriv, struct hl_cs_job *job)
223 struct hl_device *hdev = hpriv->hdev;
224 struct hl_cs_parser parser;
227 parser.ctx_id = job->cs->ctx->asid;
228 parser.cs_sequence = job->cs->sequence;
229 parser.job_id = job->id;
231 parser.hw_queue_id = job->hw_queue_id;
232 parser.job_userptr_list = &job->userptr_list;
233 parser.patched_cb = NULL;
234 parser.user_cb = job->user_cb;
235 parser.user_cb_size = job->user_cb_size;
236 parser.queue_type = job->queue_type;
237 parser.is_kernel_allocated_cb = job->is_kernel_allocated_cb;
238 job->patched_cb = NULL;
239 parser.completion = cs_needs_completion(job->cs);
241 rc = hdev->asic_funcs->cs_parser(hdev, &parser);
243 if (is_cb_patched(hdev, job)) {
245 job->patched_cb = parser.patched_cb;
246 job->job_cb_size = parser.patched_cb_size;
247 job->contains_dma_pkt = parser.contains_dma_pkt;
248 atomic_inc(&job->patched_cb->cs_cnt);
252 * Whether the parsing worked or not, we don't need the
253 * original CB anymore because it was already parsed and
254 * won't be accessed again for this CS
256 atomic_dec(&job->user_cb->cs_cnt);
257 hl_cb_put(job->user_cb);
260 job->job_cb_size = job->user_cb_size;
266 static void complete_job(struct hl_device *hdev, struct hl_cs_job *job)
268 struct hl_cs *cs = job->cs;
270 if (is_cb_patched(hdev, job)) {
271 hl_userptr_delete_list(hdev, &job->userptr_list);
274 * We might arrive here from rollback and patched CB wasn't
275 * created, so we need to check it's not NULL
277 if (job->patched_cb) {
278 atomic_dec(&job->patched_cb->cs_cnt);
279 hl_cb_put(job->patched_cb);
283 /* For H/W queue jobs, if a user CB was allocated by driver and MMU is
284 * enabled, the user CB isn't released in cs_parser() and thus should be
286 * This is also true for INT queues jobs which were allocated by driver
288 if (job->is_kernel_allocated_cb &&
289 ((job->queue_type == QUEUE_TYPE_HW && hdev->mmu_enable) ||
290 job->queue_type == QUEUE_TYPE_INT)) {
291 atomic_dec(&job->user_cb->cs_cnt);
292 hl_cb_put(job->user_cb);
296 * This is the only place where there can be multiple threads
297 * modifying the list at the same time
299 spin_lock(&cs->job_lock);
300 list_del(&job->cs_node);
301 spin_unlock(&cs->job_lock);
303 hl_debugfs_remove_job(hdev, job);
305 /* We decrement reference only for a CS that gets completion
306 * because the reference was incremented only for this kind of CS
307 * right before it was scheduled.
309 * In staged submission, only the last CS marked as 'staged_last'
310 * gets completion, hence its release function will be called from here.
311 * As for all the rest CS's in the staged submission which do not get
312 * completion, their CS reference will be decremented by the
313 * 'staged_last' CS during the CS release flow.
314 * All relevant PQ CI counters will be incremented during the CS release
315 * flow by calling 'hl_hw_queue_update_ci'.
317 if (cs_needs_completion(cs) &&
318 (job->queue_type == QUEUE_TYPE_EXT ||
319 job->queue_type == QUEUE_TYPE_HW))
326 * hl_staged_cs_find_first - locate the first CS in this staged submission
328 * @hdev: pointer to device structure
329 * @cs_seq: staged submission sequence number
331 * @note: This function must be called under 'hdev->cs_mirror_lock'
333 * Find and return a CS pointer with the given sequence
335 struct hl_cs *hl_staged_cs_find_first(struct hl_device *hdev, u64 cs_seq)
339 list_for_each_entry_reverse(cs, &hdev->cs_mirror_list, mirror_node)
340 if (cs->staged_cs && cs->staged_first &&
341 cs->sequence == cs_seq)
348 * is_staged_cs_last_exists - returns true if the last CS in sequence exists
350 * @hdev: pointer to device structure
351 * @cs: staged submission member
354 bool is_staged_cs_last_exists(struct hl_device *hdev, struct hl_cs *cs)
356 struct hl_cs *last_entry;
358 last_entry = list_last_entry(&cs->staged_cs_node, struct hl_cs,
361 if (last_entry->staged_last)
368 * staged_cs_get - get CS reference if this CS is a part of a staged CS
370 * @hdev: pointer to device structure
372 * @cs_seq: staged submission sequence number
374 * Increment CS reference for every CS in this staged submission except for
375 * the CS which get completion.
377 static void staged_cs_get(struct hl_device *hdev, struct hl_cs *cs)
379 /* Only the last CS in this staged submission will get a completion.
380 * We must increment the reference for all other CS's in this
382 * Once we get a completion we will release the whole staged submission.
384 if (!cs->staged_last)
389 * staged_cs_put - put a CS in case it is part of staged submission
391 * @hdev: pointer to device structure
394 * This function decrements a CS reference (for a non completion CS)
396 static void staged_cs_put(struct hl_device *hdev, struct hl_cs *cs)
398 /* We release all CS's in a staged submission except the last
399 * CS which we have never incremented its reference.
401 if (!cs_needs_completion(cs))
405 static void cs_handle_tdr(struct hl_device *hdev, struct hl_cs *cs)
407 bool next_entry_found = false;
410 if (!cs_needs_timeout(cs))
413 spin_lock(&hdev->cs_mirror_lock);
415 /* We need to handle tdr only once for the complete staged submission.
416 * Hence, we choose the CS that reaches this function first which is
417 * the CS marked as 'staged_last'.
419 if (cs->staged_cs && cs->staged_last)
420 cs = hl_staged_cs_find_first(hdev, cs->staged_sequence);
422 spin_unlock(&hdev->cs_mirror_lock);
424 /* Don't cancel TDR in case this CS was timedout because we might be
425 * running from the TDR context
427 if (cs && (cs->timedout ||
428 hdev->timeout_jiffies == MAX_SCHEDULE_TIMEOUT))
431 if (cs && cs->tdr_active)
432 cancel_delayed_work_sync(&cs->work_tdr);
434 spin_lock(&hdev->cs_mirror_lock);
436 /* queue TDR for next CS */
437 list_for_each_entry(next, &hdev->cs_mirror_list, mirror_node)
438 if (cs_needs_timeout(next)) {
439 next_entry_found = true;
443 if (next_entry_found && !next->tdr_active) {
444 next->tdr_active = true;
445 schedule_delayed_work(&next->work_tdr, next->timeout_jiffies);
448 spin_unlock(&hdev->cs_mirror_lock);
452 * force_complete_multi_cs - complete all contexts that wait on multi-CS
454 * @hdev: pointer to habanalabs device structure
456 static void force_complete_multi_cs(struct hl_device *hdev)
460 for (i = 0; i < MULTI_CS_MAX_USER_CTX; i++) {
461 struct multi_cs_completion *mcs_compl;
463 mcs_compl = &hdev->multi_cs_completion[i];
465 spin_lock(&mcs_compl->lock);
467 if (!mcs_compl->used) {
468 spin_unlock(&mcs_compl->lock);
472 /* when calling force complete no context should be waiting on
474 * We are calling the function as a protection for such case
475 * to free any pending context and print error message
478 "multi-CS completion context %d still waiting when calling force completion\n",
480 complete_all(&mcs_compl->completion);
481 spin_unlock(&mcs_compl->lock);
486 * complete_multi_cs - complete all waiting entities on multi-CS
488 * @hdev: pointer to habanalabs device structure
490 * The function signals a waiting entity that has an overlapping stream masters
491 * with the completed CS.
493 * - a completed CS worked on stream master QID 4, multi CS completion
494 * is actively waiting on stream master QIDs 3, 5. don't send signal as no
495 * common stream master QID
496 * - a completed CS worked on stream master QID 4, multi CS completion
497 * is actively waiting on stream master QIDs 3, 4. send signal as stream
498 * master QID 4 is common
500 static void complete_multi_cs(struct hl_device *hdev, struct hl_cs *cs)
502 struct hl_fence *fence = cs->fence;
505 /* in case of multi CS check for completion only for the first CS */
506 if (cs->staged_cs && !cs->staged_first)
509 for (i = 0; i < MULTI_CS_MAX_USER_CTX; i++) {
510 struct multi_cs_completion *mcs_compl;
512 mcs_compl = &hdev->multi_cs_completion[i];
513 if (!mcs_compl->used)
516 spin_lock(&mcs_compl->lock);
520 * 1. still waiting for completion
521 * 2. the completed CS has at least one overlapping stream
522 * master with the stream masters in the completion
524 if (mcs_compl->used &&
525 (fence->stream_master_qid_map &
526 mcs_compl->stream_master_qid_map)) {
527 /* extract the timestamp only of first completed CS */
528 if (!mcs_compl->timestamp)
529 mcs_compl->timestamp =
530 ktime_to_ns(fence->timestamp);
531 complete_all(&mcs_compl->completion);
534 spin_unlock(&mcs_compl->lock);
538 static inline void cs_release_sob_reset_handler(struct hl_device *hdev,
540 struct hl_cs_compl *hl_cs_cmpl)
542 /* Skip this handler if the cs wasn't submitted, to avoid putting
543 * the hw_sob twice, since this case already handled at this point,
544 * also skip if the hw_sob pointer wasn't set.
546 if (!hl_cs_cmpl->hw_sob || !cs->submitted)
549 spin_lock(&hl_cs_cmpl->lock);
552 * we get refcount upon reservation of signals or signal/wait cs for the
553 * hw_sob object, and need to put it when the first staged cs
554 * (which cotains the encaps signals) or cs signal/wait is completed.
556 if ((hl_cs_cmpl->type == CS_TYPE_SIGNAL) ||
557 (hl_cs_cmpl->type == CS_TYPE_WAIT) ||
558 (hl_cs_cmpl->type == CS_TYPE_COLLECTIVE_WAIT) ||
559 (!!hl_cs_cmpl->encaps_signals)) {
561 "CS 0x%llx type %d finished, sob_id: %d, sob_val: %u\n",
564 hl_cs_cmpl->hw_sob->sob_id,
565 hl_cs_cmpl->sob_val);
567 hw_sob_put(hl_cs_cmpl->hw_sob);
569 if (hl_cs_cmpl->type == CS_TYPE_COLLECTIVE_WAIT)
570 hdev->asic_funcs->reset_sob_group(hdev,
571 hl_cs_cmpl->sob_group);
574 spin_unlock(&hl_cs_cmpl->lock);
577 static void cs_do_release(struct kref *ref)
579 struct hl_cs *cs = container_of(ref, struct hl_cs, refcount);
580 struct hl_device *hdev = cs->ctx->hdev;
581 struct hl_cs_job *job, *tmp;
582 struct hl_cs_compl *hl_cs_cmpl =
583 container_of(cs->fence, struct hl_cs_compl, base_fence);
585 cs->completed = true;
588 * Although if we reached here it means that all external jobs have
589 * finished, because each one of them took refcnt to CS, we still
590 * need to go over the internal jobs and complete them. Otherwise, we
591 * will have leaked memory and what's worse, the CS object (and
592 * potentially the CTX object) could be released, while the JOB
593 * still holds a pointer to them (but no reference).
595 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
596 complete_job(hdev, job);
598 if (!cs->submitted) {
600 * In case the wait for signal CS was submitted, the fence put
601 * occurs in init_signal_wait_cs() or collective_wait_init_cs()
602 * right before hanging on the PQ.
604 if (cs->type == CS_TYPE_WAIT ||
605 cs->type == CS_TYPE_COLLECTIVE_WAIT)
606 hl_fence_put(cs->signal_fence);
611 /* Need to update CI for all queue jobs that does not get completion */
612 hl_hw_queue_update_ci(cs);
614 /* remove CS from CS mirror list */
615 spin_lock(&hdev->cs_mirror_lock);
616 list_del_init(&cs->mirror_node);
617 spin_unlock(&hdev->cs_mirror_lock);
619 cs_handle_tdr(hdev, cs);
622 /* the completion CS decrements reference for the entire
625 if (cs->staged_last) {
626 struct hl_cs *staged_cs, *tmp;
628 list_for_each_entry_safe(staged_cs, tmp,
629 &cs->staged_cs_node, staged_cs_node)
630 staged_cs_put(hdev, staged_cs);
633 /* A staged CS will be a member in the list only after it
634 * was submitted. We used 'cs_mirror_lock' when inserting
635 * it to list so we will use it again when removing it
638 spin_lock(&hdev->cs_mirror_lock);
639 list_del(&cs->staged_cs_node);
640 spin_unlock(&hdev->cs_mirror_lock);
643 /* decrement refcount to handle when first staged cs
644 * with encaps signals is completed.
646 if (hl_cs_cmpl->encaps_signals)
647 kref_put(&hl_cs_cmpl->encaps_sig_hdl->refcount,
648 hl_encaps_handle_do_release);
651 if ((cs->type == CS_TYPE_WAIT || cs->type == CS_TYPE_COLLECTIVE_WAIT)
652 && cs->encaps_signals)
653 kref_put(&cs->encaps_sig_hdl->refcount,
654 hl_encaps_handle_do_release);
657 /* Must be called before hl_ctx_put because inside we use ctx to get
660 hl_debugfs_remove_cs(cs);
664 /* We need to mark an error for not submitted because in that case
665 * the hl fence release flow is different. Mainly, we don't need
666 * to handle hw_sob for signal/wait
669 cs->fence->error = -ETIMEDOUT;
670 else if (cs->aborted)
671 cs->fence->error = -EIO;
672 else if (!cs->submitted)
673 cs->fence->error = -EBUSY;
675 if (unlikely(cs->skip_reset_on_timeout)) {
677 "Command submission %llu completed after %llu (s)\n",
679 div_u64(jiffies - cs->submission_time_jiffies, HZ));
683 cs->fence->timestamp = ktime_get();
684 complete_all(&cs->fence->completion);
685 complete_multi_cs(hdev, cs);
687 cs_release_sob_reset_handler(hdev, cs, hl_cs_cmpl);
689 hl_fence_put(cs->fence);
691 kfree(cs->jobs_in_queue_cnt);
695 static void cs_timedout(struct work_struct *work)
697 struct hl_device *hdev;
699 struct hl_cs *cs = container_of(work, struct hl_cs,
701 bool skip_reset_on_timeout = cs->skip_reset_on_timeout;
703 rc = cs_get_unless_zero(cs);
707 if ((!cs->submitted) || (cs->completed)) {
712 /* Mark the CS is timed out so we won't try to cancel its TDR */
713 if (likely(!skip_reset_on_timeout))
716 hdev = cs->ctx->hdev;
721 "Signal command submission %llu has not finished in time!\n",
727 "Wait command submission %llu has not finished in time!\n",
731 case CS_TYPE_COLLECTIVE_WAIT:
733 "Collective Wait command submission %llu has not finished in time!\n",
739 "Command submission %llu has not finished in time!\n",
744 rc = hl_state_dump(hdev);
746 dev_err(hdev->dev, "Error during system state dump %d\n", rc);
750 if (likely(!skip_reset_on_timeout)) {
751 if (hdev->reset_on_lockup)
752 hl_device_reset(hdev, HL_RESET_TDR);
754 hdev->needs_reset = true;
758 static int allocate_cs(struct hl_device *hdev, struct hl_ctx *ctx,
759 enum hl_cs_type cs_type, u64 user_sequence,
760 struct hl_cs **cs_new, u32 flags, u32 timeout)
762 struct hl_cs_counters_atomic *cntr;
763 struct hl_fence *other = NULL;
764 struct hl_cs_compl *cs_cmpl;
768 cntr = &hdev->aggregated_cs_counters;
770 cs = kzalloc(sizeof(*cs), GFP_ATOMIC);
772 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
775 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
776 atomic64_inc(&cntr->out_of_mem_drop_cnt);
780 /* increment refcnt for context */
781 hl_ctx_get(hdev, ctx);
784 cs->submitted = false;
785 cs->completed = false;
787 cs->timestamp = !!(flags & HL_CS_FLAGS_TIMESTAMP);
788 cs->encaps_signals = !!(flags & HL_CS_FLAGS_ENCAP_SIGNALS);
789 cs->timeout_jiffies = timeout;
790 cs->skip_reset_on_timeout =
791 hdev->skip_reset_on_timeout ||
792 !!(flags & HL_CS_FLAGS_SKIP_RESET_ON_TIMEOUT);
793 cs->submission_time_jiffies = jiffies;
794 INIT_LIST_HEAD(&cs->job_list);
795 INIT_DELAYED_WORK(&cs->work_tdr, cs_timedout);
796 kref_init(&cs->refcount);
797 spin_lock_init(&cs->job_lock);
799 cs_cmpl = kzalloc(sizeof(*cs_cmpl), GFP_ATOMIC);
801 cs_cmpl = kzalloc(sizeof(*cs_cmpl), GFP_KERNEL);
804 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
805 atomic64_inc(&cntr->out_of_mem_drop_cnt);
810 cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
811 sizeof(*cs->jobs_in_queue_cnt), GFP_ATOMIC);
812 if (!cs->jobs_in_queue_cnt)
813 cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
814 sizeof(*cs->jobs_in_queue_cnt), GFP_KERNEL);
816 if (!cs->jobs_in_queue_cnt) {
817 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
818 atomic64_inc(&cntr->out_of_mem_drop_cnt);
823 cs_cmpl->hdev = hdev;
824 cs_cmpl->type = cs->type;
825 spin_lock_init(&cs_cmpl->lock);
826 cs->fence = &cs_cmpl->base_fence;
828 spin_lock(&ctx->cs_lock);
830 cs_cmpl->cs_seq = ctx->cs_sequence;
831 other = ctx->cs_pending[cs_cmpl->cs_seq &
832 (hdev->asic_prop.max_pending_cs - 1)];
834 if (other && !completion_done(&other->completion)) {
835 /* If the following statement is true, it means we have reached
836 * a point in which only part of the staged submission was
837 * submitted and we don't have enough room in the 'cs_pending'
838 * array for the rest of the submission.
839 * This causes a deadlock because this CS will never be
840 * completed as it depends on future CS's for completion.
842 if (other->cs_sequence == user_sequence)
843 dev_crit_ratelimited(hdev->dev,
844 "Staged CS %llu deadlock due to lack of resources",
847 dev_dbg_ratelimited(hdev->dev,
848 "Rejecting CS because of too many in-flights CS\n");
849 atomic64_inc(&ctx->cs_counters.max_cs_in_flight_drop_cnt);
850 atomic64_inc(&cntr->max_cs_in_flight_drop_cnt);
856 hl_fence_init(&cs_cmpl->base_fence, cs_cmpl->cs_seq);
858 cs->sequence = cs_cmpl->cs_seq;
860 ctx->cs_pending[cs_cmpl->cs_seq &
861 (hdev->asic_prop.max_pending_cs - 1)] =
862 &cs_cmpl->base_fence;
865 hl_fence_get(&cs_cmpl->base_fence);
869 spin_unlock(&ctx->cs_lock);
876 spin_unlock(&ctx->cs_lock);
877 kfree(cs->jobs_in_queue_cnt);
886 static void cs_rollback(struct hl_device *hdev, struct hl_cs *cs)
888 struct hl_cs_job *job, *tmp;
890 staged_cs_put(hdev, cs);
892 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
893 complete_job(hdev, job);
896 void hl_cs_rollback_all(struct hl_device *hdev)
899 struct hl_cs *cs, *tmp;
901 flush_workqueue(hdev->sob_reset_wq);
903 /* flush all completions before iterating over the CS mirror list in
904 * order to avoid a race with the release functions
906 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
907 flush_workqueue(hdev->cq_wq[i]);
909 /* Make sure we don't have leftovers in the CS mirror list */
910 list_for_each_entry_safe(cs, tmp, &hdev->cs_mirror_list, mirror_node) {
913 dev_warn_ratelimited(hdev->dev, "Killing CS %d.%llu\n",
914 cs->ctx->asid, cs->sequence);
915 cs_rollback(hdev, cs);
919 force_complete_multi_cs(hdev);
923 wake_pending_user_interrupt_threads(struct hl_user_interrupt *interrupt)
925 struct hl_user_pending_interrupt *pend;
927 spin_lock(&interrupt->wait_list_lock);
928 list_for_each_entry(pend, &interrupt->wait_list_head, wait_list_node) {
929 pend->fence.error = -EIO;
930 complete_all(&pend->fence.completion);
932 spin_unlock(&interrupt->wait_list_lock);
935 void hl_release_pending_user_interrupts(struct hl_device *hdev)
937 struct asic_fixed_properties *prop = &hdev->asic_prop;
938 struct hl_user_interrupt *interrupt;
941 if (!prop->user_interrupt_count)
944 /* We iterate through the user interrupt requests and waking up all
945 * user threads waiting for interrupt completion. We iterate the
946 * list under a lock, this is why all user threads, once awake,
947 * will wait on the same lock and will release the waiting object upon
951 for (i = 0 ; i < prop->user_interrupt_count ; i++) {
952 interrupt = &hdev->user_interrupt[i];
953 wake_pending_user_interrupt_threads(interrupt);
956 interrupt = &hdev->common_user_interrupt;
957 wake_pending_user_interrupt_threads(interrupt);
960 static void job_wq_completion(struct work_struct *work)
962 struct hl_cs_job *job = container_of(work, struct hl_cs_job,
964 struct hl_cs *cs = job->cs;
965 struct hl_device *hdev = cs->ctx->hdev;
967 /* job is no longer needed */
968 complete_job(hdev, job);
971 static int validate_queue_index(struct hl_device *hdev,
972 struct hl_cs_chunk *chunk,
973 enum hl_queue_type *queue_type,
974 bool *is_kernel_allocated_cb)
976 struct asic_fixed_properties *asic = &hdev->asic_prop;
977 struct hw_queue_properties *hw_queue_prop;
979 /* This must be checked here to prevent out-of-bounds access to
980 * hw_queues_props array
982 if (chunk->queue_index >= asic->max_queues) {
983 dev_err(hdev->dev, "Queue index %d is invalid\n",
988 hw_queue_prop = &asic->hw_queues_props[chunk->queue_index];
990 if (hw_queue_prop->type == QUEUE_TYPE_NA) {
991 dev_err(hdev->dev, "Queue index %d is invalid\n",
996 if (hw_queue_prop->driver_only) {
998 "Queue index %d is restricted for the kernel driver\n",
1003 /* When hw queue type isn't QUEUE_TYPE_HW,
1004 * USER_ALLOC_CB flag shall be referred as "don't care".
1006 if (hw_queue_prop->type == QUEUE_TYPE_HW) {
1007 if (chunk->cs_chunk_flags & HL_CS_CHUNK_FLAGS_USER_ALLOC_CB) {
1008 if (!(hw_queue_prop->cb_alloc_flags & CB_ALLOC_USER)) {
1010 "Queue index %d doesn't support user CB\n",
1011 chunk->queue_index);
1015 *is_kernel_allocated_cb = false;
1017 if (!(hw_queue_prop->cb_alloc_flags &
1020 "Queue index %d doesn't support kernel CB\n",
1021 chunk->queue_index);
1025 *is_kernel_allocated_cb = true;
1028 *is_kernel_allocated_cb = !!(hw_queue_prop->cb_alloc_flags
1032 *queue_type = hw_queue_prop->type;
1036 static struct hl_cb *get_cb_from_cs_chunk(struct hl_device *hdev,
1037 struct hl_cb_mgr *cb_mgr,
1038 struct hl_cs_chunk *chunk)
1043 cb_handle = (u32) (chunk->cb_handle >> PAGE_SHIFT);
1045 cb = hl_cb_get(hdev, cb_mgr, cb_handle);
1047 dev_err(hdev->dev, "CB handle 0x%x invalid\n", cb_handle);
1051 if ((chunk->cb_size < 8) || (chunk->cb_size > cb->size)) {
1052 dev_err(hdev->dev, "CB size %u invalid\n", chunk->cb_size);
1056 atomic_inc(&cb->cs_cnt);
1065 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
1066 enum hl_queue_type queue_type, bool is_kernel_allocated_cb)
1068 struct hl_cs_job *job;
1070 job = kzalloc(sizeof(*job), GFP_ATOMIC);
1072 job = kzalloc(sizeof(*job), GFP_KERNEL);
1077 kref_init(&job->refcount);
1078 job->queue_type = queue_type;
1079 job->is_kernel_allocated_cb = is_kernel_allocated_cb;
1081 if (is_cb_patched(hdev, job))
1082 INIT_LIST_HEAD(&job->userptr_list);
1084 if (job->queue_type == QUEUE_TYPE_EXT)
1085 INIT_WORK(&job->finish_work, job_wq_completion);
1090 static enum hl_cs_type hl_cs_get_cs_type(u32 cs_type_flags)
1092 if (cs_type_flags & HL_CS_FLAGS_SIGNAL)
1093 return CS_TYPE_SIGNAL;
1094 else if (cs_type_flags & HL_CS_FLAGS_WAIT)
1095 return CS_TYPE_WAIT;
1096 else if (cs_type_flags & HL_CS_FLAGS_COLLECTIVE_WAIT)
1097 return CS_TYPE_COLLECTIVE_WAIT;
1098 else if (cs_type_flags & HL_CS_FLAGS_RESERVE_SIGNALS_ONLY)
1099 return CS_RESERVE_SIGNALS;
1100 else if (cs_type_flags & HL_CS_FLAGS_UNRESERVE_SIGNALS_ONLY)
1101 return CS_UNRESERVE_SIGNALS;
1103 return CS_TYPE_DEFAULT;
1106 static int hl_cs_sanity_checks(struct hl_fpriv *hpriv, union hl_cs_args *args)
1108 struct hl_device *hdev = hpriv->hdev;
1109 struct hl_ctx *ctx = hpriv->ctx;
1110 u32 cs_type_flags, num_chunks;
1111 enum hl_device_status status;
1112 enum hl_cs_type cs_type;
1114 if (!hl_device_operational(hdev, &status)) {
1115 dev_warn_ratelimited(hdev->dev,
1116 "Device is %s. Can't submit new CS\n",
1117 hdev->status[status]);
1121 if ((args->in.cs_flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
1122 !hdev->supports_staged_submission) {
1123 dev_err(hdev->dev, "staged submission not supported");
1127 cs_type_flags = args->in.cs_flags & HL_CS_FLAGS_TYPE_MASK;
1129 if (unlikely(cs_type_flags && !is_power_of_2(cs_type_flags))) {
1131 "CS type flags are mutually exclusive, context %d\n",
1136 cs_type = hl_cs_get_cs_type(cs_type_flags);
1137 num_chunks = args->in.num_chunks_execute;
1139 if (unlikely((cs_type != CS_TYPE_DEFAULT) &&
1140 !hdev->supports_sync_stream)) {
1141 dev_err(hdev->dev, "Sync stream CS is not supported\n");
1145 if (cs_type == CS_TYPE_DEFAULT) {
1148 "Got execute CS with 0 chunks, context %d\n",
1152 } else if (num_chunks != 1) {
1154 "Sync stream CS mandates one chunk only, context %d\n",
1162 static int hl_cs_copy_chunk_array(struct hl_device *hdev,
1163 struct hl_cs_chunk **cs_chunk_array,
1164 void __user *chunks, u32 num_chunks,
1169 if (num_chunks > HL_MAX_JOBS_PER_CS) {
1170 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1171 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1173 "Number of chunks can NOT be larger than %d\n",
1174 HL_MAX_JOBS_PER_CS);
1178 *cs_chunk_array = kmalloc_array(num_chunks, sizeof(**cs_chunk_array),
1180 if (!*cs_chunk_array)
1181 *cs_chunk_array = kmalloc_array(num_chunks,
1182 sizeof(**cs_chunk_array), GFP_KERNEL);
1183 if (!*cs_chunk_array) {
1184 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1185 atomic64_inc(&hdev->aggregated_cs_counters.out_of_mem_drop_cnt);
1189 size_to_copy = num_chunks * sizeof(struct hl_cs_chunk);
1190 if (copy_from_user(*cs_chunk_array, chunks, size_to_copy)) {
1191 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1192 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1193 dev_err(hdev->dev, "Failed to copy cs chunk array from user\n");
1194 kfree(*cs_chunk_array);
1201 static int cs_staged_submission(struct hl_device *hdev, struct hl_cs *cs,
1202 u64 sequence, u32 flags,
1203 u32 encaps_signal_handle)
1205 if (!(flags & HL_CS_FLAGS_STAGED_SUBMISSION))
1208 cs->staged_last = !!(flags & HL_CS_FLAGS_STAGED_SUBMISSION_LAST);
1209 cs->staged_first = !!(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST);
1211 if (cs->staged_first) {
1212 /* Staged CS sequence is the first CS sequence */
1213 INIT_LIST_HEAD(&cs->staged_cs_node);
1214 cs->staged_sequence = cs->sequence;
1216 if (cs->encaps_signals)
1217 cs->encaps_sig_hdl_id = encaps_signal_handle;
1219 /* User sequence will be validated in 'hl_hw_queue_schedule_cs'
1220 * under the cs_mirror_lock
1222 cs->staged_sequence = sequence;
1225 /* Increment CS reference if needed */
1226 staged_cs_get(hdev, cs);
1228 cs->staged_cs = true;
1233 static u32 get_stream_master_qid_mask(struct hl_device *hdev, u32 qid)
1237 for (i = 0; i < hdev->stream_master_qid_arr_size; i++)
1238 if (qid == hdev->stream_master_qid_arr[i])
1244 static int cs_ioctl_default(struct hl_fpriv *hpriv, void __user *chunks,
1245 u32 num_chunks, u64 *cs_seq, u32 flags,
1246 u32 encaps_signals_handle, u32 timeout)
1248 bool staged_mid, int_queues_only = true;
1249 struct hl_device *hdev = hpriv->hdev;
1250 struct hl_cs_chunk *cs_chunk_array;
1251 struct hl_cs_counters_atomic *cntr;
1252 struct hl_ctx *ctx = hpriv->ctx;
1253 struct hl_cs_job *job;
1257 u8 stream_master_qid_map = 0;
1260 cntr = &hdev->aggregated_cs_counters;
1261 user_sequence = *cs_seq;
1262 *cs_seq = ULLONG_MAX;
1264 rc = hl_cs_copy_chunk_array(hdev, &cs_chunk_array, chunks, num_chunks,
1269 if ((flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
1270 !(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST))
1275 rc = allocate_cs(hdev, hpriv->ctx, CS_TYPE_DEFAULT,
1276 staged_mid ? user_sequence : ULLONG_MAX, &cs, flags,
1279 goto free_cs_chunk_array;
1281 *cs_seq = cs->sequence;
1283 hl_debugfs_add_cs(cs);
1285 rc = cs_staged_submission(hdev, cs, user_sequence, flags,
1286 encaps_signals_handle);
1288 goto free_cs_object;
1290 /* Validate ALL the CS chunks before submitting the CS */
1291 for (i = 0 ; i < num_chunks ; i++) {
1292 struct hl_cs_chunk *chunk = &cs_chunk_array[i];
1293 enum hl_queue_type queue_type;
1294 bool is_kernel_allocated_cb;
1296 rc = validate_queue_index(hdev, chunk, &queue_type,
1297 &is_kernel_allocated_cb);
1299 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1300 atomic64_inc(&cntr->validation_drop_cnt);
1301 goto free_cs_object;
1304 if (is_kernel_allocated_cb) {
1305 cb = get_cb_from_cs_chunk(hdev, &hpriv->cb_mgr, chunk);
1308 &ctx->cs_counters.validation_drop_cnt);
1309 atomic64_inc(&cntr->validation_drop_cnt);
1311 goto free_cs_object;
1314 cb = (struct hl_cb *) (uintptr_t) chunk->cb_handle;
1317 if (queue_type == QUEUE_TYPE_EXT ||
1318 queue_type == QUEUE_TYPE_HW) {
1319 int_queues_only = false;
1322 * store which stream are being used for external/HW
1325 if (hdev->supports_wait_for_multi_cs)
1326 stream_master_qid_map |=
1327 get_stream_master_qid_mask(hdev,
1328 chunk->queue_index);
1331 job = hl_cs_allocate_job(hdev, queue_type,
1332 is_kernel_allocated_cb);
1334 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1335 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1336 dev_err(hdev->dev, "Failed to allocate a new job\n");
1338 if (is_kernel_allocated_cb)
1341 goto free_cs_object;
1347 job->user_cb_size = chunk->cb_size;
1348 job->hw_queue_id = chunk->queue_index;
1350 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1352 list_add_tail(&job->cs_node, &cs->job_list);
1355 * Increment CS reference. When CS reference is 0, CS is
1356 * done and can be signaled to user and free all its resources
1357 * Only increment for JOB on external or H/W queues, because
1358 * only for those JOBs we get completion
1360 if (cs_needs_completion(cs) &&
1361 (job->queue_type == QUEUE_TYPE_EXT ||
1362 job->queue_type == QUEUE_TYPE_HW))
1365 hl_debugfs_add_job(hdev, job);
1367 rc = cs_parser(hpriv, job);
1369 atomic64_inc(&ctx->cs_counters.parsing_drop_cnt);
1370 atomic64_inc(&cntr->parsing_drop_cnt);
1372 "Failed to parse JOB %d.%llu.%d, err %d, rejecting the CS\n",
1373 cs->ctx->asid, cs->sequence, job->id, rc);
1374 goto free_cs_object;
1378 /* We allow a CS with any queue type combination as long as it does
1379 * not get a completion
1381 if (int_queues_only && cs_needs_completion(cs)) {
1382 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1383 atomic64_inc(&cntr->validation_drop_cnt);
1385 "Reject CS %d.%llu since it contains only internal queues jobs and needs completion\n",
1386 cs->ctx->asid, cs->sequence);
1388 goto free_cs_object;
1392 * store the (external/HW queues) streams used by the CS in the
1393 * fence object for multi-CS completion
1395 if (hdev->supports_wait_for_multi_cs)
1396 cs->fence->stream_master_qid_map = stream_master_qid_map;
1398 rc = hl_hw_queue_schedule_cs(cs);
1402 "Failed to submit CS %d.%llu to H/W queues, error %d\n",
1403 cs->ctx->asid, cs->sequence, rc);
1404 goto free_cs_object;
1407 rc = HL_CS_STATUS_SUCCESS;
1411 atomic_dec(&cb->cs_cnt);
1414 cs_rollback(hdev, cs);
1415 *cs_seq = ULLONG_MAX;
1416 /* The path below is both for good and erroneous exits */
1418 /* We finished with the CS in this function, so put the ref */
1420 free_cs_chunk_array:
1421 kfree(cs_chunk_array);
1426 static int hl_cs_ctx_switch(struct hl_fpriv *hpriv, union hl_cs_args *args,
1429 struct hl_device *hdev = hpriv->hdev;
1430 struct hl_ctx *ctx = hpriv->ctx;
1431 bool need_soft_reset = false;
1432 int rc = 0, do_ctx_switch;
1433 void __user *chunks;
1434 u32 num_chunks, tmp;
1437 do_ctx_switch = atomic_cmpxchg(&ctx->thread_ctx_switch_token, 1, 0);
1439 if (do_ctx_switch || (args->in.cs_flags & HL_CS_FLAGS_FORCE_RESTORE)) {
1440 mutex_lock(&hpriv->restore_phase_mutex);
1442 if (do_ctx_switch) {
1443 rc = hdev->asic_funcs->context_switch(hdev, ctx->asid);
1445 dev_err_ratelimited(hdev->dev,
1446 "Failed to switch to context %d, rejecting CS! %d\n",
1449 * If we timedout, or if the device is not IDLE
1450 * while we want to do context-switch (-EBUSY),
1451 * we need to soft-reset because QMAN is
1452 * probably stuck. However, we can't call to
1453 * reset here directly because of deadlock, so
1454 * need to do it at the very end of this
1457 if ((rc == -ETIMEDOUT) || (rc == -EBUSY))
1458 need_soft_reset = true;
1459 mutex_unlock(&hpriv->restore_phase_mutex);
1464 hdev->asic_funcs->restore_phase_topology(hdev);
1466 chunks = (void __user *) (uintptr_t) args->in.chunks_restore;
1467 num_chunks = args->in.num_chunks_restore;
1471 "Need to run restore phase but restore CS is empty\n");
1474 rc = cs_ioctl_default(hpriv, chunks, num_chunks,
1475 cs_seq, 0, 0, hdev->timeout_jiffies);
1478 mutex_unlock(&hpriv->restore_phase_mutex);
1482 "Failed to submit restore CS for context %d (%d)\n",
1487 /* Need to wait for restore completion before execution phase */
1489 enum hl_cs_wait_status status;
1491 ret = _hl_cs_wait_ioctl(hdev, ctx,
1492 jiffies_to_usecs(hdev->timeout_jiffies),
1493 *cs_seq, &status, NULL);
1495 if (ret == -ERESTARTSYS) {
1496 usleep_range(100, 200);
1501 "Restore CS for context %d failed to complete %d\n",
1508 ctx->thread_ctx_switch_wait_token = 1;
1510 } else if (!ctx->thread_ctx_switch_wait_token) {
1511 rc = hl_poll_timeout_memory(hdev,
1512 &ctx->thread_ctx_switch_wait_token, tmp, (tmp == 1),
1513 100, jiffies_to_usecs(hdev->timeout_jiffies), false);
1515 if (rc == -ETIMEDOUT) {
1517 "context switch phase timeout (%d)\n", tmp);
1523 if ((rc == -ETIMEDOUT || rc == -EBUSY) && (need_soft_reset))
1524 hl_device_reset(hdev, 0);
1530 * hl_cs_signal_sob_wraparound_handler: handle SOB value wrapaound case.
1531 * if the SOB value reaches the max value move to the other SOB reserved
1533 * @hdev: pointer to device structure
1534 * @q_idx: stream queue index
1535 * @hw_sob: the H/W SOB used in this signal CS.
1536 * @count: signals count
1537 * @encaps_sig: tells whether it's reservation for encaps signals or not.
1539 * Note that this function must be called while hw_queues_lock is taken.
1541 int hl_cs_signal_sob_wraparound_handler(struct hl_device *hdev, u32 q_idx,
1542 struct hl_hw_sob **hw_sob, u32 count, bool encaps_sig)
1545 struct hl_sync_stream_properties *prop;
1546 struct hl_hw_sob *sob = *hw_sob, *other_sob;
1547 u8 other_sob_offset;
1549 prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
1553 /* check for wraparound */
1554 if (prop->next_sob_val + count >= HL_MAX_SOB_VAL) {
1556 * Decrement as we reached the max value.
1557 * The release function won't be called here as we've
1558 * just incremented the refcount right before calling this
1561 hw_sob_put_err(sob);
1564 * check the other sob value, if it still in use then fail
1565 * otherwise make the switch
1567 other_sob_offset = (prop->curr_sob_offset + 1) % HL_RSVD_SOBS;
1568 other_sob = &prop->hw_sob[other_sob_offset];
1570 if (kref_read(&other_sob->kref) != 1) {
1571 dev_err(hdev->dev, "error: Cannot switch SOBs q_idx: %d\n",
1577 * next_sob_val always points to the next available signal
1578 * in the sob, so in encaps signals it will be the next one
1579 * after reserving the required amount.
1582 prop->next_sob_val = count + 1;
1584 prop->next_sob_val = count;
1586 /* only two SOBs are currently in use */
1587 prop->curr_sob_offset = other_sob_offset;
1588 *hw_sob = other_sob;
1591 * check if other_sob needs reset, then do it before using it
1592 * for the reservation or the next signal cs.
1593 * we do it here, and for both encaps and regular signal cs
1594 * cases in order to avoid possible races of two kref_put
1595 * of the sob which can occur at the same time if we move the
1596 * sob reset(kref_put) to cs_do_release function.
1597 * in addition, if we have combination of cs signal and
1598 * encaps, and at the point we need to reset the sob there was
1599 * no more reservations and only signal cs keep coming,
1600 * in such case we need signal_cs to put the refcount and
1603 if (other_sob->need_reset)
1604 hw_sob_put(other_sob);
1607 /* set reset indication for the sob */
1608 sob->need_reset = true;
1609 hw_sob_get(other_sob);
1612 dev_dbg(hdev->dev, "switched to SOB %d, q_idx: %d\n",
1613 prop->curr_sob_offset, q_idx);
1615 prop->next_sob_val += count;
1621 static int cs_ioctl_extract_signal_seq(struct hl_device *hdev,
1622 struct hl_cs_chunk *chunk, u64 *signal_seq, struct hl_ctx *ctx,
1623 bool encaps_signals)
1625 u64 *signal_seq_arr = NULL;
1626 u32 size_to_copy, signal_seq_arr_len;
1629 if (encaps_signals) {
1630 *signal_seq = chunk->encaps_signal_seq;
1634 signal_seq_arr_len = chunk->num_signal_seq_arr;
1636 /* currently only one signal seq is supported */
1637 if (signal_seq_arr_len != 1) {
1638 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1639 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1641 "Wait for signal CS supports only one signal CS seq\n");
1645 signal_seq_arr = kmalloc_array(signal_seq_arr_len,
1646 sizeof(*signal_seq_arr),
1648 if (!signal_seq_arr)
1649 signal_seq_arr = kmalloc_array(signal_seq_arr_len,
1650 sizeof(*signal_seq_arr),
1652 if (!signal_seq_arr) {
1653 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1654 atomic64_inc(&hdev->aggregated_cs_counters.out_of_mem_drop_cnt);
1658 size_to_copy = signal_seq_arr_len * sizeof(*signal_seq_arr);
1659 if (copy_from_user(signal_seq_arr,
1660 u64_to_user_ptr(chunk->signal_seq_arr),
1662 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1663 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1665 "Failed to copy signal seq array from user\n");
1670 /* currently it is guaranteed to have only one signal seq */
1671 *signal_seq = signal_seq_arr[0];
1674 kfree(signal_seq_arr);
1679 static int cs_ioctl_signal_wait_create_jobs(struct hl_device *hdev,
1680 struct hl_ctx *ctx, struct hl_cs *cs,
1681 enum hl_queue_type q_type, u32 q_idx, u32 encaps_signal_offset)
1683 struct hl_cs_counters_atomic *cntr;
1684 struct hl_cs_job *job;
1688 cntr = &hdev->aggregated_cs_counters;
1690 job = hl_cs_allocate_job(hdev, q_type, true);
1692 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1693 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1694 dev_err(hdev->dev, "Failed to allocate a new job\n");
1698 if (cs->type == CS_TYPE_WAIT)
1699 cb_size = hdev->asic_funcs->get_wait_cb_size(hdev);
1701 cb_size = hdev->asic_funcs->get_signal_cb_size(hdev);
1703 cb = hl_cb_kernel_create(hdev, cb_size,
1704 q_type == QUEUE_TYPE_HW && hdev->mmu_enable);
1706 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1707 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1715 atomic_inc(&job->user_cb->cs_cnt);
1716 job->user_cb_size = cb_size;
1717 job->hw_queue_id = q_idx;
1719 if ((cs->type == CS_TYPE_WAIT || cs->type == CS_TYPE_COLLECTIVE_WAIT)
1720 && cs->encaps_signals)
1721 job->encaps_sig_wait_offset = encaps_signal_offset;
1723 * No need in parsing, user CB is the patched CB.
1724 * We call hl_cb_destroy() out of two reasons - we don't need the CB in
1725 * the CB idr anymore and to decrement its refcount as it was
1726 * incremented inside hl_cb_kernel_create().
1728 job->patched_cb = job->user_cb;
1729 job->job_cb_size = job->user_cb_size;
1730 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
1732 /* increment refcount as for external queues we get completion */
1735 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1737 list_add_tail(&job->cs_node, &cs->job_list);
1739 hl_debugfs_add_job(hdev, job);
1744 static int cs_ioctl_reserve_signals(struct hl_fpriv *hpriv,
1745 u32 q_idx, u32 count,
1746 u32 *handle_id, u32 *sob_addr,
1749 struct hw_queue_properties *hw_queue_prop;
1750 struct hl_sync_stream_properties *prop;
1751 struct hl_device *hdev = hpriv->hdev;
1752 struct hl_cs_encaps_sig_handle *handle;
1753 struct hl_encaps_signals_mgr *mgr;
1754 struct hl_hw_sob *hw_sob;
1758 if (count >= HL_MAX_SOB_VAL) {
1759 dev_err(hdev->dev, "signals count(%u) exceeds the max SOB value\n",
1765 if (q_idx >= hdev->asic_prop.max_queues) {
1766 dev_err(hdev->dev, "Queue index %d is invalid\n",
1772 hw_queue_prop = &hdev->asic_prop.hw_queues_props[q_idx];
1774 if (!hw_queue_prop->supports_sync_stream) {
1776 "Queue index %d does not support sync stream operations\n",
1782 prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
1784 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1790 handle->count = count;
1791 mgr = &hpriv->ctx->sig_mgr;
1793 spin_lock(&mgr->lock);
1794 hdl_id = idr_alloc(&mgr->handles, handle, 1, 0, GFP_ATOMIC);
1795 spin_unlock(&mgr->lock);
1798 dev_err(hdev->dev, "Failed to allocate IDR for a new signal reservation\n");
1803 handle->id = hdl_id;
1804 handle->q_idx = q_idx;
1805 handle->hdev = hdev;
1806 kref_init(&handle->refcount);
1808 hdev->asic_funcs->hw_queues_lock(hdev);
1810 hw_sob = &prop->hw_sob[prop->curr_sob_offset];
1813 * Increment the SOB value by count by user request
1814 * to reserve those signals
1815 * check if the signals amount to reserve is not exceeding the max sob
1816 * value, if yes then switch sob.
1818 rc = hl_cs_signal_sob_wraparound_handler(hdev, q_idx, &hw_sob, count,
1821 dev_err(hdev->dev, "Failed to switch SOB\n");
1822 hdev->asic_funcs->hw_queues_unlock(hdev);
1826 /* set the hw_sob to the handle after calling the sob wraparound handler
1827 * since sob could have changed.
1829 handle->hw_sob = hw_sob;
1831 /* store the current sob value for unreserve validity check, and
1832 * signal offset support
1834 handle->pre_sob_val = prop->next_sob_val - handle->count;
1836 *signals_count = prop->next_sob_val;
1837 hdev->asic_funcs->hw_queues_unlock(hdev);
1839 *sob_addr = handle->hw_sob->sob_addr;
1840 *handle_id = hdl_id;
1843 "Signals reserved, sob_id: %d, sob addr: 0x%x, last sob_val: %u, q_idx: %d, hdl_id: %d\n",
1844 hw_sob->sob_id, handle->hw_sob->sob_addr,
1845 prop->next_sob_val - 1, q_idx, hdl_id);
1849 spin_lock(&mgr->lock);
1850 idr_remove(&mgr->handles, hdl_id);
1851 spin_unlock(&mgr->lock);
1858 static int cs_ioctl_unreserve_signals(struct hl_fpriv *hpriv, u32 handle_id)
1860 struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
1861 struct hl_sync_stream_properties *prop;
1862 struct hl_device *hdev = hpriv->hdev;
1863 struct hl_encaps_signals_mgr *mgr;
1864 struct hl_hw_sob *hw_sob;
1865 u32 q_idx, sob_addr;
1868 mgr = &hpriv->ctx->sig_mgr;
1870 spin_lock(&mgr->lock);
1871 encaps_sig_hdl = idr_find(&mgr->handles, handle_id);
1872 if (encaps_sig_hdl) {
1873 dev_dbg(hdev->dev, "unreserve signals, handle: %u, SOB:0x%x, count: %u\n",
1874 handle_id, encaps_sig_hdl->hw_sob->sob_addr,
1875 encaps_sig_hdl->count);
1877 hdev->asic_funcs->hw_queues_lock(hdev);
1879 q_idx = encaps_sig_hdl->q_idx;
1880 prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
1881 hw_sob = &prop->hw_sob[prop->curr_sob_offset];
1882 sob_addr = hdev->asic_funcs->get_sob_addr(hdev, hw_sob->sob_id);
1884 /* Check if sob_val got out of sync due to other
1885 * signal submission requests which were handled
1886 * between the reserve-unreserve calls or SOB switch
1887 * upon reaching SOB max value.
1889 if (encaps_sig_hdl->pre_sob_val + encaps_sig_hdl->count
1890 != prop->next_sob_val ||
1891 sob_addr != encaps_sig_hdl->hw_sob->sob_addr) {
1892 dev_err(hdev->dev, "Cannot unreserve signals, SOB val ran out of sync, expected: %u, actual val: %u\n",
1893 encaps_sig_hdl->pre_sob_val,
1894 (prop->next_sob_val - encaps_sig_hdl->count));
1896 hdev->asic_funcs->hw_queues_unlock(hdev);
1902 * Decrement the SOB value by count by user request
1903 * to unreserve those signals
1905 prop->next_sob_val -= encaps_sig_hdl->count;
1907 hdev->asic_funcs->hw_queues_unlock(hdev);
1911 /* Release the id and free allocated memory of the handle */
1912 idr_remove(&mgr->handles, handle_id);
1913 kfree(encaps_sig_hdl);
1916 dev_err(hdev->dev, "failed to unreserve signals, cannot find handler\n");
1919 spin_unlock(&mgr->lock);
1924 static int cs_ioctl_signal_wait(struct hl_fpriv *hpriv, enum hl_cs_type cs_type,
1925 void __user *chunks, u32 num_chunks,
1926 u64 *cs_seq, u32 flags, u32 timeout)
1928 struct hl_cs_encaps_sig_handle *encaps_sig_hdl = NULL;
1929 bool handle_found = false, is_wait_cs = false,
1930 wait_cs_submitted = false,
1931 cs_encaps_signals = false;
1932 struct hl_cs_chunk *cs_chunk_array, *chunk;
1933 bool staged_cs_with_encaps_signals = false;
1934 struct hw_queue_properties *hw_queue_prop;
1935 struct hl_device *hdev = hpriv->hdev;
1936 struct hl_cs_compl *sig_waitcs_cmpl;
1937 u32 q_idx, collective_engine_id = 0;
1938 struct hl_cs_counters_atomic *cntr;
1939 struct hl_fence *sig_fence = NULL;
1940 struct hl_ctx *ctx = hpriv->ctx;
1941 enum hl_queue_type q_type;
1946 cntr = &hdev->aggregated_cs_counters;
1947 *cs_seq = ULLONG_MAX;
1949 rc = hl_cs_copy_chunk_array(hdev, &cs_chunk_array, chunks, num_chunks,
1954 /* currently it is guaranteed to have only one chunk */
1955 chunk = &cs_chunk_array[0];
1957 if (chunk->queue_index >= hdev->asic_prop.max_queues) {
1958 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1959 atomic64_inc(&cntr->validation_drop_cnt);
1960 dev_err(hdev->dev, "Queue index %d is invalid\n",
1961 chunk->queue_index);
1963 goto free_cs_chunk_array;
1966 q_idx = chunk->queue_index;
1967 hw_queue_prop = &hdev->asic_prop.hw_queues_props[q_idx];
1968 q_type = hw_queue_prop->type;
1970 if (!hw_queue_prop->supports_sync_stream) {
1971 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1972 atomic64_inc(&cntr->validation_drop_cnt);
1974 "Queue index %d does not support sync stream operations\n",
1977 goto free_cs_chunk_array;
1980 if (cs_type == CS_TYPE_COLLECTIVE_WAIT) {
1981 if (!(hw_queue_prop->collective_mode == HL_COLLECTIVE_MASTER)) {
1982 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1983 atomic64_inc(&cntr->validation_drop_cnt);
1985 "Queue index %d is invalid\n", q_idx);
1987 goto free_cs_chunk_array;
1990 collective_engine_id = chunk->collective_engine_id;
1993 is_wait_cs = !!(cs_type == CS_TYPE_WAIT ||
1994 cs_type == CS_TYPE_COLLECTIVE_WAIT);
1996 cs_encaps_signals = !!(flags & HL_CS_FLAGS_ENCAP_SIGNALS);
1999 rc = cs_ioctl_extract_signal_seq(hdev, chunk, &signal_seq,
2000 ctx, cs_encaps_signals);
2002 goto free_cs_chunk_array;
2004 if (cs_encaps_signals) {
2005 /* check if cs sequence has encapsulated
2011 spin_lock(&ctx->sig_mgr.lock);
2012 idp = &ctx->sig_mgr.handles;
2013 idr_for_each_entry(idp, encaps_sig_hdl, id) {
2014 if (encaps_sig_hdl->cs_seq == signal_seq) {
2015 handle_found = true;
2016 /* get refcount to protect removing
2017 * this handle from idr, needed when
2018 * multiple wait cs are used with offset
2019 * to wait on reserved encaps signals.
2021 kref_get(&encaps_sig_hdl->refcount);
2025 spin_unlock(&ctx->sig_mgr.lock);
2027 if (!handle_found) {
2028 dev_err(hdev->dev, "Cannot find encapsulated signals handle for seq 0x%llx\n",
2031 goto free_cs_chunk_array;
2034 /* validate also the signal offset value */
2035 if (chunk->encaps_signal_offset >
2036 encaps_sig_hdl->count) {
2037 dev_err(hdev->dev, "offset(%u) value exceed max reserved signals count(%u)!\n",
2038 chunk->encaps_signal_offset,
2039 encaps_sig_hdl->count);
2041 goto free_cs_chunk_array;
2045 sig_fence = hl_ctx_get_fence(ctx, signal_seq);
2046 if (IS_ERR(sig_fence)) {
2047 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2048 atomic64_inc(&cntr->validation_drop_cnt);
2050 "Failed to get signal CS with seq 0x%llx\n",
2052 rc = PTR_ERR(sig_fence);
2053 goto free_cs_chunk_array;
2057 /* signal CS already finished */
2059 goto free_cs_chunk_array;
2063 container_of(sig_fence, struct hl_cs_compl, base_fence);
2065 staged_cs_with_encaps_signals = !!
2066 (sig_waitcs_cmpl->type == CS_TYPE_DEFAULT &&
2067 (flags & HL_CS_FLAGS_ENCAP_SIGNALS));
2069 if (sig_waitcs_cmpl->type != CS_TYPE_SIGNAL &&
2070 !staged_cs_with_encaps_signals) {
2071 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2072 atomic64_inc(&cntr->validation_drop_cnt);
2074 "CS seq 0x%llx is not of a signal/encaps-signal CS\n",
2076 hl_fence_put(sig_fence);
2078 goto free_cs_chunk_array;
2081 if (completion_done(&sig_fence->completion)) {
2082 /* signal CS already finished */
2083 hl_fence_put(sig_fence);
2085 goto free_cs_chunk_array;
2089 rc = allocate_cs(hdev, ctx, cs_type, ULLONG_MAX, &cs, flags, timeout);
2092 hl_fence_put(sig_fence);
2094 goto free_cs_chunk_array;
2098 * Save the signal CS fence for later initialization right before
2099 * hanging the wait CS on the queue.
2100 * for encaps signals case, we save the cs sequence and handle pointer
2101 * for later initialization.
2104 cs->signal_fence = sig_fence;
2105 /* store the handle pointer, so we don't have to
2106 * look for it again, later on the flow
2107 * when we need to set SOB info in hw_queue.
2109 if (cs->encaps_signals)
2110 cs->encaps_sig_hdl = encaps_sig_hdl;
2113 hl_debugfs_add_cs(cs);
2115 *cs_seq = cs->sequence;
2117 if (cs_type == CS_TYPE_WAIT || cs_type == CS_TYPE_SIGNAL)
2118 rc = cs_ioctl_signal_wait_create_jobs(hdev, ctx, cs, q_type,
2119 q_idx, chunk->encaps_signal_offset);
2120 else if (cs_type == CS_TYPE_COLLECTIVE_WAIT)
2121 rc = hdev->asic_funcs->collective_wait_create_jobs(hdev, ctx,
2122 cs, q_idx, collective_engine_id,
2123 chunk->encaps_signal_offset);
2125 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2126 atomic64_inc(&cntr->validation_drop_cnt);
2131 goto free_cs_object;
2133 rc = hl_hw_queue_schedule_cs(cs);
2135 /* In case wait cs failed here, it means the signal cs
2136 * already completed. we want to free all it's related objects
2137 * but we don't want to fail the ioctl.
2141 else if (rc != -EAGAIN)
2143 "Failed to submit CS %d.%llu to H/W queues, error %d\n",
2144 ctx->asid, cs->sequence, rc);
2145 goto free_cs_object;
2148 rc = HL_CS_STATUS_SUCCESS;
2150 wait_cs_submitted = true;
2154 cs_rollback(hdev, cs);
2155 *cs_seq = ULLONG_MAX;
2156 /* The path below is both for good and erroneous exits */
2158 /* We finished with the CS in this function, so put the ref */
2160 free_cs_chunk_array:
2161 if (!wait_cs_submitted && cs_encaps_signals && handle_found &&
2163 kref_put(&encaps_sig_hdl->refcount,
2164 hl_encaps_handle_do_release);
2165 kfree(cs_chunk_array);
2170 int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data)
2172 union hl_cs_args *args = data;
2173 enum hl_cs_type cs_type = 0;
2174 u64 cs_seq = ULONG_MAX;
2175 void __user *chunks;
2176 u32 num_chunks, flags, timeout,
2177 signals_count = 0, sob_addr = 0, handle_id = 0;
2180 rc = hl_cs_sanity_checks(hpriv, args);
2184 rc = hl_cs_ctx_switch(hpriv, args, &cs_seq);
2188 cs_type = hl_cs_get_cs_type(args->in.cs_flags &
2189 ~HL_CS_FLAGS_FORCE_RESTORE);
2190 chunks = (void __user *) (uintptr_t) args->in.chunks_execute;
2191 num_chunks = args->in.num_chunks_execute;
2192 flags = args->in.cs_flags;
2194 /* In case this is a staged CS, user should supply the CS sequence */
2195 if ((flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
2196 !(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST))
2197 cs_seq = args->in.seq;
2199 timeout = flags & HL_CS_FLAGS_CUSTOM_TIMEOUT
2200 ? msecs_to_jiffies(args->in.timeout * 1000)
2201 : hpriv->hdev->timeout_jiffies;
2204 case CS_TYPE_SIGNAL:
2206 case CS_TYPE_COLLECTIVE_WAIT:
2207 rc = cs_ioctl_signal_wait(hpriv, cs_type, chunks, num_chunks,
2208 &cs_seq, args->in.cs_flags, timeout);
2210 case CS_RESERVE_SIGNALS:
2211 rc = cs_ioctl_reserve_signals(hpriv,
2212 args->in.encaps_signals_q_idx,
2213 args->in.encaps_signals_count,
2214 &handle_id, &sob_addr, &signals_count);
2216 case CS_UNRESERVE_SIGNALS:
2217 rc = cs_ioctl_unreserve_signals(hpriv,
2218 args->in.encaps_sig_handle_id);
2221 rc = cs_ioctl_default(hpriv, chunks, num_chunks, &cs_seq,
2223 args->in.encaps_sig_handle_id,
2228 if (rc != -EAGAIN) {
2229 memset(args, 0, sizeof(*args));
2231 if (cs_type == CS_RESERVE_SIGNALS) {
2232 args->out.handle_id = handle_id;
2233 args->out.sob_base_addr_offset = sob_addr;
2234 args->out.count = signals_count;
2236 args->out.seq = cs_seq;
2238 args->out.status = rc;
2244 static int hl_wait_for_fence(struct hl_ctx *ctx, u64 seq, struct hl_fence *fence,
2245 enum hl_cs_wait_status *status, u64 timeout_us,
2248 struct hl_device *hdev = ctx->hdev;
2252 if (IS_ERR(fence)) {
2253 rc = PTR_ERR(fence);
2255 dev_notice_ratelimited(hdev->dev,
2256 "Can't wait on CS %llu because current CS is at seq %llu\n",
2257 seq, ctx->cs_sequence);
2263 "Can't wait on seq %llu because current CS is at seq %llu (Fence is gone)\n",
2264 seq, ctx->cs_sequence);
2266 *status = CS_WAIT_STATUS_GONE;
2271 completion_rc = completion_done(&fence->completion);
2273 unsigned long timeout;
2275 timeout = (timeout_us == MAX_SCHEDULE_TIMEOUT) ?
2276 timeout_us : usecs_to_jiffies(timeout_us);
2278 wait_for_completion_interruptible_timeout(
2279 &fence->completion, timeout);
2282 if (completion_rc > 0) {
2283 *status = CS_WAIT_STATUS_COMPLETED;
2285 *timestamp = ktime_to_ns(fence->timestamp);
2287 *status = CS_WAIT_STATUS_BUSY;
2290 if (fence->error == -ETIMEDOUT)
2292 else if (fence->error == -EIO)
2299 * hl_cs_poll_fences - iterate CS fences to check for CS completion
2301 * @mcs_data: multi-CS internal data
2303 * @return 0 on success, otherwise non 0 error code
2305 * The function iterates on all CS sequence in the list and set bit in
2306 * completion_bitmap for each completed CS.
2307 * while iterating, the function can extracts the stream map to be later
2308 * used by the waiting function.
2309 * this function shall be called after taking context ref
2311 static int hl_cs_poll_fences(struct multi_cs_data *mcs_data)
2313 struct hl_fence **fence_ptr = mcs_data->fence_arr;
2314 struct hl_device *hdev = mcs_data->ctx->hdev;
2315 int i, rc, arr_len = mcs_data->arr_len;
2316 u64 *seq_arr = mcs_data->seq_arr;
2317 ktime_t max_ktime, first_cs_time;
2318 enum hl_cs_wait_status status;
2320 memset(fence_ptr, 0, arr_len * sizeof(*fence_ptr));
2322 /* get all fences under the same lock */
2323 rc = hl_ctx_get_fences(mcs_data->ctx, seq_arr, fence_ptr, arr_len);
2328 * set to maximum time to verify timestamp is valid: if at the end
2329 * this value is maintained- no timestamp was updated
2331 max_ktime = ktime_set(KTIME_SEC_MAX, 0);
2332 first_cs_time = max_ktime;
2334 for (i = 0; i < arr_len; i++, fence_ptr++) {
2335 struct hl_fence *fence = *fence_ptr;
2338 * function won't sleep as it is called with timeout 0 (i.e.
2341 rc = hl_wait_for_fence(mcs_data->ctx, seq_arr[i], fence,
2345 "wait_for_fence error :%d for CS seq %llu\n",
2350 mcs_data->stream_master_qid_map |= fence->stream_master_qid_map;
2352 if (status == CS_WAIT_STATUS_BUSY)
2355 mcs_data->completion_bitmap |= BIT(i);
2358 * best effort to extract timestamp. few notes:
2359 * - if even single fence is gone we cannot extract timestamp
2360 * (as fence not exist anymore)
2361 * - for all completed CSs we take the earliest timestamp.
2362 * for this we have to validate that:
2363 * 1. given timestamp was indeed set
2364 * 2. the timestamp is earliest of all timestamps so far
2367 if (status == CS_WAIT_STATUS_GONE) {
2368 mcs_data->update_ts = false;
2369 mcs_data->gone_cs = true;
2370 } else if (mcs_data->update_ts &&
2371 (ktime_compare(fence->timestamp,
2372 ktime_set(0, 0)) > 0) &&
2373 (ktime_compare(fence->timestamp, first_cs_time) < 0)) {
2374 first_cs_time = fence->timestamp;
2378 hl_fences_put(mcs_data->fence_arr, arr_len);
2380 if (mcs_data->update_ts &&
2381 (ktime_compare(first_cs_time, max_ktime) != 0))
2382 mcs_data->timestamp = ktime_to_ns(first_cs_time);
2387 static int _hl_cs_wait_ioctl(struct hl_device *hdev, struct hl_ctx *ctx,
2388 u64 timeout_us, u64 seq,
2389 enum hl_cs_wait_status *status, s64 *timestamp)
2391 struct hl_fence *fence;
2397 hl_ctx_get(hdev, ctx);
2399 fence = hl_ctx_get_fence(ctx, seq);
2401 rc = hl_wait_for_fence(ctx, seq, fence, status, timeout_us, timestamp);
2402 hl_fence_put(fence);
2409 * hl_wait_multi_cs_completion_init - init completion structure
2411 * @hdev: pointer to habanalabs device structure
2412 * @stream_master_bitmap: stream master QIDs map, set bit indicates stream
2413 * master QID to wait on
2415 * @return valid completion struct pointer on success, otherwise error pointer
2417 * up to MULTI_CS_MAX_USER_CTX calls can be done concurrently to the driver.
2418 * the function gets the first available completion (by marking it "used")
2419 * and initialize its values.
2421 static struct multi_cs_completion *hl_wait_multi_cs_completion_init(
2422 struct hl_device *hdev,
2423 u8 stream_master_bitmap)
2425 struct multi_cs_completion *mcs_compl;
2428 /* find free multi_cs completion structure */
2429 for (i = 0; i < MULTI_CS_MAX_USER_CTX; i++) {
2430 mcs_compl = &hdev->multi_cs_completion[i];
2431 spin_lock(&mcs_compl->lock);
2432 if (!mcs_compl->used) {
2433 mcs_compl->used = 1;
2434 mcs_compl->timestamp = 0;
2435 mcs_compl->stream_master_qid_map = stream_master_bitmap;
2436 reinit_completion(&mcs_compl->completion);
2437 spin_unlock(&mcs_compl->lock);
2440 spin_unlock(&mcs_compl->lock);
2443 if (i == MULTI_CS_MAX_USER_CTX) {
2445 "no available multi-CS completion structure\n");
2446 return ERR_PTR(-ENOMEM);
2452 * hl_wait_multi_cs_completion_fini - return completion structure and set as
2455 * @mcs_compl: pointer to the completion structure
2457 static void hl_wait_multi_cs_completion_fini(
2458 struct multi_cs_completion *mcs_compl)
2461 * free completion structure, do it under lock to be in-sync with the
2462 * thread that signals completion
2464 spin_lock(&mcs_compl->lock);
2465 mcs_compl->used = 0;
2466 spin_unlock(&mcs_compl->lock);
2470 * hl_wait_multi_cs_completion - wait for first CS to complete
2472 * @mcs_data: multi-CS internal data
2474 * @return 0 on success, otherwise non 0 error code
2476 static int hl_wait_multi_cs_completion(struct multi_cs_data *mcs_data)
2478 struct hl_device *hdev = mcs_data->ctx->hdev;
2479 struct multi_cs_completion *mcs_compl;
2482 mcs_compl = hl_wait_multi_cs_completion_init(hdev,
2483 mcs_data->stream_master_qid_map);
2484 if (IS_ERR(mcs_compl))
2485 return PTR_ERR(mcs_compl);
2487 completion_rc = wait_for_completion_interruptible_timeout(
2488 &mcs_compl->completion,
2489 usecs_to_jiffies(mcs_data->timeout_us));
2491 /* update timestamp */
2492 if (completion_rc > 0)
2493 mcs_data->timestamp = mcs_compl->timestamp;
2495 hl_wait_multi_cs_completion_fini(mcs_compl);
2497 mcs_data->wait_status = completion_rc;
2503 * hl_multi_cs_completion_init - init array of multi-CS completion structures
2505 * @hdev: pointer to habanalabs device structure
2507 void hl_multi_cs_completion_init(struct hl_device *hdev)
2509 struct multi_cs_completion *mcs_cmpl;
2512 for (i = 0; i < MULTI_CS_MAX_USER_CTX; i++) {
2513 mcs_cmpl = &hdev->multi_cs_completion[i];
2515 spin_lock_init(&mcs_cmpl->lock);
2516 init_completion(&mcs_cmpl->completion);
2521 * hl_multi_cs_wait_ioctl - implementation of the multi-CS wait ioctl
2523 * @hpriv: pointer to the private data of the fd
2524 * @data: pointer to multi-CS wait ioctl in/out args
2527 static int hl_multi_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data)
2529 struct hl_device *hdev = hpriv->hdev;
2530 struct multi_cs_data mcs_data = {0};
2531 union hl_wait_cs_args *args = data;
2532 struct hl_ctx *ctx = hpriv->ctx;
2533 struct hl_fence **fence_arr;
2534 void __user *seq_arr;
2540 if (!hdev->supports_wait_for_multi_cs) {
2541 dev_err(hdev->dev, "Wait for multi CS is not supported\n");
2545 seq_arr_len = args->in.seq_arr_len;
2547 if (seq_arr_len > HL_WAIT_MULTI_CS_LIST_MAX_LEN) {
2548 dev_err(hdev->dev, "Can wait only up to %d CSs, input sequence is of length %u\n",
2549 HL_WAIT_MULTI_CS_LIST_MAX_LEN, seq_arr_len);
2553 /* allocate memory for sequence array */
2555 kmalloc_array(seq_arr_len, sizeof(*cs_seq_arr), GFP_KERNEL);
2559 /* copy CS sequence array from user */
2560 seq_arr = (void __user *) (uintptr_t) args->in.seq;
2561 size_to_copy = seq_arr_len * sizeof(*cs_seq_arr);
2562 if (copy_from_user(cs_seq_arr, seq_arr, size_to_copy)) {
2563 dev_err(hdev->dev, "Failed to copy multi-cs sequence array from user\n");
2568 /* allocate array for the fences */
2569 fence_arr = kmalloc_array(seq_arr_len, sizeof(*fence_arr), GFP_KERNEL);
2575 /* initialize the multi-CS internal data */
2577 mcs_data.seq_arr = cs_seq_arr;
2578 mcs_data.fence_arr = fence_arr;
2579 mcs_data.arr_len = seq_arr_len;
2581 hl_ctx_get(hdev, ctx);
2583 /* poll all CS fences, extract timestamp */
2584 mcs_data.update_ts = true;
2585 rc = hl_cs_poll_fences(&mcs_data);
2587 * skip wait for CS completion when one of the below is true:
2588 * - an error on the poll function
2589 * - one or more CS in the list completed
2590 * - the user called ioctl with timeout 0
2592 if (rc || mcs_data.completion_bitmap || !args->in.timeout_us)
2595 /* wait (with timeout) for the first CS to be completed */
2596 mcs_data.timeout_us = args->in.timeout_us;
2597 rc = hl_wait_multi_cs_completion(&mcs_data);
2601 if (mcs_data.wait_status > 0) {
2603 * poll fences once again to update the CS map.
2604 * no timestamp should be updated this time.
2606 mcs_data.update_ts = false;
2607 rc = hl_cs_poll_fences(&mcs_data);
2610 * if hl_wait_multi_cs_completion returned before timeout (i.e.
2611 * it got a completion) we expect to see at least one CS
2612 * completed after the poll function.
2614 if (!mcs_data.completion_bitmap) {
2615 dev_err(hdev->dev, "Multi-CS got completion on wait but no CS completed\n");
2627 /* update output args */
2628 memset(args, 0, sizeof(*args));
2632 if (mcs_data.completion_bitmap) {
2633 args->out.status = HL_WAIT_CS_STATUS_COMPLETED;
2634 args->out.cs_completion_map = mcs_data.completion_bitmap;
2636 /* if timestamp not 0- it's valid */
2637 if (mcs_data.timestamp) {
2638 args->out.timestamp_nsec = mcs_data.timestamp;
2639 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_TIMESTAMP_VLD;
2642 /* update if some CS was gone */
2643 if (mcs_data.timestamp)
2644 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_GONE;
2645 } else if (mcs_data.wait_status == -ERESTARTSYS) {
2646 args->out.status = HL_WAIT_CS_STATUS_INTERRUPTED;
2648 args->out.status = HL_WAIT_CS_STATUS_BUSY;
2654 static int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data)
2656 struct hl_device *hdev = hpriv->hdev;
2657 union hl_wait_cs_args *args = data;
2658 enum hl_cs_wait_status status;
2659 u64 seq = args->in.seq;
2663 rc = _hl_cs_wait_ioctl(hdev, hpriv->ctx, args->in.timeout_us, seq,
2664 &status, ×tamp);
2666 memset(args, 0, sizeof(*args));
2669 if (rc == -ERESTARTSYS) {
2670 dev_err_ratelimited(hdev->dev,
2671 "user process got signal while waiting for CS handle %llu\n",
2673 args->out.status = HL_WAIT_CS_STATUS_INTERRUPTED;
2675 } else if (rc == -ETIMEDOUT) {
2676 dev_err_ratelimited(hdev->dev,
2677 "CS %llu has timed-out while user process is waiting for it\n",
2679 args->out.status = HL_WAIT_CS_STATUS_TIMEDOUT;
2680 } else if (rc == -EIO) {
2681 dev_err_ratelimited(hdev->dev,
2682 "CS %llu has been aborted while user process is waiting for it\n",
2684 args->out.status = HL_WAIT_CS_STATUS_ABORTED;
2690 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_TIMESTAMP_VLD;
2691 args->out.timestamp_nsec = timestamp;
2695 case CS_WAIT_STATUS_GONE:
2696 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_GONE;
2698 case CS_WAIT_STATUS_COMPLETED:
2699 args->out.status = HL_WAIT_CS_STATUS_COMPLETED;
2701 case CS_WAIT_STATUS_BUSY:
2703 args->out.status = HL_WAIT_CS_STATUS_BUSY;
2710 static int _hl_interrupt_wait_ioctl(struct hl_device *hdev, struct hl_ctx *ctx,
2711 u32 timeout_us, u64 user_address,
2712 u32 target_value, u16 interrupt_offset,
2713 enum hl_cs_wait_status *status)
2715 struct hl_user_pending_interrupt *pend;
2716 struct hl_user_interrupt *interrupt;
2717 unsigned long timeout;
2719 u32 completion_value;
2722 if (timeout_us == U32_MAX)
2723 timeout = timeout_us;
2725 timeout = usecs_to_jiffies(timeout_us);
2727 hl_ctx_get(hdev, ctx);
2729 pend = kmalloc(sizeof(*pend), GFP_KERNEL);
2735 hl_fence_init(&pend->fence, ULONG_MAX);
2737 if (interrupt_offset == HL_COMMON_USER_INTERRUPT_ID)
2738 interrupt = &hdev->common_user_interrupt;
2740 interrupt = &hdev->user_interrupt[interrupt_offset];
2742 spin_lock(&interrupt->wait_list_lock);
2743 if (!hl_device_operational(hdev, NULL)) {
2745 goto unlock_and_free_fence;
2748 if (copy_from_user(&completion_value, u64_to_user_ptr(user_address),
2751 "Failed to copy completion value from user\n");
2753 goto unlock_and_free_fence;
2756 if (completion_value >= target_value)
2757 *status = CS_WAIT_STATUS_COMPLETED;
2759 *status = CS_WAIT_STATUS_BUSY;
2761 if (!timeout_us || (*status == CS_WAIT_STATUS_COMPLETED))
2762 goto unlock_and_free_fence;
2764 /* Add pending user interrupt to relevant list for the interrupt
2765 * handler to monitor
2767 list_add_tail(&pend->wait_list_node, &interrupt->wait_list_head);
2768 spin_unlock(&interrupt->wait_list_lock);
2771 /* Wait for interrupt handler to signal completion */
2773 wait_for_completion_interruptible_timeout(
2774 &pend->fence.completion, timeout);
2776 /* If timeout did not expire we need to perform the comparison.
2777 * If comparison fails, keep waiting until timeout expires
2779 if (completion_rc > 0) {
2780 spin_lock(&interrupt->wait_list_lock);
2782 if (copy_from_user(&completion_value,
2783 u64_to_user_ptr(user_address), 4)) {
2785 spin_unlock(&interrupt->wait_list_lock);
2788 "Failed to copy completion value from user\n");
2791 goto remove_pending_user_interrupt;
2794 if (completion_value >= target_value) {
2795 spin_unlock(&interrupt->wait_list_lock);
2796 *status = CS_WAIT_STATUS_COMPLETED;
2798 reinit_completion(&pend->fence.completion);
2799 timeout = completion_rc;
2801 spin_unlock(&interrupt->wait_list_lock);
2804 } else if (completion_rc == -ERESTARTSYS) {
2805 dev_err_ratelimited(hdev->dev,
2806 "user process got signal while waiting for interrupt ID %d\n",
2807 interrupt->interrupt_id);
2808 *status = HL_WAIT_CS_STATUS_INTERRUPTED;
2811 *status = CS_WAIT_STATUS_BUSY;
2814 remove_pending_user_interrupt:
2815 spin_lock(&interrupt->wait_list_lock);
2816 list_del(&pend->wait_list_node);
2818 unlock_and_free_fence:
2819 spin_unlock(&interrupt->wait_list_lock);
2826 static int hl_interrupt_wait_ioctl(struct hl_fpriv *hpriv, void *data)
2828 u16 interrupt_id, interrupt_offset, first_interrupt, last_interrupt;
2829 struct hl_device *hdev = hpriv->hdev;
2830 struct asic_fixed_properties *prop;
2831 union hl_wait_cs_args *args = data;
2832 enum hl_cs_wait_status status;
2835 prop = &hdev->asic_prop;
2837 if (!prop->user_interrupt_count) {
2838 dev_err(hdev->dev, "no user interrupts allowed");
2843 FIELD_GET(HL_WAIT_CS_FLAGS_INTERRUPT_MASK, args->in.flags);
2845 first_interrupt = prop->first_available_user_msix_interrupt;
2846 last_interrupt = prop->first_available_user_msix_interrupt +
2847 prop->user_interrupt_count - 1;
2849 if ((interrupt_id < first_interrupt || interrupt_id > last_interrupt) &&
2850 interrupt_id != HL_COMMON_USER_INTERRUPT_ID) {
2851 dev_err(hdev->dev, "invalid user interrupt %u", interrupt_id);
2855 if (interrupt_id == HL_COMMON_USER_INTERRUPT_ID)
2856 interrupt_offset = HL_COMMON_USER_INTERRUPT_ID;
2858 interrupt_offset = interrupt_id - first_interrupt;
2860 rc = _hl_interrupt_wait_ioctl(hdev, hpriv->ctx,
2861 args->in.interrupt_timeout_us, args->in.addr,
2862 args->in.target, interrupt_offset, &status);
2864 memset(args, 0, sizeof(*args));
2868 dev_err_ratelimited(hdev->dev,
2869 "interrupt_wait_ioctl failed (%d)\n", rc);
2875 case CS_WAIT_STATUS_COMPLETED:
2876 args->out.status = HL_WAIT_CS_STATUS_COMPLETED;
2878 case CS_WAIT_STATUS_BUSY:
2880 args->out.status = HL_WAIT_CS_STATUS_BUSY;
2887 int hl_wait_ioctl(struct hl_fpriv *hpriv, void *data)
2889 union hl_wait_cs_args *args = data;
2890 u32 flags = args->in.flags;
2893 if (flags & HL_WAIT_CS_FLAGS_INTERRUPT)
2894 rc = hl_interrupt_wait_ioctl(hpriv, data);
2895 else if (flags & HL_WAIT_CS_FLAGS_MULTI_CS)
2896 rc = hl_multi_cs_wait_ioctl(hpriv, data);
2898 rc = hl_cs_wait_ioctl(hpriv, data);