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