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