xref: /linux/drivers/accel/habanalabs/common/habanalabs.h (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * Copyright 2016-2022 HabanaLabs, Ltd.
4  * All Rights Reserved.
5  *
6  */
7 
8 #ifndef HABANALABSP_H_
9 #define HABANALABSP_H_
10 
11 #include "../include/common/cpucp_if.h"
12 #include "../include/common/qman_if.h"
13 #include "../include/hw_ip/mmu/mmu_general.h"
14 #include <uapi/drm/habanalabs_accel.h>
15 
16 #include <linux/cdev.h>
17 #include <linux/iopoll.h>
18 #include <linux/irqreturn.h>
19 #include <linux/dma-direction.h>
20 #include <linux/scatterlist.h>
21 #include <linux/hashtable.h>
22 #include <linux/debugfs.h>
23 #include <linux/rwsem.h>
24 #include <linux/eventfd.h>
25 #include <linux/bitfield.h>
26 #include <linux/genalloc.h>
27 #include <linux/sched/signal.h>
28 #include <linux/io-64-nonatomic-lo-hi.h>
29 #include <linux/coresight.h>
30 #include <linux/dma-buf.h>
31 
32 #include "security.h"
33 
34 #define HL_NAME				"habanalabs"
35 
36 struct hl_device;
37 struct hl_fpriv;
38 
39 /* Use upper bits of mmap offset to store habana driver specific information.
40  * bits[63:59] - Encode mmap type
41  * bits[45:0]  - mmap offset value
42  *
43  * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
44  *  defines are w.r.t to PAGE_SIZE
45  */
46 #define HL_MMAP_TYPE_SHIFT		(59 - PAGE_SHIFT)
47 #define HL_MMAP_TYPE_MASK		(0x1full << HL_MMAP_TYPE_SHIFT)
48 #define HL_MMAP_TYPE_TS_BUFF		(0x10ull << HL_MMAP_TYPE_SHIFT)
49 #define HL_MMAP_TYPE_BLOCK		(0x4ull << HL_MMAP_TYPE_SHIFT)
50 #define HL_MMAP_TYPE_CB			(0x2ull << HL_MMAP_TYPE_SHIFT)
51 
52 #define HL_MMAP_OFFSET_VALUE_MASK	(0x1FFFFFFFFFFFull >> PAGE_SHIFT)
53 #define HL_MMAP_OFFSET_VALUE_GET(off)	(off & HL_MMAP_OFFSET_VALUE_MASK)
54 
55 #define HL_PENDING_RESET_PER_SEC		10
56 #define HL_PENDING_RESET_MAX_TRIALS		60 /* 10 minutes */
57 #define HL_PENDING_RESET_LONG_SEC		60
58 /*
59  * In device fini, wait 10 minutes for user processes to be terminated after we kill them.
60  * This is needed to prevent situation of clearing resources while user processes are still alive.
61  */
62 #define HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI	600
63 
64 #define HL_HARD_RESET_MAX_TIMEOUT	120
65 #define HL_PLDM_HARD_RESET_MAX_TIMEOUT	(HL_HARD_RESET_MAX_TIMEOUT * 3)
66 
67 #define HL_DEVICE_TIMEOUT_USEC		1000000 /* 1 s */
68 
69 #define HL_HEARTBEAT_PER_USEC		5000000 /* 5 s */
70 
71 #define HL_PLL_LOW_JOB_FREQ_USEC	5000000 /* 5 s */
72 
73 #define HL_CPUCP_INFO_TIMEOUT_USEC	10000000 /* 10s */
74 #define HL_CPUCP_EEPROM_TIMEOUT_USEC	10000000 /* 10s */
75 #define HL_CPUCP_MON_DUMP_TIMEOUT_USEC	10000000 /* 10s */
76 #define HL_CPUCP_SEC_ATTEST_INFO_TINEOUT_USEC 10000000 /* 10s */
77 
78 #define HL_FW_STATUS_POLL_INTERVAL_USEC		10000 /* 10ms */
79 #define HL_FW_COMMS_STATUS_PLDM_POLL_INTERVAL_USEC	1000000 /* 1s */
80 
81 #define HL_PCI_ELBI_TIMEOUT_MSEC	10 /* 10ms */
82 
83 #define HL_SIM_MAX_TIMEOUT_US		100000000 /* 100s */
84 
85 #define HL_INVALID_QUEUE		UINT_MAX
86 
87 #define HL_COMMON_USER_CQ_INTERRUPT_ID	0xFFF
88 #define HL_COMMON_DEC_INTERRUPT_ID	0xFFE
89 
90 #define HL_STATE_DUMP_HIST_LEN		5
91 
92 /* Default value for device reset trigger , an invalid value */
93 #define HL_RESET_TRIGGER_DEFAULT	0xFF
94 
95 #define OBJ_NAMES_HASH_TABLE_BITS	7 /* 1 << 7 buckets */
96 #define SYNC_TO_ENGINE_HASH_TABLE_BITS	7 /* 1 << 7 buckets */
97 
98 /* Memory */
99 #define MEM_HASH_TABLE_BITS		7 /* 1 << 7 buckets */
100 
101 /* MMU */
102 #define MMU_HASH_TABLE_BITS		7 /* 1 << 7 buckets */
103 
104 /**
105  * enum hl_mmu_page_table_location - mmu page table location
106  * @MMU_DR_PGT: page-table is located on device DRAM.
107  * @MMU_HR_PGT: page-table is located on host memory.
108  * @MMU_NUM_PGT_LOCATIONS: number of page-table locations currently supported.
109  */
110 enum hl_mmu_page_table_location {
111 	MMU_DR_PGT = 0,		/* device-dram-resident MMU PGT */
112 	MMU_HR_PGT,		/* host resident MMU PGT */
113 	MMU_NUM_PGT_LOCATIONS	/* num of PGT locations */
114 };
115 
116 /**
117  * enum hl_mmu_enablement - what mmu modules to enable
118  * @MMU_EN_NONE: mmu disabled.
119  * @MMU_EN_ALL: enable all.
120  * @MMU_EN_PMMU_ONLY: Enable only the PMMU leaving the DMMU disabled.
121  */
122 enum hl_mmu_enablement {
123 	MMU_EN_NONE = 0,
124 	MMU_EN_ALL = 1,
125 	MMU_EN_PMMU_ONLY = 3,	/* N/A for Goya/Gaudi */
126 };
127 
128 /*
129  * HL_RSVD_SOBS 'sync stream' reserved sync objects per QMAN stream
130  * HL_RSVD_MONS 'sync stream' reserved monitors per QMAN stream
131  */
132 #define HL_RSVD_SOBS			2
133 #define HL_RSVD_MONS			1
134 
135 /*
136  * HL_COLLECTIVE_RSVD_MSTR_MONS 'collective' reserved monitors per QMAN stream
137  */
138 #define HL_COLLECTIVE_RSVD_MSTR_MONS	2
139 
140 #define HL_MAX_SOB_VAL			(1 << 15)
141 
142 #define IS_POWER_OF_2(n)		(n != 0 && ((n & (n - 1)) == 0))
143 #define IS_MAX_PENDING_CS_VALID(n)	(IS_POWER_OF_2(n) && (n > 1))
144 
145 #define HL_PCI_NUM_BARS			6
146 
147 /* Completion queue entry relates to completed job */
148 #define HL_COMPLETION_MODE_JOB		0
149 /* Completion queue entry relates to completed command submission */
150 #define HL_COMPLETION_MODE_CS		1
151 
152 #define HL_MAX_DCORES			8
153 
154 /* DMA alloc/free wrappers */
155 #define hl_asic_dma_alloc_coherent(hdev, size, dma_handle, flags) \
156 	hl_asic_dma_alloc_coherent_caller(hdev, size, dma_handle, flags, __func__)
157 
158 #define hl_cpu_accessible_dma_pool_alloc(hdev, size, dma_handle) \
159 	hl_cpu_accessible_dma_pool_alloc_caller(hdev, size, dma_handle, __func__)
160 
161 #define hl_asic_dma_pool_zalloc(hdev, size, mem_flags, dma_handle) \
162 	hl_asic_dma_pool_zalloc_caller(hdev, size, mem_flags, dma_handle, __func__)
163 
164 #define hl_asic_dma_free_coherent(hdev, size, cpu_addr, dma_handle) \
165 	hl_asic_dma_free_coherent_caller(hdev, size, cpu_addr, dma_handle, __func__)
166 
167 #define hl_cpu_accessible_dma_pool_free(hdev, size, vaddr) \
168 	hl_cpu_accessible_dma_pool_free_caller(hdev, size, vaddr, __func__)
169 
170 #define hl_asic_dma_pool_free(hdev, vaddr, dma_addr) \
171 	hl_asic_dma_pool_free_caller(hdev, vaddr, dma_addr, __func__)
172 
173 /*
174  * Reset Flags
175  *
176  * - HL_DRV_RESET_HARD
177  *       If set do hard reset to all engines. If not set reset just
178  *       compute/DMA engines.
179  *
180  * - HL_DRV_RESET_FROM_RESET_THR
181  *       Set if the caller is the hard-reset thread
182  *
183  * - HL_DRV_RESET_HEARTBEAT
184  *       Set if reset is due to heartbeat
185  *
186  * - HL_DRV_RESET_TDR
187  *       Set if reset is due to TDR
188  *
189  * - HL_DRV_RESET_DEV_RELEASE
190  *       Set if reset is due to device release
191  *
192  * - HL_DRV_RESET_BYPASS_REQ_TO_FW
193  *       F/W will perform the reset. No need to ask it to reset the device. This is relevant
194  *       only when running with secured f/w
195  *
196  * - HL_DRV_RESET_FW_FATAL_ERR
197  *       Set if reset is due to a fatal error from FW
198  *
199  * - HL_DRV_RESET_DELAY
200  *       Set if a delay should be added before the reset
201  *
202  * - HL_DRV_RESET_FROM_WD_THR
203  *       Set if the caller is the device release watchdog thread
204  */
205 
206 #define HL_DRV_RESET_HARD		(1 << 0)
207 #define HL_DRV_RESET_FROM_RESET_THR	(1 << 1)
208 #define HL_DRV_RESET_HEARTBEAT		(1 << 2)
209 #define HL_DRV_RESET_TDR		(1 << 3)
210 #define HL_DRV_RESET_DEV_RELEASE	(1 << 4)
211 #define HL_DRV_RESET_BYPASS_REQ_TO_FW	(1 << 5)
212 #define HL_DRV_RESET_FW_FATAL_ERR	(1 << 6)
213 #define HL_DRV_RESET_DELAY		(1 << 7)
214 #define HL_DRV_RESET_FROM_WD_THR	(1 << 8)
215 
216 /*
217  * Security
218  */
219 
220 #define HL_PB_SHARED		1
221 #define HL_PB_NA		0
222 #define HL_PB_SINGLE_INSTANCE	1
223 #define HL_BLOCK_SIZE		0x1000
224 #define HL_BLOCK_GLBL_ERR_MASK	0xF40
225 #define HL_BLOCK_GLBL_ERR_ADDR	0xF44
226 #define HL_BLOCK_GLBL_ERR_CAUSE	0xF48
227 #define HL_BLOCK_GLBL_SEC_OFFS	0xF80
228 #define HL_BLOCK_GLBL_SEC_SIZE	(HL_BLOCK_SIZE - HL_BLOCK_GLBL_SEC_OFFS)
229 #define HL_BLOCK_GLBL_SEC_LEN	(HL_BLOCK_GLBL_SEC_SIZE / sizeof(u32))
230 #define UNSET_GLBL_SEC_BIT(array, b) ((array)[((b) / 32)] |= (1 << ((b) % 32)))
231 
232 enum hl_protection_levels {
233 	SECURED_LVL,
234 	PRIVILEGED_LVL,
235 	NON_SECURED_LVL
236 };
237 
238 /**
239  * struct iterate_module_ctx - HW module iterator
240  * @fn: function to apply to each HW module instance
241  * @data: optional internal data to the function iterator
242  * @rc: return code for optional use of iterator/iterator-caller
243  */
244 struct iterate_module_ctx {
245 	/*
246 	 * callback for the HW module iterator
247 	 * @hdev: pointer to the habanalabs device structure
248 	 * @block: block (ASIC specific definition can be dcore/hdcore)
249 	 * @inst: HW module instance within the block
250 	 * @offset: current HW module instance offset from the 1-st HW module instance
251 	 *          in the 1-st block
252 	 * @ctx: the iterator context.
253 	 */
254 	void (*fn)(struct hl_device *hdev, int block, int inst, u32 offset,
255 			struct iterate_module_ctx *ctx);
256 	void *data;
257 	int rc;
258 };
259 
260 struct hl_block_glbl_sec {
261 	u32 sec_array[HL_BLOCK_GLBL_SEC_LEN];
262 };
263 
264 #define HL_MAX_SOBS_PER_MONITOR	8
265 
266 /**
267  * struct hl_gen_wait_properties - properties for generating a wait CB
268  * @data: command buffer
269  * @q_idx: queue id is used to extract fence register address
270  * @size: offset in command buffer
271  * @sob_base: SOB base to use in this wait CB
272  * @sob_val: SOB value to wait for
273  * @mon_id: monitor to use in this wait CB
274  * @sob_mask: each bit represents a SOB offset from sob_base to be used
275  */
276 struct hl_gen_wait_properties {
277 	void	*data;
278 	u32	q_idx;
279 	u32	size;
280 	u16	sob_base;
281 	u16	sob_val;
282 	u16	mon_id;
283 	u8	sob_mask;
284 };
285 
286 /**
287  * struct pgt_info - MMU hop page info.
288  * @node: hash linked-list node for the pgts on host (shadow pgts for device resident MMU and
289  *        actual pgts for host resident MMU).
290  * @phys_addr: physical address of the pgt.
291  * @virt_addr: host virtual address of the pgt (see above device/host resident).
292  * @shadow_addr: shadow hop in the host for device resident MMU.
293  * @ctx: pointer to the owner ctx.
294  * @num_of_ptes: indicates how many ptes are used in the pgt. used only for dynamically
295  *               allocated HOPs (all HOPs but HOP0)
296  *
297  * The MMU page tables hierarchy can be placed either on the device's DRAM (in which case shadow
298  * pgts will be stored on host memory) or on host memory (in which case no shadow is required).
299  *
300  * When a new level (hop) is needed during mapping this structure will be used to describe
301  * the newly allocated hop as well as to track number of PTEs in it.
302  * During unmapping, if no valid PTEs remained in the page of a newly allocated hop, it is
303  * freed with its pgt_info structure.
304  */
305 struct pgt_info {
306 	struct hlist_node	node;
307 	u64			phys_addr;
308 	u64			virt_addr;
309 	u64			shadow_addr;
310 	struct hl_ctx		*ctx;
311 	int			num_of_ptes;
312 };
313 
314 /**
315  * enum hl_pci_match_mode - pci match mode per region
316  * @PCI_ADDRESS_MATCH_MODE: address match mode
317  * @PCI_BAR_MATCH_MODE: bar match mode
318  */
319 enum hl_pci_match_mode {
320 	PCI_ADDRESS_MATCH_MODE,
321 	PCI_BAR_MATCH_MODE
322 };
323 
324 /**
325  * enum hl_fw_component - F/W components to read version through registers.
326  * @FW_COMP_BOOT_FIT: boot fit.
327  * @FW_COMP_PREBOOT: preboot.
328  * @FW_COMP_LINUX: linux.
329  */
330 enum hl_fw_component {
331 	FW_COMP_BOOT_FIT,
332 	FW_COMP_PREBOOT,
333 	FW_COMP_LINUX,
334 };
335 
336 /**
337  * enum hl_fw_types - F/W types present in the system
338  * @FW_TYPE_NONE: no FW component indication
339  * @FW_TYPE_LINUX: Linux image for device CPU
340  * @FW_TYPE_BOOT_CPU: Boot image for device CPU
341  * @FW_TYPE_PREBOOT_CPU: Indicates pre-loaded CPUs are present in the system
342  *                       (preboot, ppboot etc...)
343  * @FW_TYPE_ALL_TYPES: Mask for all types
344  */
345 enum hl_fw_types {
346 	FW_TYPE_NONE = 0x0,
347 	FW_TYPE_LINUX = 0x1,
348 	FW_TYPE_BOOT_CPU = 0x2,
349 	FW_TYPE_PREBOOT_CPU = 0x4,
350 	FW_TYPE_ALL_TYPES =
351 		(FW_TYPE_LINUX | FW_TYPE_BOOT_CPU | FW_TYPE_PREBOOT_CPU)
352 };
353 
354 /**
355  * enum hl_queue_type - Supported QUEUE types.
356  * @QUEUE_TYPE_NA: queue is not available.
357  * @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the
358  *                  host.
359  * @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's
360  *			memories and/or operates the compute engines.
361  * @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU.
362  * @QUEUE_TYPE_HW: queue of DMA and compute engines jobs, for which completion
363  *                 notifications are sent by H/W.
364  */
365 enum hl_queue_type {
366 	QUEUE_TYPE_NA,
367 	QUEUE_TYPE_EXT,
368 	QUEUE_TYPE_INT,
369 	QUEUE_TYPE_CPU,
370 	QUEUE_TYPE_HW
371 };
372 
373 enum hl_cs_type {
374 	CS_TYPE_DEFAULT,
375 	CS_TYPE_SIGNAL,
376 	CS_TYPE_WAIT,
377 	CS_TYPE_COLLECTIVE_WAIT,
378 	CS_RESERVE_SIGNALS,
379 	CS_UNRESERVE_SIGNALS,
380 	CS_TYPE_ENGINE_CORE,
381 	CS_TYPE_FLUSH_PCI_HBW_WRITES,
382 };
383 
384 /*
385  * struct hl_inbound_pci_region - inbound region descriptor
386  * @mode: pci match mode for this region
387  * @addr: region target address
388  * @size: region size in bytes
389  * @offset_in_bar: offset within bar (address match mode)
390  * @bar: bar id
391  */
392 struct hl_inbound_pci_region {
393 	enum hl_pci_match_mode	mode;
394 	u64			addr;
395 	u64			size;
396 	u64			offset_in_bar;
397 	u8			bar;
398 };
399 
400 /*
401  * struct hl_outbound_pci_region - outbound region descriptor
402  * @addr: region target address
403  * @size: region size in bytes
404  */
405 struct hl_outbound_pci_region {
406 	u64	addr;
407 	u64	size;
408 };
409 
410 /*
411  * enum queue_cb_alloc_flags - Indicates queue support for CBs that
412  * allocated by Kernel or by User
413  * @CB_ALLOC_KERNEL: support only CBs that allocated by Kernel
414  * @CB_ALLOC_USER: support only CBs that allocated by User
415  */
416 enum queue_cb_alloc_flags {
417 	CB_ALLOC_KERNEL = 0x1,
418 	CB_ALLOC_USER   = 0x2
419 };
420 
421 /*
422  * struct hl_hw_sob - H/W SOB info.
423  * @hdev: habanalabs device structure.
424  * @kref: refcount of this SOB. The SOB will reset once the refcount is zero.
425  * @sob_id: id of this SOB.
426  * @sob_addr: the sob offset from the base address.
427  * @q_idx: the H/W queue that uses this SOB.
428  * @need_reset: reset indication set when switching to the other sob.
429  */
430 struct hl_hw_sob {
431 	struct hl_device	*hdev;
432 	struct kref		kref;
433 	u32			sob_id;
434 	u32			sob_addr;
435 	u32			q_idx;
436 	bool			need_reset;
437 };
438 
439 enum hl_collective_mode {
440 	HL_COLLECTIVE_NOT_SUPPORTED = 0x0,
441 	HL_COLLECTIVE_MASTER = 0x1,
442 	HL_COLLECTIVE_SLAVE = 0x2
443 };
444 
445 /**
446  * struct hw_queue_properties - queue information.
447  * @type: queue type.
448  * @cb_alloc_flags: bitmap which indicates if the hw queue supports CB
449  *                  that allocated by the Kernel driver and therefore,
450  *                  a CB handle can be provided for jobs on this queue.
451  *                  Otherwise, a CB address must be provided.
452  * @collective_mode: collective mode of current queue
453  * @driver_only: true if only the driver is allowed to send a job to this queue,
454  *               false otherwise.
455  * @binned: True if the queue is binned out and should not be used
456  * @supports_sync_stream: True if queue supports sync stream
457  */
458 struct hw_queue_properties {
459 	enum hl_queue_type		type;
460 	enum queue_cb_alloc_flags	cb_alloc_flags;
461 	enum hl_collective_mode		collective_mode;
462 	u8				driver_only;
463 	u8				binned;
464 	u8				supports_sync_stream;
465 };
466 
467 /**
468  * enum vm_type - virtual memory mapping request information.
469  * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
470  * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
471  */
472 enum vm_type {
473 	VM_TYPE_USERPTR = 0x1,
474 	VM_TYPE_PHYS_PACK = 0x2
475 };
476 
477 /**
478  * enum mmu_op_flags - mmu operation relevant information.
479  * @MMU_OP_USERPTR: operation on user memory (host resident).
480  * @MMU_OP_PHYS_PACK: operation on DRAM (device resident).
481  * @MMU_OP_CLEAR_MEMCACHE: operation has to clear memcache.
482  * @MMU_OP_SKIP_LOW_CACHE_INV: operation is allowed to skip parts of cache invalidation.
483  */
484 enum mmu_op_flags {
485 	MMU_OP_USERPTR = 0x1,
486 	MMU_OP_PHYS_PACK = 0x2,
487 	MMU_OP_CLEAR_MEMCACHE = 0x4,
488 	MMU_OP_SKIP_LOW_CACHE_INV = 0x8,
489 };
490 
491 
492 /**
493  * enum hl_device_hw_state - H/W device state. use this to understand whether
494  *                           to do reset before hw_init or not
495  * @HL_DEVICE_HW_STATE_CLEAN: H/W state is clean. i.e. after hard reset
496  * @HL_DEVICE_HW_STATE_DIRTY: H/W state is dirty. i.e. we started to execute
497  *                            hw_init
498  */
499 enum hl_device_hw_state {
500 	HL_DEVICE_HW_STATE_CLEAN = 0,
501 	HL_DEVICE_HW_STATE_DIRTY
502 };
503 
504 #define HL_MMU_VA_ALIGNMENT_NOT_NEEDED 0
505 
506 /**
507  * struct hl_mmu_properties - ASIC specific MMU address translation properties.
508  * @start_addr: virtual start address of the memory region.
509  * @end_addr: virtual end address of the memory region.
510  * @hop_shifts: array holds HOPs shifts.
511  * @hop_masks: array holds HOPs masks.
512  * @last_mask: mask to get the bit indicating this is the last hop.
513  * @pgt_size: size for page tables.
514  * @supported_pages_mask: bitmask for supported page size (relevant only for MMUs
515  *                        supporting multiple page size).
516  * @page_size: default page size used to allocate memory.
517  * @num_hops: The amount of hops supported by the translation table.
518  * @hop_table_size: HOP table size.
519  * @hop0_tables_total_size: total size for all HOP0 tables.
520  * @host_resident: Should the MMU page table reside in host memory or in the
521  *                 device DRAM.
522  */
523 struct hl_mmu_properties {
524 	u64	start_addr;
525 	u64	end_addr;
526 	u64	hop_shifts[MMU_HOP_MAX];
527 	u64	hop_masks[MMU_HOP_MAX];
528 	u64	last_mask;
529 	u64	pgt_size;
530 	u64	supported_pages_mask;
531 	u32	page_size;
532 	u32	num_hops;
533 	u32	hop_table_size;
534 	u32	hop0_tables_total_size;
535 	u8	host_resident;
536 };
537 
538 /**
539  * struct hl_hints_range - hint addresses reserved va range.
540  * @start_addr: start address of the va range.
541  * @end_addr: end address of the va range.
542  */
543 struct hl_hints_range {
544 	u64 start_addr;
545 	u64 end_addr;
546 };
547 
548 /**
549  * struct asic_fixed_properties - ASIC specific immutable properties.
550  * @hw_queues_props: H/W queues properties.
551  * @special_blocks: points to an array containing special blocks info.
552  * @skip_special_blocks_cfg: special blocks skip configs.
553  * @cpucp_info: received various information from CPU-CP regarding the H/W, e.g.
554  *		available sensors.
555  * @uboot_ver: F/W U-boot version.
556  * @preboot_ver: F/W Preboot version.
557  * @dmmu: DRAM MMU address translation properties.
558  * @pmmu: PCI (host) MMU address translation properties.
559  * @pmmu_huge: PCI (host) MMU address translation properties for memory
560  *              allocated with huge pages.
561  * @hints_dram_reserved_va_range: dram hint addresses reserved range.
562  * @hints_host_reserved_va_range: host hint addresses reserved range.
563  * @hints_host_hpage_reserved_va_range: host huge page hint addresses reserved
564  *                                      range.
565  * @sram_base_address: SRAM physical start address.
566  * @sram_end_address: SRAM physical end address.
567  * @sram_user_base_address - SRAM physical start address for user access.
568  * @dram_base_address: DRAM physical start address.
569  * @dram_end_address: DRAM physical end address.
570  * @dram_user_base_address: DRAM physical start address for user access.
571  * @dram_size: DRAM total size.
572  * @dram_pci_bar_size: size of PCI bar towards DRAM.
573  * @max_power_default: max power of the device after reset.
574  * @dc_power_default: power consumed by the device in mode idle.
575  * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
576  *                                      fault.
577  * @pcie_dbi_base_address: Base address of the PCIE_DBI block.
578  * @pcie_aux_dbi_reg_addr: Address of the PCIE_AUX DBI register.
579  * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
580  * @mmu_dram_default_page_addr: DRAM default page physical address.
581  * @tpc_enabled_mask: which TPCs are enabled.
582  * @tpc_binning_mask: which TPCs are binned. 0 means usable and 1 means binned.
583  * @dram_enabled_mask: which DRAMs are enabled.
584  * @dram_binning_mask: which DRAMs are binned. 0 means usable, 1 means binned.
585  * @dram_hints_align_mask: dram va hint addresses alignment mask which is used
586  *                  for hints validity check.
587  * @cfg_base_address: config space base address.
588  * @mmu_cache_mng_addr: address of the MMU cache.
589  * @mmu_cache_mng_size: size of the MMU cache.
590  * @device_dma_offset_for_host_access: the offset to add to host DMA addresses
591  *                                     to enable the device to access them.
592  * @host_base_address: host physical start address for host DMA from device
593  * @host_end_address: host physical end address for host DMA from device
594  * @max_freq_value: current max clk frequency.
595  * @clk_pll_index: clock PLL index that specify which PLL determines the clock
596  *                 we display to the user
597  * @mmu_pgt_size: MMU page tables total size.
598  * @mmu_pte_size: PTE size in MMU page tables.
599  * @mmu_hop_table_size: MMU hop table size.
600  * @mmu_hop0_tables_total_size: total size of MMU hop0 tables.
601  * @dram_page_size: page size for MMU DRAM allocation.
602  * @cfg_size: configuration space size on SRAM.
603  * @sram_size: total size of SRAM.
604  * @max_asid: maximum number of open contexts (ASIDs).
605  * @num_of_events: number of possible internal H/W IRQs.
606  * @psoc_pci_pll_nr: PCI PLL NR value.
607  * @psoc_pci_pll_nf: PCI PLL NF value.
608  * @psoc_pci_pll_od: PCI PLL OD value.
609  * @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value.
610  * @psoc_timestamp_frequency: frequency of the psoc timestamp clock.
611  * @high_pll: high PLL frequency used by the device.
612  * @cb_pool_cb_cnt: number of CBs in the CB pool.
613  * @cb_pool_cb_size: size of each CB in the CB pool.
614  * @decoder_enabled_mask: which decoders are enabled.
615  * @decoder_binning_mask: which decoders are binned, 0 means usable and 1
616  *                        means binned (at most one binned decoder per dcore).
617  * @edma_enabled_mask: which EDMAs are enabled.
618  * @edma_binning_mask: which EDMAs are binned, 0 means usable and 1 means
619  *                     binned (at most one binned DMA).
620  * @max_pending_cs: maximum of concurrent pending command submissions
621  * @max_queues: maximum amount of queues in the system
622  * @fw_preboot_cpu_boot_dev_sts0: bitmap representation of preboot cpu
623  *                                capabilities reported by FW, bit description
624  *                                can be found in CPU_BOOT_DEV_STS0
625  * @fw_preboot_cpu_boot_dev_sts1: bitmap representation of preboot cpu
626  *                                capabilities reported by FW, bit description
627  *                                can be found in CPU_BOOT_DEV_STS1
628  * @fw_bootfit_cpu_boot_dev_sts0: bitmap representation of boot cpu security
629  *                                status reported by FW, bit description can be
630  *                                found in CPU_BOOT_DEV_STS0
631  * @fw_bootfit_cpu_boot_dev_sts1: bitmap representation of boot cpu security
632  *                                status reported by FW, bit description can be
633  *                                found in CPU_BOOT_DEV_STS1
634  * @fw_app_cpu_boot_dev_sts0: bitmap representation of application security
635  *                            status reported by FW, bit description can be
636  *                            found in CPU_BOOT_DEV_STS0
637  * @fw_app_cpu_boot_dev_sts1: bitmap representation of application security
638  *                            status reported by FW, bit description can be
639  *                            found in CPU_BOOT_DEV_STS1
640  * @max_dec: maximum number of decoders
641  * @hmmu_hif_enabled_mask: mask of HMMUs/HIFs that are not isolated (enabled)
642  *                         1- enabled, 0- isolated.
643  * @faulty_dram_cluster_map: mask of faulty DRAM cluster.
644  *                         1- faulty cluster, 0- good cluster.
645  * @xbar_edge_enabled_mask: mask of XBAR_EDGEs that are not isolated (enabled)
646  *                          1- enabled, 0- isolated.
647  * @device_mem_alloc_default_page_size: may be different than dram_page_size only for ASICs for
648  *                                      which the property supports_user_set_page_size is true
649  *                                      (i.e. the DRAM supports multiple page sizes), otherwise
650  *                                      it will shall  be equal to dram_page_size.
651  * @num_engine_cores: number of engine cpu cores
652  * @num_of_special_blocks: special_blocks array size.
653  * @glbl_err_cause_num: global err cause number.
654  * @hbw_flush_reg: register to read to generate HBW flush. value of 0 means HBW flush is
655  *                 not supported.
656  * @collective_first_sob: first sync object available for collective use
657  * @collective_first_mon: first monitor available for collective use
658  * @sync_stream_first_sob: first sync object available for sync stream use
659  * @sync_stream_first_mon: first monitor available for sync stream use
660  * @first_available_user_sob: first sob available for the user
661  * @first_available_user_mon: first monitor available for the user
662  * @first_available_user_interrupt: first available interrupt reserved for the user
663  * @first_available_cq: first available CQ for the user.
664  * @user_interrupt_count: number of user interrupts.
665  * @user_dec_intr_count: number of decoder interrupts exposed to user.
666  * @cache_line_size: device cache line size.
667  * @server_type: Server type that the ASIC is currently installed in.
668  *               The value is according to enum hl_server_type in uapi file.
669  * @completion_queues_count: number of completion queues.
670  * @completion_mode: 0 - job based completion, 1 - cs based completion
671  * @mme_master_slave_mode: 0 - Each MME works independently, 1 - MME works
672  *                         in Master/Slave mode
673  * @fw_security_enabled: true if security measures are enabled in firmware,
674  *                       false otherwise
675  * @fw_cpu_boot_dev_sts0_valid: status bits are valid and can be fetched from
676  *                              BOOT_DEV_STS0
677  * @fw_cpu_boot_dev_sts1_valid: status bits are valid and can be fetched from
678  *                              BOOT_DEV_STS1
679  * @dram_supports_virtual_memory: is there an MMU towards the DRAM
680  * @hard_reset_done_by_fw: true if firmware is handling hard reset flow
681  * @num_functional_hbms: number of functional HBMs in each DCORE.
682  * @hints_range_reservation: device support hint addresses range reservation.
683  * @iatu_done_by_fw: true if iATU configuration is being done by FW.
684  * @dynamic_fw_load: is dynamic FW load is supported.
685  * @gic_interrupts_enable: true if FW is not blocking GIC controller,
686  *                         false otherwise.
687  * @use_get_power_for_reset_history: To support backward compatibility for Goya
688  *                                   and Gaudi
689  * @supports_compute_reset: is a reset which is not a hard-reset supported by this asic.
690  * @allow_inference_soft_reset: true if the ASIC supports soft reset that is
691  *                              initiated by user or TDR. This is only true
692  *                              in inference ASICs, as there is no real-world
693  *                              use-case of doing soft-reset in training (due
694  *                              to the fact that training runs on multiple
695  *                              devices)
696  * @configurable_stop_on_err: is stop-on-error option configurable via debugfs.
697  * @set_max_power_on_device_init: true if need to set max power in F/W on device init.
698  * @supports_user_set_page_size: true if user can set the allocation page size.
699  * @dma_mask: the dma mask to be set for this device
700  * @supports_advanced_cpucp_rc: true if new cpucp opcodes are supported.
701  */
702 struct asic_fixed_properties {
703 	struct hw_queue_properties	*hw_queues_props;
704 	struct hl_special_block_info	*special_blocks;
705 	struct hl_skip_blocks_cfg	skip_special_blocks_cfg;
706 	struct cpucp_info		cpucp_info;
707 	char				uboot_ver[VERSION_MAX_LEN];
708 	char				preboot_ver[VERSION_MAX_LEN];
709 	struct hl_mmu_properties	dmmu;
710 	struct hl_mmu_properties	pmmu;
711 	struct hl_mmu_properties	pmmu_huge;
712 	struct hl_hints_range		hints_dram_reserved_va_range;
713 	struct hl_hints_range		hints_host_reserved_va_range;
714 	struct hl_hints_range		hints_host_hpage_reserved_va_range;
715 	u64				sram_base_address;
716 	u64				sram_end_address;
717 	u64				sram_user_base_address;
718 	u64				dram_base_address;
719 	u64				dram_end_address;
720 	u64				dram_user_base_address;
721 	u64				dram_size;
722 	u64				dram_pci_bar_size;
723 	u64				max_power_default;
724 	u64				dc_power_default;
725 	u64				dram_size_for_default_page_mapping;
726 	u64				pcie_dbi_base_address;
727 	u64				pcie_aux_dbi_reg_addr;
728 	u64				mmu_pgt_addr;
729 	u64				mmu_dram_default_page_addr;
730 	u64				tpc_enabled_mask;
731 	u64				tpc_binning_mask;
732 	u64				dram_enabled_mask;
733 	u64				dram_binning_mask;
734 	u64				dram_hints_align_mask;
735 	u64				cfg_base_address;
736 	u64				mmu_cache_mng_addr;
737 	u64				mmu_cache_mng_size;
738 	u64				device_dma_offset_for_host_access;
739 	u64				host_base_address;
740 	u64				host_end_address;
741 	u64				max_freq_value;
742 	u32				clk_pll_index;
743 	u32				mmu_pgt_size;
744 	u32				mmu_pte_size;
745 	u32				mmu_hop_table_size;
746 	u32				mmu_hop0_tables_total_size;
747 	u32				dram_page_size;
748 	u32				cfg_size;
749 	u32				sram_size;
750 	u32				max_asid;
751 	u32				num_of_events;
752 	u32				psoc_pci_pll_nr;
753 	u32				psoc_pci_pll_nf;
754 	u32				psoc_pci_pll_od;
755 	u32				psoc_pci_pll_div_factor;
756 	u32				psoc_timestamp_frequency;
757 	u32				high_pll;
758 	u32				cb_pool_cb_cnt;
759 	u32				cb_pool_cb_size;
760 	u32				decoder_enabled_mask;
761 	u32				decoder_binning_mask;
762 	u32				edma_enabled_mask;
763 	u32				edma_binning_mask;
764 	u32				max_pending_cs;
765 	u32				max_queues;
766 	u32				fw_preboot_cpu_boot_dev_sts0;
767 	u32				fw_preboot_cpu_boot_dev_sts1;
768 	u32				fw_bootfit_cpu_boot_dev_sts0;
769 	u32				fw_bootfit_cpu_boot_dev_sts1;
770 	u32				fw_app_cpu_boot_dev_sts0;
771 	u32				fw_app_cpu_boot_dev_sts1;
772 	u32				max_dec;
773 	u32				hmmu_hif_enabled_mask;
774 	u32				faulty_dram_cluster_map;
775 	u32				xbar_edge_enabled_mask;
776 	u32				device_mem_alloc_default_page_size;
777 	u32				num_engine_cores;
778 	u32				num_of_special_blocks;
779 	u32				glbl_err_cause_num;
780 	u32				hbw_flush_reg;
781 	u16				collective_first_sob;
782 	u16				collective_first_mon;
783 	u16				sync_stream_first_sob;
784 	u16				sync_stream_first_mon;
785 	u16				first_available_user_sob[HL_MAX_DCORES];
786 	u16				first_available_user_mon[HL_MAX_DCORES];
787 	u16				first_available_user_interrupt;
788 	u16				first_available_cq[HL_MAX_DCORES];
789 	u16				user_interrupt_count;
790 	u16				user_dec_intr_count;
791 	u16				cache_line_size;
792 	u16				server_type;
793 	u8				completion_queues_count;
794 	u8				completion_mode;
795 	u8				mme_master_slave_mode;
796 	u8				fw_security_enabled;
797 	u8				fw_cpu_boot_dev_sts0_valid;
798 	u8				fw_cpu_boot_dev_sts1_valid;
799 	u8				dram_supports_virtual_memory;
800 	u8				hard_reset_done_by_fw;
801 	u8				num_functional_hbms;
802 	u8				hints_range_reservation;
803 	u8				iatu_done_by_fw;
804 	u8				dynamic_fw_load;
805 	u8				gic_interrupts_enable;
806 	u8				use_get_power_for_reset_history;
807 	u8				supports_compute_reset;
808 	u8				allow_inference_soft_reset;
809 	u8				configurable_stop_on_err;
810 	u8				set_max_power_on_device_init;
811 	u8				supports_user_set_page_size;
812 	u8				dma_mask;
813 	u8				supports_advanced_cpucp_rc;
814 };
815 
816 /**
817  * struct hl_fence - software synchronization primitive
818  * @completion: fence is implemented using completion
819  * @refcount: refcount for this fence
820  * @cs_sequence: sequence of the corresponding command submission
821  * @stream_master_qid_map: streams masters QID bitmap to represent all streams
822  *                         masters QIDs that multi cs is waiting on
823  * @error: mark this fence with error
824  * @timestamp: timestamp upon completion
825  * @mcs_handling_done: indicates that corresponding command submission has
826  *                     finished msc handling, this does not mean it was part
827  *                     of the mcs
828  */
829 struct hl_fence {
830 	struct completion	completion;
831 	struct kref		refcount;
832 	u64			cs_sequence;
833 	u32			stream_master_qid_map;
834 	int			error;
835 	ktime_t			timestamp;
836 	u8			mcs_handling_done;
837 };
838 
839 /**
840  * struct hl_cs_compl - command submission completion object.
841  * @base_fence: hl fence object.
842  * @lock: spinlock to protect fence.
843  * @hdev: habanalabs device structure.
844  * @hw_sob: the H/W SOB used in this signal/wait CS.
845  * @encaps_sig_hdl: encaps signals handler.
846  * @cs_seq: command submission sequence number.
847  * @type: type of the CS - signal/wait.
848  * @sob_val: the SOB value that is used in this signal/wait CS.
849  * @sob_group: the SOB group that is used in this collective wait CS.
850  * @encaps_signals: indication whether it's a completion object of cs with
851  * encaps signals or not.
852  */
853 struct hl_cs_compl {
854 	struct hl_fence		base_fence;
855 	spinlock_t		lock;
856 	struct hl_device	*hdev;
857 	struct hl_hw_sob	*hw_sob;
858 	struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
859 	u64			cs_seq;
860 	enum hl_cs_type		type;
861 	u16			sob_val;
862 	u16			sob_group;
863 	bool			encaps_signals;
864 };
865 
866 /*
867  * Command Buffers
868  */
869 
870 /**
871  * struct hl_ts_buff - describes a timestamp buffer.
872  * @kernel_buff_address: Holds the internal buffer's kernel virtual address.
873  * @user_buff_address: Holds the user buffer's kernel virtual address.
874  * @kernel_buff_size: Holds the internal kernel buffer size.
875  */
876 struct hl_ts_buff {
877 	void			*kernel_buff_address;
878 	void			*user_buff_address;
879 	u32			kernel_buff_size;
880 };
881 
882 struct hl_mmap_mem_buf;
883 
884 /**
885  * struct hl_mem_mgr - describes unified memory manager for mappable memory chunks.
886  * @dev: back pointer to the owning device
887  * @lock: protects handles
888  * @handles: an idr holding all active handles to the memory buffers in the system.
889  */
890 struct hl_mem_mgr {
891 	struct device *dev;
892 	spinlock_t lock;
893 	struct idr handles;
894 };
895 
896 /**
897  * struct hl_mmap_mem_buf_behavior - describes unified memory manager buffer behavior
898  * @topic: string identifier used for logging
899  * @mem_id: memory type identifier, embedded in the handle and used to identify
900  *          the memory type by handle.
901  * @alloc: callback executed on buffer allocation, shall allocate the memory,
902  *         set it under buffer private, and set mappable size.
903  * @mmap: callback executed on mmap, must map the buffer to vma
904  * @release: callback executed on release, must free the resources used by the buffer
905  */
906 struct hl_mmap_mem_buf_behavior {
907 	const char *topic;
908 	u64 mem_id;
909 
910 	int (*alloc)(struct hl_mmap_mem_buf *buf, gfp_t gfp, void *args);
911 	int (*mmap)(struct hl_mmap_mem_buf *buf, struct vm_area_struct *vma, void *args);
912 	void (*release)(struct hl_mmap_mem_buf *buf);
913 };
914 
915 /**
916  * struct hl_mmap_mem_buf - describes a single unified memory buffer
917  * @behavior: buffer behavior
918  * @mmg: back pointer to the unified memory manager
919  * @refcount: reference counter for buffer users
920  * @private: pointer to buffer behavior private data
921  * @mmap: atomic boolean indicating whether or not the buffer is mapped right now
922  * @real_mapped_size: the actual size of buffer mapped, after part of it may be released,
923  *                   may change at runtime.
924  * @mappable_size: the original mappable size of the buffer, does not change after
925  *                 the allocation.
926  * @handle: the buffer id in mmg handles store
927  */
928 struct hl_mmap_mem_buf {
929 	struct hl_mmap_mem_buf_behavior *behavior;
930 	struct hl_mem_mgr *mmg;
931 	struct kref refcount;
932 	void *private;
933 	atomic_t mmap;
934 	u64 real_mapped_size;
935 	u64 mappable_size;
936 	u64 handle;
937 };
938 
939 /**
940  * struct hl_cb - describes a Command Buffer.
941  * @hdev: pointer to device this CB belongs to.
942  * @ctx: pointer to the CB owner's context.
943  * @buf: back pointer to the parent mappable memory buffer
944  * @debugfs_list: node in debugfs list of command buffers.
945  * @pool_list: node in pool list of command buffers.
946  * @kernel_address: Holds the CB's kernel virtual address.
947  * @virtual_addr: Holds the CB's virtual address.
948  * @bus_address: Holds the CB's DMA address.
949  * @size: holds the CB's size.
950  * @roundup_size: holds the cb size after roundup to page size.
951  * @cs_cnt: holds number of CS that this CB participates in.
952  * @is_handle_destroyed: atomic boolean indicating whether or not the CB handle was destroyed.
953  * @is_pool: true if CB was acquired from the pool, false otherwise.
954  * @is_internal: internally allocated
955  * @is_mmu_mapped: true if the CB is mapped to the device's MMU.
956  */
957 struct hl_cb {
958 	struct hl_device	*hdev;
959 	struct hl_ctx		*ctx;
960 	struct hl_mmap_mem_buf	*buf;
961 	struct list_head	debugfs_list;
962 	struct list_head	pool_list;
963 	void			*kernel_address;
964 	u64			virtual_addr;
965 	dma_addr_t		bus_address;
966 	u32			size;
967 	u32			roundup_size;
968 	atomic_t		cs_cnt;
969 	atomic_t		is_handle_destroyed;
970 	u8			is_pool;
971 	u8			is_internal;
972 	u8			is_mmu_mapped;
973 };
974 
975 
976 /*
977  * QUEUES
978  */
979 
980 struct hl_cs_job;
981 
982 /* Queue length of external and HW queues */
983 #define HL_QUEUE_LENGTH			4096
984 #define HL_QUEUE_SIZE_IN_BYTES		(HL_QUEUE_LENGTH * HL_BD_SIZE)
985 
986 #if (HL_MAX_JOBS_PER_CS > HL_QUEUE_LENGTH)
987 #error "HL_QUEUE_LENGTH must be greater than HL_MAX_JOBS_PER_CS"
988 #endif
989 
990 /* HL_CQ_LENGTH is in units of struct hl_cq_entry */
991 #define HL_CQ_LENGTH			HL_QUEUE_LENGTH
992 #define HL_CQ_SIZE_IN_BYTES		(HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)
993 
994 /* Must be power of 2 */
995 #define HL_EQ_LENGTH			64
996 #define HL_EQ_SIZE_IN_BYTES		(HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
997 
998 /* Host <-> CPU-CP shared memory size */
999 #define HL_CPU_ACCESSIBLE_MEM_SIZE	SZ_2M
1000 
1001 /**
1002  * struct hl_sync_stream_properties -
1003  *     describes a H/W queue sync stream properties
1004  * @hw_sob: array of the used H/W SOBs by this H/W queue.
1005  * @next_sob_val: the next value to use for the currently used SOB.
1006  * @base_sob_id: the base SOB id of the SOBs used by this queue.
1007  * @base_mon_id: the base MON id of the MONs used by this queue.
1008  * @collective_mstr_mon_id: the MON ids of the MONs used by this master queue
1009  *                          in order to sync with all slave queues.
1010  * @collective_slave_mon_id: the MON id used by this slave queue in order to
1011  *                           sync with its master queue.
1012  * @collective_sob_id: current SOB id used by this collective slave queue
1013  *                     to signal its collective master queue upon completion.
1014  * @curr_sob_offset: the id offset to the currently used SOB from the
1015  *                   HL_RSVD_SOBS that are being used by this queue.
1016  */
1017 struct hl_sync_stream_properties {
1018 	struct hl_hw_sob hw_sob[HL_RSVD_SOBS];
1019 	u16		next_sob_val;
1020 	u16		base_sob_id;
1021 	u16		base_mon_id;
1022 	u16		collective_mstr_mon_id[HL_COLLECTIVE_RSVD_MSTR_MONS];
1023 	u16		collective_slave_mon_id;
1024 	u16		collective_sob_id;
1025 	u8		curr_sob_offset;
1026 };
1027 
1028 /**
1029  * struct hl_encaps_signals_mgr - describes sync stream encapsulated signals
1030  * handlers manager
1031  * @lock: protects handles.
1032  * @handles: an idr to hold all encapsulated signals handles.
1033  */
1034 struct hl_encaps_signals_mgr {
1035 	spinlock_t		lock;
1036 	struct idr		handles;
1037 };
1038 
1039 /**
1040  * struct hl_hw_queue - describes a H/W transport queue.
1041  * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
1042  * @sync_stream_prop: sync stream queue properties
1043  * @queue_type: type of queue.
1044  * @collective_mode: collective mode of current queue
1045  * @kernel_address: holds the queue's kernel virtual address.
1046  * @bus_address: holds the queue's DMA address.
1047  * @pi: holds the queue's pi value.
1048  * @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci).
1049  * @hw_queue_id: the id of the H/W queue.
1050  * @cq_id: the id for the corresponding CQ for this H/W queue.
1051  * @msi_vec: the IRQ number of the H/W queue.
1052  * @int_queue_len: length of internal queue (number of entries).
1053  * @valid: is the queue valid (we have array of 32 queues, not all of them
1054  *         exist).
1055  * @supports_sync_stream: True if queue supports sync stream
1056  */
1057 struct hl_hw_queue {
1058 	struct hl_cs_job			**shadow_queue;
1059 	struct hl_sync_stream_properties	sync_stream_prop;
1060 	enum hl_queue_type			queue_type;
1061 	enum hl_collective_mode			collective_mode;
1062 	void					*kernel_address;
1063 	dma_addr_t				bus_address;
1064 	u32					pi;
1065 	atomic_t				ci;
1066 	u32					hw_queue_id;
1067 	u32					cq_id;
1068 	u32					msi_vec;
1069 	u16					int_queue_len;
1070 	u8					valid;
1071 	u8					supports_sync_stream;
1072 };
1073 
1074 /**
1075  * struct hl_cq - describes a completion queue
1076  * @hdev: pointer to the device structure
1077  * @kernel_address: holds the queue's kernel virtual address
1078  * @bus_address: holds the queue's DMA address
1079  * @cq_idx: completion queue index in array
1080  * @hw_queue_id: the id of the matching H/W queue
1081  * @ci: ci inside the queue
1082  * @pi: pi inside the queue
1083  * @free_slots_cnt: counter of free slots in queue
1084  */
1085 struct hl_cq {
1086 	struct hl_device	*hdev;
1087 	void			*kernel_address;
1088 	dma_addr_t		bus_address;
1089 	u32			cq_idx;
1090 	u32			hw_queue_id;
1091 	u32			ci;
1092 	u32			pi;
1093 	atomic_t		free_slots_cnt;
1094 };
1095 
1096 enum hl_user_interrupt_type {
1097 	HL_USR_INTERRUPT_CQ = 0,
1098 	HL_USR_INTERRUPT_DECODER,
1099 };
1100 
1101 /**
1102  * struct hl_user_interrupt - holds user interrupt information
1103  * @hdev: pointer to the device structure
1104  * @type: user interrupt type
1105  * @wait_list_head: head to the list of user threads pending on this interrupt
1106  * @wait_list_lock: protects wait_list_head
1107  * @interrupt_id: msix interrupt id
1108  */
1109 struct hl_user_interrupt {
1110 	struct hl_device		*hdev;
1111 	enum hl_user_interrupt_type	type;
1112 	struct list_head		wait_list_head;
1113 	spinlock_t			wait_list_lock;
1114 	u32				interrupt_id;
1115 };
1116 
1117 /**
1118  * struct timestamp_reg_free_node - holds the timestamp registration free objects node
1119  * @free_objects_node: node in the list free_obj_jobs
1120  * @cq_cb: pointer to cq command buffer to be freed
1121  * @buf: pointer to timestamp buffer to be freed
1122  */
1123 struct timestamp_reg_free_node {
1124 	struct list_head	free_objects_node;
1125 	struct hl_cb		*cq_cb;
1126 	struct hl_mmap_mem_buf	*buf;
1127 };
1128 
1129 /* struct timestamp_reg_work_obj - holds the timestamp registration free objects job
1130  * the job will be to pass over the free_obj_jobs list and put refcount to objects
1131  * in each node of the list
1132  * @free_obj: workqueue object to free timestamp registration node objects
1133  * @hdev: pointer to the device structure
1134  * @free_obj_head: list of free jobs nodes (node type timestamp_reg_free_node)
1135  */
1136 struct timestamp_reg_work_obj {
1137 	struct work_struct	free_obj;
1138 	struct hl_device	*hdev;
1139 	struct list_head	*free_obj_head;
1140 };
1141 
1142 /* struct timestamp_reg_info - holds the timestamp registration related data.
1143  * @buf: pointer to the timestamp buffer which include both user/kernel buffers.
1144  *       relevant only when doing timestamps records registration.
1145  * @cq_cb: pointer to CQ counter CB.
1146  * @timestamp_kernel_addr: timestamp handle address, where to set timestamp
1147  *                         relevant only when doing timestamps records
1148  *                         registration.
1149  * @in_use: indicates if the node already in use. relevant only when doing
1150  *          timestamps records registration, since in this case the driver
1151  *          will have it's own buffer which serve as a records pool instead of
1152  *          allocating records dynamically.
1153  */
1154 struct timestamp_reg_info {
1155 	struct hl_mmap_mem_buf	*buf;
1156 	struct hl_cb		*cq_cb;
1157 	u64			*timestamp_kernel_addr;
1158 	u8			in_use;
1159 };
1160 
1161 /**
1162  * struct hl_user_pending_interrupt - holds a context to a user thread
1163  *                                    pending on an interrupt
1164  * @ts_reg_info: holds the timestamps registration nodes info
1165  * @wait_list_node: node in the list of user threads pending on an interrupt
1166  * @fence: hl fence object for interrupt completion
1167  * @cq_target_value: CQ target value
1168  * @cq_kernel_addr: CQ kernel address, to be used in the cq interrupt
1169  *                  handler for target value comparison
1170  */
1171 struct hl_user_pending_interrupt {
1172 	struct timestamp_reg_info	ts_reg_info;
1173 	struct list_head		wait_list_node;
1174 	struct hl_fence			fence;
1175 	u64				cq_target_value;
1176 	u64				*cq_kernel_addr;
1177 };
1178 
1179 /**
1180  * struct hl_eq - describes the event queue (single one per device)
1181  * @hdev: pointer to the device structure
1182  * @kernel_address: holds the queue's kernel virtual address
1183  * @bus_address: holds the queue's DMA address
1184  * @ci: ci inside the queue
1185  * @prev_eqe_index: the index of the previous event queue entry. The index of
1186  *                  the current entry's index must be +1 of the previous one.
1187  * @check_eqe_index: do we need to check the index of the current entry vs. the
1188  *                   previous one. This is for backward compatibility with older
1189  *                   firmwares
1190  */
1191 struct hl_eq {
1192 	struct hl_device	*hdev;
1193 	void			*kernel_address;
1194 	dma_addr_t		bus_address;
1195 	u32			ci;
1196 	u32			prev_eqe_index;
1197 	bool			check_eqe_index;
1198 };
1199 
1200 /**
1201  * struct hl_dec - describes a decoder sw instance.
1202  * @hdev: pointer to the device structure.
1203  * @completion_abnrm_work: workqueue object to run when decoder generates an error interrupt
1204  * @core_id: ID of the decoder.
1205  * @base_addr: base address of the decoder.
1206  */
1207 struct hl_dec {
1208 	struct hl_device		*hdev;
1209 	struct work_struct		completion_abnrm_work;
1210 	u32				core_id;
1211 	u32				base_addr;
1212 };
1213 
1214 /**
1215  * enum hl_asic_type - supported ASIC types.
1216  * @ASIC_INVALID: Invalid ASIC type.
1217  * @ASIC_GOYA: Goya device (HL-1000).
1218  * @ASIC_GAUDI: Gaudi device (HL-2000).
1219  * @ASIC_GAUDI_SEC: Gaudi secured device (HL-2000).
1220  * @ASIC_GAUDI2: Gaudi2 device.
1221  * @ASIC_GAUDI2B: Gaudi2B device.
1222  */
1223 enum hl_asic_type {
1224 	ASIC_INVALID,
1225 	ASIC_GOYA,
1226 	ASIC_GAUDI,
1227 	ASIC_GAUDI_SEC,
1228 	ASIC_GAUDI2,
1229 	ASIC_GAUDI2B,
1230 };
1231 
1232 struct hl_cs_parser;
1233 
1234 /**
1235  * enum hl_pm_mng_profile - power management profile.
1236  * @PM_AUTO: internal clock is set by the Linux driver.
1237  * @PM_MANUAL: internal clock is set by the user.
1238  * @PM_LAST: last power management type.
1239  */
1240 enum hl_pm_mng_profile {
1241 	PM_AUTO = 1,
1242 	PM_MANUAL,
1243 	PM_LAST
1244 };
1245 
1246 /**
1247  * enum hl_pll_frequency - PLL frequency.
1248  * @PLL_HIGH: high frequency.
1249  * @PLL_LOW: low frequency.
1250  * @PLL_LAST: last frequency values that were configured by the user.
1251  */
1252 enum hl_pll_frequency {
1253 	PLL_HIGH = 1,
1254 	PLL_LOW,
1255 	PLL_LAST
1256 };
1257 
1258 #define PLL_REF_CLK 50
1259 
1260 enum div_select_defs {
1261 	DIV_SEL_REF_CLK = 0,
1262 	DIV_SEL_PLL_CLK = 1,
1263 	DIV_SEL_DIVIDED_REF = 2,
1264 	DIV_SEL_DIVIDED_PLL = 3,
1265 };
1266 
1267 enum debugfs_access_type {
1268 	DEBUGFS_READ8,
1269 	DEBUGFS_WRITE8,
1270 	DEBUGFS_READ32,
1271 	DEBUGFS_WRITE32,
1272 	DEBUGFS_READ64,
1273 	DEBUGFS_WRITE64,
1274 };
1275 
1276 enum pci_region {
1277 	PCI_REGION_CFG,
1278 	PCI_REGION_SRAM,
1279 	PCI_REGION_DRAM,
1280 	PCI_REGION_SP_SRAM,
1281 	PCI_REGION_NUMBER,
1282 };
1283 
1284 /**
1285  * struct pci_mem_region - describe memory region in a PCI bar
1286  * @region_base: region base address
1287  * @region_size: region size
1288  * @bar_size: size of the BAR
1289  * @offset_in_bar: region offset into the bar
1290  * @bar_id: bar ID of the region
1291  * @used: if used 1, otherwise 0
1292  */
1293 struct pci_mem_region {
1294 	u64 region_base;
1295 	u64 region_size;
1296 	u64 bar_size;
1297 	u64 offset_in_bar;
1298 	u8 bar_id;
1299 	u8 used;
1300 };
1301 
1302 /**
1303  * struct static_fw_load_mgr - static FW load manager
1304  * @preboot_version_max_off: max offset to preboot version
1305  * @boot_fit_version_max_off: max offset to boot fit version
1306  * @kmd_msg_to_cpu_reg: register address for KDM->CPU messages
1307  * @cpu_cmd_status_to_host_reg: register address for CPU command status response
1308  * @cpu_boot_status_reg: boot status register
1309  * @cpu_boot_dev_status0_reg: boot device status register 0
1310  * @cpu_boot_dev_status1_reg: boot device status register 1
1311  * @boot_err0_reg: boot error register 0
1312  * @boot_err1_reg: boot error register 1
1313  * @preboot_version_offset_reg: SRAM offset to preboot version register
1314  * @boot_fit_version_offset_reg: SRAM offset to boot fit version register
1315  * @sram_offset_mask: mask for getting offset into the SRAM
1316  * @cpu_reset_wait_msec: used when setting WFE via kmd_msg_to_cpu_reg
1317  */
1318 struct static_fw_load_mgr {
1319 	u64 preboot_version_max_off;
1320 	u64 boot_fit_version_max_off;
1321 	u32 kmd_msg_to_cpu_reg;
1322 	u32 cpu_cmd_status_to_host_reg;
1323 	u32 cpu_boot_status_reg;
1324 	u32 cpu_boot_dev_status0_reg;
1325 	u32 cpu_boot_dev_status1_reg;
1326 	u32 boot_err0_reg;
1327 	u32 boot_err1_reg;
1328 	u32 preboot_version_offset_reg;
1329 	u32 boot_fit_version_offset_reg;
1330 	u32 sram_offset_mask;
1331 	u32 cpu_reset_wait_msec;
1332 };
1333 
1334 /**
1335  * struct fw_response - FW response to LKD command
1336  * @ram_offset: descriptor offset into the RAM
1337  * @ram_type: RAM type containing the descriptor (SRAM/DRAM)
1338  * @status: command status
1339  */
1340 struct fw_response {
1341 	u32 ram_offset;
1342 	u8 ram_type;
1343 	u8 status;
1344 };
1345 
1346 /**
1347  * struct dynamic_fw_load_mgr - dynamic FW load manager
1348  * @response: FW to LKD response
1349  * @comm_desc: the communication descriptor with FW
1350  * @image_region: region to copy the FW image to
1351  * @fw_image_size: size of FW image to load
1352  * @wait_for_bl_timeout: timeout for waiting for boot loader to respond
1353  * @fw_desc_valid: true if FW descriptor has been validated and hence the data can be used
1354  */
1355 struct dynamic_fw_load_mgr {
1356 	struct fw_response response;
1357 	struct lkd_fw_comms_desc comm_desc;
1358 	struct pci_mem_region *image_region;
1359 	size_t fw_image_size;
1360 	u32 wait_for_bl_timeout;
1361 	bool fw_desc_valid;
1362 };
1363 
1364 /**
1365  * struct pre_fw_load_props - needed properties for pre-FW load
1366  * @cpu_boot_status_reg: cpu_boot_status register address
1367  * @sts_boot_dev_sts0_reg: sts_boot_dev_sts0 register address
1368  * @sts_boot_dev_sts1_reg: sts_boot_dev_sts1 register address
1369  * @boot_err0_reg: boot_err0 register address
1370  * @boot_err1_reg: boot_err1 register address
1371  * @wait_for_preboot_timeout: timeout to poll for preboot ready
1372  */
1373 struct pre_fw_load_props {
1374 	u32 cpu_boot_status_reg;
1375 	u32 sts_boot_dev_sts0_reg;
1376 	u32 sts_boot_dev_sts1_reg;
1377 	u32 boot_err0_reg;
1378 	u32 boot_err1_reg;
1379 	u32 wait_for_preboot_timeout;
1380 };
1381 
1382 /**
1383  * struct fw_image_props - properties of FW image
1384  * @image_name: name of the image
1385  * @src_off: offset in src FW to copy from
1386  * @copy_size: amount of bytes to copy (0 to copy the whole binary)
1387  */
1388 struct fw_image_props {
1389 	char *image_name;
1390 	u32 src_off;
1391 	u32 copy_size;
1392 };
1393 
1394 /**
1395  * struct fw_load_mgr - manager FW loading process
1396  * @dynamic_loader: specific structure for dynamic load
1397  * @static_loader: specific structure for static load
1398  * @pre_fw_load_props: parameter for pre FW load
1399  * @boot_fit_img: boot fit image properties
1400  * @linux_img: linux image properties
1401  * @cpu_timeout: CPU response timeout in usec
1402  * @boot_fit_timeout: Boot fit load timeout in usec
1403  * @skip_bmc: should BMC be skipped
1404  * @sram_bar_id: SRAM bar ID
1405  * @dram_bar_id: DRAM bar ID
1406  * @fw_comp_loaded: bitmask of loaded FW components. set bit meaning loaded
1407  *                  component. values are set according to enum hl_fw_types.
1408  */
1409 struct fw_load_mgr {
1410 	union {
1411 		struct dynamic_fw_load_mgr dynamic_loader;
1412 		struct static_fw_load_mgr static_loader;
1413 	};
1414 	struct pre_fw_load_props pre_fw_load;
1415 	struct fw_image_props boot_fit_img;
1416 	struct fw_image_props linux_img;
1417 	u32 cpu_timeout;
1418 	u32 boot_fit_timeout;
1419 	u8 skip_bmc;
1420 	u8 sram_bar_id;
1421 	u8 dram_bar_id;
1422 	u8 fw_comp_loaded;
1423 };
1424 
1425 struct hl_cs;
1426 
1427 /**
1428  * struct engines_data - asic engines data
1429  * @buf: buffer for engines data in ascii
1430  * @actual_size: actual size of data that was written by the driver to the allocated buffer
1431  * @allocated_buf_size: total size of allocated buffer
1432  */
1433 struct engines_data {
1434 	char *buf;
1435 	int actual_size;
1436 	u32 allocated_buf_size;
1437 };
1438 
1439 /**
1440  * struct hl_asic_funcs - ASIC specific functions that are can be called from
1441  *                        common code.
1442  * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
1443  * @early_fini: tears down what was done in early_init.
1444  * @late_init: sets up late driver/hw state (post hw_init) - Optional.
1445  * @late_fini: tears down what was done in late_init (pre hw_fini) - Optional.
1446  * @sw_init: sets up driver state, does not configure H/W.
1447  * @sw_fini: tears down driver state, does not configure H/W.
1448  * @hw_init: sets up the H/W state.
1449  * @hw_fini: tears down the H/W state.
1450  * @halt_engines: halt engines, needed for reset sequence. This also disables
1451  *                interrupts from the device. Should be called before
1452  *                hw_fini and before CS rollback.
1453  * @suspend: handles IP specific H/W or SW changes for suspend.
1454  * @resume: handles IP specific H/W or SW changes for resume.
1455  * @mmap: maps a memory.
1456  * @ring_doorbell: increment PI on a given QMAN.
1457  * @pqe_write: Write the PQ entry to the PQ. This is ASIC-specific
1458  *             function because the PQs are located in different memory areas
1459  *             per ASIC (SRAM, DRAM, Host memory) and therefore, the method of
1460  *             writing the PQE must match the destination memory area
1461  *             properties.
1462  * @asic_dma_alloc_coherent: Allocate coherent DMA memory by calling
1463  *                           dma_alloc_coherent(). This is ASIC function because
1464  *                           its implementation is not trivial when the driver
1465  *                           is loaded in simulation mode (not upstreamed).
1466  * @asic_dma_free_coherent:  Free coherent DMA memory by calling
1467  *                           dma_free_coherent(). This is ASIC function because
1468  *                           its implementation is not trivial when the driver
1469  *                           is loaded in simulation mode (not upstreamed).
1470  * @scrub_device_mem: Scrub the entire SRAM and DRAM.
1471  * @scrub_device_dram: Scrub the dram memory of the device.
1472  * @get_int_queue_base: get the internal queue base address.
1473  * @test_queues: run simple test on all queues for sanity check.
1474  * @asic_dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool.
1475  *                        size of allocation is HL_DMA_POOL_BLK_SIZE.
1476  * @asic_dma_pool_free: free small DMA allocation from pool.
1477  * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
1478  * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
1479  * @asic_dma_unmap_single: unmap a single DMA buffer
1480  * @asic_dma_map_single: map a single buffer to a DMA
1481  * @hl_dma_unmap_sgtable: DMA unmap scatter-gather table.
1482  * @cs_parser: parse Command Submission.
1483  * @asic_dma_map_sgtable: DMA map scatter-gather table.
1484  * @add_end_of_cb_packets: Add packets to the end of CB, if device requires it.
1485  * @update_eq_ci: update event queue CI.
1486  * @context_switch: called upon ASID context switch.
1487  * @restore_phase_topology: clear all SOBs amd MONs.
1488  * @debugfs_read_dma: debug interface for reading up to 2MB from the device's
1489  *                    internal memory via DMA engine.
1490  * @add_device_attr: add ASIC specific device attributes.
1491  * @handle_eqe: handle event queue entry (IRQ) from CPU-CP.
1492  * @get_events_stat: retrieve event queue entries histogram.
1493  * @read_pte: read MMU page table entry from DRAM.
1494  * @write_pte: write MMU page table entry to DRAM.
1495  * @mmu_invalidate_cache: flush MMU STLB host/DRAM cache, either with soft
1496  *                        (L1 only) or hard (L0 & L1) flush.
1497  * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with ASID-VA-size mask.
1498  * @mmu_prefetch_cache_range: pre-fetch specific MMU STLB cache lines with ASID-VA-size mask.
1499  * @send_heartbeat: send is-alive packet to CPU-CP and verify response.
1500  * @debug_coresight: perform certain actions on Coresight for debugging.
1501  * @is_device_idle: return true if device is idle, false otherwise.
1502  * @compute_reset_late_init: perform certain actions needed after a compute reset
1503  * @hw_queues_lock: acquire H/W queues lock.
1504  * @hw_queues_unlock: release H/W queues lock.
1505  * @get_pci_id: retrieve PCI ID.
1506  * @get_eeprom_data: retrieve EEPROM data from F/W.
1507  * @get_monitor_dump: retrieve monitor registers dump from F/W.
1508  * @send_cpu_message: send message to F/W. If the message is timedout, the
1509  *                    driver will eventually reset the device. The timeout can
1510  *                    be determined by the calling function or it can be 0 and
1511  *                    then the timeout is the default timeout for the specific
1512  *                    ASIC
1513  * @get_hw_state: retrieve the H/W state
1514  * @pci_bars_map: Map PCI BARs.
1515  * @init_iatu: Initialize the iATU unit inside the PCI controller.
1516  * @rreg: Read a register. Needed for simulator support.
1517  * @wreg: Write a register. Needed for simulator support.
1518  * @halt_coresight: stop the ETF and ETR traces.
1519  * @ctx_init: context dependent initialization.
1520  * @ctx_fini: context dependent cleanup.
1521  * @pre_schedule_cs: Perform pre-CS-scheduling operations.
1522  * @get_queue_id_for_cq: Get the H/W queue id related to the given CQ index.
1523  * @load_firmware_to_device: load the firmware to the device's memory
1524  * @load_boot_fit_to_device: load boot fit to device's memory
1525  * @get_signal_cb_size: Get signal CB size.
1526  * @get_wait_cb_size: Get wait CB size.
1527  * @gen_signal_cb: Generate a signal CB.
1528  * @gen_wait_cb: Generate a wait CB.
1529  * @reset_sob: Reset a SOB.
1530  * @reset_sob_group: Reset SOB group
1531  * @get_device_time: Get the device time.
1532  * @pb_print_security_errors: print security errors according block and cause
1533  * @collective_wait_init_cs: Generate collective master/slave packets
1534  *                           and place them in the relevant cs jobs
1535  * @collective_wait_create_jobs: allocate collective wait cs jobs
1536  * @get_dec_base_addr: get the base address of a given decoder.
1537  * @scramble_addr: Routine to scramble the address prior of mapping it
1538  *                 in the MMU.
1539  * @descramble_addr: Routine to de-scramble the address prior of
1540  *                   showing it to users.
1541  * @ack_protection_bits_errors: ack and dump all security violations
1542  * @get_hw_block_id: retrieve a HW block id to be used by the user to mmap it.
1543  *                   also returns the size of the block if caller supplies
1544  *                   a valid pointer for it
1545  * @hw_block_mmap: mmap a HW block with a given id.
1546  * @enable_events_from_fw: send interrupt to firmware to notify them the
1547  *                         driver is ready to receive asynchronous events. This
1548  *                         function should be called during the first init and
1549  *                         after every hard-reset of the device
1550  * @ack_mmu_errors: check and ack mmu errors, page fault, access violation.
1551  * @get_msi_info: Retrieve asic-specific MSI ID of the f/w async event
1552  * @map_pll_idx_to_fw_idx: convert driver specific per asic PLL index to
1553  *                         generic f/w compatible PLL Indexes
1554  * @init_firmware_preload_params: initialize pre FW-load parameters.
1555  * @init_firmware_loader: initialize data for FW loader.
1556  * @init_cpu_scrambler_dram: Enable CPU specific DRAM scrambling
1557  * @state_dump_init: initialize constants required for state dump
1558  * @get_sob_addr: get SOB base address offset.
1559  * @set_pci_memory_regions: setting properties of PCI memory regions
1560  * @get_stream_master_qid_arr: get pointer to stream masters QID array
1561  * @check_if_razwi_happened: check if there was a razwi due to RR violation.
1562  * @access_dev_mem: access device memory
1563  * @set_dram_bar_base: set the base of the DRAM BAR
1564  * @set_engine_cores: set a config command to engine cores
1565  * @send_device_activity: indication to FW about device availability
1566  * @set_dram_properties: set DRAM related properties.
1567  * @set_binning_masks: set binning/enable masks for all relevant components.
1568  */
1569 struct hl_asic_funcs {
1570 	int (*early_init)(struct hl_device *hdev);
1571 	int (*early_fini)(struct hl_device *hdev);
1572 	int (*late_init)(struct hl_device *hdev);
1573 	void (*late_fini)(struct hl_device *hdev);
1574 	int (*sw_init)(struct hl_device *hdev);
1575 	int (*sw_fini)(struct hl_device *hdev);
1576 	int (*hw_init)(struct hl_device *hdev);
1577 	void (*hw_fini)(struct hl_device *hdev, bool hard_reset, bool fw_reset);
1578 	void (*halt_engines)(struct hl_device *hdev, bool hard_reset, bool fw_reset);
1579 	int (*suspend)(struct hl_device *hdev);
1580 	int (*resume)(struct hl_device *hdev);
1581 	int (*mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
1582 			void *cpu_addr, dma_addr_t dma_addr, size_t size);
1583 	void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
1584 	void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
1585 			struct hl_bd *bd);
1586 	void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
1587 					dma_addr_t *dma_handle, gfp_t flag);
1588 	void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
1589 					void *cpu_addr, dma_addr_t dma_handle);
1590 	int (*scrub_device_mem)(struct hl_device *hdev);
1591 	int (*scrub_device_dram)(struct hl_device *hdev, u64 val);
1592 	void* (*get_int_queue_base)(struct hl_device *hdev, u32 queue_id,
1593 				dma_addr_t *dma_handle, u16 *queue_len);
1594 	int (*test_queues)(struct hl_device *hdev);
1595 	void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
1596 				gfp_t mem_flags, dma_addr_t *dma_handle);
1597 	void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
1598 				dma_addr_t dma_addr);
1599 	void* (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev,
1600 				size_t size, dma_addr_t *dma_handle);
1601 	void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,
1602 				size_t size, void *vaddr);
1603 	void (*asic_dma_unmap_single)(struct hl_device *hdev,
1604 				dma_addr_t dma_addr, int len,
1605 				enum dma_data_direction dir);
1606 	dma_addr_t (*asic_dma_map_single)(struct hl_device *hdev,
1607 				void *addr, int len,
1608 				enum dma_data_direction dir);
1609 	void (*hl_dma_unmap_sgtable)(struct hl_device *hdev,
1610 				struct sg_table *sgt,
1611 				enum dma_data_direction dir);
1612 	int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
1613 	int (*asic_dma_map_sgtable)(struct hl_device *hdev, struct sg_table *sgt,
1614 				enum dma_data_direction dir);
1615 	void (*add_end_of_cb_packets)(struct hl_device *hdev,
1616 					void *kernel_address, u32 len,
1617 					u32 original_len,
1618 					u64 cq_addr, u32 cq_val, u32 msix_num,
1619 					bool eb);
1620 	void (*update_eq_ci)(struct hl_device *hdev, u32 val);
1621 	int (*context_switch)(struct hl_device *hdev, u32 asid);
1622 	void (*restore_phase_topology)(struct hl_device *hdev);
1623 	int (*debugfs_read_dma)(struct hl_device *hdev, u64 addr, u32 size,
1624 				void *blob_addr);
1625 	void (*add_device_attr)(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp,
1626 				struct attribute_group *dev_vrm_attr_grp);
1627 	void (*handle_eqe)(struct hl_device *hdev,
1628 				struct hl_eq_entry *eq_entry);
1629 	void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
1630 				u32 *size);
1631 	u64 (*read_pte)(struct hl_device *hdev, u64 addr);
1632 	void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
1633 	int (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard,
1634 					u32 flags);
1635 	int (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
1636 				u32 flags, u32 asid, u64 va, u64 size);
1637 	int (*mmu_prefetch_cache_range)(struct hl_ctx *ctx, u32 flags, u32 asid, u64 va, u64 size);
1638 	int (*send_heartbeat)(struct hl_device *hdev);
1639 	int (*debug_coresight)(struct hl_device *hdev, struct hl_ctx *ctx, void *data);
1640 	bool (*is_device_idle)(struct hl_device *hdev, u64 *mask_arr, u8 mask_len,
1641 				struct engines_data *e);
1642 	int (*compute_reset_late_init)(struct hl_device *hdev);
1643 	void (*hw_queues_lock)(struct hl_device *hdev);
1644 	void (*hw_queues_unlock)(struct hl_device *hdev);
1645 	u32 (*get_pci_id)(struct hl_device *hdev);
1646 	int (*get_eeprom_data)(struct hl_device *hdev, void *data, size_t max_size);
1647 	int (*get_monitor_dump)(struct hl_device *hdev, void *data);
1648 	int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
1649 				u16 len, u32 timeout, u64 *result);
1650 	int (*pci_bars_map)(struct hl_device *hdev);
1651 	int (*init_iatu)(struct hl_device *hdev);
1652 	u32 (*rreg)(struct hl_device *hdev, u32 reg);
1653 	void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
1654 	void (*halt_coresight)(struct hl_device *hdev, struct hl_ctx *ctx);
1655 	int (*ctx_init)(struct hl_ctx *ctx);
1656 	void (*ctx_fini)(struct hl_ctx *ctx);
1657 	int (*pre_schedule_cs)(struct hl_cs *cs);
1658 	u32 (*get_queue_id_for_cq)(struct hl_device *hdev, u32 cq_idx);
1659 	int (*load_firmware_to_device)(struct hl_device *hdev);
1660 	int (*load_boot_fit_to_device)(struct hl_device *hdev);
1661 	u32 (*get_signal_cb_size)(struct hl_device *hdev);
1662 	u32 (*get_wait_cb_size)(struct hl_device *hdev);
1663 	u32 (*gen_signal_cb)(struct hl_device *hdev, void *data, u16 sob_id,
1664 			u32 size, bool eb);
1665 	u32 (*gen_wait_cb)(struct hl_device *hdev,
1666 			struct hl_gen_wait_properties *prop);
1667 	void (*reset_sob)(struct hl_device *hdev, void *data);
1668 	void (*reset_sob_group)(struct hl_device *hdev, u16 sob_group);
1669 	u64 (*get_device_time)(struct hl_device *hdev);
1670 	void (*pb_print_security_errors)(struct hl_device *hdev,
1671 			u32 block_addr, u32 cause, u32 offended_addr);
1672 	int (*collective_wait_init_cs)(struct hl_cs *cs);
1673 	int (*collective_wait_create_jobs)(struct hl_device *hdev,
1674 			struct hl_ctx *ctx, struct hl_cs *cs,
1675 			u32 wait_queue_id, u32 collective_engine_id,
1676 			u32 encaps_signal_offset);
1677 	u32 (*get_dec_base_addr)(struct hl_device *hdev, u32 core_id);
1678 	u64 (*scramble_addr)(struct hl_device *hdev, u64 addr);
1679 	u64 (*descramble_addr)(struct hl_device *hdev, u64 addr);
1680 	void (*ack_protection_bits_errors)(struct hl_device *hdev);
1681 	int (*get_hw_block_id)(struct hl_device *hdev, u64 block_addr,
1682 				u32 *block_size, u32 *block_id);
1683 	int (*hw_block_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
1684 			u32 block_id, u32 block_size);
1685 	void (*enable_events_from_fw)(struct hl_device *hdev);
1686 	int (*ack_mmu_errors)(struct hl_device *hdev, u64 mmu_cap_mask);
1687 	void (*get_msi_info)(__le32 *table);
1688 	int (*map_pll_idx_to_fw_idx)(u32 pll_idx);
1689 	void (*init_firmware_preload_params)(struct hl_device *hdev);
1690 	void (*init_firmware_loader)(struct hl_device *hdev);
1691 	void (*init_cpu_scrambler_dram)(struct hl_device *hdev);
1692 	void (*state_dump_init)(struct hl_device *hdev);
1693 	u32 (*get_sob_addr)(struct hl_device *hdev, u32 sob_id);
1694 	void (*set_pci_memory_regions)(struct hl_device *hdev);
1695 	u32* (*get_stream_master_qid_arr)(void);
1696 	void (*check_if_razwi_happened)(struct hl_device *hdev);
1697 	int (*mmu_get_real_page_size)(struct hl_device *hdev, struct hl_mmu_properties *mmu_prop,
1698 					u32 page_size, u32 *real_page_size, bool is_dram_addr);
1699 	int (*access_dev_mem)(struct hl_device *hdev, enum pci_region region_type,
1700 				u64 addr, u64 *val, enum debugfs_access_type acc_type);
1701 	u64 (*set_dram_bar_base)(struct hl_device *hdev, u64 addr);
1702 	int (*set_engine_cores)(struct hl_device *hdev, u32 *core_ids,
1703 					u32 num_cores, u32 core_command);
1704 	int (*send_device_activity)(struct hl_device *hdev, bool open);
1705 	int (*set_dram_properties)(struct hl_device *hdev);
1706 	int (*set_binning_masks)(struct hl_device *hdev);
1707 };
1708 
1709 
1710 /*
1711  * CONTEXTS
1712  */
1713 
1714 #define HL_KERNEL_ASID_ID	0
1715 
1716 /**
1717  * enum hl_va_range_type - virtual address range type.
1718  * @HL_VA_RANGE_TYPE_HOST: range type of host pages
1719  * @HL_VA_RANGE_TYPE_HOST_HUGE: range type of host huge pages
1720  * @HL_VA_RANGE_TYPE_DRAM: range type of dram pages
1721  */
1722 enum hl_va_range_type {
1723 	HL_VA_RANGE_TYPE_HOST,
1724 	HL_VA_RANGE_TYPE_HOST_HUGE,
1725 	HL_VA_RANGE_TYPE_DRAM,
1726 	HL_VA_RANGE_TYPE_MAX
1727 };
1728 
1729 /**
1730  * struct hl_va_range - virtual addresses range.
1731  * @lock: protects the virtual addresses list.
1732  * @list: list of virtual addresses blocks available for mappings.
1733  * @start_addr: range start address.
1734  * @end_addr: range end address.
1735  * @page_size: page size of this va range.
1736  */
1737 struct hl_va_range {
1738 	struct mutex		lock;
1739 	struct list_head	list;
1740 	u64			start_addr;
1741 	u64			end_addr;
1742 	u32			page_size;
1743 };
1744 
1745 /**
1746  * struct hl_cs_counters_atomic - command submission counters
1747  * @out_of_mem_drop_cnt: dropped due to memory allocation issue
1748  * @parsing_drop_cnt: dropped due to error in packet parsing
1749  * @queue_full_drop_cnt: dropped due to queue full
1750  * @device_in_reset_drop_cnt: dropped due to device in reset
1751  * @max_cs_in_flight_drop_cnt: dropped due to maximum CS in-flight
1752  * @validation_drop_cnt: dropped due to error in validation
1753  */
1754 struct hl_cs_counters_atomic {
1755 	atomic64_t out_of_mem_drop_cnt;
1756 	atomic64_t parsing_drop_cnt;
1757 	atomic64_t queue_full_drop_cnt;
1758 	atomic64_t device_in_reset_drop_cnt;
1759 	atomic64_t max_cs_in_flight_drop_cnt;
1760 	atomic64_t validation_drop_cnt;
1761 };
1762 
1763 /**
1764  * struct hl_dmabuf_priv - a dma-buf private object.
1765  * @dmabuf: pointer to dma-buf object.
1766  * @ctx: pointer to the dma-buf owner's context.
1767  * @phys_pg_pack: pointer to physical page pack if the dma-buf was exported
1768  *                where virtual memory is supported.
1769  * @memhash_hnode: pointer to the memhash node. this object holds the export count.
1770  * @device_address: physical address of the device's memory. Relevant only
1771  *                  if phys_pg_pack is NULL (dma-buf was exported from address).
1772  *                  The total size can be taken from the dmabuf object.
1773  */
1774 struct hl_dmabuf_priv {
1775 	struct dma_buf			*dmabuf;
1776 	struct hl_ctx			*ctx;
1777 	struct hl_vm_phys_pg_pack	*phys_pg_pack;
1778 	struct hl_vm_hash_node		*memhash_hnode;
1779 	uint64_t			device_address;
1780 };
1781 
1782 #define HL_CS_OUTCOME_HISTORY_LEN 256
1783 
1784 /**
1785  * struct hl_cs_outcome - represents a single completed CS outcome
1786  * @list_link: link to either container's used list or free list
1787  * @map_link: list to the container hash map
1788  * @ts: completion ts
1789  * @seq: the original cs sequence
1790  * @error: error code cs completed with, if any
1791  */
1792 struct hl_cs_outcome {
1793 	struct list_head list_link;
1794 	struct hlist_node map_link;
1795 	ktime_t ts;
1796 	u64 seq;
1797 	int error;
1798 };
1799 
1800 /**
1801  * struct hl_cs_outcome_store - represents a limited store of completed CS outcomes
1802  * @outcome_map: index of completed CS searchable by sequence number
1803  * @used_list: list of outcome objects currently in use
1804  * @free_list: list of outcome objects currently not in use
1805  * @nodes_pool: a static pool of pre-allocated outcome objects
1806  * @db_lock: any operation on the store must take this lock
1807  */
1808 struct hl_cs_outcome_store {
1809 	DECLARE_HASHTABLE(outcome_map, 8);
1810 	struct list_head used_list;
1811 	struct list_head free_list;
1812 	struct hl_cs_outcome nodes_pool[HL_CS_OUTCOME_HISTORY_LEN];
1813 	spinlock_t db_lock;
1814 };
1815 
1816 /**
1817  * struct hl_ctx - user/kernel context.
1818  * @mem_hash: holds mapping from virtual address to virtual memory area
1819  *		descriptor (hl_vm_phys_pg_list or hl_userptr).
1820  * @mmu_shadow_hash: holds a mapping from shadow address to pgt_info structure.
1821  * @hr_mmu_phys_hash: if host-resident MMU is used, holds a mapping from
1822  *                    MMU-hop-page physical address to its host-resident
1823  *                    pgt_info structure.
1824  * @hpriv: pointer to the private (Kernel Driver) data of the process (fd).
1825  * @hdev: pointer to the device structure.
1826  * @refcount: reference counter for the context. Context is released only when
1827  *		this hits 0l. It is incremented on CS and CS_WAIT.
1828  * @cs_pending: array of hl fence objects representing pending CS.
1829  * @outcome_store: storage data structure used to remember outcomes of completed
1830  *                 command submissions for a long time after CS id wraparound.
1831  * @va_range: holds available virtual addresses for host and dram mappings.
1832  * @mem_hash_lock: protects the mem_hash.
1833  * @hw_block_list_lock: protects the HW block memory list.
1834  * @debugfs_list: node in debugfs list of contexts.
1835  * @hw_block_mem_list: list of HW block virtual mapped addresses.
1836  * @cs_counters: context command submission counters.
1837  * @cb_va_pool: device VA pool for command buffers which are mapped to the
1838  *              device's MMU.
1839  * @sig_mgr: encaps signals handle manager.
1840  * @cb_va_pool_base: the base address for the device VA pool
1841  * @cs_sequence: sequence number for CS. Value is assigned to a CS and passed
1842  *			to user so user could inquire about CS. It is used as
1843  *			index to cs_pending array.
1844  * @dram_default_hops: array that holds all hops addresses needed for default
1845  *                     DRAM mapping.
1846  * @cs_lock: spinlock to protect cs_sequence.
1847  * @dram_phys_mem: amount of used physical DRAM memory by this context.
1848  * @thread_ctx_switch_token: token to prevent multiple threads of the same
1849  *				context	from running the context switch phase.
1850  *				Only a single thread should run it.
1851  * @thread_ctx_switch_wait_token: token to prevent the threads that didn't run
1852  *				the context switch phase from moving to their
1853  *				execution phase before the context switch phase
1854  *				has finished.
1855  * @asid: context's unique address space ID in the device's MMU.
1856  * @handle: context's opaque handle for user
1857  */
1858 struct hl_ctx {
1859 	DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
1860 	DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
1861 	DECLARE_HASHTABLE(hr_mmu_phys_hash, MMU_HASH_TABLE_BITS);
1862 	struct hl_fpriv			*hpriv;
1863 	struct hl_device		*hdev;
1864 	struct kref			refcount;
1865 	struct hl_fence			**cs_pending;
1866 	struct hl_cs_outcome_store	outcome_store;
1867 	struct hl_va_range		*va_range[HL_VA_RANGE_TYPE_MAX];
1868 	struct mutex			mem_hash_lock;
1869 	struct mutex			hw_block_list_lock;
1870 	struct list_head		debugfs_list;
1871 	struct list_head		hw_block_mem_list;
1872 	struct hl_cs_counters_atomic	cs_counters;
1873 	struct gen_pool			*cb_va_pool;
1874 	struct hl_encaps_signals_mgr	sig_mgr;
1875 	u64				cb_va_pool_base;
1876 	u64				cs_sequence;
1877 	u64				*dram_default_hops;
1878 	spinlock_t			cs_lock;
1879 	atomic64_t			dram_phys_mem;
1880 	atomic_t			thread_ctx_switch_token;
1881 	u32				thread_ctx_switch_wait_token;
1882 	u32				asid;
1883 	u32				handle;
1884 };
1885 
1886 /**
1887  * struct hl_ctx_mgr - for handling multiple contexts.
1888  * @lock: protects ctx_handles.
1889  * @handles: idr to hold all ctx handles.
1890  */
1891 struct hl_ctx_mgr {
1892 	struct mutex	lock;
1893 	struct idr	handles;
1894 };
1895 
1896 
1897 /*
1898  * COMMAND SUBMISSIONS
1899  */
1900 
1901 /**
1902  * struct hl_userptr - memory mapping chunk information
1903  * @vm_type: type of the VM.
1904  * @job_node: linked-list node for hanging the object on the Job's list.
1905  * @pages: pointer to struct page array
1906  * @npages: size of @pages array
1907  * @sgt: pointer to the scatter-gather table that holds the pages.
1908  * @dir: for DMA unmapping, the direction must be supplied, so save it.
1909  * @debugfs_list: node in debugfs list of command submissions.
1910  * @pid: the pid of the user process owning the memory
1911  * @addr: user-space virtual address of the start of the memory area.
1912  * @size: size of the memory area to pin & map.
1913  * @dma_mapped: true if the SG was mapped to DMA addresses, false otherwise.
1914  */
1915 struct hl_userptr {
1916 	enum vm_type		vm_type; /* must be first */
1917 	struct list_head	job_node;
1918 	struct page		**pages;
1919 	unsigned int		npages;
1920 	struct sg_table		*sgt;
1921 	enum dma_data_direction dir;
1922 	struct list_head	debugfs_list;
1923 	pid_t			pid;
1924 	u64			addr;
1925 	u64			size;
1926 	u8			dma_mapped;
1927 };
1928 
1929 /**
1930  * struct hl_cs - command submission.
1931  * @jobs_in_queue_cnt: per each queue, maintain counter of submitted jobs.
1932  * @ctx: the context this CS belongs to.
1933  * @job_list: list of the CS's jobs in the various queues.
1934  * @job_lock: spinlock for the CS's jobs list. Needed for free_job.
1935  * @refcount: reference counter for usage of the CS.
1936  * @fence: pointer to the fence object of this CS.
1937  * @signal_fence: pointer to the fence object of the signal CS (used by wait
1938  *                CS only).
1939  * @finish_work: workqueue object to run when CS is completed by H/W.
1940  * @work_tdr: delayed work node for TDR.
1941  * @mirror_node : node in device mirror list of command submissions.
1942  * @staged_cs_node: node in the staged cs list.
1943  * @debugfs_list: node in debugfs list of command submissions.
1944  * @encaps_sig_hdl: holds the encaps signals handle.
1945  * @sequence: the sequence number of this CS.
1946  * @staged_sequence: the sequence of the staged submission this CS is part of,
1947  *                   relevant only if staged_cs is set.
1948  * @timeout_jiffies: cs timeout in jiffies.
1949  * @submission_time_jiffies: submission time of the cs
1950  * @type: CS_TYPE_*.
1951  * @jobs_cnt: counter of submitted jobs on all queues.
1952  * @encaps_sig_hdl_id: encaps signals handle id, set for the first staged cs.
1953  * @completion_timestamp: timestamp of the last completed cs job.
1954  * @sob_addr_offset: sob offset from the configuration base address.
1955  * @initial_sob_count: count of completed signals in SOB before current submission of signal or
1956  *                     cs with encaps signals.
1957  * @submitted: true if CS was submitted to H/W.
1958  * @completed: true if CS was completed by device.
1959  * @timedout : true if CS was timedout.
1960  * @tdr_active: true if TDR was activated for this CS (to prevent
1961  *		double TDR activation).
1962  * @aborted: true if CS was aborted due to some device error.
1963  * @timestamp: true if a timestamp must be captured upon completion.
1964  * @staged_last: true if this is the last staged CS and needs completion.
1965  * @staged_first: true if this is the first staged CS and we need to receive
1966  *                timeout for this CS.
1967  * @staged_cs: true if this CS is part of a staged submission.
1968  * @skip_reset_on_timeout: true if we shall not reset the device in case
1969  *                         timeout occurs (debug scenario).
1970  * @encaps_signals: true if this CS has encaps reserved signals.
1971  */
1972 struct hl_cs {
1973 	u16			*jobs_in_queue_cnt;
1974 	struct hl_ctx		*ctx;
1975 	struct list_head	job_list;
1976 	spinlock_t		job_lock;
1977 	struct kref		refcount;
1978 	struct hl_fence		*fence;
1979 	struct hl_fence		*signal_fence;
1980 	struct work_struct	finish_work;
1981 	struct delayed_work	work_tdr;
1982 	struct list_head	mirror_node;
1983 	struct list_head	staged_cs_node;
1984 	struct list_head	debugfs_list;
1985 	struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
1986 	ktime_t			completion_timestamp;
1987 	u64			sequence;
1988 	u64			staged_sequence;
1989 	u64			timeout_jiffies;
1990 	u64			submission_time_jiffies;
1991 	enum hl_cs_type		type;
1992 	u32			jobs_cnt;
1993 	u32			encaps_sig_hdl_id;
1994 	u32			sob_addr_offset;
1995 	u16			initial_sob_count;
1996 	u8			submitted;
1997 	u8			completed;
1998 	u8			timedout;
1999 	u8			tdr_active;
2000 	u8			aborted;
2001 	u8			timestamp;
2002 	u8			staged_last;
2003 	u8			staged_first;
2004 	u8			staged_cs;
2005 	u8			skip_reset_on_timeout;
2006 	u8			encaps_signals;
2007 };
2008 
2009 /**
2010  * struct hl_cs_job - command submission job.
2011  * @cs_node: the node to hang on the CS jobs list.
2012  * @cs: the CS this job belongs to.
2013  * @user_cb: the CB we got from the user.
2014  * @patched_cb: in case of patching, this is internal CB which is submitted on
2015  *		the queue instead of the CB we got from the IOCTL.
2016  * @finish_work: workqueue object to run when job is completed.
2017  * @userptr_list: linked-list of userptr mappings that belong to this job and
2018  *			wait for completion.
2019  * @debugfs_list: node in debugfs list of command submission jobs.
2020  * @refcount: reference counter for usage of the CS job.
2021  * @queue_type: the type of the H/W queue this job is submitted to.
2022  * @timestamp: timestamp upon job completion
2023  * @id: the id of this job inside a CS.
2024  * @hw_queue_id: the id of the H/W queue this job is submitted to.
2025  * @user_cb_size: the actual size of the CB we got from the user.
2026  * @job_cb_size: the actual size of the CB that we put on the queue.
2027  * @encaps_sig_wait_offset: encapsulated signals offset, which allow user
2028  *                          to wait on part of the reserved signals.
2029  * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
2030  *                          handle to a kernel-allocated CB object, false
2031  *                          otherwise (SRAM/DRAM/host address).
2032  * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
2033  *                    info is needed later, when adding the 2xMSG_PROT at the
2034  *                    end of the JOB, to know which barriers to put in the
2035  *                    MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
2036  *                    have streams so the engine can't be busy by another
2037  *                    stream.
2038  */
2039 struct hl_cs_job {
2040 	struct list_head	cs_node;
2041 	struct hl_cs		*cs;
2042 	struct hl_cb		*user_cb;
2043 	struct hl_cb		*patched_cb;
2044 	struct work_struct	finish_work;
2045 	struct list_head	userptr_list;
2046 	struct list_head	debugfs_list;
2047 	struct kref		refcount;
2048 	enum hl_queue_type	queue_type;
2049 	ktime_t			timestamp;
2050 	u32			id;
2051 	u32			hw_queue_id;
2052 	u32			user_cb_size;
2053 	u32			job_cb_size;
2054 	u32			encaps_sig_wait_offset;
2055 	u8			is_kernel_allocated_cb;
2056 	u8			contains_dma_pkt;
2057 };
2058 
2059 /**
2060  * struct hl_cs_parser - command submission parser properties.
2061  * @user_cb: the CB we got from the user.
2062  * @patched_cb: in case of patching, this is internal CB which is submitted on
2063  *		the queue instead of the CB we got from the IOCTL.
2064  * @job_userptr_list: linked-list of userptr mappings that belong to the related
2065  *			job and wait for completion.
2066  * @cs_sequence: the sequence number of the related CS.
2067  * @queue_type: the type of the H/W queue this job is submitted to.
2068  * @ctx_id: the ID of the context the related CS belongs to.
2069  * @hw_queue_id: the id of the H/W queue this job is submitted to.
2070  * @user_cb_size: the actual size of the CB we got from the user.
2071  * @patched_cb_size: the size of the CB after parsing.
2072  * @job_id: the id of the related job inside the related CS.
2073  * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
2074  *                          handle to a kernel-allocated CB object, false
2075  *                          otherwise (SRAM/DRAM/host address).
2076  * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
2077  *                    info is needed later, when adding the 2xMSG_PROT at the
2078  *                    end of the JOB, to know which barriers to put in the
2079  *                    MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
2080  *                    have streams so the engine can't be busy by another
2081  *                    stream.
2082  * @completion: true if we need completion for this CS.
2083  */
2084 struct hl_cs_parser {
2085 	struct hl_cb		*user_cb;
2086 	struct hl_cb		*patched_cb;
2087 	struct list_head	*job_userptr_list;
2088 	u64			cs_sequence;
2089 	enum hl_queue_type	queue_type;
2090 	u32			ctx_id;
2091 	u32			hw_queue_id;
2092 	u32			user_cb_size;
2093 	u32			patched_cb_size;
2094 	u8			job_id;
2095 	u8			is_kernel_allocated_cb;
2096 	u8			contains_dma_pkt;
2097 	u8			completion;
2098 };
2099 
2100 /*
2101  * MEMORY STRUCTURE
2102  */
2103 
2104 /**
2105  * struct hl_vm_hash_node - hash element from virtual address to virtual
2106  *				memory area descriptor (hl_vm_phys_pg_list or
2107  *				hl_userptr).
2108  * @node: node to hang on the hash table in context object.
2109  * @vaddr: key virtual address.
2110  * @handle: memory handle for device memory allocation.
2111  * @ptr: value pointer (hl_vm_phys_pg_list or hl_userptr).
2112  * @export_cnt: number of exports from within the VA block.
2113  */
2114 struct hl_vm_hash_node {
2115 	struct hlist_node	node;
2116 	u64			vaddr;
2117 	u64			handle;
2118 	void			*ptr;
2119 	int			export_cnt;
2120 };
2121 
2122 /**
2123  * struct hl_vm_hw_block_list_node - list element from user virtual address to
2124  *				HW block id.
2125  * @node: node to hang on the list in context object.
2126  * @ctx: the context this node belongs to.
2127  * @vaddr: virtual address of the HW block.
2128  * @block_size: size of the block.
2129  * @mapped_size: size of the block which is mapped. May change if partial un-mappings are done.
2130  * @id: HW block id (handle).
2131  */
2132 struct hl_vm_hw_block_list_node {
2133 	struct list_head	node;
2134 	struct hl_ctx		*ctx;
2135 	unsigned long		vaddr;
2136 	u32			block_size;
2137 	u32			mapped_size;
2138 	u32			id;
2139 };
2140 
2141 /**
2142  * struct hl_vm_phys_pg_pack - physical page pack.
2143  * @vm_type: describes the type of the virtual area descriptor.
2144  * @pages: the physical page array.
2145  * @npages: num physical pages in the pack.
2146  * @total_size: total size of all the pages in this list.
2147  * @exported_size: buffer exported size.
2148  * @node: used to attach to deletion list that is used when all the allocations are cleared
2149  *        at the teardown of the context.
2150  * @mapping_cnt: number of shared mappings.
2151  * @asid: the context related to this list.
2152  * @page_size: size of each page in the pack.
2153  * @flags: HL_MEM_* flags related to this list.
2154  * @handle: the provided handle related to this list.
2155  * @offset: offset from the first page.
2156  * @contiguous: is contiguous physical memory.
2157  * @created_from_userptr: is product of host virtual address.
2158  */
2159 struct hl_vm_phys_pg_pack {
2160 	enum vm_type		vm_type; /* must be first */
2161 	u64			*pages;
2162 	u64			npages;
2163 	u64			total_size;
2164 	u64			exported_size;
2165 	struct list_head	node;
2166 	atomic_t		mapping_cnt;
2167 	u32			asid;
2168 	u32			page_size;
2169 	u32			flags;
2170 	u32			handle;
2171 	u32			offset;
2172 	u8			contiguous;
2173 	u8			created_from_userptr;
2174 };
2175 
2176 /**
2177  * struct hl_vm_va_block - virtual range block information.
2178  * @node: node to hang on the virtual range list in context object.
2179  * @start: virtual range start address.
2180  * @end: virtual range end address.
2181  * @size: virtual range size.
2182  */
2183 struct hl_vm_va_block {
2184 	struct list_head	node;
2185 	u64			start;
2186 	u64			end;
2187 	u64			size;
2188 };
2189 
2190 /**
2191  * struct hl_vm - virtual memory manager for MMU.
2192  * @dram_pg_pool: pool for DRAM physical pages of 2MB.
2193  * @dram_pg_pool_refcount: reference counter for the pool usage.
2194  * @idr_lock: protects the phys_pg_list_handles.
2195  * @phys_pg_pack_handles: idr to hold all device allocations handles.
2196  * @init_done: whether initialization was done. We need this because VM
2197  *		initialization might be skipped during device initialization.
2198  */
2199 struct hl_vm {
2200 	struct gen_pool		*dram_pg_pool;
2201 	struct kref		dram_pg_pool_refcount;
2202 	spinlock_t		idr_lock;
2203 	struct idr		phys_pg_pack_handles;
2204 	u8			init_done;
2205 };
2206 
2207 
2208 /*
2209  * DEBUG, PROFILING STRUCTURE
2210  */
2211 
2212 /**
2213  * struct hl_debug_params - Coresight debug parameters.
2214  * @input: pointer to component specific input parameters.
2215  * @output: pointer to component specific output parameters.
2216  * @output_size: size of output buffer.
2217  * @reg_idx: relevant register ID.
2218  * @op: component operation to execute.
2219  * @enable: true if to enable component debugging, false otherwise.
2220  */
2221 struct hl_debug_params {
2222 	void *input;
2223 	void *output;
2224 	u32 output_size;
2225 	u32 reg_idx;
2226 	u32 op;
2227 	bool enable;
2228 };
2229 
2230 /**
2231  * struct hl_notifier_event - holds the notifier data structure
2232  * @eventfd: the event file descriptor to raise the notifications
2233  * @lock: mutex lock to protect the notifier data flows
2234  * @events_mask: indicates the bitmap events
2235  */
2236 struct hl_notifier_event {
2237 	struct eventfd_ctx	*eventfd;
2238 	struct mutex		lock;
2239 	u64			events_mask;
2240 };
2241 
2242 /*
2243  * FILE PRIVATE STRUCTURE
2244  */
2245 
2246 /**
2247  * struct hl_fpriv - process information stored in FD private data.
2248  * @hdev: habanalabs device structure.
2249  * @filp: pointer to the given file structure.
2250  * @taskpid: current process ID.
2251  * @ctx: current executing context. TODO: remove for multiple ctx per process
2252  * @ctx_mgr: context manager to handle multiple context for this FD.
2253  * @mem_mgr: manager descriptor for memory exportable via mmap
2254  * @notifier_event: notifier eventfd towards user process
2255  * @debugfs_list: list of relevant ASIC debugfs.
2256  * @dev_node: node in the device list of file private data
2257  * @refcount: number of related contexts.
2258  * @restore_phase_mutex: lock for context switch and restore phase.
2259  * @ctx_lock: protects the pointer to current executing context pointer. TODO: remove for multiple
2260  *            ctx per process.
2261  */
2262 struct hl_fpriv {
2263 	struct hl_device		*hdev;
2264 	struct file			*filp;
2265 	struct pid			*taskpid;
2266 	struct hl_ctx			*ctx;
2267 	struct hl_ctx_mgr		ctx_mgr;
2268 	struct hl_mem_mgr		mem_mgr;
2269 	struct hl_notifier_event	notifier_event;
2270 	struct list_head		debugfs_list;
2271 	struct list_head		dev_node;
2272 	struct kref			refcount;
2273 	struct mutex			restore_phase_mutex;
2274 	struct mutex			ctx_lock;
2275 };
2276 
2277 
2278 /*
2279  * DebugFS
2280  */
2281 
2282 /**
2283  * struct hl_info_list - debugfs file ops.
2284  * @name: file name.
2285  * @show: function to output information.
2286  * @write: function to write to the file.
2287  */
2288 struct hl_info_list {
2289 	const char	*name;
2290 	int		(*show)(struct seq_file *s, void *data);
2291 	ssize_t		(*write)(struct file *file, const char __user *buf,
2292 				size_t count, loff_t *f_pos);
2293 };
2294 
2295 /**
2296  * struct hl_debugfs_entry - debugfs dentry wrapper.
2297  * @info_ent: dentry related ops.
2298  * @dev_entry: ASIC specific debugfs manager.
2299  */
2300 struct hl_debugfs_entry {
2301 	const struct hl_info_list	*info_ent;
2302 	struct hl_dbg_device_entry	*dev_entry;
2303 };
2304 
2305 /**
2306  * struct hl_dbg_device_entry - ASIC specific debugfs manager.
2307  * @root: root dentry.
2308  * @hdev: habanalabs device structure.
2309  * @entry_arr: array of available hl_debugfs_entry.
2310  * @file_list: list of available debugfs files.
2311  * @file_mutex: protects file_list.
2312  * @cb_list: list of available CBs.
2313  * @cb_spinlock: protects cb_list.
2314  * @cs_list: list of available CSs.
2315  * @cs_spinlock: protects cs_list.
2316  * @cs_job_list: list of available CB jobs.
2317  * @cs_job_spinlock: protects cs_job_list.
2318  * @userptr_list: list of available userptrs (virtual memory chunk descriptor).
2319  * @userptr_spinlock: protects userptr_list.
2320  * @ctx_mem_hash_list: list of available contexts with MMU mappings.
2321  * @ctx_mem_hash_spinlock: protects cb_list.
2322  * @data_dma_blob_desc: data DMA descriptor of blob.
2323  * @mon_dump_blob_desc: monitor dump descriptor of blob.
2324  * @state_dump: data of the system states in case of a bad cs.
2325  * @state_dump_sem: protects state_dump.
2326  * @addr: next address to read/write from/to in read/write32.
2327  * @mmu_addr: next virtual address to translate to physical address in mmu_show.
2328  * @mmu_cap_mask: mmu hw capability mask, to be used in mmu_ack_error.
2329  * @userptr_lookup: the target user ptr to look up for on demand.
2330  * @mmu_asid: ASID to use while translating in mmu_show.
2331  * @state_dump_head: index of the latest state dump
2332  * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
2333  * @i2c_addr: generic u8 debugfs file for address value to use in i2c_data_read.
2334  * @i2c_reg: generic u8 debugfs file for register value to use in i2c_data_read.
2335  * @i2c_len: generic u8 debugfs file for length value to use in i2c_data_read.
2336  */
2337 struct hl_dbg_device_entry {
2338 	struct dentry			*root;
2339 	struct hl_device		*hdev;
2340 	struct hl_debugfs_entry		*entry_arr;
2341 	struct list_head		file_list;
2342 	struct mutex			file_mutex;
2343 	struct list_head		cb_list;
2344 	spinlock_t			cb_spinlock;
2345 	struct list_head		cs_list;
2346 	spinlock_t			cs_spinlock;
2347 	struct list_head		cs_job_list;
2348 	spinlock_t			cs_job_spinlock;
2349 	struct list_head		userptr_list;
2350 	spinlock_t			userptr_spinlock;
2351 	struct list_head		ctx_mem_hash_list;
2352 	spinlock_t			ctx_mem_hash_spinlock;
2353 	struct debugfs_blob_wrapper	data_dma_blob_desc;
2354 	struct debugfs_blob_wrapper	mon_dump_blob_desc;
2355 	char				*state_dump[HL_STATE_DUMP_HIST_LEN];
2356 	struct rw_semaphore		state_dump_sem;
2357 	u64				addr;
2358 	u64				mmu_addr;
2359 	u64				mmu_cap_mask;
2360 	u64				userptr_lookup;
2361 	u32				mmu_asid;
2362 	u32				state_dump_head;
2363 	u8				i2c_bus;
2364 	u8				i2c_addr;
2365 	u8				i2c_reg;
2366 	u8				i2c_len;
2367 };
2368 
2369 /**
2370  * struct hl_hw_obj_name_entry - single hw object name, member of
2371  * hl_state_dump_specs
2372  * @node: link to the containing hash table
2373  * @name: hw object name
2374  * @id: object identifier
2375  */
2376 struct hl_hw_obj_name_entry {
2377 	struct hlist_node	node;
2378 	const char		*name;
2379 	u32			id;
2380 };
2381 
2382 enum hl_state_dump_specs_props {
2383 	SP_SYNC_OBJ_BASE_ADDR,
2384 	SP_NEXT_SYNC_OBJ_ADDR,
2385 	SP_SYNC_OBJ_AMOUNT,
2386 	SP_MON_OBJ_WR_ADDR_LOW,
2387 	SP_MON_OBJ_WR_ADDR_HIGH,
2388 	SP_MON_OBJ_WR_DATA,
2389 	SP_MON_OBJ_ARM_DATA,
2390 	SP_MON_OBJ_STATUS,
2391 	SP_MONITORS_AMOUNT,
2392 	SP_TPC0_CMDQ,
2393 	SP_TPC0_CFG_SO,
2394 	SP_NEXT_TPC,
2395 	SP_MME_CMDQ,
2396 	SP_MME_CFG_SO,
2397 	SP_NEXT_MME,
2398 	SP_DMA_CMDQ,
2399 	SP_DMA_CFG_SO,
2400 	SP_DMA_QUEUES_OFFSET,
2401 	SP_NUM_OF_MME_ENGINES,
2402 	SP_SUB_MME_ENG_NUM,
2403 	SP_NUM_OF_DMA_ENGINES,
2404 	SP_NUM_OF_TPC_ENGINES,
2405 	SP_ENGINE_NUM_OF_QUEUES,
2406 	SP_ENGINE_NUM_OF_STREAMS,
2407 	SP_ENGINE_NUM_OF_FENCES,
2408 	SP_FENCE0_CNT_OFFSET,
2409 	SP_FENCE0_RDATA_OFFSET,
2410 	SP_CP_STS_OFFSET,
2411 	SP_NUM_CORES,
2412 
2413 	SP_MAX
2414 };
2415 
2416 enum hl_sync_engine_type {
2417 	ENGINE_TPC,
2418 	ENGINE_DMA,
2419 	ENGINE_MME,
2420 };
2421 
2422 /**
2423  * struct hl_mon_state_dump - represents a state dump of a single monitor
2424  * @id: monitor id
2425  * @wr_addr_low: address monitor will write to, low bits
2426  * @wr_addr_high: address monitor will write to, high bits
2427  * @wr_data: data monitor will write
2428  * @arm_data: register value containing monitor configuration
2429  * @status: monitor status
2430  */
2431 struct hl_mon_state_dump {
2432 	u32		id;
2433 	u32		wr_addr_low;
2434 	u32		wr_addr_high;
2435 	u32		wr_data;
2436 	u32		arm_data;
2437 	u32		status;
2438 };
2439 
2440 /**
2441  * struct hl_sync_to_engine_map_entry - sync object id to engine mapping entry
2442  * @engine_type: type of the engine
2443  * @engine_id: id of the engine
2444  * @sync_id: id of the sync object
2445  */
2446 struct hl_sync_to_engine_map_entry {
2447 	struct hlist_node		node;
2448 	enum hl_sync_engine_type	engine_type;
2449 	u32				engine_id;
2450 	u32				sync_id;
2451 };
2452 
2453 /**
2454  * struct hl_sync_to_engine_map - maps sync object id to associated engine id
2455  * @tb: hash table containing the mapping, each element is of type
2456  *      struct hl_sync_to_engine_map_entry
2457  */
2458 struct hl_sync_to_engine_map {
2459 	DECLARE_HASHTABLE(tb, SYNC_TO_ENGINE_HASH_TABLE_BITS);
2460 };
2461 
2462 /**
2463  * struct hl_state_dump_specs_funcs - virtual functions used by the state dump
2464  * @gen_sync_to_engine_map: generate a hash map from sync obj id to its engine
2465  * @print_single_monitor: format monitor data as string
2466  * @monitor_valid: return true if given monitor dump is valid
2467  * @print_fences_single_engine: format fences data as string
2468  */
2469 struct hl_state_dump_specs_funcs {
2470 	int (*gen_sync_to_engine_map)(struct hl_device *hdev,
2471 				struct hl_sync_to_engine_map *map);
2472 	int (*print_single_monitor)(char **buf, size_t *size, size_t *offset,
2473 				    struct hl_device *hdev,
2474 				    struct hl_mon_state_dump *mon);
2475 	int (*monitor_valid)(struct hl_mon_state_dump *mon);
2476 	int (*print_fences_single_engine)(struct hl_device *hdev,
2477 					u64 base_offset,
2478 					u64 status_base_offset,
2479 					enum hl_sync_engine_type engine_type,
2480 					u32 engine_id, char **buf,
2481 					size_t *size, size_t *offset);
2482 };
2483 
2484 /**
2485  * struct hl_state_dump_specs - defines ASIC known hw objects names
2486  * @so_id_to_str_tb: sync objects names index table
2487  * @monitor_id_to_str_tb: monitors names index table
2488  * @funcs: virtual functions used for state dump
2489  * @sync_namager_names: readable names for sync manager if available (ex: N_E)
2490  * @props: pointer to a per asic const props array required for state dump
2491  */
2492 struct hl_state_dump_specs {
2493 	DECLARE_HASHTABLE(so_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
2494 	DECLARE_HASHTABLE(monitor_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
2495 	struct hl_state_dump_specs_funcs	funcs;
2496 	const char * const			*sync_namager_names;
2497 	s64					*props;
2498 };
2499 
2500 
2501 /*
2502  * DEVICES
2503  */
2504 
2505 #define HL_STR_MAX	32
2506 
2507 #define HL_DEV_STS_MAX (HL_DEVICE_STATUS_LAST + 1)
2508 
2509 /* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
2510  * x16 cards. In extreme cases, there are hosts that can accommodate 16 cards.
2511  */
2512 #define HL_MAX_MINORS	256
2513 
2514 /*
2515  * Registers read & write functions.
2516  */
2517 
2518 u32 hl_rreg(struct hl_device *hdev, u32 reg);
2519 void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
2520 
2521 #define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
2522 #define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
2523 #define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n",	\
2524 			hdev->asic_funcs->rreg(hdev, (reg)))
2525 
2526 #define WREG32_P(reg, val, mask)				\
2527 	do {							\
2528 		u32 tmp_ = RREG32(reg);				\
2529 		tmp_ &= (mask);					\
2530 		tmp_ |= ((val) & ~(mask));			\
2531 		WREG32(reg, tmp_);				\
2532 	} while (0)
2533 #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
2534 #define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
2535 
2536 #define RMWREG32_SHIFTED(reg, val, mask) WREG32_P(reg, val, ~(mask))
2537 
2538 #define RMWREG32(reg, val, mask) RMWREG32_SHIFTED(reg, (val) << __ffs(mask), mask)
2539 
2540 #define RREG32_MASK(reg, mask) ((RREG32(reg) & mask) >> __ffs(mask))
2541 
2542 #define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
2543 #define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
2544 #define WREG32_FIELD(reg, offset, field, val)	\
2545 	WREG32(mm##reg + offset, (RREG32(mm##reg + offset) & \
2546 				~REG_FIELD_MASK(reg, field)) | \
2547 				(val) << REG_FIELD_SHIFT(reg, field))
2548 
2549 /* Timeout should be longer when working with simulator but cap the
2550  * increased timeout to some maximum
2551  */
2552 #define hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, elbi) \
2553 ({ \
2554 	ktime_t __timeout; \
2555 	u32 __elbi_read; \
2556 	int __rc = 0; \
2557 	if (hdev->pdev) \
2558 		__timeout = ktime_add_us(ktime_get(), timeout_us); \
2559 	else \
2560 		__timeout = ktime_add_us(ktime_get(),\
2561 				min((u64)(timeout_us * 10), \
2562 					(u64) HL_SIM_MAX_TIMEOUT_US)); \
2563 	might_sleep_if(sleep_us); \
2564 	for (;;) { \
2565 		if (elbi) { \
2566 			__rc = hl_pci_elbi_read(hdev, addr, &__elbi_read); \
2567 			if (__rc) \
2568 				break; \
2569 			(val) = __elbi_read; \
2570 		} else {\
2571 			(val) = RREG32(lower_32_bits(addr)); \
2572 		} \
2573 		if (cond) \
2574 			break; \
2575 		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2576 			if (elbi) { \
2577 				__rc = hl_pci_elbi_read(hdev, addr, &__elbi_read); \
2578 				if (__rc) \
2579 					break; \
2580 				(val) = __elbi_read; \
2581 			} else {\
2582 				(val) = RREG32(lower_32_bits(addr)); \
2583 			} \
2584 			break; \
2585 		} \
2586 		if (sleep_us) \
2587 			usleep_range((sleep_us >> 2) + 1, sleep_us); \
2588 	} \
2589 	__rc ? __rc : ((cond) ? 0 : -ETIMEDOUT); \
2590 })
2591 
2592 #define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
2593 		hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, false)
2594 
2595 #define hl_poll_timeout_elbi(hdev, addr, val, cond, sleep_us, timeout_us) \
2596 		hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, true)
2597 
2598 /*
2599  * poll array of register addresses.
2600  * condition is satisfied if all registers values match the expected value.
2601  * once some register in the array satisfies the condition it will not be polled again,
2602  * this is done both for efficiency and due to some registers are "clear on read".
2603  * TODO: use read from PCI bar in other places in the code (SW-91406)
2604  */
2605 #define hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
2606 						timeout_us, elbi) \
2607 ({ \
2608 	ktime_t __timeout; \
2609 	u64 __elem_bitmask; \
2610 	u32 __read_val;	\
2611 	u8 __arr_idx;	\
2612 	int __rc = 0; \
2613 	\
2614 	if (hdev->pdev) \
2615 		__timeout = ktime_add_us(ktime_get(), timeout_us); \
2616 	else \
2617 		__timeout = ktime_add_us(ktime_get(),\
2618 				min(((u64)timeout_us * 10), \
2619 					(u64) HL_SIM_MAX_TIMEOUT_US)); \
2620 	\
2621 	might_sleep_if(sleep_us); \
2622 	if (arr_size >= 64) \
2623 		__rc = -EINVAL; \
2624 	else \
2625 		__elem_bitmask = BIT_ULL(arr_size) - 1; \
2626 	for (;;) { \
2627 		if (__rc) \
2628 			break; \
2629 		for (__arr_idx = 0; __arr_idx < (arr_size); __arr_idx++) {	\
2630 			if (!(__elem_bitmask & BIT_ULL(__arr_idx)))	\
2631 				continue;	\
2632 			if (elbi) { \
2633 				__rc = hl_pci_elbi_read(hdev, (addr_arr)[__arr_idx], &__read_val); \
2634 				if (__rc) \
2635 					break; \
2636 			} else { \
2637 				__read_val = RREG32(lower_32_bits(addr_arr[__arr_idx])); \
2638 			} \
2639 			if (__read_val == (expected_val))	\
2640 				__elem_bitmask &= ~BIT_ULL(__arr_idx);	\
2641 		}	\
2642 		if (__rc || (__elem_bitmask == 0)) \
2643 			break; \
2644 		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) \
2645 			break; \
2646 		if (sleep_us) \
2647 			usleep_range((sleep_us >> 2) + 1, sleep_us); \
2648 	} \
2649 	__rc ? __rc : ((__elem_bitmask == 0) ? 0 : -ETIMEDOUT); \
2650 })
2651 
2652 #define hl_poll_reg_array_timeout(hdev, addr_arr, arr_size, expected_val, sleep_us, \
2653 					timeout_us) \
2654 	hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
2655 						timeout_us, false)
2656 
2657 #define hl_poll_reg_array_timeout_elbi(hdev, addr_arr, arr_size, expected_val, sleep_us, \
2658 					timeout_us) \
2659 	hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
2660 						timeout_us, true)
2661 
2662 /*
2663  * address in this macro points always to a memory location in the
2664  * host's (server's) memory. That location is updated asynchronously
2665  * either by the direct access of the device or by another core.
2666  *
2667  * To work both in LE and BE architectures, we need to distinguish between the
2668  * two states (device or another core updates the memory location). Therefore,
2669  * if mem_written_by_device is true, the host memory being polled will be
2670  * updated directly by the device. If false, the host memory being polled will
2671  * be updated by host CPU. Required so host knows whether or not the memory
2672  * might need to be byte-swapped before returning value to caller.
2673  */
2674 #define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
2675 				mem_written_by_device) \
2676 ({ \
2677 	ktime_t __timeout; \
2678 	if (hdev->pdev) \
2679 		__timeout = ktime_add_us(ktime_get(), timeout_us); \
2680 	else \
2681 		__timeout = ktime_add_us(ktime_get(),\
2682 				min((u64)(timeout_us * 100), \
2683 					(u64) HL_SIM_MAX_TIMEOUT_US)); \
2684 	might_sleep_if(sleep_us); \
2685 	for (;;) { \
2686 		/* Verify we read updates done by other cores or by device */ \
2687 		mb(); \
2688 		(val) = *((u32 *)(addr)); \
2689 		if (mem_written_by_device) \
2690 			(val) = le32_to_cpu(*(__le32 *) &(val)); \
2691 		if (cond) \
2692 			break; \
2693 		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2694 			(val) = *((u32 *)(addr)); \
2695 			if (mem_written_by_device) \
2696 				(val) = le32_to_cpu(*(__le32 *) &(val)); \
2697 			break; \
2698 		} \
2699 		if (sleep_us) \
2700 			usleep_range((sleep_us >> 2) + 1, sleep_us); \
2701 	} \
2702 	(cond) ? 0 : -ETIMEDOUT; \
2703 })
2704 
2705 #define HL_USR_MAPPED_BLK_INIT(blk, base, sz) \
2706 ({ \
2707 	struct user_mapped_block *p = blk; \
2708 \
2709 	p->address = base; \
2710 	p->size = sz; \
2711 })
2712 
2713 #define HL_USR_INTR_STRUCT_INIT(usr_intr, hdev, intr_id, intr_type) \
2714 ({ \
2715 	usr_intr.hdev = hdev; \
2716 	usr_intr.interrupt_id = intr_id; \
2717 	usr_intr.type = intr_type; \
2718 	INIT_LIST_HEAD(&usr_intr.wait_list_head); \
2719 	spin_lock_init(&usr_intr.wait_list_lock); \
2720 })
2721 
2722 struct hwmon_chip_info;
2723 
2724 /**
2725  * struct hl_device_reset_work - reset work wrapper.
2726  * @reset_work: reset work to be done.
2727  * @hdev: habanalabs device structure.
2728  * @flags: reset flags.
2729  */
2730 struct hl_device_reset_work {
2731 	struct delayed_work	reset_work;
2732 	struct hl_device	*hdev;
2733 	u32			flags;
2734 };
2735 
2736 /**
2737  * struct hl_mmu_hr_pgt_priv - used for holding per-device mmu host-resident
2738  * page-table internal information.
2739  * @mmu_pgt_pool: pool of page tables used by a host-resident MMU for
2740  *                allocating hops.
2741  * @mmu_asid_hop0: per-ASID array of host-resident hop0 tables.
2742  */
2743 struct hl_mmu_hr_priv {
2744 	struct gen_pool	*mmu_pgt_pool;
2745 	struct pgt_info	*mmu_asid_hop0;
2746 };
2747 
2748 /**
2749  * struct hl_mmu_dr_pgt_priv - used for holding per-device mmu device-resident
2750  * page-table internal information.
2751  * @mmu_pgt_pool: pool of page tables used by MMU for allocating hops.
2752  * @mmu_shadow_hop0: shadow array of hop0 tables.
2753  */
2754 struct hl_mmu_dr_priv {
2755 	struct gen_pool *mmu_pgt_pool;
2756 	void *mmu_shadow_hop0;
2757 };
2758 
2759 /**
2760  * struct hl_mmu_priv - used for holding per-device mmu internal information.
2761  * @dr: information on the device-resident MMU, when exists.
2762  * @hr: information on the host-resident MMU, when exists.
2763  */
2764 struct hl_mmu_priv {
2765 	struct hl_mmu_dr_priv dr;
2766 	struct hl_mmu_hr_priv hr;
2767 };
2768 
2769 /**
2770  * struct hl_mmu_per_hop_info - A structure describing one TLB HOP and its entry
2771  *                that was created in order to translate a virtual address to a
2772  *                physical one.
2773  * @hop_addr: The address of the hop.
2774  * @hop_pte_addr: The address of the hop entry.
2775  * @hop_pte_val: The value in the hop entry.
2776  */
2777 struct hl_mmu_per_hop_info {
2778 	u64 hop_addr;
2779 	u64 hop_pte_addr;
2780 	u64 hop_pte_val;
2781 };
2782 
2783 /**
2784  * struct hl_mmu_hop_info - A structure describing the TLB hops and their
2785  * hop-entries that were created in order to translate a virtual address to a
2786  * physical one.
2787  * @scrambled_vaddr: The value of the virtual address after scrambling. This
2788  *                   address replaces the original virtual-address when mapped
2789  *                   in the MMU tables.
2790  * @unscrambled_paddr: The un-scrambled physical address.
2791  * @hop_info: Array holding the per-hop information used for the translation.
2792  * @used_hops: The number of hops used for the translation.
2793  * @range_type: virtual address range type.
2794  */
2795 struct hl_mmu_hop_info {
2796 	u64 scrambled_vaddr;
2797 	u64 unscrambled_paddr;
2798 	struct hl_mmu_per_hop_info hop_info[MMU_ARCH_6_HOPS];
2799 	u32 used_hops;
2800 	enum hl_va_range_type range_type;
2801 };
2802 
2803 /**
2804  * struct hl_hr_mmu_funcs - Device related host resident MMU functions.
2805  * @get_hop0_pgt_info: get page table info structure for HOP0.
2806  * @get_pgt_info: get page table info structure for HOP other than HOP0.
2807  * @add_pgt_info: add page table info structure to hash.
2808  * @get_tlb_mapping_params: get mapping parameters needed for getting TLB info for specific mapping.
2809  */
2810 struct hl_hr_mmu_funcs {
2811 	struct pgt_info *(*get_hop0_pgt_info)(struct hl_ctx *ctx);
2812 	struct pgt_info *(*get_pgt_info)(struct hl_ctx *ctx, u64 phys_hop_addr);
2813 	void (*add_pgt_info)(struct hl_ctx *ctx, struct pgt_info *pgt_info, dma_addr_t phys_addr);
2814 	int (*get_tlb_mapping_params)(struct hl_device *hdev, struct hl_mmu_properties **mmu_prop,
2815 								struct hl_mmu_hop_info *hops,
2816 								u64 virt_addr, bool *is_huge);
2817 };
2818 
2819 /**
2820  * struct hl_mmu_funcs - Device related MMU functions.
2821  * @init: initialize the MMU module.
2822  * @fini: release the MMU module.
2823  * @ctx_init: Initialize a context for using the MMU module.
2824  * @ctx_fini: disable a ctx from using the mmu module.
2825  * @map: maps a virtual address to physical address for a context.
2826  * @unmap: unmap a virtual address of a context.
2827  * @flush: flush all writes from all cores to reach device MMU.
2828  * @swap_out: marks all mapping of the given context as swapped out.
2829  * @swap_in: marks all mapping of the given context as swapped in.
2830  * @get_tlb_info: returns the list of hops and hop-entries used that were
2831  *                created in order to translate the giver virtual address to a
2832  *                physical one.
2833  * @hr_funcs: functions specific to host resident MMU.
2834  */
2835 struct hl_mmu_funcs {
2836 	int (*init)(struct hl_device *hdev);
2837 	void (*fini)(struct hl_device *hdev);
2838 	int (*ctx_init)(struct hl_ctx *ctx);
2839 	void (*ctx_fini)(struct hl_ctx *ctx);
2840 	int (*map)(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size,
2841 				bool is_dram_addr);
2842 	int (*unmap)(struct hl_ctx *ctx, u64 virt_addr, bool is_dram_addr);
2843 	void (*flush)(struct hl_ctx *ctx);
2844 	void (*swap_out)(struct hl_ctx *ctx);
2845 	void (*swap_in)(struct hl_ctx *ctx);
2846 	int (*get_tlb_info)(struct hl_ctx *ctx, u64 virt_addr, struct hl_mmu_hop_info *hops);
2847 	struct hl_hr_mmu_funcs hr_funcs;
2848 };
2849 
2850 /**
2851  * struct hl_prefetch_work - prefetch work structure handler
2852  * @prefetch_work: actual work struct.
2853  * @ctx: compute context.
2854  * @va: virtual address to pre-fetch.
2855  * @size: pre-fetch size.
2856  * @flags: operation flags.
2857  * @asid: ASID for maintenance operation.
2858  */
2859 struct hl_prefetch_work {
2860 	struct work_struct	prefetch_work;
2861 	struct hl_ctx		*ctx;
2862 	u64			va;
2863 	u64			size;
2864 	u32			flags;
2865 	u32			asid;
2866 };
2867 
2868 /*
2869  * number of user contexts allowed to call wait_for_multi_cs ioctl in
2870  * parallel
2871  */
2872 #define MULTI_CS_MAX_USER_CTX	2
2873 
2874 /**
2875  * struct multi_cs_completion - multi CS wait completion.
2876  * @completion: completion of any of the CS in the list
2877  * @lock: spinlock for the completion structure
2878  * @timestamp: timestamp for the multi-CS completion
2879  * @stream_master_qid_map: bitmap of all stream masters on which the multi-CS
2880  *                        is waiting
2881  * @used: 1 if in use, otherwise 0
2882  */
2883 struct multi_cs_completion {
2884 	struct completion	completion;
2885 	spinlock_t		lock;
2886 	s64			timestamp;
2887 	u32			stream_master_qid_map;
2888 	u8			used;
2889 };
2890 
2891 /**
2892  * struct multi_cs_data - internal data for multi CS call
2893  * @ctx: pointer to the context structure
2894  * @fence_arr: array of fences of all CSs
2895  * @seq_arr: array of CS sequence numbers
2896  * @timeout_jiffies: timeout in jiffies for waiting for CS to complete
2897  * @timestamp: timestamp of first completed CS
2898  * @wait_status: wait for CS status
2899  * @completion_bitmap: bitmap of completed CSs (1- completed, otherwise 0)
2900  * @arr_len: fence_arr and seq_arr array length
2901  * @gone_cs: indication of gone CS (1- there was gone CS, otherwise 0)
2902  * @update_ts: update timestamp. 1- update the timestamp, otherwise 0.
2903  */
2904 struct multi_cs_data {
2905 	struct hl_ctx	*ctx;
2906 	struct hl_fence	**fence_arr;
2907 	u64		*seq_arr;
2908 	s64		timeout_jiffies;
2909 	s64		timestamp;
2910 	long		wait_status;
2911 	u32		completion_bitmap;
2912 	u8		arr_len;
2913 	u8		gone_cs;
2914 	u8		update_ts;
2915 };
2916 
2917 /**
2918  * struct hl_clk_throttle_timestamp - current/last clock throttling timestamp
2919  * @start: timestamp taken when 'start' event is received in driver
2920  * @end: timestamp taken when 'end' event is received in driver
2921  */
2922 struct hl_clk_throttle_timestamp {
2923 	ktime_t		start;
2924 	ktime_t		end;
2925 };
2926 
2927 /**
2928  * struct hl_clk_throttle - keeps current/last clock throttling timestamps
2929  * @timestamp: timestamp taken by driver and firmware, index 0 refers to POWER
2930  *             index 1 refers to THERMAL
2931  * @lock: protects this structure as it can be accessed from both event queue
2932  *        context and info_ioctl context
2933  * @current_reason: bitmask represents the current clk throttling reasons
2934  * @aggregated_reason: bitmask represents aggregated clk throttling reasons since driver load
2935  */
2936 struct hl_clk_throttle {
2937 	struct hl_clk_throttle_timestamp timestamp[HL_CLK_THROTTLE_TYPE_MAX];
2938 	struct mutex	lock;
2939 	u32		current_reason;
2940 	u32		aggregated_reason;
2941 };
2942 
2943 /**
2944  * struct user_mapped_block - describes a hw block allowed to be mmapped by user
2945  * @address: physical HW block address
2946  * @size: allowed size for mmap
2947  */
2948 struct user_mapped_block {
2949 	u32 address;
2950 	u32 size;
2951 };
2952 
2953 /**
2954  * struct cs_timeout_info - info of last CS timeout occurred.
2955  * @timestamp: CS timeout timestamp.
2956  * @write_enable: if set writing to CS parameters in the structure is enabled. otherwise - disabled,
2957  *                so the first (root cause) CS timeout will not be overwritten.
2958  * @seq: CS timeout sequence number.
2959  */
2960 struct cs_timeout_info {
2961 	ktime_t		timestamp;
2962 	atomic_t	write_enable;
2963 	u64		seq;
2964 };
2965 
2966 #define MAX_QMAN_STREAMS_INFO		4
2967 #define OPCODE_INFO_MAX_ADDR_SIZE	8
2968 /**
2969  * struct undefined_opcode_info - info about last undefined opcode error
2970  * @timestamp: timestamp of the undefined opcode error
2971  * @cb_addr_streams: CB addresses (per stream) that are currently exists in the PQ
2972  *                   entries. In case all streams array entries are
2973  *                   filled with values, it means the execution was in Lower-CP.
2974  * @cq_addr: the address of the current handled command buffer
2975  * @cq_size: the size of the current handled command buffer
2976  * @cb_addr_streams_len: num of streams - actual len of cb_addr_streams array.
2977  *                       should be equal to 1 incase of undefined opcode
2978  *                       in Upper-CP (specific stream) and equal to 4 incase
2979  *                       of undefined opcode in Lower-CP.
2980  * @engine_id: engine-id that the error occurred on
2981  * @stream_id: the stream id the error occurred on. In case the stream equals to
2982  *             MAX_QMAN_STREAMS_INFO it means the error occurred on a Lower-CP.
2983  * @write_enable: if set, writing to undefined opcode parameters in the structure
2984  *                 is enable so the first (root cause) undefined opcode will not be
2985  *                 overwritten.
2986  */
2987 struct undefined_opcode_info {
2988 	ktime_t timestamp;
2989 	u64 cb_addr_streams[MAX_QMAN_STREAMS_INFO][OPCODE_INFO_MAX_ADDR_SIZE];
2990 	u64 cq_addr;
2991 	u32 cq_size;
2992 	u32 cb_addr_streams_len;
2993 	u32 engine_id;
2994 	u32 stream_id;
2995 	bool write_enable;
2996 };
2997 
2998 /**
2999  * struct page_fault_info - page fault information.
3000  * @page_fault: holds information collected during a page fault.
3001  * @user_mappings: buffer containing user mappings.
3002  * @num_of_user_mappings: number of user mappings.
3003  * @page_fault_detected: if set as 1, then a page-fault was discovered for the
3004  *                       first time after the driver has finished booting-up.
3005  *                       Since we're looking for the page-fault's root cause,
3006  *                       we don't care of the others that might follow it-
3007  *                       so once changed to 1, it will remain that way.
3008  * @page_fault_info_available: indicates that a page fault info is now available.
3009  */
3010 struct page_fault_info {
3011 	struct hl_page_fault_info	page_fault;
3012 	struct hl_user_mapping		*user_mappings;
3013 	u64				num_of_user_mappings;
3014 	atomic_t			page_fault_detected;
3015 	bool				page_fault_info_available;
3016 };
3017 
3018 /**
3019  * struct razwi_info - RAZWI information.
3020  * @razwi: holds information collected during a RAZWI
3021  * @razwi_detected: if set as 1, then a RAZWI was discovered for the
3022  *                  first time after the driver has finished booting-up.
3023  *                  Since we're looking for the RAZWI's root cause,
3024  *                  we don't care of the others that might follow it-
3025  *                  so once changed to 1, it will remain that way.
3026  * @razwi_info_available: indicates that a RAZWI info is now available.
3027  */
3028 struct razwi_info {
3029 	struct hl_info_razwi_event	razwi;
3030 	atomic_t			razwi_detected;
3031 	bool				razwi_info_available;
3032 };
3033 
3034 /**
3035  * struct hl_error_info - holds information collected during an error.
3036  * @cs_timeout: CS timeout error information.
3037  * @razwi_info: RAZWI information.
3038  * @undef_opcode: undefined opcode information.
3039  * @page_fault_info: page fault information.
3040  */
3041 struct hl_error_info {
3042 	struct cs_timeout_info		cs_timeout;
3043 	struct razwi_info		razwi_info;
3044 	struct undefined_opcode_info	undef_opcode;
3045 	struct page_fault_info		page_fault_info;
3046 };
3047 
3048 /**
3049  * struct hl_reset_info - holds current device reset information.
3050  * @lock: lock to protect critical reset flows.
3051  * @compute_reset_cnt: number of compute resets since the driver was loaded.
3052  * @hard_reset_cnt: number of hard resets since the driver was loaded.
3053  * @hard_reset_schedule_flags: hard reset is scheduled to after current compute reset,
3054  *                             here we hold the hard reset flags.
3055  * @in_reset: is device in reset flow.
3056  * @in_compute_reset: Device is currently in reset but not in hard-reset.
3057  * @needs_reset: true if reset_on_lockup is false and device should be reset
3058  *               due to lockup.
3059  * @hard_reset_pending: is there a hard reset work pending.
3060  * @curr_reset_cause: saves an enumerated reset cause when a hard reset is
3061  *                    triggered, and cleared after it is shared with preboot.
3062  * @prev_reset_trigger: saves the previous trigger which caused a reset, overridden
3063  *                      with a new value on next reset
3064  * @reset_trigger_repeated: set if device reset is triggered more than once with
3065  *                          same cause.
3066  * @skip_reset_on_timeout: Skip device reset if CS has timed out, wait for it to
3067  *                         complete instead.
3068  * @watchdog_active: true if a device release watchdog work is scheduled.
3069  */
3070 struct hl_reset_info {
3071 	spinlock_t	lock;
3072 	u32		compute_reset_cnt;
3073 	u32		hard_reset_cnt;
3074 	u32		hard_reset_schedule_flags;
3075 	u8		in_reset;
3076 	u8		in_compute_reset;
3077 	u8		needs_reset;
3078 	u8		hard_reset_pending;
3079 	u8		curr_reset_cause;
3080 	u8		prev_reset_trigger;
3081 	u8		reset_trigger_repeated;
3082 	u8		skip_reset_on_timeout;
3083 	u8		watchdog_active;
3084 };
3085 
3086 /**
3087  * struct hl_device - habanalabs device structure.
3088  * @pdev: pointer to PCI device, can be NULL in case of simulator device.
3089  * @pcie_bar_phys: array of available PCIe bars physical addresses.
3090  *		   (required only for PCI address match mode)
3091  * @pcie_bar: array of available PCIe bars virtual addresses.
3092  * @rmmio: configuration area address on SRAM.
3093  * @cdev: related char device.
3094  * @cdev_ctrl: char device for control operations only (INFO IOCTL)
3095  * @dev: related kernel basic device structure.
3096  * @dev_ctrl: related kernel device structure for the control device
3097  * @work_heartbeat: delayed work for CPU-CP is-alive check.
3098  * @device_reset_work: delayed work which performs hard reset
3099  * @device_release_watchdog_work: watchdog work that performs hard reset if user doesn't release
3100  *                                device upon certain error cases.
3101  * @asic_name: ASIC specific name.
3102  * @asic_type: ASIC specific type.
3103  * @completion_queue: array of hl_cq.
3104  * @user_interrupt: array of hl_user_interrupt. upon the corresponding user
3105  *                  interrupt, driver will monitor the list of fences
3106  *                  registered to this interrupt.
3107  * @common_user_cq_interrupt: common user CQ interrupt for all user CQ interrupts.
3108  *                         upon any user CQ interrupt, driver will monitor the
3109  *                         list of fences registered to this common structure.
3110  * @common_decoder_interrupt: common decoder interrupt for all user decoder interrupts.
3111  * @shadow_cs_queue: pointer to a shadow queue that holds pointers to
3112  *                   outstanding command submissions.
3113  * @cq_wq: work queues of completion queues for executing work in process
3114  *         context.
3115  * @eq_wq: work queue of event queue for executing work in process context.
3116  * @cs_cmplt_wq: work queue of CS completions for executing work in process
3117  *               context.
3118  * @ts_free_obj_wq: work queue for timestamp registration objects release.
3119  * @prefetch_wq: work queue for MMU pre-fetch operations.
3120  * @reset_wq: work queue for device reset procedure.
3121  * @kernel_ctx: Kernel driver context structure.
3122  * @kernel_queues: array of hl_hw_queue.
3123  * @cs_mirror_list: CS mirror list for TDR.
3124  * @cs_mirror_lock: protects cs_mirror_list.
3125  * @kernel_mem_mgr: memory manager for memory buffers with lifespan of driver.
3126  * @event_queue: event queue for IRQ from CPU-CP.
3127  * @dma_pool: DMA pool for small allocations.
3128  * @cpu_accessible_dma_mem: Host <-> CPU-CP shared memory CPU address.
3129  * @cpu_accessible_dma_address: Host <-> CPU-CP shared memory DMA address.
3130  * @cpu_accessible_dma_pool: Host <-> CPU-CP shared memory pool.
3131  * @asid_bitmap: holds used/available ASIDs.
3132  * @asid_mutex: protects asid_bitmap.
3133  * @send_cpu_message_lock: enforces only one message in Host <-> CPU-CP queue.
3134  * @debug_lock: protects critical section of setting debug mode for device
3135  * @mmu_lock: protects the MMU page tables and invalidation h/w. Although the
3136  *            page tables are per context, the invalidation h/w is per MMU.
3137  *            Therefore, we can't allow multiple contexts (we only have two,
3138  *            user and kernel) to access the invalidation h/w at the same time.
3139  *            In addition, any change to the PGT, modifying the MMU hash or
3140  *            walking the PGT requires talking this lock.
3141  * @asic_prop: ASIC specific immutable properties.
3142  * @asic_funcs: ASIC specific functions.
3143  * @asic_specific: ASIC specific information to use only from ASIC files.
3144  * @vm: virtual memory manager for MMU.
3145  * @hwmon_dev: H/W monitor device.
3146  * @hl_chip_info: ASIC's sensors information.
3147  * @device_status_description: device status description.
3148  * @hl_debugfs: device's debugfs manager.
3149  * @cb_pool: list of pre allocated CBs.
3150  * @cb_pool_lock: protects the CB pool.
3151  * @internal_cb_pool_virt_addr: internal command buffer pool virtual address.
3152  * @internal_cb_pool_dma_addr: internal command buffer pool dma address.
3153  * @internal_cb_pool: internal command buffer memory pool.
3154  * @internal_cb_va_base: internal cb pool mmu virtual address base
3155  * @fpriv_list: list of file private data structures. Each structure is created
3156  *              when a user opens the device
3157  * @fpriv_ctrl_list: list of file private data structures. Each structure is created
3158  *              when a user opens the control device
3159  * @fpriv_list_lock: protects the fpriv_list
3160  * @fpriv_ctrl_list_lock: protects the fpriv_ctrl_list
3161  * @aggregated_cs_counters: aggregated cs counters among all contexts
3162  * @mmu_priv: device-specific MMU data.
3163  * @mmu_func: device-related MMU functions.
3164  * @dec: list of decoder sw instance
3165  * @fw_loader: FW loader manager.
3166  * @pci_mem_region: array of memory regions in the PCI
3167  * @state_dump_specs: constants and dictionaries needed to dump system state.
3168  * @multi_cs_completion: array of multi-CS completion.
3169  * @clk_throttling: holds information about current/previous clock throttling events
3170  * @captured_err_info: holds information about errors.
3171  * @reset_info: holds current device reset information.
3172  * @stream_master_qid_arr: pointer to array with QIDs of master streams.
3173  * @fw_major_version: major version of current loaded preboot.
3174  * @fw_minor_version: minor version of current loaded preboot.
3175  * @dram_used_mem: current DRAM memory consumption.
3176  * @memory_scrub_val: the value to which the dram will be scrubbed to using cb scrub_device_dram
3177  * @timeout_jiffies: device CS timeout value.
3178  * @max_power: the max power of the device, as configured by the sysadmin. This
3179  *             value is saved so in case of hard-reset, the driver will restore
3180  *             this value and update the F/W after the re-initialization
3181  * @boot_error_status_mask: contains a mask of the device boot error status.
3182  *                          Each bit represents a different error, according to
3183  *                          the defines in hl_boot_if.h. If the bit is cleared,
3184  *                          the error will be ignored by the driver during
3185  *                          device initialization. Mainly used to debug and
3186  *                          workaround firmware bugs
3187  * @dram_pci_bar_start: start bus address of PCIe bar towards DRAM.
3188  * @last_successful_open_ktime: timestamp (ktime) of the last successful device open.
3189  * @last_successful_open_jif: timestamp (jiffies) of the last successful
3190  *                            device open.
3191  * @last_open_session_duration_jif: duration (jiffies) of the last device open
3192  *                                  session.
3193  * @open_counter: number of successful device open operations.
3194  * @fw_poll_interval_usec: FW status poll interval in usec.
3195  *                         used for CPU boot status
3196  * @fw_comms_poll_interval_usec: FW comms/protocol poll interval in usec.
3197  *                                  used for COMMs protocols cmds(COMMS_STS_*)
3198  * @dram_binning: contains mask of drams that is received from the f/w which indicates which
3199  *                drams are binned-out
3200  * @tpc_binning: contains mask of tpc engines that is received from the f/w which indicates which
3201  *               tpc engines are binned-out
3202  * @card_type: Various ASICs have several card types. This indicates the card
3203  *             type of the current device.
3204  * @major: habanalabs kernel driver major.
3205  * @high_pll: high PLL profile frequency.
3206  * @decoder_binning: contains mask of decoder engines that is received from the f/w which
3207  *                   indicates which decoder engines are binned-out
3208  * @edma_binning: contains mask of edma engines that is received from the f/w which
3209  *                   indicates which edma engines are binned-out
3210  * @device_release_watchdog_timeout_sec: device release watchdog timeout value in seconds.
3211  * @rotator_binning: contains mask of rotators engines that is received from the f/w
3212  *			which indicates which rotator engines are binned-out(Gaudi3 and above).
3213  * @id: device minor.
3214  * @id_control: minor of the control device.
3215  * @cdev_idx: char device index. Used for setting its name.
3216  * @cpu_pci_msb_addr: 50-bit extension bits for the device CPU's 40-bit
3217  *                    addresses.
3218  * @is_in_dram_scrub: true if dram scrub operation is on going.
3219  * @disabled: is device disabled.
3220  * @late_init_done: is late init stage was done during initialization.
3221  * @hwmon_initialized: is H/W monitor sensors was initialized.
3222  * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
3223  *                   otherwise.
3224  * @dram_default_page_mapping: is DRAM default page mapping enabled.
3225  * @memory_scrub: true to perform device memory scrub in various locations,
3226  *                such as context-switch, context close, page free, etc.
3227  * @pmmu_huge_range: is a different virtual addresses range used for PMMU with
3228  *                   huge pages.
3229  * @init_done: is the initialization of the device done.
3230  * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
3231  * @in_debug: whether the device is in a state where the profiling/tracing infrastructure
3232  *            can be used. This indication is needed because in some ASICs we need to do
3233  *            specific operations to enable that infrastructure.
3234  * @cdev_sysfs_created: were char devices and sysfs nodes created.
3235  * @stop_on_err: true if engines should stop on error.
3236  * @supports_sync_stream: is sync stream supported.
3237  * @sync_stream_queue_idx: helper index for sync stream queues initialization.
3238  * @collective_mon_idx: helper index for collective initialization
3239  * @supports_coresight: is CoreSight supported.
3240  * @supports_cb_mapping: is mapping a CB to the device's MMU supported.
3241  * @process_kill_trial_cnt: number of trials reset thread tried killing
3242  *                          user processes
3243  * @device_fini_pending: true if device_fini was called and might be
3244  *                       waiting for the reset thread to finish
3245  * @supports_staged_submission: true if staged submissions are supported
3246  * @device_cpu_is_halted: Flag to indicate whether the device CPU was already
3247  *                        halted. We can't halt it again because the COMMS
3248  *                        protocol will throw an error. Relevant only for
3249  *                        cases where Linux was not loaded to device CPU
3250  * @supports_wait_for_multi_cs: true if wait for multi CS is supported
3251  * @is_compute_ctx_active: Whether there is an active compute context executing.
3252  * @compute_ctx_in_release: true if the current compute context is being released.
3253  * @supports_mmu_prefetch: true if prefetch is supported, otherwise false.
3254  * @reset_upon_device_release: reset the device when the user closes the file descriptor of the
3255  *                             device.
3256  * @nic_ports_mask: Controls which NIC ports are enabled. Used only for testing.
3257  * @fw_components: Controls which f/w components to load to the device. There are multiple f/w
3258  *                 stages and sometimes we want to stop at a certain stage. Used only for testing.
3259  * @mmu_enable: Whether to enable or disable the device MMU(s). Used only for testing.
3260  * @cpu_queues_enable: Whether to enable queues communication vs. the f/w. Used only for testing.
3261  * @pldm: Whether we are running in Palladium environment. Used only for testing.
3262  * @hard_reset_on_fw_events: Whether to do device hard-reset when a fatal event is received from
3263  *                           the f/w. Used only for testing.
3264  * @bmc_enable: Whether we are running in a box with BMC. Used only for testing.
3265  * @reset_on_preboot_fail: Whether to reset the device if preboot f/w fails to load.
3266  *                         Used only for testing.
3267  * @heartbeat: Controls if we want to enable the heartbeat mechanism vs. the f/w, which verifies
3268  *             that the f/w is always alive. Used only for testing.
3269  * @supports_ctx_switch: true if a ctx switch is required upon first submission.
3270  * @support_preboot_binning: true if we support read binning info from preboot.
3271  */
3272 struct hl_device {
3273 	struct pci_dev			*pdev;
3274 	u64				pcie_bar_phys[HL_PCI_NUM_BARS];
3275 	void __iomem			*pcie_bar[HL_PCI_NUM_BARS];
3276 	void __iomem			*rmmio;
3277 	struct cdev			cdev;
3278 	struct cdev			cdev_ctrl;
3279 	struct device			*dev;
3280 	struct device			*dev_ctrl;
3281 	struct delayed_work		work_heartbeat;
3282 	struct hl_device_reset_work	device_reset_work;
3283 	struct hl_device_reset_work	device_release_watchdog_work;
3284 	char				asic_name[HL_STR_MAX];
3285 	char				status[HL_DEV_STS_MAX][HL_STR_MAX];
3286 	enum hl_asic_type		asic_type;
3287 	struct hl_cq			*completion_queue;
3288 	struct hl_user_interrupt	*user_interrupt;
3289 	struct hl_user_interrupt	common_user_cq_interrupt;
3290 	struct hl_user_interrupt	common_decoder_interrupt;
3291 	struct hl_cs			**shadow_cs_queue;
3292 	struct workqueue_struct		**cq_wq;
3293 	struct workqueue_struct		*eq_wq;
3294 	struct workqueue_struct		*cs_cmplt_wq;
3295 	struct workqueue_struct		*ts_free_obj_wq;
3296 	struct workqueue_struct		*prefetch_wq;
3297 	struct workqueue_struct		*reset_wq;
3298 	struct hl_ctx			*kernel_ctx;
3299 	struct hl_hw_queue		*kernel_queues;
3300 	struct list_head		cs_mirror_list;
3301 	spinlock_t			cs_mirror_lock;
3302 	struct hl_mem_mgr		kernel_mem_mgr;
3303 	struct hl_eq			event_queue;
3304 	struct dma_pool			*dma_pool;
3305 	void				*cpu_accessible_dma_mem;
3306 	dma_addr_t			cpu_accessible_dma_address;
3307 	struct gen_pool			*cpu_accessible_dma_pool;
3308 	unsigned long			*asid_bitmap;
3309 	struct mutex			asid_mutex;
3310 	struct mutex			send_cpu_message_lock;
3311 	struct mutex			debug_lock;
3312 	struct mutex			mmu_lock;
3313 	struct asic_fixed_properties	asic_prop;
3314 	const struct hl_asic_funcs	*asic_funcs;
3315 	void				*asic_specific;
3316 	struct hl_vm			vm;
3317 	struct device			*hwmon_dev;
3318 	struct hwmon_chip_info		*hl_chip_info;
3319 
3320 	struct hl_dbg_device_entry	hl_debugfs;
3321 
3322 	struct list_head		cb_pool;
3323 	spinlock_t			cb_pool_lock;
3324 
3325 	void				*internal_cb_pool_virt_addr;
3326 	dma_addr_t			internal_cb_pool_dma_addr;
3327 	struct gen_pool			*internal_cb_pool;
3328 	u64				internal_cb_va_base;
3329 
3330 	struct list_head		fpriv_list;
3331 	struct list_head		fpriv_ctrl_list;
3332 	struct mutex			fpriv_list_lock;
3333 	struct mutex			fpriv_ctrl_list_lock;
3334 
3335 	struct hl_cs_counters_atomic	aggregated_cs_counters;
3336 
3337 	struct hl_mmu_priv		mmu_priv;
3338 	struct hl_mmu_funcs		mmu_func[MMU_NUM_PGT_LOCATIONS];
3339 
3340 	struct hl_dec			*dec;
3341 
3342 	struct fw_load_mgr		fw_loader;
3343 
3344 	struct pci_mem_region		pci_mem_region[PCI_REGION_NUMBER];
3345 
3346 	struct hl_state_dump_specs	state_dump_specs;
3347 
3348 	struct multi_cs_completion	multi_cs_completion[
3349 							MULTI_CS_MAX_USER_CTX];
3350 	struct hl_clk_throttle		clk_throttling;
3351 	struct hl_error_info		captured_err_info;
3352 
3353 	struct hl_reset_info		reset_info;
3354 
3355 	u32				*stream_master_qid_arr;
3356 	u32				fw_major_version;
3357 	u32				fw_minor_version;
3358 	atomic64_t			dram_used_mem;
3359 	u64				memory_scrub_val;
3360 	u64				timeout_jiffies;
3361 	u64				max_power;
3362 	u64				boot_error_status_mask;
3363 	u64				dram_pci_bar_start;
3364 	u64				last_successful_open_jif;
3365 	u64				last_open_session_duration_jif;
3366 	u64				open_counter;
3367 	u64				fw_poll_interval_usec;
3368 	ktime_t				last_successful_open_ktime;
3369 	u64				fw_comms_poll_interval_usec;
3370 	u64				dram_binning;
3371 	u64				tpc_binning;
3372 
3373 	enum cpucp_card_types		card_type;
3374 	u32				major;
3375 	u32				high_pll;
3376 	u32				decoder_binning;
3377 	u32				edma_binning;
3378 	u32				device_release_watchdog_timeout_sec;
3379 	u32				rotator_binning;
3380 	u16				id;
3381 	u16				id_control;
3382 	u16				cdev_idx;
3383 	u16				cpu_pci_msb_addr;
3384 	u8				is_in_dram_scrub;
3385 	u8				disabled;
3386 	u8				late_init_done;
3387 	u8				hwmon_initialized;
3388 	u8				reset_on_lockup;
3389 	u8				dram_default_page_mapping;
3390 	u8				memory_scrub;
3391 	u8				pmmu_huge_range;
3392 	u8				init_done;
3393 	u8				device_cpu_disabled;
3394 	u8				in_debug;
3395 	u8				cdev_sysfs_created;
3396 	u8				stop_on_err;
3397 	u8				supports_sync_stream;
3398 	u8				sync_stream_queue_idx;
3399 	u8				collective_mon_idx;
3400 	u8				supports_coresight;
3401 	u8				supports_cb_mapping;
3402 	u8				process_kill_trial_cnt;
3403 	u8				device_fini_pending;
3404 	u8				supports_staged_submission;
3405 	u8				device_cpu_is_halted;
3406 	u8				supports_wait_for_multi_cs;
3407 	u8				stream_master_qid_arr_size;
3408 	u8				is_compute_ctx_active;
3409 	u8				compute_ctx_in_release;
3410 	u8				supports_mmu_prefetch;
3411 	u8				reset_upon_device_release;
3412 	u8				supports_ctx_switch;
3413 	u8				support_preboot_binning;
3414 
3415 	/* Parameters for bring-up */
3416 	u64				nic_ports_mask;
3417 	u64				fw_components;
3418 	u8				mmu_enable;
3419 	u8				cpu_queues_enable;
3420 	u8				pldm;
3421 	u8				hard_reset_on_fw_events;
3422 	u8				bmc_enable;
3423 	u8				reset_on_preboot_fail;
3424 	u8				heartbeat;
3425 };
3426 
3427 
3428 /**
3429  * struct hl_cs_encaps_sig_handle - encapsulated signals handle structure
3430  * @refcount: refcount used to protect removing this id when several
3431  *            wait cs are used to wait of the reserved encaps signals.
3432  * @hdev: pointer to habanalabs device structure.
3433  * @hw_sob: pointer to  H/W SOB used in the reservation.
3434  * @ctx: pointer to the user's context data structure
3435  * @cs_seq: staged cs sequence which contains encapsulated signals
3436  * @id: idr handler id to be used to fetch the handler info
3437  * @q_idx: stream queue index
3438  * @pre_sob_val: current SOB value before reservation
3439  * @count: signals number
3440  */
3441 struct hl_cs_encaps_sig_handle {
3442 	struct kref refcount;
3443 	struct hl_device *hdev;
3444 	struct hl_hw_sob *hw_sob;
3445 	struct hl_ctx *ctx;
3446 	u64  cs_seq;
3447 	u32  id;
3448 	u32  q_idx;
3449 	u32  pre_sob_val;
3450 	u32  count;
3451 };
3452 
3453 /*
3454  * IOCTLs
3455  */
3456 
3457 /**
3458  * typedef hl_ioctl_t - typedef for ioctl function in the driver
3459  * @hpriv: pointer to the FD's private data, which contains state of
3460  *		user process
3461  * @data: pointer to the input/output arguments structure of the IOCTL
3462  *
3463  * Return: 0 for success, negative value for error
3464  */
3465 typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
3466 
3467 /**
3468  * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
3469  * @cmd: the IOCTL code as created by the kernel macros.
3470  * @func: pointer to the driver's function that should be called for this IOCTL.
3471  */
3472 struct hl_ioctl_desc {
3473 	unsigned int cmd;
3474 	hl_ioctl_t *func;
3475 };
3476 
3477 
3478 /*
3479  * Kernel module functions that can be accessed by entire module
3480  */
3481 
3482 /**
3483  * hl_get_sg_info() - get number of pages and the DMA address from SG list.
3484  * @sg: the SG list.
3485  * @dma_addr: pointer to DMA address to return.
3486  *
3487  * Calculate the number of consecutive pages described by the SG list. Take the
3488  * offset of the address in the first page, add to it the length and round it up
3489  * to the number of needed pages.
3490  */
3491 static inline u32 hl_get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr)
3492 {
3493 	*dma_addr = sg_dma_address(sg);
3494 
3495 	return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) +
3496 			(PAGE_SIZE - 1)) >> PAGE_SHIFT;
3497 }
3498 
3499 /**
3500  * hl_mem_area_inside_range() - Checks whether address+size are inside a range.
3501  * @address: The start address of the area we want to validate.
3502  * @size: The size in bytes of the area we want to validate.
3503  * @range_start_address: The start address of the valid range.
3504  * @range_end_address: The end address of the valid range.
3505  *
3506  * Return: true if the area is inside the valid range, false otherwise.
3507  */
3508 static inline bool hl_mem_area_inside_range(u64 address, u64 size,
3509 				u64 range_start_address, u64 range_end_address)
3510 {
3511 	u64 end_address = address + size;
3512 
3513 	if ((address >= range_start_address) &&
3514 			(end_address <= range_end_address) &&
3515 			(end_address > address))
3516 		return true;
3517 
3518 	return false;
3519 }
3520 
3521 /**
3522  * hl_mem_area_crosses_range() - Checks whether address+size crossing a range.
3523  * @address: The start address of the area we want to validate.
3524  * @size: The size in bytes of the area we want to validate.
3525  * @range_start_address: The start address of the valid range.
3526  * @range_end_address: The end address of the valid range.
3527  *
3528  * Return: true if the area overlaps part or all of the valid range,
3529  *		false otherwise.
3530  */
3531 static inline bool hl_mem_area_crosses_range(u64 address, u32 size,
3532 				u64 range_start_address, u64 range_end_address)
3533 {
3534 	u64 end_address = address + size - 1;
3535 
3536 	return ((address <= range_end_address) && (range_start_address <= end_address));
3537 }
3538 
3539 uint64_t hl_set_dram_bar_default(struct hl_device *hdev, u64 addr);
3540 void *hl_asic_dma_alloc_coherent_caller(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle,
3541 					gfp_t flag, const char *caller);
3542 void hl_asic_dma_free_coherent_caller(struct hl_device *hdev, size_t size, void *cpu_addr,
3543 					dma_addr_t dma_handle, const char *caller);
3544 void *hl_cpu_accessible_dma_pool_alloc_caller(struct hl_device *hdev, size_t size,
3545 						dma_addr_t *dma_handle, const char *caller);
3546 void hl_cpu_accessible_dma_pool_free_caller(struct hl_device *hdev, size_t size, void *vaddr,
3547 						const char *caller);
3548 void *hl_asic_dma_pool_zalloc_caller(struct hl_device *hdev, size_t size, gfp_t mem_flags,
3549 					dma_addr_t *dma_handle, const char *caller);
3550 void hl_asic_dma_pool_free_caller(struct hl_device *hdev, void *vaddr, dma_addr_t dma_addr,
3551 					const char *caller);
3552 int hl_dma_map_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir);
3553 void hl_dma_unmap_sgtable(struct hl_device *hdev, struct sg_table *sgt,
3554 				enum dma_data_direction dir);
3555 int hl_access_sram_dram_region(struct hl_device *hdev, u64 addr, u64 *val,
3556 	enum debugfs_access_type acc_type, enum pci_region region_type, bool set_dram_bar);
3557 int hl_access_cfg_region(struct hl_device *hdev, u64 addr, u64 *val,
3558 	enum debugfs_access_type acc_type);
3559 int hl_access_dev_mem(struct hl_device *hdev, enum pci_region region_type,
3560 			u64 addr, u64 *val, enum debugfs_access_type acc_type);
3561 int hl_device_open(struct inode *inode, struct file *filp);
3562 int hl_device_open_ctrl(struct inode *inode, struct file *filp);
3563 bool hl_device_operational(struct hl_device *hdev,
3564 		enum hl_device_status *status);
3565 bool hl_ctrl_device_operational(struct hl_device *hdev,
3566 		enum hl_device_status *status);
3567 enum hl_device_status hl_device_status(struct hl_device *hdev);
3568 int hl_device_set_debug_mode(struct hl_device *hdev, struct hl_ctx *ctx, bool enable);
3569 int hl_hw_queues_create(struct hl_device *hdev);
3570 void hl_hw_queues_destroy(struct hl_device *hdev);
3571 int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
3572 		u32 cb_size, u64 cb_ptr);
3573 void hl_hw_queue_submit_bd(struct hl_device *hdev, struct hl_hw_queue *q,
3574 		u32 ctl, u32 len, u64 ptr);
3575 int hl_hw_queue_schedule_cs(struct hl_cs *cs);
3576 u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
3577 void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
3578 void hl_hw_queue_update_ci(struct hl_cs *cs);
3579 void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
3580 
3581 #define hl_queue_inc_ptr(p)		hl_hw_queue_add_ptr(p, 1)
3582 #define hl_pi_2_offset(pi)		((pi) & (HL_QUEUE_LENGTH - 1))
3583 
3584 int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
3585 void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
3586 int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
3587 void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
3588 void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
3589 void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
3590 irqreturn_t hl_irq_handler_cq(int irq, void *arg);
3591 irqreturn_t hl_irq_handler_eq(int irq, void *arg);
3592 irqreturn_t hl_irq_handler_dec_abnrm(int irq, void *arg);
3593 irqreturn_t hl_irq_handler_user_interrupt(int irq, void *arg);
3594 irqreturn_t hl_irq_handler_default(int irq, void *arg);
3595 u32 hl_cq_inc_ptr(u32 ptr);
3596 
3597 int hl_asid_init(struct hl_device *hdev);
3598 void hl_asid_fini(struct hl_device *hdev);
3599 unsigned long hl_asid_alloc(struct hl_device *hdev);
3600 void hl_asid_free(struct hl_device *hdev, unsigned long asid);
3601 
3602 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
3603 void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
3604 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
3605 void hl_ctx_do_release(struct kref *ref);
3606 void hl_ctx_get(struct hl_ctx *ctx);
3607 int hl_ctx_put(struct hl_ctx *ctx);
3608 struct hl_ctx *hl_get_compute_ctx(struct hl_device *hdev);
3609 struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
3610 int hl_ctx_get_fences(struct hl_ctx *ctx, u64 *seq_arr,
3611 				struct hl_fence **fence, u32 arr_len);
3612 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
3613 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
3614 
3615 int hl_device_init(struct hl_device *hdev, struct class *hclass);
3616 void hl_device_fini(struct hl_device *hdev);
3617 int hl_device_suspend(struct hl_device *hdev);
3618 int hl_device_resume(struct hl_device *hdev);
3619 int hl_device_reset(struct hl_device *hdev, u32 flags);
3620 int hl_device_cond_reset(struct hl_device *hdev, u32 flags, u64 event_mask);
3621 void hl_hpriv_get(struct hl_fpriv *hpriv);
3622 int hl_hpriv_put(struct hl_fpriv *hpriv);
3623 int hl_device_utilization(struct hl_device *hdev, u32 *utilization);
3624 
3625 int hl_build_hwmon_channel_info(struct hl_device *hdev,
3626 		struct cpucp_sensor *sensors_arr);
3627 
3628 void hl_notifier_event_send_all(struct hl_device *hdev, u64 event_mask);
3629 
3630 int hl_sysfs_init(struct hl_device *hdev);
3631 void hl_sysfs_fini(struct hl_device *hdev);
3632 
3633 int hl_hwmon_init(struct hl_device *hdev);
3634 void hl_hwmon_fini(struct hl_device *hdev);
3635 void hl_hwmon_release_resources(struct hl_device *hdev);
3636 
3637 int hl_cb_create(struct hl_device *hdev, struct hl_mem_mgr *mmg,
3638 			struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
3639 			bool map_cb, u64 *handle);
3640 int hl_cb_destroy(struct hl_mem_mgr *mmg, u64 cb_handle);
3641 int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
3642 struct hl_cb *hl_cb_get(struct hl_mem_mgr *mmg, u64 handle);
3643 void hl_cb_put(struct hl_cb *cb);
3644 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
3645 					bool internal_cb);
3646 int hl_cb_pool_init(struct hl_device *hdev);
3647 int hl_cb_pool_fini(struct hl_device *hdev);
3648 int hl_cb_va_pool_init(struct hl_ctx *ctx);
3649 void hl_cb_va_pool_fini(struct hl_ctx *ctx);
3650 
3651 void hl_cs_rollback_all(struct hl_device *hdev, bool skip_wq_flush);
3652 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
3653 		enum hl_queue_type queue_type, bool is_kernel_allocated_cb);
3654 void hl_sob_reset_error(struct kref *ref);
3655 int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask);
3656 void hl_fence_put(struct hl_fence *fence);
3657 void hl_fences_put(struct hl_fence **fence, int len);
3658 void hl_fence_get(struct hl_fence *fence);
3659 void cs_get(struct hl_cs *cs);
3660 bool cs_needs_completion(struct hl_cs *cs);
3661 bool cs_needs_timeout(struct hl_cs *cs);
3662 bool is_staged_cs_last_exists(struct hl_device *hdev, struct hl_cs *cs);
3663 struct hl_cs *hl_staged_cs_find_first(struct hl_device *hdev, u64 cs_seq);
3664 void hl_multi_cs_completion_init(struct hl_device *hdev);
3665 
3666 void goya_set_asic_funcs(struct hl_device *hdev);
3667 void gaudi_set_asic_funcs(struct hl_device *hdev);
3668 void gaudi2_set_asic_funcs(struct hl_device *hdev);
3669 
3670 int hl_vm_ctx_init(struct hl_ctx *ctx);
3671 void hl_vm_ctx_fini(struct hl_ctx *ctx);
3672 
3673 int hl_vm_init(struct hl_device *hdev);
3674 void hl_vm_fini(struct hl_device *hdev);
3675 
3676 void hl_hw_block_mem_init(struct hl_ctx *ctx);
3677 void hl_hw_block_mem_fini(struct hl_ctx *ctx);
3678 
3679 u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
3680 		enum hl_va_range_type type, u64 size, u32 alignment);
3681 int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
3682 		u64 start_addr, u64 size);
3683 int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
3684 			struct hl_userptr *userptr);
3685 void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
3686 void hl_userptr_delete_list(struct hl_device *hdev,
3687 				struct list_head *userptr_list);
3688 bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, u32 size,
3689 				struct list_head *userptr_list,
3690 				struct hl_userptr **userptr);
3691 
3692 int hl_mmu_init(struct hl_device *hdev);
3693 void hl_mmu_fini(struct hl_device *hdev);
3694 int hl_mmu_ctx_init(struct hl_ctx *ctx);
3695 void hl_mmu_ctx_fini(struct hl_ctx *ctx);
3696 int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
3697 		u32 page_size, bool flush_pte);
3698 int hl_mmu_get_real_page_size(struct hl_device *hdev, struct hl_mmu_properties *mmu_prop,
3699 				u32 page_size, u32 *real_page_size, bool is_dram_addr);
3700 int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
3701 		bool flush_pte);
3702 int hl_mmu_map_contiguous(struct hl_ctx *ctx, u64 virt_addr,
3703 					u64 phys_addr, u32 size);
3704 int hl_mmu_unmap_contiguous(struct hl_ctx *ctx, u64 virt_addr, u32 size);
3705 int hl_mmu_invalidate_cache(struct hl_device *hdev, bool is_hard, u32 flags);
3706 int hl_mmu_invalidate_cache_range(struct hl_device *hdev, bool is_hard,
3707 					u32 flags, u32 asid, u64 va, u64 size);
3708 int hl_mmu_prefetch_cache_range(struct hl_ctx *ctx, u32 flags, u32 asid, u64 va, u64 size);
3709 u64 hl_mmu_get_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte);
3710 u64 hl_mmu_get_hop_pte_phys_addr(struct hl_ctx *ctx, struct hl_mmu_properties *mmu_prop,
3711 					u8 hop_idx, u64 hop_addr, u64 virt_addr);
3712 void hl_mmu_hr_flush(struct hl_ctx *ctx);
3713 int hl_mmu_hr_init(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv, u32 hop_table_size,
3714 			u64 pgt_size);
3715 void hl_mmu_hr_fini(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv, u32 hop_table_size);
3716 void hl_mmu_hr_free_hop_remove_pgt(struct pgt_info *pgt_info, struct hl_mmu_hr_priv *hr_priv,
3717 				u32 hop_table_size);
3718 u64 hl_mmu_hr_pte_phys_to_virt(struct hl_ctx *ctx, struct pgt_info *pgt, u64 phys_pte_addr,
3719 							u32 hop_table_size);
3720 void hl_mmu_hr_write_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, u64 phys_pte_addr,
3721 							u64 val, u32 hop_table_size);
3722 void hl_mmu_hr_clear_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, u64 phys_pte_addr,
3723 							u32 hop_table_size);
3724 int hl_mmu_hr_put_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, struct hl_mmu_hr_priv *hr_priv,
3725 							u32 hop_table_size);
3726 void hl_mmu_hr_get_pte(struct hl_ctx *ctx, struct hl_hr_mmu_funcs *hr_func, u64 phys_hop_addr);
3727 struct pgt_info *hl_mmu_hr_get_next_hop_pgt_info(struct hl_ctx *ctx,
3728 							struct hl_hr_mmu_funcs *hr_func,
3729 							u64 curr_pte);
3730 struct pgt_info *hl_mmu_hr_alloc_hop(struct hl_ctx *ctx, struct hl_mmu_hr_priv *hr_priv,
3731 							struct hl_hr_mmu_funcs *hr_func,
3732 							struct hl_mmu_properties *mmu_prop);
3733 struct pgt_info *hl_mmu_hr_get_alloc_next_hop(struct hl_ctx *ctx,
3734 							struct hl_mmu_hr_priv *hr_priv,
3735 							struct hl_hr_mmu_funcs *hr_func,
3736 							struct hl_mmu_properties *mmu_prop,
3737 							u64 curr_pte, bool *is_new_hop);
3738 int hl_mmu_hr_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr, struct hl_mmu_hop_info *hops,
3739 							struct hl_hr_mmu_funcs *hr_func);
3740 void hl_mmu_swap_out(struct hl_ctx *ctx);
3741 void hl_mmu_swap_in(struct hl_ctx *ctx);
3742 int hl_mmu_if_set_funcs(struct hl_device *hdev);
3743 void hl_mmu_v1_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
3744 void hl_mmu_v2_hr_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
3745 int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr);
3746 int hl_mmu_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
3747 			struct hl_mmu_hop_info *hops);
3748 u64 hl_mmu_scramble_addr(struct hl_device *hdev, u64 addr);
3749 u64 hl_mmu_descramble_addr(struct hl_device *hdev, u64 addr);
3750 bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr);
3751 
3752 int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
3753 				void __iomem *dst, u32 src_offset, u32 size);
3754 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode, u64 value);
3755 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
3756 				u16 len, u32 timeout, u64 *result);
3757 int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type);
3758 int hl_fw_unmask_irq_arr(struct hl_device *hdev, const u32 *irq_arr,
3759 		size_t irq_arr_size);
3760 int hl_fw_test_cpu_queue(struct hl_device *hdev);
3761 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
3762 						dma_addr_t *dma_handle);
3763 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
3764 					void *vaddr);
3765 int hl_fw_send_heartbeat(struct hl_device *hdev);
3766 int hl_fw_cpucp_info_get(struct hl_device *hdev,
3767 				u32 sts_boot_dev_sts0_reg,
3768 				u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
3769 				u32 boot_err1_reg);
3770 int hl_fw_cpucp_handshake(struct hl_device *hdev,
3771 				u32 sts_boot_dev_sts0_reg,
3772 				u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
3773 				u32 boot_err1_reg);
3774 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
3775 int hl_fw_get_monitor_dump(struct hl_device *hdev, void *data);
3776 int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
3777 		struct hl_info_pci_counters *counters);
3778 int hl_fw_cpucp_total_energy_get(struct hl_device *hdev,
3779 			u64 *total_energy);
3780 int get_used_pll_index(struct hl_device *hdev, u32 input_pll_index,
3781 						enum pll_index *pll_index);
3782 int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
3783 		u16 *pll_freq_arr);
3784 int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power);
3785 void hl_fw_ask_hard_reset_without_linux(struct hl_device *hdev);
3786 void hl_fw_ask_halt_machine_without_linux(struct hl_device *hdev);
3787 int hl_fw_init_cpu(struct hl_device *hdev);
3788 int hl_fw_wait_preboot_ready(struct hl_device *hdev);
3789 int hl_fw_read_preboot_status(struct hl_device *hdev);
3790 int hl_fw_dynamic_send_protocol_cmd(struct hl_device *hdev,
3791 				struct fw_load_mgr *fw_loader,
3792 				enum comms_cmd cmd, unsigned int size,
3793 				bool wait_ok, u32 timeout);
3794 int hl_fw_dram_replaced_row_get(struct hl_device *hdev,
3795 				struct cpucp_hbm_row_info *info);
3796 int hl_fw_dram_pending_row_get(struct hl_device *hdev, u32 *pend_rows_num);
3797 int hl_fw_cpucp_engine_core_asid_set(struct hl_device *hdev, u32 asid);
3798 int hl_fw_send_device_activity(struct hl_device *hdev, bool open);
3799 int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
3800 			bool is_wc[3]);
3801 int hl_pci_elbi_read(struct hl_device *hdev, u64 addr, u32 *data);
3802 int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
3803 int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
3804 		struct hl_inbound_pci_region *pci_region);
3805 int hl_pci_set_outbound_region(struct hl_device *hdev,
3806 		struct hl_outbound_pci_region *pci_region);
3807 enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr);
3808 int hl_pci_init(struct hl_device *hdev);
3809 void hl_pci_fini(struct hl_device *hdev);
3810 
3811 long hl_fw_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr);
3812 void hl_fw_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq);
3813 int hl_get_temperature(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
3814 int hl_set_temperature(struct hl_device *hdev, int sensor_index, u32 attr, long value);
3815 int hl_get_voltage(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
3816 int hl_get_current(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
3817 int hl_get_fan_speed(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
3818 int hl_get_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
3819 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr, long value);
3820 long hl_fw_get_max_power(struct hl_device *hdev);
3821 void hl_fw_set_max_power(struct hl_device *hdev);
3822 int hl_fw_get_sec_attest_info(struct hl_device *hdev, struct cpucp_sec_attest_info *sec_attest_info,
3823 				u32 nonce);
3824 int hl_set_voltage(struct hl_device *hdev, int sensor_index, u32 attr, long value);
3825 int hl_set_current(struct hl_device *hdev, int sensor_index, u32 attr, long value);
3826 int hl_set_power(struct hl_device *hdev, int sensor_index, u32 attr, long value);
3827 int hl_get_power(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
3828 int hl_fw_get_clk_rate(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk);
3829 void hl_fw_set_pll_profile(struct hl_device *hdev);
3830 void hl_sysfs_add_dev_clk_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp);
3831 void hl_sysfs_add_dev_vrm_attr(struct hl_device *hdev, struct attribute_group *dev_vrm_attr_grp);
3832 int hl_fw_send_generic_request(struct hl_device *hdev, enum hl_passthrough_type sub_opcode,
3833 						dma_addr_t buff, u32 *size);
3834 
3835 void hw_sob_get(struct hl_hw_sob *hw_sob);
3836 void hw_sob_put(struct hl_hw_sob *hw_sob);
3837 void hl_encaps_release_handle_and_put_ctx(struct kref *ref);
3838 void hl_encaps_release_handle_and_put_sob_ctx(struct kref *ref);
3839 void hl_hw_queue_encaps_sig_set_sob_info(struct hl_device *hdev,
3840 			struct hl_cs *cs, struct hl_cs_job *job,
3841 			struct hl_cs_compl *cs_cmpl);
3842 
3843 int hl_dec_init(struct hl_device *hdev);
3844 void hl_dec_fini(struct hl_device *hdev);
3845 void hl_dec_ctx_fini(struct hl_ctx *ctx);
3846 
3847 void hl_release_pending_user_interrupts(struct hl_device *hdev);
3848 void hl_abort_waitings_for_completion(struct hl_device *hdev);
3849 int hl_cs_signal_sob_wraparound_handler(struct hl_device *hdev, u32 q_idx,
3850 			struct hl_hw_sob **hw_sob, u32 count, bool encaps_sig);
3851 
3852 int hl_state_dump(struct hl_device *hdev);
3853 const char *hl_state_dump_get_sync_name(struct hl_device *hdev, u32 sync_id);
3854 const char *hl_state_dump_get_monitor_name(struct hl_device *hdev,
3855 					struct hl_mon_state_dump *mon);
3856 void hl_state_dump_free_sync_to_engine_map(struct hl_sync_to_engine_map *map);
3857 __printf(4, 5) int hl_snprintf_resize(char **buf, size_t *size, size_t *offset,
3858 					const char *format, ...);
3859 char *hl_format_as_binary(char *buf, size_t buf_len, u32 n);
3860 const char *hl_sync_engine_to_string(enum hl_sync_engine_type engine_type);
3861 
3862 void hl_mem_mgr_init(struct device *dev, struct hl_mem_mgr *mmg);
3863 void hl_mem_mgr_fini(struct hl_mem_mgr *mmg);
3864 int hl_mem_mgr_mmap(struct hl_mem_mgr *mmg, struct vm_area_struct *vma,
3865 		    void *args);
3866 struct hl_mmap_mem_buf *hl_mmap_mem_buf_get(struct hl_mem_mgr *mmg,
3867 						   u64 handle);
3868 int hl_mmap_mem_buf_put_handle(struct hl_mem_mgr *mmg, u64 handle);
3869 int hl_mmap_mem_buf_put(struct hl_mmap_mem_buf *buf);
3870 struct hl_mmap_mem_buf *
3871 hl_mmap_mem_buf_alloc(struct hl_mem_mgr *mmg,
3872 		      struct hl_mmap_mem_buf_behavior *behavior, gfp_t gfp,
3873 		      void *args);
3874 __printf(2, 3) void hl_engine_data_sprintf(struct engines_data *e, const char *fmt, ...);
3875 void hl_capture_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines,
3876 			u8 flags);
3877 void hl_handle_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines,
3878 			u8 flags, u64 *event_mask);
3879 void hl_capture_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu);
3880 void hl_handle_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu,
3881 				u64 *event_mask);
3882 
3883 #ifdef CONFIG_DEBUG_FS
3884 
3885 void hl_debugfs_init(void);
3886 void hl_debugfs_fini(void);
3887 void hl_debugfs_add_device(struct hl_device *hdev);
3888 void hl_debugfs_remove_device(struct hl_device *hdev);
3889 void hl_debugfs_add_file(struct hl_fpriv *hpriv);
3890 void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
3891 void hl_debugfs_add_cb(struct hl_cb *cb);
3892 void hl_debugfs_remove_cb(struct hl_cb *cb);
3893 void hl_debugfs_add_cs(struct hl_cs *cs);
3894 void hl_debugfs_remove_cs(struct hl_cs *cs);
3895 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
3896 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
3897 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
3898 void hl_debugfs_remove_userptr(struct hl_device *hdev,
3899 				struct hl_userptr *userptr);
3900 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
3901 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
3902 void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
3903 					unsigned long length);
3904 
3905 #else
3906 
3907 static inline void __init hl_debugfs_init(void)
3908 {
3909 }
3910 
3911 static inline void hl_debugfs_fini(void)
3912 {
3913 }
3914 
3915 static inline void hl_debugfs_add_device(struct hl_device *hdev)
3916 {
3917 }
3918 
3919 static inline void hl_debugfs_remove_device(struct hl_device *hdev)
3920 {
3921 }
3922 
3923 static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
3924 {
3925 }
3926 
3927 static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
3928 {
3929 }
3930 
3931 static inline void hl_debugfs_add_cb(struct hl_cb *cb)
3932 {
3933 }
3934 
3935 static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
3936 {
3937 }
3938 
3939 static inline void hl_debugfs_add_cs(struct hl_cs *cs)
3940 {
3941 }
3942 
3943 static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
3944 {
3945 }
3946 
3947 static inline void hl_debugfs_add_job(struct hl_device *hdev,
3948 					struct hl_cs_job *job)
3949 {
3950 }
3951 
3952 static inline void hl_debugfs_remove_job(struct hl_device *hdev,
3953 					struct hl_cs_job *job)
3954 {
3955 }
3956 
3957 static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
3958 					struct hl_userptr *userptr)
3959 {
3960 }
3961 
3962 static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
3963 					struct hl_userptr *userptr)
3964 {
3965 }
3966 
3967 static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
3968 					struct hl_ctx *ctx)
3969 {
3970 }
3971 
3972 static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
3973 					struct hl_ctx *ctx)
3974 {
3975 }
3976 
3977 static inline void hl_debugfs_set_state_dump(struct hl_device *hdev,
3978 					char *data, unsigned long length)
3979 {
3980 }
3981 
3982 #endif
3983 
3984 /* Security */
3985 int hl_unsecure_register(struct hl_device *hdev, u32 mm_reg_addr, int offset,
3986 		const u32 pb_blocks[], struct hl_block_glbl_sec sgs_array[],
3987 		int array_size);
3988 int hl_unsecure_registers(struct hl_device *hdev, const u32 mm_reg_array[],
3989 		int mm_array_size, int offset, const u32 pb_blocks[],
3990 		struct hl_block_glbl_sec sgs_array[], int blocks_array_size);
3991 void hl_config_glbl_sec(struct hl_device *hdev, const u32 pb_blocks[],
3992 		struct hl_block_glbl_sec sgs_array[], u32 block_offset,
3993 		int array_size);
3994 void hl_secure_block(struct hl_device *hdev,
3995 		struct hl_block_glbl_sec sgs_array[], int array_size);
3996 int hl_init_pb_with_mask(struct hl_device *hdev, u32 num_dcores,
3997 		u32 dcore_offset, u32 num_instances, u32 instance_offset,
3998 		const u32 pb_blocks[], u32 blocks_array_size,
3999 		const u32 *regs_array, u32 regs_array_size, u64 mask);
4000 int hl_init_pb(struct hl_device *hdev, u32 num_dcores, u32 dcore_offset,
4001 		u32 num_instances, u32 instance_offset,
4002 		const u32 pb_blocks[], u32 blocks_array_size,
4003 		const u32 *regs_array, u32 regs_array_size);
4004 int hl_init_pb_ranges_with_mask(struct hl_device *hdev, u32 num_dcores,
4005 		u32 dcore_offset, u32 num_instances, u32 instance_offset,
4006 		const u32 pb_blocks[], u32 blocks_array_size,
4007 		const struct range *regs_range_array, u32 regs_range_array_size,
4008 		u64 mask);
4009 int hl_init_pb_ranges(struct hl_device *hdev, u32 num_dcores,
4010 		u32 dcore_offset, u32 num_instances, u32 instance_offset,
4011 		const u32 pb_blocks[], u32 blocks_array_size,
4012 		const struct range *regs_range_array,
4013 		u32 regs_range_array_size);
4014 int hl_init_pb_single_dcore(struct hl_device *hdev, u32 dcore_offset,
4015 		u32 num_instances, u32 instance_offset,
4016 		const u32 pb_blocks[], u32 blocks_array_size,
4017 		const u32 *regs_array, u32 regs_array_size);
4018 int hl_init_pb_ranges_single_dcore(struct hl_device *hdev, u32 dcore_offset,
4019 		u32 num_instances, u32 instance_offset,
4020 		const u32 pb_blocks[], u32 blocks_array_size,
4021 		const struct range *regs_range_array,
4022 		u32 regs_range_array_size);
4023 void hl_ack_pb(struct hl_device *hdev, u32 num_dcores, u32 dcore_offset,
4024 		u32 num_instances, u32 instance_offset,
4025 		const u32 pb_blocks[], u32 blocks_array_size);
4026 void hl_ack_pb_with_mask(struct hl_device *hdev, u32 num_dcores,
4027 		u32 dcore_offset, u32 num_instances, u32 instance_offset,
4028 		const u32 pb_blocks[], u32 blocks_array_size, u64 mask);
4029 void hl_ack_pb_single_dcore(struct hl_device *hdev, u32 dcore_offset,
4030 		u32 num_instances, u32 instance_offset,
4031 		const u32 pb_blocks[], u32 blocks_array_size);
4032 
4033 /* IOCTLs */
4034 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
4035 long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
4036 int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
4037 int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
4038 int hl_wait_ioctl(struct hl_fpriv *hpriv, void *data);
4039 int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
4040 
4041 #endif /* HABANALABSP_H_ */
4042