habanalabs.h 154 KB
Newer Older
1 2
/* SPDX-License-Identifier: GPL-2.0
 *
3
 * Copyright 2016-2022 HabanaLabs, Ltd.
4 5 6 7 8 9 10
 * All Rights Reserved.
 *
 */

#ifndef HABANALABSP_H_
#define HABANALABSP_H_

11
#include "../include/common/cpucp_if.h"
12
#include "../include/common/qman_if.h"
13
#include "../include/hw_ip/mmu/mmu_general.h"
14
#include <uapi/drm/habanalabs_accel.h>
15

16
#include <linux/cdev.h>
17
#include <linux/iopoll.h>
18
#include <linux/irqreturn.h>
19 20
#include <linux/dma-direction.h>
#include <linux/scatterlist.h>
21
#include <linux/hashtable.h>
22
#include <linux/debugfs.h>
23
#include <linux/rwsem.h>
24
#include <linux/eventfd.h>
25
#include <linux/bitfield.h>
26 27 28 29
#include <linux/genalloc.h>
#include <linux/sched/signal.h>
#include <linux/io-64-nonatomic-lo-hi.h>
#include <linux/coresight.h>
30
#include <linux/dma-buf.h>
31

32 33
#include "security.h"

34 35
#define HL_NAME				"habanalabs"

36 37 38
struct hl_device;
struct hl_fpriv;

39
/* Use upper bits of mmap offset to store habana driver specific information.
40
 * bits[63:59] - Encode mmap type
41 42 43 44 45
 * bits[45:0]  - mmap offset value
 *
 * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
 *  defines are w.r.t to PAGE_SIZE
 */
46 47 48
#define HL_MMAP_TYPE_SHIFT		(59 - PAGE_SHIFT)
#define HL_MMAP_TYPE_MASK		(0x1full << HL_MMAP_TYPE_SHIFT)
#define HL_MMAP_TYPE_TS_BUFF		(0x10ull << HL_MMAP_TYPE_SHIFT)
49
#define HL_MMAP_TYPE_BLOCK		(0x4ull << HL_MMAP_TYPE_SHIFT)
50 51
#define HL_MMAP_TYPE_CB			(0x2ull << HL_MMAP_TYPE_SHIFT)

52
#define HL_MMAP_OFFSET_VALUE_MASK	(0x1FFFFFFFFFFFull >> PAGE_SHIFT)
53
#define HL_MMAP_OFFSET_VALUE_GET(off)	(off & HL_MMAP_OFFSET_VALUE_MASK)
54

55 56 57 58 59 60 61 62
#define HL_PENDING_RESET_PER_SEC		10
#define HL_PENDING_RESET_MAX_TRIALS		60 /* 10 minutes */
#define HL_PENDING_RESET_LONG_SEC		60
/*
 * In device fini, wait 10 minutes for user processes to be terminated after we kill them.
 * This is needed to prevent situation of clearing resources while user processes are still alive.
 */
#define HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI	600
63

64
#define HL_HARD_RESET_MAX_TIMEOUT	120
65
#define HL_PLDM_HARD_RESET_MAX_TIMEOUT	(HL_HARD_RESET_MAX_TIMEOUT * 3)
66

67 68
#define HL_DEVICE_TIMEOUT_USEC		1000000 /* 1 s */

69 70
#define HL_HEARTBEAT_PER_USEC		5000000 /* 5 s */

71 72
#define HL_PLL_LOW_JOB_FREQ_USEC	5000000 /* 5 s */

73 74
#define HL_CPUCP_INFO_TIMEOUT_USEC	10000000 /* 10s */
#define HL_CPUCP_EEPROM_TIMEOUT_USEC	10000000 /* 10s */
75
#define HL_CPUCP_MON_DUMP_TIMEOUT_USEC	10000000 /* 10s */
76
#define HL_CPUCP_SEC_ATTEST_INFO_TINEOUT_USEC 10000000 /* 10s */
77

78
#define HL_FW_STATUS_POLL_INTERVAL_USEC		10000 /* 10ms */
79
#define HL_FW_COMMS_STATUS_PLDM_POLL_INTERVAL_USEC	1000000 /* 1s */
80

81 82
#define HL_PCI_ELBI_TIMEOUT_MSEC	10 /* 10ms */

83 84 85
#define HL_SIM_MAX_TIMEOUT_US		100000000 /* 100s */

#define HL_INVALID_QUEUE		UINT_MAX
86

87
#define HL_COMMON_USER_CQ_INTERRUPT_ID	0xFFF
88
#define HL_COMMON_DEC_INTERRUPT_ID	0xFFE
89

90 91
#define HL_STATE_DUMP_HIST_LEN		5

92 93 94
/* Default value for device reset trigger , an invalid value */
#define HL_RESET_TRIGGER_DEFAULT	0xFF

95 96 97
#define OBJ_NAMES_HASH_TABLE_BITS	7 /* 1 << 7 buckets */
#define SYNC_TO_ENGINE_HASH_TABLE_BITS	7 /* 1 << 7 buckets */

98 99 100 101 102 103
/* Memory */
#define MEM_HASH_TABLE_BITS		7 /* 1 << 7 buckets */

/* MMU */
#define MMU_HASH_TABLE_BITS		7 /* 1 << 7 buckets */

104
/**
105
 * enum hl_mmu_page_table_location - mmu page table location
106 107 108 109 110 111 112 113 114 115
 * @MMU_DR_PGT: page-table is located on device DRAM.
 * @MMU_HR_PGT: page-table is located on host memory.
 * @MMU_NUM_PGT_LOCATIONS: number of page-table locations currently supported.
 */
enum hl_mmu_page_table_location {
	MMU_DR_PGT = 0,		/* device-dram-resident MMU PGT */
	MMU_HR_PGT,		/* host resident MMU PGT */
	MMU_NUM_PGT_LOCATIONS	/* num of PGT locations */
};

116 117 118 119 120 121 122 123 124 125 126 127
/**
 * enum hl_mmu_enablement - what mmu modules to enable
 * @MMU_EN_NONE: mmu disabled.
 * @MMU_EN_ALL: enable all.
 * @MMU_EN_PMMU_ONLY: Enable only the PMMU leaving the DMMU disabled.
 */
enum hl_mmu_enablement {
	MMU_EN_NONE = 0,
	MMU_EN_ALL = 1,
	MMU_EN_PMMU_ONLY = 3,	/* N/A for Goya/Gaudi */
};

128 129 130 131
/*
 * HL_RSVD_SOBS 'sync stream' reserved sync objects per QMAN stream
 * HL_RSVD_MONS 'sync stream' reserved monitors per QMAN stream
 */
132 133
#define HL_RSVD_SOBS			2
#define HL_RSVD_MONS			1
134

135 136 137 138 139
/*
 * HL_COLLECTIVE_RSVD_MSTR_MONS 'collective' reserved monitors per QMAN stream
 */
#define HL_COLLECTIVE_RSVD_MSTR_MONS	2

140 141
#define HL_MAX_SOB_VAL			(1 << 15)

142 143 144
#define IS_POWER_OF_2(n)		(n != 0 && ((n & (n - 1)) == 0))
#define IS_MAX_PENDING_CS_VALID(n)	(IS_POWER_OF_2(n) && (n > 1))

145 146
#define HL_PCI_NUM_BARS			6

147 148 149 150 151 152
/* Completion queue entry relates to completed job */
#define HL_COMPLETION_MODE_JOB		0
/* Completion queue entry relates to completed command submission */
#define HL_COMPLETION_MODE_CS		1

#define HL_MAX_DCORES			8
153

154 155 156 157 158 159 160 161 162 163 164 165 166
/* DMA alloc/free wrappers */
#define hl_asic_dma_alloc_coherent(hdev, size, dma_handle, flags) \
	hl_asic_dma_alloc_coherent_caller(hdev, size, dma_handle, flags, __func__)

#define hl_asic_dma_pool_zalloc(hdev, size, mem_flags, dma_handle) \
	hl_asic_dma_pool_zalloc_caller(hdev, size, mem_flags, dma_handle, __func__)

#define hl_asic_dma_free_coherent(hdev, size, cpu_addr, dma_handle) \
	hl_asic_dma_free_coherent_caller(hdev, size, cpu_addr, dma_handle, __func__)

#define hl_asic_dma_pool_free(hdev, vaddr, dma_addr) \
	hl_asic_dma_pool_free_caller(hdev, vaddr, dma_addr, __func__)

167 168 169
/*
 * Reset Flags
 *
170
 * - HL_DRV_RESET_HARD
171 172 173
 *       If set do hard reset to all engines. If not set reset just
 *       compute/DMA engines.
 *
174
 * - HL_DRV_RESET_FROM_RESET_THR
175 176
 *       Set if the caller is the hard-reset thread
 *
177
 * - HL_DRV_RESET_HEARTBEAT
178
 *       Set if reset is due to heartbeat
179
 *
180
 * - HL_DRV_RESET_TDR
181
 *       Set if reset is due to TDR
182
 *
183
 * - HL_DRV_RESET_DEV_RELEASE
184
 *       Set if reset is due to device release
185
 *
186
 * - HL_DRV_RESET_BYPASS_REQ_TO_FW
187 188
 *       F/W will perform the reset. No need to ask it to reset the device. This is relevant
 *       only when running with secured f/w
189
 *
190
 * - HL_DRV_RESET_FW_FATAL_ERR
191
 *       Set if reset is due to a fatal error from FW
192 193 194
 *
 * - HL_DRV_RESET_DELAY
 *       Set if a delay should be added before the reset
195 196 197
 *
 * - HL_DRV_RESET_FROM_WD_THR
 *       Set if the caller is the device release watchdog thread
198
 */
199

200 201 202 203 204 205 206
#define HL_DRV_RESET_HARD		(1 << 0)
#define HL_DRV_RESET_FROM_RESET_THR	(1 << 1)
#define HL_DRV_RESET_HEARTBEAT		(1 << 2)
#define HL_DRV_RESET_TDR		(1 << 3)
#define HL_DRV_RESET_DEV_RELEASE	(1 << 4)
#define HL_DRV_RESET_BYPASS_REQ_TO_FW	(1 << 5)
#define HL_DRV_RESET_FW_FATAL_ERR	(1 << 6)
207
#define HL_DRV_RESET_DELAY		(1 << 7)
208
#define HL_DRV_RESET_FROM_WD_THR	(1 << 8)
209

210 211 212 213
/*
 * Security
 */

214 215 216
#define HL_PB_SHARED		1
#define HL_PB_NA		0
#define HL_PB_SINGLE_INSTANCE	1
217
#define HL_BLOCK_SIZE		0x1000
218 219 220 221 222 223 224 225 226 227 228 229 230
#define HL_BLOCK_GLBL_ERR_MASK	0xF40
#define HL_BLOCK_GLBL_ERR_ADDR	0xF44
#define HL_BLOCK_GLBL_ERR_CAUSE	0xF48
#define HL_BLOCK_GLBL_SEC_OFFS	0xF80
#define HL_BLOCK_GLBL_SEC_SIZE	(HL_BLOCK_SIZE - HL_BLOCK_GLBL_SEC_OFFS)
#define HL_BLOCK_GLBL_SEC_LEN	(HL_BLOCK_GLBL_SEC_SIZE / sizeof(u32))
#define UNSET_GLBL_SEC_BIT(array, b) ((array)[((b) / 32)] |= (1 << ((b) % 32)))

enum hl_protection_levels {
	SECURED_LVL,
	PRIVILEGED_LVL,
	NON_SECURED_LVL
};
231 232 233 234 235

/**
 * struct iterate_module_ctx - HW module iterator
 * @fn: function to apply to each HW module instance
 * @data: optional internal data to the function iterator
236
 * @rc: return code for optional use of iterator/iterator-caller
237 238 239 240 241 242 243 244 245
 */
struct iterate_module_ctx {
	/*
	 * callback for the HW module iterator
	 * @hdev: pointer to the habanalabs device structure
	 * @block: block (ASIC specific definition can be dcore/hdcore)
	 * @inst: HW module instance within the block
	 * @offset: current HW module instance offset from the 1-st HW module instance
	 *          in the 1-st block
246
	 * @ctx: the iterator context.
247
	 */
248 249
	void (*fn)(struct hl_device *hdev, int block, int inst, u32 offset,
			struct iterate_module_ctx *ctx);
250
	void *data;
251
	int rc;
252 253
};

254 255 256 257
struct hl_block_glbl_sec {
	u32 sec_array[HL_BLOCK_GLBL_SEC_LEN];
};

258 259
#define HL_MAX_SOBS_PER_MONITOR	8

260 261 262 263
/**
 * struct hl_gen_wait_properties - properties for generating a wait CB
 * @data: command buffer
 * @q_idx: queue id is used to extract fence register address
264 265
 * @size: offset in command buffer
 * @sob_base: SOB base to use in this wait CB
266 267
 * @sob_val: SOB value to wait for
 * @mon_id: monitor to use in this wait CB
268
 * @sob_mask: each bit represents a SOB offset from sob_base to be used
269 270 271 272
 */
struct hl_gen_wait_properties {
	void	*data;
	u32	q_idx;
273 274
	u32	size;
	u16	sob_base;
275 276
	u16	sob_val;
	u16	mon_id;
277
	u8	sob_mask;
278 279
};

280 281
/**
 * struct pgt_info - MMU hop page info.
282 283
 * @node: hash linked-list node for the pgts on host (shadow pgts for device resident MMU and
 *        actual pgts for host resident MMU).
284
 * @phys_addr: physical address of the pgt.
285 286
 * @virt_addr: host virtual address of the pgt (see above device/host resident).
 * @shadow_addr: shadow hop in the host for device resident MMU.
287
 * @ctx: pointer to the owner ctx.
288 289 290 291 292
 * @num_of_ptes: indicates how many ptes are used in the pgt. used only for dynamically
 *               allocated HOPs (all HOPs but HOP0)
 *
 * The MMU page tables hierarchy can be placed either on the device's DRAM (in which case shadow
 * pgts will be stored on host memory) or on host memory (in which case no shadow is required).
293
 *
294 295 296 297
 * When a new level (hop) is needed during mapping this structure will be used to describe
 * the newly allocated hop as well as to track number of PTEs in it.
 * During unmapping, if no valid PTEs remained in the page of a newly allocated hop, it is
 * freed with its pgt_info structure.
298 299
 */
struct pgt_info {
300 301
	struct hlist_node	node;
	u64			phys_addr;
302
	u64			virt_addr;
303 304 305
	u64			shadow_addr;
	struct hl_ctx		*ctx;
	int			num_of_ptes;
306 307
};

308 309 310 311 312 313 314 315 316 317
/**
 * enum hl_pci_match_mode - pci match mode per region
 * @PCI_ADDRESS_MATCH_MODE: address match mode
 * @PCI_BAR_MATCH_MODE: bar match mode
 */
enum hl_pci_match_mode {
	PCI_ADDRESS_MATCH_MODE,
	PCI_BAR_MATCH_MODE
};

318 319
/**
 * enum hl_fw_component - F/W components to read version through registers.
320
 * @FW_COMP_BOOT_FIT: boot fit.
321
 * @FW_COMP_PREBOOT: preboot.
322
 * @FW_COMP_LINUX: linux.
323 324
 */
enum hl_fw_component {
325
	FW_COMP_BOOT_FIT,
326 327
	FW_COMP_PREBOOT,
	FW_COMP_LINUX,
328 329
};

330
/**
331
 * enum hl_fw_types - F/W types present in the system
332
 * @FW_TYPE_NONE: no FW component indication
333 334
 * @FW_TYPE_LINUX: Linux image for device CPU
 * @FW_TYPE_BOOT_CPU: Boot image for device CPU
335 336
 * @FW_TYPE_PREBOOT_CPU: Indicates pre-loaded CPUs are present in the system
 *                       (preboot, ppboot etc...)
337 338 339
 * @FW_TYPE_ALL_TYPES: Mask for all types
 */
enum hl_fw_types {
340
	FW_TYPE_NONE = 0x0,
341 342
	FW_TYPE_LINUX = 0x1,
	FW_TYPE_BOOT_CPU = 0x2,
343 344 345
	FW_TYPE_PREBOOT_CPU = 0x4,
	FW_TYPE_ALL_TYPES =
		(FW_TYPE_LINUX | FW_TYPE_BOOT_CPU | FW_TYPE_PREBOOT_CPU)
346 347
};

348 349 350 351 352 353 354 355
/**
 * enum hl_queue_type - Supported QUEUE types.
 * @QUEUE_TYPE_NA: queue is not available.
 * @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the
 *                  host.
 * @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's
 *			memories and/or operates the compute engines.
 * @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU.
356 357
 * @QUEUE_TYPE_HW: queue of DMA and compute engines jobs, for which completion
 *                 notifications are sent by H/W.
358 359 360 361 362
 */
enum hl_queue_type {
	QUEUE_TYPE_NA,
	QUEUE_TYPE_EXT,
	QUEUE_TYPE_INT,
363 364
	QUEUE_TYPE_CPU,
	QUEUE_TYPE_HW
365 366
};

367 368 369
enum hl_cs_type {
	CS_TYPE_DEFAULT,
	CS_TYPE_SIGNAL,
370
	CS_TYPE_WAIT,
371 372
	CS_TYPE_COLLECTIVE_WAIT,
	CS_RESERVE_SIGNALS,
373
	CS_UNRESERVE_SIGNALS,
374
	CS_TYPE_ENGINE_CORE,
375
	CS_TYPE_ENGINES,
376
	CS_TYPE_FLUSH_PCI_HBW_WRITES,
377 378
};

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/*
 * struct hl_inbound_pci_region - inbound region descriptor
 * @mode: pci match mode for this region
 * @addr: region target address
 * @size: region size in bytes
 * @offset_in_bar: offset within bar (address match mode)
 * @bar: bar id
 */
struct hl_inbound_pci_region {
	enum hl_pci_match_mode	mode;
	u64			addr;
	u64			size;
	u64			offset_in_bar;
	u8			bar;
};

/*
 * struct hl_outbound_pci_region - outbound region descriptor
 * @addr: region target address
 * @size: region size in bytes
 */
struct hl_outbound_pci_region {
	u64	addr;
	u64	size;
};

405 406 407 408 409 410 411 412 413 414 415
/*
 * enum queue_cb_alloc_flags - Indicates queue support for CBs that
 * allocated by Kernel or by User
 * @CB_ALLOC_KERNEL: support only CBs that allocated by Kernel
 * @CB_ALLOC_USER: support only CBs that allocated by User
 */
enum queue_cb_alloc_flags {
	CB_ALLOC_KERNEL = 0x1,
	CB_ALLOC_USER   = 0x2
};

416 417 418 419 420
/*
 * struct hl_hw_sob - H/W SOB info.
 * @hdev: habanalabs device structure.
 * @kref: refcount of this SOB. The SOB will reset once the refcount is zero.
 * @sob_id: id of this SOB.
421
 * @sob_addr: the sob offset from the base address.
422
 * @q_idx: the H/W queue that uses this SOB.
423
 * @need_reset: reset indication set when switching to the other sob.
424 425 426 427 428
 */
struct hl_hw_sob {
	struct hl_device	*hdev;
	struct kref		kref;
	u32			sob_id;
429
	u32			sob_addr;
430
	u32			q_idx;
431
	bool			need_reset;
432 433
};

434 435 436 437 438 439
enum hl_collective_mode {
	HL_COLLECTIVE_NOT_SUPPORTED = 0x0,
	HL_COLLECTIVE_MASTER = 0x1,
	HL_COLLECTIVE_SLAVE = 0x2
};

440 441 442
/**
 * struct hw_queue_properties - queue information.
 * @type: queue type.
443 444 445 446
 * @cb_alloc_flags: bitmap which indicates if the hw queue supports CB
 *                  that allocated by the Kernel driver and therefore,
 *                  a CB handle can be provided for jobs on this queue.
 *                  Otherwise, a CB address must be provided.
447
 * @collective_mode: collective mode of current queue
448 449
 * @driver_only: true if only the driver is allowed to send a job to this queue,
 *               false otherwise.
450
 * @binned: True if the queue is binned out and should not be used
451
 * @supports_sync_stream: True if queue supports sync stream
452 453
 */
struct hw_queue_properties {
454 455 456 457 458 459
	enum hl_queue_type		type;
	enum queue_cb_alloc_flags	cb_alloc_flags;
	enum hl_collective_mode		collective_mode;
	u8				driver_only;
	u8				binned;
	u8				supports_sync_stream;
460
};
461

462
/**
463
 * enum vm_type - virtual memory mapping request information.
464
 * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
465
 * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
466
 */
467
enum vm_type {
468 469
	VM_TYPE_USERPTR = 0x1,
	VM_TYPE_PHYS_PACK = 0x2
470 471
};

472 473 474 475
/**
 * enum mmu_op_flags - mmu operation relevant information.
 * @MMU_OP_USERPTR: operation on user memory (host resident).
 * @MMU_OP_PHYS_PACK: operation on DRAM (device resident).
476 477
 * @MMU_OP_CLEAR_MEMCACHE: operation has to clear memcache.
 * @MMU_OP_SKIP_LOW_CACHE_INV: operation is allowed to skip parts of cache invalidation.
478 479 480
 */
enum mmu_op_flags {
	MMU_OP_USERPTR = 0x1,
481 482 483
	MMU_OP_PHYS_PACK = 0x2,
	MMU_OP_CLEAR_MEMCACHE = 0x4,
	MMU_OP_SKIP_LOW_CACHE_INV = 0x8,
484 485 486
};


487 488 489 490 491 492 493 494 495 496 497 498
/**
 * enum hl_device_hw_state - H/W device state. use this to understand whether
 *                           to do reset before hw_init or not
 * @HL_DEVICE_HW_STATE_CLEAN: H/W state is clean. i.e. after hard reset
 * @HL_DEVICE_HW_STATE_DIRTY: H/W state is dirty. i.e. we started to execute
 *                            hw_init
 */
enum hl_device_hw_state {
	HL_DEVICE_HW_STATE_CLEAN = 0,
	HL_DEVICE_HW_STATE_DIRTY
};

499 500
#define HL_MMU_VA_ALIGNMENT_NOT_NEEDED 0

501 502
/**
 * struct hl_mmu_properties - ASIC specific MMU address translation properties.
503 504
 * @start_addr: virtual start address of the memory region.
 * @end_addr: virtual end address of the memory region.
505 506
 * @hop_shifts: array holds HOPs shifts.
 * @hop_masks: array holds HOPs masks.
507
 * @last_mask: mask to get the bit indicating this is the last hop.
508
 * @pgt_size: size for page tables.
509 510
 * @supported_pages_mask: bitmask for supported page size (relevant only for MMUs
 *                        supporting multiple page size).
511
 * @page_size: default page size used to allocate memory.
512
 * @num_hops: The amount of hops supported by the translation table.
513 514
 * @hop_table_size: HOP table size.
 * @hop0_tables_total_size: total size for all HOP0 tables.
515 516
 * @host_resident: Should the MMU page table reside in host memory or in the
 *                 device DRAM.
517 518
 */
struct hl_mmu_properties {
519 520
	u64	start_addr;
	u64	end_addr;
521 522
	u64	hop_shifts[MMU_HOP_MAX];
	u64	hop_masks[MMU_HOP_MAX];
523
	u64	last_mask;
524
	u64	pgt_size;
525
	u64	supported_pages_mask;
526
	u32	page_size;
527
	u32	num_hops;
528 529
	u32	hop_table_size;
	u32	hop0_tables_total_size;
530
	u8	host_resident;
531 532
};

533 534 535 536 537 538 539 540 541 542
/**
 * struct hl_hints_range - hint addresses reserved va range.
 * @start_addr: start address of the va range.
 * @end_addr: end address of the va range.
 */
struct hl_hints_range {
	u64 start_addr;
	u64 end_addr;
};

543 544
/**
 * struct asic_fixed_properties - ASIC specific immutable properties.
545
 * @hw_queues_props: H/W queues properties.
546 547
 * @special_blocks: points to an array containing special blocks info.
 * @skip_special_blocks_cfg: special blocks skip configs.
548
 * @cpucp_info: received various information from CPU-CP regarding the H/W, e.g.
549
 *		available sensors.
550 551
 * @uboot_ver: F/W U-boot version.
 * @preboot_ver: F/W Preboot version.
552 553
 * @dmmu: DRAM MMU address translation properties.
 * @pmmu: PCI (host) MMU address translation properties.
554 555
 * @pmmu_huge: PCI (host) MMU address translation properties for memory
 *              allocated with huge pages.
556 557 558 559
 * @hints_dram_reserved_va_range: dram hint addresses reserved range.
 * @hints_host_reserved_va_range: host hint addresses reserved range.
 * @hints_host_hpage_reserved_va_range: host huge page hint addresses reserved
 *                                      range.
560 561 562 563 564 565 566 567
 * @sram_base_address: SRAM physical start address.
 * @sram_end_address: SRAM physical end address.
 * @sram_user_base_address - SRAM physical start address for user access.
 * @dram_base_address: DRAM physical start address.
 * @dram_end_address: DRAM physical end address.
 * @dram_user_base_address: DRAM physical start address for user access.
 * @dram_size: DRAM total size.
 * @dram_pci_bar_size: size of PCI bar towards DRAM.
568
 * @max_power_default: max power of the device after reset.
569
 * @dc_power_default: power consumed by the device in mode idle.
570 571
 * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
 *                                      fault.
572 573
 * @pcie_dbi_base_address: Base address of the PCIE_DBI block.
 * @pcie_aux_dbi_reg_addr: Address of the PCIE_AUX DBI register.
574
 * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
575
 * @mmu_dram_default_page_addr: DRAM default page physical address.
576 577 578 579
 * @tpc_enabled_mask: which TPCs are enabled.
 * @tpc_binning_mask: which TPCs are binned. 0 means usable and 1 means binned.
 * @dram_enabled_mask: which DRAMs are enabled.
 * @dram_binning_mask: which DRAMs are binned. 0 means usable, 1 means binned.
580 581
 * @dram_hints_align_mask: dram va hint addresses alignment mask which is used
 *                  for hints validity check.
582 583 584
 * @cfg_base_address: config space base address.
 * @mmu_cache_mng_addr: address of the MMU cache.
 * @mmu_cache_mng_size: size of the MMU cache.
585 586 587 588
 * @device_dma_offset_for_host_access: the offset to add to host DMA addresses
 *                                     to enable the device to access them.
 * @host_base_address: host physical start address for host DMA from device
 * @host_end_address: host physical end address for host DMA from device
589
 * @max_freq_value: current max clk frequency.
590 591
 * @engine_core_interrupt_reg_addr: interrupt register address for engine core to use
 *                                  in order to raise events toward FW.
592 593
 * @clk_pll_index: clock PLL index that specify which PLL determines the clock
 *                 we display to the user
594 595 596 597 598
 * @mmu_pgt_size: MMU page tables total size.
 * @mmu_pte_size: PTE size in MMU page tables.
 * @mmu_hop_table_size: MMU hop table size.
 * @mmu_hop0_tables_total_size: total size of MMU hop0 tables.
 * @dram_page_size: page size for MMU DRAM allocation.
599 600 601
 * @cfg_size: configuration space size on SRAM.
 * @sram_size: total size of SRAM.
 * @max_asid: maximum number of open contexts (ASIDs).
602
 * @num_of_events: number of possible internal H/W IRQs.
603 604 605 606
 * @psoc_pci_pll_nr: PCI PLL NR value.
 * @psoc_pci_pll_nf: PCI PLL NF value.
 * @psoc_pci_pll_od: PCI PLL OD value.
 * @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value.
607
 * @psoc_timestamp_frequency: frequency of the psoc timestamp clock.
608
 * @high_pll: high PLL frequency used by the device.
609 610
 * @cb_pool_cb_cnt: number of CBs in the CB pool.
 * @cb_pool_cb_size: size of each CB in the CB pool.
611
 * @decoder_enabled_mask: which decoders are enabled.
612 613
 * @decoder_binning_mask: which decoders are binned, 0 means usable and 1 means binned.
 * @rotator_enabled_mask: which rotators are enabled.
614 615 616
 * @edma_enabled_mask: which EDMAs are enabled.
 * @edma_binning_mask: which EDMAs are binned, 0 means usable and 1 means
 *                     binned (at most one binned DMA).
617 618
 * @max_pending_cs: maximum of concurrent pending command submissions
 * @max_queues: maximum amount of queues in the system
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
 * @fw_preboot_cpu_boot_dev_sts0: bitmap representation of preboot cpu
 *                                capabilities reported by FW, bit description
 *                                can be found in CPU_BOOT_DEV_STS0
 * @fw_preboot_cpu_boot_dev_sts1: bitmap representation of preboot cpu
 *                                capabilities reported by FW, bit description
 *                                can be found in CPU_BOOT_DEV_STS1
 * @fw_bootfit_cpu_boot_dev_sts0: bitmap representation of boot cpu security
 *                                status reported by FW, bit description can be
 *                                found in CPU_BOOT_DEV_STS0
 * @fw_bootfit_cpu_boot_dev_sts1: bitmap representation of boot cpu security
 *                                status reported by FW, bit description can be
 *                                found in CPU_BOOT_DEV_STS1
 * @fw_app_cpu_boot_dev_sts0: bitmap representation of application security
 *                            status reported by FW, bit description can be
 *                            found in CPU_BOOT_DEV_STS0
 * @fw_app_cpu_boot_dev_sts1: bitmap representation of application security
 *                            status reported by FW, bit description can be
 *                            found in CPU_BOOT_DEV_STS1
637 638 639 640 641 642 643
 * @max_dec: maximum number of decoders
 * @hmmu_hif_enabled_mask: mask of HMMUs/HIFs that are not isolated (enabled)
 *                         1- enabled, 0- isolated.
 * @faulty_dram_cluster_map: mask of faulty DRAM cluster.
 *                         1- faulty cluster, 0- good cluster.
 * @xbar_edge_enabled_mask: mask of XBAR_EDGEs that are not isolated (enabled)
 *                          1- enabled, 0- isolated.
644 645 646 647
 * @device_mem_alloc_default_page_size: may be different than dram_page_size only for ASICs for
 *                                      which the property supports_user_set_page_size is true
 *                                      (i.e. the DRAM supports multiple page sizes), otherwise
 *                                      it will shall  be equal to dram_page_size.
648 649
 * @num_engine_cores: number of engine cpu cores.
 * @max_num_of_engines: maximum number of all engines in the ASIC.
650 651
 * @num_of_special_blocks: special_blocks array size.
 * @glbl_err_cause_num: global err cause number.
652 653
 * @hbw_flush_reg: register to read to generate HBW flush. value of 0 means HBW flush is
 *                 not supported.
654 655
 * @collective_first_sob: first sync object available for collective use
 * @collective_first_mon: first monitor available for collective use
656 657
 * @sync_stream_first_sob: first sync object available for sync stream use
 * @sync_stream_first_mon: first monitor available for sync stream use
658 659
 * @first_available_user_sob: first sob available for the user
 * @first_available_user_mon: first monitor available for the user
660
 * @first_available_user_interrupt: first available interrupt reserved for the user
661
 * @first_available_cq: first available CQ for the user.
662
 * @user_interrupt_count: number of user interrupts.
663
 * @user_dec_intr_count: number of decoder interrupts exposed to user.
664
 * @tpc_interrupt_id: interrupt id for TPC to use in order to raise events towards the host.
665
 * @cache_line_size: device cache line size.
666 667
 * @server_type: Server type that the ASIC is currently installed in.
 *               The value is according to enum hl_server_type in uapi file.
668
 * @completion_queues_count: number of completion queues.
669 670 671
 * @completion_mode: 0 - job based completion, 1 - cs based completion
 * @mme_master_slave_mode: 0 - Each MME works independently, 1 - MME works
 *                         in Master/Slave mode
672 673
 * @fw_security_enabled: true if security measures are enabled in firmware,
 *                       false otherwise
674 675 676 677
 * @fw_cpu_boot_dev_sts0_valid: status bits are valid and can be fetched from
 *                              BOOT_DEV_STS0
 * @fw_cpu_boot_dev_sts1_valid: status bits are valid and can be fetched from
 *                              BOOT_DEV_STS1
678
 * @dram_supports_virtual_memory: is there an MMU towards the DRAM
679
 * @hard_reset_done_by_fw: true if firmware is handling hard reset flow
680
 * @num_functional_hbms: number of functional HBMs in each DCORE.
681
 * @hints_range_reservation: device support hint addresses range reservation.
682
 * @iatu_done_by_fw: true if iATU configuration is being done by FW.
683
 * @dynamic_fw_load: is dynamic FW load is supported.
684 685
 * @gic_interrupts_enable: true if FW is not blocking GIC controller,
 *                         false otherwise.
686 687
 * @use_get_power_for_reset_history: To support backward compatibility for Goya
 *                                   and Gaudi
688
 * @supports_compute_reset: is a reset which is not a hard-reset supported by this asic.
689 690 691 692 693 694
 * @allow_inference_soft_reset: true if the ASIC supports soft reset that is
 *                              initiated by user or TDR. This is only true
 *                              in inference ASICs, as there is no real-world
 *                              use-case of doing soft-reset in training (due
 *                              to the fact that training runs on multiple
 *                              devices)
695
 * @configurable_stop_on_err: is stop-on-error option configurable via debugfs.
696
 * @set_max_power_on_device_init: true if need to set max power in F/W on device init.
697
 * @supports_user_set_page_size: true if user can set the allocation page size.
698
 * @dma_mask: the dma mask to be set for this device
699
 * @supports_advanced_cpucp_rc: true if new cpucp opcodes are supported.
700
 * @supports_engine_modes: true if changing engines/engine_cores modes is supported.
701 702
 */
struct asic_fixed_properties {
703
	struct hw_queue_properties	*hw_queues_props;
704 705
	struct hl_special_block_info	*special_blocks;
	struct hl_skip_blocks_cfg	skip_special_blocks_cfg;
706
	struct cpucp_info		cpucp_info;
707 708 709 710
	char				uboot_ver[VERSION_MAX_LEN];
	char				preboot_ver[VERSION_MAX_LEN];
	struct hl_mmu_properties	dmmu;
	struct hl_mmu_properties	pmmu;
711
	struct hl_mmu_properties	pmmu_huge;
712 713 714
	struct hl_hints_range		hints_dram_reserved_va_range;
	struct hl_hints_range		hints_host_reserved_va_range;
	struct hl_hints_range		hints_host_hpage_reserved_va_range;
715 716 717 718 719 720 721 722 723
	u64				sram_base_address;
	u64				sram_end_address;
	u64				sram_user_base_address;
	u64				dram_base_address;
	u64				dram_end_address;
	u64				dram_user_base_address;
	u64				dram_size;
	u64				dram_pci_bar_size;
	u64				max_power_default;
724
	u64				dc_power_default;
725 726 727 728 729
	u64				dram_size_for_default_page_mapping;
	u64				pcie_dbi_base_address;
	u64				pcie_aux_dbi_reg_addr;
	u64				mmu_pgt_addr;
	u64				mmu_dram_default_page_addr;
730 731 732 733
	u64				tpc_enabled_mask;
	u64				tpc_binning_mask;
	u64				dram_enabled_mask;
	u64				dram_binning_mask;
734
	u64				dram_hints_align_mask;
735 736 737
	u64				cfg_base_address;
	u64				mmu_cache_mng_addr;
	u64				mmu_cache_mng_size;
738
	u64				device_dma_offset_for_host_access;
739 740
	u64				host_base_address;
	u64				host_end_address;
741
	u64				max_freq_value;
742
	u64				engine_core_interrupt_reg_addr;
743
	u32				clk_pll_index;
744 745 746 747 748 749 750 751 752 753 754 755 756
	u32				mmu_pgt_size;
	u32				mmu_pte_size;
	u32				mmu_hop_table_size;
	u32				mmu_hop0_tables_total_size;
	u32				dram_page_size;
	u32				cfg_size;
	u32				sram_size;
	u32				max_asid;
	u32				num_of_events;
	u32				psoc_pci_pll_nr;
	u32				psoc_pci_pll_nf;
	u32				psoc_pci_pll_od;
	u32				psoc_pci_pll_div_factor;
757
	u32				psoc_timestamp_frequency;
758 759 760
	u32				high_pll;
	u32				cb_pool_cb_cnt;
	u32				cb_pool_cb_size;
761 762
	u32				decoder_enabled_mask;
	u32				decoder_binning_mask;
763
	u32				rotator_enabled_mask;
764 765
	u32				edma_enabled_mask;
	u32				edma_binning_mask;
766
	u32				max_pending_cs;
767
	u32				max_queues;
768 769 770 771 772 773
	u32				fw_preboot_cpu_boot_dev_sts0;
	u32				fw_preboot_cpu_boot_dev_sts1;
	u32				fw_bootfit_cpu_boot_dev_sts0;
	u32				fw_bootfit_cpu_boot_dev_sts1;
	u32				fw_app_cpu_boot_dev_sts0;
	u32				fw_app_cpu_boot_dev_sts1;
774 775 776 777
	u32				max_dec;
	u32				hmmu_hif_enabled_mask;
	u32				faulty_dram_cluster_map;
	u32				xbar_edge_enabled_mask;
778
	u32				device_mem_alloc_default_page_size;
779
	u32				num_engine_cores;
780
	u32				max_num_of_engines;
781 782
	u32				num_of_special_blocks;
	u32				glbl_err_cause_num;
783
	u32				hbw_flush_reg;
784 785
	u16				collective_first_sob;
	u16				collective_first_mon;
786 787
	u16				sync_stream_first_sob;
	u16				sync_stream_first_mon;
788 789
	u16				first_available_user_sob[HL_MAX_DCORES];
	u16				first_available_user_mon[HL_MAX_DCORES];
790
	u16				first_available_user_interrupt;
791
	u16				first_available_cq[HL_MAX_DCORES];
792
	u16				user_interrupt_count;
793
	u16				user_dec_intr_count;
794
	u16				tpc_interrupt_id;
795
	u16				cache_line_size;
796
	u16				server_type;
797
	u8				completion_queues_count;
798 799
	u8				completion_mode;
	u8				mme_master_slave_mode;
800
	u8				fw_security_enabled;
801 802
	u8				fw_cpu_boot_dev_sts0_valid;
	u8				fw_cpu_boot_dev_sts1_valid;
803
	u8				dram_supports_virtual_memory;
804
	u8				hard_reset_done_by_fw;
805
	u8				num_functional_hbms;
806
	u8				hints_range_reservation;
807
	u8				iatu_done_by_fw;
808
	u8				dynamic_fw_load;
809
	u8				gic_interrupts_enable;
810
	u8				use_get_power_for_reset_history;
811
	u8				supports_compute_reset;
812
	u8				allow_inference_soft_reset;
813
	u8				configurable_stop_on_err;
814
	u8				set_max_power_on_device_init;
815
	u8				supports_user_set_page_size;
816
	u8				dma_mask;
817
	u8				supports_advanced_cpucp_rc;
818
	u8				supports_engine_modes;
819 820
};

821 822 823 824
/**
 * struct hl_fence - software synchronization primitive
 * @completion: fence is implemented using completion
 * @refcount: refcount for this fence
825
 * @cs_sequence: sequence of the corresponding command submission
826 827
 * @stream_master_qid_map: streams masters QID bitmap to represent all streams
 *                         masters QIDs that multi cs is waiting on
828
 * @error: mark this fence with error
829
 * @timestamp: timestamp upon completion
830 831 832
 * @mcs_handling_done: indicates that corresponding command submission has
 *                     finished msc handling, this does not mean it was part
 *                     of the mcs
833 834 835 836
 */
struct hl_fence {
	struct completion	completion;
	struct kref		refcount;
837
	u64			cs_sequence;
838
	u32			stream_master_qid_map;
839
	int			error;
840
	ktime_t			timestamp;
841
	u8			mcs_handling_done;
842 843
};

844
/**
845
 * struct hl_cs_compl - command submission completion object.
846
 * @base_fence: hl fence object.
847 848
 * @lock: spinlock to protect fence.
 * @hdev: habanalabs device structure.
849
 * @hw_sob: the H/W SOB used in this signal/wait CS.
850
 * @encaps_sig_hdl: encaps signals handler.
851
 * @cs_seq: command submission sequence number.
852 853
 * @type: type of the CS - signal/wait.
 * @sob_val: the SOB value that is used in this signal/wait CS.
854
 * @sob_group: the SOB group that is used in this collective wait CS.
855 856
 * @encaps_signals: indication whether it's a completion object of cs with
 * encaps signals or not.
857
 */
858
struct hl_cs_compl {
859
	struct hl_fence		base_fence;
860 861
	spinlock_t		lock;
	struct hl_device	*hdev;
862
	struct hl_hw_sob	*hw_sob;
863
	struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
864
	u64			cs_seq;
865 866
	enum hl_cs_type		type;
	u16			sob_val;
867
	u16			sob_group;
868
	bool			encaps_signals;
869
};
870

871 872 873 874
/*
 * Command Buffers
 */

875 876 877 878 879 880 881 882 883 884 885 886
/**
 * struct hl_ts_buff - describes a timestamp buffer.
 * @kernel_buff_address: Holds the internal buffer's kernel virtual address.
 * @user_buff_address: Holds the user buffer's kernel virtual address.
 * @kernel_buff_size: Holds the internal kernel buffer size.
 */
struct hl_ts_buff {
	void			*kernel_buff_address;
	void			*user_buff_address;
	u32			kernel_buff_size;
};

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
struct hl_mmap_mem_buf;

/**
 * struct hl_mem_mgr - describes unified memory manager for mappable memory chunks.
 * @dev: back pointer to the owning device
 * @lock: protects handles
 * @handles: an idr holding all active handles to the memory buffers in the system.
 */
struct hl_mem_mgr {
	struct device *dev;
	spinlock_t lock;
	struct idr handles;
};

/**
902
 * struct hl_mmap_mem_buf_behavior - describes unified memory manager buffer behavior
903
 * @topic: string identifier used for logging
904 905
 * @mem_id: memory type identifier, embedded in the handle and used to identify
 *          the memory type by handle.
906 907 908 909 910
 * @alloc: callback executed on buffer allocation, shall allocate the memory,
 *         set it under buffer private, and set mappable size.
 * @mmap: callback executed on mmap, must map the buffer to vma
 * @release: callback executed on release, must free the resources used by the buffer
 */
911
struct hl_mmap_mem_buf_behavior {
912
	const char *topic;
913 914
	u64 mem_id;

915 916 917 918 919 920
	int (*alloc)(struct hl_mmap_mem_buf *buf, gfp_t gfp, void *args);
	int (*mmap)(struct hl_mmap_mem_buf *buf, struct vm_area_struct *vma, void *args);
	void (*release)(struct hl_mmap_mem_buf *buf);
};

/**
921 922
 * struct hl_mmap_mem_buf - describes a single unified memory buffer
 * @behavior: buffer behavior
923 924 925 926 927 928 929 930 931 932 933
 * @mmg: back pointer to the unified memory manager
 * @refcount: reference counter for buffer users
 * @private: pointer to buffer behavior private data
 * @mmap: atomic boolean indicating whether or not the buffer is mapped right now
 * @real_mapped_size: the actual size of buffer mapped, after part of it may be released,
 *                   may change at runtime.
 * @mappable_size: the original mappable size of the buffer, does not change after
 *                 the allocation.
 * @handle: the buffer id in mmg handles store
 */
struct hl_mmap_mem_buf {
934
	struct hl_mmap_mem_buf_behavior *behavior;
935 936 937 938 939 940
	struct hl_mem_mgr *mmg;
	struct kref refcount;
	void *private;
	atomic_t mmap;
	u64 real_mapped_size;
	u64 mappable_size;
941
	u64 handle;
942 943
};

944 945 946
/**
 * struct hl_cb - describes a Command Buffer.
 * @hdev: pointer to device this CB belongs to.
947
 * @ctx: pointer to the CB owner's context.
948
 * @buf: back pointer to the parent mappable memory buffer
949
 * @debugfs_list: node in debugfs list of command buffers.
950 951
 * @pool_list: node in pool list of command buffers.
 * @kernel_address: Holds the CB's kernel virtual address.
952
 * @virtual_addr: Holds the CB's virtual address.
953 954
 * @bus_address: Holds the CB's DMA address.
 * @size: holds the CB's size.
955
 * @roundup_size: holds the cb size after roundup to page size.
956
 * @cs_cnt: holds number of CS that this CB participates in.
957
 * @is_handle_destroyed: atomic boolean indicating whether or not the CB handle was destroyed.
958
 * @is_pool: true if CB was acquired from the pool, false otherwise.
959
 * @is_internal: internally allocated
960
 * @is_mmu_mapped: true if the CB is mapped to the device's MMU.
961 962 963
 */
struct hl_cb {
	struct hl_device	*hdev;
964
	struct hl_ctx		*ctx;
965
	struct hl_mmap_mem_buf	*buf;
966
	struct list_head	debugfs_list;
967
	struct list_head	pool_list;
968
	void			*kernel_address;
969
	u64			virtual_addr;
970 971
	dma_addr_t		bus_address;
	u32			size;
972
	u32			roundup_size;
973
	atomic_t		cs_cnt;
974
	atomic_t		is_handle_destroyed;
975
	u8			is_pool;
976
	u8			is_internal;
977
	u8			is_mmu_mapped;
978 979 980
};


981 982 983 984 985 986
/*
 * QUEUES
 */

struct hl_cs_job;

987 988
/* Queue length of external and HW queues */
#define HL_QUEUE_LENGTH			4096
989 990
#define HL_QUEUE_SIZE_IN_BYTES		(HL_QUEUE_LENGTH * HL_BD_SIZE)

991 992 993 994
#if (HL_MAX_JOBS_PER_CS > HL_QUEUE_LENGTH)
#error "HL_QUEUE_LENGTH must be greater than HL_MAX_JOBS_PER_CS"
#endif

995
/* HL_CQ_LENGTH is in units of struct hl_cq_entry */
996 997 998
#define HL_CQ_LENGTH			HL_QUEUE_LENGTH
#define HL_CQ_SIZE_IN_BYTES		(HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)

999
/* Must be power of 2 */
1000 1001
#define HL_EQ_LENGTH			64
#define HL_EQ_SIZE_IN_BYTES		(HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
1002

1003
/* Host <-> CPU-CP shared memory size */
1004
#define HL_CPU_ACCESSIBLE_MEM_SIZE	SZ_2M
1005 1006

/**
1007 1008
 * struct hl_sync_stream_properties -
 *     describes a H/W queue sync stream properties
1009
 * @hw_sob: array of the used H/W SOBs by this H/W queue.
1010 1011 1012
 * @next_sob_val: the next value to use for the currently used SOB.
 * @base_sob_id: the base SOB id of the SOBs used by this queue.
 * @base_mon_id: the base MON id of the MONs used by this queue.
1013 1014 1015 1016 1017 1018
 * @collective_mstr_mon_id: the MON ids of the MONs used by this master queue
 *                          in order to sync with all slave queues.
 * @collective_slave_mon_id: the MON id used by this slave queue in order to
 *                           sync with its master queue.
 * @collective_sob_id: current SOB id used by this collective slave queue
 *                     to signal its collective master queue upon completion.
1019 1020 1021 1022
 * @curr_sob_offset: the id offset to the currently used SOB from the
 *                   HL_RSVD_SOBS that are being used by this queue.
 */
struct hl_sync_stream_properties {
1023 1024 1025 1026 1027 1028 1029 1030
	struct hl_hw_sob hw_sob[HL_RSVD_SOBS];
	u16		next_sob_val;
	u16		base_sob_id;
	u16		base_mon_id;
	u16		collective_mstr_mon_id[HL_COLLECTIVE_RSVD_MSTR_MONS];
	u16		collective_slave_mon_id;
	u16		collective_sob_id;
	u8		curr_sob_offset;
1031 1032
};

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
/**
 * struct hl_encaps_signals_mgr - describes sync stream encapsulated signals
 * handlers manager
 * @lock: protects handles.
 * @handles: an idr to hold all encapsulated signals handles.
 */
struct hl_encaps_signals_mgr {
	spinlock_t		lock;
	struct idr		handles;
};

1044 1045
/**
 * struct hl_hw_queue - describes a H/W transport queue.
1046
 * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
1047
 * @sync_stream_prop: sync stream queue properties
1048
 * @queue_type: type of queue.
1049
 * @collective_mode: collective mode of current queue
1050 1051 1052 1053 1054
 * @kernel_address: holds the queue's kernel virtual address.
 * @bus_address: holds the queue's DMA address.
 * @pi: holds the queue's pi value.
 * @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci).
 * @hw_queue_id: the id of the H/W queue.
1055 1056
 * @cq_id: the id for the corresponding CQ for this H/W queue.
 * @msi_vec: the IRQ number of the H/W queue.
1057 1058
 * @int_queue_len: length of internal queue (number of entries).
 * @valid: is the queue valid (we have array of 32 queues, not all of them
1059
 *         exist).
1060
 * @supports_sync_stream: True if queue supports sync stream
1061 1062
 */
struct hl_hw_queue {
1063 1064 1065
	struct hl_cs_job			**shadow_queue;
	struct hl_sync_stream_properties	sync_stream_prop;
	enum hl_queue_type			queue_type;
1066
	enum hl_collective_mode			collective_mode;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	void					*kernel_address;
	dma_addr_t				bus_address;
	u32					pi;
	atomic_t				ci;
	u32					hw_queue_id;
	u32					cq_id;
	u32					msi_vec;
	u16					int_queue_len;
	u8					valid;
	u8					supports_sync_stream;
1077 1078 1079 1080 1081 1082 1083
};

/**
 * struct hl_cq - describes a completion queue
 * @hdev: pointer to the device structure
 * @kernel_address: holds the queue's kernel virtual address
 * @bus_address: holds the queue's DMA address
1084
 * @cq_idx: completion queue index in array
1085 1086 1087 1088 1089 1090 1091
 * @hw_queue_id: the id of the matching H/W queue
 * @ci: ci inside the queue
 * @pi: pi inside the queue
 * @free_slots_cnt: counter of free slots in queue
 */
struct hl_cq {
	struct hl_device	*hdev;
1092
	void			*kernel_address;
1093
	dma_addr_t		bus_address;
1094
	u32			cq_idx;
1095 1096 1097 1098 1099
	u32			hw_queue_id;
	u32			ci;
	u32			pi;
	atomic_t		free_slots_cnt;
};
1100

1101 1102 1103
enum hl_user_interrupt_type {
	HL_USR_INTERRUPT_CQ = 0,
	HL_USR_INTERRUPT_DECODER,
1104
	HL_USR_INTERRUPT_TPC
1105 1106
};

1107 1108 1109
/**
 * struct hl_user_interrupt - holds user interrupt information
 * @hdev: pointer to the device structure
1110
 * @type: user interrupt type
1111 1112
 * @wait_list_head: head to the list of user threads pending on this interrupt
 * @wait_list_lock: protects wait_list_head
1113
 * @timestamp: last timestamp taken upon interrupt
1114 1115 1116
 * @interrupt_id: msix interrupt id
 */
struct hl_user_interrupt {
1117 1118 1119 1120
	struct hl_device		*hdev;
	enum hl_user_interrupt_type	type;
	struct list_head		wait_list_head;
	spinlock_t			wait_list_lock;
1121
	ktime_t				timestamp;
1122
	u32				interrupt_id;
1123 1124
};

1125 1126 1127 1128
/**
 * struct timestamp_reg_free_node - holds the timestamp registration free objects node
 * @free_objects_node: node in the list free_obj_jobs
 * @cq_cb: pointer to cq command buffer to be freed
1129
 * @buf: pointer to timestamp buffer to be freed
1130 1131 1132 1133
 */
struct timestamp_reg_free_node {
	struct list_head	free_objects_node;
	struct hl_cb		*cq_cb;
1134
	struct hl_mmap_mem_buf	*buf;
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
};

/* struct timestamp_reg_work_obj - holds the timestamp registration free objects job
 * the job will be to pass over the free_obj_jobs list and put refcount to objects
 * in each node of the list
 * @free_obj: workqueue object to free timestamp registration node objects
 * @hdev: pointer to the device structure
 * @free_obj_head: list of free jobs nodes (node type timestamp_reg_free_node)
 */
struct timestamp_reg_work_obj {
	struct work_struct	free_obj;
	struct hl_device	*hdev;
	struct list_head	*free_obj_head;
};

/* struct timestamp_reg_info - holds the timestamp registration related data.
1151 1152
 * @buf: pointer to the timestamp buffer which include both user/kernel buffers.
 *       relevant only when doing timestamps records registration.
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
 * @cq_cb: pointer to CQ counter CB.
 * @timestamp_kernel_addr: timestamp handle address, where to set timestamp
 *                         relevant only when doing timestamps records
 *                         registration.
 * @in_use: indicates if the node already in use. relevant only when doing
 *          timestamps records registration, since in this case the driver
 *          will have it's own buffer which serve as a records pool instead of
 *          allocating records dynamically.
 */
struct timestamp_reg_info {
1163
	struct hl_mmap_mem_buf	*buf;
1164 1165 1166 1167 1168
	struct hl_cb		*cq_cb;
	u64			*timestamp_kernel_addr;
	u8			in_use;
};

1169 1170 1171
/**
 * struct hl_user_pending_interrupt - holds a context to a user thread
 *                                    pending on an interrupt
1172
 * @ts_reg_info: holds the timestamps registration nodes info
1173 1174
 * @wait_list_node: node in the list of user threads pending on an interrupt
 * @fence: hl fence object for interrupt completion
1175 1176
 * @cq_target_value: CQ target value
 * @cq_kernel_addr: CQ kernel address, to be used in the cq interrupt
1177
 *                  handler for target value comparison
1178 1179
 */
struct hl_user_pending_interrupt {
1180 1181 1182 1183 1184
	struct timestamp_reg_info	ts_reg_info;
	struct list_head		wait_list_node;
	struct hl_fence			fence;
	u64				cq_target_value;
	u64				*cq_kernel_addr;
1185 1186
};

1187 1188 1189 1190 1191 1192
/**
 * struct hl_eq - describes the event queue (single one per device)
 * @hdev: pointer to the device structure
 * @kernel_address: holds the queue's kernel virtual address
 * @bus_address: holds the queue's DMA address
 * @ci: ci inside the queue
1193 1194 1195 1196 1197
 * @prev_eqe_index: the index of the previous event queue entry. The index of
 *                  the current entry's index must be +1 of the previous one.
 * @check_eqe_index: do we need to check the index of the current entry vs. the
 *                   previous one. This is for backward compatibility with older
 *                   firmwares
1198 1199 1200
 */
struct hl_eq {
	struct hl_device	*hdev;
1201
	void			*kernel_address;
1202 1203
	dma_addr_t		bus_address;
	u32			ci;
1204 1205
	u32			prev_eqe_index;
	bool			check_eqe_index;
1206 1207
};

1208 1209 1210 1211 1212 1213
/**
 * struct hl_dec - describes a decoder sw instance.
 * @hdev: pointer to the device structure.
 * @completion_abnrm_work: workqueue object to run when decoder generates an error interrupt
 * @core_id: ID of the decoder.
 * @base_addr: base address of the decoder.
1214
 */
1215 1216 1217 1218 1219 1220
struct hl_dec {
	struct hl_device		*hdev;
	struct work_struct		completion_abnrm_work;
	u32				core_id;
	u32				base_addr;
};
1221 1222 1223 1224

/**
 * enum hl_asic_type - supported ASIC types.
 * @ASIC_INVALID: Invalid ASIC type.
1225 1226
 * @ASIC_GOYA: Goya device (HL-1000).
 * @ASIC_GAUDI: Gaudi device (HL-2000).
1227
 * @ASIC_GAUDI_SEC: Gaudi secured device (HL-2000).
1228
 * @ASIC_GAUDI2: Gaudi2 device.
1229
 * @ASIC_GAUDI2B: Gaudi2B device.
1230 1231
 */
enum hl_asic_type {
1232
	ASIC_INVALID,
1233
	ASIC_GOYA,
1234
	ASIC_GAUDI,
1235 1236
	ASIC_GAUDI_SEC,
	ASIC_GAUDI2,
1237
	ASIC_GAUDI2B,
1238 1239
};

1240 1241
struct hl_cs_parser;

1242 1243
/**
 * enum hl_pm_mng_profile - power management profile.
1244
 * @PM_AUTO: internal clock is set by the Linux driver.
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
 * @PM_MANUAL: internal clock is set by the user.
 * @PM_LAST: last power management type.
 */
enum hl_pm_mng_profile {
	PM_AUTO = 1,
	PM_MANUAL,
	PM_LAST
};

/**
 * enum hl_pll_frequency - PLL frequency.
 * @PLL_HIGH: high frequency.
 * @PLL_LOW: low frequency.
 * @PLL_LAST: last frequency values that were configured by the user.
 */
enum hl_pll_frequency {
	PLL_HIGH = 1,
	PLL_LOW,
	PLL_LAST
};

1266 1267 1268 1269 1270 1271 1272 1273 1274
#define PLL_REF_CLK 50

enum div_select_defs {
	DIV_SEL_REF_CLK = 0,
	DIV_SEL_PLL_CLK = 1,
	DIV_SEL_DIVIDED_REF = 2,
	DIV_SEL_DIVIDED_PLL = 3,
};

1275
enum debugfs_access_type {
1276 1277
	DEBUGFS_READ8,
	DEBUGFS_WRITE8,
1278 1279 1280 1281 1282 1283
	DEBUGFS_READ32,
	DEBUGFS_WRITE32,
	DEBUGFS_READ64,
	DEBUGFS_WRITE64,
};

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
enum pci_region {
	PCI_REGION_CFG,
	PCI_REGION_SRAM,
	PCI_REGION_DRAM,
	PCI_REGION_SP_SRAM,
	PCI_REGION_NUMBER,
};

/**
 * struct pci_mem_region - describe memory region in a PCI bar
 * @region_base: region base address
 * @region_size: region size
1296
 * @bar_size: size of the BAR
1297 1298 1299 1300 1301 1302 1303
 * @offset_in_bar: region offset into the bar
 * @bar_id: bar ID of the region
 * @used: if used 1, otherwise 0
 */
struct pci_mem_region {
	u64 region_base;
	u64 region_size;
1304
	u64 bar_size;
1305
	u64 offset_in_bar;
1306 1307 1308 1309
	u8 bar_id;
	u8 used;
};

1310
/**
1311
 * struct static_fw_load_mgr - static FW load manager
1312 1313
 * @preboot_version_max_off: max offset to preboot version
 * @boot_fit_version_max_off: max offset to boot fit version
1314 1315
 * @kmd_msg_to_cpu_reg: register address for KDM->CPU messages
 * @cpu_cmd_status_to_host_reg: register address for CPU command status response
1316
 * @cpu_boot_status_reg: boot status register
1317 1318 1319 1320
 * @cpu_boot_dev_status0_reg: boot device status register 0
 * @cpu_boot_dev_status1_reg: boot device status register 1
 * @boot_err0_reg: boot error register 0
 * @boot_err1_reg: boot error register 1
1321 1322 1323
 * @preboot_version_offset_reg: SRAM offset to preboot version register
 * @boot_fit_version_offset_reg: SRAM offset to boot fit version register
 * @sram_offset_mask: mask for getting offset into the SRAM
1324
 * @cpu_reset_wait_msec: used when setting WFE via kmd_msg_to_cpu_reg
1325
 */
1326
struct static_fw_load_mgr {
1327 1328
	u64 preboot_version_max_off;
	u64 boot_fit_version_max_off;
1329 1330
	u32 kmd_msg_to_cpu_reg;
	u32 cpu_cmd_status_to_host_reg;
1331
	u32 cpu_boot_status_reg;
1332 1333
	u32 cpu_boot_dev_status0_reg;
	u32 cpu_boot_dev_status1_reg;
1334
	u32 boot_err0_reg;
1335
	u32 boot_err1_reg;
1336 1337 1338
	u32 preboot_version_offset_reg;
	u32 boot_fit_version_offset_reg;
	u32 sram_offset_mask;
1339
	u32 cpu_reset_wait_msec;
1340 1341
};

1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
/**
 * struct fw_response - FW response to LKD command
 * @ram_offset: descriptor offset into the RAM
 * @ram_type: RAM type containing the descriptor (SRAM/DRAM)
 * @status: command status
 */
struct fw_response {
	u32 ram_offset;
	u8 ram_type;
	u8 status;
};

1354 1355
/**
 * struct dynamic_fw_load_mgr - dynamic FW load manager
1356 1357 1358
 * @response: FW to LKD response
 * @comm_desc: the communication descriptor with FW
 * @image_region: region to copy the FW image to
1359 1360
 * @fw_image_size: size of FW image to load
 * @wait_for_bl_timeout: timeout for waiting for boot loader to respond
1361
 * @fw_desc_valid: true if FW descriptor has been validated and hence the data can be used
1362 1363
 */
struct dynamic_fw_load_mgr {
1364 1365 1366 1367
	struct fw_response response;
	struct lkd_fw_comms_desc comm_desc;
	struct pci_mem_region *image_region;
	size_t fw_image_size;
1368
	u32 wait_for_bl_timeout;
1369
	bool fw_desc_valid;
1370 1371
};

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
/**
 * struct pre_fw_load_props - needed properties for pre-FW load
 * @cpu_boot_status_reg: cpu_boot_status register address
 * @sts_boot_dev_sts0_reg: sts_boot_dev_sts0 register address
 * @sts_boot_dev_sts1_reg: sts_boot_dev_sts1 register address
 * @boot_err0_reg: boot_err0 register address
 * @boot_err1_reg: boot_err1 register address
 * @wait_for_preboot_timeout: timeout to poll for preboot ready
 */
struct pre_fw_load_props {
	u32 cpu_boot_status_reg;
	u32 sts_boot_dev_sts0_reg;
	u32 sts_boot_dev_sts1_reg;
	u32 boot_err0_reg;
	u32 boot_err1_reg;
	u32 wait_for_preboot_timeout;
};

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
/**
 * struct fw_image_props - properties of FW image
 * @image_name: name of the image
 * @src_off: offset in src FW to copy from
 * @copy_size: amount of bytes to copy (0 to copy the whole binary)
 */
struct fw_image_props {
	char *image_name;
	u32 src_off;
	u32 copy_size;
1400 1401 1402 1403
};

/**
 * struct fw_load_mgr - manager FW loading process
1404 1405
 * @dynamic_loader: specific structure for dynamic load
 * @static_loader: specific structure for static load
1406
 * @pre_fw_load_props: parameter for pre FW load
1407 1408
 * @boot_fit_img: boot fit image properties
 * @linux_img: linux image properties
1409 1410 1411 1412
 * @cpu_timeout: CPU response timeout in usec
 * @boot_fit_timeout: Boot fit load timeout in usec
 * @skip_bmc: should BMC be skipped
 * @sram_bar_id: SRAM bar ID
1413
 * @dram_bar_id: DRAM bar ID
1414 1415
 * @fw_comp_loaded: bitmask of loaded FW components. set bit meaning loaded
 *                  component. values are set according to enum hl_fw_types.
1416 1417
 */
struct fw_load_mgr {
1418 1419 1420 1421
	union {
		struct dynamic_fw_load_mgr dynamic_loader;
		struct static_fw_load_mgr static_loader;
	};
1422
	struct pre_fw_load_props pre_fw_load;
1423 1424
	struct fw_image_props boot_fit_img;
	struct fw_image_props linux_img;
1425 1426 1427
	u32 cpu_timeout;
	u32 boot_fit_timeout;
	u8 skip_bmc;
1428
	u8 sram_bar_id;
1429
	u8 dram_bar_id;
1430
	u8 fw_comp_loaded;
1431 1432
};

1433 1434
struct hl_cs;

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
/**
 * struct engines_data - asic engines data
 * @buf: buffer for engines data in ascii
 * @actual_size: actual size of data that was written by the driver to the allocated buffer
 * @allocated_buf_size: total size of allocated buffer
 */
struct engines_data {
	char *buf;
	int actual_size;
	u32 allocated_buf_size;
};

1447 1448 1449 1450 1451
/**
 * struct hl_asic_funcs - ASIC specific functions that are can be called from
 *                        common code.
 * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
 * @early_fini: tears down what was done in early_init.
1452 1453
 * @late_init: sets up late driver/hw state (post hw_init) - Optional.
 * @late_fini: tears down what was done in late_init (pre hw_fini) - Optional.
1454 1455
 * @sw_init: sets up driver state, does not configure H/W.
 * @sw_fini: tears down driver state, does not configure H/W.
1456 1457
 * @hw_init: sets up the H/W state.
 * @hw_fini: tears down the H/W state.
1458 1459 1460
 * @halt_engines: halt engines, needed for reset sequence. This also disables
 *                interrupts from the device. Should be called before
 *                hw_fini and before CS rollback.
1461 1462
 * @suspend: handles IP specific H/W or SW changes for suspend.
 * @resume: handles IP specific H/W or SW changes for resume.
1463
 * @mmap: maps a memory.
1464
 * @ring_doorbell: increment PI on a given QMAN.
1465 1466 1467 1468 1469
 * @pqe_write: Write the PQ entry to the PQ. This is ASIC-specific
 *             function because the PQs are located in different memory areas
 *             per ASIC (SRAM, DRAM, Host memory) and therefore, the method of
 *             writing the PQE must match the destination memory area
 *             properties.
1470 1471 1472 1473 1474 1475 1476 1477
 * @asic_dma_alloc_coherent: Allocate coherent DMA memory by calling
 *                           dma_alloc_coherent(). This is ASIC function because
 *                           its implementation is not trivial when the driver
 *                           is loaded in simulation mode (not upstreamed).
 * @asic_dma_free_coherent:  Free coherent DMA memory by calling
 *                           dma_free_coherent(). This is ASIC function because
 *                           its implementation is not trivial when the driver
 *                           is loaded in simulation mode (not upstreamed).
1478
 * @scrub_device_mem: Scrub the entire SRAM and DRAM.
1479
 * @scrub_device_dram: Scrub the dram memory of the device.
1480 1481
 * @get_int_queue_base: get the internal queue base address.
 * @test_queues: run simple test on all queues for sanity check.
1482 1483 1484
 * @asic_dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool.
 *                        size of allocation is HL_DMA_POOL_BLK_SIZE.
 * @asic_dma_pool_free: free small DMA allocation from pool.
1485 1486
 * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
 * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
1487 1488
 * @asic_dma_unmap_single: unmap a single DMA buffer
 * @asic_dma_map_single: map a single buffer to a DMA
1489
 * @hl_dma_unmap_sgtable: DMA unmap scatter-gather table.
1490
 * @cs_parser: parse Command Submission.
1491
 * @asic_dma_map_sgtable: DMA map scatter-gather table.
1492
 * @add_end_of_cb_packets: Add packets to the end of CB, if device requires it.
1493
 * @update_eq_ci: update event queue CI.
1494 1495
 * @context_switch: called upon ASID context switch.
 * @restore_phase_topology: clear all SOBs amd MONs.
1496 1497
 * @debugfs_read_dma: debug interface for reading up to 2MB from the device's
 *                    internal memory via DMA engine.
1498
 * @add_device_attr: add ASIC specific device attributes.
1499
 * @handle_eqe: handle event queue entry (IRQ) from CPU-CP.
1500
 * @get_events_stat: retrieve event queue entries histogram.
1501 1502
 * @read_pte: read MMU page table entry from DRAM.
 * @write_pte: write MMU page table entry to DRAM.
1503 1504
 * @mmu_invalidate_cache: flush MMU STLB host/DRAM cache, either with soft
 *                        (L1 only) or hard (L0 & L1) flush.
1505 1506
 * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with ASID-VA-size mask.
 * @mmu_prefetch_cache_range: pre-fetch specific MMU STLB cache lines with ASID-VA-size mask.
1507
 * @send_heartbeat: send is-alive packet to CPU-CP and verify response.
1508
 * @debug_coresight: perform certain actions on Coresight for debugging.
1509
 * @is_device_idle: return true if device is idle, false otherwise.
1510
 * @compute_reset_late_init: perform certain actions needed after a compute reset
1511 1512
 * @hw_queues_lock: acquire H/W queues lock.
 * @hw_queues_unlock: release H/W queues lock.
1513
 * @get_pci_id: retrieve PCI ID.
1514
 * @get_eeprom_data: retrieve EEPROM data from F/W.
1515
 * @get_monitor_dump: retrieve monitor registers dump from F/W.
1516 1517 1518 1519 1520
 * @send_cpu_message: send message to F/W. If the message is timedout, the
 *                    driver will eventually reset the device. The timeout can
 *                    be determined by the calling function or it can be 0 and
 *                    then the timeout is the default timeout for the specific
 *                    ASIC
1521
 * @get_hw_state: retrieve the H/W state
1522 1523
 * @pci_bars_map: Map PCI BARs.
 * @init_iatu: Initialize the iATU unit inside the PCI controller.
1524 1525
 * @rreg: Read a register. Needed for simulator support.
 * @wreg: Write a register. Needed for simulator support.
1526
 * @halt_coresight: stop the ETF and ETR traces.
1527
 * @ctx_init: context dependent initialization.
1528
 * @ctx_fini: context dependent cleanup.
1529
 * @pre_schedule_cs: Perform pre-CS-scheduling operations.
1530
 * @get_queue_id_for_cq: Get the H/W queue id related to the given CQ index.
1531
 * @load_firmware_to_device: load the firmware to the device's memory
1532
 * @load_boot_fit_to_device: load boot fit to device's memory
1533 1534 1535 1536 1537
 * @get_signal_cb_size: Get signal CB size.
 * @get_wait_cb_size: Get wait CB size.
 * @gen_signal_cb: Generate a signal CB.
 * @gen_wait_cb: Generate a wait CB.
 * @reset_sob: Reset a SOB.
1538
 * @reset_sob_group: Reset SOB group
1539
 * @get_device_time: Get the device time.
1540
 * @pb_print_security_errors: print security errors according block and cause
1541 1542 1543
 * @collective_wait_init_cs: Generate collective master/slave packets
 *                           and place them in the relevant cs jobs
 * @collective_wait_create_jobs: allocate collective wait cs jobs
1544
 * @get_dec_base_addr: get the base address of a given decoder.
1545
 * @scramble_addr: Routine to scramble the address prior of mapping it
1546
 *                 in the MMU.
1547
 * @descramble_addr: Routine to de-scramble the address prior of
1548
 *                   showing it to users.
1549
 * @ack_protection_bits_errors: ack and dump all security violations
1550
 * @get_hw_block_id: retrieve a HW block id to be used by the user to mmap it.
1551 1552
 *                   also returns the size of the block if caller supplies
 *                   a valid pointer for it
1553
 * @hw_block_mmap: mmap a HW block with a given id.
1554 1555 1556 1557
 * @enable_events_from_fw: send interrupt to firmware to notify them the
 *                         driver is ready to receive asynchronous events. This
 *                         function should be called during the first init and
 *                         after every hard-reset of the device
1558
 * @ack_mmu_errors: check and ack mmu errors, page fault, access violation.
1559 1560 1561
 * @get_msi_info: Retrieve asic-specific MSI ID of the f/w async event
 * @map_pll_idx_to_fw_idx: convert driver specific per asic PLL index to
 *                         generic f/w compatible PLL Indexes
1562
 * @init_firmware_preload_params: initialize pre FW-load parameters.
1563 1564
 * @init_firmware_loader: initialize data for FW loader.
 * @init_cpu_scrambler_dram: Enable CPU specific DRAM scrambling
1565
 * @state_dump_init: initialize constants required for state dump
1566
 * @get_sob_addr: get SOB base address offset.
1567
 * @set_pci_memory_regions: setting properties of PCI memory regions
1568
 * @get_stream_master_qid_arr: get pointer to stream masters QID array
1569
 * @check_if_razwi_happened: check if there was a razwi due to RR violation.
1570 1571
 * @access_dev_mem: access device memory
 * @set_dram_bar_base: set the base of the DRAM BAR
1572
 * @set_engine_cores: set a config command to engine cores
1573
 * @set_engines: set a config command to user engines
1574
 * @send_device_activity: indication to FW about device availability
1575
 * @set_dram_properties: set DRAM related properties.
1576
 * @set_binning_masks: set binning/enable masks for all relevant components.
1577 1578 1579 1580
 */
struct hl_asic_funcs {
	int (*early_init)(struct hl_device *hdev);
	int (*early_fini)(struct hl_device *hdev);
1581 1582
	int (*late_init)(struct hl_device *hdev);
	void (*late_fini)(struct hl_device *hdev);
1583 1584
	int (*sw_init)(struct hl_device *hdev);
	int (*sw_fini)(struct hl_device *hdev);
1585
	int (*hw_init)(struct hl_device *hdev);
1586
	int (*hw_fini)(struct hl_device *hdev, bool hard_reset, bool fw_reset);
1587
	void (*halt_engines)(struct hl_device *hdev, bool hard_reset, bool fw_reset);
1588 1589
	int (*suspend)(struct hl_device *hdev);
	int (*resume)(struct hl_device *hdev);
1590
	int (*mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
1591
			void *cpu_addr, dma_addr_t dma_addr, size_t size);
1592
	void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
1593 1594
	void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
			struct hl_bd *bd);
1595
	void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
1596
					dma_addr_t *dma_handle, gfp_t flag);
1597
	void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
1598
					void *cpu_addr, dma_addr_t dma_handle);
1599
	int (*scrub_device_mem)(struct hl_device *hdev);
1600
	int (*scrub_device_dram)(struct hl_device *hdev, u64 val);
1601 1602 1603
	void* (*get_int_queue_base)(struct hl_device *hdev, u32 queue_id,
				dma_addr_t *dma_handle, u16 *queue_len);
	int (*test_queues)(struct hl_device *hdev);
1604
	void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
1605
				gfp_t mem_flags, dma_addr_t *dma_handle);
1606
	void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
1607 1608 1609 1610 1611
				dma_addr_t dma_addr);
	void* (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev,
				size_t size, dma_addr_t *dma_handle);
	void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,
				size_t size, void *vaddr);
1612 1613 1614 1615 1616 1617
	void (*asic_dma_unmap_single)(struct hl_device *hdev,
				dma_addr_t dma_addr, int len,
				enum dma_data_direction dir);
	dma_addr_t (*asic_dma_map_single)(struct hl_device *hdev,
				void *addr, int len,
				enum dma_data_direction dir);
1618 1619
	void (*hl_dma_unmap_sgtable)(struct hl_device *hdev,
				struct sg_table *sgt,
1620 1621
				enum dma_data_direction dir);
	int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
1622
	int (*asic_dma_map_sgtable)(struct hl_device *hdev, struct sg_table *sgt,
1623
				enum dma_data_direction dir);
1624
	void (*add_end_of_cb_packets)(struct hl_device *hdev,
1625
					void *kernel_address, u32 len,
1626
					u32 original_len,
1627 1628
					u64 cq_addr, u32 cq_val, u32 msix_num,
					bool eb);
1629
	void (*update_eq_ci)(struct hl_device *hdev, u32 val);
1630 1631
	int (*context_switch)(struct hl_device *hdev, u32 asid);
	void (*restore_phase_topology)(struct hl_device *hdev);
1632 1633
	int (*debugfs_read_dma)(struct hl_device *hdev, u64 addr, u32 size,
				void *blob_addr);
1634 1635
	void (*add_device_attr)(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp,
				struct attribute_group *dev_vrm_attr_grp);
1636 1637
	void (*handle_eqe)(struct hl_device *hdev,
				struct hl_eq_entry *eq_entry);
1638 1639
	void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
				u32 *size);
1640 1641
	u64 (*read_pte)(struct hl_device *hdev, u64 addr);
	void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
1642
	int (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard,
1643
					u32 flags);
1644
	int (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
1645
				u32 flags, u32 asid, u64 va, u64 size);
1646
	int (*mmu_prefetch_cache_range)(struct hl_ctx *ctx, u32 flags, u32 asid, u64 va, u64 size);
1647
	int (*send_heartbeat)(struct hl_device *hdev);
1648
	int (*debug_coresight)(struct hl_device *hdev, struct hl_ctx *ctx, void *data);
1649 1650
	bool (*is_device_idle)(struct hl_device *hdev, u64 *mask_arr, u8 mask_len,
				struct engines_data *e);
1651
	int (*compute_reset_late_init)(struct hl_device *hdev);
1652 1653
	void (*hw_queues_lock)(struct hl_device *hdev);
	void (*hw_queues_unlock)(struct hl_device *hdev);
1654
	u32 (*get_pci_id)(struct hl_device *hdev);
1655 1656
	int (*get_eeprom_data)(struct hl_device *hdev, void *data, size_t max_size);
	int (*get_monitor_dump)(struct hl_device *hdev, void *data);
1657
	int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
1658
				u16 len, u32 timeout, u64 *result);
1659 1660
	int (*pci_bars_map)(struct hl_device *hdev);
	int (*init_iatu)(struct hl_device *hdev);
1661 1662
	u32 (*rreg)(struct hl_device *hdev, u32 reg);
	void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
1663
	void (*halt_coresight)(struct hl_device *hdev, struct hl_ctx *ctx);
1664
	int (*ctx_init)(struct hl_ctx *ctx);
1665
	void (*ctx_fini)(struct hl_ctx *ctx);
1666
	int (*pre_schedule_cs)(struct hl_cs *cs);
1667
	u32 (*get_queue_id_for_cq)(struct hl_device *hdev, u32 cq_idx);
1668
	int (*load_firmware_to_device)(struct hl_device *hdev);
1669
	int (*load_boot_fit_to_device)(struct hl_device *hdev);
1670 1671
	u32 (*get_signal_cb_size)(struct hl_device *hdev);
	u32 (*get_wait_cb_size)(struct hl_device *hdev);
1672
	u32 (*gen_signal_cb)(struct hl_device *hdev, void *data, u16 sob_id,
1673
			u32 size, bool eb);
1674
	u32 (*gen_wait_cb)(struct hl_device *hdev,
1675
			struct hl_gen_wait_properties *prop);
1676
	void (*reset_sob)(struct hl_device *hdev, void *data);
1677
	void (*reset_sob_group)(struct hl_device *hdev, u16 sob_group);
1678
	u64 (*get_device_time)(struct hl_device *hdev);
1679 1680
	void (*pb_print_security_errors)(struct hl_device *hdev,
			u32 block_addr, u32 cause, u32 offended_addr);
1681
	int (*collective_wait_init_cs)(struct hl_cs *cs);
1682
	int (*collective_wait_create_jobs)(struct hl_device *hdev,
1683 1684 1685
			struct hl_ctx *ctx, struct hl_cs *cs,
			u32 wait_queue_id, u32 collective_engine_id,
			u32 encaps_signal_offset);
1686
	u32 (*get_dec_base_addr)(struct hl_device *hdev, u32 core_id);
1687 1688
	u64 (*scramble_addr)(struct hl_device *hdev, u64 addr);
	u64 (*descramble_addr)(struct hl_device *hdev, u64 addr);
1689
	void (*ack_protection_bits_errors)(struct hl_device *hdev);
1690
	int (*get_hw_block_id)(struct hl_device *hdev, u64 block_addr,
1691
				u32 *block_size, u32 *block_id);
1692 1693
	int (*hw_block_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
			u32 block_id, u32 block_size);
1694
	void (*enable_events_from_fw)(struct hl_device *hdev);
1695
	int (*ack_mmu_errors)(struct hl_device *hdev, u64 mmu_cap_mask);
1696
	void (*get_msi_info)(__le32 *table);
1697
	int (*map_pll_idx_to_fw_idx)(u32 pll_idx);
1698
	void (*init_firmware_preload_params)(struct hl_device *hdev);
1699
	void (*init_firmware_loader)(struct hl_device *hdev);
1700
	void (*init_cpu_scrambler_dram)(struct hl_device *hdev);
1701
	void (*state_dump_init)(struct hl_device *hdev);
1702
	u32 (*get_sob_addr)(struct hl_device *hdev, u32 sob_id);
1703
	void (*set_pci_memory_regions)(struct hl_device *hdev);
1704
	u32* (*get_stream_master_qid_arr)(void);
1705
	void (*check_if_razwi_happened)(struct hl_device *hdev);
1706 1707
	int (*mmu_get_real_page_size)(struct hl_device *hdev, struct hl_mmu_properties *mmu_prop,
					u32 page_size, u32 *real_page_size, bool is_dram_addr);
1708 1709
	int (*access_dev_mem)(struct hl_device *hdev, enum pci_region region_type,
				u64 addr, u64 *val, enum debugfs_access_type acc_type);
1710
	u64 (*set_dram_bar_base)(struct hl_device *hdev, u64 addr);
1711 1712
	int (*set_engine_cores)(struct hl_device *hdev, u32 *core_ids,
					u32 num_cores, u32 core_command);
1713 1714
	int (*set_engines)(struct hl_device *hdev, u32 *engine_ids,
					u32 num_engines, u32 engine_command);
1715
	int (*send_device_activity)(struct hl_device *hdev, bool open);
1716
	int (*set_dram_properties)(struct hl_device *hdev);
1717
	int (*set_binning_masks)(struct hl_device *hdev);
1718
};
1719

1720 1721 1722 1723 1724 1725 1726

/*
 * CONTEXTS
 */

#define HL_KERNEL_ASID_ID	0

1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
/**
 * enum hl_va_range_type - virtual address range type.
 * @HL_VA_RANGE_TYPE_HOST: range type of host pages
 * @HL_VA_RANGE_TYPE_HOST_HUGE: range type of host huge pages
 * @HL_VA_RANGE_TYPE_DRAM: range type of dram pages
 */
enum hl_va_range_type {
	HL_VA_RANGE_TYPE_HOST,
	HL_VA_RANGE_TYPE_HOST_HUGE,
	HL_VA_RANGE_TYPE_DRAM,
	HL_VA_RANGE_TYPE_MAX
};

1740 1741 1742 1743 1744 1745
/**
 * struct hl_va_range - virtual addresses range.
 * @lock: protects the virtual addresses list.
 * @list: list of virtual addresses blocks available for mappings.
 * @start_addr: range start address.
 * @end_addr: range end address.
1746
 * @page_size: page size of this va range.
1747 1748 1749 1750 1751 1752
 */
struct hl_va_range {
	struct mutex		lock;
	struct list_head	list;
	u64			start_addr;
	u64			end_addr;
1753
	u32			page_size;
1754 1755
};

1756 1757 1758 1759 1760 1761 1762
/**
 * struct hl_cs_counters_atomic - command submission counters
 * @out_of_mem_drop_cnt: dropped due to memory allocation issue
 * @parsing_drop_cnt: dropped due to error in packet parsing
 * @queue_full_drop_cnt: dropped due to queue full
 * @device_in_reset_drop_cnt: dropped due to device in reset
 * @max_cs_in_flight_drop_cnt: dropped due to maximum CS in-flight
1763
 * @validation_drop_cnt: dropped due to error in validation
1764 1765 1766 1767 1768 1769 1770
 */
struct hl_cs_counters_atomic {
	atomic64_t out_of_mem_drop_cnt;
	atomic64_t parsing_drop_cnt;
	atomic64_t queue_full_drop_cnt;
	atomic64_t device_in_reset_drop_cnt;
	atomic64_t max_cs_in_flight_drop_cnt;
1771
	atomic64_t validation_drop_cnt;
1772 1773
};

1774 1775 1776 1777
/**
 * struct hl_dmabuf_priv - a dma-buf private object.
 * @dmabuf: pointer to dma-buf object.
 * @ctx: pointer to the dma-buf owner's context.
1778 1779 1780
 * @phys_pg_pack: pointer to physical page pack if the dma-buf was exported
 *                where virtual memory is supported.
 * @memhash_hnode: pointer to the memhash node. this object holds the export count.
1781 1782 1783 1784 1785 1786 1787
 * @device_address: physical address of the device's memory. Relevant only
 *                  if phys_pg_pack is NULL (dma-buf was exported from address).
 *                  The total size can be taken from the dmabuf object.
 */
struct hl_dmabuf_priv {
	struct dma_buf			*dmabuf;
	struct hl_ctx			*ctx;
1788 1789
	struct hl_vm_phys_pg_pack	*phys_pg_pack;
	struct hl_vm_hash_node		*memhash_hnode;
1790 1791 1792
	uint64_t			device_address;
};

1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
#define HL_CS_OUTCOME_HISTORY_LEN 256

/**
 * struct hl_cs_outcome - represents a single completed CS outcome
 * @list_link: link to either container's used list or free list
 * @map_link: list to the container hash map
 * @ts: completion ts
 * @seq: the original cs sequence
 * @error: error code cs completed with, if any
 */
struct hl_cs_outcome {
	struct list_head list_link;
	struct hlist_node map_link;
	ktime_t ts;
	u64 seq;
	int error;
};

/**
 * struct hl_cs_outcome_store - represents a limited store of completed CS outcomes
1813
 * @outcome_map: index of completed CS searchable by sequence number
1814 1815
 * @used_list: list of outcome objects currently in use
 * @free_list: list of outcome objects currently not in use
1816
 * @nodes_pool: a static pool of pre-allocated outcome objects
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
 * @db_lock: any operation on the store must take this lock
 */
struct hl_cs_outcome_store {
	DECLARE_HASHTABLE(outcome_map, 8);
	struct list_head used_list;
	struct list_head free_list;
	struct hl_cs_outcome nodes_pool[HL_CS_OUTCOME_HISTORY_LEN];
	spinlock_t db_lock;
};

1827 1828
/**
 * struct hl_ctx - user/kernel context.
1829 1830
 * @mem_hash: holds mapping from virtual address to virtual memory area
 *		descriptor (hl_vm_phys_pg_list or hl_userptr).
1831
 * @mmu_shadow_hash: holds a mapping from shadow address to pgt_info structure.
1832 1833 1834
 * @hr_mmu_phys_hash: if host-resident MMU is used, holds a mapping from
 *                    MMU-hop-page physical address to its host-resident
 *                    pgt_info structure.
1835
 * @hpriv: pointer to the private (Kernel Driver) data of the process (fd).
1836 1837
 * @hdev: pointer to the device structure.
 * @refcount: reference counter for the context. Context is released only when
1838
 *		this hits 0. It is incremented on CS and CS_WAIT.
1839
 * @cs_pending: array of hl fence objects representing pending CS.
1840
 * @outcome_store: storage data structure used to remember outcomes of completed
1841
 *                 command submissions for a long time after CS id wraparound.
1842
 * @va_range: holds available virtual addresses for host and dram mappings.
1843
 * @mem_hash_lock: protects the mem_hash.
1844
 * @hw_block_list_lock: protects the HW block memory list.
1845
 * @debugfs_list: node in debugfs list of contexts.
1846
 * @hw_block_mem_list: list of HW block virtual mapped addresses.
1847
 * @cs_counters: context command submission counters.
1848 1849
 * @cb_va_pool: device VA pool for command buffers which are mapped to the
 *              device's MMU.
1850
 * @sig_mgr: encaps signals handle manager.
1851
 * @cb_va_pool_base: the base address for the device VA pool
1852 1853 1854
 * @cs_sequence: sequence number for CS. Value is assigned to a CS and passed
 *			to user so user could inquire about CS. It is used as
 *			index to cs_pending array.
1855 1856
 * @dram_default_hops: array that holds all hops addresses needed for default
 *                     DRAM mapping.
1857
 * @cs_lock: spinlock to protect cs_sequence.
1858
 * @dram_phys_mem: amount of used physical DRAM memory by this context.
1859 1860 1861 1862 1863 1864 1865
 * @thread_ctx_switch_token: token to prevent multiple threads of the same
 *				context	from running the context switch phase.
 *				Only a single thread should run it.
 * @thread_ctx_switch_wait_token: token to prevent the threads that didn't run
 *				the context switch phase from moving to their
 *				execution phase before the context switch phase
 *				has finished.
1866
 * @asid: context's unique address space ID in the device's MMU.
1867
 * @handle: context's opaque handle for user
1868 1869
 */
struct hl_ctx {
1870
	DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
1871
	DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
1872
	DECLARE_HASHTABLE(hr_mmu_phys_hash, MMU_HASH_TABLE_BITS);
1873 1874 1875 1876
	struct hl_fpriv			*hpriv;
	struct hl_device		*hdev;
	struct kref			refcount;
	struct hl_fence			**cs_pending;
1877
	struct hl_cs_outcome_store	outcome_store;
1878
	struct hl_va_range		*va_range[HL_VA_RANGE_TYPE_MAX];
1879
	struct mutex			mem_hash_lock;
1880
	struct mutex			hw_block_list_lock;
1881
	struct list_head		debugfs_list;
1882
	struct list_head		hw_block_mem_list;
1883 1884
	struct hl_cs_counters_atomic	cs_counters;
	struct gen_pool			*cb_va_pool;
1885
	struct hl_encaps_signals_mgr	sig_mgr;
1886
	u64				cb_va_pool_base;
1887 1888 1889 1890 1891 1892 1893 1894
	u64				cs_sequence;
	u64				*dram_default_hops;
	spinlock_t			cs_lock;
	atomic64_t			dram_phys_mem;
	atomic_t			thread_ctx_switch_token;
	u32				thread_ctx_switch_wait_token;
	u32				asid;
	u32				handle;
1895 1896 1897 1898
};

/**
 * struct hl_ctx_mgr - for handling multiple contexts.
1899 1900
 * @lock: protects ctx_handles.
 * @handles: idr to hold all ctx handles.
1901 1902
 */
struct hl_ctx_mgr {
1903 1904
	struct mutex	lock;
	struct idr	handles;
1905 1906 1907
};


1908 1909 1910 1911 1912 1913 1914 1915
/*
 * COMMAND SUBMISSIONS
 */

/**
 * struct hl_userptr - memory mapping chunk information
 * @vm_type: type of the VM.
 * @job_node: linked-list node for hanging the object on the Job's list.
1916 1917
 * @pages: pointer to struct page array
 * @npages: size of @pages array
1918 1919 1920
 * @sgt: pointer to the scatter-gather table that holds the pages.
 * @dir: for DMA unmapping, the direction must be supplied, so save it.
 * @debugfs_list: node in debugfs list of command submissions.
1921
 * @pid: the pid of the user process owning the memory
1922
 * @addr: user-space virtual address of the start of the memory area.
1923 1924 1925 1926
 * @size: size of the memory area to pin & map.
 * @dma_mapped: true if the SG was mapped to DMA addresses, false otherwise.
 */
struct hl_userptr {
1927
	enum vm_type		vm_type; /* must be first */
1928
	struct list_head	job_node;
1929 1930
	struct page		**pages;
	unsigned int		npages;
1931 1932 1933
	struct sg_table		*sgt;
	enum dma_data_direction dir;
	struct list_head	debugfs_list;
1934
	pid_t			pid;
1935
	u64			addr;
1936
	u64			size;
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
	u8			dma_mapped;
};

/**
 * struct hl_cs - command submission.
 * @jobs_in_queue_cnt: per each queue, maintain counter of submitted jobs.
 * @ctx: the context this CS belongs to.
 * @job_list: list of the CS's jobs in the various queues.
 * @job_lock: spinlock for the CS's jobs list. Needed for free_job.
 * @refcount: reference counter for usage of the CS.
 * @fence: pointer to the fence object of this CS.
1948 1949
 * @signal_fence: pointer to the fence object of the signal CS (used by wait
 *                CS only).
1950
 * @finish_work: workqueue object to run when CS is completed by H/W.
1951 1952
 * @work_tdr: delayed work node for TDR.
 * @mirror_node : node in device mirror list of command submissions.
1953
 * @staged_cs_node: node in the staged cs list.
1954
 * @debugfs_list: node in debugfs list of command submissions.
1955
 * @encaps_sig_hdl: holds the encaps signals handle.
1956
 * @sequence: the sequence number of this CS.
1957 1958
 * @staged_sequence: the sequence of the staged submission this CS is part of,
 *                   relevant only if staged_cs is set.
1959
 * @timeout_jiffies: cs timeout in jiffies.
1960
 * @submission_time_jiffies: submission time of the cs
1961
 * @type: CS_TYPE_*.
1962
 * @jobs_cnt: counter of submitted jobs on all queues.
1963
 * @encaps_sig_hdl_id: encaps signals handle id, set for the first staged cs.
1964
 * @completion_timestamp: timestamp of the last completed cs job.
1965 1966 1967
 * @sob_addr_offset: sob offset from the configuration base address.
 * @initial_sob_count: count of completed signals in SOB before current submission of signal or
 *                     cs with encaps signals.
1968 1969 1970 1971 1972 1973
 * @submitted: true if CS was submitted to H/W.
 * @completed: true if CS was completed by device.
 * @timedout : true if CS was timedout.
 * @tdr_active: true if TDR was activated for this CS (to prevent
 *		double TDR activation).
 * @aborted: true if CS was aborted due to some device error.
1974
 * @timestamp: true if a timestamp must be captured upon completion.
1975 1976 1977 1978
 * @staged_last: true if this is the last staged CS and needs completion.
 * @staged_first: true if this is the first staged CS and we need to receive
 *                timeout for this CS.
 * @staged_cs: true if this CS is part of a staged submission.
1979 1980
 * @skip_reset_on_timeout: true if we shall not reset the device in case
 *                         timeout occurs (debug scenario).
1981
 * @encaps_signals: true if this CS has encaps reserved signals.
1982 1983
 */
struct hl_cs {
1984
	u16			*jobs_in_queue_cnt;
1985 1986 1987 1988
	struct hl_ctx		*ctx;
	struct list_head	job_list;
	spinlock_t		job_lock;
	struct kref		refcount;
1989 1990
	struct hl_fence		*fence;
	struct hl_fence		*signal_fence;
1991
	struct work_struct	finish_work;
1992 1993
	struct delayed_work	work_tdr;
	struct list_head	mirror_node;
1994
	struct list_head	staged_cs_node;
1995
	struct list_head	debugfs_list;
1996
	struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
1997
	ktime_t			completion_timestamp;
1998
	u64			sequence;
1999
	u64			staged_sequence;
2000
	u64			timeout_jiffies;
2001
	u64			submission_time_jiffies;
2002
	enum hl_cs_type		type;
2003
	u32			jobs_cnt;
2004
	u32			encaps_sig_hdl_id;
2005 2006
	u32			sob_addr_offset;
	u16			initial_sob_count;
2007 2008 2009 2010 2011
	u8			submitted;
	u8			completed;
	u8			timedout;
	u8			tdr_active;
	u8			aborted;
2012
	u8			timestamp;
2013 2014 2015
	u8			staged_last;
	u8			staged_first;
	u8			staged_cs;
2016
	u8			skip_reset_on_timeout;
2017
	u8			encaps_signals;
2018 2019
};

2020 2021
/**
 * struct hl_cs_job - command submission job.
2022 2023 2024 2025 2026
 * @cs_node: the node to hang on the CS jobs list.
 * @cs: the CS this job belongs to.
 * @user_cb: the CB we got from the user.
 * @patched_cb: in case of patching, this is internal CB which is submitted on
 *		the queue instead of the CB we got from the IOCTL.
2027
 * @finish_work: workqueue object to run when job is completed.
2028 2029
 * @userptr_list: linked-list of userptr mappings that belong to this job and
 *			wait for completion.
2030
 * @debugfs_list: node in debugfs list of command submission jobs.
2031
 * @refcount: reference counter for usage of the CS job.
2032
 * @queue_type: the type of the H/W queue this job is submitted to.
2033
 * @timestamp: timestamp upon job completion
2034
 * @id: the id of this job inside a CS.
2035 2036 2037
 * @hw_queue_id: the id of the H/W queue this job is submitted to.
 * @user_cb_size: the actual size of the CB we got from the user.
 * @job_cb_size: the actual size of the CB that we put on the queue.
2038 2039
 * @encaps_sig_wait_offset: encapsulated signals offset, which allow user
 *                          to wait on part of the reserved signals.
2040 2041 2042
 * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
 *                          handle to a kernel-allocated CB object, false
 *                          otherwise (SRAM/DRAM/host address).
2043 2044 2045 2046 2047 2048
 * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
 *                    info is needed later, when adding the 2xMSG_PROT at the
 *                    end of the JOB, to know which barriers to put in the
 *                    MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
 *                    have streams so the engine can't be busy by another
 *                    stream.
2049 2050
 */
struct hl_cs_job {
2051 2052 2053 2054
	struct list_head	cs_node;
	struct hl_cs		*cs;
	struct hl_cb		*user_cb;
	struct hl_cb		*patched_cb;
2055
	struct work_struct	finish_work;
2056
	struct list_head	userptr_list;
2057
	struct list_head	debugfs_list;
2058
	struct kref		refcount;
2059
	enum hl_queue_type	queue_type;
2060
	ktime_t			timestamp;
2061
	u32			id;
2062 2063 2064
	u32			hw_queue_id;
	u32			user_cb_size;
	u32			job_cb_size;
2065
	u32			encaps_sig_wait_offset;
2066
	u8			is_kernel_allocated_cb;
2067
	u8			contains_dma_pkt;
2068 2069 2070
};

/**
Tomer Tayar's avatar
Tomer Tayar committed
2071
 * struct hl_cs_parser - command submission parser properties.
2072 2073 2074 2075 2076 2077
 * @user_cb: the CB we got from the user.
 * @patched_cb: in case of patching, this is internal CB which is submitted on
 *		the queue instead of the CB we got from the IOCTL.
 * @job_userptr_list: linked-list of userptr mappings that belong to the related
 *			job and wait for completion.
 * @cs_sequence: the sequence number of the related CS.
2078
 * @queue_type: the type of the H/W queue this job is submitted to.
2079 2080 2081 2082 2083
 * @ctx_id: the ID of the context the related CS belongs to.
 * @hw_queue_id: the id of the H/W queue this job is submitted to.
 * @user_cb_size: the actual size of the CB we got from the user.
 * @patched_cb_size: the size of the CB after parsing.
 * @job_id: the id of the related job inside the related CS.
2084 2085 2086
 * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
 *                          handle to a kernel-allocated CB object, false
 *                          otherwise (SRAM/DRAM/host address).
2087 2088 2089 2090 2091 2092
 * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
 *                    info is needed later, when adding the 2xMSG_PROT at the
 *                    end of the JOB, to know which barriers to put in the
 *                    MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
 *                    have streams so the engine can't be busy by another
 *                    stream.
2093
 * @completion: true if we need completion for this CS.
2094 2095 2096 2097 2098 2099
 */
struct hl_cs_parser {
	struct hl_cb		*user_cb;
	struct hl_cb		*patched_cb;
	struct list_head	*job_userptr_list;
	u64			cs_sequence;
2100
	enum hl_queue_type	queue_type;
2101 2102 2103 2104 2105
	u32			ctx_id;
	u32			hw_queue_id;
	u32			user_cb_size;
	u32			patched_cb_size;
	u8			job_id;
2106
	u8			is_kernel_allocated_cb;
2107
	u8			contains_dma_pkt;
2108
	u8			completion;
2109
};
2110

2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
/*
 * MEMORY STRUCTURE
 */

/**
 * struct hl_vm_hash_node - hash element from virtual address to virtual
 *				memory area descriptor (hl_vm_phys_pg_list or
 *				hl_userptr).
 * @node: node to hang on the hash table in context object.
 * @vaddr: key virtual address.
2121
 * @handle: memory handle for device memory allocation.
2122
 * @ptr: value pointer (hl_vm_phys_pg_list or hl_userptr).
2123
 * @export_cnt: number of exports from within the VA block.
2124 2125 2126 2127
 */
struct hl_vm_hash_node {
	struct hlist_node	node;
	u64			vaddr;
2128
	u64			handle;
2129
	void			*ptr;
2130
	int			export_cnt;
2131 2132
};

2133 2134 2135 2136 2137 2138
/**
 * struct hl_vm_hw_block_list_node - list element from user virtual address to
 *				HW block id.
 * @node: node to hang on the list in context object.
 * @ctx: the context this node belongs to.
 * @vaddr: virtual address of the HW block.
2139 2140
 * @block_size: size of the block.
 * @mapped_size: size of the block which is mapped. May change if partial un-mappings are done.
2141 2142 2143 2144 2145 2146
 * @id: HW block id (handle).
 */
struct hl_vm_hw_block_list_node {
	struct list_head	node;
	struct hl_ctx		*ctx;
	unsigned long		vaddr;
2147 2148
	u32			block_size;
	u32			mapped_size;
2149 2150 2151
	u32			id;
};

2152 2153 2154 2155
/**
 * struct hl_vm_phys_pg_pack - physical page pack.
 * @vm_type: describes the type of the virtual area descriptor.
 * @pages: the physical page array.
2156 2157
 * @npages: num physical pages in the pack.
 * @total_size: total size of all the pages in this list.
2158
 * @exported_size: buffer exported size.
2159 2160
 * @node: used to attach to deletion list that is used when all the allocations are cleared
 *        at the teardown of the context.
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
 * @mapping_cnt: number of shared mappings.
 * @asid: the context related to this list.
 * @page_size: size of each page in the pack.
 * @flags: HL_MEM_* flags related to this list.
 * @handle: the provided handle related to this list.
 * @offset: offset from the first page.
 * @contiguous: is contiguous physical memory.
 * @created_from_userptr: is product of host virtual address.
 */
struct hl_vm_phys_pg_pack {
2171
	enum vm_type		vm_type; /* must be first */
2172
	u64			*pages;
2173 2174
	u64			npages;
	u64			total_size;
2175
	u64			exported_size;
2176
	struct list_head	node;
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
	atomic_t		mapping_cnt;
	u32			asid;
	u32			page_size;
	u32			flags;
	u32			handle;
	u32			offset;
	u8			contiguous;
	u8			created_from_userptr;
};

/**
 * struct hl_vm_va_block - virtual range block information.
 * @node: node to hang on the virtual range list in context object.
 * @start: virtual range start address.
 * @end: virtual range end address.
 * @size: virtual range size.
 */
struct hl_vm_va_block {
	struct list_head	node;
	u64			start;
	u64			end;
	u64			size;
};

/**
 * struct hl_vm - virtual memory manager for MMU.
 * @dram_pg_pool: pool for DRAM physical pages of 2MB.
 * @dram_pg_pool_refcount: reference counter for the pool usage.
 * @idr_lock: protects the phys_pg_list_handles.
 * @phys_pg_pack_handles: idr to hold all device allocations handles.
 * @init_done: whether initialization was done. We need this because VM
 *		initialization might be skipped during device initialization.
 */
struct hl_vm {
	struct gen_pool		*dram_pg_pool;
	struct kref		dram_pg_pool_refcount;
	spinlock_t		idr_lock;
	struct idr		phys_pg_pack_handles;
	u8			init_done;
};

2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240

/*
 * DEBUG, PROFILING STRUCTURE
 */

/**
 * struct hl_debug_params - Coresight debug parameters.
 * @input: pointer to component specific input parameters.
 * @output: pointer to component specific output parameters.
 * @output_size: size of output buffer.
 * @reg_idx: relevant register ID.
 * @op: component operation to execute.
 * @enable: true if to enable component debugging, false otherwise.
 */
struct hl_debug_params {
	void *input;
	void *output;
	u32 output_size;
	u32 reg_idx;
	u32 op;
	bool enable;
};

2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
/**
 * struct hl_notifier_event - holds the notifier data structure
 * @eventfd: the event file descriptor to raise the notifications
 * @lock: mutex lock to protect the notifier data flows
 * @events_mask: indicates the bitmap events
 */
struct hl_notifier_event {
	struct eventfd_ctx	*eventfd;
	struct mutex		lock;
	u64			events_mask;
};

2253 2254 2255 2256 2257 2258 2259 2260 2261
/*
 * FILE PRIVATE STRUCTURE
 */

/**
 * struct hl_fpriv - process information stored in FD private data.
 * @hdev: habanalabs device structure.
 * @filp: pointer to the given file structure.
 * @taskpid: current process ID.
2262
 * @ctx: current executing context. TODO: remove for multiple ctx per process
2263
 * @ctx_mgr: context manager to handle multiple context for this FD.
2264
 * @mem_mgr: manager descriptor for memory exportable via mmap
2265
 * @notifier_event: notifier eventfd towards user process
2266
 * @debugfs_list: list of relevant ASIC debugfs.
2267
 * @dev_node: node in the device list of file private data
2268
 * @refcount: number of related contexts.
2269
 * @restore_phase_mutex: lock for context switch and restore phase.
2270 2271
 * @ctx_lock: protects the pointer to current executing context pointer. TODO: remove for multiple
 *            ctx per process.
2272 2273
 */
struct hl_fpriv {
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
	struct hl_device		*hdev;
	struct file			*filp;
	struct pid			*taskpid;
	struct hl_ctx			*ctx;
	struct hl_ctx_mgr		ctx_mgr;
	struct hl_mem_mgr		mem_mgr;
	struct hl_notifier_event	notifier_event;
	struct list_head		debugfs_list;
	struct list_head		dev_node;
	struct kref			refcount;
	struct mutex			restore_phase_mutex;
2285
	struct mutex			ctx_lock;
2286 2287 2288
};


2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
/*
 * DebugFS
 */

/**
 * struct hl_info_list - debugfs file ops.
 * @name: file name.
 * @show: function to output information.
 * @write: function to write to the file.
 */
struct hl_info_list {
	const char	*name;
	int		(*show)(struct seq_file *s, void *data);
	ssize_t		(*write)(struct file *file, const char __user *buf,
				size_t count, loff_t *f_pos);
};

/**
 * struct hl_debugfs_entry - debugfs dentry wrapper.
2308
 * @info_ent: dentry related ops.
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
 * @dev_entry: ASIC specific debugfs manager.
 */
struct hl_debugfs_entry {
	const struct hl_info_list	*info_ent;
	struct hl_dbg_device_entry	*dev_entry;
};

/**
 * struct hl_dbg_device_entry - ASIC specific debugfs manager.
 * @root: root dentry.
 * @hdev: habanalabs device structure.
 * @entry_arr: array of available hl_debugfs_entry.
 * @file_list: list of available debugfs files.
 * @file_mutex: protects file_list.
 * @cb_list: list of available CBs.
 * @cb_spinlock: protects cb_list.
 * @cs_list: list of available CSs.
 * @cs_spinlock: protects cs_list.
 * @cs_job_list: list of available CB jobs.
 * @cs_job_spinlock: protects cs_job_list.
 * @userptr_list: list of available userptrs (virtual memory chunk descriptor).
 * @userptr_spinlock: protects userptr_list.
 * @ctx_mem_hash_list: list of available contexts with MMU mappings.
2332
 * @ctx_mem_hash_mutex: protects list of available contexts with MMU mappings.
2333 2334
 * @data_dma_blob_desc: data DMA descriptor of blob.
 * @mon_dump_blob_desc: monitor dump descriptor of blob.
2335 2336
 * @state_dump: data of the system states in case of a bad cs.
 * @state_dump_sem: protects state_dump.
2337 2338
 * @addr: next address to read/write from/to in read/write32.
 * @mmu_addr: next virtual address to translate to physical address in mmu_show.
2339
 * @mmu_cap_mask: mmu hw capability mask, to be used in mmu_ack_error.
2340
 * @userptr_lookup: the target user ptr to look up for on demand.
2341
 * @mmu_asid: ASID to use while translating in mmu_show.
2342
 * @state_dump_head: index of the latest state dump
2343
 * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
2344 2345
 * @i2c_addr: generic u8 debugfs file for address value to use in i2c_data_read.
 * @i2c_reg: generic u8 debugfs file for register value to use in i2c_data_read.
2346
 * @i2c_len: generic u8 debugfs file for length value to use in i2c_data_read.
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
 */
struct hl_dbg_device_entry {
	struct dentry			*root;
	struct hl_device		*hdev;
	struct hl_debugfs_entry		*entry_arr;
	struct list_head		file_list;
	struct mutex			file_mutex;
	struct list_head		cb_list;
	spinlock_t			cb_spinlock;
	struct list_head		cs_list;
	spinlock_t			cs_spinlock;
	struct list_head		cs_job_list;
	spinlock_t			cs_job_spinlock;
	struct list_head		userptr_list;
	spinlock_t			userptr_spinlock;
	struct list_head		ctx_mem_hash_list;
2363
	struct mutex			ctx_mem_hash_mutex;
2364 2365
	struct debugfs_blob_wrapper	data_dma_blob_desc;
	struct debugfs_blob_wrapper	mon_dump_blob_desc;
2366 2367
	char				*state_dump[HL_STATE_DUMP_HIST_LEN];
	struct rw_semaphore		state_dump_sem;
2368 2369
	u64				addr;
	u64				mmu_addr;
2370
	u64				mmu_cap_mask;
2371
	u64				userptr_lookup;
2372
	u32				mmu_asid;
2373
	u32				state_dump_head;
2374 2375 2376
	u8				i2c_bus;
	u8				i2c_addr;
	u8				i2c_reg;
2377
	u8				i2c_len;
2378 2379
};

2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
/**
 * struct hl_hw_obj_name_entry - single hw object name, member of
 * hl_state_dump_specs
 * @node: link to the containing hash table
 * @name: hw object name
 * @id: object identifier
 */
struct hl_hw_obj_name_entry {
	struct hlist_node	node;
	const char		*name;
	u32			id;
};

enum hl_state_dump_specs_props {
	SP_SYNC_OBJ_BASE_ADDR,
	SP_NEXT_SYNC_OBJ_ADDR,
	SP_SYNC_OBJ_AMOUNT,
	SP_MON_OBJ_WR_ADDR_LOW,
	SP_MON_OBJ_WR_ADDR_HIGH,
	SP_MON_OBJ_WR_DATA,
	SP_MON_OBJ_ARM_DATA,
	SP_MON_OBJ_STATUS,
	SP_MONITORS_AMOUNT,
	SP_TPC0_CMDQ,
	SP_TPC0_CFG_SO,
	SP_NEXT_TPC,
	SP_MME_CMDQ,
	SP_MME_CFG_SO,
	SP_NEXT_MME,
	SP_DMA_CMDQ,
	SP_DMA_CFG_SO,
	SP_DMA_QUEUES_OFFSET,
	SP_NUM_OF_MME_ENGINES,
	SP_SUB_MME_ENG_NUM,
	SP_NUM_OF_DMA_ENGINES,
	SP_NUM_OF_TPC_ENGINES,
	SP_ENGINE_NUM_OF_QUEUES,
	SP_ENGINE_NUM_OF_STREAMS,
	SP_ENGINE_NUM_OF_FENCES,
	SP_FENCE0_CNT_OFFSET,
	SP_FENCE0_RDATA_OFFSET,
	SP_CP_STS_OFFSET,
	SP_NUM_CORES,

	SP_MAX
};

enum hl_sync_engine_type {
	ENGINE_TPC,
	ENGINE_DMA,
	ENGINE_MME,
};

2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
/**
 * struct hl_mon_state_dump - represents a state dump of a single monitor
 * @id: monitor id
 * @wr_addr_low: address monitor will write to, low bits
 * @wr_addr_high: address monitor will write to, high bits
 * @wr_data: data monitor will write
 * @arm_data: register value containing monitor configuration
 * @status: monitor status
 */
struct hl_mon_state_dump {
	u32		id;
	u32		wr_addr_low;
	u32		wr_addr_high;
	u32		wr_data;
	u32		arm_data;
	u32		status;
};

2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
/**
 * struct hl_sync_to_engine_map_entry - sync object id to engine mapping entry
 * @engine_type: type of the engine
 * @engine_id: id of the engine
 * @sync_id: id of the sync object
 */
struct hl_sync_to_engine_map_entry {
	struct hlist_node		node;
	enum hl_sync_engine_type	engine_type;
	u32				engine_id;
	u32				sync_id;
};

/**
 * struct hl_sync_to_engine_map - maps sync object id to associated engine id
 * @tb: hash table containing the mapping, each element is of type
 *      struct hl_sync_to_engine_map_entry
 */
struct hl_sync_to_engine_map {
	DECLARE_HASHTABLE(tb, SYNC_TO_ENGINE_HASH_TABLE_BITS);
};

/**
 * struct hl_state_dump_specs_funcs - virtual functions used by the state dump
 * @gen_sync_to_engine_map: generate a hash map from sync obj id to its engine
2476 2477 2478
 * @print_single_monitor: format monitor data as string
 * @monitor_valid: return true if given monitor dump is valid
 * @print_fences_single_engine: format fences data as string
2479 2480 2481 2482
 */
struct hl_state_dump_specs_funcs {
	int (*gen_sync_to_engine_map)(struct hl_device *hdev,
				struct hl_sync_to_engine_map *map);
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
	int (*print_single_monitor)(char **buf, size_t *size, size_t *offset,
				    struct hl_device *hdev,
				    struct hl_mon_state_dump *mon);
	int (*monitor_valid)(struct hl_mon_state_dump *mon);
	int (*print_fences_single_engine)(struct hl_device *hdev,
					u64 base_offset,
					u64 status_base_offset,
					enum hl_sync_engine_type engine_type,
					u32 engine_id, char **buf,
					size_t *size, size_t *offset);
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
};

/**
 * struct hl_state_dump_specs - defines ASIC known hw objects names
 * @so_id_to_str_tb: sync objects names index table
 * @monitor_id_to_str_tb: monitors names index table
 * @funcs: virtual functions used for state dump
 * @sync_namager_names: readable names for sync manager if available (ex: N_E)
 * @props: pointer to a per asic const props array required for state dump
 */
struct hl_state_dump_specs {
	DECLARE_HASHTABLE(so_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
	DECLARE_HASHTABLE(monitor_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
	struct hl_state_dump_specs_funcs	funcs;
	const char * const			*sync_namager_names;
	s64					*props;
};

2511

2512 2513 2514 2515
/*
 * DEVICES
 */

2516 2517
#define HL_STR_MAX	32

2518
#define HL_DEV_STS_MAX (HL_DEVICE_STATUS_LAST + 1)
2519

2520
/* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
2521
 * x16 cards. In extreme cases, there are hosts that can accommodate 16 cards.
2522 2523 2524
 */
#define HL_MAX_MINORS	256

2525 2526 2527 2528 2529 2530 2531
/*
 * Registers read & write functions.
 */

u32 hl_rreg(struct hl_device *hdev, u32 reg);
void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);

2532 2533
#define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
#define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
2534
#define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n",	\
2535
			hdev->asic_funcs->rreg(hdev, (reg)))
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546

#define WREG32_P(reg, val, mask)				\
	do {							\
		u32 tmp_ = RREG32(reg);				\
		tmp_ &= (mask);					\
		tmp_ |= ((val) & ~(mask));			\
		WREG32(reg, tmp_);				\
	} while (0)
#define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
#define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))

2547 2548 2549
#define RMWREG32_SHIFTED(reg, val, mask) WREG32_P(reg, val, ~(mask))

#define RMWREG32(reg, val, mask) RMWREG32_SHIFTED(reg, (val) << __ffs(mask), mask)
2550 2551 2552

#define RREG32_MASK(reg, mask) ((RREG32(reg) & mask) >> __ffs(mask))

2553 2554
#define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
#define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
2555 2556 2557 2558
#define WREG32_FIELD(reg, offset, field, val)	\
	WREG32(mm##reg + offset, (RREG32(mm##reg + offset) & \
				~REG_FIELD_MASK(reg, field)) | \
				(val) << REG_FIELD_SHIFT(reg, field))
2559

2560 2561 2562
/* Timeout should be longer when working with simulator but cap the
 * increased timeout to some maximum
 */
2563
#define hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, elbi) \
2564
({ \
2565
	ktime_t __timeout; \
2566 2567
	u32 __elbi_read; \
	int __rc = 0; \
2568 2569 2570
	if (hdev->pdev) \
		__timeout = ktime_add_us(ktime_get(), timeout_us); \
	else \
2571 2572 2573
		__timeout = ktime_add_us(ktime_get(),\
				min((u64)(timeout_us * 10), \
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
2574 2575
	might_sleep_if(sleep_us); \
	for (;;) { \
2576 2577 2578 2579 2580 2581
		if (elbi) { \
			__rc = hl_pci_elbi_read(hdev, addr, &__elbi_read); \
			if (__rc) \
				break; \
			(val) = __elbi_read; \
		} else {\
2582
			(val) = RREG32(lower_32_bits(addr)); \
2583
		} \
2584 2585 2586
		if (cond) \
			break; \
		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2587 2588 2589 2590 2591 2592
			if (elbi) { \
				__rc = hl_pci_elbi_read(hdev, addr, &__elbi_read); \
				if (__rc) \
					break; \
				(val) = __elbi_read; \
			} else {\
2593
				(val) = RREG32(lower_32_bits(addr)); \
2594
			} \
2595 2596 2597 2598 2599
			break; \
		} \
		if (sleep_us) \
			usleep_range((sleep_us >> 2) + 1, sleep_us); \
	} \
2600
	__rc ? __rc : ((cond) ? 0 : -ETIMEDOUT); \
2601 2602
})

2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
#define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
		hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, false)

#define hl_poll_timeout_elbi(hdev, addr, val, cond, sleep_us, timeout_us) \
		hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, true)

/*
 * poll array of register addresses.
 * condition is satisfied if all registers values match the expected value.
 * once some register in the array satisfies the condition it will not be polled again,
 * this is done both for efficiency and due to some registers are "clear on read".
 * TODO: use read from PCI bar in other places in the code (SW-91406)
 */
#define hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
						timeout_us, elbi) \
({ \
	ktime_t __timeout; \
	u64 __elem_bitmask; \
	u32 __read_val;	\
	u8 __arr_idx;	\
	int __rc = 0; \
	\
	if (hdev->pdev) \
		__timeout = ktime_add_us(ktime_get(), timeout_us); \
	else \
		__timeout = ktime_add_us(ktime_get(),\
				min(((u64)timeout_us * 10), \
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
	\
	might_sleep_if(sleep_us); \
	if (arr_size >= 64) \
		__rc = -EINVAL; \
	else \
		__elem_bitmask = BIT_ULL(arr_size) - 1; \
	for (;;) { \
		if (__rc) \
			break; \
		for (__arr_idx = 0; __arr_idx < (arr_size); __arr_idx++) {	\
			if (!(__elem_bitmask & BIT_ULL(__arr_idx)))	\
				continue;	\
			if (elbi) { \
				__rc = hl_pci_elbi_read(hdev, (addr_arr)[__arr_idx], &__read_val); \
				if (__rc) \
					break; \
			} else { \
2648
				__read_val = RREG32(lower_32_bits(addr_arr[__arr_idx])); \
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672
			} \
			if (__read_val == (expected_val))	\
				__elem_bitmask &= ~BIT_ULL(__arr_idx);	\
		}	\
		if (__rc || (__elem_bitmask == 0)) \
			break; \
		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) \
			break; \
		if (sleep_us) \
			usleep_range((sleep_us >> 2) + 1, sleep_us); \
	} \
	__rc ? __rc : ((__elem_bitmask == 0) ? 0 : -ETIMEDOUT); \
})

#define hl_poll_reg_array_timeout(hdev, addr_arr, arr_size, expected_val, sleep_us, \
					timeout_us) \
	hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
						timeout_us, false)

#define hl_poll_reg_array_timeout_elbi(hdev, addr_arr, arr_size, expected_val, sleep_us, \
					timeout_us) \
	hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
						timeout_us, true)

2673 2674 2675
/*
 * address in this macro points always to a memory location in the
 * host's (server's) memory. That location is updated asynchronously
2676 2677 2678 2679 2680 2681 2682 2683
 * either by the direct access of the device or by another core.
 *
 * To work both in LE and BE architectures, we need to distinguish between the
 * two states (device or another core updates the memory location). Therefore,
 * if mem_written_by_device is true, the host memory being polled will be
 * updated directly by the device. If false, the host memory being polled will
 * be updated by host CPU. Required so host knows whether or not the memory
 * might need to be byte-swapped before returning value to caller.
2684
 */
2685 2686
#define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
				mem_written_by_device) \
2687 2688 2689 2690 2691
({ \
	ktime_t __timeout; \
	if (hdev->pdev) \
		__timeout = ktime_add_us(ktime_get(), timeout_us); \
	else \
2692
		__timeout = ktime_add_us(ktime_get(),\
2693
				min((u64)(timeout_us * 100), \
2694
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
2695 2696 2697 2698
	might_sleep_if(sleep_us); \
	for (;;) { \
		/* Verify we read updates done by other cores or by device */ \
		mb(); \
2699
		(val) = *((u32 *)(addr)); \
2700
		if (mem_written_by_device) \
2701
			(val) = le32_to_cpu(*(__le32 *) &(val)); \
2702 2703 2704
		if (cond) \
			break; \
		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2705
			(val) = *((u32 *)(addr)); \
2706
			if (mem_written_by_device) \
2707
				(val) = le32_to_cpu(*(__le32 *) &(val)); \
2708 2709 2710 2711 2712 2713 2714 2715
			break; \
		} \
		if (sleep_us) \
			usleep_range((sleep_us >> 2) + 1, sleep_us); \
	} \
	(cond) ? 0 : -ETIMEDOUT; \
})

2716 2717 2718 2719 2720 2721 2722 2723
#define HL_USR_MAPPED_BLK_INIT(blk, base, sz) \
({ \
	struct user_mapped_block *p = blk; \
\
	p->address = base; \
	p->size = sz; \
})

2724
#define HL_USR_INTR_STRUCT_INIT(usr_intr, hdev, intr_id, intr_type) \
2725 2726 2727
({ \
	usr_intr.hdev = hdev; \
	usr_intr.interrupt_id = intr_id; \
2728
	usr_intr.type = intr_type; \
2729 2730 2731 2732
	INIT_LIST_HEAD(&usr_intr.wait_list_head); \
	spin_lock_init(&usr_intr.wait_list_lock); \
})

2733 2734
struct hwmon_chip_info;

2735
/**
2736
 * struct hl_device_reset_work - reset work wrapper.
2737 2738
 * @reset_work: reset work to be done.
 * @hdev: habanalabs device structure.
2739
 * @flags: reset flags.
2740 2741
 */
struct hl_device_reset_work {
2742 2743 2744
	struct delayed_work	reset_work;
	struct hl_device	*hdev;
	u32			flags;
2745 2746
};

2747
/**
2748 2749
 * struct hl_mmu_hr_pgt_priv - used for holding per-device mmu host-resident
 * page-table internal information.
2750 2751 2752
 * @mmu_pgt_pool: pool of page tables used by a host-resident MMU for
 *                allocating hops.
 * @mmu_asid_hop0: per-ASID array of host-resident hop0 tables.
2753
 */
2754
struct hl_mmu_hr_priv {
2755 2756
	struct gen_pool	*mmu_pgt_pool;
	struct pgt_info	*mmu_asid_hop0;
2757 2758 2759 2760 2761 2762 2763 2764 2765
};

/**
 * struct hl_mmu_dr_pgt_priv - used for holding per-device mmu device-resident
 * page-table internal information.
 * @mmu_pgt_pool: pool of page tables used by MMU for allocating hops.
 * @mmu_shadow_hop0: shadow array of hop0 tables.
 */
struct hl_mmu_dr_priv {
2766 2767 2768 2769
	struct gen_pool *mmu_pgt_pool;
	void *mmu_shadow_hop0;
};

2770 2771 2772 2773 2774 2775 2776 2777 2778 2779
/**
 * struct hl_mmu_priv - used for holding per-device mmu internal information.
 * @dr: information on the device-resident MMU, when exists.
 * @hr: information on the host-resident MMU, when exists.
 */
struct hl_mmu_priv {
	struct hl_mmu_dr_priv dr;
	struct hl_mmu_hr_priv hr;
};

2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
/**
 * struct hl_mmu_per_hop_info - A structure describing one TLB HOP and its entry
 *                that was created in order to translate a virtual address to a
 *                physical one.
 * @hop_addr: The address of the hop.
 * @hop_pte_addr: The address of the hop entry.
 * @hop_pte_val: The value in the hop entry.
 */
struct hl_mmu_per_hop_info {
	u64 hop_addr;
	u64 hop_pte_addr;
	u64 hop_pte_val;
};

/**
 * struct hl_mmu_hop_info - A structure describing the TLB hops and their
 * hop-entries that were created in order to translate a virtual address to a
 * physical one.
2798 2799 2800
 * @scrambled_vaddr: The value of the virtual address after scrambling. This
 *                   address replaces the original virtual-address when mapped
 *                   in the MMU tables.
2801
 * @unscrambled_paddr: The un-scrambled physical address.
2802 2803
 * @hop_info: Array holding the per-hop information used for the translation.
 * @used_hops: The number of hops used for the translation.
2804
 * @range_type: virtual address range type.
2805 2806
 */
struct hl_mmu_hop_info {
2807
	u64 scrambled_vaddr;
2808
	u64 unscrambled_paddr;
2809
	struct hl_mmu_per_hop_info hop_info[MMU_ARCH_6_HOPS];
2810
	u32 used_hops;
2811
	enum hl_va_range_type range_type;
2812 2813
};

2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
/**
 * struct hl_hr_mmu_funcs - Device related host resident MMU functions.
 * @get_hop0_pgt_info: get page table info structure for HOP0.
 * @get_pgt_info: get page table info structure for HOP other than HOP0.
 * @add_pgt_info: add page table info structure to hash.
 * @get_tlb_mapping_params: get mapping parameters needed for getting TLB info for specific mapping.
 */
struct hl_hr_mmu_funcs {
	struct pgt_info *(*get_hop0_pgt_info)(struct hl_ctx *ctx);
	struct pgt_info *(*get_pgt_info)(struct hl_ctx *ctx, u64 phys_hop_addr);
	void (*add_pgt_info)(struct hl_ctx *ctx, struct pgt_info *pgt_info, dma_addr_t phys_addr);
	int (*get_tlb_mapping_params)(struct hl_device *hdev, struct hl_mmu_properties **mmu_prop,
								struct hl_mmu_hop_info *hops,
								u64 virt_addr, bool *is_huge);
};

2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
/**
 * struct hl_mmu_funcs - Device related MMU functions.
 * @init: initialize the MMU module.
 * @fini: release the MMU module.
 * @ctx_init: Initialize a context for using the MMU module.
 * @ctx_fini: disable a ctx from using the mmu module.
 * @map: maps a virtual address to physical address for a context.
 * @unmap: unmap a virtual address of a context.
 * @flush: flush all writes from all cores to reach device MMU.
 * @swap_out: marks all mapping of the given context as swapped out.
 * @swap_in: marks all mapping of the given context as swapped in.
2841 2842 2843
 * @get_tlb_info: returns the list of hops and hop-entries used that were
 *                created in order to translate the giver virtual address to a
 *                physical one.
2844
 * @hr_funcs: functions specific to host resident MMU.
2845 2846 2847 2848 2849 2850
 */
struct hl_mmu_funcs {
	int (*init)(struct hl_device *hdev);
	void (*fini)(struct hl_device *hdev);
	int (*ctx_init)(struct hl_ctx *ctx);
	void (*ctx_fini)(struct hl_ctx *ctx);
2851 2852 2853
	int (*map)(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size,
				bool is_dram_addr);
	int (*unmap)(struct hl_ctx *ctx, u64 virt_addr, bool is_dram_addr);
2854 2855 2856
	void (*flush)(struct hl_ctx *ctx);
	void (*swap_out)(struct hl_ctx *ctx);
	void (*swap_in)(struct hl_ctx *ctx);
2857 2858
	int (*get_tlb_info)(struct hl_ctx *ctx, u64 virt_addr, struct hl_mmu_hop_info *hops);
	struct hl_hr_mmu_funcs hr_funcs;
2859 2860
};

2861
/**
2862
 * struct hl_prefetch_work - prefetch work structure handler
2863
 * @prefetch_work: actual work struct.
2864 2865 2866 2867 2868 2869 2870
 * @ctx: compute context.
 * @va: virtual address to pre-fetch.
 * @size: pre-fetch size.
 * @flags: operation flags.
 * @asid: ASID for maintenance operation.
 */
struct hl_prefetch_work {
2871
	struct work_struct	prefetch_work;
2872 2873 2874 2875 2876 2877 2878 2879
	struct hl_ctx		*ctx;
	u64			va;
	u64			size;
	u32			flags;
	u32			asid;
};

/*
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
 * number of user contexts allowed to call wait_for_multi_cs ioctl in
 * parallel
 */
#define MULTI_CS_MAX_USER_CTX	2

/**
 * struct multi_cs_completion - multi CS wait completion.
 * @completion: completion of any of the CS in the list
 * @lock: spinlock for the completion structure
 * @timestamp: timestamp for the multi-CS completion
2890 2891
 * @stream_master_qid_map: bitmap of all stream masters on which the multi-CS
 *                        is waiting
2892 2893 2894 2895 2896 2897
 * @used: 1 if in use, otherwise 0
 */
struct multi_cs_completion {
	struct completion	completion;
	spinlock_t		lock;
	s64			timestamp;
2898
	u32			stream_master_qid_map;
2899 2900 2901 2902 2903 2904 2905 2906
	u8			used;
};

/**
 * struct multi_cs_data - internal data for multi CS call
 * @ctx: pointer to the context structure
 * @fence_arr: array of fences of all CSs
 * @seq_arr: array of CS sequence numbers
2907
 * @timeout_jiffies: timeout in jiffies for waiting for CS to complete
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
 * @timestamp: timestamp of first completed CS
 * @wait_status: wait for CS status
 * @completion_bitmap: bitmap of completed CSs (1- completed, otherwise 0)
 * @arr_len: fence_arr and seq_arr array length
 * @gone_cs: indication of gone CS (1- there was gone CS, otherwise 0)
 * @update_ts: update timestamp. 1- update the timestamp, otherwise 0.
 */
struct multi_cs_data {
	struct hl_ctx	*ctx;
	struct hl_fence	**fence_arr;
	u64		*seq_arr;
2919
	s64		timeout_jiffies;
2920 2921 2922 2923 2924 2925 2926 2927
	s64		timestamp;
	long		wait_status;
	u32		completion_bitmap;
	u8		arr_len;
	u8		gone_cs;
	u8		update_ts;
};

2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953
/**
 * struct hl_clk_throttle_timestamp - current/last clock throttling timestamp
 * @start: timestamp taken when 'start' event is received in driver
 * @end: timestamp taken when 'end' event is received in driver
 */
struct hl_clk_throttle_timestamp {
	ktime_t		start;
	ktime_t		end;
};

/**
 * struct hl_clk_throttle - keeps current/last clock throttling timestamps
 * @timestamp: timestamp taken by driver and firmware, index 0 refers to POWER
 *             index 1 refers to THERMAL
 * @lock: protects this structure as it can be accessed from both event queue
 *        context and info_ioctl context
 * @current_reason: bitmask represents the current clk throttling reasons
 * @aggregated_reason: bitmask represents aggregated clk throttling reasons since driver load
 */
struct hl_clk_throttle {
	struct hl_clk_throttle_timestamp timestamp[HL_CLK_THROTTLE_TYPE_MAX];
	struct mutex	lock;
	u32		current_reason;
	u32		aggregated_reason;
};

2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
/**
 * struct user_mapped_block - describes a hw block allowed to be mmapped by user
 * @address: physical HW block address
 * @size: allowed size for mmap
 */
struct user_mapped_block {
	u32 address;
	u32 size;
};

2964
/**
2965 2966
 * struct cs_timeout_info - info of last CS timeout occurred.
 * @timestamp: CS timeout timestamp.
2967 2968
 * @write_enable: if set writing to CS parameters in the structure is enabled. otherwise - disabled,
 *                so the first (root cause) CS timeout will not be overwritten.
2969 2970 2971 2972
 * @seq: CS timeout sequence number.
 */
struct cs_timeout_info {
	ktime_t		timestamp;
2973
	atomic_t	write_enable;
2974 2975 2976
	u64		seq;
};

2977 2978 2979 2980 2981 2982
#define MAX_QMAN_STREAMS_INFO		4
#define OPCODE_INFO_MAX_ADDR_SIZE	8
/**
 * struct undefined_opcode_info - info about last undefined opcode error
 * @timestamp: timestamp of the undefined opcode error
 * @cb_addr_streams: CB addresses (per stream) that are currently exists in the PQ
2983
 *                   entries. In case all streams array entries are
2984 2985 2986 2987
 *                   filled with values, it means the execution was in Lower-CP.
 * @cq_addr: the address of the current handled command buffer
 * @cq_size: the size of the current handled command buffer
 * @cb_addr_streams_len: num of streams - actual len of cb_addr_streams array.
2988 2989
 *                       should be equal to 1 in case of undefined opcode
 *                       in Upper-CP (specific stream) and equal to 4 in case
2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
 *                       of undefined opcode in Lower-CP.
 * @engine_id: engine-id that the error occurred on
 * @stream_id: the stream id the error occurred on. In case the stream equals to
 *             MAX_QMAN_STREAMS_INFO it means the error occurred on a Lower-CP.
 * @write_enable: if set, writing to undefined opcode parameters in the structure
 *                 is enable so the first (root cause) undefined opcode will not be
 *                 overwritten.
 */
struct undefined_opcode_info {
	ktime_t timestamp;
	u64 cb_addr_streams[MAX_QMAN_STREAMS_INFO][OPCODE_INFO_MAX_ADDR_SIZE];
	u64 cq_addr;
	u32 cq_size;
	u32 cb_addr_streams_len;
	u32 engine_id;
	u32 stream_id;
	bool write_enable;
};

3009
/**
3010 3011
 * struct page_fault_info - page fault information.
 * @page_fault: holds information collected during a page fault.
3012 3013
 * @user_mappings: buffer containing user mappings.
 * @num_of_user_mappings: number of user mappings.
3014 3015 3016 3017 3018
 * @page_fault_detected: if set as 1, then a page-fault was discovered for the
 *                       first time after the driver has finished booting-up.
 *                       Since we're looking for the page-fault's root cause,
 *                       we don't care of the others that might follow it-
 *                       so once changed to 1, it will remain that way.
3019
 * @page_fault_info_available: indicates that a page fault info is now available.
3020 3021
 */
struct page_fault_info {
3022
	struct hl_page_fault_info	page_fault;
3023 3024
	struct hl_user_mapping		*user_mappings;
	u64				num_of_user_mappings;
3025
	atomic_t			page_fault_detected;
3026
	bool				page_fault_info_available;
3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
};

/**
 * struct razwi_info - RAZWI information.
 * @razwi: holds information collected during a RAZWI
 * @razwi_detected: if set as 1, then a RAZWI was discovered for the
 *                  first time after the driver has finished booting-up.
 *                  Since we're looking for the RAZWI's root cause,
 *                  we don't care of the others that might follow it-
 *                  so once changed to 1, it will remain that way.
3037
 * @razwi_info_available: indicates that a RAZWI info is now available.
3038 3039 3040 3041
 */
struct razwi_info {
	struct hl_info_razwi_event	razwi;
	atomic_t			razwi_detected;
3042
	bool				razwi_info_available;
3043 3044
};

3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078
/**
 * struct hw_err_info - HW error information.
 * @event: holds information on the event.
 * @event_detected: if set as 1, then a HW event was discovered for the
 *                  first time after the driver has finished booting-up.
 *                  currently we assume that only fatal events (that require hard-reset) are
 *                  reported so we don't care of the others that might follow it.
 *                  so once changed to 1, it will remain that way.
 *                  TODO: support multiple events.
 * @event_info_available: indicates that a HW event info is now available.
 */
struct hw_err_info {
	struct hl_info_hw_err_event	event;
	atomic_t			event_detected;
	bool				event_info_available;
};

/**
 * struct fw_err_info - FW error information.
 * @event: holds information on the event.
 * @event_detected: if set as 1, then a FW event was discovered for the
 *                  first time after the driver has finished booting-up.
 *                  currently we assume that only fatal events (that require hard-reset) are
 *                  reported so we don't care of the others that might follow it.
 *                  so once changed to 1, it will remain that way.
 *                  TODO: support multiple events.
 * @event_info_available: indicates that a HW event info is now available.
 */
struct fw_err_info {
	struct hl_info_fw_err_event	event;
	atomic_t			event_detected;
	bool				event_info_available;
};

3079
/**
3080 3081
 * struct hl_error_info - holds information collected during an error.
 * @cs_timeout: CS timeout error information.
3082 3083 3084
 * @razwi_info: RAZWI information.
 * @undef_opcode: undefined opcode information.
 * @page_fault_info: page fault information.
3085 3086
 * @hw_err: (fatal) hardware error information.
 * @fw_err: firmware error information.
3087
 */
3088
struct hl_error_info {
3089
	struct cs_timeout_info		cs_timeout;
3090
	struct razwi_info		razwi_info;
3091
	struct undefined_opcode_info	undef_opcode;
3092
	struct page_fault_info		page_fault_info;
3093 3094
	struct hw_err_info		hw_err;
	struct fw_err_info		fw_err;
3095 3096
};

3097 3098
/**
 * struct hl_reset_info - holds current device reset information.
3099
 * @lock: lock to protect critical reset flows.
3100
 * @compute_reset_cnt: number of compute resets since the driver was loaded.
3101 3102
 * @hard_reset_cnt: number of hard resets since the driver was loaded.
 * @hard_reset_schedule_flags: hard reset is scheduled to after current compute reset,
3103
 *                             here we hold the hard reset flags.
3104
 * @in_reset: is device in reset flow.
3105
 * @in_compute_reset: Device is currently in reset but not in hard-reset.
3106 3107 3108 3109 3110
 * @needs_reset: true if reset_on_lockup is false and device should be reset
 *               due to lockup.
 * @hard_reset_pending: is there a hard reset work pending.
 * @curr_reset_cause: saves an enumerated reset cause when a hard reset is
 *                    triggered, and cleared after it is shared with preboot.
3111
 * @prev_reset_trigger: saves the previous trigger which caused a reset, overridden
3112 3113 3114 3115 3116
 *                      with a new value on next reset
 * @reset_trigger_repeated: set if device reset is triggered more than once with
 *                          same cause.
 * @skip_reset_on_timeout: Skip device reset if CS has timed out, wait for it to
 *                         complete instead.
3117
 * @watchdog_active: true if a device release watchdog work is scheduled.
3118 3119
 */
struct hl_reset_info {
3120
	spinlock_t	lock;
3121
	u32		compute_reset_cnt;
3122
	u32		hard_reset_cnt;
3123
	u32		hard_reset_schedule_flags;
3124
	u8		in_reset;
3125
	u8		in_compute_reset;
3126 3127 3128 3129 3130 3131
	u8		needs_reset;
	u8		hard_reset_pending;
	u8		curr_reset_cause;
	u8		prev_reset_trigger;
	u8		reset_trigger_repeated;
	u8		skip_reset_on_timeout;
3132
	u8		watchdog_active;
3133 3134
};

3135 3136 3137
/**
 * struct hl_device - habanalabs device structure.
 * @pdev: pointer to PCI device, can be NULL in case of simulator device.
3138 3139 3140
 * @pcie_bar_phys: array of available PCIe bars physical addresses.
 *		   (required only for PCI address match mode)
 * @pcie_bar: array of available PCIe bars virtual addresses.
3141
 * @rmmio: configuration area address on SRAM.
3142
 * @hclass: pointer to the habanalabs class.
3143
 * @cdev: related char device.
3144 3145 3146
 * @cdev_ctrl: char device for control operations only (INFO IOCTL)
 * @dev: related kernel basic device structure.
 * @dev_ctrl: related kernel device structure for the control device
3147
 * @work_heartbeat: delayed work for CPU-CP is-alive check.
3148
 * @device_reset_work: delayed work which performs hard reset
3149 3150
 * @device_release_watchdog_work: watchdog work that performs hard reset if user doesn't release
 *                                device upon certain error cases.
3151
 * @asic_name: ASIC specific name.
3152
 * @asic_type: ASIC specific type.
3153
 * @completion_queue: array of hl_cq.
3154 3155 3156
 * @user_interrupt: array of hl_user_interrupt. upon the corresponding user
 *                  interrupt, driver will monitor the list of fences
 *                  registered to this interrupt.
3157
 * @tpc_interrupt: single TPC interrupt for all TPCs.
3158 3159
 * @common_user_cq_interrupt: common user CQ interrupt for all user CQ interrupts.
 *                         upon any user CQ interrupt, driver will monitor the
3160
 *                         list of fences registered to this common structure.
3161
 * @common_decoder_interrupt: common decoder interrupt for all user decoder interrupts.
3162 3163
 * @shadow_cs_queue: pointer to a shadow queue that holds pointers to
 *                   outstanding command submissions.
3164 3165
 * @cq_wq: work queues of completion queues for executing work in process
 *         context.
3166
 * @eq_wq: work queue of event queue for executing work in process context.
3167 3168
 * @cs_cmplt_wq: work queue of CS completions for executing work in process
 *               context.
3169
 * @ts_free_obj_wq: work queue for timestamp registration objects release.
3170
 * @prefetch_wq: work queue for MMU pre-fetch operations.
3171
 * @reset_wq: work queue for device reset procedure.
3172
 * @kernel_ctx: Kernel driver context structure.
3173
 * @kernel_queues: array of hl_hw_queue.
3174 3175
 * @cs_mirror_list: CS mirror list for TDR.
 * @cs_mirror_lock: protects cs_mirror_list.
3176
 * @kernel_mem_mgr: memory manager for memory buffers with lifespan of driver.
3177
 * @event_queue: event queue for IRQ from CPU-CP.
3178
 * @dma_pool: DMA pool for small allocations.
3179 3180 3181
 * @cpu_accessible_dma_mem: Host <-> CPU-CP shared memory CPU address.
 * @cpu_accessible_dma_address: Host <-> CPU-CP shared memory DMA address.
 * @cpu_accessible_dma_pool: Host <-> CPU-CP shared memory pool.
3182 3183
 * @asid_bitmap: holds used/available ASIDs.
 * @asid_mutex: protects asid_bitmap.
3184
 * @send_cpu_message_lock: enforces only one message in Host <-> CPU-CP queue.
3185
 * @debug_lock: protects critical section of setting debug mode for device
3186 3187 3188 3189 3190 3191
 * @mmu_lock: protects the MMU page tables and invalidation h/w. Although the
 *            page tables are per context, the invalidation h/w is per MMU.
 *            Therefore, we can't allow multiple contexts (we only have two,
 *            user and kernel) to access the invalidation h/w at the same time.
 *            In addition, any change to the PGT, modifying the MMU hash or
 *            walking the PGT requires talking this lock.
3192 3193 3194
 * @asic_prop: ASIC specific immutable properties.
 * @asic_funcs: ASIC specific functions.
 * @asic_specific: ASIC specific information to use only from ASIC files.
3195
 * @vm: virtual memory manager for MMU.
3196 3197
 * @hwmon_dev: H/W monitor device.
 * @hl_chip_info: ASIC's sensors information.
3198
 * @device_status_description: device status description.
3199
 * @hl_debugfs: device's debugfs manager.
3200
 * @cb_pool: list of pre allocated CBs.
3201
 * @cb_pool_lock: protects the CB pool.
3202 3203 3204 3205
 * @internal_cb_pool_virt_addr: internal command buffer pool virtual address.
 * @internal_cb_pool_dma_addr: internal command buffer pool dma address.
 * @internal_cb_pool: internal command buffer memory pool.
 * @internal_cb_va_base: internal cb pool mmu virtual address base
3206 3207
 * @fpriv_list: list of file private data structures. Each structure is created
 *              when a user opens the device
3208 3209
 * @fpriv_ctrl_list: list of file private data structures. Each structure is created
 *              when a user opens the control device
3210
 * @fpriv_list_lock: protects the fpriv_list
3211
 * @fpriv_ctrl_list_lock: protects the fpriv_ctrl_list
3212
 * @aggregated_cs_counters: aggregated cs counters among all contexts
3213 3214
 * @mmu_priv: device-specific MMU data.
 * @mmu_func: device-related MMU functions.
3215
 * @dec: list of decoder sw instance
3216
 * @fw_loader: FW loader manager.
3217
 * @pci_mem_region: array of memory regions in the PCI
3218
 * @state_dump_specs: constants and dictionaries needed to dump system state.
3219
 * @multi_cs_completion: array of multi-CS completion.
3220
 * @clk_throttling: holds information about current/previous clock throttling events
3221
 * @captured_err_info: holds information about errors.
3222
 * @reset_info: holds current device reset information.
3223
 * @stream_master_qid_arr: pointer to array with QIDs of master streams.
3224 3225
 * @fw_major_version: major version of current loaded preboot.
 * @fw_minor_version: minor version of current loaded preboot.
3226
 * @dram_used_mem: current DRAM memory consumption.
3227
 * @memory_scrub_val: the value to which the dram will be scrubbed to using cb scrub_device_dram
3228
 * @timeout_jiffies: device CS timeout value.
3229
 * @max_power: the max power of the device, as configured by the sysadmin. This
3230 3231
 *             value is saved so in case of hard-reset, the driver will restore
 *             this value and update the F/W after the re-initialization
3232 3233 3234 3235 3236 3237
 * @boot_error_status_mask: contains a mask of the device boot error status.
 *                          Each bit represents a different error, according to
 *                          the defines in hl_boot_if.h. If the bit is cleared,
 *                          the error will be ignored by the driver during
 *                          device initialization. Mainly used to debug and
 *                          workaround firmware bugs
3238
 * @dram_pci_bar_start: start bus address of PCIe bar towards DRAM.
3239
 * @last_successful_open_ktime: timestamp (ktime) of the last successful device open.
3240 3241 3242 3243 3244
 * @last_successful_open_jif: timestamp (jiffies) of the last successful
 *                            device open.
 * @last_open_session_duration_jif: duration (jiffies) of the last device open
 *                                  session.
 * @open_counter: number of successful device open operations.
3245
 * @fw_poll_interval_usec: FW status poll interval in usec.
3246 3247 3248
 *                         used for CPU boot status
 * @fw_comms_poll_interval_usec: FW comms/protocol poll interval in usec.
 *                                  used for COMMs protocols cmds(COMMS_STS_*)
3249 3250 3251 3252
 * @dram_binning: contains mask of drams that is received from the f/w which indicates which
 *                drams are binned-out
 * @tpc_binning: contains mask of tpc engines that is received from the f/w which indicates which
 *               tpc engines are binned-out
3253
 * @dmabuf_export_cnt: number of dma-buf exporting.
3254 3255
 * @card_type: Various ASICs have several card types. This indicates the card
 *             type of the current device.
3256
 * @major: habanalabs kernel driver major.
3257
 * @high_pll: high PLL profile frequency.
3258 3259 3260 3261
 * @decoder_binning: contains mask of decoder engines that is received from the f/w which
 *                   indicates which decoder engines are binned-out
 * @edma_binning: contains mask of edma engines that is received from the f/w which
 *                   indicates which edma engines are binned-out
3262
 * @device_release_watchdog_timeout_sec: device release watchdog timeout value in seconds.
3263 3264
 * @rotator_binning: contains mask of rotators engines that is received from the f/w
 *			which indicates which rotator engines are binned-out(Gaudi3 and above).
3265
 * @id: device minor.
3266 3267
 * @id_control: minor of the control device.
 * @cdev_idx: char device index. Used for setting its name.
3268 3269
 * @cpu_pci_msb_addr: 50-bit extension bits for the device CPU's 40-bit
 *                    addresses.
3270
 * @is_in_dram_scrub: true if dram scrub operation is on going.
3271
 * @disabled: is device disabled.
3272 3273
 * @late_init_done: is late init stage was done during initialization.
 * @hwmon_initialized: is H/W monitor sensors was initialized.
3274 3275
 * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
 *                   otherwise.
3276
 * @dram_default_page_mapping: is DRAM default page mapping enabled.
3277 3278
 * @memory_scrub: true to perform device memory scrub in various locations,
 *                such as context-switch, context close, page free, etc.
3279 3280
 * @pmmu_huge_range: is a different virtual addresses range used for PMMU with
 *                   huge pages.
3281
 * @init_done: is the initialization of the device done.
3282
 * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
3283 3284 3285
 * @in_debug: whether the device is in a state where the profiling/tracing infrastructure
 *            can be used. This indication is needed because in some ASICs we need to do
 *            specific operations to enable that infrastructure.
3286
 * @cdev_sysfs_created: were char devices and sysfs nodes created.
3287
 * @stop_on_err: true if engines should stop on error.
3288
 * @supports_sync_stream: is sync stream supported.
3289
 * @sync_stream_queue_idx: helper index for sync stream queues initialization.
3290
 * @collective_mon_idx: helper index for collective initialization
3291
 * @supports_coresight: is CoreSight supported.
3292
 * @supports_cb_mapping: is mapping a CB to the device's MMU supported.
3293 3294 3295 3296
 * @process_kill_trial_cnt: number of trials reset thread tried killing
 *                          user processes
 * @device_fini_pending: true if device_fini was called and might be
 *                       waiting for the reset thread to finish
3297
 * @supports_staged_submission: true if staged submissions are supported
3298 3299 3300 3301
 * @device_cpu_is_halted: Flag to indicate whether the device CPU was already
 *                        halted. We can't halt it again because the COMMS
 *                        protocol will throw an error. Relevant only for
 *                        cases where Linux was not loaded to device CPU
3302
 * @supports_wait_for_multi_cs: true if wait for multi CS is supported
3303
 * @is_compute_ctx_active: Whether there is an active compute context executing.
3304
 * @compute_ctx_in_release: true if the current compute context is being released.
3305
 * @supports_mmu_prefetch: true if prefetch is supported, otherwise false.
3306 3307
 * @reset_upon_device_release: reset the device when the user closes the file descriptor of the
 *                             device.
3308 3309
 * @supports_ctx_switch: true if a ctx switch is required upon first submission.
 * @support_preboot_binning: true if we support read binning info from preboot.
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322
 * @nic_ports_mask: Controls which NIC ports are enabled. Used only for testing.
 * @fw_components: Controls which f/w components to load to the device. There are multiple f/w
 *                 stages and sometimes we want to stop at a certain stage. Used only for testing.
 * @mmu_enable: Whether to enable or disable the device MMU(s). Used only for testing.
 * @cpu_queues_enable: Whether to enable queues communication vs. the f/w. Used only for testing.
 * @pldm: Whether we are running in Palladium environment. Used only for testing.
 * @hard_reset_on_fw_events: Whether to do device hard-reset when a fatal event is received from
 *                           the f/w. Used only for testing.
 * @bmc_enable: Whether we are running in a box with BMC. Used only for testing.
 * @reset_on_preboot_fail: Whether to reset the device if preboot f/w fails to load.
 *                         Used only for testing.
 * @heartbeat: Controls if we want to enable the heartbeat mechanism vs. the f/w, which verifies
 *             that the f/w is always alive. Used only for testing.
3323 3324 3325
 */
struct hl_device {
	struct pci_dev			*pdev;
3326 3327
	u64				pcie_bar_phys[HL_PCI_NUM_BARS];
	void __iomem			*pcie_bar[HL_PCI_NUM_BARS];
3328
	void __iomem			*rmmio;
3329
	struct class			*hclass;
3330
	struct cdev			cdev;
3331
	struct cdev			cdev_ctrl;
3332
	struct device			*dev;
3333
	struct device			*dev_ctrl;
3334
	struct delayed_work		work_heartbeat;
3335
	struct hl_device_reset_work	device_reset_work;
3336
	struct hl_device_reset_work	device_release_watchdog_work;
3337 3338
	char				asic_name[HL_STR_MAX];
	char				status[HL_DEV_STS_MAX][HL_STR_MAX];
3339
	enum hl_asic_type		asic_type;
3340
	struct hl_cq			*completion_queue;
3341
	struct hl_user_interrupt	*user_interrupt;
3342
	struct hl_user_interrupt	tpc_interrupt;
3343
	struct hl_user_interrupt	common_user_cq_interrupt;
3344
	struct hl_user_interrupt	common_decoder_interrupt;
3345
	struct hl_cs			**shadow_cs_queue;
3346
	struct workqueue_struct		**cq_wq;
3347
	struct workqueue_struct		*eq_wq;
3348
	struct workqueue_struct		*cs_cmplt_wq;
3349
	struct workqueue_struct		*ts_free_obj_wq;
3350
	struct workqueue_struct		*prefetch_wq;
3351
	struct workqueue_struct		*reset_wq;
3352
	struct hl_ctx			*kernel_ctx;
3353
	struct hl_hw_queue		*kernel_queues;
3354 3355
	struct list_head		cs_mirror_list;
	spinlock_t			cs_mirror_lock;
3356
	struct hl_mem_mgr		kernel_mem_mgr;
3357
	struct hl_eq			event_queue;
3358 3359 3360 3361
	struct dma_pool			*dma_pool;
	void				*cpu_accessible_dma_mem;
	dma_addr_t			cpu_accessible_dma_address;
	struct gen_pool			*cpu_accessible_dma_pool;
3362 3363
	unsigned long			*asid_bitmap;
	struct mutex			asid_mutex;
3364
	struct mutex			send_cpu_message_lock;
3365
	struct mutex			debug_lock;
3366
	struct mutex			mmu_lock;
3367 3368 3369
	struct asic_fixed_properties	asic_prop;
	const struct hl_asic_funcs	*asic_funcs;
	void				*asic_specific;
3370
	struct hl_vm			vm;
3371 3372
	struct device			*hwmon_dev;
	struct hwmon_chip_info		*hl_chip_info;
3373

3374 3375
	struct hl_dbg_device_entry	hl_debugfs;

3376 3377 3378
	struct list_head		cb_pool;
	spinlock_t			cb_pool_lock;

3379 3380 3381 3382 3383
	void				*internal_cb_pool_virt_addr;
	dma_addr_t			internal_cb_pool_dma_addr;
	struct gen_pool			*internal_cb_pool;
	u64				internal_cb_va_base;

3384
	struct list_head		fpriv_list;
3385
	struct list_head		fpriv_ctrl_list;
3386
	struct mutex			fpriv_list_lock;
3387
	struct mutex			fpriv_ctrl_list_lock;
3388

3389
	struct hl_cs_counters_atomic	aggregated_cs_counters;
3390

3391
	struct hl_mmu_priv		mmu_priv;
3392
	struct hl_mmu_funcs		mmu_func[MMU_NUM_PGT_LOCATIONS];
3393

3394 3395
	struct hl_dec			*dec;

3396 3397
	struct fw_load_mgr		fw_loader;

3398 3399
	struct pci_mem_region		pci_mem_region[PCI_REGION_NUMBER];

3400 3401
	struct hl_state_dump_specs	state_dump_specs;

3402 3403
	struct multi_cs_completion	multi_cs_completion[
							MULTI_CS_MAX_USER_CTX];
3404
	struct hl_clk_throttle		clk_throttling;
3405
	struct hl_error_info		captured_err_info;
3406

3407 3408
	struct hl_reset_info		reset_info;

3409
	u32				*stream_master_qid_arr;
3410
	u32				fw_major_version;
3411
	u32				fw_minor_version;
3412
	atomic64_t			dram_used_mem;
3413
	u64				memory_scrub_val;
3414 3415
	u64				timeout_jiffies;
	u64				max_power;
3416
	u64				boot_error_status_mask;
3417
	u64				dram_pci_bar_start;
3418 3419 3420
	u64				last_successful_open_jif;
	u64				last_open_session_duration_jif;
	u64				open_counter;
3421
	u64				fw_poll_interval_usec;
3422
	ktime_t				last_successful_open_ktime;
3423
	u64				fw_comms_poll_interval_usec;
3424 3425
	u64				dram_binning;
	u64				tpc_binning;
3426
	atomic_t			dmabuf_export_cnt;
3427
	enum cpucp_card_types		card_type;
3428
	u32				major;
3429
	u32				high_pll;
3430 3431
	u32				decoder_binning;
	u32				edma_binning;
3432
	u32				device_release_watchdog_timeout_sec;
3433
	u32				rotator_binning;
3434
	u16				id;
3435
	u16				id_control;
3436
	u16				cdev_idx;
3437
	u16				cpu_pci_msb_addr;
3438
	u8				is_in_dram_scrub;
3439
	u8				disabled;
3440 3441
	u8				late_init_done;
	u8				hwmon_initialized;
3442
	u8				reset_on_lockup;
3443
	u8				dram_default_page_mapping;
3444
	u8				memory_scrub;
3445
	u8				pmmu_huge_range;
3446
	u8				init_done;
3447
	u8				device_cpu_disabled;
3448
	u8				in_debug;
3449
	u8				cdev_sysfs_created;
3450
	u8				stop_on_err;
3451
	u8				supports_sync_stream;
3452
	u8				sync_stream_queue_idx;
3453
	u8				collective_mon_idx;
3454
	u8				supports_coresight;
3455
	u8				supports_cb_mapping;
3456 3457
	u8				process_kill_trial_cnt;
	u8				device_fini_pending;
3458
	u8				supports_staged_submission;
3459
	u8				device_cpu_is_halted;
3460
	u8				supports_wait_for_multi_cs;
3461
	u8				stream_master_qid_arr_size;
3462
	u8				is_compute_ctx_active;
3463
	u8				compute_ctx_in_release;
3464
	u8				supports_mmu_prefetch;
3465
	u8				reset_upon_device_release;
3466
	u8				supports_ctx_switch;
3467
	u8				support_preboot_binning;
3468

3469
	/* Parameters for bring-up to be upstreamed */
3470
	u64				nic_ports_mask;
3471
	u64				fw_components;
3472
	u8				mmu_enable;
3473
	u8				cpu_queues_enable;
3474
	u8				pldm;
3475 3476
	u8				hard_reset_on_fw_events;
	u8				bmc_enable;
3477
	u8				reset_on_preboot_fail;
3478
	u8				heartbeat;
3479 3480 3481
};


3482 3483 3484 3485 3486 3487
/**
 * struct hl_cs_encaps_sig_handle - encapsulated signals handle structure
 * @refcount: refcount used to protect removing this id when several
 *            wait cs are used to wait of the reserved encaps signals.
 * @hdev: pointer to habanalabs device structure.
 * @hw_sob: pointer to  H/W SOB used in the reservation.
3488
 * @ctx: pointer to the user's context data structure
3489 3490 3491 3492 3493 3494 3495 3496 3497 3498
 * @cs_seq: staged cs sequence which contains encapsulated signals
 * @id: idr handler id to be used to fetch the handler info
 * @q_idx: stream queue index
 * @pre_sob_val: current SOB value before reservation
 * @count: signals number
 */
struct hl_cs_encaps_sig_handle {
	struct kref refcount;
	struct hl_device *hdev;
	struct hl_hw_sob *hw_sob;
3499
	struct hl_ctx *ctx;
3500 3501 3502 3503 3504 3505 3506
	u64  cs_seq;
	u32  id;
	u32  q_idx;
	u32  pre_sob_val;
	u32  count;
};

3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
/**
 * struct hl_info_fw_err_info - firmware error information structure
 * @err_type: The type of error detected (or reported).
 * @event_mask: Pointer to the event mask to be modified with the detected error flag
 *              (can be NULL)
 * @event_id: The id of the event that reported the error
 *            (applicable when err_type is HL_INFO_FW_REPORTED_ERR).
 */
struct hl_info_fw_err_info {
	enum hl_info_fw_err_type err_type;
	u64 *event_mask;
	u16 event_id;
};

3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
/*
 * IOCTLs
 */

/**
 * typedef hl_ioctl_t - typedef for ioctl function in the driver
 * @hpriv: pointer to the FD's private data, which contains state of
 *		user process
 * @data: pointer to the input/output arguments structure of the IOCTL
 *
 * Return: 0 for success, negative value for error
 */
typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);

/**
 * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
 * @cmd: the IOCTL code as created by the kernel macros.
 * @func: pointer to the driver's function that should be called for this IOCTL.
 */
struct hl_ioctl_desc {
	unsigned int cmd;
	hl_ioctl_t *func;
};


/*
 * Kernel module functions that can be accessed by entire module
 */

3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566
/**
 * hl_get_sg_info() - get number of pages and the DMA address from SG list.
 * @sg: the SG list.
 * @dma_addr: pointer to DMA address to return.
 *
 * Calculate the number of consecutive pages described by the SG list. Take the
 * offset of the address in the first page, add to it the length and round it up
 * to the number of needed pages.
 */
static inline u32 hl_get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr)
{
	*dma_addr = sg_dma_address(sg);

	return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) +
			(PAGE_SIZE - 1)) >> PAGE_SHIFT;
}

3567 3568 3569 3570 3571 3572 3573 3574 3575
/**
 * hl_mem_area_inside_range() - Checks whether address+size are inside a range.
 * @address: The start address of the area we want to validate.
 * @size: The size in bytes of the area we want to validate.
 * @range_start_address: The start address of the valid range.
 * @range_end_address: The end address of the valid range.
 *
 * Return: true if the area is inside the valid range, false otherwise.
 */
3576
static inline bool hl_mem_area_inside_range(u64 address, u64 size,
3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601
				u64 range_start_address, u64 range_end_address)
{
	u64 end_address = address + size;

	if ((address >= range_start_address) &&
			(end_address <= range_end_address) &&
			(end_address > address))
		return true;

	return false;
}

/**
 * hl_mem_area_crosses_range() - Checks whether address+size crossing a range.
 * @address: The start address of the area we want to validate.
 * @size: The size in bytes of the area we want to validate.
 * @range_start_address: The start address of the valid range.
 * @range_end_address: The end address of the valid range.
 *
 * Return: true if the area overlaps part or all of the valid range,
 *		false otherwise.
 */
static inline bool hl_mem_area_crosses_range(u64 address, u32 size,
				u64 range_start_address, u64 range_end_address)
{
3602
	u64 end_address = address + size - 1;
3603

3604
	return ((address <= range_end_address) && (range_start_address <= end_address));
3605 3606
}

3607
uint64_t hl_set_dram_bar_default(struct hl_device *hdev, u64 addr);
3608 3609
void *hl_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle);
void hl_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size, void *vaddr);
3610 3611 3612 3613 3614 3615 3616 3617
void *hl_asic_dma_alloc_coherent_caller(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle,
					gfp_t flag, const char *caller);
void hl_asic_dma_free_coherent_caller(struct hl_device *hdev, size_t size, void *cpu_addr,
					dma_addr_t dma_handle, const char *caller);
void *hl_asic_dma_pool_zalloc_caller(struct hl_device *hdev, size_t size, gfp_t mem_flags,
					dma_addr_t *dma_handle, const char *caller);
void hl_asic_dma_pool_free_caller(struct hl_device *hdev, void *vaddr, dma_addr_t dma_addr,
					const char *caller);
3618 3619 3620
int hl_dma_map_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir);
void hl_dma_unmap_sgtable(struct hl_device *hdev, struct sg_table *sgt,
				enum dma_data_direction dir);
3621 3622
int hl_access_sram_dram_region(struct hl_device *hdev, u64 addr, u64 *val,
	enum debugfs_access_type acc_type, enum pci_region region_type, bool set_dram_bar);
3623 3624
int hl_access_cfg_region(struct hl_device *hdev, u64 addr, u64 *val,
	enum debugfs_access_type acc_type);
3625 3626
int hl_access_dev_mem(struct hl_device *hdev, enum pci_region region_type,
			u64 addr, u64 *val, enum debugfs_access_type acc_type);
3627
int hl_device_open(struct inode *inode, struct file *filp);
3628
int hl_device_open_ctrl(struct inode *inode, struct file *filp);
3629 3630
bool hl_device_operational(struct hl_device *hdev,
		enum hl_device_status *status);
3631 3632
bool hl_ctrl_device_operational(struct hl_device *hdev,
		enum hl_device_status *status);
3633
enum hl_device_status hl_device_status(struct hl_device *hdev);
3634
int hl_device_set_debug_mode(struct hl_device *hdev, struct hl_ctx *ctx, bool enable);
3635 3636 3637
int hl_hw_queues_create(struct hl_device *hdev);
void hl_hw_queues_destroy(struct hl_device *hdev);
int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
3638 3639 3640
		u32 cb_size, u64 cb_ptr);
void hl_hw_queue_submit_bd(struct hl_device *hdev, struct hl_hw_queue *q,
		u32 ctl, u32 len, u64 ptr);
3641
int hl_hw_queue_schedule_cs(struct hl_cs *cs);
3642 3643
u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
3644
void hl_hw_queue_update_ci(struct hl_cs *cs);
3645
void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
3646 3647 3648 3649 3650 3651

#define hl_queue_inc_ptr(p)		hl_hw_queue_add_ptr(p, 1)
#define hl_pi_2_offset(pi)		((pi) & (HL_QUEUE_LENGTH - 1))

int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
3652 3653
int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
3654 3655
void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
3656 3657
irqreturn_t hl_irq_handler_cq(int irq, void *arg);
irqreturn_t hl_irq_handler_eq(int irq, void *arg);
3658 3659
irqreturn_t hl_irq_handler_dec_abnrm(int irq, void *arg);
irqreturn_t hl_irq_handler_user_interrupt(int irq, void *arg);
3660
irqreturn_t hl_irq_user_interrupt_thread_handler(int irq, void *arg);
3661 3662
u32 hl_cq_inc_ptr(u32 ptr);

3663 3664 3665 3666 3667 3668 3669 3670
int hl_asid_init(struct hl_device *hdev);
void hl_asid_fini(struct hl_device *hdev);
unsigned long hl_asid_alloc(struct hl_device *hdev);
void hl_asid_free(struct hl_device *hdev, unsigned long asid);

int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
3671
void hl_ctx_do_release(struct kref *ref);
3672
void hl_ctx_get(struct hl_ctx *ctx);
3673
int hl_ctx_put(struct hl_ctx *ctx);
3674
struct hl_ctx *hl_get_compute_ctx(struct hl_device *hdev);
3675
struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
3676 3677
int hl_ctx_get_fences(struct hl_ctx *ctx, u64 *seq_arr,
				struct hl_fence **fence, u32 arr_len);
3678 3679
void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
3680

3681
int hl_device_init(struct hl_device *hdev);
3682 3683 3684
void hl_device_fini(struct hl_device *hdev);
int hl_device_suspend(struct hl_device *hdev);
int hl_device_resume(struct hl_device *hdev);
3685
int hl_device_reset(struct hl_device *hdev, u32 flags);
3686
int hl_device_cond_reset(struct hl_device *hdev, u32 flags, u64 event_mask);
3687
void hl_hpriv_get(struct hl_fpriv *hpriv);
3688
int hl_hpriv_put(struct hl_fpriv *hpriv);
3689
int hl_device_utilization(struct hl_device *hdev, u32 *utilization);
3690

3691
int hl_build_hwmon_channel_info(struct hl_device *hdev,
3692
		struct cpucp_sensor *sensors_arr);
3693

3694
void hl_notifier_event_send_all(struct hl_device *hdev, u64 event_mask);
3695

3696 3697 3698 3699 3700
int hl_sysfs_init(struct hl_device *hdev);
void hl_sysfs_fini(struct hl_device *hdev);

int hl_hwmon_init(struct hl_device *hdev);
void hl_hwmon_fini(struct hl_device *hdev);
3701
void hl_hwmon_release_resources(struct hl_device *hdev);
3702

3703
int hl_cb_create(struct hl_device *hdev, struct hl_mem_mgr *mmg,
3704
			struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
3705
			bool map_cb, u64 *handle);
3706
int hl_cb_destroy(struct hl_mem_mgr *mmg, u64 cb_handle);
3707
int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
3708
struct hl_cb *hl_cb_get(struct hl_mem_mgr *mmg, u64 handle);
3709
void hl_cb_put(struct hl_cb *cb);
3710 3711
struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
					bool internal_cb);
3712 3713
int hl_cb_pool_init(struct hl_device *hdev);
int hl_cb_pool_fini(struct hl_device *hdev);
3714 3715
int hl_cb_va_pool_init(struct hl_ctx *ctx);
void hl_cb_va_pool_fini(struct hl_ctx *ctx);
3716

3717
void hl_cs_rollback_all(struct hl_device *hdev, bool skip_wq_flush);
3718 3719
struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
		enum hl_queue_type queue_type, bool is_kernel_allocated_cb);
3720
void hl_sob_reset_error(struct kref *ref);
3721
int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask);
3722
void hl_fence_put(struct hl_fence *fence);
3723
void hl_fences_put(struct hl_fence **fence, int len);
3724
void hl_fence_get(struct hl_fence *fence);
3725
void cs_get(struct hl_cs *cs);
3726 3727
bool cs_needs_completion(struct hl_cs *cs);
bool cs_needs_timeout(struct hl_cs *cs);
3728 3729
bool is_staged_cs_last_exists(struct hl_device *hdev, struct hl_cs *cs);
struct hl_cs *hl_staged_cs_find_first(struct hl_device *hdev, u64 cs_seq);
3730
void hl_multi_cs_completion_init(struct hl_device *hdev);
3731
u32 hl_get_active_cs_num(struct hl_device *hdev);
3732

3733
void goya_set_asic_funcs(struct hl_device *hdev);
3734
void gaudi_set_asic_funcs(struct hl_device *hdev);
3735
void gaudi2_set_asic_funcs(struct hl_device *hdev);
3736

3737 3738 3739 3740 3741 3742
int hl_vm_ctx_init(struct hl_ctx *ctx);
void hl_vm_ctx_fini(struct hl_ctx *ctx);

int hl_vm_init(struct hl_device *hdev);
void hl_vm_fini(struct hl_device *hdev);

3743 3744 3745
void hl_hw_block_mem_init(struct hl_ctx *ctx);
void hl_hw_block_mem_fini(struct hl_ctx *ctx);

3746
u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
3747
		enum hl_va_range_type type, u64 size, u32 alignment);
3748 3749
int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
		u64 start_addr, u64 size);
3750
int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
3751
			struct hl_userptr *userptr);
3752
void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
3753 3754 3755 3756 3757 3758
void hl_userptr_delete_list(struct hl_device *hdev,
				struct list_head *userptr_list);
bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, u32 size,
				struct list_head *userptr_list,
				struct hl_userptr **userptr);

3759 3760
int hl_mmu_init(struct hl_device *hdev);
void hl_mmu_fini(struct hl_device *hdev);
3761
int hl_mmu_ctx_init(struct hl_ctx *ctx);
3762
void hl_mmu_ctx_fini(struct hl_ctx *ctx);
3763
int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
3764
		u32 page_size, bool flush_pte);
3765 3766
int hl_mmu_get_real_page_size(struct hl_device *hdev, struct hl_mmu_properties *mmu_prop,
				u32 page_size, u32 *real_page_size, bool is_dram_addr);
3767
int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
3768
		bool flush_pte);
3769 3770 3771
int hl_mmu_map_contiguous(struct hl_ctx *ctx, u64 virt_addr,
					u64 phys_addr, u32 size);
int hl_mmu_unmap_contiguous(struct hl_ctx *ctx, u64 virt_addr, u32 size);
3772 3773 3774
int hl_mmu_invalidate_cache(struct hl_device *hdev, bool is_hard, u32 flags);
int hl_mmu_invalidate_cache_range(struct hl_device *hdev, bool is_hard,
					u32 flags, u32 asid, u64 va, u64 size);
3775
int hl_mmu_prefetch_cache_range(struct hl_ctx *ctx, u32 flags, u32 asid, u64 va, u64 size);
3776 3777 3778
u64 hl_mmu_get_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte);
u64 hl_mmu_get_hop_pte_phys_addr(struct hl_ctx *ctx, struct hl_mmu_properties *mmu_prop,
					u8 hop_idx, u64 hop_addr, u64 virt_addr);
3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806
void hl_mmu_hr_flush(struct hl_ctx *ctx);
int hl_mmu_hr_init(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv, u32 hop_table_size,
			u64 pgt_size);
void hl_mmu_hr_fini(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv, u32 hop_table_size);
void hl_mmu_hr_free_hop_remove_pgt(struct pgt_info *pgt_info, struct hl_mmu_hr_priv *hr_priv,
				u32 hop_table_size);
u64 hl_mmu_hr_pte_phys_to_virt(struct hl_ctx *ctx, struct pgt_info *pgt, u64 phys_pte_addr,
							u32 hop_table_size);
void hl_mmu_hr_write_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, u64 phys_pte_addr,
							u64 val, u32 hop_table_size);
void hl_mmu_hr_clear_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, u64 phys_pte_addr,
							u32 hop_table_size);
int hl_mmu_hr_put_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, struct hl_mmu_hr_priv *hr_priv,
							u32 hop_table_size);
void hl_mmu_hr_get_pte(struct hl_ctx *ctx, struct hl_hr_mmu_funcs *hr_func, u64 phys_hop_addr);
struct pgt_info *hl_mmu_hr_get_next_hop_pgt_info(struct hl_ctx *ctx,
							struct hl_hr_mmu_funcs *hr_func,
							u64 curr_pte);
struct pgt_info *hl_mmu_hr_alloc_hop(struct hl_ctx *ctx, struct hl_mmu_hr_priv *hr_priv,
							struct hl_hr_mmu_funcs *hr_func,
							struct hl_mmu_properties *mmu_prop);
struct pgt_info *hl_mmu_hr_get_alloc_next_hop(struct hl_ctx *ctx,
							struct hl_mmu_hr_priv *hr_priv,
							struct hl_hr_mmu_funcs *hr_func,
							struct hl_mmu_properties *mmu_prop,
							u64 curr_pte, bool *is_new_hop);
int hl_mmu_hr_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr, struct hl_mmu_hop_info *hops,
							struct hl_hr_mmu_funcs *hr_func);
3807 3808
void hl_mmu_swap_out(struct hl_ctx *ctx);
void hl_mmu_swap_in(struct hl_ctx *ctx);
3809
int hl_mmu_if_set_funcs(struct hl_device *hdev);
3810
void hl_mmu_v1_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
3811
void hl_mmu_v2_hr_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
3812 3813 3814
int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr);
int hl_mmu_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
			struct hl_mmu_hop_info *hops);
3815 3816
u64 hl_mmu_scramble_addr(struct hl_device *hdev, u64 addr);
u64 hl_mmu_descramble_addr(struct hl_device *hdev, u64 addr);
3817
bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr);
3818

3819
int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
3820
				void __iomem *dst, u32 src_offset, u32 size);
3821
int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode, u64 value);
3822
int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
3823
				u16 len, u32 timeout, u64 *result);
3824 3825 3826
int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type);
int hl_fw_unmask_irq_arr(struct hl_device *hdev, const u32 *irq_arr,
		size_t irq_arr_size);
3827 3828 3829 3830 3831 3832
int hl_fw_test_cpu_queue(struct hl_device *hdev);
void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
						dma_addr_t *dma_handle);
void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
					void *vaddr);
int hl_fw_send_heartbeat(struct hl_device *hdev);
3833
int hl_fw_cpucp_info_get(struct hl_device *hdev,
3834 3835 3836
				u32 sts_boot_dev_sts0_reg,
				u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
				u32 boot_err1_reg);
3837
int hl_fw_cpucp_handshake(struct hl_device *hdev,
3838 3839 3840
				u32 sts_boot_dev_sts0_reg,
				u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
				u32 boot_err1_reg);
3841
int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
3842
int hl_fw_get_monitor_dump(struct hl_device *hdev, void *data);
3843
int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
3844
		struct hl_info_pci_counters *counters);
3845
int hl_fw_cpucp_total_energy_get(struct hl_device *hdev,
3846
			u64 *total_energy);
3847
int get_used_pll_index(struct hl_device *hdev, u32 input_pll_index,
3848
						enum pll_index *pll_index);
3849
int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
3850
		u16 *pll_freq_arr);
3851
int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power);
3852 3853
void hl_fw_ask_hard_reset_without_linux(struct hl_device *hdev);
void hl_fw_ask_halt_machine_without_linux(struct hl_device *hdev);
3854
int hl_fw_init_cpu(struct hl_device *hdev);
3855
int hl_fw_wait_preboot_ready(struct hl_device *hdev);
3856
int hl_fw_read_preboot_status(struct hl_device *hdev);
3857 3858 3859 3860
int hl_fw_dynamic_send_protocol_cmd(struct hl_device *hdev,
				struct fw_load_mgr *fw_loader,
				enum comms_cmd cmd, unsigned int size,
				bool wait_ok, u32 timeout);
3861 3862 3863
int hl_fw_dram_replaced_row_get(struct hl_device *hdev,
				struct cpucp_hbm_row_info *info);
int hl_fw_dram_pending_row_get(struct hl_device *hdev, u32 *pend_rows_num);
3864
int hl_fw_cpucp_engine_core_asid_set(struct hl_device *hdev, u32 asid);
3865
int hl_fw_send_device_activity(struct hl_device *hdev, bool open);
3866 3867
int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
			bool is_wc[3]);
3868
int hl_pci_elbi_read(struct hl_device *hdev, u64 addr, u32 *data);
3869
int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
3870 3871 3872 3873
int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
		struct hl_inbound_pci_region *pci_region);
int hl_pci_set_outbound_region(struct hl_device *hdev,
		struct hl_outbound_pci_region *pci_region);
3874
enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr);
3875
int hl_pci_init(struct hl_device *hdev);
3876 3877
void hl_pci_fini(struct hl_device *hdev);

3878 3879 3880 3881 3882 3883 3884 3885 3886
long hl_fw_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr);
void hl_fw_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq);
int hl_get_temperature(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
int hl_set_temperature(struct hl_device *hdev, int sensor_index, u32 attr, long value);
int hl_get_voltage(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
int hl_get_current(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
int hl_get_fan_speed(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
int hl_get_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr, long value);
3887
long hl_fw_get_max_power(struct hl_device *hdev);
3888
void hl_fw_set_max_power(struct hl_device *hdev);
3889 3890
int hl_fw_get_sec_attest_info(struct hl_device *hdev, struct cpucp_sec_attest_info *sec_attest_info,
				u32 nonce);
3891 3892 3893 3894
int hl_set_voltage(struct hl_device *hdev, int sensor_index, u32 attr, long value);
int hl_set_current(struct hl_device *hdev, int sensor_index, u32 attr, long value);
int hl_set_power(struct hl_device *hdev, int sensor_index, u32 attr, long value);
int hl_get_power(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
Oded Gabbay's avatar
Oded Gabbay committed
3895
int hl_fw_get_clk_rate(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk);
3896
void hl_fw_set_pll_profile(struct hl_device *hdev);
3897
void hl_sysfs_add_dev_clk_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp);
3898
void hl_sysfs_add_dev_vrm_attr(struct hl_device *hdev, struct attribute_group *dev_vrm_attr_grp);
3899 3900
int hl_fw_send_generic_request(struct hl_device *hdev, enum hl_passthrough_type sub_opcode,
						dma_addr_t buff, u32 *size);
3901

3902 3903
void hw_sob_get(struct hl_hw_sob *hw_sob);
void hw_sob_put(struct hl_hw_sob *hw_sob);
3904 3905
void hl_encaps_release_handle_and_put_ctx(struct kref *ref);
void hl_encaps_release_handle_and_put_sob_ctx(struct kref *ref);
3906 3907 3908
void hl_hw_queue_encaps_sig_set_sob_info(struct hl_device *hdev,
			struct hl_cs *cs, struct hl_cs_job *job,
			struct hl_cs_compl *cs_cmpl);
3909 3910 3911 3912 3913

int hl_dec_init(struct hl_device *hdev);
void hl_dec_fini(struct hl_device *hdev);
void hl_dec_ctx_fini(struct hl_ctx *ctx);

3914
void hl_release_pending_user_interrupts(struct hl_device *hdev);
3915
void hl_abort_waitings_for_completion(struct hl_device *hdev);
3916
int hl_cs_signal_sob_wraparound_handler(struct hl_device *hdev, u32 q_idx,
3917
			struct hl_hw_sob **hw_sob, u32 count, bool encaps_sig);
3918

3919 3920
int hl_state_dump(struct hl_device *hdev);
const char *hl_state_dump_get_sync_name(struct hl_device *hdev, u32 sync_id);
3921 3922
const char *hl_state_dump_get_monitor_name(struct hl_device *hdev,
					struct hl_mon_state_dump *mon);
3923 3924 3925 3926 3927
void hl_state_dump_free_sync_to_engine_map(struct hl_sync_to_engine_map *map);
__printf(4, 5) int hl_snprintf_resize(char **buf, size_t *size, size_t *offset,
					const char *format, ...);
char *hl_format_as_binary(char *buf, size_t buf_len, u32 n);
const char *hl_sync_engine_to_string(enum hl_sync_engine_type engine_type);
3928

3929
void hl_mem_mgr_init(struct device *dev, struct hl_mem_mgr *mmg);
3930
void hl_mem_mgr_fini(struct hl_mem_mgr *mmg);
3931
void hl_mem_mgr_idr_destroy(struct hl_mem_mgr *mmg);
3932 3933 3934
int hl_mem_mgr_mmap(struct hl_mem_mgr *mmg, struct vm_area_struct *vma,
		    void *args);
struct hl_mmap_mem_buf *hl_mmap_mem_buf_get(struct hl_mem_mgr *mmg,
3935
						   u64 handle);
3936
int hl_mmap_mem_buf_put_handle(struct hl_mem_mgr *mmg, u64 handle);
3937 3938 3939
int hl_mmap_mem_buf_put(struct hl_mmap_mem_buf *buf);
struct hl_mmap_mem_buf *
hl_mmap_mem_buf_alloc(struct hl_mem_mgr *mmg,
3940
		      struct hl_mmap_mem_buf_behavior *behavior, gfp_t gfp,
3941
		      void *args);
3942
__printf(2, 3) void hl_engine_data_sprintf(struct engines_data *e, const char *fmt, ...);
3943 3944
void hl_capture_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines,
			u8 flags);
3945 3946
void hl_handle_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines,
			u8 flags, u64 *event_mask);
3947
void hl_capture_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu);
3948 3949
void hl_handle_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu,
				u64 *event_mask);
3950 3951
void hl_handle_critical_hw_err(struct hl_device *hdev, u16 event_id, u64 *event_mask);
void hl_handle_fw_err(struct hl_device *hdev, struct hl_info_fw_err_info *info);
3952

3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971
#ifdef CONFIG_DEBUG_FS

void hl_debugfs_init(void);
void hl_debugfs_fini(void);
void hl_debugfs_add_device(struct hl_device *hdev);
void hl_debugfs_remove_device(struct hl_device *hdev);
void hl_debugfs_add_file(struct hl_fpriv *hpriv);
void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
void hl_debugfs_add_cb(struct hl_cb *cb);
void hl_debugfs_remove_cb(struct hl_cb *cb);
void hl_debugfs_add_cs(struct hl_cs *cs);
void hl_debugfs_remove_cs(struct hl_cs *cs);
void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
void hl_debugfs_remove_userptr(struct hl_device *hdev,
				struct hl_userptr *userptr);
void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
3972 3973
void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
					unsigned long length);
3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046

#else

static inline void __init hl_debugfs_init(void)
{
}

static inline void hl_debugfs_fini(void)
{
}

static inline void hl_debugfs_add_device(struct hl_device *hdev)
{
}

static inline void hl_debugfs_remove_device(struct hl_device *hdev)
{
}

static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
{
}

static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
{
}

static inline void hl_debugfs_add_cb(struct hl_cb *cb)
{
}

static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
{
}

static inline void hl_debugfs_add_cs(struct hl_cs *cs)
{
}

static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
{
}

static inline void hl_debugfs_add_job(struct hl_device *hdev,
					struct hl_cs_job *job)
{
}

static inline void hl_debugfs_remove_job(struct hl_device *hdev,
					struct hl_cs_job *job)
{
}

static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
					struct hl_userptr *userptr)
{
}

static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
					struct hl_userptr *userptr)
{
}

static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
					struct hl_ctx *ctx)
{
}

static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
					struct hl_ctx *ctx)
{
}

4047 4048
static inline void hl_debugfs_set_state_dump(struct hl_device *hdev,
					char *data, unsigned long length)
4049 4050 4051
{
}

4052 4053
#endif

4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102
/* Security */
int hl_unsecure_register(struct hl_device *hdev, u32 mm_reg_addr, int offset,
		const u32 pb_blocks[], struct hl_block_glbl_sec sgs_array[],
		int array_size);
int hl_unsecure_registers(struct hl_device *hdev, const u32 mm_reg_array[],
		int mm_array_size, int offset, const u32 pb_blocks[],
		struct hl_block_glbl_sec sgs_array[], int blocks_array_size);
void hl_config_glbl_sec(struct hl_device *hdev, const u32 pb_blocks[],
		struct hl_block_glbl_sec sgs_array[], u32 block_offset,
		int array_size);
void hl_secure_block(struct hl_device *hdev,
		struct hl_block_glbl_sec sgs_array[], int array_size);
int hl_init_pb_with_mask(struct hl_device *hdev, u32 num_dcores,
		u32 dcore_offset, u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const u32 *regs_array, u32 regs_array_size, u64 mask);
int hl_init_pb(struct hl_device *hdev, u32 num_dcores, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const u32 *regs_array, u32 regs_array_size);
int hl_init_pb_ranges_with_mask(struct hl_device *hdev, u32 num_dcores,
		u32 dcore_offset, u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const struct range *regs_range_array, u32 regs_range_array_size,
		u64 mask);
int hl_init_pb_ranges(struct hl_device *hdev, u32 num_dcores,
		u32 dcore_offset, u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const struct range *regs_range_array,
		u32 regs_range_array_size);
int hl_init_pb_single_dcore(struct hl_device *hdev, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const u32 *regs_array, u32 regs_array_size);
int hl_init_pb_ranges_single_dcore(struct hl_device *hdev, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const struct range *regs_range_array,
		u32 regs_range_array_size);
void hl_ack_pb(struct hl_device *hdev, u32 num_dcores, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size);
void hl_ack_pb_with_mask(struct hl_device *hdev, u32 num_dcores,
		u32 dcore_offset, u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size, u64 mask);
void hl_ack_pb_single_dcore(struct hl_device *hdev, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size);

4103 4104
/* IOCTLs */
long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
4105
long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
4106
int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
4107
int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
4108
int hl_wait_ioctl(struct hl_fpriv *hpriv, void *data);
4109
int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
4110

4111
#endif /* HABANALABSP_H_ */