This source file includes following definitions.
- hl_mem_area_inside_range
- hl_mem_area_crosses_range
- hl_debugfs_init
- hl_debugfs_fini
- hl_debugfs_add_device
- hl_debugfs_remove_device
- hl_debugfs_add_file
- hl_debugfs_remove_file
- hl_debugfs_add_cb
- hl_debugfs_remove_cb
- hl_debugfs_add_cs
- hl_debugfs_remove_cs
- hl_debugfs_add_job
- hl_debugfs_remove_job
- hl_debugfs_add_userptr
- hl_debugfs_remove_userptr
- hl_debugfs_add_ctx_mem_hash
- hl_debugfs_remove_ctx_mem_hash
1
2
3
4
5
6
7
8 #ifndef HABANALABSP_H_
9 #define HABANALABSP_H_
10
11 #include "include/armcp_if.h"
12 #include "include/qman_if.h"
13
14 #include <linux/cdev.h>
15 #include <linux/iopoll.h>
16 #include <linux/irqreturn.h>
17 #include <linux/dma-fence.h>
18 #include <linux/dma-direction.h>
19 #include <linux/scatterlist.h>
20 #include <linux/hashtable.h>
21
22 #define HL_NAME "habanalabs"
23
24 #define HL_MMAP_CB_MASK (0x8000000000000000ull >> PAGE_SHIFT)
25
26 #define HL_PENDING_RESET_PER_SEC 5
27
28 #define HL_DEVICE_TIMEOUT_USEC 1000000
29
30 #define HL_HEARTBEAT_PER_USEC 5000000
31
32 #define HL_PLL_LOW_JOB_FREQ_USEC 5000000
33
34 #define HL_ARMCP_INFO_TIMEOUT_USEC 10000000
35 #define HL_ARMCP_EEPROM_TIMEOUT_USEC 10000000
36
37 #define HL_PCI_ELBI_TIMEOUT_MSEC 10
38
39 #define HL_SIM_MAX_TIMEOUT_US 10000000
40
41 #define HL_MAX_QUEUES 128
42
43 #define HL_MAX_JOBS_PER_CS 64
44
45
46 #define HL_MAX_PENDING_CS 64
47
48 #define HL_IDLE_BUSY_TS_ARR_SIZE 4096
49
50
51 #define MEM_HASH_TABLE_BITS 7
52
53
54 #define MMU_HASH_TABLE_BITS 7
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 struct pgt_info {
70 struct hlist_node node;
71 u64 phys_addr;
72 u64 shadow_addr;
73 struct hl_ctx *ctx;
74 int num_of_ptes;
75 };
76
77 struct hl_device;
78 struct hl_fpriv;
79
80
81
82
83
84
85
86
87
88
89 enum hl_queue_type {
90 QUEUE_TYPE_NA,
91 QUEUE_TYPE_EXT,
92 QUEUE_TYPE_INT,
93 QUEUE_TYPE_CPU
94 };
95
96
97
98
99
100
101
102 struct hw_queue_properties {
103 enum hl_queue_type type;
104 u8 driver_only;
105 };
106
107
108
109
110
111
112 enum vm_type_t {
113 VM_TYPE_USERPTR,
114 VM_TYPE_PHYS_PACK
115 };
116
117
118
119
120
121
122
123
124 enum hl_device_hw_state {
125 HL_DEVICE_HW_STATE_CLEAN = 0,
126 HL_DEVICE_HW_STATE_DIRTY
127 };
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178 struct asic_fixed_properties {
179 struct hw_queue_properties hw_queues_props[HL_MAX_QUEUES];
180 struct armcp_info armcp_info;
181 char uboot_ver[VERSION_MAX_LEN];
182 char preboot_ver[VERSION_MAX_LEN];
183 u64 sram_base_address;
184 u64 sram_end_address;
185 u64 sram_user_base_address;
186 u64 dram_base_address;
187 u64 dram_end_address;
188 u64 dram_user_base_address;
189 u64 dram_size;
190 u64 dram_pci_bar_size;
191 u64 max_power_default;
192 u64 va_space_host_start_address;
193 u64 va_space_host_end_address;
194 u64 va_space_dram_start_address;
195 u64 va_space_dram_end_address;
196 u64 dram_size_for_default_page_mapping;
197 u64 pcie_dbi_base_address;
198 u64 pcie_aux_dbi_reg_addr;
199 u64 mmu_pgt_addr;
200 u64 mmu_dram_default_page_addr;
201 u32 mmu_pgt_size;
202 u32 mmu_pte_size;
203 u32 mmu_hop_table_size;
204 u32 mmu_hop0_tables_total_size;
205 u32 dram_page_size;
206 u32 cfg_size;
207 u32 sram_size;
208 u32 max_asid;
209 u32 num_of_events;
210 u32 psoc_pci_pll_nr;
211 u32 psoc_pci_pll_nf;
212 u32 psoc_pci_pll_od;
213 u32 psoc_pci_pll_div_factor;
214 u32 high_pll;
215 u32 cb_pool_cb_cnt;
216 u32 cb_pool_cb_size;
217 u8 completion_queues_count;
218 u8 tpc_enabled_mask;
219 };
220
221
222
223
224
225
226
227
228 struct hl_dma_fence {
229 struct dma_fence base_fence;
230 spinlock_t lock;
231 struct hl_device *hdev;
232 u64 cs_seq;
233 };
234
235
236
237
238
239 #define HL_MAX_CB_SIZE 0x200000
240
241
242
243
244
245
246 struct hl_cb_mgr {
247 spinlock_t cb_lock;
248 struct idr cb_handles;
249 };
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 struct hl_cb {
269 struct kref refcount;
270 struct hl_device *hdev;
271 spinlock_t lock;
272 struct list_head debugfs_list;
273 struct list_head pool_list;
274 u64 kernel_address;
275 dma_addr_t bus_address;
276 u32 mmap_size;
277 u32 size;
278 u32 id;
279 u32 cs_cnt;
280 u32 ctx_id;
281 u8 mmap;
282 u8 is_pool;
283 };
284
285
286
287
288
289
290 struct hl_cs_job;
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 #define HL_PAGE_SIZE 4096
312
313 #define HL_QUEUE_LENGTH 256
314 #define HL_QUEUE_SIZE_IN_BYTES (HL_QUEUE_LENGTH * HL_BD_SIZE)
315
316
317
318
319
320 #define HL_CQ_LENGTH HL_QUEUE_LENGTH
321 #define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)
322
323
324 #define HL_EQ_LENGTH 64
325 #define HL_EQ_SIZE_IN_BYTES (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
326
327
328 #define HL_CPU_ACCESSIBLE_MEM_SIZE SZ_2M
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 struct hl_hw_queue {
344 struct hl_cs_job **shadow_queue;
345 enum hl_queue_type queue_type;
346 u64 kernel_address;
347 dma_addr_t bus_address;
348 u32 pi;
349 u32 ci;
350 u32 hw_queue_id;
351 u16 int_queue_len;
352 u8 valid;
353 };
354
355
356
357
358
359
360
361
362
363
364
365 struct hl_cq {
366 struct hl_device *hdev;
367 u64 kernel_address;
368 dma_addr_t bus_address;
369 u32 hw_queue_id;
370 u32 ci;
371 u32 pi;
372 atomic_t free_slots_cnt;
373 };
374
375
376
377
378
379
380
381
382 struct hl_eq {
383 struct hl_device *hdev;
384 u64 kernel_address;
385 dma_addr_t bus_address;
386 u32 ci;
387 };
388
389
390
391
392
393
394
395
396
397
398
399 enum hl_asic_type {
400 ASIC_INVALID,
401 ASIC_GOYA
402 };
403
404 struct hl_cs_parser;
405
406
407
408
409
410
411
412 enum hl_pm_mng_profile {
413 PM_AUTO = 1,
414 PM_MANUAL,
415 PM_LAST
416 };
417
418
419
420
421
422
423
424 enum hl_pll_frequency {
425 PLL_HIGH = 1,
426 PLL_LOW,
427 PLL_LAST
428 };
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 struct hl_asic_funcs {
507 int (*early_init)(struct hl_device *hdev);
508 int (*early_fini)(struct hl_device *hdev);
509 int (*late_init)(struct hl_device *hdev);
510 void (*late_fini)(struct hl_device *hdev);
511 int (*sw_init)(struct hl_device *hdev);
512 int (*sw_fini)(struct hl_device *hdev);
513 int (*hw_init)(struct hl_device *hdev);
514 void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
515 void (*halt_engines)(struct hl_device *hdev, bool hard_reset);
516 int (*suspend)(struct hl_device *hdev);
517 int (*resume)(struct hl_device *hdev);
518 int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
519 u64 kaddress, phys_addr_t paddress, u32 size);
520 void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
521 void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
522 struct hl_bd *bd);
523 void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
524 dma_addr_t *dma_handle, gfp_t flag);
525 void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
526 void *cpu_addr, dma_addr_t dma_handle);
527 void* (*get_int_queue_base)(struct hl_device *hdev, u32 queue_id,
528 dma_addr_t *dma_handle, u16 *queue_len);
529 int (*test_queues)(struct hl_device *hdev);
530 void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
531 gfp_t mem_flags, dma_addr_t *dma_handle);
532 void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
533 dma_addr_t dma_addr);
534 void* (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev,
535 size_t size, dma_addr_t *dma_handle);
536 void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,
537 size_t size, void *vaddr);
538 void (*hl_dma_unmap_sg)(struct hl_device *hdev,
539 struct scatterlist *sgl, int nents,
540 enum dma_data_direction dir);
541 int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
542 int (*asic_dma_map_sg)(struct hl_device *hdev,
543 struct scatterlist *sgl, int nents,
544 enum dma_data_direction dir);
545 u32 (*get_dma_desc_list_size)(struct hl_device *hdev,
546 struct sg_table *sgt);
547 void (*add_end_of_cb_packets)(struct hl_device *hdev,
548 u64 kernel_address, u32 len,
549 u64 cq_addr, u32 cq_val, u32 msix_num);
550 void (*update_eq_ci)(struct hl_device *hdev, u32 val);
551 int (*context_switch)(struct hl_device *hdev, u32 asid);
552 void (*restore_phase_topology)(struct hl_device *hdev);
553 int (*debugfs_read32)(struct hl_device *hdev, u64 addr, u32 *val);
554 int (*debugfs_write32)(struct hl_device *hdev, u64 addr, u32 val);
555 void (*add_device_attr)(struct hl_device *hdev,
556 struct attribute_group *dev_attr_grp);
557 void (*handle_eqe)(struct hl_device *hdev,
558 struct hl_eq_entry *eq_entry);
559 void (*set_pll_profile)(struct hl_device *hdev,
560 enum hl_pll_frequency freq);
561 void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
562 u32 *size);
563 u64 (*read_pte)(struct hl_device *hdev, u64 addr);
564 void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
565 void (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard);
566 void (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
567 u32 asid, u64 va, u64 size);
568 int (*send_heartbeat)(struct hl_device *hdev);
569 int (*debug_coresight)(struct hl_device *hdev, void *data);
570 bool (*is_device_idle)(struct hl_device *hdev, u32 *mask,
571 struct seq_file *s);
572 int (*soft_reset_late_init)(struct hl_device *hdev);
573 void (*hw_queues_lock)(struct hl_device *hdev);
574 void (*hw_queues_unlock)(struct hl_device *hdev);
575 u32 (*get_pci_id)(struct hl_device *hdev);
576 int (*get_eeprom_data)(struct hl_device *hdev, void *data,
577 size_t max_size);
578 int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
579 u16 len, u32 timeout, long *result);
580 enum hl_device_hw_state (*get_hw_state)(struct hl_device *hdev);
581 int (*pci_bars_map)(struct hl_device *hdev);
582 u64 (*set_dram_bar_base)(struct hl_device *hdev, u64 addr);
583 int (*init_iatu)(struct hl_device *hdev);
584 u32 (*rreg)(struct hl_device *hdev, u32 reg);
585 void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
586 void (*halt_coresight)(struct hl_device *hdev);
587 };
588
589
590
591
592
593
594 #define HL_KERNEL_ASID_ID 0
595
596
597
598
599
600
601
602
603 struct hl_va_range {
604 struct mutex lock;
605 struct list_head list;
606 u64 start_addr;
607 u64 end_addr;
608 };
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644 struct hl_ctx {
645 DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
646 DECLARE_HASHTABLE(mmu_phys_hash, MMU_HASH_TABLE_BITS);
647 DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
648 struct hl_fpriv *hpriv;
649 struct hl_device *hdev;
650 struct kref refcount;
651 struct dma_fence *cs_pending[HL_MAX_PENDING_CS];
652 struct hl_va_range host_va_range;
653 struct hl_va_range dram_va_range;
654 struct mutex mem_hash_lock;
655 struct mutex mmu_lock;
656 struct list_head debugfs_list;
657 u64 cs_sequence;
658 u64 *dram_default_hops;
659 spinlock_t cs_lock;
660 atomic64_t dram_phys_mem;
661 atomic_t thread_ctx_switch_token;
662 u32 thread_ctx_switch_wait_token;
663 u32 asid;
664 u32 handle;
665 };
666
667
668
669
670
671
672 struct hl_ctx_mgr {
673 struct mutex ctx_lock;
674 struct idr ctx_handles;
675 };
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695 struct hl_userptr {
696 enum vm_type_t vm_type;
697 struct list_head job_node;
698 struct frame_vector *vec;
699 struct sg_table *sgt;
700 enum dma_data_direction dir;
701 struct list_head debugfs_list;
702 u64 addr;
703 u32 size;
704 u8 dma_mapped;
705 };
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726 struct hl_cs {
727 u8 jobs_in_queue_cnt[HL_MAX_QUEUES];
728 struct hl_ctx *ctx;
729 struct list_head job_list;
730 spinlock_t job_lock;
731 struct kref refcount;
732 struct dma_fence *fence;
733 struct delayed_work work_tdr;
734 struct list_head mirror_node;
735 struct list_head debugfs_list;
736 u64 sequence;
737 u8 submitted;
738 u8 completed;
739 u8 timedout;
740 u8 tdr_active;
741 u8 aborted;
742 };
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761 struct hl_cs_job {
762 struct list_head cs_node;
763 struct hl_cs *cs;
764 struct hl_cb *user_cb;
765 struct hl_cb *patched_cb;
766 struct work_struct finish_work;
767 struct list_head userptr_list;
768 struct list_head debugfs_list;
769 u32 id;
770 u32 hw_queue_id;
771 u32 user_cb_size;
772 u32 job_cb_size;
773 u8 ext_queue;
774 };
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791 struct hl_cs_parser {
792 struct hl_cb *user_cb;
793 struct hl_cb *patched_cb;
794 struct list_head *job_userptr_list;
795 u64 cs_sequence;
796 u32 ctx_id;
797 u32 hw_queue_id;
798 u32 user_cb_size;
799 u32 patched_cb_size;
800 u8 ext_queue;
801 u8 job_id;
802 };
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817 struct hl_vm_hash_node {
818 struct hlist_node node;
819 u64 vaddr;
820 void *ptr;
821 };
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838 struct hl_vm_phys_pg_pack {
839 enum vm_type_t vm_type;
840 u64 *pages;
841 u64 npages;
842 u64 total_size;
843 atomic_t mapping_cnt;
844 u32 asid;
845 u32 page_size;
846 u32 flags;
847 u32 handle;
848 u32 offset;
849 u8 contiguous;
850 u8 created_from_userptr;
851 };
852
853
854
855
856
857
858
859
860 struct hl_vm_va_block {
861 struct list_head node;
862 u64 start;
863 u64 end;
864 u64 size;
865 };
866
867
868
869
870
871
872
873
874
875
876 struct hl_vm {
877 struct gen_pool *dram_pg_pool;
878 struct kref dram_pg_pool_refcount;
879 spinlock_t idr_lock;
880 struct idr phys_pg_pack_handles;
881 u8 init_done;
882 };
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898 struct hl_debug_params {
899 void *input;
900 void *output;
901 u32 output_size;
902 u32 reg_idx;
903 u32 op;
904 bool enable;
905 };
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925 struct hl_fpriv {
926 struct hl_device *hdev;
927 struct file *filp;
928 struct pid *taskpid;
929 struct hl_ctx *ctx;
930 struct hl_ctx_mgr ctx_mgr;
931 struct hl_cb_mgr cb_mgr;
932 struct list_head debugfs_list;
933 struct list_head dev_node;
934 struct kref refcount;
935 struct mutex restore_phase_mutex;
936 u8 is_control;
937 };
938
939
940
941
942
943
944
945
946
947
948
949
950 struct hl_info_list {
951 const char *name;
952 int (*show)(struct seq_file *s, void *data);
953 ssize_t (*write)(struct file *file, const char __user *buf,
954 size_t count, loff_t *f_pos);
955 };
956
957
958
959
960
961
962
963 struct hl_debugfs_entry {
964 struct dentry *dent;
965 const struct hl_info_list *info_ent;
966 struct hl_dbg_device_entry *dev_entry;
967 };
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993 struct hl_dbg_device_entry {
994 struct dentry *root;
995 struct hl_device *hdev;
996 struct hl_debugfs_entry *entry_arr;
997 struct list_head file_list;
998 struct mutex file_mutex;
999 struct list_head cb_list;
1000 spinlock_t cb_spinlock;
1001 struct list_head cs_list;
1002 spinlock_t cs_spinlock;
1003 struct list_head cs_job_list;
1004 spinlock_t cs_job_spinlock;
1005 struct list_head userptr_list;
1006 spinlock_t userptr_spinlock;
1007 struct list_head ctx_mem_hash_list;
1008 spinlock_t ctx_mem_hash_spinlock;
1009 u64 addr;
1010 u64 mmu_addr;
1011 u32 mmu_asid;
1012 u8 i2c_bus;
1013 u8 i2c_addr;
1014 u8 i2c_reg;
1015 };
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 #define HL_MAX_MINORS 256
1026
1027
1028
1029
1030
1031 u32 hl_rreg(struct hl_device *hdev, u32 reg);
1032 void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
1033
1034 #define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
1035 #define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
1036 #define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n", \
1037 hdev->asic_funcs->rreg(hdev, (reg)))
1038
1039 #define WREG32_P(reg, val, mask) \
1040 do { \
1041 u32 tmp_ = RREG32(reg); \
1042 tmp_ &= (mask); \
1043 tmp_ |= ((val) & ~(mask)); \
1044 WREG32(reg, tmp_); \
1045 } while (0)
1046 #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
1047 #define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
1048
1049 #define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
1050 #define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
1051 #define WREG32_FIELD(reg, field, val) \
1052 WREG32(mm##reg, (RREG32(mm##reg) & ~REG_FIELD_MASK(reg, field)) | \
1053 (val) << REG_FIELD_SHIFT(reg, field))
1054
1055
1056
1057
1058 #define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
1059 ({ \
1060 ktime_t __timeout; \
1061 if (hdev->pdev) \
1062 __timeout = ktime_add_us(ktime_get(), timeout_us); \
1063 else \
1064 __timeout = ktime_add_us(ktime_get(),\
1065 min((u64)(timeout_us * 10), \
1066 (u64) HL_SIM_MAX_TIMEOUT_US)); \
1067 might_sleep_if(sleep_us); \
1068 for (;;) { \
1069 (val) = RREG32(addr); \
1070 if (cond) \
1071 break; \
1072 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
1073 (val) = RREG32(addr); \
1074 break; \
1075 } \
1076 if (sleep_us) \
1077 usleep_range((sleep_us >> 2) + 1, sleep_us); \
1078 } \
1079 (cond) ? 0 : -ETIMEDOUT; \
1080 })
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 #define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
1095 mem_written_by_device) \
1096 ({ \
1097 ktime_t __timeout; \
1098 if (hdev->pdev) \
1099 __timeout = ktime_add_us(ktime_get(), timeout_us); \
1100 else \
1101 __timeout = ktime_add_us(ktime_get(),\
1102 min((u64)(timeout_us * 10), \
1103 (u64) HL_SIM_MAX_TIMEOUT_US)); \
1104 might_sleep_if(sleep_us); \
1105 for (;;) { \
1106 \
1107 mb(); \
1108 (val) = *((u32 *) (uintptr_t) (addr)); \
1109 if (mem_written_by_device) \
1110 (val) = le32_to_cpu(*(__le32 *) &(val)); \
1111 if (cond) \
1112 break; \
1113 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
1114 (val) = *((u32 *) (uintptr_t) (addr)); \
1115 if (mem_written_by_device) \
1116 (val) = le32_to_cpu(*(__le32 *) &(val)); \
1117 break; \
1118 } \
1119 if (sleep_us) \
1120 usleep_range((sleep_us >> 2) + 1, sleep_us); \
1121 } \
1122 (cond) ? 0 : -ETIMEDOUT; \
1123 })
1124
1125 #define hl_poll_timeout_device_memory(hdev, addr, val, cond, sleep_us, \
1126 timeout_us) \
1127 ({ \
1128 ktime_t __timeout; \
1129 if (hdev->pdev) \
1130 __timeout = ktime_add_us(ktime_get(), timeout_us); \
1131 else \
1132 __timeout = ktime_add_us(ktime_get(),\
1133 min((u64)(timeout_us * 10), \
1134 (u64) HL_SIM_MAX_TIMEOUT_US)); \
1135 might_sleep_if(sleep_us); \
1136 for (;;) { \
1137 (val) = readl(addr); \
1138 if (cond) \
1139 break; \
1140 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
1141 (val) = readl(addr); \
1142 break; \
1143 } \
1144 if (sleep_us) \
1145 usleep_range((sleep_us >> 2) + 1, sleep_us); \
1146 } \
1147 (cond) ? 0 : -ETIMEDOUT; \
1148 })
1149
1150 struct hwmon_chip_info;
1151
1152
1153
1154
1155
1156
1157 struct hl_device_reset_work {
1158 struct work_struct reset_work;
1159 struct hl_device *hdev;
1160 };
1161
1162
1163
1164
1165
1166
1167 struct hl_device_idle_busy_ts {
1168 ktime_t idle_to_busy_ts;
1169 ktime_t busy_to_idle_ts;
1170 };
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254 struct hl_device {
1255 struct pci_dev *pdev;
1256 void __iomem *pcie_bar[6];
1257 void __iomem *rmmio;
1258 struct cdev cdev;
1259 struct cdev cdev_ctrl;
1260 struct device *dev;
1261 struct device *dev_ctrl;
1262 struct delayed_work work_freq;
1263 struct delayed_work work_heartbeat;
1264 char asic_name[16];
1265 enum hl_asic_type asic_type;
1266 struct hl_cq *completion_queue;
1267 struct workqueue_struct *cq_wq;
1268 struct workqueue_struct *eq_wq;
1269 struct hl_ctx *kernel_ctx;
1270 struct hl_hw_queue *kernel_queues;
1271 struct list_head hw_queues_mirror_list;
1272 spinlock_t hw_queues_mirror_lock;
1273 struct hl_cb_mgr kernel_cb_mgr;
1274 struct hl_eq event_queue;
1275 struct dma_pool *dma_pool;
1276 void *cpu_accessible_dma_mem;
1277 dma_addr_t cpu_accessible_dma_address;
1278 struct gen_pool *cpu_accessible_dma_pool;
1279 unsigned long *asid_bitmap;
1280 struct mutex asid_mutex;
1281 struct mutex send_cpu_message_lock;
1282 struct mutex debug_lock;
1283 struct asic_fixed_properties asic_prop;
1284 const struct hl_asic_funcs *asic_funcs;
1285 void *asic_specific;
1286 struct gen_pool *mmu_pgt_pool;
1287 struct hl_vm vm;
1288 struct mutex mmu_cache_lock;
1289 void *mmu_shadow_hop0;
1290 struct device *hwmon_dev;
1291 enum hl_pm_mng_profile pm_mng_profile;
1292 struct hwmon_chip_info *hl_chip_info;
1293
1294 struct hl_dbg_device_entry hl_debugfs;
1295
1296 struct list_head cb_pool;
1297 spinlock_t cb_pool_lock;
1298
1299 struct list_head fpriv_list;
1300 struct mutex fpriv_list_lock;
1301
1302 struct hl_ctx *compute_ctx;
1303
1304 struct hl_device_idle_busy_ts *idle_busy_ts_arr;
1305
1306 atomic64_t dram_used_mem;
1307 u64 timeout_jiffies;
1308 u64 max_power;
1309 atomic_t in_reset;
1310 enum hl_pll_frequency curr_pll_profile;
1311 int cs_active_cnt;
1312 u32 major;
1313 u32 high_pll;
1314 u32 soft_reset_cnt;
1315 u32 hard_reset_cnt;
1316 u32 idle_busy_ts_idx;
1317 u16 id;
1318 u16 id_control;
1319 u8 disabled;
1320 u8 late_init_done;
1321 u8 hwmon_initialized;
1322 u8 hard_reset_pending;
1323 u8 heartbeat;
1324 u8 reset_on_lockup;
1325 u8 dram_supports_virtual_memory;
1326 u8 dram_default_page_mapping;
1327 u8 init_done;
1328 u8 device_cpu_disabled;
1329 u8 dma_mask;
1330 u8 in_debug;
1331 u8 cdev_sysfs_created;
1332
1333
1334 u8 mmu_enable;
1335 u8 cpu_enable;
1336 u8 reset_pcilink;
1337 u8 cpu_queues_enable;
1338 u8 fw_loading;
1339 u8 pldm;
1340 };
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355 typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
1356
1357
1358
1359
1360
1361
1362 struct hl_ioctl_desc {
1363 unsigned int cmd;
1364 hl_ioctl_t *func;
1365 };
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381 static inline bool hl_mem_area_inside_range(u64 address, u32 size,
1382 u64 range_start_address, u64 range_end_address)
1383 {
1384 u64 end_address = address + size;
1385
1386 if ((address >= range_start_address) &&
1387 (end_address <= range_end_address) &&
1388 (end_address > address))
1389 return true;
1390
1391 return false;
1392 }
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404 static inline bool hl_mem_area_crosses_range(u64 address, u32 size,
1405 u64 range_start_address, u64 range_end_address)
1406 {
1407 u64 end_address = address + size;
1408
1409 if ((address >= range_start_address) &&
1410 (address < range_end_address))
1411 return true;
1412
1413 if ((end_address >= range_start_address) &&
1414 (end_address < range_end_address))
1415 return true;
1416
1417 if ((address < range_start_address) &&
1418 (end_address >= range_end_address))
1419 return true;
1420
1421 return false;
1422 }
1423
1424 int hl_device_open(struct inode *inode, struct file *filp);
1425 int hl_device_open_ctrl(struct inode *inode, struct file *filp);
1426 bool hl_device_disabled_or_in_reset(struct hl_device *hdev);
1427 enum hl_device_status hl_device_status(struct hl_device *hdev);
1428 int hl_device_set_debug_mode(struct hl_device *hdev, bool enable);
1429 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
1430 enum hl_asic_type asic_type, int minor);
1431 void destroy_hdev(struct hl_device *hdev);
1432 int hl_hw_queues_create(struct hl_device *hdev);
1433 void hl_hw_queues_destroy(struct hl_device *hdev);
1434 int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
1435 u32 cb_size, u64 cb_ptr);
1436 int hl_hw_queue_schedule_cs(struct hl_cs *cs);
1437 u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
1438 void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
1439 void hl_int_hw_queue_update_ci(struct hl_cs *cs);
1440 void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
1441
1442 #define hl_queue_inc_ptr(p) hl_hw_queue_add_ptr(p, 1)
1443 #define hl_pi_2_offset(pi) ((pi) & (HL_QUEUE_LENGTH - 1))
1444
1445 int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
1446 void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
1447 int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
1448 void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
1449 void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
1450 void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
1451 irqreturn_t hl_irq_handler_cq(int irq, void *arg);
1452 irqreturn_t hl_irq_handler_eq(int irq, void *arg);
1453 u32 hl_cq_inc_ptr(u32 ptr);
1454
1455 int hl_asid_init(struct hl_device *hdev);
1456 void hl_asid_fini(struct hl_device *hdev);
1457 unsigned long hl_asid_alloc(struct hl_device *hdev);
1458 void hl_asid_free(struct hl_device *hdev, unsigned long asid);
1459
1460 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
1461 void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
1462 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
1463 void hl_ctx_do_release(struct kref *ref);
1464 void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx);
1465 int hl_ctx_put(struct hl_ctx *ctx);
1466 struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
1467 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
1468 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
1469
1470 int hl_device_init(struct hl_device *hdev, struct class *hclass);
1471 void hl_device_fini(struct hl_device *hdev);
1472 int hl_device_suspend(struct hl_device *hdev);
1473 int hl_device_resume(struct hl_device *hdev);
1474 int hl_device_reset(struct hl_device *hdev, bool hard_reset,
1475 bool from_hard_reset_thread);
1476 void hl_hpriv_get(struct hl_fpriv *hpriv);
1477 void hl_hpriv_put(struct hl_fpriv *hpriv);
1478 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq);
1479 uint32_t hl_device_utilization(struct hl_device *hdev, uint32_t period_ms);
1480
1481 int hl_build_hwmon_channel_info(struct hl_device *hdev,
1482 struct armcp_sensor *sensors_arr);
1483
1484 int hl_sysfs_init(struct hl_device *hdev);
1485 void hl_sysfs_fini(struct hl_device *hdev);
1486
1487 int hl_hwmon_init(struct hl_device *hdev);
1488 void hl_hwmon_fini(struct hl_device *hdev);
1489
1490 int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr, u32 cb_size,
1491 u64 *handle, int ctx_id);
1492 int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
1493 int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
1494 struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
1495 u32 handle);
1496 void hl_cb_put(struct hl_cb *cb);
1497 void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
1498 void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
1499 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size);
1500 int hl_cb_pool_init(struct hl_device *hdev);
1501 int hl_cb_pool_fini(struct hl_device *hdev);
1502
1503 void hl_cs_rollback_all(struct hl_device *hdev);
1504 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev, bool ext_queue);
1505
1506 void goya_set_asic_funcs(struct hl_device *hdev);
1507
1508 int hl_vm_ctx_init(struct hl_ctx *ctx);
1509 void hl_vm_ctx_fini(struct hl_ctx *ctx);
1510
1511 int hl_vm_init(struct hl_device *hdev);
1512 void hl_vm_fini(struct hl_device *hdev);
1513
1514 int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
1515 struct hl_userptr *userptr);
1516 int hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
1517 void hl_userptr_delete_list(struct hl_device *hdev,
1518 struct list_head *userptr_list);
1519 bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, u32 size,
1520 struct list_head *userptr_list,
1521 struct hl_userptr **userptr);
1522
1523 int hl_mmu_init(struct hl_device *hdev);
1524 void hl_mmu_fini(struct hl_device *hdev);
1525 int hl_mmu_ctx_init(struct hl_ctx *ctx);
1526 void hl_mmu_ctx_fini(struct hl_ctx *ctx);
1527 int hl_mmu_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size);
1528 int hl_mmu_unmap(struct hl_ctx *ctx, u64 virt_addr, u32 page_size);
1529 void hl_mmu_swap_out(struct hl_ctx *ctx);
1530 void hl_mmu_swap_in(struct hl_ctx *ctx);
1531
1532 int hl_fw_push_fw_to_device(struct hl_device *hdev, const char *fw_name,
1533 void __iomem *dst);
1534 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode);
1535 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
1536 u16 len, u32 timeout, long *result);
1537 int hl_fw_test_cpu_queue(struct hl_device *hdev);
1538 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
1539 dma_addr_t *dma_handle);
1540 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
1541 void *vaddr);
1542 int hl_fw_send_heartbeat(struct hl_device *hdev);
1543 int hl_fw_armcp_info_get(struct hl_device *hdev);
1544 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
1545
1546 int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
1547 bool is_wc[3]);
1548 int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
1549 int hl_pci_set_dram_bar_base(struct hl_device *hdev, u8 inbound_region, u8 bar,
1550 u64 addr);
1551 int hl_pci_init_iatu(struct hl_device *hdev, u64 sram_base_address,
1552 u64 dram_base_address, u64 host_phys_base_address,
1553 u64 host_phys_size);
1554 int hl_pci_init(struct hl_device *hdev, u8 dma_mask);
1555 void hl_pci_fini(struct hl_device *hdev);
1556 int hl_pci_set_dma_mask(struct hl_device *hdev, u8 dma_mask);
1557
1558 long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr);
1559 void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq);
1560 long hl_get_temperature(struct hl_device *hdev, int sensor_index, u32 attr);
1561 long hl_get_voltage(struct hl_device *hdev, int sensor_index, u32 attr);
1562 long hl_get_current(struct hl_device *hdev, int sensor_index, u32 attr);
1563 long hl_get_fan_speed(struct hl_device *hdev, int sensor_index, u32 attr);
1564 long hl_get_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr);
1565 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
1566 long value);
1567 u64 hl_get_max_power(struct hl_device *hdev);
1568 void hl_set_max_power(struct hl_device *hdev, u64 value);
1569
1570 #ifdef CONFIG_DEBUG_FS
1571
1572 void hl_debugfs_init(void);
1573 void hl_debugfs_fini(void);
1574 void hl_debugfs_add_device(struct hl_device *hdev);
1575 void hl_debugfs_remove_device(struct hl_device *hdev);
1576 void hl_debugfs_add_file(struct hl_fpriv *hpriv);
1577 void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
1578 void hl_debugfs_add_cb(struct hl_cb *cb);
1579 void hl_debugfs_remove_cb(struct hl_cb *cb);
1580 void hl_debugfs_add_cs(struct hl_cs *cs);
1581 void hl_debugfs_remove_cs(struct hl_cs *cs);
1582 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
1583 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
1584 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
1585 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1586 struct hl_userptr *userptr);
1587 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
1588 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
1589
1590 #else
1591
1592 static inline void __init hl_debugfs_init(void)
1593 {
1594 }
1595
1596 static inline void hl_debugfs_fini(void)
1597 {
1598 }
1599
1600 static inline void hl_debugfs_add_device(struct hl_device *hdev)
1601 {
1602 }
1603
1604 static inline void hl_debugfs_remove_device(struct hl_device *hdev)
1605 {
1606 }
1607
1608 static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1609 {
1610 }
1611
1612 static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1613 {
1614 }
1615
1616 static inline void hl_debugfs_add_cb(struct hl_cb *cb)
1617 {
1618 }
1619
1620 static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
1621 {
1622 }
1623
1624 static inline void hl_debugfs_add_cs(struct hl_cs *cs)
1625 {
1626 }
1627
1628 static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
1629 {
1630 }
1631
1632 static inline void hl_debugfs_add_job(struct hl_device *hdev,
1633 struct hl_cs_job *job)
1634 {
1635 }
1636
1637 static inline void hl_debugfs_remove_job(struct hl_device *hdev,
1638 struct hl_cs_job *job)
1639 {
1640 }
1641
1642 static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
1643 struct hl_userptr *userptr)
1644 {
1645 }
1646
1647 static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
1648 struct hl_userptr *userptr)
1649 {
1650 }
1651
1652 static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
1653 struct hl_ctx *ctx)
1654 {
1655 }
1656
1657 static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
1658 struct hl_ctx *ctx)
1659 {
1660 }
1661
1662 #endif
1663
1664
1665 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
1666 long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
1667 int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
1668 int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
1669 int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data);
1670 int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
1671
1672 #endif