1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Copyright 2016-2021 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
8 #include <uapi/drm/habanalabs_accel.h>
9 #include "habanalabs.h"
10
11 #include <linux/uaccess.h>
12 #include <linux/slab.h>
13
14 #define HL_CS_FLAGS_TYPE_MASK (HL_CS_FLAGS_SIGNAL | HL_CS_FLAGS_WAIT | \
15 HL_CS_FLAGS_COLLECTIVE_WAIT | HL_CS_FLAGS_RESERVE_SIGNALS_ONLY | \
16 HL_CS_FLAGS_UNRESERVE_SIGNALS_ONLY | HL_CS_FLAGS_ENGINE_CORE_COMMAND | \
17 HL_CS_FLAGS_ENGINES_COMMAND | HL_CS_FLAGS_FLUSH_PCI_HBW_WRITES)
18
19
20 #define MAX_TS_ITER_NUM 100
21
22 /**
23 * enum hl_cs_wait_status - cs wait status
24 * @CS_WAIT_STATUS_BUSY: cs was not completed yet
25 * @CS_WAIT_STATUS_COMPLETED: cs completed
26 * @CS_WAIT_STATUS_GONE: cs completed but fence is already gone
27 */
28 enum hl_cs_wait_status {
29 CS_WAIT_STATUS_BUSY,
30 CS_WAIT_STATUS_COMPLETED,
31 CS_WAIT_STATUS_GONE
32 };
33
34 /*
35 * Data used while handling wait/timestamp nodes.
36 * The purpose of this struct is to store the needed data for both operations
37 * in one variable instead of passing large number of arguments to functions.
38 */
39 struct wait_interrupt_data {
40 struct hl_user_interrupt *interrupt;
41 struct hl_mmap_mem_buf *buf;
42 struct hl_mem_mgr *mmg;
43 struct hl_cb *cq_cb;
44 u64 ts_handle;
45 u64 ts_offset;
46 u64 cq_handle;
47 u64 cq_offset;
48 u64 target_value;
49 u64 intr_timeout_us;
50 };
51
52 static void job_wq_completion(struct work_struct *work);
53 static int _hl_cs_wait_ioctl(struct hl_device *hdev, struct hl_ctx *ctx, u64 timeout_us, u64 seq,
54 enum hl_cs_wait_status *status, s64 *timestamp);
55 static void cs_do_release(struct kref *ref);
56
hl_push_cs_outcome(struct hl_device * hdev,struct hl_cs_outcome_store * outcome_store,u64 seq,ktime_t ts,int error)57 static void hl_push_cs_outcome(struct hl_device *hdev,
58 struct hl_cs_outcome_store *outcome_store,
59 u64 seq, ktime_t ts, int error)
60 {
61 struct hl_cs_outcome *node;
62 unsigned long flags;
63
64 /*
65 * CS outcome store supports the following operations:
66 * push outcome - store a recent CS outcome in the store
67 * pop outcome - retrieve a SPECIFIC (by seq) CS outcome from the store
68 * It uses 2 lists: used list and free list.
69 * It has a pre-allocated amount of nodes, each node stores
70 * a single CS outcome.
71 * Initially, all the nodes are in the free list.
72 * On push outcome, a node (any) is taken from the free list, its
73 * information is filled in, and the node is moved to the used list.
74 * It is possible, that there are no nodes left in the free list.
75 * In this case, we will lose some information about old outcomes. We
76 * will pop the OLDEST node from the used list, and make it free.
77 * On pop, the node is searched for in the used list (using a search
78 * index).
79 * If found, the node is then removed from the used list, and moved
80 * back to the free list. The outcome data that the node contained is
81 * returned back to the user.
82 */
83
84 spin_lock_irqsave(&outcome_store->db_lock, flags);
85
86 if (list_empty(&outcome_store->free_list)) {
87 node = list_last_entry(&outcome_store->used_list,
88 struct hl_cs_outcome, list_link);
89 hash_del(&node->map_link);
90 dev_dbg(hdev->dev, "CS %llu outcome was lost\n", node->seq);
91 } else {
92 node = list_last_entry(&outcome_store->free_list,
93 struct hl_cs_outcome, list_link);
94 }
95
96 list_del_init(&node->list_link);
97
98 node->seq = seq;
99 node->ts = ts;
100 node->error = error;
101
102 list_add(&node->list_link, &outcome_store->used_list);
103 hash_add(outcome_store->outcome_map, &node->map_link, node->seq);
104
105 spin_unlock_irqrestore(&outcome_store->db_lock, flags);
106 }
107
hl_pop_cs_outcome(struct hl_cs_outcome_store * outcome_store,u64 seq,ktime_t * ts,int * error)108 static bool hl_pop_cs_outcome(struct hl_cs_outcome_store *outcome_store,
109 u64 seq, ktime_t *ts, int *error)
110 {
111 struct hl_cs_outcome *node;
112 unsigned long flags;
113
114 spin_lock_irqsave(&outcome_store->db_lock, flags);
115
116 hash_for_each_possible(outcome_store->outcome_map, node, map_link, seq)
117 if (node->seq == seq) {
118 *ts = node->ts;
119 *error = node->error;
120
121 hash_del(&node->map_link);
122 list_del_init(&node->list_link);
123 list_add(&node->list_link, &outcome_store->free_list);
124
125 spin_unlock_irqrestore(&outcome_store->db_lock, flags);
126
127 return true;
128 }
129
130 spin_unlock_irqrestore(&outcome_store->db_lock, flags);
131
132 return false;
133 }
134
hl_sob_reset(struct kref * ref)135 static void hl_sob_reset(struct kref *ref)
136 {
137 struct hl_hw_sob *hw_sob = container_of(ref, struct hl_hw_sob,
138 kref);
139 struct hl_device *hdev = hw_sob->hdev;
140
141 dev_dbg(hdev->dev, "reset sob id %u\n", hw_sob->sob_id);
142
143 hdev->asic_funcs->reset_sob(hdev, hw_sob);
144
145 hw_sob->need_reset = false;
146 }
147
hl_sob_reset_error(struct kref * ref)148 void hl_sob_reset_error(struct kref *ref)
149 {
150 struct hl_hw_sob *hw_sob = container_of(ref, struct hl_hw_sob,
151 kref);
152 struct hl_device *hdev = hw_sob->hdev;
153
154 dev_crit(hdev->dev,
155 "SOB release shouldn't be called here, q_idx: %d, sob_id: %d\n",
156 hw_sob->q_idx, hw_sob->sob_id);
157 }
158
hw_sob_put(struct hl_hw_sob * hw_sob)159 void hw_sob_put(struct hl_hw_sob *hw_sob)
160 {
161 if (hw_sob)
162 kref_put(&hw_sob->kref, hl_sob_reset);
163 }
164
hw_sob_put_err(struct hl_hw_sob * hw_sob)165 static void hw_sob_put_err(struct hl_hw_sob *hw_sob)
166 {
167 if (hw_sob)
168 kref_put(&hw_sob->kref, hl_sob_reset_error);
169 }
170
hw_sob_get(struct hl_hw_sob * hw_sob)171 void hw_sob_get(struct hl_hw_sob *hw_sob)
172 {
173 if (hw_sob)
174 kref_get(&hw_sob->kref);
175 }
176
177 /**
178 * hl_gen_sob_mask() - Generates a sob mask to be used in a monitor arm packet
179 * @sob_base: sob base id
180 * @sob_mask: sob user mask, each bit represents a sob offset from sob base
181 * @mask: generated mask
182 *
183 * Return: 0 if given parameters are valid
184 */
hl_gen_sob_mask(u16 sob_base,u8 sob_mask,u8 * mask)185 int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask)
186 {
187 int i;
188
189 if (sob_mask == 0)
190 return -EINVAL;
191
192 if (sob_mask == 0x1) {
193 *mask = ~(1 << (sob_base & 0x7));
194 } else {
195 /* find msb in order to verify sob range is valid */
196 for (i = BITS_PER_BYTE - 1 ; i >= 0 ; i--)
197 if (BIT(i) & sob_mask)
198 break;
199
200 if (i > (HL_MAX_SOBS_PER_MONITOR - (sob_base & 0x7) - 1))
201 return -EINVAL;
202
203 *mask = ~sob_mask;
204 }
205
206 return 0;
207 }
208
hl_fence_release(struct kref * kref)209 static void hl_fence_release(struct kref *kref)
210 {
211 struct hl_fence *fence =
212 container_of(kref, struct hl_fence, refcount);
213 struct hl_cs_compl *hl_cs_cmpl =
214 container_of(fence, struct hl_cs_compl, base_fence);
215
216 kfree(hl_cs_cmpl);
217 }
218
hl_fence_put(struct hl_fence * fence)219 void hl_fence_put(struct hl_fence *fence)
220 {
221 if (IS_ERR_OR_NULL(fence))
222 return;
223 kref_put(&fence->refcount, hl_fence_release);
224 }
225
hl_fences_put(struct hl_fence ** fence,int len)226 void hl_fences_put(struct hl_fence **fence, int len)
227 {
228 int i;
229
230 for (i = 0; i < len; i++, fence++)
231 hl_fence_put(*fence);
232 }
233
hl_fence_get(struct hl_fence * fence)234 void hl_fence_get(struct hl_fence *fence)
235 {
236 if (fence)
237 kref_get(&fence->refcount);
238 }
239
hl_fence_init(struct hl_fence * fence,u64 sequence)240 static void hl_fence_init(struct hl_fence *fence, u64 sequence)
241 {
242 kref_init(&fence->refcount);
243 fence->cs_sequence = sequence;
244 fence->error = 0;
245 fence->timestamp = ktime_set(0, 0);
246 fence->mcs_handling_done = false;
247 init_completion(&fence->completion);
248 }
249
cs_get(struct hl_cs * cs)250 void cs_get(struct hl_cs *cs)
251 {
252 kref_get(&cs->refcount);
253 }
254
cs_get_unless_zero(struct hl_cs * cs)255 static int cs_get_unless_zero(struct hl_cs *cs)
256 {
257 return kref_get_unless_zero(&cs->refcount);
258 }
259
cs_put(struct hl_cs * cs)260 static void cs_put(struct hl_cs *cs)
261 {
262 kref_put(&cs->refcount, cs_do_release);
263 }
264
cs_job_do_release(struct kref * ref)265 static void cs_job_do_release(struct kref *ref)
266 {
267 struct hl_cs_job *job = container_of(ref, struct hl_cs_job, refcount);
268
269 kfree(job);
270 }
271
hl_cs_job_put(struct hl_cs_job * job)272 static void hl_cs_job_put(struct hl_cs_job *job)
273 {
274 kref_put(&job->refcount, cs_job_do_release);
275 }
276
cs_needs_completion(struct hl_cs * cs)277 bool cs_needs_completion(struct hl_cs *cs)
278 {
279 /* In case this is a staged CS, only the last CS in sequence should
280 * get a completion, any non staged CS will always get a completion
281 */
282 if (cs->staged_cs && !cs->staged_last)
283 return false;
284
285 return true;
286 }
287
cs_needs_timeout(struct hl_cs * cs)288 bool cs_needs_timeout(struct hl_cs *cs)
289 {
290 /* In case this is a staged CS, only the first CS in sequence should
291 * get a timeout, any non staged CS will always get a timeout
292 */
293 if (cs->staged_cs && !cs->staged_first)
294 return false;
295
296 return true;
297 }
298
is_cb_patched(struct hl_device * hdev,struct hl_cs_job * job)299 static bool is_cb_patched(struct hl_device *hdev, struct hl_cs_job *job)
300 {
301 /* Patched CB is created for external queues jobs */
302 return (job->queue_type == QUEUE_TYPE_EXT);
303 }
304
305 /*
306 * cs_parser - parse the user command submission
307 *
308 * @hpriv : pointer to the private data of the fd
309 * @job : pointer to the job that holds the command submission info
310 *
311 * The function parses the command submission of the user. It calls the
312 * ASIC specific parser, which returns a list of memory blocks to send
313 * to the device as different command buffers
314 *
315 */
cs_parser(struct hl_fpriv * hpriv,struct hl_cs_job * job)316 static int cs_parser(struct hl_fpriv *hpriv, struct hl_cs_job *job)
317 {
318 struct hl_device *hdev = hpriv->hdev;
319 struct hl_cs_parser parser;
320 int rc;
321
322 parser.ctx_id = job->cs->ctx->asid;
323 parser.cs_sequence = job->cs->sequence;
324 parser.job_id = job->id;
325
326 parser.hw_queue_id = job->hw_queue_id;
327 parser.job_userptr_list = &job->userptr_list;
328 parser.patched_cb = NULL;
329 parser.user_cb = job->user_cb;
330 parser.user_cb_size = job->user_cb_size;
331 parser.queue_type = job->queue_type;
332 parser.is_kernel_allocated_cb = job->is_kernel_allocated_cb;
333 job->patched_cb = NULL;
334 parser.completion = cs_needs_completion(job->cs);
335
336 rc = hdev->asic_funcs->cs_parser(hdev, &parser);
337
338 if (is_cb_patched(hdev, job)) {
339 if (!rc) {
340 job->patched_cb = parser.patched_cb;
341 job->job_cb_size = parser.patched_cb_size;
342 job->contains_dma_pkt = parser.contains_dma_pkt;
343 atomic_inc(&job->patched_cb->cs_cnt);
344 }
345
346 /*
347 * Whether the parsing worked or not, we don't need the
348 * original CB anymore because it was already parsed and
349 * won't be accessed again for this CS
350 */
351 atomic_dec(&job->user_cb->cs_cnt);
352 hl_cb_put(job->user_cb);
353 job->user_cb = NULL;
354 } else if (!rc) {
355 job->job_cb_size = job->user_cb_size;
356 }
357
358 return rc;
359 }
360
hl_complete_job(struct hl_device * hdev,struct hl_cs_job * job)361 static void hl_complete_job(struct hl_device *hdev, struct hl_cs_job *job)
362 {
363 struct hl_cs *cs = job->cs;
364
365 if (is_cb_patched(hdev, job)) {
366 hl_userptr_delete_list(hdev, &job->userptr_list);
367
368 /*
369 * We might arrive here from rollback and patched CB wasn't
370 * created, so we need to check it's not NULL
371 */
372 if (job->patched_cb) {
373 atomic_dec(&job->patched_cb->cs_cnt);
374 hl_cb_put(job->patched_cb);
375 }
376 }
377
378 /* For H/W queue jobs, if a user CB was allocated by driver,
379 * the user CB isn't released in cs_parser() and thus should be
380 * released here. This is also true for INT queues jobs which were
381 * allocated by driver.
382 */
383 if (job->is_kernel_allocated_cb &&
384 (job->queue_type == QUEUE_TYPE_HW || job->queue_type == QUEUE_TYPE_INT)) {
385 atomic_dec(&job->user_cb->cs_cnt);
386 hl_cb_put(job->user_cb);
387 }
388
389 /*
390 * This is the only place where there can be multiple threads
391 * modifying the list at the same time
392 */
393 spin_lock(&cs->job_lock);
394 list_del(&job->cs_node);
395 spin_unlock(&cs->job_lock);
396
397 hl_debugfs_remove_job(hdev, job);
398
399 /* We decrement reference only for a CS that gets completion
400 * because the reference was incremented only for this kind of CS
401 * right before it was scheduled.
402 *
403 * In staged submission, only the last CS marked as 'staged_last'
404 * gets completion, hence its release function will be called from here.
405 * As for all the rest CS's in the staged submission which do not get
406 * completion, their CS reference will be decremented by the
407 * 'staged_last' CS during the CS release flow.
408 * All relevant PQ CI counters will be incremented during the CS release
409 * flow by calling 'hl_hw_queue_update_ci'.
410 */
411 if (cs_needs_completion(cs) &&
412 (job->queue_type == QUEUE_TYPE_EXT || job->queue_type == QUEUE_TYPE_HW)) {
413
414 /* In CS based completions, the timestamp is already available,
415 * so no need to extract it from job
416 */
417 if (hdev->asic_prop.completion_mode == HL_COMPLETION_MODE_JOB)
418 cs->completion_timestamp = job->timestamp;
419
420 cs_put(cs);
421 }
422
423 hl_cs_job_put(job);
424 }
425
426 /*
427 * hl_staged_cs_find_first - locate the first CS in this staged submission
428 *
429 * @hdev: pointer to device structure
430 * @cs_seq: staged submission sequence number
431 *
432 * @note: This function must be called under 'hdev->cs_mirror_lock'
433 *
434 * Find and return a CS pointer with the given sequence
435 */
hl_staged_cs_find_first(struct hl_device * hdev,u64 cs_seq)436 struct hl_cs *hl_staged_cs_find_first(struct hl_device *hdev, u64 cs_seq)
437 {
438 struct hl_cs *cs;
439
440 list_for_each_entry_reverse(cs, &hdev->cs_mirror_list, mirror_node)
441 if (cs->staged_cs && cs->staged_first &&
442 cs->sequence == cs_seq)
443 return cs;
444
445 return NULL;
446 }
447
448 /*
449 * is_staged_cs_last_exists - returns true if the last CS in sequence exists
450 *
451 * @hdev: pointer to device structure
452 * @cs: staged submission member
453 *
454 */
is_staged_cs_last_exists(struct hl_device * hdev,struct hl_cs * cs)455 bool is_staged_cs_last_exists(struct hl_device *hdev, struct hl_cs *cs)
456 {
457 struct hl_cs *last_entry;
458
459 last_entry = list_last_entry(&cs->staged_cs_node, struct hl_cs,
460 staged_cs_node);
461
462 if (last_entry->staged_last)
463 return true;
464
465 return false;
466 }
467
468 /*
469 * staged_cs_get - get CS reference if this CS is a part of a staged CS
470 *
471 * @hdev: pointer to device structure
472 * @cs: current CS
473 * @cs_seq: staged submission sequence number
474 *
475 * Increment CS reference for every CS in this staged submission except for
476 * the CS which get completion.
477 */
staged_cs_get(struct hl_device * hdev,struct hl_cs * cs)478 static void staged_cs_get(struct hl_device *hdev, struct hl_cs *cs)
479 {
480 /* Only the last CS in this staged submission will get a completion.
481 * We must increment the reference for all other CS's in this
482 * staged submission.
483 * Once we get a completion we will release the whole staged submission.
484 */
485 if (!cs->staged_last)
486 cs_get(cs);
487 }
488
489 /*
490 * staged_cs_put - put a CS in case it is part of staged submission
491 *
492 * @hdev: pointer to device structure
493 * @cs: CS to put
494 *
495 * This function decrements a CS reference (for a non completion CS)
496 */
staged_cs_put(struct hl_device * hdev,struct hl_cs * cs)497 static void staged_cs_put(struct hl_device *hdev, struct hl_cs *cs)
498 {
499 /* We release all CS's in a staged submission except the last
500 * CS which we have never incremented its reference.
501 */
502 if (!cs_needs_completion(cs))
503 cs_put(cs);
504 }
505
cs_handle_tdr(struct hl_device * hdev,struct hl_cs * cs)506 static void cs_handle_tdr(struct hl_device *hdev, struct hl_cs *cs)
507 {
508 struct hl_cs *next = NULL, *iter, *first_cs;
509
510 if (!cs_needs_timeout(cs))
511 return;
512
513 spin_lock(&hdev->cs_mirror_lock);
514
515 /* We need to handle tdr only once for the complete staged submission.
516 * Hence, we choose the CS that reaches this function first which is
517 * the CS marked as 'staged_last'.
518 * In case single staged cs was submitted which has both first and last
519 * indications, then "cs_find_first" below will return NULL, since we
520 * removed the cs node from the list before getting here,
521 * in such cases just continue with the cs to cancel it's TDR work.
522 */
523 if (cs->staged_cs && cs->staged_last) {
524 first_cs = hl_staged_cs_find_first(hdev, cs->staged_sequence);
525 if (first_cs)
526 cs = first_cs;
527 }
528
529 spin_unlock(&hdev->cs_mirror_lock);
530
531 /* Don't cancel TDR in case this CS was timedout because we might be
532 * running from the TDR context
533 */
534 if (cs->timedout || hdev->timeout_jiffies == MAX_SCHEDULE_TIMEOUT)
535 return;
536
537 if (cs->tdr_active)
538 cancel_delayed_work_sync(&cs->work_tdr);
539
540 spin_lock(&hdev->cs_mirror_lock);
541
542 /* queue TDR for next CS */
543 list_for_each_entry(iter, &hdev->cs_mirror_list, mirror_node)
544 if (cs_needs_timeout(iter)) {
545 next = iter;
546 break;
547 }
548
549 if (next && !next->tdr_active) {
550 next->tdr_active = true;
551 schedule_delayed_work(&next->work_tdr, next->timeout_jiffies);
552 }
553
554 spin_unlock(&hdev->cs_mirror_lock);
555 }
556
557 /*
558 * force_complete_multi_cs - complete all contexts that wait on multi-CS
559 *
560 * @hdev: pointer to habanalabs device structure
561 */
force_complete_multi_cs(struct hl_device * hdev)562 static void force_complete_multi_cs(struct hl_device *hdev)
563 {
564 int i;
565
566 for (i = 0; i < MULTI_CS_MAX_USER_CTX; i++) {
567 struct multi_cs_completion *mcs_compl;
568
569 mcs_compl = &hdev->multi_cs_completion[i];
570
571 spin_lock(&mcs_compl->lock);
572
573 if (!mcs_compl->used) {
574 spin_unlock(&mcs_compl->lock);
575 continue;
576 }
577
578 /* when calling force complete no context should be waiting on
579 * multi-cS.
580 * We are calling the function as a protection for such case
581 * to free any pending context and print error message
582 */
583 dev_err(hdev->dev,
584 "multi-CS completion context %d still waiting when calling force completion\n",
585 i);
586 complete_all(&mcs_compl->completion);
587 spin_unlock(&mcs_compl->lock);
588 }
589 }
590
591 /*
592 * complete_multi_cs - complete all waiting entities on multi-CS
593 *
594 * @hdev: pointer to habanalabs device structure
595 * @cs: CS structure
596 * The function signals a waiting entity that has an overlapping stream masters
597 * with the completed CS.
598 * For example:
599 * - a completed CS worked on stream master QID 4, multi CS completion
600 * is actively waiting on stream master QIDs 3, 5. don't send signal as no
601 * common stream master QID
602 * - a completed CS worked on stream master QID 4, multi CS completion
603 * is actively waiting on stream master QIDs 3, 4. send signal as stream
604 * master QID 4 is common
605 */
complete_multi_cs(struct hl_device * hdev,struct hl_cs * cs)606 static void complete_multi_cs(struct hl_device *hdev, struct hl_cs *cs)
607 {
608 struct hl_fence *fence = cs->fence;
609 int i;
610
611 /* in case of multi CS check for completion only for the first CS */
612 if (cs->staged_cs && !cs->staged_first)
613 return;
614
615 for (i = 0; i < MULTI_CS_MAX_USER_CTX; i++) {
616 struct multi_cs_completion *mcs_compl;
617
618 mcs_compl = &hdev->multi_cs_completion[i];
619 if (!mcs_compl->used)
620 continue;
621
622 spin_lock(&mcs_compl->lock);
623
624 /*
625 * complete if:
626 * 1. still waiting for completion
627 * 2. the completed CS has at least one overlapping stream
628 * master with the stream masters in the completion
629 */
630 if (mcs_compl->used &&
631 (fence->stream_master_qid_map &
632 mcs_compl->stream_master_qid_map)) {
633 /* extract the timestamp only of first completed CS */
634 if (!mcs_compl->timestamp)
635 mcs_compl->timestamp = ktime_to_ns(fence->timestamp);
636
637 complete_all(&mcs_compl->completion);
638
639 /*
640 * Setting mcs_handling_done inside the lock ensures
641 * at least one fence have mcs_handling_done set to
642 * true before wait for mcs finish. This ensures at
643 * least one CS will be set as completed when polling
644 * mcs fences.
645 */
646 fence->mcs_handling_done = true;
647 }
648
649 spin_unlock(&mcs_compl->lock);
650 }
651 /* In case CS completed without mcs completion initialized */
652 fence->mcs_handling_done = true;
653 }
654
cs_release_sob_reset_handler(struct hl_device * hdev,struct hl_cs * cs,struct hl_cs_compl * hl_cs_cmpl)655 static inline void cs_release_sob_reset_handler(struct hl_device *hdev,
656 struct hl_cs *cs,
657 struct hl_cs_compl *hl_cs_cmpl)
658 {
659 /* Skip this handler if the cs wasn't submitted, to avoid putting
660 * the hw_sob twice, since this case already handled at this point,
661 * also skip if the hw_sob pointer wasn't set.
662 */
663 if (!hl_cs_cmpl->hw_sob || !cs->submitted)
664 return;
665
666 spin_lock(&hl_cs_cmpl->lock);
667
668 /*
669 * we get refcount upon reservation of signals or signal/wait cs for the
670 * hw_sob object, and need to put it when the first staged cs
671 * (which contains the encaps signals) or cs signal/wait is completed.
672 */
673 if ((hl_cs_cmpl->type == CS_TYPE_SIGNAL) ||
674 (hl_cs_cmpl->type == CS_TYPE_WAIT) ||
675 (hl_cs_cmpl->type == CS_TYPE_COLLECTIVE_WAIT) ||
676 (!!hl_cs_cmpl->encaps_signals)) {
677 dev_dbg(hdev->dev,
678 "CS 0x%llx type %d finished, sob_id: %d, sob_val: %u\n",
679 hl_cs_cmpl->cs_seq,
680 hl_cs_cmpl->type,
681 hl_cs_cmpl->hw_sob->sob_id,
682 hl_cs_cmpl->sob_val);
683
684 hw_sob_put(hl_cs_cmpl->hw_sob);
685
686 if (hl_cs_cmpl->type == CS_TYPE_COLLECTIVE_WAIT)
687 hdev->asic_funcs->reset_sob_group(hdev,
688 hl_cs_cmpl->sob_group);
689 }
690
691 spin_unlock(&hl_cs_cmpl->lock);
692 }
693
cs_do_release(struct kref * ref)694 static void cs_do_release(struct kref *ref)
695 {
696 struct hl_cs *cs = container_of(ref, struct hl_cs, refcount);
697 struct hl_device *hdev = cs->ctx->hdev;
698 struct hl_cs_job *job, *tmp;
699 struct hl_cs_compl *hl_cs_cmpl =
700 container_of(cs->fence, struct hl_cs_compl, base_fence);
701
702 cs->completed = true;
703
704 /*
705 * Although if we reached here it means that all external jobs have
706 * finished, because each one of them took refcnt to CS, we still
707 * need to go over the internal jobs and complete them. Otherwise, we
708 * will have leaked memory and what's worse, the CS object (and
709 * potentially the CTX object) could be released, while the JOB
710 * still holds a pointer to them (but no reference).
711 */
712 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
713 hl_complete_job(hdev, job);
714
715 if (!cs->submitted) {
716 /*
717 * In case the wait for signal CS was submitted, the fence put
718 * occurs in init_signal_wait_cs() or collective_wait_init_cs()
719 * right before hanging on the PQ.
720 */
721 if (cs->type == CS_TYPE_WAIT ||
722 cs->type == CS_TYPE_COLLECTIVE_WAIT)
723 hl_fence_put(cs->signal_fence);
724
725 goto out;
726 }
727
728 /* Need to update CI for all queue jobs that does not get completion */
729 hl_hw_queue_update_ci(cs);
730
731 /* remove CS from CS mirror list */
732 spin_lock(&hdev->cs_mirror_lock);
733 list_del_init(&cs->mirror_node);
734 spin_unlock(&hdev->cs_mirror_lock);
735
736 cs_handle_tdr(hdev, cs);
737
738 if (cs->staged_cs) {
739 /* the completion CS decrements reference for the entire
740 * staged submission
741 */
742 if (cs->staged_last) {
743 struct hl_cs *staged_cs, *tmp_cs;
744
745 list_for_each_entry_safe(staged_cs, tmp_cs,
746 &cs->staged_cs_node, staged_cs_node)
747 staged_cs_put(hdev, staged_cs);
748 }
749
750 /* A staged CS will be a member in the list only after it
751 * was submitted. We used 'cs_mirror_lock' when inserting
752 * it to list so we will use it again when removing it
753 */
754 if (cs->submitted) {
755 spin_lock(&hdev->cs_mirror_lock);
756 list_del(&cs->staged_cs_node);
757 spin_unlock(&hdev->cs_mirror_lock);
758 }
759
760 /* decrement refcount to handle when first staged cs
761 * with encaps signals is completed.
762 */
763 if (hl_cs_cmpl->encaps_signals)
764 kref_put(&hl_cs_cmpl->encaps_sig_hdl->refcount,
765 hl_encaps_release_handle_and_put_ctx);
766 }
767
768 if ((cs->type == CS_TYPE_WAIT || cs->type == CS_TYPE_COLLECTIVE_WAIT) && cs->encaps_signals)
769 kref_put(&cs->encaps_sig_hdl->refcount, hl_encaps_release_handle_and_put_ctx);
770
771 out:
772 /* Must be called before hl_ctx_put because inside we use ctx to get
773 * the device
774 */
775 hl_debugfs_remove_cs(cs);
776
777 hdev->shadow_cs_queue[cs->sequence & (hdev->asic_prop.max_pending_cs - 1)] = NULL;
778
779 /* We need to mark an error for not submitted because in that case
780 * the hl fence release flow is different. Mainly, we don't need
781 * to handle hw_sob for signal/wait
782 */
783 if (cs->timedout)
784 cs->fence->error = -ETIMEDOUT;
785 else if (cs->aborted)
786 cs->fence->error = -EIO;
787 else if (!cs->submitted)
788 cs->fence->error = -EBUSY;
789
790 if (unlikely(cs->skip_reset_on_timeout)) {
791 dev_err(hdev->dev,
792 "Command submission %llu completed after %llu (s)\n",
793 cs->sequence,
794 div_u64(jiffies - cs->submission_time_jiffies, HZ));
795 }
796
797 if (cs->timestamp) {
798 cs->fence->timestamp = cs->completion_timestamp;
799 hl_push_cs_outcome(hdev, &cs->ctx->outcome_store, cs->sequence,
800 cs->fence->timestamp, cs->fence->error);
801 }
802
803 hl_ctx_put(cs->ctx);
804
805 complete_all(&cs->fence->completion);
806 complete_multi_cs(hdev, cs);
807
808 cs_release_sob_reset_handler(hdev, cs, hl_cs_cmpl);
809
810 hl_fence_put(cs->fence);
811
812 kfree(cs->jobs_in_queue_cnt);
813 kfree(cs);
814 }
815
cs_timedout(struct work_struct * work)816 static void cs_timedout(struct work_struct *work)
817 {
818 struct hl_cs *cs = container_of(work, struct hl_cs, work_tdr.work);
819 bool skip_reset_on_timeout, device_reset = false;
820 struct hl_device *hdev;
821 u64 event_mask = 0x0;
822 uint timeout_sec;
823 int rc;
824
825 skip_reset_on_timeout = cs->skip_reset_on_timeout;
826
827 rc = cs_get_unless_zero(cs);
828 if (!rc)
829 return;
830
831 if ((!cs->submitted) || (cs->completed)) {
832 cs_put(cs);
833 return;
834 }
835
836 hdev = cs->ctx->hdev;
837
838 if (likely(!skip_reset_on_timeout)) {
839 if (hdev->reset_on_lockup)
840 device_reset = true;
841 else
842 hdev->reset_info.needs_reset = true;
843
844 /* Mark the CS is timed out so we won't try to cancel its TDR */
845 cs->timedout = true;
846 }
847
848 /* Save only the first CS timeout parameters */
849 rc = atomic_cmpxchg(&hdev->captured_err_info.cs_timeout.write_enable, 1, 0);
850 if (rc) {
851 hdev->captured_err_info.cs_timeout.timestamp = ktime_get();
852 hdev->captured_err_info.cs_timeout.seq = cs->sequence;
853 event_mask |= HL_NOTIFIER_EVENT_CS_TIMEOUT;
854 }
855
856 timeout_sec = jiffies_to_msecs(hdev->timeout_jiffies) / 1000;
857
858 switch (cs->type) {
859 case CS_TYPE_SIGNAL:
860 dev_err(hdev->dev,
861 "Signal command submission %llu has not finished in %u seconds!\n",
862 cs->sequence, timeout_sec);
863 break;
864
865 case CS_TYPE_WAIT:
866 dev_err(hdev->dev,
867 "Wait command submission %llu has not finished in %u seconds!\n",
868 cs->sequence, timeout_sec);
869 break;
870
871 case CS_TYPE_COLLECTIVE_WAIT:
872 dev_err(hdev->dev,
873 "Collective Wait command submission %llu has not finished in %u seconds!\n",
874 cs->sequence, timeout_sec);
875 break;
876
877 default:
878 dev_err(hdev->dev,
879 "Command submission %llu has not finished in %u seconds!\n",
880 cs->sequence, timeout_sec);
881 break;
882 }
883
884 rc = hl_state_dump(hdev);
885 if (rc)
886 dev_err(hdev->dev, "Error during system state dump %d\n", rc);
887
888 cs_put(cs);
889
890 if (device_reset) {
891 event_mask |= HL_NOTIFIER_EVENT_DEVICE_RESET;
892 hl_device_cond_reset(hdev, HL_DRV_RESET_TDR, event_mask);
893 } else if (event_mask) {
894 hl_notifier_event_send_all(hdev, event_mask);
895 }
896 }
897
allocate_cs(struct hl_device * hdev,struct hl_ctx * ctx,enum hl_cs_type cs_type,u64 user_sequence,struct hl_cs ** cs_new,u32 flags,u32 timeout)898 static int allocate_cs(struct hl_device *hdev, struct hl_ctx *ctx,
899 enum hl_cs_type cs_type, u64 user_sequence,
900 struct hl_cs **cs_new, u32 flags, u32 timeout)
901 {
902 struct hl_cs_counters_atomic *cntr;
903 struct hl_fence *other = NULL;
904 struct hl_cs_compl *cs_cmpl;
905 struct hl_cs *cs;
906 int rc;
907
908 cntr = &hdev->aggregated_cs_counters;
909
910 cs = kzalloc_obj(*cs, GFP_ATOMIC);
911 if (!cs)
912 cs = kzalloc_obj(*cs);
913
914 if (!cs) {
915 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
916 atomic64_inc(&cntr->out_of_mem_drop_cnt);
917 return -ENOMEM;
918 }
919
920 /* increment refcnt for context */
921 hl_ctx_get(ctx);
922
923 cs->ctx = ctx;
924 cs->submitted = false;
925 cs->completed = false;
926 cs->type = cs_type;
927 cs->timestamp = !!(flags & HL_CS_FLAGS_TIMESTAMP);
928 cs->encaps_signals = !!(flags & HL_CS_FLAGS_ENCAP_SIGNALS);
929 cs->timeout_jiffies = timeout;
930 cs->skip_reset_on_timeout =
931 hdev->reset_info.skip_reset_on_timeout ||
932 !!(flags & HL_CS_FLAGS_SKIP_RESET_ON_TIMEOUT);
933 cs->submission_time_jiffies = jiffies;
934 INIT_LIST_HEAD(&cs->job_list);
935 INIT_DELAYED_WORK(&cs->work_tdr, cs_timedout);
936 kref_init(&cs->refcount);
937 spin_lock_init(&cs->job_lock);
938
939 cs_cmpl = kzalloc_obj(*cs_cmpl, GFP_ATOMIC);
940 if (!cs_cmpl)
941 cs_cmpl = kzalloc_obj(*cs_cmpl);
942
943 if (!cs_cmpl) {
944 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
945 atomic64_inc(&cntr->out_of_mem_drop_cnt);
946 rc = -ENOMEM;
947 goto free_cs;
948 }
949
950 cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
951 sizeof(*cs->jobs_in_queue_cnt), GFP_ATOMIC);
952 if (!cs->jobs_in_queue_cnt)
953 cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
954 sizeof(*cs->jobs_in_queue_cnt), GFP_KERNEL);
955
956 if (!cs->jobs_in_queue_cnt) {
957 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
958 atomic64_inc(&cntr->out_of_mem_drop_cnt);
959 rc = -ENOMEM;
960 goto free_cs_cmpl;
961 }
962
963 cs_cmpl->hdev = hdev;
964 cs_cmpl->type = cs->type;
965 spin_lock_init(&cs_cmpl->lock);
966 cs->fence = &cs_cmpl->base_fence;
967
968 spin_lock(&ctx->cs_lock);
969
970 cs_cmpl->cs_seq = ctx->cs_sequence;
971 other = ctx->cs_pending[cs_cmpl->cs_seq &
972 (hdev->asic_prop.max_pending_cs - 1)];
973
974 if (other && !completion_done(&other->completion)) {
975 /* If the following statement is true, it means we have reached
976 * a point in which only part of the staged submission was
977 * submitted and we don't have enough room in the 'cs_pending'
978 * array for the rest of the submission.
979 * This causes a deadlock because this CS will never be
980 * completed as it depends on future CS's for completion.
981 */
982 if (other->cs_sequence == user_sequence)
983 dev_crit_ratelimited(hdev->dev,
984 "Staged CS %llu deadlock due to lack of resources",
985 user_sequence);
986
987 dev_dbg_ratelimited(hdev->dev,
988 "Rejecting CS because of too many in-flights CS\n");
989 atomic64_inc(&ctx->cs_counters.max_cs_in_flight_drop_cnt);
990 atomic64_inc(&cntr->max_cs_in_flight_drop_cnt);
991 rc = -EAGAIN;
992 goto free_fence;
993 }
994
995 /* init hl_fence */
996 hl_fence_init(&cs_cmpl->base_fence, cs_cmpl->cs_seq);
997
998 cs->sequence = cs_cmpl->cs_seq;
999
1000 ctx->cs_pending[cs_cmpl->cs_seq &
1001 (hdev->asic_prop.max_pending_cs - 1)] =
1002 &cs_cmpl->base_fence;
1003 ctx->cs_sequence++;
1004
1005 hl_fence_get(&cs_cmpl->base_fence);
1006
1007 hl_fence_put(other);
1008
1009 spin_unlock(&ctx->cs_lock);
1010
1011 *cs_new = cs;
1012
1013 return 0;
1014
1015 free_fence:
1016 spin_unlock(&ctx->cs_lock);
1017 kfree(cs->jobs_in_queue_cnt);
1018 free_cs_cmpl:
1019 kfree(cs_cmpl);
1020 free_cs:
1021 kfree(cs);
1022 hl_ctx_put(ctx);
1023 return rc;
1024 }
1025
cs_rollback(struct hl_device * hdev,struct hl_cs * cs)1026 static void cs_rollback(struct hl_device *hdev, struct hl_cs *cs)
1027 {
1028 struct hl_cs_job *job, *tmp;
1029
1030 staged_cs_put(hdev, cs);
1031
1032 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
1033 hl_complete_job(hdev, job);
1034 }
1035
1036 /*
1037 * release_reserved_encaps_signals() - release reserved encapsulated signals.
1038 * @hdev: pointer to habanalabs device structure
1039 *
1040 * Release reserved encapsulated signals which weren't un-reserved, or for which a CS with
1041 * encapsulated signals wasn't submitted and thus weren't released as part of CS roll-back.
1042 * For these signals need also to put the refcount of the H/W SOB which was taken at the
1043 * reservation.
1044 */
release_reserved_encaps_signals(struct hl_device * hdev)1045 static void release_reserved_encaps_signals(struct hl_device *hdev)
1046 {
1047 struct hl_ctx *ctx = hl_get_compute_ctx(hdev);
1048 struct hl_cs_encaps_sig_handle *handle;
1049 struct hl_encaps_signals_mgr *mgr;
1050 u32 id;
1051
1052 if (!ctx)
1053 return;
1054
1055 mgr = &ctx->sig_mgr;
1056
1057 idr_for_each_entry(&mgr->handles, handle, id)
1058 if (handle->cs_seq == ULLONG_MAX)
1059 kref_put(&handle->refcount, hl_encaps_release_handle_and_put_sob_ctx);
1060
1061 hl_ctx_put(ctx);
1062 }
1063
hl_cs_rollback_all(struct hl_device * hdev,bool skip_wq_flush)1064 void hl_cs_rollback_all(struct hl_device *hdev, bool skip_wq_flush)
1065 {
1066 int i;
1067 struct hl_cs *cs, *tmp;
1068
1069 if (!skip_wq_flush) {
1070 flush_workqueue(hdev->ts_free_obj_wq);
1071
1072 /* flush all completions before iterating over the CS mirror list in
1073 * order to avoid a race with the release functions
1074 */
1075 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1076 flush_workqueue(hdev->cq_wq[i]);
1077
1078 flush_workqueue(hdev->cs_cmplt_wq);
1079 }
1080
1081 /* Make sure we don't have leftovers in the CS mirror list */
1082 list_for_each_entry_safe(cs, tmp, &hdev->cs_mirror_list, mirror_node) {
1083 cs_get(cs);
1084 cs->aborted = true;
1085 dev_warn_ratelimited(hdev->dev, "Killing CS %d.%llu\n",
1086 cs->ctx->asid, cs->sequence);
1087 cs_rollback(hdev, cs);
1088 cs_put(cs);
1089 }
1090
1091 force_complete_multi_cs(hdev);
1092
1093 release_reserved_encaps_signals(hdev);
1094 }
1095
1096 static void
wake_pending_user_interrupt_threads(struct hl_user_interrupt * interrupt)1097 wake_pending_user_interrupt_threads(struct hl_user_interrupt *interrupt)
1098 {
1099 struct hl_user_pending_interrupt *pend, *temp;
1100 unsigned long flags;
1101
1102 spin_lock_irqsave(&interrupt->wait_list_lock, flags);
1103 list_for_each_entry_safe(pend, temp, &interrupt->wait_list_head, list_node) {
1104 pend->fence.error = -EIO;
1105 complete_all(&pend->fence.completion);
1106 }
1107 spin_unlock_irqrestore(&interrupt->wait_list_lock, flags);
1108
1109 spin_lock_irqsave(&interrupt->ts_list_lock, flags);
1110 list_for_each_entry_safe(pend, temp, &interrupt->ts_list_head, list_node) {
1111 list_del(&pend->list_node);
1112 hl_mmap_mem_buf_put(pend->ts_reg_info.buf);
1113 hl_cb_put(pend->ts_reg_info.cq_cb);
1114 }
1115 spin_unlock_irqrestore(&interrupt->ts_list_lock, flags);
1116 }
1117
hl_release_pending_user_interrupts(struct hl_device * hdev)1118 void hl_release_pending_user_interrupts(struct hl_device *hdev)
1119 {
1120 struct asic_fixed_properties *prop = &hdev->asic_prop;
1121 struct hl_user_interrupt *interrupt;
1122 int i;
1123
1124 if (!prop->user_interrupt_count)
1125 return;
1126
1127 /* We iterate through the user interrupt requests and waking up all
1128 * user threads waiting for interrupt completion. We iterate the
1129 * list under a lock, this is why all user threads, once awake,
1130 * will wait on the same lock and will release the waiting object upon
1131 * unlock.
1132 */
1133
1134 for (i = 0 ; i < prop->user_interrupt_count ; i++) {
1135 interrupt = &hdev->user_interrupt[i];
1136 wake_pending_user_interrupt_threads(interrupt);
1137 }
1138
1139 interrupt = &hdev->common_user_cq_interrupt;
1140 wake_pending_user_interrupt_threads(interrupt);
1141
1142 interrupt = &hdev->common_decoder_interrupt;
1143 wake_pending_user_interrupt_threads(interrupt);
1144 }
1145
force_complete_cs(struct hl_device * hdev)1146 static void force_complete_cs(struct hl_device *hdev)
1147 {
1148 struct hl_cs *cs;
1149
1150 spin_lock(&hdev->cs_mirror_lock);
1151
1152 list_for_each_entry(cs, &hdev->cs_mirror_list, mirror_node) {
1153 cs->fence->error = -EIO;
1154 complete_all(&cs->fence->completion);
1155 }
1156
1157 spin_unlock(&hdev->cs_mirror_lock);
1158 }
1159
hl_abort_waiting_for_cs_completions(struct hl_device * hdev)1160 void hl_abort_waiting_for_cs_completions(struct hl_device *hdev)
1161 {
1162 force_complete_cs(hdev);
1163 force_complete_multi_cs(hdev);
1164 }
1165
job_wq_completion(struct work_struct * work)1166 static void job_wq_completion(struct work_struct *work)
1167 {
1168 struct hl_cs_job *job = container_of(work, struct hl_cs_job,
1169 finish_work);
1170 struct hl_cs *cs = job->cs;
1171 struct hl_device *hdev = cs->ctx->hdev;
1172
1173 /* job is no longer needed */
1174 hl_complete_job(hdev, job);
1175 }
1176
cs_completion(struct work_struct * work)1177 static void cs_completion(struct work_struct *work)
1178 {
1179 struct hl_cs *cs = container_of(work, struct hl_cs, finish_work);
1180 struct hl_device *hdev = cs->ctx->hdev;
1181 struct hl_cs_job *job, *tmp;
1182
1183 list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
1184 hl_complete_job(hdev, job);
1185 }
1186
hl_get_active_cs_num(struct hl_device * hdev)1187 u32 hl_get_active_cs_num(struct hl_device *hdev)
1188 {
1189 u32 active_cs_num = 0;
1190 struct hl_cs *cs;
1191
1192 spin_lock(&hdev->cs_mirror_lock);
1193
1194 list_for_each_entry(cs, &hdev->cs_mirror_list, mirror_node)
1195 if (!cs->completed)
1196 active_cs_num++;
1197
1198 spin_unlock(&hdev->cs_mirror_lock);
1199
1200 return active_cs_num;
1201 }
1202
validate_queue_index(struct hl_device * hdev,struct hl_cs_chunk * chunk,enum hl_queue_type * queue_type,bool * is_kernel_allocated_cb)1203 static int validate_queue_index(struct hl_device *hdev,
1204 struct hl_cs_chunk *chunk,
1205 enum hl_queue_type *queue_type,
1206 bool *is_kernel_allocated_cb)
1207 {
1208 struct asic_fixed_properties *asic = &hdev->asic_prop;
1209 struct hw_queue_properties *hw_queue_prop;
1210
1211 /* This must be checked here to prevent out-of-bounds access to
1212 * hw_queues_props array
1213 */
1214 if (chunk->queue_index >= asic->max_queues) {
1215 dev_err(hdev->dev, "Queue index %d is invalid\n",
1216 chunk->queue_index);
1217 return -EINVAL;
1218 }
1219
1220 hw_queue_prop = &asic->hw_queues_props[chunk->queue_index];
1221
1222 if (hw_queue_prop->type == QUEUE_TYPE_NA) {
1223 dev_err(hdev->dev, "Queue index %d is not applicable\n",
1224 chunk->queue_index);
1225 return -EINVAL;
1226 }
1227
1228 if (hw_queue_prop->binned) {
1229 dev_err(hdev->dev, "Queue index %d is binned out\n",
1230 chunk->queue_index);
1231 return -EINVAL;
1232 }
1233
1234 if (hw_queue_prop->driver_only) {
1235 dev_err(hdev->dev,
1236 "Queue index %d is restricted for the kernel driver\n",
1237 chunk->queue_index);
1238 return -EINVAL;
1239 }
1240
1241 /* When hw queue type isn't QUEUE_TYPE_HW,
1242 * USER_ALLOC_CB flag shall be referred as "don't care".
1243 */
1244 if (hw_queue_prop->type == QUEUE_TYPE_HW) {
1245 if (chunk->cs_chunk_flags & HL_CS_CHUNK_FLAGS_USER_ALLOC_CB) {
1246 if (!(hw_queue_prop->cb_alloc_flags & CB_ALLOC_USER)) {
1247 dev_err(hdev->dev,
1248 "Queue index %d doesn't support user CB\n",
1249 chunk->queue_index);
1250 return -EINVAL;
1251 }
1252
1253 *is_kernel_allocated_cb = false;
1254 } else {
1255 if (!(hw_queue_prop->cb_alloc_flags &
1256 CB_ALLOC_KERNEL)) {
1257 dev_err(hdev->dev,
1258 "Queue index %d doesn't support kernel CB\n",
1259 chunk->queue_index);
1260 return -EINVAL;
1261 }
1262
1263 *is_kernel_allocated_cb = true;
1264 }
1265 } else {
1266 *is_kernel_allocated_cb = !!(hw_queue_prop->cb_alloc_flags
1267 & CB_ALLOC_KERNEL);
1268 }
1269
1270 *queue_type = hw_queue_prop->type;
1271 return 0;
1272 }
1273
get_cb_from_cs_chunk(struct hl_device * hdev,struct hl_mem_mgr * mmg,struct hl_cs_chunk * chunk)1274 static struct hl_cb *get_cb_from_cs_chunk(struct hl_device *hdev,
1275 struct hl_mem_mgr *mmg,
1276 struct hl_cs_chunk *chunk)
1277 {
1278 struct hl_cb *cb;
1279
1280 cb = hl_cb_get(mmg, chunk->cb_handle);
1281 if (!cb) {
1282 dev_err(hdev->dev, "CB handle 0x%llx invalid\n", chunk->cb_handle);
1283 return NULL;
1284 }
1285
1286 if ((chunk->cb_size < 8) || (chunk->cb_size > cb->size)) {
1287 dev_err(hdev->dev, "CB size %u invalid\n", chunk->cb_size);
1288 goto release_cb;
1289 }
1290
1291 atomic_inc(&cb->cs_cnt);
1292
1293 return cb;
1294
1295 release_cb:
1296 hl_cb_put(cb);
1297 return NULL;
1298 }
1299
hl_cs_allocate_job(struct hl_device * hdev,enum hl_queue_type queue_type,bool is_kernel_allocated_cb)1300 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
1301 enum hl_queue_type queue_type, bool is_kernel_allocated_cb)
1302 {
1303 struct hl_cs_job *job;
1304
1305 job = kzalloc_obj(*job, GFP_ATOMIC);
1306 if (!job)
1307 job = kzalloc_obj(*job);
1308
1309 if (!job)
1310 return NULL;
1311
1312 kref_init(&job->refcount);
1313 job->queue_type = queue_type;
1314 job->is_kernel_allocated_cb = is_kernel_allocated_cb;
1315
1316 if (is_cb_patched(hdev, job))
1317 INIT_LIST_HEAD(&job->userptr_list);
1318
1319 if (job->queue_type == QUEUE_TYPE_EXT)
1320 INIT_WORK(&job->finish_work, job_wq_completion);
1321
1322 return job;
1323 }
1324
hl_cs_get_cs_type(u32 cs_type_flags)1325 static enum hl_cs_type hl_cs_get_cs_type(u32 cs_type_flags)
1326 {
1327 if (cs_type_flags & HL_CS_FLAGS_SIGNAL)
1328 return CS_TYPE_SIGNAL;
1329 else if (cs_type_flags & HL_CS_FLAGS_WAIT)
1330 return CS_TYPE_WAIT;
1331 else if (cs_type_flags & HL_CS_FLAGS_COLLECTIVE_WAIT)
1332 return CS_TYPE_COLLECTIVE_WAIT;
1333 else if (cs_type_flags & HL_CS_FLAGS_RESERVE_SIGNALS_ONLY)
1334 return CS_RESERVE_SIGNALS;
1335 else if (cs_type_flags & HL_CS_FLAGS_UNRESERVE_SIGNALS_ONLY)
1336 return CS_UNRESERVE_SIGNALS;
1337 else if (cs_type_flags & HL_CS_FLAGS_ENGINE_CORE_COMMAND)
1338 return CS_TYPE_ENGINE_CORE;
1339 else if (cs_type_flags & HL_CS_FLAGS_ENGINES_COMMAND)
1340 return CS_TYPE_ENGINES;
1341 else if (cs_type_flags & HL_CS_FLAGS_FLUSH_PCI_HBW_WRITES)
1342 return CS_TYPE_FLUSH_PCI_HBW_WRITES;
1343 else
1344 return CS_TYPE_DEFAULT;
1345 }
1346
hl_cs_sanity_checks(struct hl_fpriv * hpriv,union hl_cs_args * args)1347 static int hl_cs_sanity_checks(struct hl_fpriv *hpriv, union hl_cs_args *args)
1348 {
1349 struct hl_device *hdev = hpriv->hdev;
1350 struct hl_ctx *ctx = hpriv->ctx;
1351 u32 cs_type_flags, num_chunks;
1352 enum hl_device_status status;
1353 enum hl_cs_type cs_type;
1354 bool is_sync_stream;
1355 int i;
1356
1357 for (i = 0 ; i < sizeof(args->in.pad) ; i++)
1358 if (args->in.pad[i]) {
1359 dev_dbg(hdev->dev, "Padding bytes must be 0\n");
1360 return -EINVAL;
1361 }
1362
1363 if (!hl_device_operational(hdev, &status))
1364 return -EBUSY;
1365
1366 if ((args->in.cs_flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
1367 !hdev->supports_staged_submission) {
1368 dev_err(hdev->dev, "staged submission not supported");
1369 return -EPERM;
1370 }
1371
1372 cs_type_flags = args->in.cs_flags & HL_CS_FLAGS_TYPE_MASK;
1373
1374 if (unlikely(cs_type_flags && !is_power_of_2(cs_type_flags))) {
1375 dev_err(hdev->dev,
1376 "CS type flags are mutually exclusive, context %d\n",
1377 ctx->asid);
1378 return -EINVAL;
1379 }
1380
1381 cs_type = hl_cs_get_cs_type(cs_type_flags);
1382 num_chunks = args->in.num_chunks_execute;
1383
1384 is_sync_stream = (cs_type == CS_TYPE_SIGNAL || cs_type == CS_TYPE_WAIT ||
1385 cs_type == CS_TYPE_COLLECTIVE_WAIT);
1386
1387 if (unlikely(is_sync_stream && !hdev->supports_sync_stream)) {
1388 dev_err(hdev->dev, "Sync stream CS is not supported\n");
1389 return -EINVAL;
1390 }
1391
1392 if (cs_type == CS_TYPE_DEFAULT) {
1393 if (!num_chunks) {
1394 dev_err(hdev->dev, "Got execute CS with 0 chunks, context %d\n", ctx->asid);
1395 return -EINVAL;
1396 }
1397 } else if (is_sync_stream && num_chunks != 1) {
1398 dev_err(hdev->dev,
1399 "Sync stream CS mandates one chunk only, context %d\n",
1400 ctx->asid);
1401 return -EINVAL;
1402 }
1403
1404 return 0;
1405 }
1406
hl_cs_copy_chunk_array(struct hl_device * hdev,struct hl_cs_chunk ** cs_chunk_array,void __user * chunks,u32 num_chunks,struct hl_ctx * ctx)1407 static int hl_cs_copy_chunk_array(struct hl_device *hdev,
1408 struct hl_cs_chunk **cs_chunk_array,
1409 void __user *chunks, u32 num_chunks,
1410 struct hl_ctx *ctx)
1411 {
1412 u32 size_to_copy;
1413
1414 if (num_chunks > HL_MAX_JOBS_PER_CS) {
1415 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1416 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1417 dev_err(hdev->dev,
1418 "Number of chunks can NOT be larger than %d\n",
1419 HL_MAX_JOBS_PER_CS);
1420 return -EINVAL;
1421 }
1422
1423 *cs_chunk_array = kmalloc_objs(**cs_chunk_array, num_chunks, GFP_ATOMIC);
1424 if (!*cs_chunk_array)
1425 *cs_chunk_array = kmalloc_objs(**cs_chunk_array, num_chunks);
1426 if (!*cs_chunk_array) {
1427 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1428 atomic64_inc(&hdev->aggregated_cs_counters.out_of_mem_drop_cnt);
1429 return -ENOMEM;
1430 }
1431
1432 size_to_copy = num_chunks * sizeof(struct hl_cs_chunk);
1433 if (copy_from_user(*cs_chunk_array, chunks, size_to_copy)) {
1434 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1435 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1436 dev_err(hdev->dev, "Failed to copy cs chunk array from user\n");
1437 kfree(*cs_chunk_array);
1438 return -EFAULT;
1439 }
1440
1441 return 0;
1442 }
1443
cs_staged_submission(struct hl_device * hdev,struct hl_cs * cs,u64 sequence,u32 flags,u32 encaps_signal_handle)1444 static int cs_staged_submission(struct hl_device *hdev, struct hl_cs *cs,
1445 u64 sequence, u32 flags,
1446 u32 encaps_signal_handle)
1447 {
1448 if (!(flags & HL_CS_FLAGS_STAGED_SUBMISSION))
1449 return 0;
1450
1451 cs->staged_last = !!(flags & HL_CS_FLAGS_STAGED_SUBMISSION_LAST);
1452 cs->staged_first = !!(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST);
1453
1454 if (cs->staged_first) {
1455 /* Staged CS sequence is the first CS sequence */
1456 INIT_LIST_HEAD(&cs->staged_cs_node);
1457 cs->staged_sequence = cs->sequence;
1458
1459 if (cs->encaps_signals)
1460 cs->encaps_sig_hdl_id = encaps_signal_handle;
1461 } else {
1462 /* User sequence will be validated in 'hl_hw_queue_schedule_cs'
1463 * under the cs_mirror_lock
1464 */
1465 cs->staged_sequence = sequence;
1466 }
1467
1468 /* Increment CS reference if needed */
1469 staged_cs_get(hdev, cs);
1470
1471 cs->staged_cs = true;
1472
1473 return 0;
1474 }
1475
get_stream_master_qid_mask(struct hl_device * hdev,u32 qid)1476 static u32 get_stream_master_qid_mask(struct hl_device *hdev, u32 qid)
1477 {
1478 int i;
1479
1480 for (i = 0; i < hdev->stream_master_qid_arr_size; i++)
1481 if (qid == hdev->stream_master_qid_arr[i])
1482 return BIT(i);
1483
1484 return 0;
1485 }
1486
cs_ioctl_default(struct hl_fpriv * hpriv,void __user * chunks,u32 num_chunks,u64 * cs_seq,u32 flags,u32 encaps_signals_handle,u32 timeout,u16 * signal_initial_sob_count)1487 static int cs_ioctl_default(struct hl_fpriv *hpriv, void __user *chunks,
1488 u32 num_chunks, u64 *cs_seq, u32 flags,
1489 u32 encaps_signals_handle, u32 timeout,
1490 u16 *signal_initial_sob_count)
1491 {
1492 bool staged_mid, int_queues_only = true, using_hw_queues = false;
1493 struct hl_device *hdev = hpriv->hdev;
1494 struct hl_cs_chunk *cs_chunk_array;
1495 struct hl_cs_counters_atomic *cntr;
1496 struct hl_ctx *ctx = hpriv->ctx;
1497 struct hl_cs_job *job;
1498 struct hl_cs *cs;
1499 struct hl_cb *cb;
1500 u64 user_sequence;
1501 u8 stream_master_qid_map = 0;
1502 int rc, i;
1503
1504 cntr = &hdev->aggregated_cs_counters;
1505 user_sequence = *cs_seq;
1506 *cs_seq = ULLONG_MAX;
1507
1508 rc = hl_cs_copy_chunk_array(hdev, &cs_chunk_array, chunks, num_chunks,
1509 hpriv->ctx);
1510 if (rc)
1511 goto out;
1512
1513 if ((flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
1514 !(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST))
1515 staged_mid = true;
1516 else
1517 staged_mid = false;
1518
1519 rc = allocate_cs(hdev, hpriv->ctx, CS_TYPE_DEFAULT,
1520 staged_mid ? user_sequence : ULLONG_MAX, &cs, flags,
1521 timeout);
1522 if (rc)
1523 goto free_cs_chunk_array;
1524
1525 *cs_seq = cs->sequence;
1526
1527 hl_debugfs_add_cs(cs);
1528
1529 rc = cs_staged_submission(hdev, cs, user_sequence, flags,
1530 encaps_signals_handle);
1531 if (rc)
1532 goto free_cs_object;
1533
1534 /* If this is a staged submission we must return the staged sequence
1535 * rather than the internal CS sequence
1536 */
1537 if (cs->staged_cs)
1538 *cs_seq = cs->staged_sequence;
1539
1540 /* Validate ALL the CS chunks before submitting the CS */
1541 for (i = 0 ; i < num_chunks ; i++) {
1542 struct hl_cs_chunk *chunk = &cs_chunk_array[i];
1543 enum hl_queue_type queue_type;
1544 bool is_kernel_allocated_cb;
1545
1546 rc = validate_queue_index(hdev, chunk, &queue_type,
1547 &is_kernel_allocated_cb);
1548 if (rc) {
1549 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1550 atomic64_inc(&cntr->validation_drop_cnt);
1551 goto free_cs_object;
1552 }
1553
1554 if (is_kernel_allocated_cb) {
1555 cb = get_cb_from_cs_chunk(hdev, &hpriv->mem_mgr, chunk);
1556 if (!cb) {
1557 atomic64_inc(
1558 &ctx->cs_counters.validation_drop_cnt);
1559 atomic64_inc(&cntr->validation_drop_cnt);
1560 rc = -EINVAL;
1561 goto free_cs_object;
1562 }
1563 } else {
1564 cb = (struct hl_cb *) (uintptr_t) chunk->cb_handle;
1565 }
1566
1567 if (queue_type == QUEUE_TYPE_EXT ||
1568 queue_type == QUEUE_TYPE_HW) {
1569 int_queues_only = false;
1570
1571 /*
1572 * store which stream are being used for external/HW
1573 * queues of this CS
1574 */
1575 if (hdev->supports_wait_for_multi_cs)
1576 stream_master_qid_map |=
1577 get_stream_master_qid_mask(hdev,
1578 chunk->queue_index);
1579 }
1580
1581 if (queue_type == QUEUE_TYPE_HW)
1582 using_hw_queues = true;
1583
1584 job = hl_cs_allocate_job(hdev, queue_type,
1585 is_kernel_allocated_cb);
1586 if (!job) {
1587 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1588 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1589 dev_err(hdev->dev, "Failed to allocate a new job\n");
1590 rc = -ENOMEM;
1591 if (is_kernel_allocated_cb)
1592 goto release_cb;
1593
1594 goto free_cs_object;
1595 }
1596
1597 job->id = i + 1;
1598 job->cs = cs;
1599 job->user_cb = cb;
1600 job->user_cb_size = chunk->cb_size;
1601 job->hw_queue_id = chunk->queue_index;
1602
1603 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1604 cs->jobs_cnt++;
1605
1606 list_add_tail(&job->cs_node, &cs->job_list);
1607
1608 /*
1609 * Increment CS reference. When CS reference is 0, CS is
1610 * done and can be signaled to user and free all its resources
1611 * Only increment for JOB on external or H/W queues, because
1612 * only for those JOBs we get completion
1613 */
1614 if (cs_needs_completion(cs) &&
1615 (job->queue_type == QUEUE_TYPE_EXT ||
1616 job->queue_type == QUEUE_TYPE_HW))
1617 cs_get(cs);
1618
1619 hl_debugfs_add_job(hdev, job);
1620
1621 rc = cs_parser(hpriv, job);
1622 if (rc) {
1623 atomic64_inc(&ctx->cs_counters.parsing_drop_cnt);
1624 atomic64_inc(&cntr->parsing_drop_cnt);
1625 dev_err(hdev->dev,
1626 "Failed to parse JOB %d.%llu.%d, err %d, rejecting the CS\n",
1627 cs->ctx->asid, cs->sequence, job->id, rc);
1628 goto free_cs_object;
1629 }
1630 }
1631
1632 /* We allow a CS with any queue type combination as long as it does
1633 * not get a completion
1634 */
1635 if (int_queues_only && cs_needs_completion(cs)) {
1636 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1637 atomic64_inc(&cntr->validation_drop_cnt);
1638 dev_err(hdev->dev,
1639 "Reject CS %d.%llu since it contains only internal queues jobs and needs completion\n",
1640 cs->ctx->asid, cs->sequence);
1641 rc = -EINVAL;
1642 goto free_cs_object;
1643 }
1644
1645 if (using_hw_queues)
1646 INIT_WORK(&cs->finish_work, cs_completion);
1647
1648 /*
1649 * store the (external/HW queues) streams used by the CS in the
1650 * fence object for multi-CS completion
1651 */
1652 if (hdev->supports_wait_for_multi_cs)
1653 cs->fence->stream_master_qid_map = stream_master_qid_map;
1654
1655 rc = hl_hw_queue_schedule_cs(cs);
1656 if (rc) {
1657 if (rc != -EAGAIN)
1658 dev_err(hdev->dev,
1659 "Failed to submit CS %d.%llu to H/W queues, error %d\n",
1660 cs->ctx->asid, cs->sequence, rc);
1661 goto free_cs_object;
1662 }
1663
1664 *signal_initial_sob_count = cs->initial_sob_count;
1665
1666 rc = HL_CS_STATUS_SUCCESS;
1667 goto put_cs;
1668
1669 release_cb:
1670 atomic_dec(&cb->cs_cnt);
1671 hl_cb_put(cb);
1672 free_cs_object:
1673 cs_rollback(hdev, cs);
1674 *cs_seq = ULLONG_MAX;
1675 /* The path below is both for good and erroneous exits */
1676 put_cs:
1677 /* We finished with the CS in this function, so put the ref */
1678 cs_put(cs);
1679 free_cs_chunk_array:
1680 kfree(cs_chunk_array);
1681 out:
1682 return rc;
1683 }
1684
hl_cs_ctx_switch(struct hl_fpriv * hpriv,union hl_cs_args * args,u64 * cs_seq)1685 static int hl_cs_ctx_switch(struct hl_fpriv *hpriv, union hl_cs_args *args,
1686 u64 *cs_seq)
1687 {
1688 struct hl_device *hdev = hpriv->hdev;
1689 struct hl_ctx *ctx = hpriv->ctx;
1690 bool need_soft_reset = false;
1691 int rc = 0, do_ctx_switch = 0;
1692 void __user *chunks;
1693 u32 num_chunks, tmp;
1694 u16 sob_count;
1695 int ret;
1696
1697 if (hdev->supports_ctx_switch)
1698 do_ctx_switch = atomic_cmpxchg(&ctx->thread_ctx_switch_token, 1, 0);
1699
1700 if (do_ctx_switch || (args->in.cs_flags & HL_CS_FLAGS_FORCE_RESTORE)) {
1701 mutex_lock(&hpriv->restore_phase_mutex);
1702
1703 if (do_ctx_switch) {
1704 rc = hdev->asic_funcs->context_switch(hdev, ctx->asid);
1705 if (rc) {
1706 dev_err_ratelimited(hdev->dev,
1707 "Failed to switch to context %d, rejecting CS! %d\n",
1708 ctx->asid, rc);
1709 /*
1710 * If we timedout, or if the device is not IDLE
1711 * while we want to do context-switch (-EBUSY),
1712 * we need to soft-reset because QMAN is
1713 * probably stuck. However, we can't call to
1714 * reset here directly because of deadlock, so
1715 * need to do it at the very end of this
1716 * function
1717 */
1718 if ((rc == -ETIMEDOUT) || (rc == -EBUSY))
1719 need_soft_reset = true;
1720 mutex_unlock(&hpriv->restore_phase_mutex);
1721 goto out;
1722 }
1723 }
1724
1725 hdev->asic_funcs->restore_phase_topology(hdev);
1726
1727 chunks = (void __user *) (uintptr_t) args->in.chunks_restore;
1728 num_chunks = args->in.num_chunks_restore;
1729
1730 if (!num_chunks) {
1731 dev_dbg(hdev->dev,
1732 "Need to run restore phase but restore CS is empty\n");
1733 rc = 0;
1734 } else {
1735 rc = cs_ioctl_default(hpriv, chunks, num_chunks,
1736 cs_seq, 0, 0, hdev->timeout_jiffies, &sob_count);
1737 }
1738
1739 mutex_unlock(&hpriv->restore_phase_mutex);
1740
1741 if (rc) {
1742 dev_err(hdev->dev,
1743 "Failed to submit restore CS for context %d (%d)\n",
1744 ctx->asid, rc);
1745 goto out;
1746 }
1747
1748 /* Need to wait for restore completion before execution phase */
1749 if (num_chunks) {
1750 enum hl_cs_wait_status status;
1751
1752 ret = _hl_cs_wait_ioctl(hdev, ctx,
1753 jiffies_to_usecs(hdev->timeout_jiffies),
1754 *cs_seq, &status, NULL);
1755 if (ret) {
1756 dev_err(hdev->dev,
1757 "Restore CS for context %d failed to complete %d\n",
1758 ctx->asid, ret);
1759 rc = -ENOEXEC;
1760 goto out;
1761 }
1762 }
1763
1764 if (hdev->supports_ctx_switch)
1765 ctx->thread_ctx_switch_wait_token = 1;
1766
1767 } else if (hdev->supports_ctx_switch && !ctx->thread_ctx_switch_wait_token) {
1768 rc = hl_poll_timeout_memory(hdev,
1769 &ctx->thread_ctx_switch_wait_token, tmp, (tmp == 1),
1770 100, jiffies_to_usecs(hdev->timeout_jiffies), false);
1771
1772 if (rc == -ETIMEDOUT) {
1773 dev_err(hdev->dev,
1774 "context switch phase timeout (%d)\n", tmp);
1775 goto out;
1776 }
1777 }
1778
1779 out:
1780 if ((rc == -ETIMEDOUT || rc == -EBUSY) && (need_soft_reset))
1781 hl_device_reset(hdev, 0);
1782
1783 return rc;
1784 }
1785
1786 /*
1787 * hl_cs_signal_sob_wraparound_handler: handle SOB value wrapaound case.
1788 * if the SOB value reaches the max value move to the other SOB reserved
1789 * to the queue.
1790 * @hdev: pointer to device structure
1791 * @q_idx: stream queue index
1792 * @hw_sob: the H/W SOB used in this signal CS.
1793 * @count: signals count
1794 * @encaps_sig: tells whether it's reservation for encaps signals or not.
1795 *
1796 * Note that this function must be called while hw_queues_lock is taken.
1797 */
hl_cs_signal_sob_wraparound_handler(struct hl_device * hdev,u32 q_idx,struct hl_hw_sob ** hw_sob,u32 count,bool encaps_sig)1798 int hl_cs_signal_sob_wraparound_handler(struct hl_device *hdev, u32 q_idx,
1799 struct hl_hw_sob **hw_sob, u32 count, bool encaps_sig)
1800
1801 {
1802 struct hl_sync_stream_properties *prop;
1803 struct hl_hw_sob *sob = *hw_sob, *other_sob;
1804 u8 other_sob_offset;
1805
1806 prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
1807
1808 hw_sob_get(sob);
1809
1810 /* check for wraparound */
1811 if (prop->next_sob_val + count >= HL_MAX_SOB_VAL) {
1812 /*
1813 * Decrement as we reached the max value.
1814 * The release function won't be called here as we've
1815 * just incremented the refcount right before calling this
1816 * function.
1817 */
1818 hw_sob_put_err(sob);
1819
1820 /*
1821 * check the other sob value, if it still in use then fail
1822 * otherwise make the switch
1823 */
1824 other_sob_offset = (prop->curr_sob_offset + 1) % HL_RSVD_SOBS;
1825 other_sob = &prop->hw_sob[other_sob_offset];
1826
1827 if (kref_read(&other_sob->kref) != 1) {
1828 dev_err(hdev->dev, "error: Cannot switch SOBs q_idx: %d\n",
1829 q_idx);
1830 return -EINVAL;
1831 }
1832
1833 /*
1834 * next_sob_val always points to the next available signal
1835 * in the sob, so in encaps signals it will be the next one
1836 * after reserving the required amount.
1837 */
1838 if (encaps_sig)
1839 prop->next_sob_val = count + 1;
1840 else
1841 prop->next_sob_val = count;
1842
1843 /* only two SOBs are currently in use */
1844 prop->curr_sob_offset = other_sob_offset;
1845 *hw_sob = other_sob;
1846
1847 /*
1848 * check if other_sob needs reset, then do it before using it
1849 * for the reservation or the next signal cs.
1850 * we do it here, and for both encaps and regular signal cs
1851 * cases in order to avoid possible races of two kref_put
1852 * of the sob which can occur at the same time if we move the
1853 * sob reset(kref_put) to cs_do_release function.
1854 * in addition, if we have combination of cs signal and
1855 * encaps, and at the point we need to reset the sob there was
1856 * no more reservations and only signal cs keep coming,
1857 * in such case we need signal_cs to put the refcount and
1858 * reset the sob.
1859 */
1860 if (other_sob->need_reset)
1861 hw_sob_put(other_sob);
1862
1863 if (encaps_sig) {
1864 /* set reset indication for the sob */
1865 sob->need_reset = true;
1866 hw_sob_get(other_sob);
1867 }
1868
1869 dev_dbg(hdev->dev, "switched to SOB %d, q_idx: %d\n",
1870 prop->curr_sob_offset, q_idx);
1871 } else {
1872 prop->next_sob_val += count;
1873 }
1874
1875 return 0;
1876 }
1877
cs_ioctl_extract_signal_seq(struct hl_device * hdev,struct hl_cs_chunk * chunk,u64 * signal_seq,struct hl_ctx * ctx,bool encaps_signals)1878 static int cs_ioctl_extract_signal_seq(struct hl_device *hdev,
1879 struct hl_cs_chunk *chunk, u64 *signal_seq, struct hl_ctx *ctx,
1880 bool encaps_signals)
1881 {
1882 u64 *signal_seq_arr = NULL;
1883 u32 size_to_copy, signal_seq_arr_len;
1884 int rc = 0;
1885
1886 if (encaps_signals) {
1887 *signal_seq = chunk->encaps_signal_seq;
1888 return 0;
1889 }
1890
1891 signal_seq_arr_len = chunk->num_signal_seq_arr;
1892
1893 /* currently only one signal seq is supported */
1894 if (signal_seq_arr_len != 1) {
1895 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1896 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1897 dev_err(hdev->dev,
1898 "Wait for signal CS supports only one signal CS seq\n");
1899 return -EINVAL;
1900 }
1901
1902 signal_seq_arr = kmalloc_array(signal_seq_arr_len,
1903 sizeof(*signal_seq_arr),
1904 GFP_ATOMIC);
1905 if (!signal_seq_arr)
1906 signal_seq_arr = kmalloc_array(signal_seq_arr_len,
1907 sizeof(*signal_seq_arr),
1908 GFP_KERNEL);
1909 if (!signal_seq_arr) {
1910 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1911 atomic64_inc(&hdev->aggregated_cs_counters.out_of_mem_drop_cnt);
1912 return -ENOMEM;
1913 }
1914
1915 size_to_copy = signal_seq_arr_len * sizeof(*signal_seq_arr);
1916 if (copy_from_user(signal_seq_arr,
1917 u64_to_user_ptr(chunk->signal_seq_arr),
1918 size_to_copy)) {
1919 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
1920 atomic64_inc(&hdev->aggregated_cs_counters.validation_drop_cnt);
1921 dev_err(hdev->dev,
1922 "Failed to copy signal seq array from user\n");
1923 rc = -EFAULT;
1924 goto out;
1925 }
1926
1927 /* currently it is guaranteed to have only one signal seq */
1928 *signal_seq = signal_seq_arr[0];
1929
1930 out:
1931 kfree(signal_seq_arr);
1932
1933 return rc;
1934 }
1935
cs_ioctl_signal_wait_create_jobs(struct hl_device * hdev,struct hl_ctx * ctx,struct hl_cs * cs,enum hl_queue_type q_type,u32 q_idx,u32 encaps_signal_offset)1936 static int cs_ioctl_signal_wait_create_jobs(struct hl_device *hdev,
1937 struct hl_ctx *ctx, struct hl_cs *cs,
1938 enum hl_queue_type q_type, u32 q_idx, u32 encaps_signal_offset)
1939 {
1940 struct hl_cs_counters_atomic *cntr;
1941 struct hl_cs_job *job;
1942 struct hl_cb *cb;
1943 u32 cb_size;
1944
1945 cntr = &hdev->aggregated_cs_counters;
1946
1947 job = hl_cs_allocate_job(hdev, q_type, true);
1948 if (!job) {
1949 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1950 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1951 dev_err(hdev->dev, "Failed to allocate a new job\n");
1952 return -ENOMEM;
1953 }
1954
1955 if (cs->type == CS_TYPE_WAIT)
1956 cb_size = hdev->asic_funcs->get_wait_cb_size(hdev);
1957 else
1958 cb_size = hdev->asic_funcs->get_signal_cb_size(hdev);
1959
1960 cb = hl_cb_kernel_create(hdev, cb_size, q_type == QUEUE_TYPE_HW);
1961 if (!cb) {
1962 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1963 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1964 kfree(job);
1965 return -EFAULT;
1966 }
1967
1968 job->id = 0;
1969 job->cs = cs;
1970 job->user_cb = cb;
1971 atomic_inc(&job->user_cb->cs_cnt);
1972 job->user_cb_size = cb_size;
1973 job->hw_queue_id = q_idx;
1974
1975 if ((cs->type == CS_TYPE_WAIT || cs->type == CS_TYPE_COLLECTIVE_WAIT)
1976 && cs->encaps_signals)
1977 job->encaps_sig_wait_offset = encaps_signal_offset;
1978 /*
1979 * No need in parsing, user CB is the patched CB.
1980 * We call hl_cb_destroy() out of two reasons - we don't need the CB in
1981 * the CB idr anymore and to decrement its refcount as it was
1982 * incremented inside hl_cb_kernel_create().
1983 */
1984 job->patched_cb = job->user_cb;
1985 job->job_cb_size = job->user_cb_size;
1986 hl_cb_destroy(&hdev->kernel_mem_mgr, cb->buf->handle);
1987
1988 /* increment refcount as for external queues we get completion */
1989 cs_get(cs);
1990
1991 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1992 cs->jobs_cnt++;
1993
1994 list_add_tail(&job->cs_node, &cs->job_list);
1995
1996 hl_debugfs_add_job(hdev, job);
1997
1998 return 0;
1999 }
2000
cs_ioctl_reserve_signals(struct hl_fpriv * hpriv,u32 q_idx,u32 count,u32 * handle_id,u32 * sob_addr,u32 * signals_count)2001 static int cs_ioctl_reserve_signals(struct hl_fpriv *hpriv,
2002 u32 q_idx, u32 count,
2003 u32 *handle_id, u32 *sob_addr,
2004 u32 *signals_count)
2005 {
2006 struct hw_queue_properties *hw_queue_prop;
2007 struct hl_sync_stream_properties *prop;
2008 struct hl_device *hdev = hpriv->hdev;
2009 struct hl_cs_encaps_sig_handle *handle;
2010 struct hl_encaps_signals_mgr *mgr;
2011 struct hl_hw_sob *hw_sob;
2012 int hdl_id;
2013 int rc = 0;
2014
2015 if (count >= HL_MAX_SOB_VAL) {
2016 dev_err(hdev->dev, "signals count(%u) exceeds the max SOB value\n",
2017 count);
2018 rc = -EINVAL;
2019 goto out;
2020 }
2021
2022 if (q_idx >= hdev->asic_prop.max_queues) {
2023 dev_err(hdev->dev, "Queue index %d is invalid\n",
2024 q_idx);
2025 rc = -EINVAL;
2026 goto out;
2027 }
2028
2029 hw_queue_prop = &hdev->asic_prop.hw_queues_props[q_idx];
2030
2031 if (!hw_queue_prop->supports_sync_stream) {
2032 dev_err(hdev->dev,
2033 "Queue index %d does not support sync stream operations\n",
2034 q_idx);
2035 rc = -EINVAL;
2036 goto out;
2037 }
2038
2039 prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
2040
2041 handle = kzalloc_obj(*handle);
2042 if (!handle) {
2043 rc = -ENOMEM;
2044 goto out;
2045 }
2046
2047 handle->count = count;
2048
2049 hl_ctx_get(hpriv->ctx);
2050 handle->ctx = hpriv->ctx;
2051 mgr = &hpriv->ctx->sig_mgr;
2052
2053 spin_lock(&mgr->lock);
2054 hdl_id = idr_alloc(&mgr->handles, handle, 1, 0, GFP_ATOMIC);
2055 spin_unlock(&mgr->lock);
2056
2057 if (hdl_id < 0) {
2058 dev_err(hdev->dev, "Failed to allocate IDR for a new signal reservation\n");
2059 rc = -EINVAL;
2060 goto put_ctx;
2061 }
2062
2063 handle->id = hdl_id;
2064 handle->q_idx = q_idx;
2065 handle->hdev = hdev;
2066 kref_init(&handle->refcount);
2067
2068 hdev->asic_funcs->hw_queues_lock(hdev);
2069
2070 hw_sob = &prop->hw_sob[prop->curr_sob_offset];
2071
2072 /*
2073 * Increment the SOB value by count by user request
2074 * to reserve those signals
2075 * check if the signals amount to reserve is not exceeding the max sob
2076 * value, if yes then switch sob.
2077 */
2078 rc = hl_cs_signal_sob_wraparound_handler(hdev, q_idx, &hw_sob, count,
2079 true);
2080 if (rc) {
2081 dev_err(hdev->dev, "Failed to switch SOB\n");
2082 hdev->asic_funcs->hw_queues_unlock(hdev);
2083 rc = -EINVAL;
2084 goto remove_idr;
2085 }
2086 /* set the hw_sob to the handle after calling the sob wraparound handler
2087 * since sob could have changed.
2088 */
2089 handle->hw_sob = hw_sob;
2090
2091 /* store the current sob value for unreserve validity check, and
2092 * signal offset support
2093 */
2094 handle->pre_sob_val = prop->next_sob_val - handle->count;
2095
2096 handle->cs_seq = ULLONG_MAX;
2097
2098 *signals_count = prop->next_sob_val;
2099 hdev->asic_funcs->hw_queues_unlock(hdev);
2100
2101 *sob_addr = handle->hw_sob->sob_addr;
2102 *handle_id = hdl_id;
2103
2104 dev_dbg(hdev->dev,
2105 "Signals reserved, sob_id: %d, sob addr: 0x%x, last sob_val: %u, q_idx: %d, hdl_id: %d\n",
2106 hw_sob->sob_id, handle->hw_sob->sob_addr,
2107 prop->next_sob_val - 1, q_idx, hdl_id);
2108 goto out;
2109
2110 remove_idr:
2111 spin_lock(&mgr->lock);
2112 idr_remove(&mgr->handles, hdl_id);
2113 spin_unlock(&mgr->lock);
2114
2115 put_ctx:
2116 hl_ctx_put(handle->ctx);
2117 kfree(handle);
2118
2119 out:
2120 return rc;
2121 }
2122
cs_ioctl_unreserve_signals(struct hl_fpriv * hpriv,u32 handle_id)2123 static int cs_ioctl_unreserve_signals(struct hl_fpriv *hpriv, u32 handle_id)
2124 {
2125 struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
2126 struct hl_sync_stream_properties *prop;
2127 struct hl_device *hdev = hpriv->hdev;
2128 struct hl_encaps_signals_mgr *mgr;
2129 struct hl_hw_sob *hw_sob;
2130 u32 q_idx, sob_addr;
2131 int rc = 0;
2132
2133 mgr = &hpriv->ctx->sig_mgr;
2134
2135 spin_lock(&mgr->lock);
2136 encaps_sig_hdl = idr_find(&mgr->handles, handle_id);
2137 if (encaps_sig_hdl) {
2138 dev_dbg(hdev->dev, "unreserve signals, handle: %u, SOB:0x%x, count: %u\n",
2139 handle_id, encaps_sig_hdl->hw_sob->sob_addr,
2140 encaps_sig_hdl->count);
2141
2142 hdev->asic_funcs->hw_queues_lock(hdev);
2143
2144 q_idx = encaps_sig_hdl->q_idx;
2145 prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
2146 hw_sob = &prop->hw_sob[prop->curr_sob_offset];
2147 sob_addr = hdev->asic_funcs->get_sob_addr(hdev, hw_sob->sob_id);
2148
2149 /* Check if sob_val got out of sync due to other
2150 * signal submission requests which were handled
2151 * between the reserve-unreserve calls or SOB switch
2152 * upon reaching SOB max value.
2153 */
2154 if (encaps_sig_hdl->pre_sob_val + encaps_sig_hdl->count
2155 != prop->next_sob_val ||
2156 sob_addr != encaps_sig_hdl->hw_sob->sob_addr) {
2157 dev_err(hdev->dev, "Cannot unreserve signals, SOB val ran out of sync, expected: %u, actual val: %u\n",
2158 encaps_sig_hdl->pre_sob_val,
2159 (prop->next_sob_val - encaps_sig_hdl->count));
2160
2161 hdev->asic_funcs->hw_queues_unlock(hdev);
2162 rc = -EINVAL;
2163 goto out_unlock;
2164 }
2165
2166 /*
2167 * Decrement the SOB value by count by user request
2168 * to unreserve those signals
2169 */
2170 prop->next_sob_val -= encaps_sig_hdl->count;
2171
2172 hdev->asic_funcs->hw_queues_unlock(hdev);
2173
2174 hw_sob_put(hw_sob);
2175
2176 /* Release the id and free allocated memory of the handle */
2177 idr_remove(&mgr->handles, handle_id);
2178
2179 /* unlock before calling ctx_put, where we might sleep */
2180 spin_unlock(&mgr->lock);
2181 hl_ctx_put(encaps_sig_hdl->ctx);
2182 kfree(encaps_sig_hdl);
2183 goto out;
2184 } else {
2185 rc = -EINVAL;
2186 dev_err(hdev->dev, "failed to unreserve signals, cannot find handler\n");
2187 }
2188
2189 out_unlock:
2190 spin_unlock(&mgr->lock);
2191
2192 out:
2193 return rc;
2194 }
2195
cs_ioctl_signal_wait(struct hl_fpriv * hpriv,enum hl_cs_type cs_type,void __user * chunks,u32 num_chunks,u64 * cs_seq,u32 flags,u32 timeout,u32 * signal_sob_addr_offset,u16 * signal_initial_sob_count)2196 static int cs_ioctl_signal_wait(struct hl_fpriv *hpriv, enum hl_cs_type cs_type,
2197 void __user *chunks, u32 num_chunks,
2198 u64 *cs_seq, u32 flags, u32 timeout,
2199 u32 *signal_sob_addr_offset, u16 *signal_initial_sob_count)
2200 {
2201 struct hl_cs_encaps_sig_handle *encaps_sig_hdl = NULL;
2202 bool handle_found = false, is_wait_cs = false,
2203 wait_cs_submitted = false,
2204 cs_encaps_signals = false;
2205 struct hl_cs_chunk *cs_chunk_array, *chunk;
2206 bool staged_cs_with_encaps_signals = false;
2207 struct hw_queue_properties *hw_queue_prop;
2208 struct hl_device *hdev = hpriv->hdev;
2209 struct hl_cs_compl *sig_waitcs_cmpl;
2210 u32 q_idx, collective_engine_id = 0;
2211 struct hl_cs_counters_atomic *cntr;
2212 struct hl_fence *sig_fence = NULL;
2213 struct hl_ctx *ctx = hpriv->ctx;
2214 enum hl_queue_type q_type;
2215 struct hl_cs *cs;
2216 u64 signal_seq;
2217 int rc;
2218
2219 cntr = &hdev->aggregated_cs_counters;
2220 *cs_seq = ULLONG_MAX;
2221
2222 rc = hl_cs_copy_chunk_array(hdev, &cs_chunk_array, chunks, num_chunks,
2223 ctx);
2224 if (rc)
2225 goto out;
2226
2227 /* currently it is guaranteed to have only one chunk */
2228 chunk = &cs_chunk_array[0];
2229
2230 if (chunk->queue_index >= hdev->asic_prop.max_queues) {
2231 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2232 atomic64_inc(&cntr->validation_drop_cnt);
2233 dev_err(hdev->dev, "Queue index %d is invalid\n",
2234 chunk->queue_index);
2235 rc = -EINVAL;
2236 goto free_cs_chunk_array;
2237 }
2238
2239 q_idx = chunk->queue_index;
2240 hw_queue_prop = &hdev->asic_prop.hw_queues_props[q_idx];
2241 q_type = hw_queue_prop->type;
2242
2243 if (!hw_queue_prop->supports_sync_stream) {
2244 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2245 atomic64_inc(&cntr->validation_drop_cnt);
2246 dev_err(hdev->dev,
2247 "Queue index %d does not support sync stream operations\n",
2248 q_idx);
2249 rc = -EINVAL;
2250 goto free_cs_chunk_array;
2251 }
2252
2253 if (cs_type == CS_TYPE_COLLECTIVE_WAIT) {
2254 if (!(hw_queue_prop->collective_mode == HL_COLLECTIVE_MASTER)) {
2255 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2256 atomic64_inc(&cntr->validation_drop_cnt);
2257 dev_err(hdev->dev,
2258 "Queue index %d is invalid\n", q_idx);
2259 rc = -EINVAL;
2260 goto free_cs_chunk_array;
2261 }
2262
2263 if (!hdev->nic_ports_mask) {
2264 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2265 atomic64_inc(&cntr->validation_drop_cnt);
2266 dev_err(hdev->dev,
2267 "Collective operations not supported when NIC ports are disabled");
2268 rc = -EINVAL;
2269 goto free_cs_chunk_array;
2270 }
2271
2272 collective_engine_id = chunk->collective_engine_id;
2273 }
2274
2275 is_wait_cs = !!(cs_type == CS_TYPE_WAIT ||
2276 cs_type == CS_TYPE_COLLECTIVE_WAIT);
2277
2278 cs_encaps_signals = !!(flags & HL_CS_FLAGS_ENCAP_SIGNALS);
2279
2280 if (is_wait_cs) {
2281 rc = cs_ioctl_extract_signal_seq(hdev, chunk, &signal_seq,
2282 ctx, cs_encaps_signals);
2283 if (rc)
2284 goto free_cs_chunk_array;
2285
2286 if (cs_encaps_signals) {
2287 /* check if cs sequence has encapsulated
2288 * signals handle
2289 */
2290 struct idr *idp;
2291 u32 id;
2292
2293 spin_lock(&ctx->sig_mgr.lock);
2294 idp = &ctx->sig_mgr.handles;
2295 idr_for_each_entry(idp, encaps_sig_hdl, id) {
2296 if (encaps_sig_hdl->cs_seq == signal_seq) {
2297 /* get refcount to protect removing this handle from idr,
2298 * needed when multiple wait cs are used with offset
2299 * to wait on reserved encaps signals.
2300 * Since kref_put of this handle is executed outside the
2301 * current lock, it is possible that the handle refcount
2302 * is 0 but it yet to be removed from the list. In this
2303 * case need to consider the handle as not valid.
2304 */
2305 if (kref_get_unless_zero(&encaps_sig_hdl->refcount))
2306 handle_found = true;
2307 break;
2308 }
2309 }
2310 spin_unlock(&ctx->sig_mgr.lock);
2311
2312 if (!handle_found) {
2313 /* treat as signal CS already finished */
2314 dev_dbg(hdev->dev, "Cannot find encapsulated signals handle for seq 0x%llx\n",
2315 signal_seq);
2316 rc = 0;
2317 goto free_cs_chunk_array;
2318 }
2319
2320 /* validate also the signal offset value */
2321 if (chunk->encaps_signal_offset >
2322 encaps_sig_hdl->count) {
2323 dev_err(hdev->dev, "offset(%u) value exceed max reserved signals count(%u)!\n",
2324 chunk->encaps_signal_offset,
2325 encaps_sig_hdl->count);
2326 rc = -EINVAL;
2327 goto free_cs_chunk_array;
2328 }
2329 }
2330
2331 sig_fence = hl_ctx_get_fence(ctx, signal_seq);
2332 if (IS_ERR(sig_fence)) {
2333 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2334 atomic64_inc(&cntr->validation_drop_cnt);
2335 dev_err(hdev->dev,
2336 "Failed to get signal CS with seq 0x%llx\n",
2337 signal_seq);
2338 rc = PTR_ERR(sig_fence);
2339 goto free_cs_chunk_array;
2340 }
2341
2342 if (!sig_fence) {
2343 /* signal CS already finished */
2344 rc = 0;
2345 goto free_cs_chunk_array;
2346 }
2347
2348 sig_waitcs_cmpl =
2349 container_of(sig_fence, struct hl_cs_compl, base_fence);
2350
2351 staged_cs_with_encaps_signals = !!
2352 (sig_waitcs_cmpl->type == CS_TYPE_DEFAULT &&
2353 (flags & HL_CS_FLAGS_ENCAP_SIGNALS));
2354
2355 if (sig_waitcs_cmpl->type != CS_TYPE_SIGNAL &&
2356 !staged_cs_with_encaps_signals) {
2357 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2358 atomic64_inc(&cntr->validation_drop_cnt);
2359 dev_err(hdev->dev,
2360 "CS seq 0x%llx is not of a signal/encaps-signal CS\n",
2361 signal_seq);
2362 hl_fence_put(sig_fence);
2363 rc = -EINVAL;
2364 goto free_cs_chunk_array;
2365 }
2366
2367 if (completion_done(&sig_fence->completion)) {
2368 /* signal CS already finished */
2369 hl_fence_put(sig_fence);
2370 rc = 0;
2371 goto free_cs_chunk_array;
2372 }
2373 }
2374
2375 rc = allocate_cs(hdev, ctx, cs_type, ULLONG_MAX, &cs, flags, timeout);
2376 if (rc) {
2377 if (is_wait_cs)
2378 hl_fence_put(sig_fence);
2379
2380 goto free_cs_chunk_array;
2381 }
2382
2383 /*
2384 * Save the signal CS fence for later initialization right before
2385 * hanging the wait CS on the queue.
2386 * for encaps signals case, we save the cs sequence and handle pointer
2387 * for later initialization.
2388 */
2389 if (is_wait_cs) {
2390 cs->signal_fence = sig_fence;
2391 /* store the handle pointer, so we don't have to
2392 * look for it again, later on the flow
2393 * when we need to set SOB info in hw_queue.
2394 */
2395 if (cs->encaps_signals)
2396 cs->encaps_sig_hdl = encaps_sig_hdl;
2397 }
2398
2399 hl_debugfs_add_cs(cs);
2400
2401 *cs_seq = cs->sequence;
2402
2403 if (cs_type == CS_TYPE_WAIT || cs_type == CS_TYPE_SIGNAL)
2404 rc = cs_ioctl_signal_wait_create_jobs(hdev, ctx, cs, q_type,
2405 q_idx, chunk->encaps_signal_offset);
2406 else if (cs_type == CS_TYPE_COLLECTIVE_WAIT)
2407 rc = hdev->asic_funcs->collective_wait_create_jobs(hdev, ctx,
2408 cs, q_idx, collective_engine_id,
2409 chunk->encaps_signal_offset);
2410 else {
2411 atomic64_inc(&ctx->cs_counters.validation_drop_cnt);
2412 atomic64_inc(&cntr->validation_drop_cnt);
2413 rc = -EINVAL;
2414 }
2415
2416 if (rc)
2417 goto free_cs_object;
2418
2419 if (q_type == QUEUE_TYPE_HW)
2420 INIT_WORK(&cs->finish_work, cs_completion);
2421
2422 rc = hl_hw_queue_schedule_cs(cs);
2423 if (rc) {
2424 /* In case wait cs failed here, it means the signal cs
2425 * already completed. we want to free all it's related objects
2426 * but we don't want to fail the ioctl.
2427 */
2428 if (is_wait_cs)
2429 rc = 0;
2430 else if (rc != -EAGAIN)
2431 dev_err(hdev->dev,
2432 "Failed to submit CS %d.%llu to H/W queues, error %d\n",
2433 ctx->asid, cs->sequence, rc);
2434 goto free_cs_object;
2435 }
2436
2437 *signal_sob_addr_offset = cs->sob_addr_offset;
2438 *signal_initial_sob_count = cs->initial_sob_count;
2439
2440 rc = HL_CS_STATUS_SUCCESS;
2441 if (is_wait_cs)
2442 wait_cs_submitted = true;
2443 goto put_cs;
2444
2445 free_cs_object:
2446 cs_rollback(hdev, cs);
2447 *cs_seq = ULLONG_MAX;
2448 /* The path below is both for good and erroneous exits */
2449 put_cs:
2450 /* We finished with the CS in this function, so put the ref */
2451 cs_put(cs);
2452 free_cs_chunk_array:
2453 if (!wait_cs_submitted && cs_encaps_signals && handle_found && is_wait_cs)
2454 kref_put(&encaps_sig_hdl->refcount, hl_encaps_release_handle_and_put_ctx);
2455 kfree(cs_chunk_array);
2456 out:
2457 return rc;
2458 }
2459
cs_ioctl_engine_cores(struct hl_fpriv * hpriv,u64 engine_cores,u32 num_engine_cores,u32 core_command)2460 static int cs_ioctl_engine_cores(struct hl_fpriv *hpriv, u64 engine_cores,
2461 u32 num_engine_cores, u32 core_command)
2462 {
2463 struct hl_device *hdev = hpriv->hdev;
2464 void __user *engine_cores_arr;
2465 u32 *cores;
2466 int rc;
2467
2468 if (!hdev->asic_prop.supports_engine_modes)
2469 return -EPERM;
2470
2471 if (!num_engine_cores || num_engine_cores > hdev->asic_prop.num_engine_cores) {
2472 dev_err(hdev->dev, "Number of engine cores %d is invalid\n", num_engine_cores);
2473 return -EINVAL;
2474 }
2475
2476 if (core_command != HL_ENGINE_CORE_RUN && core_command != HL_ENGINE_CORE_HALT) {
2477 dev_err(hdev->dev, "Engine core command is invalid\n");
2478 return -EINVAL;
2479 }
2480
2481 engine_cores_arr = (void __user *) (uintptr_t) engine_cores;
2482 cores = kmalloc_array(num_engine_cores, sizeof(u32), GFP_KERNEL);
2483 if (!cores)
2484 return -ENOMEM;
2485
2486 if (copy_from_user(cores, engine_cores_arr, num_engine_cores * sizeof(u32))) {
2487 dev_err(hdev->dev, "Failed to copy core-ids array from user\n");
2488 kfree(cores);
2489 return -EFAULT;
2490 }
2491
2492 rc = hdev->asic_funcs->set_engine_cores(hdev, cores, num_engine_cores, core_command);
2493 kfree(cores);
2494
2495 return rc;
2496 }
2497
cs_ioctl_engines(struct hl_fpriv * hpriv,u64 engines_arr_user_addr,u32 num_engines,enum hl_engine_command command)2498 static int cs_ioctl_engines(struct hl_fpriv *hpriv, u64 engines_arr_user_addr,
2499 u32 num_engines, enum hl_engine_command command)
2500 {
2501 struct hl_device *hdev = hpriv->hdev;
2502 u32 *engines, max_num_of_engines;
2503 void __user *engines_arr;
2504 int rc;
2505
2506 if (!hdev->asic_prop.supports_engine_modes)
2507 return -EPERM;
2508
2509 if (command >= HL_ENGINE_COMMAND_MAX) {
2510 dev_err(hdev->dev, "Engine command is invalid\n");
2511 return -EINVAL;
2512 }
2513
2514 max_num_of_engines = hdev->asic_prop.max_num_of_engines;
2515 if (command == HL_ENGINE_CORE_RUN || command == HL_ENGINE_CORE_HALT)
2516 max_num_of_engines = hdev->asic_prop.num_engine_cores;
2517
2518 if (!num_engines || num_engines > max_num_of_engines) {
2519 dev_err(hdev->dev, "Number of engines %d is invalid\n", num_engines);
2520 return -EINVAL;
2521 }
2522
2523 engines_arr = (void __user *) (uintptr_t) engines_arr_user_addr;
2524 engines = kmalloc_array(num_engines, sizeof(u32), GFP_KERNEL);
2525 if (!engines)
2526 return -ENOMEM;
2527
2528 if (copy_from_user(engines, engines_arr, num_engines * sizeof(u32))) {
2529 dev_err(hdev->dev, "Failed to copy engine-ids array from user\n");
2530 kfree(engines);
2531 return -EFAULT;
2532 }
2533
2534 rc = hdev->asic_funcs->set_engines(hdev, engines, num_engines, command);
2535 kfree(engines);
2536
2537 return rc;
2538 }
2539
cs_ioctl_flush_pci_hbw_writes(struct hl_fpriv * hpriv)2540 static int cs_ioctl_flush_pci_hbw_writes(struct hl_fpriv *hpriv)
2541 {
2542 struct hl_device *hdev = hpriv->hdev;
2543 struct asic_fixed_properties *prop = &hdev->asic_prop;
2544
2545 if (!prop->hbw_flush_reg) {
2546 dev_dbg(hdev->dev, "HBW flush is not supported\n");
2547 return -EOPNOTSUPP;
2548 }
2549
2550 RREG32(prop->hbw_flush_reg);
2551
2552 return 0;
2553 }
2554
hl_cs_ioctl(struct drm_device * ddev,void * data,struct drm_file * file_priv)2555 int hl_cs_ioctl(struct drm_device *ddev, void *data, struct drm_file *file_priv)
2556 {
2557 struct hl_fpriv *hpriv = file_priv->driver_priv;
2558 union hl_cs_args *args = data;
2559 enum hl_cs_type cs_type = 0;
2560 u64 cs_seq = ULONG_MAX;
2561 void __user *chunks;
2562 u32 num_chunks, flags, timeout,
2563 signals_count = 0, sob_addr = 0, handle_id = 0;
2564 u16 sob_initial_count = 0;
2565 int rc;
2566
2567 rc = hl_cs_sanity_checks(hpriv, args);
2568 if (rc)
2569 goto out;
2570
2571 rc = hl_cs_ctx_switch(hpriv, args, &cs_seq);
2572 if (rc)
2573 goto out;
2574
2575 cs_type = hl_cs_get_cs_type(args->in.cs_flags &
2576 ~HL_CS_FLAGS_FORCE_RESTORE);
2577 chunks = (void __user *) (uintptr_t) args->in.chunks_execute;
2578 num_chunks = args->in.num_chunks_execute;
2579 flags = args->in.cs_flags;
2580
2581 /* In case this is a staged CS, user should supply the CS sequence */
2582 if ((flags & HL_CS_FLAGS_STAGED_SUBMISSION) &&
2583 !(flags & HL_CS_FLAGS_STAGED_SUBMISSION_FIRST))
2584 cs_seq = args->in.seq;
2585
2586 timeout = flags & HL_CS_FLAGS_CUSTOM_TIMEOUT
2587 ? secs_to_jiffies(args->in.timeout)
2588 : hpriv->hdev->timeout_jiffies;
2589
2590 switch (cs_type) {
2591 case CS_TYPE_SIGNAL:
2592 case CS_TYPE_WAIT:
2593 case CS_TYPE_COLLECTIVE_WAIT:
2594 rc = cs_ioctl_signal_wait(hpriv, cs_type, chunks, num_chunks,
2595 &cs_seq, args->in.cs_flags, timeout,
2596 &sob_addr, &sob_initial_count);
2597 break;
2598 case CS_RESERVE_SIGNALS:
2599 rc = cs_ioctl_reserve_signals(hpriv,
2600 args->in.encaps_signals_q_idx,
2601 args->in.encaps_signals_count,
2602 &handle_id, &sob_addr, &signals_count);
2603 break;
2604 case CS_UNRESERVE_SIGNALS:
2605 rc = cs_ioctl_unreserve_signals(hpriv,
2606 args->in.encaps_sig_handle_id);
2607 break;
2608 case CS_TYPE_ENGINE_CORE:
2609 rc = cs_ioctl_engine_cores(hpriv, args->in.engine_cores,
2610 args->in.num_engine_cores, args->in.core_command);
2611 break;
2612 case CS_TYPE_ENGINES:
2613 rc = cs_ioctl_engines(hpriv, args->in.engines,
2614 args->in.num_engines, args->in.engine_command);
2615 break;
2616 case CS_TYPE_FLUSH_PCI_HBW_WRITES:
2617 rc = cs_ioctl_flush_pci_hbw_writes(hpriv);
2618 break;
2619 default:
2620 rc = cs_ioctl_default(hpriv, chunks, num_chunks, &cs_seq,
2621 args->in.cs_flags,
2622 args->in.encaps_sig_handle_id,
2623 timeout, &sob_initial_count);
2624 break;
2625 }
2626 out:
2627 if (rc != -EAGAIN) {
2628 memset(args, 0, sizeof(*args));
2629
2630 switch (cs_type) {
2631 case CS_RESERVE_SIGNALS:
2632 args->out.handle_id = handle_id;
2633 args->out.sob_base_addr_offset = sob_addr;
2634 args->out.count = signals_count;
2635 break;
2636 case CS_TYPE_SIGNAL:
2637 args->out.sob_base_addr_offset = sob_addr;
2638 args->out.sob_count_before_submission = sob_initial_count;
2639 args->out.seq = cs_seq;
2640 break;
2641 case CS_TYPE_DEFAULT:
2642 args->out.sob_count_before_submission = sob_initial_count;
2643 args->out.seq = cs_seq;
2644 break;
2645 default:
2646 args->out.seq = cs_seq;
2647 break;
2648 }
2649
2650 args->out.status = rc;
2651 }
2652
2653 return rc;
2654 }
2655
hl_wait_for_fence(struct hl_ctx * ctx,u64 seq,struct hl_fence * fence,enum hl_cs_wait_status * status,u64 timeout_us,s64 * timestamp)2656 static int hl_wait_for_fence(struct hl_ctx *ctx, u64 seq, struct hl_fence *fence,
2657 enum hl_cs_wait_status *status, u64 timeout_us, s64 *timestamp)
2658 {
2659 struct hl_device *hdev = ctx->hdev;
2660 ktime_t timestamp_kt;
2661 long completion_rc;
2662 int rc = 0, error;
2663
2664 if (IS_ERR(fence)) {
2665 rc = PTR_ERR(fence);
2666 if (rc == -EINVAL)
2667 dev_notice_ratelimited(hdev->dev,
2668 "Can't wait on CS %llu because current CS is at seq %llu\n",
2669 seq, ctx->cs_sequence);
2670 return rc;
2671 }
2672
2673 if (!fence) {
2674 if (!hl_pop_cs_outcome(&ctx->outcome_store, seq, ×tamp_kt, &error)) {
2675 dev_dbg(hdev->dev,
2676 "Can't wait on seq %llu because current CS is at seq %llu (Fence is gone)\n",
2677 seq, ctx->cs_sequence);
2678 *status = CS_WAIT_STATUS_GONE;
2679 return 0;
2680 }
2681
2682 completion_rc = 1;
2683 goto report_results;
2684 }
2685
2686 if (!timeout_us) {
2687 completion_rc = completion_done(&fence->completion);
2688 } else {
2689 unsigned long timeout;
2690
2691 timeout = (timeout_us == MAX_SCHEDULE_TIMEOUT) ?
2692 timeout_us : usecs_to_jiffies(timeout_us);
2693 completion_rc =
2694 wait_for_completion_interruptible_timeout(
2695 &fence->completion, timeout);
2696 }
2697
2698 error = fence->error;
2699 timestamp_kt = fence->timestamp;
2700
2701 report_results:
2702 if (completion_rc > 0) {
2703 *status = CS_WAIT_STATUS_COMPLETED;
2704 if (timestamp)
2705 *timestamp = ktime_to_ns(timestamp_kt);
2706 } else {
2707 *status = CS_WAIT_STATUS_BUSY;
2708 }
2709
2710 if (completion_rc == -ERESTARTSYS)
2711 rc = completion_rc;
2712 else if (error == -ETIMEDOUT || error == -EIO)
2713 rc = error;
2714
2715 return rc;
2716 }
2717
2718 /*
2719 * hl_cs_poll_fences - iterate CS fences to check for CS completion
2720 *
2721 * @mcs_data: multi-CS internal data
2722 * @mcs_compl: multi-CS completion structure
2723 *
2724 * @return 0 on success, otherwise non 0 error code
2725 *
2726 * The function iterates on all CS sequence in the list and set bit in
2727 * completion_bitmap for each completed CS.
2728 * While iterating, the function sets the stream map of each fence in the fence
2729 * array in the completion QID stream map to be used by CSs to perform
2730 * completion to the multi-CS context.
2731 * This function shall be called after taking context ref
2732 */
hl_cs_poll_fences(struct multi_cs_data * mcs_data,struct multi_cs_completion * mcs_compl)2733 static int hl_cs_poll_fences(struct multi_cs_data *mcs_data, struct multi_cs_completion *mcs_compl)
2734 {
2735 struct hl_fence **fence_ptr = mcs_data->fence_arr;
2736 struct hl_device *hdev = mcs_data->ctx->hdev;
2737 int i, rc, arr_len = mcs_data->arr_len;
2738 u64 *seq_arr = mcs_data->seq_arr;
2739 ktime_t max_ktime, first_cs_time;
2740 enum hl_cs_wait_status status;
2741
2742 memset(fence_ptr, 0, arr_len * sizeof(struct hl_fence *));
2743
2744 /* get all fences under the same lock */
2745 rc = hl_ctx_get_fences(mcs_data->ctx, seq_arr, fence_ptr, arr_len);
2746 if (rc)
2747 return rc;
2748
2749 /*
2750 * re-initialize the completion here to handle 2 possible cases:
2751 * 1. CS will complete the multi-CS prior clearing the completion. in which
2752 * case the fence iteration is guaranteed to catch the CS completion.
2753 * 2. the completion will occur after re-init of the completion.
2754 * in which case we will wake up immediately in wait_for_completion.
2755 */
2756 reinit_completion(&mcs_compl->completion);
2757
2758 /*
2759 * set to maximum time to verify timestamp is valid: if at the end
2760 * this value is maintained- no timestamp was updated
2761 */
2762 max_ktime = ktime_set(KTIME_SEC_MAX, 0);
2763 first_cs_time = max_ktime;
2764
2765 for (i = 0; i < arr_len; i++, fence_ptr++) {
2766 struct hl_fence *fence = *fence_ptr;
2767
2768 /*
2769 * In order to prevent case where we wait until timeout even though a CS associated
2770 * with the multi-CS actually completed we do things in the below order:
2771 * 1. for each fence set it's QID map in the multi-CS completion QID map. This way
2772 * any CS can, potentially, complete the multi CS for the specific QID (note
2773 * that once completion is initialized, calling complete* and then wait on the
2774 * completion will cause it to return at once)
2775 * 2. only after allowing multi-CS completion for the specific QID we check whether
2776 * the specific CS already completed (and thus the wait for completion part will
2777 * be skipped). if the CS not completed it is guaranteed that completing CS will
2778 * wake up the completion.
2779 */
2780 if (fence)
2781 mcs_compl->stream_master_qid_map |= fence->stream_master_qid_map;
2782
2783 /*
2784 * function won't sleep as it is called with timeout 0 (i.e.
2785 * poll the fence)
2786 */
2787 rc = hl_wait_for_fence(mcs_data->ctx, seq_arr[i], fence, &status, 0, NULL);
2788 if (rc) {
2789 dev_err(hdev->dev,
2790 "wait_for_fence error :%d for CS seq %llu\n",
2791 rc, seq_arr[i]);
2792 break;
2793 }
2794
2795 switch (status) {
2796 case CS_WAIT_STATUS_BUSY:
2797 /* CS did not finished, QID to wait on already stored */
2798 break;
2799 case CS_WAIT_STATUS_COMPLETED:
2800 /*
2801 * Using mcs_handling_done to avoid possibility of mcs_data
2802 * returns to user indicating CS completed before it finished
2803 * all of its mcs handling, to avoid race the next time the
2804 * user waits for mcs.
2805 * note: when reaching this case fence is definitely not NULL
2806 * but NULL check was added to overcome static analysis
2807 */
2808 if (fence && !fence->mcs_handling_done) {
2809 /*
2810 * in case multi CS is completed but MCS handling not done
2811 * we "complete" the multi CS to prevent it from waiting
2812 * until time-out and the "multi-CS handling done" will have
2813 * another chance at the next iteration
2814 */
2815 complete_all(&mcs_compl->completion);
2816 break;
2817 }
2818
2819 mcs_data->completion_bitmap |= BIT(i);
2820 /*
2821 * For all completed CSs we take the earliest timestamp.
2822 * For this we have to validate that the timestamp is
2823 * earliest of all timestamps so far.
2824 */
2825 if (fence && mcs_data->update_ts &&
2826 (ktime_compare(fence->timestamp, first_cs_time) < 0))
2827 first_cs_time = fence->timestamp;
2828 break;
2829 case CS_WAIT_STATUS_GONE:
2830 mcs_data->update_ts = false;
2831 mcs_data->gone_cs = true;
2832 /*
2833 * It is possible to get an old sequence numbers from user
2834 * which related to already completed CSs and their fences
2835 * already gone. In this case, CS set as completed but
2836 * no need to consider its QID for mcs completion.
2837 */
2838 mcs_data->completion_bitmap |= BIT(i);
2839 break;
2840 default:
2841 dev_err(hdev->dev, "Invalid fence status\n");
2842 rc = -EINVAL;
2843 break;
2844 }
2845
2846 }
2847
2848 hl_fences_put(mcs_data->fence_arr, arr_len);
2849
2850 if (mcs_data->update_ts &&
2851 (ktime_compare(first_cs_time, max_ktime) != 0))
2852 mcs_data->timestamp = ktime_to_ns(first_cs_time);
2853
2854 return rc;
2855 }
2856
_hl_cs_wait_ioctl(struct hl_device * hdev,struct hl_ctx * ctx,u64 timeout_us,u64 seq,enum hl_cs_wait_status * status,s64 * timestamp)2857 static int _hl_cs_wait_ioctl(struct hl_device *hdev, struct hl_ctx *ctx, u64 timeout_us, u64 seq,
2858 enum hl_cs_wait_status *status, s64 *timestamp)
2859 {
2860 struct hl_fence *fence;
2861 int rc = 0;
2862
2863 if (timestamp)
2864 *timestamp = 0;
2865
2866 hl_ctx_get(ctx);
2867
2868 fence = hl_ctx_get_fence(ctx, seq);
2869
2870 rc = hl_wait_for_fence(ctx, seq, fence, status, timeout_us, timestamp);
2871 hl_fence_put(fence);
2872 hl_ctx_put(ctx);
2873
2874 return rc;
2875 }
2876
hl_usecs64_to_jiffies(const u64 usecs)2877 static inline unsigned long hl_usecs64_to_jiffies(const u64 usecs)
2878 {
2879 if (usecs <= U32_MAX)
2880 return usecs_to_jiffies(usecs);
2881
2882 /*
2883 * If the value in nanoseconds is larger than 64 bit, use the largest
2884 * 64 bit value.
2885 */
2886 if (usecs >= ((u64)(U64_MAX / NSEC_PER_USEC)))
2887 return nsecs_to_jiffies(U64_MAX);
2888
2889 return nsecs_to_jiffies(usecs * NSEC_PER_USEC);
2890 }
2891
2892 /*
2893 * hl_wait_multi_cs_completion_init - init completion structure
2894 *
2895 * @hdev: pointer to habanalabs device structure
2896 * @stream_master_bitmap: stream master QIDs map, set bit indicates stream
2897 * master QID to wait on
2898 *
2899 * @return valid completion struct pointer on success, otherwise error pointer
2900 *
2901 * up to MULTI_CS_MAX_USER_CTX calls can be done concurrently to the driver.
2902 * the function gets the first available completion (by marking it "used")
2903 * and initialize its values.
2904 */
hl_wait_multi_cs_completion_init(struct hl_device * hdev)2905 static struct multi_cs_completion *hl_wait_multi_cs_completion_init(struct hl_device *hdev)
2906 {
2907 struct multi_cs_completion *mcs_compl;
2908 int i;
2909
2910 /* find free multi_cs completion structure */
2911 for (i = 0; i < MULTI_CS_MAX_USER_CTX; i++) {
2912 mcs_compl = &hdev->multi_cs_completion[i];
2913 spin_lock(&mcs_compl->lock);
2914 if (!mcs_compl->used) {
2915 mcs_compl->used = 1;
2916 mcs_compl->timestamp = 0;
2917 /*
2918 * init QID map to 0 to avoid completion by CSs. the actual QID map
2919 * to multi-CS CSs will be set incrementally at a later stage
2920 */
2921 mcs_compl->stream_master_qid_map = 0;
2922 spin_unlock(&mcs_compl->lock);
2923 break;
2924 }
2925 spin_unlock(&mcs_compl->lock);
2926 }
2927
2928 if (i == MULTI_CS_MAX_USER_CTX) {
2929 dev_err(hdev->dev, "no available multi-CS completion structure\n");
2930 return ERR_PTR(-ENOMEM);
2931 }
2932 return mcs_compl;
2933 }
2934
2935 /*
2936 * hl_wait_multi_cs_completion_fini - return completion structure and set as
2937 * unused
2938 *
2939 * @mcs_compl: pointer to the completion structure
2940 */
hl_wait_multi_cs_completion_fini(struct multi_cs_completion * mcs_compl)2941 static void hl_wait_multi_cs_completion_fini(
2942 struct multi_cs_completion *mcs_compl)
2943 {
2944 /*
2945 * free completion structure, do it under lock to be in-sync with the
2946 * thread that signals completion
2947 */
2948 spin_lock(&mcs_compl->lock);
2949 mcs_compl->used = 0;
2950 spin_unlock(&mcs_compl->lock);
2951 }
2952
2953 /*
2954 * hl_wait_multi_cs_completion - wait for first CS to complete
2955 *
2956 * @mcs_data: multi-CS internal data
2957 *
2958 * @return 0 on success, otherwise non 0 error code
2959 */
hl_wait_multi_cs_completion(struct multi_cs_data * mcs_data,struct multi_cs_completion * mcs_compl)2960 static int hl_wait_multi_cs_completion(struct multi_cs_data *mcs_data,
2961 struct multi_cs_completion *mcs_compl)
2962 {
2963 long completion_rc;
2964
2965 completion_rc = wait_for_completion_interruptible_timeout(&mcs_compl->completion,
2966 mcs_data->timeout_jiffies);
2967
2968 /* update timestamp */
2969 if (completion_rc > 0)
2970 mcs_data->timestamp = mcs_compl->timestamp;
2971
2972 if (completion_rc == -ERESTARTSYS)
2973 return completion_rc;
2974
2975 mcs_data->wait_status = completion_rc;
2976
2977 return 0;
2978 }
2979
2980 /*
2981 * hl_multi_cs_completion_init - init array of multi-CS completion structures
2982 *
2983 * @hdev: pointer to habanalabs device structure
2984 */
hl_multi_cs_completion_init(struct hl_device * hdev)2985 void hl_multi_cs_completion_init(struct hl_device *hdev)
2986 {
2987 struct multi_cs_completion *mcs_cmpl;
2988 int i;
2989
2990 for (i = 0; i < MULTI_CS_MAX_USER_CTX; i++) {
2991 mcs_cmpl = &hdev->multi_cs_completion[i];
2992 mcs_cmpl->used = 0;
2993 spin_lock_init(&mcs_cmpl->lock);
2994 init_completion(&mcs_cmpl->completion);
2995 }
2996 }
2997
2998 /*
2999 * hl_multi_cs_wait_ioctl - implementation of the multi-CS wait ioctl
3000 *
3001 * @hpriv: pointer to the private data of the fd
3002 * @data: pointer to multi-CS wait ioctl in/out args
3003 *
3004 */
hl_multi_cs_wait_ioctl(struct hl_fpriv * hpriv,void * data)3005 static int hl_multi_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data)
3006 {
3007 struct multi_cs_completion *mcs_compl;
3008 struct hl_device *hdev = hpriv->hdev;
3009 struct multi_cs_data mcs_data = {};
3010 union hl_wait_cs_args *args = data;
3011 struct hl_ctx *ctx = hpriv->ctx;
3012 struct hl_fence **fence_arr;
3013 void __user *seq_arr;
3014 u32 size_to_copy;
3015 u64 *cs_seq_arr;
3016 u8 seq_arr_len;
3017 int rc, i;
3018
3019 for (i = 0 ; i < sizeof(args->in.pad) ; i++)
3020 if (args->in.pad[i]) {
3021 dev_dbg(hdev->dev, "Padding bytes must be 0\n");
3022 return -EINVAL;
3023 }
3024
3025 if (!hdev->supports_wait_for_multi_cs) {
3026 dev_err(hdev->dev, "Wait for multi CS is not supported\n");
3027 return -EPERM;
3028 }
3029
3030 seq_arr_len = args->in.seq_arr_len;
3031
3032 if (seq_arr_len > HL_WAIT_MULTI_CS_LIST_MAX_LEN) {
3033 dev_err(hdev->dev, "Can wait only up to %d CSs, input sequence is of length %u\n",
3034 HL_WAIT_MULTI_CS_LIST_MAX_LEN, seq_arr_len);
3035 return -EINVAL;
3036 }
3037
3038 /* allocate memory for sequence array */
3039 cs_seq_arr =
3040 kmalloc_array(seq_arr_len, sizeof(*cs_seq_arr), GFP_KERNEL);
3041 if (!cs_seq_arr)
3042 return -ENOMEM;
3043
3044 /* copy CS sequence array from user */
3045 seq_arr = (void __user *) (uintptr_t) args->in.seq;
3046 size_to_copy = seq_arr_len * sizeof(*cs_seq_arr);
3047 if (copy_from_user(cs_seq_arr, seq_arr, size_to_copy)) {
3048 dev_err(hdev->dev, "Failed to copy multi-cs sequence array from user\n");
3049 rc = -EFAULT;
3050 goto free_seq_arr;
3051 }
3052
3053 /* allocate array for the fences */
3054 fence_arr = kmalloc_objs(struct hl_fence *, seq_arr_len);
3055 if (!fence_arr) {
3056 rc = -ENOMEM;
3057 goto free_seq_arr;
3058 }
3059
3060 /* initialize the multi-CS internal data */
3061 mcs_data.ctx = ctx;
3062 mcs_data.seq_arr = cs_seq_arr;
3063 mcs_data.fence_arr = fence_arr;
3064 mcs_data.arr_len = seq_arr_len;
3065
3066 hl_ctx_get(ctx);
3067
3068 /* wait (with timeout) for the first CS to be completed */
3069 mcs_data.timeout_jiffies = hl_usecs64_to_jiffies(args->in.timeout_us);
3070 mcs_compl = hl_wait_multi_cs_completion_init(hdev);
3071 if (IS_ERR(mcs_compl)) {
3072 rc = PTR_ERR(mcs_compl);
3073 goto put_ctx;
3074 }
3075
3076 /* poll all CS fences, extract timestamp */
3077 mcs_data.update_ts = true;
3078 rc = hl_cs_poll_fences(&mcs_data, mcs_compl);
3079 /*
3080 * skip wait for CS completion when one of the below is true:
3081 * - an error on the poll function
3082 * - one or more CS in the list completed
3083 * - the user called ioctl with timeout 0
3084 */
3085 if (rc || mcs_data.completion_bitmap || !args->in.timeout_us)
3086 goto completion_fini;
3087
3088 while (true) {
3089 rc = hl_wait_multi_cs_completion(&mcs_data, mcs_compl);
3090 if (rc || (mcs_data.wait_status == 0))
3091 break;
3092
3093 /*
3094 * poll fences once again to update the CS map.
3095 * no timestamp should be updated this time.
3096 */
3097 mcs_data.update_ts = false;
3098 rc = hl_cs_poll_fences(&mcs_data, mcs_compl);
3099
3100 if (rc || mcs_data.completion_bitmap)
3101 break;
3102
3103 /*
3104 * if hl_wait_multi_cs_completion returned before timeout (i.e.
3105 * it got a completion) it either got completed by CS in the multi CS list
3106 * (in which case the indication will be non empty completion_bitmap) or it
3107 * got completed by CS submitted to one of the shared stream master but
3108 * not in the multi CS list (in which case we should wait again but modify
3109 * the timeout and set timestamp as zero to let a CS related to the current
3110 * multi-CS set a new, relevant, timestamp)
3111 */
3112 mcs_data.timeout_jiffies = mcs_data.wait_status;
3113 mcs_compl->timestamp = 0;
3114 }
3115
3116 completion_fini:
3117 hl_wait_multi_cs_completion_fini(mcs_compl);
3118
3119 put_ctx:
3120 hl_ctx_put(ctx);
3121 kfree(fence_arr);
3122
3123 free_seq_arr:
3124 kfree(cs_seq_arr);
3125
3126 if (rc == -ERESTARTSYS) {
3127 dev_err_ratelimited(hdev->dev,
3128 "user process got signal while waiting for Multi-CS\n");
3129 rc = -EINTR;
3130 }
3131
3132 if (rc)
3133 return rc;
3134
3135 /* update output args */
3136 memset(args, 0, sizeof(*args));
3137
3138 if (mcs_data.completion_bitmap) {
3139 args->out.status = HL_WAIT_CS_STATUS_COMPLETED;
3140 args->out.cs_completion_map = mcs_data.completion_bitmap;
3141
3142 /* if timestamp not 0- it's valid */
3143 if (mcs_data.timestamp) {
3144 args->out.timestamp_nsec = mcs_data.timestamp;
3145 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_TIMESTAMP_VLD;
3146 }
3147
3148 /* update if some CS was gone */
3149 if (!mcs_data.timestamp)
3150 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_GONE;
3151 } else {
3152 args->out.status = HL_WAIT_CS_STATUS_BUSY;
3153 }
3154
3155 return 0;
3156 }
3157
hl_cs_wait_ioctl(struct hl_fpriv * hpriv,void * data)3158 static int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data)
3159 {
3160 struct hl_device *hdev = hpriv->hdev;
3161 union hl_wait_cs_args *args = data;
3162 enum hl_cs_wait_status status;
3163 u64 seq = args->in.seq;
3164 s64 timestamp;
3165 int rc;
3166
3167 rc = _hl_cs_wait_ioctl(hdev, hpriv->ctx, args->in.timeout_us, seq, &status, ×tamp);
3168
3169 if (rc == -ERESTARTSYS) {
3170 dev_err_ratelimited(hdev->dev,
3171 "user process got signal while waiting for CS handle %llu\n",
3172 seq);
3173 return -EINTR;
3174 }
3175
3176 memset(args, 0, sizeof(*args));
3177
3178 if (rc) {
3179 if (rc == -ETIMEDOUT) {
3180 dev_err_ratelimited(hdev->dev,
3181 "CS %llu has timed-out while user process is waiting for it\n",
3182 seq);
3183 args->out.status = HL_WAIT_CS_STATUS_TIMEDOUT;
3184 } else if (rc == -EIO) {
3185 dev_err_ratelimited(hdev->dev,
3186 "CS %llu has been aborted while user process is waiting for it\n",
3187 seq);
3188 args->out.status = HL_WAIT_CS_STATUS_ABORTED;
3189 }
3190 return rc;
3191 }
3192
3193 if (timestamp) {
3194 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_TIMESTAMP_VLD;
3195 args->out.timestamp_nsec = timestamp;
3196 }
3197
3198 switch (status) {
3199 case CS_WAIT_STATUS_GONE:
3200 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_GONE;
3201 fallthrough;
3202 case CS_WAIT_STATUS_COMPLETED:
3203 args->out.status = HL_WAIT_CS_STATUS_COMPLETED;
3204 break;
3205 case CS_WAIT_STATUS_BUSY:
3206 default:
3207 args->out.status = HL_WAIT_CS_STATUS_BUSY;
3208 break;
3209 }
3210
3211 return 0;
3212 }
3213
set_record_cq_info(struct hl_user_pending_interrupt * record,struct hl_cb * cq_cb,u32 cq_offset,u32 target_value)3214 static inline void set_record_cq_info(struct hl_user_pending_interrupt *record,
3215 struct hl_cb *cq_cb, u32 cq_offset, u32 target_value)
3216 {
3217 record->ts_reg_info.cq_cb = cq_cb;
3218 record->cq_kernel_addr = (u64 *) cq_cb->kernel_address + cq_offset;
3219 record->cq_target_value = target_value;
3220 }
3221
validate_and_get_ts_record(struct device * dev,struct hl_ts_buff * ts_buff,u64 ts_offset,struct hl_user_pending_interrupt ** req_event_record)3222 static int validate_and_get_ts_record(struct device *dev,
3223 struct hl_ts_buff *ts_buff, u64 ts_offset,
3224 struct hl_user_pending_interrupt **req_event_record)
3225 {
3226 struct hl_user_pending_interrupt *ts_cb_last;
3227
3228 *req_event_record = (struct hl_user_pending_interrupt *)ts_buff->kernel_buff_address +
3229 ts_offset;
3230 ts_cb_last = (struct hl_user_pending_interrupt *)ts_buff->kernel_buff_address +
3231 (ts_buff->kernel_buff_size / sizeof(struct hl_user_pending_interrupt));
3232
3233 /* Validate ts_offset not exceeding last max */
3234 if (*req_event_record >= ts_cb_last) {
3235 dev_err(dev, "Ts offset(%llu) exceeds max CB offset(0x%llx)\n",
3236 ts_offset, (u64)(uintptr_t)ts_cb_last);
3237 return -EINVAL;
3238 }
3239
3240 return 0;
3241 }
3242
unregister_timestamp_node(struct hl_device * hdev,struct hl_user_pending_interrupt * record,bool need_lock)3243 static void unregister_timestamp_node(struct hl_device *hdev,
3244 struct hl_user_pending_interrupt *record, bool need_lock)
3245 {
3246 struct hl_user_interrupt *interrupt = record->ts_reg_info.interrupt;
3247 bool ts_rec_found = false;
3248 unsigned long flags;
3249
3250 if (need_lock)
3251 spin_lock_irqsave(&interrupt->ts_list_lock, flags);
3252
3253 if (record->ts_reg_info.in_use) {
3254 record->ts_reg_info.in_use = false;
3255 list_del(&record->list_node);
3256 ts_rec_found = true;
3257 }
3258
3259 if (need_lock)
3260 spin_unlock_irqrestore(&interrupt->ts_list_lock, flags);
3261
3262 /* Put refcounts that were taken when we registered the event */
3263 if (ts_rec_found) {
3264 hl_mmap_mem_buf_put(record->ts_reg_info.buf);
3265 hl_cb_put(record->ts_reg_info.cq_cb);
3266 }
3267 }
3268
ts_get_and_handle_kernel_record(struct hl_device * hdev,struct hl_ctx * ctx,struct wait_interrupt_data * data,unsigned long * flags,struct hl_user_pending_interrupt ** pend)3269 static int ts_get_and_handle_kernel_record(struct hl_device *hdev, struct hl_ctx *ctx,
3270 struct wait_interrupt_data *data, unsigned long *flags,
3271 struct hl_user_pending_interrupt **pend)
3272 {
3273 struct hl_user_pending_interrupt *req_offset_record;
3274 struct hl_ts_buff *ts_buff = data->buf->private;
3275 bool need_lock = false;
3276 int rc;
3277
3278 rc = validate_and_get_ts_record(data->buf->mmg->dev, ts_buff, data->ts_offset,
3279 &req_offset_record);
3280 if (rc)
3281 return rc;
3282
3283 /* In case the node already registered, need to unregister first then re-use */
3284 if (req_offset_record->ts_reg_info.in_use) {
3285 /*
3286 * Since interrupt here can be different than the one the node currently registered
3287 * on, and we don't want to lock two lists while we're doing unregister, so
3288 * unlock the new interrupt wait list here and acquire the lock again after you done
3289 */
3290 if (data->interrupt->interrupt_id !=
3291 req_offset_record->ts_reg_info.interrupt->interrupt_id) {
3292
3293 need_lock = true;
3294 spin_unlock_irqrestore(&data->interrupt->ts_list_lock, *flags);
3295 }
3296
3297 unregister_timestamp_node(hdev, req_offset_record, need_lock);
3298
3299 if (need_lock)
3300 spin_lock_irqsave(&data->interrupt->ts_list_lock, *flags);
3301 }
3302
3303 /* Fill up the new registration node info and add it to the list */
3304 req_offset_record->ts_reg_info.in_use = true;
3305 req_offset_record->ts_reg_info.buf = data->buf;
3306 req_offset_record->ts_reg_info.timestamp_kernel_addr =
3307 (u64 *) ts_buff->user_buff_address + data->ts_offset;
3308 req_offset_record->ts_reg_info.interrupt = data->interrupt;
3309 set_record_cq_info(req_offset_record, data->cq_cb, data->cq_offset,
3310 data->target_value);
3311
3312 *pend = req_offset_record;
3313
3314 return rc;
3315 }
3316
_hl_interrupt_ts_reg_ioctl(struct hl_device * hdev,struct hl_ctx * ctx,struct wait_interrupt_data * data,u32 * status,u64 * timestamp)3317 static int _hl_interrupt_ts_reg_ioctl(struct hl_device *hdev, struct hl_ctx *ctx,
3318 struct wait_interrupt_data *data,
3319 u32 *status, u64 *timestamp)
3320 {
3321 struct hl_user_pending_interrupt *pend;
3322 unsigned long flags;
3323 int rc = 0;
3324
3325 hl_ctx_get(ctx);
3326
3327 data->cq_cb = hl_cb_get(data->mmg, data->cq_handle);
3328 if (!data->cq_cb) {
3329 rc = -EINVAL;
3330 goto put_ctx;
3331 }
3332
3333 /* Validate the cq offset */
3334 if (((u64 *) data->cq_cb->kernel_address + data->cq_offset) >=
3335 ((u64 *) data->cq_cb->kernel_address + (data->cq_cb->size / sizeof(u64)))) {
3336 rc = -EINVAL;
3337 goto put_cq_cb;
3338 }
3339
3340 data->buf = hl_mmap_mem_buf_get(data->mmg, data->ts_handle);
3341 if (!data->buf) {
3342 rc = -EINVAL;
3343 goto put_cq_cb;
3344 }
3345
3346 spin_lock_irqsave(&data->interrupt->ts_list_lock, flags);
3347
3348 /* get ts buffer record */
3349 rc = ts_get_and_handle_kernel_record(hdev, ctx, data, &flags, &pend);
3350 if (rc) {
3351 spin_unlock_irqrestore(&data->interrupt->ts_list_lock, flags);
3352 goto put_ts_buff;
3353 }
3354
3355 /* We check for completion value as interrupt could have been received
3356 * before we add the timestamp node to the ts list.
3357 */
3358 if (*pend->cq_kernel_addr >= data->target_value) {
3359 spin_unlock_irqrestore(&data->interrupt->ts_list_lock, flags);
3360
3361 pend->ts_reg_info.in_use = 0;
3362 *status = HL_WAIT_CS_STATUS_COMPLETED;
3363 *pend->ts_reg_info.timestamp_kernel_addr = ktime_get_ns();
3364
3365 goto put_ts_buff;
3366 }
3367
3368 list_add_tail(&pend->list_node, &data->interrupt->ts_list_head);
3369 spin_unlock_irqrestore(&data->interrupt->ts_list_lock, flags);
3370
3371 rc = *status = HL_WAIT_CS_STATUS_COMPLETED;
3372
3373 hl_ctx_put(ctx);
3374
3375 return rc;
3376
3377 put_ts_buff:
3378 hl_mmap_mem_buf_put(data->buf);
3379 put_cq_cb:
3380 hl_cb_put(data->cq_cb);
3381 put_ctx:
3382 hl_ctx_put(ctx);
3383
3384 return rc;
3385 }
3386
_hl_interrupt_wait_ioctl(struct hl_device * hdev,struct hl_ctx * ctx,struct wait_interrupt_data * data,u32 * status,u64 * timestamp)3387 static int _hl_interrupt_wait_ioctl(struct hl_device *hdev, struct hl_ctx *ctx,
3388 struct wait_interrupt_data *data,
3389 u32 *status, u64 *timestamp)
3390 {
3391 struct hl_user_pending_interrupt *pend;
3392 unsigned long timeout, flags;
3393 long completion_rc;
3394 int rc = 0;
3395
3396 timeout = hl_usecs64_to_jiffies(data->intr_timeout_us);
3397
3398 hl_ctx_get(ctx);
3399
3400 data->cq_cb = hl_cb_get(data->mmg, data->cq_handle);
3401 if (!data->cq_cb) {
3402 rc = -EINVAL;
3403 goto put_ctx;
3404 }
3405
3406 /* Validate the cq offset */
3407 if (((u64 *) data->cq_cb->kernel_address + data->cq_offset) >=
3408 ((u64 *) data->cq_cb->kernel_address + (data->cq_cb->size / sizeof(u64)))) {
3409 rc = -EINVAL;
3410 goto put_cq_cb;
3411 }
3412
3413 pend = kzalloc_obj(*pend);
3414 if (!pend) {
3415 rc = -ENOMEM;
3416 goto put_cq_cb;
3417 }
3418
3419 hl_fence_init(&pend->fence, ULONG_MAX);
3420 pend->cq_kernel_addr = (u64 *) data->cq_cb->kernel_address + data->cq_offset;
3421 pend->cq_target_value = data->target_value;
3422 spin_lock_irqsave(&data->interrupt->wait_list_lock, flags);
3423
3424
3425 /* We check for completion value as interrupt could have been received
3426 * before we add the wait node to the wait list.
3427 */
3428 if (*pend->cq_kernel_addr >= data->target_value || (!data->intr_timeout_us)) {
3429 spin_unlock_irqrestore(&data->interrupt->wait_list_lock, flags);
3430
3431 if (*pend->cq_kernel_addr >= data->target_value)
3432 *status = HL_WAIT_CS_STATUS_COMPLETED;
3433 else
3434 *status = HL_WAIT_CS_STATUS_BUSY;
3435
3436 pend->fence.timestamp = ktime_get();
3437 goto set_timestamp;
3438 }
3439
3440 /* Add pending user interrupt to relevant list for the interrupt
3441 * handler to monitor.
3442 * Note that we cannot have sorted list by target value,
3443 * in order to shorten the list pass loop, since
3444 * same list could have nodes for different cq counter handle.
3445 */
3446 list_add_tail(&pend->list_node, &data->interrupt->wait_list_head);
3447 spin_unlock_irqrestore(&data->interrupt->wait_list_lock, flags);
3448
3449 /* Wait for interrupt handler to signal completion */
3450 completion_rc = wait_for_completion_interruptible_timeout(&pend->fence.completion,
3451 timeout);
3452 if (completion_rc > 0) {
3453 if (pend->fence.error == -EIO) {
3454 dev_err_ratelimited(hdev->dev,
3455 "interrupt based wait ioctl aborted(error:%d) due to a reset cycle initiated\n",
3456 pend->fence.error);
3457 rc = -EIO;
3458 *status = HL_WAIT_CS_STATUS_ABORTED;
3459 } else {
3460 *status = HL_WAIT_CS_STATUS_COMPLETED;
3461 }
3462 } else {
3463 if (completion_rc == -ERESTARTSYS) {
3464 dev_err_ratelimited(hdev->dev,
3465 "user process got signal while waiting for interrupt ID %d\n",
3466 data->interrupt->interrupt_id);
3467 rc = -EINTR;
3468 *status = HL_WAIT_CS_STATUS_ABORTED;
3469 } else {
3470 /* The wait has timed-out. We don't know anything beyond that
3471 * because the workload was not submitted through the driver.
3472 * Therefore, from driver's perspective, the workload is still
3473 * executing.
3474 */
3475 rc = 0;
3476 *status = HL_WAIT_CS_STATUS_BUSY;
3477 }
3478 }
3479
3480 /*
3481 * We keep removing the node from list here, and not at the irq handler
3482 * for completion timeout case. and if it's a registration
3483 * for ts record, the node will be deleted in the irq handler after
3484 * we reach the target value.
3485 */
3486 spin_lock_irqsave(&data->interrupt->wait_list_lock, flags);
3487 list_del(&pend->list_node);
3488 spin_unlock_irqrestore(&data->interrupt->wait_list_lock, flags);
3489
3490 set_timestamp:
3491 *timestamp = ktime_to_ns(pend->fence.timestamp);
3492 kfree(pend);
3493 hl_cb_put(data->cq_cb);
3494 hl_ctx_put(ctx);
3495
3496 return rc;
3497
3498 put_cq_cb:
3499 hl_cb_put(data->cq_cb);
3500 put_ctx:
3501 hl_ctx_put(ctx);
3502
3503 return rc;
3504 }
3505
_hl_interrupt_wait_ioctl_user_addr(struct hl_device * hdev,struct hl_ctx * ctx,u64 timeout_us,u64 user_address,u64 target_value,struct hl_user_interrupt * interrupt,u32 * status,u64 * timestamp)3506 static int _hl_interrupt_wait_ioctl_user_addr(struct hl_device *hdev, struct hl_ctx *ctx,
3507 u64 timeout_us, u64 user_address,
3508 u64 target_value, struct hl_user_interrupt *interrupt,
3509 u32 *status,
3510 u64 *timestamp)
3511 {
3512 struct hl_user_pending_interrupt *pend;
3513 unsigned long timeout, flags;
3514 u64 completion_value;
3515 long completion_rc;
3516 int rc = 0;
3517
3518 timeout = hl_usecs64_to_jiffies(timeout_us);
3519
3520 hl_ctx_get(ctx);
3521
3522 pend = kzalloc_obj(*pend);
3523 if (!pend) {
3524 hl_ctx_put(ctx);
3525 return -ENOMEM;
3526 }
3527
3528 hl_fence_init(&pend->fence, ULONG_MAX);
3529
3530 /* Add pending user interrupt to relevant list for the interrupt
3531 * handler to monitor
3532 */
3533 spin_lock_irqsave(&interrupt->wait_list_lock, flags);
3534 list_add_tail(&pend->list_node, &interrupt->wait_list_head);
3535 spin_unlock_irqrestore(&interrupt->wait_list_lock, flags);
3536
3537 /* We check for completion value as interrupt could have been received
3538 * before we added the node to the wait list
3539 */
3540 if (copy_from_user(&completion_value, u64_to_user_ptr(user_address), 8)) {
3541 dev_err(hdev->dev, "Failed to copy completion value from user\n");
3542 rc = -EFAULT;
3543 goto remove_pending_user_interrupt;
3544 }
3545
3546 if (completion_value >= target_value) {
3547 *status = HL_WAIT_CS_STATUS_COMPLETED;
3548 /* There was no interrupt, we assume the completion is now. */
3549 pend->fence.timestamp = ktime_get();
3550 } else {
3551 *status = HL_WAIT_CS_STATUS_BUSY;
3552 }
3553
3554 if (!timeout_us || (*status == HL_WAIT_CS_STATUS_COMPLETED))
3555 goto remove_pending_user_interrupt;
3556
3557 wait_again:
3558 /* Wait for interrupt handler to signal completion */
3559 completion_rc = wait_for_completion_interruptible_timeout(&pend->fence.completion,
3560 timeout);
3561
3562 /* If timeout did not expire we need to perform the comparison.
3563 * If comparison fails, keep waiting until timeout expires
3564 */
3565 if (completion_rc > 0) {
3566 spin_lock_irqsave(&interrupt->wait_list_lock, flags);
3567 /* reinit_completion must be called before we check for user
3568 * completion value, otherwise, if interrupt is received after
3569 * the comparison and before the next wait_for_completion,
3570 * we will reach timeout and fail
3571 */
3572 reinit_completion(&pend->fence.completion);
3573 spin_unlock_irqrestore(&interrupt->wait_list_lock, flags);
3574
3575 if (copy_from_user(&completion_value, u64_to_user_ptr(user_address), 8)) {
3576 dev_err(hdev->dev, "Failed to copy completion value from user\n");
3577 rc = -EFAULT;
3578
3579 goto remove_pending_user_interrupt;
3580 }
3581
3582 if (completion_value >= target_value) {
3583 *status = HL_WAIT_CS_STATUS_COMPLETED;
3584 } else if (pend->fence.error) {
3585 dev_err_ratelimited(hdev->dev,
3586 "interrupt based wait ioctl aborted(error:%d) due to a reset cycle initiated\n",
3587 pend->fence.error);
3588 /* set the command completion status as ABORTED */
3589 *status = HL_WAIT_CS_STATUS_ABORTED;
3590 } else {
3591 timeout = completion_rc;
3592 goto wait_again;
3593 }
3594 } else if (completion_rc == -ERESTARTSYS) {
3595 dev_err_ratelimited(hdev->dev,
3596 "user process got signal while waiting for interrupt ID %d\n",
3597 interrupt->interrupt_id);
3598 rc = -EINTR;
3599 } else {
3600 /* The wait has timed-out. We don't know anything beyond that
3601 * because the workload wasn't submitted through the driver.
3602 * Therefore, from driver's perspective, the workload is still
3603 * executing.
3604 */
3605 rc = 0;
3606 *status = HL_WAIT_CS_STATUS_BUSY;
3607 }
3608
3609 remove_pending_user_interrupt:
3610 spin_lock_irqsave(&interrupt->wait_list_lock, flags);
3611 list_del(&pend->list_node);
3612 spin_unlock_irqrestore(&interrupt->wait_list_lock, flags);
3613
3614 *timestamp = ktime_to_ns(pend->fence.timestamp);
3615
3616 kfree(pend);
3617 hl_ctx_put(ctx);
3618
3619 return rc;
3620 }
3621
hl_interrupt_wait_ioctl(struct hl_fpriv * hpriv,void * data)3622 static int hl_interrupt_wait_ioctl(struct hl_fpriv *hpriv, void *data)
3623 {
3624 u16 interrupt_id, first_interrupt, last_interrupt;
3625 struct hl_device *hdev = hpriv->hdev;
3626 struct asic_fixed_properties *prop;
3627 struct hl_user_interrupt *interrupt;
3628 union hl_wait_cs_args *args = data;
3629 u32 status = HL_WAIT_CS_STATUS_BUSY;
3630 u64 timestamp = 0;
3631 int rc, int_idx;
3632
3633 prop = &hdev->asic_prop;
3634
3635 if (!(prop->user_interrupt_count + prop->user_dec_intr_count)) {
3636 dev_err(hdev->dev, "no user interrupts allowed");
3637 return -EPERM;
3638 }
3639
3640 interrupt_id = FIELD_GET(HL_WAIT_CS_FLAGS_INTERRUPT_MASK, args->in.flags);
3641
3642 first_interrupt = prop->first_available_user_interrupt;
3643 last_interrupt = prop->first_available_user_interrupt + prop->user_interrupt_count - 1;
3644
3645 if (interrupt_id < prop->user_dec_intr_count) {
3646
3647 /* Check if the requested core is enabled */
3648 if (!(prop->decoder_enabled_mask & BIT(interrupt_id))) {
3649 dev_err(hdev->dev, "interrupt on a disabled core(%u) not allowed",
3650 interrupt_id);
3651 return -EINVAL;
3652 }
3653
3654 interrupt = &hdev->user_interrupt[interrupt_id];
3655
3656 } else if (interrupt_id >= first_interrupt && interrupt_id <= last_interrupt) {
3657
3658 int_idx = interrupt_id - first_interrupt + prop->user_dec_intr_count;
3659 interrupt = &hdev->user_interrupt[int_idx];
3660
3661 } else if (interrupt_id == HL_COMMON_USER_CQ_INTERRUPT_ID) {
3662 interrupt = &hdev->common_user_cq_interrupt;
3663 } else if (interrupt_id == HL_COMMON_DEC_INTERRUPT_ID) {
3664 interrupt = &hdev->common_decoder_interrupt;
3665 } else {
3666 dev_err(hdev->dev, "invalid user interrupt %u", interrupt_id);
3667 return -EINVAL;
3668 }
3669
3670 if (args->in.flags & HL_WAIT_CS_FLAGS_INTERRUPT_KERNEL_CQ) {
3671 struct wait_interrupt_data wait_intr_data = {0};
3672
3673 wait_intr_data.interrupt = interrupt;
3674 wait_intr_data.mmg = &hpriv->mem_mgr;
3675 wait_intr_data.cq_handle = args->in.cq_counters_handle;
3676 wait_intr_data.cq_offset = args->in.cq_counters_offset;
3677 wait_intr_data.ts_handle = args->in.timestamp_handle;
3678 wait_intr_data.ts_offset = args->in.timestamp_offset;
3679 wait_intr_data.target_value = args->in.target;
3680 wait_intr_data.intr_timeout_us = args->in.interrupt_timeout_us;
3681
3682 if (args->in.flags & HL_WAIT_CS_FLAGS_REGISTER_INTERRUPT) {
3683 /*
3684 * Allow only one registration at a time. this is needed in order to prevent
3685 * issues while handling the flow of re-use of the same offset.
3686 * Since the registration flow is protected only by the interrupt lock,
3687 * re-use flow might request to move ts node to another interrupt list,
3688 * and in such case we're not protected.
3689 */
3690 mutex_lock(&hpriv->ctx->ts_reg_lock);
3691
3692 rc = _hl_interrupt_ts_reg_ioctl(hdev, hpriv->ctx, &wait_intr_data,
3693 &status, ×tamp);
3694
3695 mutex_unlock(&hpriv->ctx->ts_reg_lock);
3696 } else
3697 rc = _hl_interrupt_wait_ioctl(hdev, hpriv->ctx, &wait_intr_data,
3698 &status, ×tamp);
3699 } else {
3700 rc = _hl_interrupt_wait_ioctl_user_addr(hdev, hpriv->ctx,
3701 args->in.interrupt_timeout_us, args->in.addr,
3702 args->in.target, interrupt, &status,
3703 ×tamp);
3704 }
3705
3706 if (rc)
3707 return rc;
3708
3709 memset(args, 0, sizeof(*args));
3710 args->out.status = status;
3711
3712 if (timestamp) {
3713 args->out.timestamp_nsec = timestamp;
3714 args->out.flags |= HL_WAIT_CS_STATUS_FLAG_TIMESTAMP_VLD;
3715 }
3716
3717 return 0;
3718 }
3719
hl_wait_ioctl(struct drm_device * ddev,void * data,struct drm_file * file_priv)3720 int hl_wait_ioctl(struct drm_device *ddev, void *data, struct drm_file *file_priv)
3721 {
3722 struct hl_fpriv *hpriv = file_priv->driver_priv;
3723 struct hl_device *hdev = hpriv->hdev;
3724 union hl_wait_cs_args *args = data;
3725 u32 flags = args->in.flags;
3726 int rc;
3727
3728 /* If the device is not operational, or if an error has happened and user should release the
3729 * device, there is no point in waiting for any command submission or user interrupt.
3730 */
3731 if (!hl_device_operational(hpriv->hdev, NULL) || hdev->reset_info.watchdog_active)
3732 return -EBUSY;
3733
3734 if (flags & HL_WAIT_CS_FLAGS_INTERRUPT)
3735 rc = hl_interrupt_wait_ioctl(hpriv, data);
3736 else if (flags & HL_WAIT_CS_FLAGS_MULTI_CS)
3737 rc = hl_multi_cs_wait_ioctl(hpriv, data);
3738 else
3739 rc = hl_cs_wait_ioctl(hpriv, data);
3740
3741 return rc;
3742 }
3743