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