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_SIG_WAIT (HL_CS_FLAGS_SIGNAL | HL_CS_FLAGS_WAIT)
16 static void job_wq_completion(struct work_struct *work);
17 static long _hl_cs_wait_ioctl(struct hl_device *hdev,
18 struct hl_ctx *ctx, u64 timeout_us, u64 seq);
19 static void cs_do_release(struct kref *ref);
21 static void hl_sob_reset(struct kref *ref)
23 struct hl_hw_sob *hw_sob = container_of(ref, struct hl_hw_sob,
25 struct hl_device *hdev = hw_sob->hdev;
27 hdev->asic_funcs->reset_sob(hdev, hw_sob);
30 void hl_sob_reset_error(struct kref *ref)
32 struct hl_hw_sob *hw_sob = container_of(ref, struct hl_hw_sob,
34 struct hl_device *hdev = hw_sob->hdev;
37 "SOB release shouldn't be called here, q_idx: %d, sob_id: %d\n",
38 hw_sob->q_idx, hw_sob->sob_id);
41 static void hl_fence_release(struct kref *kref)
43 struct hl_fence *fence =
44 container_of(kref, struct hl_fence, refcount);
45 struct hl_cs_compl *hl_cs_cmpl =
46 container_of(fence, struct hl_cs_compl, base_fence);
47 struct hl_device *hdev = hl_cs_cmpl->hdev;
49 /* EBUSY means the CS was never submitted and hence we don't have
50 * an attached hw_sob object that we should handle here
52 if (fence->error == -EBUSY)
55 if ((hl_cs_cmpl->type == CS_TYPE_SIGNAL) ||
56 (hl_cs_cmpl->type == CS_TYPE_WAIT)) {
59 "CS 0x%llx type %d finished, sob_id: %d, sob_val: 0x%x\n",
62 hl_cs_cmpl->hw_sob->sob_id,
66 * A signal CS can get completion while the corresponding wait
67 * for signal CS is on its way to the PQ. The wait for signal CS
68 * will get stuck if the signal CS incremented the SOB to its
69 * max value and there are no pending (submitted) waits on this
71 * We do the following to void this situation:
72 * 1. The wait for signal CS must get a ref for the signal CS as
73 * soon as possible in cs_ioctl_signal_wait() and put it
74 * before being submitted to the PQ but after it incremented
75 * the SOB refcnt in init_signal_wait_cs().
76 * 2. Signal/Wait for signal CS will decrement the SOB refcnt
78 * These two measures guarantee that the wait for signal CS will
79 * reset the SOB upon completion rather than the signal CS and
80 * hence the above scenario is avoided.
82 kref_put(&hl_cs_cmpl->hw_sob->kref, hl_sob_reset);
89 void hl_fence_put(struct hl_fence *fence)
92 kref_put(&fence->refcount, hl_fence_release);
95 void hl_fence_get(struct hl_fence *fence)
98 kref_get(&fence->refcount);
101 static void hl_fence_init(struct hl_fence *fence)
103 kref_init(&fence->refcount);
105 init_completion(&fence->completion);
108 static void cs_get(struct hl_cs *cs)
110 kref_get(&cs->refcount);
113 static int cs_get_unless_zero(struct hl_cs *cs)
115 return kref_get_unless_zero(&cs->refcount);
118 static void cs_put(struct hl_cs *cs)
120 kref_put(&cs->refcount, cs_do_release);
123 static bool is_cb_patched(struct hl_device *hdev, struct hl_cs_job *job)
126 * Patched CB is created for external queues jobs, and for H/W queues
127 * jobs if the user CB was allocated by driver and MMU is disabled.
129 return (job->queue_type == QUEUE_TYPE_EXT ||
130 (job->queue_type == QUEUE_TYPE_HW &&
131 job->is_kernel_allocated_cb &&
136 * cs_parser - parse the user command submission
138 * @hpriv : pointer to the private data of the fd
139 * @job : pointer to the job that holds the command submission info
141 * The function parses the command submission of the user. It calls the
142 * ASIC specific parser, which returns a list of memory blocks to send
143 * to the device as different command buffers
146 static int cs_parser(struct hl_fpriv *hpriv, struct hl_cs_job *job)
148 struct hl_device *hdev = hpriv->hdev;
149 struct hl_cs_parser parser;
152 parser.ctx_id = job->cs->ctx->asid;
153 parser.cs_sequence = job->cs->sequence;
154 parser.job_id = job->id;
156 parser.hw_queue_id = job->hw_queue_id;
157 parser.job_userptr_list = &job->userptr_list;
158 parser.patched_cb = NULL;
159 parser.user_cb = job->user_cb;
160 parser.user_cb_size = job->user_cb_size;
161 parser.queue_type = job->queue_type;
162 parser.is_kernel_allocated_cb = job->is_kernel_allocated_cb;
163 job->patched_cb = NULL;
165 rc = hdev->asic_funcs->cs_parser(hdev, &parser);
167 if (is_cb_patched(hdev, job)) {
169 job->patched_cb = parser.patched_cb;
170 job->job_cb_size = parser.patched_cb_size;
171 job->contains_dma_pkt = parser.contains_dma_pkt;
173 spin_lock(&job->patched_cb->lock);
174 job->patched_cb->cs_cnt++;
175 spin_unlock(&job->patched_cb->lock);
179 * Whether the parsing worked or not, we don't need the
180 * original CB anymore because it was already parsed and
181 * won't be accessed again for this CS
183 spin_lock(&job->user_cb->lock);
184 job->user_cb->cs_cnt--;
185 spin_unlock(&job->user_cb->lock);
186 hl_cb_put(job->user_cb);
189 job->job_cb_size = job->user_cb_size;
195 static void free_job(struct hl_device *hdev, struct hl_cs_job *job)
197 struct hl_cs *cs = job->cs;
199 if (is_cb_patched(hdev, job)) {
200 hl_userptr_delete_list(hdev, &job->userptr_list);
203 * We might arrive here from rollback and patched CB wasn't
204 * created, so we need to check it's not NULL
206 if (job->patched_cb) {
207 spin_lock(&job->patched_cb->lock);
208 job->patched_cb->cs_cnt--;
209 spin_unlock(&job->patched_cb->lock);
211 hl_cb_put(job->patched_cb);
215 /* For H/W queue jobs, if a user CB was allocated by driver and MMU is
216 * enabled, the user CB isn't released in cs_parser() and thus should be
219 if (job->queue_type == QUEUE_TYPE_HW &&
220 job->is_kernel_allocated_cb && hdev->mmu_enable) {
221 spin_lock(&job->user_cb->lock);
222 job->user_cb->cs_cnt--;
223 spin_unlock(&job->user_cb->lock);
225 hl_cb_put(job->user_cb);
229 * This is the only place where there can be multiple threads
230 * modifying the list at the same time
232 spin_lock(&cs->job_lock);
233 list_del(&job->cs_node);
234 spin_unlock(&cs->job_lock);
236 hl_debugfs_remove_job(hdev, job);
238 if (job->queue_type == QUEUE_TYPE_EXT ||
239 job->queue_type == QUEUE_TYPE_HW)
245 static void cs_counters_aggregate(struct hl_device *hdev, struct hl_ctx *ctx)
247 hdev->aggregated_cs_counters.device_in_reset_drop_cnt +=
248 ctx->cs_counters.device_in_reset_drop_cnt;
249 hdev->aggregated_cs_counters.out_of_mem_drop_cnt +=
250 ctx->cs_counters.out_of_mem_drop_cnt;
251 hdev->aggregated_cs_counters.parsing_drop_cnt +=
252 ctx->cs_counters.parsing_drop_cnt;
253 hdev->aggregated_cs_counters.queue_full_drop_cnt +=
254 ctx->cs_counters.queue_full_drop_cnt;
255 hdev->aggregated_cs_counters.max_cs_in_flight_drop_cnt +=
256 ctx->cs_counters.max_cs_in_flight_drop_cnt;
259 static void cs_do_release(struct kref *ref)
261 struct hl_cs *cs = container_of(ref, struct hl_cs,
263 struct hl_device *hdev = cs->ctx->hdev;
264 struct hl_cs_job *job, *tmp;
266 cs->completed = true;
269 * Although if we reached here it means that all external jobs have
270 * finished, because each one of them took refcnt to CS, we still
271 * need to go over the internal jobs and free them. Otherwise, we
272 * will have leaked memory and what's worse, the CS object (and
273 * potentially the CTX object) could be released, while the JOB
274 * still holds a pointer to them (but no reference).
276 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
279 /* We also need to update CI for internal queues */
281 hdev->asic_funcs->hw_queues_lock(hdev);
283 hdev->cs_active_cnt--;
284 if (!hdev->cs_active_cnt) {
285 struct hl_device_idle_busy_ts *ts;
287 ts = &hdev->idle_busy_ts_arr[hdev->idle_busy_ts_idx++];
288 ts->busy_to_idle_ts = ktime_get();
290 if (hdev->idle_busy_ts_idx == HL_IDLE_BUSY_TS_ARR_SIZE)
291 hdev->idle_busy_ts_idx = 0;
292 } else if (hdev->cs_active_cnt < 0) {
293 dev_crit(hdev->dev, "CS active cnt %d is negative\n",
294 hdev->cs_active_cnt);
297 hdev->asic_funcs->hw_queues_unlock(hdev);
299 hl_int_hw_queue_update_ci(cs);
301 spin_lock(&hdev->hw_queues_mirror_lock);
302 /* remove CS from hw_queues mirror list */
303 list_del_init(&cs->mirror_node);
304 spin_unlock(&hdev->hw_queues_mirror_lock);
307 * Don't cancel TDR in case this CS was timedout because we
308 * might be running from the TDR context
310 if ((!cs->timedout) &&
311 (hdev->timeout_jiffies != MAX_SCHEDULE_TIMEOUT)) {
315 cancel_delayed_work_sync(&cs->work_tdr);
317 spin_lock(&hdev->hw_queues_mirror_lock);
319 /* queue TDR for next CS */
320 next = list_first_entry_or_null(
321 &hdev->hw_queues_mirror_list,
322 struct hl_cs, mirror_node);
324 if ((next) && (!next->tdr_active)) {
325 next->tdr_active = true;
326 schedule_delayed_work(&next->work_tdr,
327 hdev->timeout_jiffies);
330 spin_unlock(&hdev->hw_queues_mirror_lock);
332 } else if (cs->type == CS_TYPE_WAIT) {
334 * In case the wait for signal CS was submitted, the put occurs
335 * in init_signal_wait_cs() right before hanging on the PQ.
337 hl_fence_put(cs->signal_fence);
341 * Must be called before hl_ctx_put because inside we use ctx to get
344 hl_debugfs_remove_cs(cs);
348 /* We need to mark an error for not submitted because in that case
349 * the hl fence release flow is different. Mainly, we don't need
350 * to handle hw_sob for signal/wait
353 cs->fence->error = -ETIMEDOUT;
354 else if (cs->aborted)
355 cs->fence->error = -EIO;
356 else if (!cs->submitted)
357 cs->fence->error = -EBUSY;
359 complete_all(&cs->fence->completion);
360 hl_fence_put(cs->fence);
361 cs_counters_aggregate(hdev, cs->ctx);
363 kfree(cs->jobs_in_queue_cnt);
367 static void cs_timedout(struct work_struct *work)
369 struct hl_device *hdev;
371 struct hl_cs *cs = container_of(work, struct hl_cs,
373 rc = cs_get_unless_zero(cs);
377 if ((!cs->submitted) || (cs->completed)) {
382 /* Mark the CS is timed out so we won't try to cancel its TDR */
385 hdev = cs->ctx->hdev;
388 "Command submission %llu has not finished in time!\n",
393 if (hdev->reset_on_lockup)
394 hl_device_reset(hdev, false, false);
397 static int allocate_cs(struct hl_device *hdev, struct hl_ctx *ctx,
398 enum hl_cs_type cs_type, struct hl_cs **cs_new)
400 struct hl_cs_compl *cs_cmpl;
401 struct hl_fence *other = NULL;
405 cs = kzalloc(sizeof(*cs), GFP_ATOMIC);
410 cs->submitted = false;
411 cs->completed = false;
413 INIT_LIST_HEAD(&cs->job_list);
414 INIT_DELAYED_WORK(&cs->work_tdr, cs_timedout);
415 kref_init(&cs->refcount);
416 spin_lock_init(&cs->job_lock);
418 cs_cmpl = kmalloc(sizeof(*cs_cmpl), GFP_ATOMIC);
424 cs_cmpl->hdev = hdev;
425 cs_cmpl->type = cs->type;
426 spin_lock_init(&cs_cmpl->lock);
427 cs->fence = &cs_cmpl->base_fence;
429 spin_lock(&ctx->cs_lock);
431 cs_cmpl->cs_seq = ctx->cs_sequence;
432 other = ctx->cs_pending[cs_cmpl->cs_seq &
433 (hdev->asic_prop.max_pending_cs - 1)];
435 if (other && !completion_done(&other->completion)) {
436 dev_dbg_ratelimited(hdev->dev,
437 "Rejecting CS because of too many in-flights CS\n");
438 ctx->cs_counters.max_cs_in_flight_drop_cnt++;
443 cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
444 sizeof(*cs->jobs_in_queue_cnt), GFP_ATOMIC);
445 if (!cs->jobs_in_queue_cnt) {
451 hl_fence_init(&cs_cmpl->base_fence);
453 cs->sequence = cs_cmpl->cs_seq;
455 ctx->cs_pending[cs_cmpl->cs_seq &
456 (hdev->asic_prop.max_pending_cs - 1)] =
457 &cs_cmpl->base_fence;
460 hl_fence_get(&cs_cmpl->base_fence);
464 spin_unlock(&ctx->cs_lock);
471 spin_unlock(&ctx->cs_lock);
478 static void cs_rollback(struct hl_device *hdev, struct hl_cs *cs)
480 struct hl_cs_job *job, *tmp;
482 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
486 void hl_cs_rollback_all(struct hl_device *hdev)
489 struct hl_cs *cs, *tmp;
491 /* flush all completions */
492 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
493 flush_workqueue(hdev->cq_wq[i]);
495 /* Make sure we don't have leftovers in the H/W queues mirror list */
496 list_for_each_entry_safe(cs, tmp, &hdev->hw_queues_mirror_list,
500 dev_warn_ratelimited(hdev->dev, "Killing CS %d.%llu\n",
501 cs->ctx->asid, cs->sequence);
502 cs_rollback(hdev, cs);
507 static void job_wq_completion(struct work_struct *work)
509 struct hl_cs_job *job = container_of(work, struct hl_cs_job,
511 struct hl_cs *cs = job->cs;
512 struct hl_device *hdev = cs->ctx->hdev;
514 /* job is no longer needed */
518 static int validate_queue_index(struct hl_device *hdev,
519 struct hl_cs_chunk *chunk,
520 enum hl_queue_type *queue_type,
521 bool *is_kernel_allocated_cb)
523 struct asic_fixed_properties *asic = &hdev->asic_prop;
524 struct hw_queue_properties *hw_queue_prop;
526 /* This must be checked here to prevent out-of-bounds access to
527 * hw_queues_props array
529 if (chunk->queue_index >= asic->max_queues) {
530 dev_err(hdev->dev, "Queue index %d is invalid\n",
535 hw_queue_prop = &asic->hw_queues_props[chunk->queue_index];
537 if (hw_queue_prop->type == QUEUE_TYPE_NA) {
538 dev_err(hdev->dev, "Queue index %d is invalid\n",
543 if (hw_queue_prop->driver_only) {
545 "Queue index %d is restricted for the kernel driver\n",
550 *queue_type = hw_queue_prop->type;
551 *is_kernel_allocated_cb = !!hw_queue_prop->requires_kernel_cb;
556 static struct hl_cb *get_cb_from_cs_chunk(struct hl_device *hdev,
557 struct hl_cb_mgr *cb_mgr,
558 struct hl_cs_chunk *chunk)
563 cb_handle = (u32) (chunk->cb_handle >> PAGE_SHIFT);
565 cb = hl_cb_get(hdev, cb_mgr, cb_handle);
567 dev_err(hdev->dev, "CB handle 0x%x invalid\n", cb_handle);
571 if ((chunk->cb_size < 8) || (chunk->cb_size > cb->size)) {
572 dev_err(hdev->dev, "CB size %u invalid\n", chunk->cb_size);
576 spin_lock(&cb->lock);
578 spin_unlock(&cb->lock);
587 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
588 enum hl_queue_type queue_type, bool is_kernel_allocated_cb)
590 struct hl_cs_job *job;
592 job = kzalloc(sizeof(*job), GFP_ATOMIC);
596 job->queue_type = queue_type;
597 job->is_kernel_allocated_cb = is_kernel_allocated_cb;
599 if (is_cb_patched(hdev, job))
600 INIT_LIST_HEAD(&job->userptr_list);
602 if (job->queue_type == QUEUE_TYPE_EXT)
603 INIT_WORK(&job->finish_work, job_wq_completion);
608 static int cs_ioctl_default(struct hl_fpriv *hpriv, void __user *chunks,
609 u32 num_chunks, u64 *cs_seq)
611 struct hl_device *hdev = hpriv->hdev;
612 struct hl_cs_chunk *cs_chunk_array;
613 struct hl_cs_job *job;
616 bool int_queues_only = true;
620 *cs_seq = ULLONG_MAX;
622 if (num_chunks > HL_MAX_JOBS_PER_CS) {
624 "Number of chunks can NOT be larger than %d\n",
630 cs_chunk_array = kmalloc_array(num_chunks, sizeof(*cs_chunk_array),
632 if (!cs_chunk_array) {
637 size_to_copy = num_chunks * sizeof(struct hl_cs_chunk);
638 if (copy_from_user(cs_chunk_array, chunks, size_to_copy)) {
639 dev_err(hdev->dev, "Failed to copy cs chunk array from user\n");
641 goto free_cs_chunk_array;
644 /* increment refcnt for context */
645 hl_ctx_get(hdev, hpriv->ctx);
647 rc = allocate_cs(hdev, hpriv->ctx, CS_TYPE_DEFAULT, &cs);
649 hl_ctx_put(hpriv->ctx);
650 goto free_cs_chunk_array;
653 *cs_seq = cs->sequence;
655 hl_debugfs_add_cs(cs);
657 /* Validate ALL the CS chunks before submitting the CS */
658 for (i = 0 ; i < num_chunks ; i++) {
659 struct hl_cs_chunk *chunk = &cs_chunk_array[i];
660 enum hl_queue_type queue_type;
661 bool is_kernel_allocated_cb;
663 rc = validate_queue_index(hdev, chunk, &queue_type,
664 &is_kernel_allocated_cb);
666 hpriv->ctx->cs_counters.parsing_drop_cnt++;
670 if (is_kernel_allocated_cb) {
671 cb = get_cb_from_cs_chunk(hdev, &hpriv->cb_mgr, chunk);
673 hpriv->ctx->cs_counters.parsing_drop_cnt++;
678 cb = (struct hl_cb *) (uintptr_t) chunk->cb_handle;
681 if (queue_type == QUEUE_TYPE_EXT || queue_type == QUEUE_TYPE_HW)
682 int_queues_only = false;
684 job = hl_cs_allocate_job(hdev, queue_type,
685 is_kernel_allocated_cb);
687 hpriv->ctx->cs_counters.out_of_mem_drop_cnt++;
688 dev_err(hdev->dev, "Failed to allocate a new job\n");
690 if (is_kernel_allocated_cb)
699 job->user_cb_size = chunk->cb_size;
700 job->hw_queue_id = chunk->queue_index;
702 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
704 list_add_tail(&job->cs_node, &cs->job_list);
707 * Increment CS reference. When CS reference is 0, CS is
708 * done and can be signaled to user and free all its resources
709 * Only increment for JOB on external or H/W queues, because
710 * only for those JOBs we get completion
712 if (job->queue_type == QUEUE_TYPE_EXT ||
713 job->queue_type == QUEUE_TYPE_HW)
716 hl_debugfs_add_job(hdev, job);
718 rc = cs_parser(hpriv, job);
720 hpriv->ctx->cs_counters.parsing_drop_cnt++;
722 "Failed to parse JOB %d.%llu.%d, err %d, rejecting the CS\n",
723 cs->ctx->asid, cs->sequence, job->id, rc);
728 if (int_queues_only) {
729 hpriv->ctx->cs_counters.parsing_drop_cnt++;
731 "Reject CS %d.%llu because only internal queues jobs are present\n",
732 cs->ctx->asid, cs->sequence);
737 rc = hl_hw_queue_schedule_cs(cs);
741 "Failed to submit CS %d.%llu to H/W queues, error %d\n",
742 cs->ctx->asid, cs->sequence, rc);
746 rc = HL_CS_STATUS_SUCCESS;
750 spin_lock(&cb->lock);
752 spin_unlock(&cb->lock);
755 cs_rollback(hdev, cs);
756 *cs_seq = ULLONG_MAX;
757 /* The path below is both for good and erroneous exits */
759 /* We finished with the CS in this function, so put the ref */
762 kfree(cs_chunk_array);
767 static int cs_ioctl_signal_wait(struct hl_fpriv *hpriv, enum hl_cs_type cs_type,
768 void __user *chunks, u32 num_chunks,
771 struct hl_device *hdev = hpriv->hdev;
772 struct hl_ctx *ctx = hpriv->ctx;
773 struct hl_cs_chunk *cs_chunk_array, *chunk;
774 struct hw_queue_properties *hw_queue_prop;
775 struct hl_fence *sig_fence = NULL;
776 struct hl_cs_job *job;
779 enum hl_queue_type q_type;
780 u64 *signal_seq_arr = NULL, signal_seq;
781 u32 size_to_copy, q_idx, signal_seq_arr_len, cb_size;
784 *cs_seq = ULLONG_MAX;
786 if (num_chunks > HL_MAX_JOBS_PER_CS) {
788 "Number of chunks can NOT be larger than %d\n",
794 cs_chunk_array = kmalloc_array(num_chunks, sizeof(*cs_chunk_array),
796 if (!cs_chunk_array) {
801 size_to_copy = num_chunks * sizeof(struct hl_cs_chunk);
802 if (copy_from_user(cs_chunk_array, chunks, size_to_copy)) {
803 dev_err(hdev->dev, "Failed to copy cs chunk array from user\n");
805 goto free_cs_chunk_array;
808 /* currently it is guaranteed to have only one chunk */
809 chunk = &cs_chunk_array[0];
811 if (chunk->queue_index >= hdev->asic_prop.max_queues) {
812 dev_err(hdev->dev, "Queue index %d is invalid\n",
815 goto free_cs_chunk_array;
818 q_idx = chunk->queue_index;
819 hw_queue_prop = &hdev->asic_prop.hw_queues_props[q_idx];
820 q_type = hw_queue_prop->type;
822 if ((q_idx >= hdev->asic_prop.max_queues) ||
823 (!hw_queue_prop->supports_sync_stream)) {
824 dev_err(hdev->dev, "Queue index %d is invalid\n", q_idx);
826 goto free_cs_chunk_array;
829 if (cs_type == CS_TYPE_WAIT) {
830 struct hl_cs_compl *sig_waitcs_cmpl;
832 signal_seq_arr_len = chunk->num_signal_seq_arr;
834 /* currently only one signal seq is supported */
835 if (signal_seq_arr_len != 1) {
837 "Wait for signal CS supports only one signal CS seq\n");
839 goto free_cs_chunk_array;
842 signal_seq_arr = kmalloc_array(signal_seq_arr_len,
843 sizeof(*signal_seq_arr),
845 if (!signal_seq_arr) {
847 goto free_cs_chunk_array;
850 size_to_copy = chunk->num_signal_seq_arr *
851 sizeof(*signal_seq_arr);
852 if (copy_from_user(signal_seq_arr,
853 u64_to_user_ptr(chunk->signal_seq_arr),
856 "Failed to copy signal seq array from user\n");
858 goto free_signal_seq_array;
861 /* currently it is guaranteed to have only one signal seq */
862 signal_seq = signal_seq_arr[0];
863 sig_fence = hl_ctx_get_fence(ctx, signal_seq);
864 if (IS_ERR(sig_fence)) {
866 "Failed to get signal CS with seq 0x%llx\n",
868 rc = PTR_ERR(sig_fence);
869 goto free_signal_seq_array;
873 /* signal CS already finished */
875 goto free_signal_seq_array;
879 container_of(sig_fence, struct hl_cs_compl, base_fence);
881 if (sig_waitcs_cmpl->type != CS_TYPE_SIGNAL) {
883 "CS seq 0x%llx is not of a signal CS\n",
885 hl_fence_put(sig_fence);
887 goto free_signal_seq_array;
890 if (completion_done(&sig_fence->completion)) {
891 /* signal CS already finished */
892 hl_fence_put(sig_fence);
894 goto free_signal_seq_array;
898 /* increment refcnt for context */
899 hl_ctx_get(hdev, ctx);
901 rc = allocate_cs(hdev, ctx, cs_type, &cs);
903 if (cs_type == CS_TYPE_WAIT)
904 hl_fence_put(sig_fence);
906 goto free_signal_seq_array;
910 * Save the signal CS fence for later initialization right before
911 * hanging the wait CS on the queue.
913 if (cs->type == CS_TYPE_WAIT)
914 cs->signal_fence = sig_fence;
916 hl_debugfs_add_cs(cs);
918 *cs_seq = cs->sequence;
920 job = hl_cs_allocate_job(hdev, q_type, true);
922 ctx->cs_counters.out_of_mem_drop_cnt++;
923 dev_err(hdev->dev, "Failed to allocate a new job\n");
928 if (cs->type == CS_TYPE_WAIT)
929 cb_size = hdev->asic_funcs->get_wait_cb_size(hdev);
931 cb_size = hdev->asic_funcs->get_signal_cb_size(hdev);
933 cb = hl_cb_kernel_create(hdev, cb_size,
934 q_type == QUEUE_TYPE_HW && hdev->mmu_enable);
936 ctx->cs_counters.out_of_mem_drop_cnt++;
945 job->user_cb->cs_cnt++;
946 job->user_cb_size = cb_size;
947 job->hw_queue_id = q_idx;
950 * No need in parsing, user CB is the patched CB.
951 * We call hl_cb_destroy() out of two reasons - we don't need the CB in
952 * the CB idr anymore and to decrement its refcount as it was
953 * incremented inside hl_cb_kernel_create().
955 job->patched_cb = job->user_cb;
956 job->job_cb_size = job->user_cb_size;
957 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
959 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
961 list_add_tail(&job->cs_node, &cs->job_list);
963 /* increment refcount as for external queues we get completion */
966 hl_debugfs_add_job(hdev, job);
968 rc = hl_hw_queue_schedule_cs(cs);
972 "Failed to submit CS %d.%llu to H/W queues, error %d\n",
973 ctx->asid, cs->sequence, rc);
977 rc = HL_CS_STATUS_SUCCESS;
981 cs_rollback(hdev, cs);
982 *cs_seq = ULLONG_MAX;
983 /* The path below is both for good and erroneous exits */
985 /* We finished with the CS in this function, so put the ref */
987 free_signal_seq_array:
988 if (cs_type == CS_TYPE_WAIT)
989 kfree(signal_seq_arr);
991 kfree(cs_chunk_array);
996 int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data)
998 struct hl_device *hdev = hpriv->hdev;
999 union hl_cs_args *args = data;
1000 struct hl_ctx *ctx = hpriv->ctx;
1001 void __user *chunks_execute, *chunks_restore;
1002 enum hl_cs_type cs_type;
1003 u32 num_chunks_execute, num_chunks_restore, sig_wait_flags;
1004 u64 cs_seq = ULONG_MAX;
1005 int rc, do_ctx_switch;
1006 bool need_soft_reset = false;
1008 if (hl_device_disabled_or_in_reset(hdev)) {
1009 dev_warn_ratelimited(hdev->dev,
1010 "Device is %s. Can't submit new CS\n",
1011 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
1016 sig_wait_flags = args->in.cs_flags & HL_CS_FLAGS_SIG_WAIT;
1018 if (unlikely(sig_wait_flags == HL_CS_FLAGS_SIG_WAIT)) {
1020 "Signal and wait CS flags are mutually exclusive, context %d\n",
1026 if (unlikely((sig_wait_flags & HL_CS_FLAGS_SIG_WAIT) &&
1027 (!hdev->supports_sync_stream))) {
1028 dev_err(hdev->dev, "Sync stream CS is not supported\n");
1033 if (args->in.cs_flags & HL_CS_FLAGS_SIGNAL)
1034 cs_type = CS_TYPE_SIGNAL;
1035 else if (args->in.cs_flags & HL_CS_FLAGS_WAIT)
1036 cs_type = CS_TYPE_WAIT;
1038 cs_type = CS_TYPE_DEFAULT;
1040 chunks_execute = (void __user *) (uintptr_t) args->in.chunks_execute;
1041 num_chunks_execute = args->in.num_chunks_execute;
1043 if (cs_type == CS_TYPE_DEFAULT) {
1044 if (!num_chunks_execute) {
1046 "Got execute CS with 0 chunks, context %d\n",
1051 } else if (num_chunks_execute != 1) {
1053 "Sync stream CS mandates one chunk only, context %d\n",
1059 do_ctx_switch = atomic_cmpxchg(&ctx->thread_ctx_switch_token, 1, 0);
1061 if (do_ctx_switch || (args->in.cs_flags & HL_CS_FLAGS_FORCE_RESTORE)) {
1065 (void __user *) (uintptr_t) args->in.chunks_restore;
1066 num_chunks_restore = args->in.num_chunks_restore;
1068 mutex_lock(&hpriv->restore_phase_mutex);
1070 if (do_ctx_switch) {
1071 rc = hdev->asic_funcs->context_switch(hdev, ctx->asid);
1073 dev_err_ratelimited(hdev->dev,
1074 "Failed to switch to context %d, rejecting CS! %d\n",
1077 * If we timedout, or if the device is not IDLE
1078 * while we want to do context-switch (-EBUSY),
1079 * we need to soft-reset because QMAN is
1080 * probably stuck. However, we can't call to
1081 * reset here directly because of deadlock, so
1082 * need to do it at the very end of this
1085 if ((rc == -ETIMEDOUT) || (rc == -EBUSY))
1086 need_soft_reset = true;
1087 mutex_unlock(&hpriv->restore_phase_mutex);
1092 hdev->asic_funcs->restore_phase_topology(hdev);
1094 if (!num_chunks_restore) {
1096 "Need to run restore phase but restore CS is empty\n");
1099 rc = cs_ioctl_default(hpriv, chunks_restore,
1100 num_chunks_restore, &cs_seq);
1103 mutex_unlock(&hpriv->restore_phase_mutex);
1107 "Failed to submit restore CS for context %d (%d)\n",
1112 /* Need to wait for restore completion before execution phase */
1113 if (num_chunks_restore) {
1114 ret = _hl_cs_wait_ioctl(hdev, ctx,
1115 jiffies_to_usecs(hdev->timeout_jiffies),
1119 "Restore CS for context %d failed to complete %ld\n",
1126 ctx->thread_ctx_switch_wait_token = 1;
1127 } else if (!ctx->thread_ctx_switch_wait_token) {
1130 rc = hl_poll_timeout_memory(hdev,
1131 &ctx->thread_ctx_switch_wait_token, tmp, (tmp == 1),
1132 100, jiffies_to_usecs(hdev->timeout_jiffies), false);
1134 if (rc == -ETIMEDOUT) {
1136 "context switch phase timeout (%d)\n", tmp);
1141 if (cs_type == CS_TYPE_DEFAULT)
1142 rc = cs_ioctl_default(hpriv, chunks_execute, num_chunks_execute,
1145 rc = cs_ioctl_signal_wait(hpriv, cs_type, chunks_execute,
1146 num_chunks_execute, &cs_seq);
1149 if (rc != -EAGAIN) {
1150 memset(args, 0, sizeof(*args));
1151 args->out.status = rc;
1152 args->out.seq = cs_seq;
1155 if (((rc == -ETIMEDOUT) || (rc == -EBUSY)) && (need_soft_reset))
1156 hl_device_reset(hdev, false, false);
1161 static long _hl_cs_wait_ioctl(struct hl_device *hdev,
1162 struct hl_ctx *ctx, u64 timeout_us, u64 seq)
1164 struct hl_fence *fence;
1165 unsigned long timeout;
1168 if (timeout_us == MAX_SCHEDULE_TIMEOUT)
1169 timeout = timeout_us;
1171 timeout = usecs_to_jiffies(timeout_us);
1173 hl_ctx_get(hdev, ctx);
1175 fence = hl_ctx_get_fence(ctx, seq);
1176 if (IS_ERR(fence)) {
1177 rc = PTR_ERR(fence);
1179 dev_notice_ratelimited(hdev->dev,
1180 "Can't wait on CS %llu because current CS is at seq %llu\n",
1181 seq, ctx->cs_sequence);
1184 rc = completion_done(&fence->completion);
1186 rc = wait_for_completion_interruptible_timeout(
1187 &fence->completion, timeout);
1189 if (fence->error == -ETIMEDOUT)
1191 else if (fence->error == -EIO)
1194 hl_fence_put(fence);
1197 "Can't wait on seq %llu because current CS is at seq %llu (Fence is gone)\n",
1198 seq, ctx->cs_sequence);
1207 int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data)
1209 struct hl_device *hdev = hpriv->hdev;
1210 union hl_wait_cs_args *args = data;
1211 u64 seq = args->in.seq;
1214 rc = _hl_cs_wait_ioctl(hdev, hpriv->ctx, args->in.timeout_us, seq);
1216 memset(args, 0, sizeof(*args));
1219 if (rc == -ERESTARTSYS) {
1220 dev_err_ratelimited(hdev->dev,
1221 "user process got signal while waiting for CS handle %llu\n",
1223 args->out.status = HL_WAIT_CS_STATUS_INTERRUPTED;
1225 } else if (rc == -ETIMEDOUT) {
1226 dev_err_ratelimited(hdev->dev,
1227 "CS %llu has timed-out while user process is waiting for it\n",
1229 args->out.status = HL_WAIT_CS_STATUS_TIMEDOUT;
1230 } else if (rc == -EIO) {
1231 dev_err_ratelimited(hdev->dev,
1232 "CS %llu has been aborted while user process is waiting for it\n",
1234 args->out.status = HL_WAIT_CS_STATUS_ABORTED;
1240 args->out.status = HL_WAIT_CS_STATUS_BUSY;
1242 args->out.status = HL_WAIT_CS_STATUS_COMPLETED;