habanalabs.h 76.0 KB
Newer Older
O
Oded Gabbay 已提交
1 2 3 4 5 6 7 8 9 10
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright 2016-2019 HabanaLabs, Ltd.
 * 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 <uapi/misc/habanalabs.h>
14

O
Oded Gabbay 已提交
15
#include <linux/cdev.h>
16
#include <linux/iopoll.h>
17
#include <linux/irqreturn.h>
18 19
#include <linux/dma-direction.h>
#include <linux/scatterlist.h>
20
#include <linux/hashtable.h>
21
#include <linux/bitfield.h>
O
Oded Gabbay 已提交
22 23 24

#define HL_NAME				"habanalabs"

O
Oded Gabbay 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
/* Use upper bits of mmap offset to store habana driver specific information.
 * bits[63:62] - Encode mmap type
 * 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
 */
#define HL_MMAP_TYPE_SHIFT		(62 - PAGE_SHIFT)
#define HL_MMAP_TYPE_MASK		(0x3ull << HL_MMAP_TYPE_SHIFT)
#define HL_MMAP_TYPE_CB			(0x2ull << HL_MMAP_TYPE_SHIFT)

#define HL_MMAP_OFFSET_VALUE_MASK	(0x3FFFFFFFFFFFull >> PAGE_SHIFT)
#define HL_MMAP_OFFSET_VALUE_GET(off)	(off & HL_MMAP_OFFSET_VALUE_MASK)
38

39
#define HL_PENDING_RESET_PER_SEC	30
40

41 42
#define HL_HARD_RESET_MAX_TIMEOUT	120

43 44
#define HL_DEVICE_TIMEOUT_USEC		1000000 /* 1 s */

45 46
#define HL_HEARTBEAT_PER_USEC		5000000 /* 5 s */

47 48
#define HL_PLL_LOW_JOB_FREQ_USEC	5000000 /* 5 s */

49 50
#define HL_CPUCP_INFO_TIMEOUT_USEC	10000000 /* 10s */
#define HL_CPUCP_EEPROM_TIMEOUT_USEC	10000000 /* 10s */
51

52 53
#define HL_PCI_ELBI_TIMEOUT_MSEC	10 /* 10ms */

O
Oded Gabbay 已提交
54 55
#define HL_SIM_MAX_TIMEOUT_US		10000000 /* 10s */

56 57
#define HL_IDLE_BUSY_TS_ARR_SIZE	4096

58 59 60 61 62 63
/* Memory */
#define MEM_HASH_TABLE_BITS		7 /* 1 << 7 buckets */

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

64 65 66 67
/*
 * HL_RSVD_SOBS 'sync stream' reserved sync objects per QMAN stream
 * HL_RSVD_MONS 'sync stream' reserved monitors per QMAN stream
 */
68 69
#define HL_RSVD_SOBS			2
#define HL_RSVD_MONS			1
70

71 72 73 74 75
/*
 * HL_COLLECTIVE_RSVD_MSTR_MONS 'collective' reserved monitors per QMAN stream
 */
#define HL_COLLECTIVE_RSVD_MSTR_MONS	2

76 77
#define HL_MAX_SOB_VAL			(1 << 15)

78 79 80
#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))

O
Ofir Bitton 已提交
81 82
#define HL_PCI_NUM_BARS			6

83 84
#define HL_MAX_DCORES			4

85 86
#define HL_MAX_SOBS_PER_MONITOR	8

87 88 89 90
/**
 * 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
91 92
 * @size: offset in command buffer
 * @sob_base: SOB base to use in this wait CB
93 94
 * @sob_val: SOB value to wait for
 * @mon_id: monitor to use in this wait CB
95
 * @sob_mask: each bit represents a SOB offset from sob_base to be used
96 97 98 99
 */
struct hl_gen_wait_properties {
	void	*data;
	u32	q_idx;
100 101
	u32	size;
	u16	sob_base;
102 103
	u16	sob_val;
	u16	mon_id;
104
	u8	sob_mask;
105 106
};

107 108
/**
 * struct pgt_info - MMU hop page info.
109 110 111
 * @node: hash linked-list node for the pgts shadow hash of pgts.
 * @phys_addr: physical address of the pgt.
 * @shadow_addr: shadow hop in the host.
112 113 114 115 116 117 118 119 120
 * @ctx: pointer to the owner ctx.
 * @num_of_ptes: indicates how many ptes are used in the pgt.
 *
 * The MMU page tables hierarchy is placed on the DRAM. When a new level (hop)
 * is needed during mapping, a new page is allocated and this structure holds
 * its essential information. During unmapping, if no valid PTEs remained in the
 * page, it is freed with its pgt_info structure.
 */
struct pgt_info {
121 122 123 124 125
	struct hlist_node	node;
	u64			phys_addr;
	u64			shadow_addr;
	struct hl_ctx		*ctx;
	int			num_of_ptes;
126 127
};

O
Oded Gabbay 已提交
128
struct hl_device;
129
struct hl_fpriv;
O
Oded Gabbay 已提交
130

O
Ofir Bitton 已提交
131 132 133 134 135 136 137 138 139 140
/**
 * 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
};

141 142 143 144 145 146 147 148 149 150
/**
 * enum hl_fw_component - F/W components to read version through registers.
 * @FW_COMP_UBOOT: u-boot.
 * @FW_COMP_PREBOOT: preboot.
 */
enum hl_fw_component {
	FW_COMP_UBOOT,
	FW_COMP_PREBOOT
};

151 152 153 154 155 156 157 158 159 160 161 162
/**
 * enum hl_fw_types - F/W types to load
 * @FW_TYPE_LINUX: Linux image for device CPU
 * @FW_TYPE_BOOT_CPU: Boot image for device CPU
 * @FW_TYPE_ALL_TYPES: Mask for all types
 */
enum hl_fw_types {
	FW_TYPE_LINUX = 0x1,
	FW_TYPE_BOOT_CPU = 0x2,
	FW_TYPE_ALL_TYPES = (FW_TYPE_LINUX | FW_TYPE_BOOT_CPU)
};

O
Oded Gabbay 已提交
163 164 165 166 167 168 169 170
/**
 * 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.
T
Tomer Tayar 已提交
171 172
 * @QUEUE_TYPE_HW: queue of DMA and compute engines jobs, for which completion
 *                 notifications are sent by H/W.
O
Oded Gabbay 已提交
173 174 175 176 177
 */
enum hl_queue_type {
	QUEUE_TYPE_NA,
	QUEUE_TYPE_EXT,
	QUEUE_TYPE_INT,
T
Tomer Tayar 已提交
178 179
	QUEUE_TYPE_CPU,
	QUEUE_TYPE_HW
O
Oded Gabbay 已提交
180 181
};

182 183 184
enum hl_cs_type {
	CS_TYPE_DEFAULT,
	CS_TYPE_SIGNAL,
185 186
	CS_TYPE_WAIT,
	CS_TYPE_COLLECTIVE_WAIT
187 188
};

O
Ofir Bitton 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/*
 * 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;
};

215 216 217 218 219 220 221 222 223 224 225
/*
 * 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
};

226 227 228 229 230 231 232 233 234 235 236 237 238 239
/*
 * 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.
 * @q_idx: the H/W queue that uses this SOB.
 */
struct hl_hw_sob {
	struct hl_device	*hdev;
	struct kref		kref;
	u32			sob_id;
	u32			q_idx;
};

240 241 242 243 244 245
enum hl_collective_mode {
	HL_COLLECTIVE_NOT_SUPPORTED = 0x0,
	HL_COLLECTIVE_MASTER = 0x1,
	HL_COLLECTIVE_SLAVE = 0x2
};

O
Oded Gabbay 已提交
246 247 248
/**
 * struct hw_queue_properties - queue information.
 * @type: queue type.
249 250 251 252
 * @queue_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.
253
 * @collective_mode: collective mode of current queue
254 255
 * @driver_only: true if only the driver is allowed to send a job to this queue,
 *               false otherwise.
256
 * @supports_sync_stream: True if queue supports sync stream
O
Oded Gabbay 已提交
257 258 259
 */
struct hw_queue_properties {
	enum hl_queue_type	type;
260
	enum queue_cb_alloc_flags cb_alloc_flags;
261
	enum hl_collective_mode	collective_mode;
262
	u8			driver_only;
263
	u8			supports_sync_stream;
O
Oded Gabbay 已提交
264
};
O
Oded Gabbay 已提交
265

266 267 268
/**
 * enum vm_type_t - virtual memory mapping request information.
 * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
269
 * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
270 271
 */
enum vm_type_t {
272 273
	VM_TYPE_USERPTR = 0x1,
	VM_TYPE_PHYS_PACK = 0x2
274 275
};

276 277 278 279 280 281 282 283 284 285 286 287
/**
 * 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
};

288 289
/**
 * struct hl_mmu_properties - ASIC specific MMU address translation properties.
290 291
 * @start_addr: virtual start address of the memory region.
 * @end_addr: virtual end address of the memory region.
292 293 294 295 296
 * @hop0_shift: shift of hop 0 mask.
 * @hop1_shift: shift of hop 1 mask.
 * @hop2_shift: shift of hop 2 mask.
 * @hop3_shift: shift of hop 3 mask.
 * @hop4_shift: shift of hop 4 mask.
297
 * @hop5_shift: shift of hop 5 mask.
298 299 300 301 302
 * @hop0_mask: mask to get the PTE address in hop 0.
 * @hop1_mask: mask to get the PTE address in hop 1.
 * @hop2_mask: mask to get the PTE address in hop 2.
 * @hop3_mask: mask to get the PTE address in hop 3.
 * @hop4_mask: mask to get the PTE address in hop 4.
303
 * @hop5_mask: mask to get the PTE address in hop 5.
304
 * @page_size: default page size used to allocate memory.
305
 * @num_hops: The amount of hops supported by the translation table.
306 307
 */
struct hl_mmu_properties {
308 309
	u64	start_addr;
	u64	end_addr;
310 311 312 313 314
	u64	hop0_shift;
	u64	hop1_shift;
	u64	hop2_shift;
	u64	hop3_shift;
	u64	hop4_shift;
315
	u64	hop5_shift;
316 317 318 319 320
	u64	hop0_mask;
	u64	hop1_mask;
	u64	hop2_mask;
	u64	hop3_mask;
	u64	hop4_mask;
321
	u64	hop5_mask;
322
	u32	page_size;
323
	u32	num_hops;
324 325
};

O
Oded Gabbay 已提交
326 327
/**
 * struct asic_fixed_properties - ASIC specific immutable properties.
O
Oded Gabbay 已提交
328
 * @hw_queues_props: H/W queues properties.
329
 * @cpucp_info: received various information from CPU-CP regarding the H/W, e.g.
330
 *		available sensors.
331 332
 * @uboot_ver: F/W U-boot version.
 * @preboot_ver: F/W Preboot version.
333 334
 * @dmmu: DRAM MMU address translation properties.
 * @pmmu: PCI (host) MMU address translation properties.
335 336
 * @pmmu_huge: PCI (host) MMU address translation properties for memory
 *              allocated with huge pages.
O
Oded Gabbay 已提交
337 338 339 340 341 342 343 344
 * @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.
345
 * @max_power_default: max power of the device after reset
346 347
 * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
 *                                      fault.
348 349
 * @pcie_dbi_base_address: Base address of the PCIE_DBI block.
 * @pcie_aux_dbi_reg_addr: Address of the PCIE_AUX DBI register.
350
 * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
351
 * @mmu_dram_default_page_addr: DRAM default page physical address.
352 353 354 355
 * @cb_va_start_addr: virtual start address of command buffers which are mapped
 *                    to the device's MMU.
 * @cb_va_end_addr: virtual end address of command buffers which are mapped to
 *                  the device's MMU.
356 357 358 359 360
 * @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.
O
Oded Gabbay 已提交
361 362 363
 * @cfg_size: configuration space size on SRAM.
 * @sram_size: total size of SRAM.
 * @max_asid: maximum number of open contexts (ASIDs).
364
 * @num_of_events: number of possible internal H/W IRQs.
365 366 367 368
 * @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.
369
 * @psoc_timestamp_frequency: frequency of the psoc timestamp clock.
O
Oded Gabbay 已提交
370
 * @high_pll: high PLL frequency used by the device.
371 372
 * @cb_pool_cb_cnt: number of CBs in the CB pool.
 * @cb_pool_cb_size: size of each CB in the CB pool.
373 374
 * @max_pending_cs: maximum of concurrent pending command submissions
 * @max_queues: maximum amount of queues in the system
375 376
 * @collective_first_sob: first sync object available for collective use
 * @collective_first_mon: first monitor available for collective use
377 378
 * @sync_stream_first_sob: first sync object available for sync stream use
 * @sync_stream_first_mon: first monitor available for sync stream use
379 380
 * @first_available_user_sob: first sob available for the user
 * @first_available_user_mon: first monitor available for the user
381
 * @tpc_enabled_mask: which TPCs are enabled.
382
 * @completion_queues_count: number of completion queues.
383 384
 * @fw_security_disabled: true if security measures are disabled in firmware,
 *                        false otherwise
O
Oded Gabbay 已提交
385 386
 */
struct asic_fixed_properties {
387
	struct hw_queue_properties	*hw_queues_props;
388
	struct cpucp_info		cpucp_info;
389 390 391 392
	char				uboot_ver[VERSION_MAX_LEN];
	char				preboot_ver[VERSION_MAX_LEN];
	struct hl_mmu_properties	dmmu;
	struct hl_mmu_properties	pmmu;
393
	struct hl_mmu_properties	pmmu_huge;
394 395 396 397 398 399 400 401 402 403 404 405 406 407
	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;
	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;
408 409
	u64				cb_va_start_addr;
	u64				cb_va_end_addr;
410 411 412 413 414 415 416 417 418 419 420 421 422
	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;
423
	u32				psoc_timestamp_frequency;
424 425 426
	u32				high_pll;
	u32				cb_pool_cb_cnt;
	u32				cb_pool_cb_size;
427
	u32				max_pending_cs;
428
	u32				max_queues;
429 430
	u16				collective_first_sob;
	u16				collective_first_mon;
431 432
	u16				sync_stream_first_sob;
	u16				sync_stream_first_mon;
433 434
	u16				first_available_user_sob[HL_MAX_DCORES];
	u16				first_available_user_mon[HL_MAX_DCORES];
435 436
	u8				tpc_enabled_mask;
	u8				completion_queues_count;
437
	u8				fw_security_disabled;
O
Oded Gabbay 已提交
438 439
};

440 441 442 443 444 445 446 447 448 449 450 451 452
/**
 * struct hl_fence - software synchronization primitive
 * @completion: fence is implemented using completion
 * @refcount: refcount for this fence
 * @error: mark this fence with error
 *
 */
struct hl_fence {
	struct completion	completion;
	struct kref		refcount;
	int			error;
};

453
/**
454
 * struct hl_cs_compl - command submission completion object.
455
 * @base_fence: hl fence object.
456 457
 * @lock: spinlock to protect fence.
 * @hdev: habanalabs device structure.
458
 * @hw_sob: the H/W SOB used in this signal/wait CS.
459
 * @cs_seq: command submission sequence number.
460 461
 * @type: type of the CS - signal/wait.
 * @sob_val: the SOB value that is used in this signal/wait CS.
462
 * @sob_group: the SOB group that is used in this collective wait CS.
463
 */
464
struct hl_cs_compl {
465
	struct hl_fence		base_fence;
466 467
	spinlock_t		lock;
	struct hl_device	*hdev;
468
	struct hl_hw_sob	*hw_sob;
469
	u64			cs_seq;
470 471
	enum hl_cs_type		type;
	u16			sob_val;
472
	u16			sob_group;
473
};
O
Oded Gabbay 已提交
474

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
/*
 * Command Buffers
 */

/**
 * struct hl_cb_mgr - describes a Command Buffer Manager.
 * @cb_lock: protects cb_handles.
 * @cb_handles: an idr to hold all command buffer handles.
 */
struct hl_cb_mgr {
	spinlock_t		cb_lock;
	struct idr		cb_handles; /* protected by cb_lock */
};

/**
 * struct hl_cb - describes a Command Buffer.
 * @refcount: reference counter for usage of the CB.
 * @hdev: pointer to device this CB belongs to.
493
 * @ctx: pointer to the CB owner's context.
494
 * @lock: spinlock to protect mmap/cs flows.
O
Oded Gabbay 已提交
495
 * @debugfs_list: node in debugfs list of command buffers.
496
 * @pool_list: node in pool list of command buffers.
497 498
 * @va_block_list: list of virtual addresses blocks of the CB if it is mapped to
 *                 the device's MMU.
499
 * @id: the CB's ID.
500 501 502 503
 * @kernel_address: Holds the CB's kernel virtual address.
 * @bus_address: Holds the CB's DMA address.
 * @mmap_size: Holds the CB's size that was mmaped.
 * @size: holds the CB's size.
504
 * @cs_cnt: holds number of CS that this CB participates in.
505 506
 * @mmap: true if the CB is currently mmaped to user.
 * @is_pool: true if CB was acquired from the pool, false otherwise.
507
 * @is_internal: internaly allocated
508
 * @is_mmu_mapped: true if the CB is mapped to the device's MMU.
509 510 511 512
 */
struct hl_cb {
	struct kref		refcount;
	struct hl_device	*hdev;
513
	struct hl_ctx		*ctx;
514
	spinlock_t		lock;
O
Oded Gabbay 已提交
515
	struct list_head	debugfs_list;
516
	struct list_head	pool_list;
517
	struct list_head	va_block_list;
518
	u64			id;
519
	void			*kernel_address;
520 521 522
	dma_addr_t		bus_address;
	u32			mmap_size;
	u32			size;
523
	u32			cs_cnt;
524 525
	u8			mmap;
	u8			is_pool;
526
	u8			is_internal;
527
	u8			is_mmu_mapped;
528 529 530
};


O
Oded Gabbay 已提交
531 532 533 534
/*
 * QUEUES
 */

535
struct hl_cs;
O
Oded Gabbay 已提交
536 537
struct hl_cs_job;

O
Ofir Bitton 已提交
538 539
/* Queue length of external and HW queues */
#define HL_QUEUE_LENGTH			4096
O
Oded Gabbay 已提交
540 541
#define HL_QUEUE_SIZE_IN_BYTES		(HL_QUEUE_LENGTH * HL_BD_SIZE)

542 543 544 545
#if (HL_MAX_JOBS_PER_CS > HL_QUEUE_LENGTH)
#error "HL_QUEUE_LENGTH must be greater than HL_MAX_JOBS_PER_CS"
#endif

O
Ofir Bitton 已提交
546
/* HL_CQ_LENGTH is in units of struct hl_cq_entry */
O
Oded Gabbay 已提交
547 548 549
#define HL_CQ_LENGTH			HL_QUEUE_LENGTH
#define HL_CQ_SIZE_IN_BYTES		(HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)

O
Ofir Bitton 已提交
550
/* Must be power of 2 */
551 552
#define HL_EQ_LENGTH			64
#define HL_EQ_SIZE_IN_BYTES		(HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
O
Oded Gabbay 已提交
553

O
Oded Gabbay 已提交
554
/* Host <-> CPU-CP shared memory size */
555
#define HL_CPU_ACCESSIBLE_MEM_SIZE	SZ_2M
O
Oded Gabbay 已提交
556 557

/**
558 559
 * struct hl_sync_stream_properties -
 *     describes a H/W queue sync stream properties
560
 * @hw_sob: array of the used H/W SOBs by this H/W queue.
561 562 563
 * @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.
564 565 566 567 568 569
 * @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.
570 571 572 573
 * @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 {
574 575 576 577 578 579 580 581
	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;
582 583 584 585
};

/**
 * struct hl_hw_queue - describes a H/W transport queue.
O
Oded Gabbay 已提交
586
 * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
587
 * @sync_stream_prop: sync stream queue properties
O
Oded Gabbay 已提交
588
 * @queue_type: type of queue.
589
 * @collective_mode: collective mode of current queue
O
Oded Gabbay 已提交
590 591 592 593 594
 * @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.
595 596
 * @cq_id: the id for the corresponding CQ for this H/W queue.
 * @msi_vec: the IRQ number of the H/W queue.
O
Oded Gabbay 已提交
597 598
 * @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
599
 *         exist).
600
 * @supports_sync_stream: True if queue supports sync stream
O
Oded Gabbay 已提交
601 602
 */
struct hl_hw_queue {
603 604 605
	struct hl_cs_job			**shadow_queue;
	struct hl_sync_stream_properties	sync_stream_prop;
	enum hl_queue_type			queue_type;
606
	enum hl_collective_mode			collective_mode;
607 608 609 610 611 612 613 614 615 616
	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;
O
Oded Gabbay 已提交
617 618 619 620 621 622 623
};

/**
 * 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
624
 * @cq_idx: completion queue index in array
O
Oded Gabbay 已提交
625 626 627 628 629 630 631
 * @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;
632
	void			*kernel_address;
O
Oded Gabbay 已提交
633
	dma_addr_t		bus_address;
634
	u32			cq_idx;
O
Oded Gabbay 已提交
635 636 637 638 639
	u32			hw_queue_id;
	u32			ci;
	u32			pi;
	atomic_t		free_slots_cnt;
};
640

641 642 643 644 645 646 647 648 649
/**
 * 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
 */
struct hl_eq {
	struct hl_device	*hdev;
650
	void			*kernel_address;
651 652 653 654
	dma_addr_t		bus_address;
	u32			ci;
};

655

O
Oded Gabbay 已提交
656 657 658 659 660 661 662
/*
 * ASICs
 */

/**
 * enum hl_asic_type - supported ASIC types.
 * @ASIC_INVALID: Invalid ASIC type.
663
 * @ASIC_GOYA: Goya device.
664
 * @ASIC_GAUDI: Gaudi device.
O
Oded Gabbay 已提交
665 666
 */
enum hl_asic_type {
667
	ASIC_INVALID,
668 669
	ASIC_GOYA,
	ASIC_GAUDI
O
Oded Gabbay 已提交
670 671
};

672 673
struct hl_cs_parser;

674 675
/**
 * enum hl_pm_mng_profile - power management profile.
676
 * @PM_AUTO: internal clock is set by the Linux driver.
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
 * @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
};

698 699 700 701 702 703 704 705 706
#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,
};

O
Oded Gabbay 已提交
707 708 709 710 711
/**
 * 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.
712 713
 * @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.
O
Oded Gabbay 已提交
714 715
 * @sw_init: sets up driver state, does not configure H/W.
 * @sw_fini: tears down driver state, does not configure H/W.
716 717
 * @hw_init: sets up the H/W state.
 * @hw_fini: tears down the H/W state.
718 719 720
 * @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.
O
Oded Gabbay 已提交
721 722
 * @suspend: handles IP specific H/W or SW changes for suspend.
 * @resume: handles IP specific H/W or SW changes for resume.
723
 * @cb_mmap: maps a CB.
O
Oded Gabbay 已提交
724
 * @ring_doorbell: increment PI on a given QMAN.
725 726 727 728 729
 * @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.
730 731 732 733 734 735 736 737
 * @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).
O
Oded Gabbay 已提交
738 739
 * @get_int_queue_base: get the internal queue base address.
 * @test_queues: run simple test on all queues for sanity check.
740 741 742
 * @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.
O
Oded Gabbay 已提交
743 744
 * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
 * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
745 746 747 748 749
 * @hl_dma_unmap_sg: DMA unmap scatter-gather list.
 * @cs_parser: parse Command Submission.
 * @asic_dma_map_sg: DMA map scatter-gather list.
 * @get_dma_desc_list_size: get number of LIN_DMA packets required for CB.
 * @add_end_of_cb_packets: Add packets to the end of CB, if device requires it.
750
 * @update_eq_ci: update event queue CI.
751 752
 * @context_switch: called upon ASID context switch.
 * @restore_phase_topology: clear all SOBs amd MONs.
O
Oded Gabbay 已提交
753 754
 * @debugfs_read32: debug interface for reading u32 from DRAM/SRAM.
 * @debugfs_write32: debug interface for writing u32 to DRAM/SRAM.
755
 * @add_device_attr: add ASIC specific device attributes.
O
Oded Gabbay 已提交
756
 * @handle_eqe: handle event queue entry (IRQ) from CPU-CP.
757
 * @set_pll_profile: change PLL profile (manual/automatic).
758
 * @get_events_stat: retrieve event queue entries histogram.
759 760
 * @read_pte: read MMU page table entry from DRAM.
 * @write_pte: write MMU page table entry to DRAM.
761 762
 * @mmu_invalidate_cache: flush MMU STLB host/DRAM cache, either with soft
 *                        (L1 only) or hard (L0 & L1) flush.
763 764
 * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with
 *                              ASID-VA-size mask.
O
Oded Gabbay 已提交
765
 * @send_heartbeat: send is-alive packet to CPU-CP and verify response.
766 767 768
 * @set_clock_gating: enable/disable clock gating per engine according to
 *                    clock gating mask in hdev
 * @disable_clock_gating: disable clock gating completely
769
 * @debug_coresight: perform certain actions on Coresight for debugging.
770
 * @is_device_idle: return true if device is idle, false otherwise.
771
 * @soft_reset_late_init: perform certain actions needed after soft reset.
O
Oded Gabbay 已提交
772 773
 * @hw_queues_lock: acquire H/W queues lock.
 * @hw_queues_unlock: release H/W queues lock.
O
Oded Gabbay 已提交
774
 * @get_pci_id: retrieve PCI ID.
775
 * @get_eeprom_data: retrieve EEPROM data from F/W.
776 777 778 779 780
 * @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
781
 * @get_hw_state: retrieve the H/W state
782 783
 * @pci_bars_map: Map PCI BARs.
 * @init_iatu: Initialize the iATU unit inside the PCI controller.
784 785
 * @rreg: Read a register. Needed for simulator support.
 * @wreg: Write a register. Needed for simulator support.
786
 * @halt_coresight: stop the ETF and ETR traces.
787
 * @ctx_init: context dependent initialization.
788
 * @ctx_fini: context dependent cleanup.
789
 * @get_clk_rate: Retrieve the ASIC current and maximum clock rate in MHz
790
 * @get_queue_id_for_cq: Get the H/W queue id related to the given CQ index.
791 792 793
 * @read_device_fw_version: read the device's firmware versions that are
 *                          contained in registers
 * @load_firmware_to_device: load the firmware to the device's memory
794
 * @load_boot_fit_to_device: load boot fit to device's memory
795 796 797 798 799
 * @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.
800
 * @reset_sob_group: Reset SOB group
801 802
 * @set_dma_mask_from_fw: set the DMA mask in the driver according to the
 *                        firmware configuration
803
 * @get_device_time: Get the device time.
804 805 806
 * @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
O
Oded Gabbay 已提交
807 808 809 810
 */
struct hl_asic_funcs {
	int (*early_init)(struct hl_device *hdev);
	int (*early_fini)(struct hl_device *hdev);
811 812
	int (*late_init)(struct hl_device *hdev);
	void (*late_fini)(struct hl_device *hdev);
O
Oded Gabbay 已提交
813 814
	int (*sw_init)(struct hl_device *hdev);
	int (*sw_fini)(struct hl_device *hdev);
815 816
	int (*hw_init)(struct hl_device *hdev);
	void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
817
	void (*halt_engines)(struct hl_device *hdev, bool hard_reset);
O
Oded Gabbay 已提交
818 819
	int (*suspend)(struct hl_device *hdev);
	int (*resume)(struct hl_device *hdev);
820
	int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
821
			void *cpu_addr, dma_addr_t dma_addr, size_t size);
O
Oded Gabbay 已提交
822
	void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
823 824
	void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
			struct hl_bd *bd);
825
	void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
O
Oded Gabbay 已提交
826
					dma_addr_t *dma_handle, gfp_t flag);
827
	void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
O
Oded Gabbay 已提交
828
					void *cpu_addr, dma_addr_t dma_handle);
O
Oded Gabbay 已提交
829 830 831
	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);
832
	void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
O
Oded Gabbay 已提交
833
				gfp_t mem_flags, dma_addr_t *dma_handle);
834
	void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
O
Oded Gabbay 已提交
835 836 837 838 839
				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);
840
	void (*hl_dma_unmap_sg)(struct hl_device *hdev,
841
				struct scatterlist *sgl, int nents,
842 843 844
				enum dma_data_direction dir);
	int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
	int (*asic_dma_map_sg)(struct hl_device *hdev,
845
				struct scatterlist *sgl, int nents,
846 847 848
				enum dma_data_direction dir);
	u32 (*get_dma_desc_list_size)(struct hl_device *hdev,
					struct sg_table *sgt);
849
	void (*add_end_of_cb_packets)(struct hl_device *hdev,
850
					void *kernel_address, u32 len,
851 852
					u64 cq_addr, u32 cq_val, u32 msix_num,
					bool eb);
853
	void (*update_eq_ci)(struct hl_device *hdev, u32 val);
854 855
	int (*context_switch)(struct hl_device *hdev, u32 asid);
	void (*restore_phase_topology)(struct hl_device *hdev);
O
Oded Gabbay 已提交
856 857
	int (*debugfs_read32)(struct hl_device *hdev, u64 addr, u32 *val);
	int (*debugfs_write32)(struct hl_device *hdev, u64 addr, u32 val);
858 859
	int (*debugfs_read64)(struct hl_device *hdev, u64 addr, u64 *val);
	int (*debugfs_write64)(struct hl_device *hdev, u64 addr, u64 val);
860 861
	void (*add_device_attr)(struct hl_device *hdev,
				struct attribute_group *dev_attr_grp);
862 863
	void (*handle_eqe)(struct hl_device *hdev,
				struct hl_eq_entry *eq_entry);
864 865
	void (*set_pll_profile)(struct hl_device *hdev,
			enum hl_pll_frequency freq);
866 867
	void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
				u32 *size);
868 869
	u64 (*read_pte)(struct hl_device *hdev, u64 addr);
	void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
870
	int (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard,
871
					u32 flags);
872
	int (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
873
			u32 asid, u64 va, u64 size);
874
	int (*send_heartbeat)(struct hl_device *hdev);
875
	void (*set_clock_gating)(struct hl_device *hdev);
876
	void (*disable_clock_gating)(struct hl_device *hdev);
877
	int (*debug_coresight)(struct hl_device *hdev, void *data);
878
	bool (*is_device_idle)(struct hl_device *hdev, u64 *mask,
879
				struct seq_file *s);
880
	int (*soft_reset_late_init)(struct hl_device *hdev);
O
Oded Gabbay 已提交
881 882
	void (*hw_queues_lock)(struct hl_device *hdev);
	void (*hw_queues_unlock)(struct hl_device *hdev);
O
Oded Gabbay 已提交
883
	u32 (*get_pci_id)(struct hl_device *hdev);
884 885
	int (*get_eeprom_data)(struct hl_device *hdev, void *data,
				size_t max_size);
O
Oded Gabbay 已提交
886 887
	int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
				u16 len, u32 timeout, long *result);
888
	enum hl_device_hw_state (*get_hw_state)(struct hl_device *hdev);
889 890
	int (*pci_bars_map)(struct hl_device *hdev);
	int (*init_iatu)(struct hl_device *hdev);
891 892
	u32 (*rreg)(struct hl_device *hdev, u32 reg);
	void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
893
	void (*halt_coresight)(struct hl_device *hdev);
894
	int (*ctx_init)(struct hl_ctx *ctx);
895
	void (*ctx_fini)(struct hl_ctx *ctx);
896
	int (*get_clk_rate)(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk);
897
	u32 (*get_queue_id_for_cq)(struct hl_device *hdev, u32 cq_idx);
898 899 900
	void (*read_device_fw_version)(struct hl_device *hdev,
					enum hl_fw_component fwc);
	int (*load_firmware_to_device)(struct hl_device *hdev);
901
	int (*load_boot_fit_to_device)(struct hl_device *hdev);
902 903
	u32 (*get_signal_cb_size)(struct hl_device *hdev);
	u32 (*get_wait_cb_size)(struct hl_device *hdev);
904 905 906
	u32 (*gen_signal_cb)(struct hl_device *hdev, void *data, u16 sob_id,
			u32 size);
	u32 (*gen_wait_cb)(struct hl_device *hdev,
907
			struct hl_gen_wait_properties *prop);
908
	void (*reset_sob)(struct hl_device *hdev, void *data);
909
	void (*reset_sob_group)(struct hl_device *hdev, u16 sob_group);
910
	void (*set_dma_mask_from_fw)(struct hl_device *hdev);
911
	u64 (*get_device_time)(struct hl_device *hdev);
912 913 914 915
	void (*collective_wait_init_cs)(struct hl_cs *cs);
	int (*collective_wait_create_jobs)(struct hl_device *hdev,
			struct hl_ctx *ctx, struct hl_cs *cs, u32 wait_queue_id,
			u32 collective_engine_id);
O
Oded Gabbay 已提交
916
};
O
Oded Gabbay 已提交
917

918 919 920 921 922 923 924

/*
 * CONTEXTS
 */

#define HL_KERNEL_ASID_ID	0

925 926 927 928 929 930 931 932 933 934 935 936 937 938
/**
 * 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.
 */
struct hl_va_range {
	struct mutex		lock;
	struct list_head	list;
	u64			start_addr;
	u64			end_addr;
};

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
/**
 * 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
 */
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;
};

955 956
/**
 * struct hl_ctx - user/kernel context.
957 958
 * @mem_hash: holds mapping from virtual address to virtual memory area
 *		descriptor (hl_vm_phys_pg_list or hl_userptr).
959
 * @mmu_shadow_hash: holds a mapping from shadow address to pgt_info structure.
960
 * @hpriv: pointer to the private (Kernel Driver) data of the process (fd).
961 962 963
 * @hdev: pointer to the device structure.
 * @refcount: reference counter for the context. Context is released only when
 *		this hits 0l. It is incremented on CS and CS_WAIT.
964
 * @cs_pending: array of hl fence objects representing pending CS.
965
 * @host_va_range: holds available virtual addresses for host mappings.
966 967
 * @host_huge_va_range: holds available virtual addresses for host mappings
 *                      with huge pages.
968 969
 * @dram_va_range: holds available virtual addresses for DRAM mappings.
 * @mem_hash_lock: protects the mem_hash.
970 971
 * @mmu_lock: protects the MMU page tables. Any change to the PGT, modifying the
 *            MMU hash or walking the PGT requires talking this lock.
O
Oded Gabbay 已提交
972
 * @debugfs_list: node in debugfs list of contexts.
973
 * @cs_counters: context command submission counters.
974 975
 * @cb_va_pool: device VA pool for command buffers which are mapped to the
 *              device's MMU.
976 977 978
 * @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.
979 980
 * @dram_default_hops: array that holds all hops addresses needed for default
 *                     DRAM mapping.
981
 * @cs_lock: spinlock to protect cs_sequence.
982
 * @dram_phys_mem: amount of used physical DRAM memory by this context.
983 984 985 986 987 988 989
 * @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.
990
 * @asid: context's unique address space ID in the device's MMU.
991
 * @handle: context's opaque handle for user
992 993
 */
struct hl_ctx {
994
	DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
995
	DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	struct hl_fpriv			*hpriv;
	struct hl_device		*hdev;
	struct kref			refcount;
	struct hl_fence			**cs_pending;
	struct hl_va_range		*host_va_range;
	struct hl_va_range		*host_huge_va_range;
	struct hl_va_range		*dram_va_range;
	struct mutex			mem_hash_lock;
	struct mutex			mmu_lock;
	struct list_head		debugfs_list;
	struct hl_cs_counters_atomic	cs_counters;
	struct gen_pool			*cb_va_pool;
	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;
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
};

/**
 * struct hl_ctx_mgr - for handling multiple contexts.
 * @ctx_lock: protects ctx_handles.
 * @ctx_handles: idr to hold all ctx handles.
 */
struct hl_ctx_mgr {
	struct mutex		ctx_lock;
	struct idr		ctx_handles;
};


1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

/*
 * 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.
 * @vec: pointer to the frame vector.
 * @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.
1042
 * @addr: user-space virtual address of the start of the memory area.
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
 * @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 {
	enum vm_type_t		vm_type; /* must be first */
	struct list_head	job_node;
	struct frame_vector	*vec;
	struct sg_table		*sgt;
	enum dma_data_direction dir;
	struct list_head	debugfs_list;
	u64			addr;
	u32			size;
	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.
1066 1067
 * @signal_fence: pointer to the fence object of the signal CS (used by wait
 *                CS only).
1068
 * @finish_work: workqueue object to run when CS is completed by H/W.
1069 1070
 * @work_tdr: delayed work node for TDR.
 * @mirror_node : node in device mirror list of command submissions.
O
Oded Gabbay 已提交
1071
 * @debugfs_list: node in debugfs list of command submissions.
1072
 * @sequence: the sequence number of this CS.
1073
 * @type: CS_TYPE_*.
1074 1075 1076 1077 1078 1079 1080 1081
 * @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.
 */
struct hl_cs {
1082
	u16			*jobs_in_queue_cnt;
1083 1084 1085 1086
	struct hl_ctx		*ctx;
	struct list_head	job_list;
	spinlock_t		job_lock;
	struct kref		refcount;
1087 1088
	struct hl_fence		*fence;
	struct hl_fence		*signal_fence;
1089
	struct work_struct	finish_work;
1090 1091
	struct delayed_work	work_tdr;
	struct list_head	mirror_node;
O
Oded Gabbay 已提交
1092
	struct list_head	debugfs_list;
1093
	u64			sequence;
1094
	enum hl_cs_type		type;
1095 1096 1097 1098 1099 1100 1101
	u8			submitted;
	u8			completed;
	u8			timedout;
	u8			tdr_active;
	u8			aborted;
};

O
Oded Gabbay 已提交
1102 1103
/**
 * struct hl_cs_job - command submission job.
1104 1105 1106 1107 1108
 * @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.
O
Oded Gabbay 已提交
1109
 * @finish_work: workqueue object to run when job is completed.
1110 1111
 * @userptr_list: linked-list of userptr mappings that belong to this job and
 *			wait for completion.
O
Oded Gabbay 已提交
1112
 * @debugfs_list: node in debugfs list of command submission jobs.
T
Tomer Tayar 已提交
1113
 * @queue_type: the type of the H/W queue this job is submitted to.
O
Oded Gabbay 已提交
1114
 * @id: the id of this job inside a CS.
1115 1116 1117
 * @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.
T
Tomer Tayar 已提交
1118 1119 1120
 * @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).
1121 1122 1123 1124 1125 1126
 * @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.
O
Oded Gabbay 已提交
1127 1128
 */
struct hl_cs_job {
1129 1130 1131 1132
	struct list_head	cs_node;
	struct hl_cs		*cs;
	struct hl_cb		*user_cb;
	struct hl_cb		*patched_cb;
O
Oded Gabbay 已提交
1133
	struct work_struct	finish_work;
1134
	struct list_head	userptr_list;
O
Oded Gabbay 已提交
1135
	struct list_head	debugfs_list;
T
Tomer Tayar 已提交
1136
	enum hl_queue_type	queue_type;
O
Oded Gabbay 已提交
1137
	u32			id;
1138 1139 1140
	u32			hw_queue_id;
	u32			user_cb_size;
	u32			job_cb_size;
T
Tomer Tayar 已提交
1141
	u8			is_kernel_allocated_cb;
1142
	u8			contains_dma_pkt;
1143 1144 1145
};

/**
T
Tomer Tayar 已提交
1146
 * struct hl_cs_parser - command submission parser properties.
1147 1148 1149 1150 1151 1152
 * @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.
T
Tomer Tayar 已提交
1153
 * @queue_type: the type of the H/W queue this job is submitted to.
1154 1155 1156 1157 1158
 * @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.
T
Tomer Tayar 已提交
1159 1160 1161
 * @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).
1162 1163 1164 1165 1166 1167
 * @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.
1168 1169 1170 1171 1172 1173
 */
struct hl_cs_parser {
	struct hl_cb		*user_cb;
	struct hl_cb		*patched_cb;
	struct list_head	*job_userptr_list;
	u64			cs_sequence;
T
Tomer Tayar 已提交
1174
	enum hl_queue_type	queue_type;
1175 1176 1177 1178 1179
	u32			ctx_id;
	u32			hw_queue_id;
	u32			user_cb_size;
	u32			patched_cb_size;
	u8			job_id;
T
Tomer Tayar 已提交
1180
	u8			is_kernel_allocated_cb;
1181
	u8			contains_dma_pkt;
O
Oded Gabbay 已提交
1182
};
1183

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
/*
 * 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.
 * @ptr: value pointer (hl_vm_phys_pg_list or hl_userptr).
 */
struct hl_vm_hash_node {
	struct hlist_node	node;
	u64			vaddr;
	void			*ptr;
};

/**
 * struct hl_vm_phys_pg_pack - physical page pack.
 * @vm_type: describes the type of the virtual area descriptor.
 * @pages: the physical page array.
1206 1207
 * @npages: num physical pages in the pack.
 * @total_size: total size of all the pages in this list.
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
 * @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 {
	enum vm_type_t		vm_type; /* must be first */
	u64			*pages;
1220 1221
	u64			npages;
	u64			total_size;
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	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;
};

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285

/*
 * 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;
};

O
Oded Gabbay 已提交
1286 1287 1288 1289 1290 1291 1292 1293 1294
/*
 * 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.
1295
 * @ctx: current executing context. TODO: remove for multiple ctx per process
1296
 * @ctx_mgr: context manager to handle multiple context for this FD.
1297
 * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
O
Oded Gabbay 已提交
1298
 * @debugfs_list: list of relevant ASIC debugfs.
1299
 * @dev_node: node in the device list of file private data
O
Oded Gabbay 已提交
1300
 * @refcount: number of related contexts.
1301
 * @restore_phase_mutex: lock for context switch and restore phase.
1302
 * @is_control: true for control device, false otherwise
O
Oded Gabbay 已提交
1303 1304 1305 1306 1307
 */
struct hl_fpriv {
	struct hl_device	*hdev;
	struct file		*filp;
	struct pid		*taskpid;
1308
	struct hl_ctx		*ctx;
1309
	struct hl_ctx_mgr	ctx_mgr;
1310
	struct hl_cb_mgr	cb_mgr;
O
Oded Gabbay 已提交
1311
	struct list_head	debugfs_list;
1312
	struct list_head	dev_node;
O
Oded Gabbay 已提交
1313
	struct kref		refcount;
1314
	struct mutex		restore_phase_mutex;
1315
	u8			is_control;
O
Oded Gabbay 已提交
1316 1317 1318
};


O
Oded Gabbay 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
/*
 * 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.
 * @dent: base debugfs entry structure.
 * @info_ent: dentry realted ops.
 * @dev_entry: ASIC specific debugfs manager.
 */
struct hl_debugfs_entry {
	struct dentry			*dent;
	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.
 * @ctx_mem_hash_spinlock: protects cb_list.
 * @addr: next address to read/write from/to in read/write32.
 * @mmu_addr: next virtual address to translate to physical address in mmu_show.
 * @mmu_asid: ASID to use while translating in mmu_show.
 * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
 * @i2c_bus: generic u8 debugfs file for address value to use in i2c_data_read.
 * @i2c_bus: generic u8 debugfs file for register value to use in i2c_data_read.
 */
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;
	spinlock_t			ctx_mem_hash_spinlock;
	u64				addr;
	u64				mmu_addr;
	u32				mmu_asid;
	u8				i2c_bus;
	u8				i2c_addr;
	u8				i2c_reg;
};


O
Oded Gabbay 已提交
1397 1398 1399 1400 1401
/*
 * DEVICES
 */

/* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
1402
 * x16 cards. In extreme cases, there are hosts that can accommodate 16 cards.
O
Oded Gabbay 已提交
1403 1404 1405
 */
#define HL_MAX_MINORS	256

O
Oded Gabbay 已提交
1406 1407 1408 1409 1410 1411 1412
/*
 * Registers read & write functions.
 */

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

1413 1414
#define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
#define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
O
Oded Gabbay 已提交
1415
#define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n",	\
1416
			hdev->asic_funcs->rreg(hdev, (reg)))
O
Oded Gabbay 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427

#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))

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
#define RMWREG32(reg, val, mask)				\
	do {							\
		u32 tmp_ = RREG32(reg);				\
		tmp_ &= ~(mask);				\
		tmp_ |= ((val) << __ffs(mask));			\
		WREG32(reg, tmp_);				\
	} while (0)

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

O
Oded Gabbay 已提交
1438 1439
#define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
#define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
1440 1441 1442 1443
#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))
O
Oded Gabbay 已提交
1444

O
Oded Gabbay 已提交
1445 1446 1447
/* Timeout should be longer when working with simulator but cap the
 * increased timeout to some maximum
 */
1448 1449
#define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
({ \
1450 1451 1452 1453
	ktime_t __timeout; \
	if (hdev->pdev) \
		__timeout = ktime_add_us(ktime_get(), timeout_us); \
	else \
O
Oded Gabbay 已提交
1454 1455 1456
		__timeout = ktime_add_us(ktime_get(),\
				min((u64)(timeout_us * 10), \
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	might_sleep_if(sleep_us); \
	for (;;) { \
		(val) = RREG32(addr); \
		if (cond) \
			break; \
		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
			(val) = RREG32(addr); \
			break; \
		} \
		if (sleep_us) \
			usleep_range((sleep_us >> 2) + 1, sleep_us); \
	} \
	(cond) ? 0 : -ETIMEDOUT; \
})

1472 1473 1474
/*
 * address in this macro points always to a memory location in the
 * host's (server's) memory. That location is updated asynchronously
1475 1476 1477 1478 1479 1480 1481 1482
 * 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.
1483
 */
1484 1485
#define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
				mem_written_by_device) \
1486 1487 1488 1489 1490
({ \
	ktime_t __timeout; \
	if (hdev->pdev) \
		__timeout = ktime_add_us(ktime_get(), timeout_us); \
	else \
O
Oded Gabbay 已提交
1491 1492 1493
		__timeout = ktime_add_us(ktime_get(),\
				min((u64)(timeout_us * 10), \
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
1494 1495 1496 1497
	might_sleep_if(sleep_us); \
	for (;;) { \
		/* Verify we read updates done by other cores or by device */ \
		mb(); \
1498
		(val) = *((u32 *)(addr)); \
1499
		if (mem_written_by_device) \
1500
			(val) = le32_to_cpu(*(__le32 *) &(val)); \
1501 1502 1503
		if (cond) \
			break; \
		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
1504
			(val) = *((u32 *)(addr)); \
1505
			if (mem_written_by_device) \
1506
				(val) = le32_to_cpu(*(__le32 *) &(val)); \
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
			break; \
		} \
		if (sleep_us) \
			usleep_range((sleep_us >> 2) + 1, sleep_us); \
	} \
	(cond) ? 0 : -ETIMEDOUT; \
})

#define hl_poll_timeout_device_memory(hdev, addr, val, cond, sleep_us, \
					timeout_us) \
({ \
	ktime_t __timeout; \
	if (hdev->pdev) \
		__timeout = ktime_add_us(ktime_get(), timeout_us); \
	else \
O
Oded Gabbay 已提交
1522 1523 1524
		__timeout = ktime_add_us(ktime_get(),\
				min((u64)(timeout_us * 10), \
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	might_sleep_if(sleep_us); \
	for (;;) { \
		(val) = readl(addr); \
		if (cond) \
			break; \
		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
			(val) = readl(addr); \
			break; \
		} \
		if (sleep_us) \
			usleep_range((sleep_us >> 2) + 1, sleep_us); \
	} \
	(cond) ? 0 : -ETIMEDOUT; \
})
1539

1540 1541
struct hwmon_chip_info;

1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
/**
 * struct hl_device_reset_work - reset workqueue task wrapper.
 * @reset_work: reset work to be done.
 * @hdev: habanalabs device structure.
 */
struct hl_device_reset_work {
	struct work_struct		reset_work;
	struct hl_device		*hdev;
};

1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
/**
 * struct hl_device_idle_busy_ts - used for calculating device utilization rate.
 * @idle_to_busy_ts: timestamp where device changed from idle to busy.
 * @busy_to_idle_ts: timestamp where device changed from busy to idle.
 */
struct hl_device_idle_busy_ts {
	ktime_t				idle_to_busy_ts;
	ktime_t				busy_to_idle_ts;
};

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

/**
 * struct hl_mmu_priv - used for holding per-device mmu 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_priv {
	struct gen_pool *mmu_pgt_pool;
	void *mmu_shadow_hop0;
};

/**
 * 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.
 */
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);
	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);
	void (*flush)(struct hl_ctx *ctx);
	void (*swap_out)(struct hl_ctx *ctx);
	void (*swap_in)(struct hl_ctx *ctx);
};

O
Oded Gabbay 已提交
1600 1601 1602
/**
 * struct hl_device - habanalabs device structure.
 * @pdev: pointer to PCI device, can be NULL in case of simulator device.
O
Ofir Bitton 已提交
1603 1604 1605
 * @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.
O
Oded Gabbay 已提交
1606
 * @rmmio: configuration area address on SRAM.
O
Oded Gabbay 已提交
1607
 * @cdev: related char device.
1608 1609 1610
 * @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
1611
 * @work_freq: delayed work to lower device frequency if possible.
O
Oded Gabbay 已提交
1612 1613
 * @work_heartbeat: delayed work for CPU-CP is-alive check.
 * @asic_name: ASIC specific name.
O
Oded Gabbay 已提交
1614
 * @asic_type: ASIC specific type.
O
Oded Gabbay 已提交
1615
 * @completion_queue: array of hl_cq.
1616 1617
 * @cq_wq: work queues of completion queues for executing work in process
 *         context.
O
Oded Gabbay 已提交
1618
 * @eq_wq: work queue of event queue for executing work in process context.
1619
 * @kernel_ctx: Kernel driver context structure.
O
Oded Gabbay 已提交
1620
 * @kernel_queues: array of hl_hw_queue.
1621 1622
 * @hw_queues_mirror_list: CS mirror list for TDR.
 * @hw_queues_mirror_lock: protects hw_queues_mirror_list.
1623
 * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs.
O
Oded Gabbay 已提交
1624
 * @event_queue: event queue for IRQ from CPU-CP.
O
Oded Gabbay 已提交
1625
 * @dma_pool: DMA pool for small allocations.
O
Oded Gabbay 已提交
1626 1627 1628
 * @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.
1629 1630
 * @asid_bitmap: holds used/available ASIDs.
 * @asid_mutex: protects asid_bitmap.
O
Oded Gabbay 已提交
1631
 * @send_cpu_message_lock: enforces only one message in Host <-> CPU-CP queue.
1632
 * @debug_lock: protects critical section of setting debug mode for device
O
Oded Gabbay 已提交
1633 1634 1635
 * @asic_prop: ASIC specific immutable properties.
 * @asic_funcs: ASIC specific functions.
 * @asic_specific: ASIC specific information to use only from ASIC files.
1636
 * @vm: virtual memory manager for MMU.
1637
 * @mmu_cache_lock: protects MMU cache invalidation as it can serve one context.
1638 1639 1640
 * @hwmon_dev: H/W monitor device.
 * @pm_mng_profile: current power management profile.
 * @hl_chip_info: ASIC's sensors information.
O
Oded Gabbay 已提交
1641
 * @hl_debugfs: device's debugfs manager.
1642 1643
 * @cb_pool: list of preallocated CBs.
 * @cb_pool_lock: protects the CB pool.
1644 1645 1646 1647
 * @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
1648 1649 1650
 * @fpriv_list: list of file private data structures. Each structure is created
 *              when a user opens the device
 * @fpriv_list_lock: protects the fpriv_list
1651
 * @compute_ctx: current compute context executing.
1652 1653
 * @idle_busy_ts_arr: array to hold time stamps of transitions from idle to busy
 *                    and vice-versa
1654
 * @aggregated_cs_counters: aggregated cs counters among all contexts
1655 1656
 * @mmu_priv: device-specific MMU data.
 * @mmu_func: device-related MMU functions.
1657
 * @dram_used_mem: current DRAM memory consumption.
1658
 * @timeout_jiffies: device CS timeout value.
1659
 * @max_power: the max power of the device, as configured by the sysadmin. This
1660 1661
 *             value is saved so in case of hard-reset, the driver will restore
 *             this value and update the F/W after the re-initialization
1662 1663 1664
 * @clock_gating_mask: is clock gating enabled. bitmask that represents the
 *                     different engines. See debugfs-driver-habanalabs for
 *                     details.
1665
 * @in_reset: is device in reset flow.
1666
 * @curr_pll_profile: current PLL profile.
1667 1668
 * @card_type: Various ASICs have several card types. This indicates the card
 *             type of the current device.
1669 1670
 * @cs_active_cnt: number of active command submissions on this device (active
 *                 means already in H/W queues)
1671
 * @major: habanalabs kernel driver major.
1672
 * @high_pll: high PLL profile frequency.
1673 1674
 * @soft_reset_cnt: number of soft reset since the driver was loaded.
 * @hard_reset_cnt: number of hard reset since the driver was loaded.
1675
 * @idle_busy_ts_idx: index of current entry in idle_busy_ts_arr
1676
 * @clk_throttling_reason: bitmask represents the current clk throttling reasons
O
Oded Gabbay 已提交
1677
 * @id: device minor.
1678
 * @id_control: minor of the control device
1679 1680
 * @cpu_pci_msb_addr: 50-bit extension bits for the device CPU's 40-bit
 *                    addresses.
O
Oded Gabbay 已提交
1681
 * @disabled: is device disabled.
1682 1683
 * @late_init_done: is late init stage was done during initialization.
 * @hwmon_initialized: is H/W monitor sensors was initialized.
1684
 * @hard_reset_pending: is there a hard reset work pending.
O
Oded Gabbay 已提交
1685
 * @heartbeat: is heartbeat sanity check towards CPU-CP enabled.
1686 1687
 * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
 *                   otherwise.
1688
 * @dram_supports_virtual_memory: is MMU enabled towards DRAM.
1689
 * @dram_default_page_mapping: is DRAM default page mapping enabled.
1690 1691
 * @pmmu_huge_range: is a different virtual addresses range used for PMMU with
 *                   huge pages.
1692
 * @init_done: is the initialization of the device done.
1693
 * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
1694
 * @dma_mask: the dma mask that was set for this device
1695
 * @in_debug: is device under debug. This, together with fpriv_list, enforces
1696
 *            that only a single user is configuring the debug infrastructure.
1697 1698
 * @power9_64bit_dma_enable: true to enable 64-bit DMA mask support. Relevant
 *                           only to POWER9 machines.
1699
 * @cdev_sysfs_created: were char devices and sysfs nodes created.
1700
 * @stop_on_err: true if engines should stop on error.
1701
 * @supports_sync_stream: is sync stream supported.
1702
 * @sync_stream_queue_idx: helper index for sync stream queues initialization.
1703
 * @collective_mon_idx: helper index for collective initialization
1704
 * @supports_coresight: is CoreSight supported.
1705
 * @supports_soft_reset: is soft reset supported.
1706
 * @supports_cb_mapping: is mapping a CB to the device's MMU supported.
O
Oded Gabbay 已提交
1707 1708 1709
 */
struct hl_device {
	struct pci_dev			*pdev;
O
Ofir Bitton 已提交
1710 1711
	u64				pcie_bar_phys[HL_PCI_NUM_BARS];
	void __iomem			*pcie_bar[HL_PCI_NUM_BARS];
O
Oded Gabbay 已提交
1712
	void __iomem			*rmmio;
O
Oded Gabbay 已提交
1713
	struct cdev			cdev;
1714
	struct cdev			cdev_ctrl;
O
Oded Gabbay 已提交
1715
	struct device			*dev;
1716
	struct device			*dev_ctrl;
1717
	struct delayed_work		work_freq;
1718
	struct delayed_work		work_heartbeat;
1719
	char				asic_name[32];
O
Oded Gabbay 已提交
1720
	enum hl_asic_type		asic_type;
O
Oded Gabbay 已提交
1721
	struct hl_cq			*completion_queue;
1722
	struct workqueue_struct		**cq_wq;
1723
	struct workqueue_struct		*eq_wq;
1724
	struct hl_ctx			*kernel_ctx;
O
Oded Gabbay 已提交
1725
	struct hl_hw_queue		*kernel_queues;
1726 1727
	struct list_head		hw_queues_mirror_list;
	spinlock_t			hw_queues_mirror_lock;
1728
	struct hl_cb_mgr		kernel_cb_mgr;
1729
	struct hl_eq			event_queue;
O
Oded Gabbay 已提交
1730 1731 1732 1733
	struct dma_pool			*dma_pool;
	void				*cpu_accessible_dma_mem;
	dma_addr_t			cpu_accessible_dma_address;
	struct gen_pool			*cpu_accessible_dma_pool;
1734 1735
	unsigned long			*asid_bitmap;
	struct mutex			asid_mutex;
O
Oded Gabbay 已提交
1736
	struct mutex			send_cpu_message_lock;
1737
	struct mutex			debug_lock;
O
Oded Gabbay 已提交
1738 1739 1740
	struct asic_fixed_properties	asic_prop;
	const struct hl_asic_funcs	*asic_funcs;
	void				*asic_specific;
1741 1742
	struct hl_vm			vm;
	struct mutex			mmu_cache_lock;
1743 1744 1745
	struct device			*hwmon_dev;
	enum hl_pm_mng_profile		pm_mng_profile;
	struct hwmon_chip_info		*hl_chip_info;
1746

O
Oded Gabbay 已提交
1747 1748
	struct hl_dbg_device_entry	hl_debugfs;

1749 1750 1751
	struct list_head		cb_pool;
	spinlock_t			cb_pool_lock;

1752 1753 1754 1755 1756
	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;

1757 1758 1759
	struct list_head		fpriv_list;
	struct mutex			fpriv_list_lock;

1760
	struct hl_ctx			*compute_ctx;
1761

1762 1763
	struct hl_device_idle_busy_ts	*idle_busy_ts_arr;

1764
	struct hl_cs_counters_atomic	aggregated_cs_counters;
1765

1766 1767 1768
	struct hl_mmu_priv		mmu_priv;
	struct hl_mmu_funcs		mmu_func;

1769
	atomic64_t			dram_used_mem;
1770 1771
	u64				timeout_jiffies;
	u64				max_power;
1772
	u64				clock_gating_mask;
1773
	atomic_t			in_reset;
1774
	enum hl_pll_frequency		curr_pll_profile;
1775
	enum cpucp_card_types		card_type;
1776
	int				cs_active_cnt;
O
Oded Gabbay 已提交
1777
	u32				major;
1778
	u32				high_pll;
1779 1780
	u32				soft_reset_cnt;
	u32				hard_reset_cnt;
1781
	u32				idle_busy_ts_idx;
1782
	u32				clk_throttling_reason;
O
Oded Gabbay 已提交
1783
	u16				id;
1784
	u16				id_control;
1785
	u16				cpu_pci_msb_addr;
O
Oded Gabbay 已提交
1786
	u8				disabled;
1787 1788
	u8				late_init_done;
	u8				hwmon_initialized;
1789 1790
	u8				hard_reset_pending;
	u8				heartbeat;
1791
	u8				reset_on_lockup;
1792
	u8				dram_supports_virtual_memory;
1793
	u8				dram_default_page_mapping;
1794
	u8				pmmu_huge_range;
1795
	u8				init_done;
1796
	u8				device_cpu_disabled;
1797
	u8				dma_mask;
1798
	u8				in_debug;
1799
	u8				power9_64bit_dma_enable;
1800
	u8				cdev_sysfs_created;
1801
	u8				stop_on_err;
1802
	u8				supports_sync_stream;
1803
	u8				sync_stream_queue_idx;
1804
	u8				collective_mon_idx;
1805
	u8				supports_coresight;
1806
	u8				supports_soft_reset;
1807
	u8				supports_cb_mapping;
O
Oded Gabbay 已提交
1808 1809

	/* Parameters for bring-up */
1810
	u64				nic_ports_mask;
1811
	u64				fw_loading;
1812
	u8				mmu_enable;
1813
	u8				mmu_huge_page_opt;
1814
	u8				cpu_enable;
O
Oded Gabbay 已提交
1815
	u8				reset_pcilink;
O
Oded Gabbay 已提交
1816
	u8				cpu_queues_enable;
1817
	u8				pldm;
1818 1819 1820 1821 1822 1823
	u8				axi_drain;
	u8				sram_scrambler_enable;
	u8				dram_scrambler_enable;
	u8				hard_reset_on_fw_events;
	u8				bmc_enable;
	u8				rl_enable;
O
Oded Gabbay 已提交
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
};


/*
 * 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
 */

1856 1857 1858 1859 1860 1861 1862 1863 1864
/**
 * 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.
 */
1865
static inline bool hl_mem_area_inside_range(u64 address, u64 size,
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
				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)
{
	u64 end_address = address + size;

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

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

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

	return false;
}

O
Oded Gabbay 已提交
1908
int hl_device_open(struct inode *inode, struct file *filp);
1909
int hl_device_open_ctrl(struct inode *inode, struct file *filp);
1910
bool hl_device_disabled_or_in_reset(struct hl_device *hdev);
1911
enum hl_device_status hl_device_status(struct hl_device *hdev);
1912
int hl_device_set_debug_mode(struct hl_device *hdev, bool enable);
O
Oded Gabbay 已提交
1913 1914 1915
int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
		enum hl_asic_type asic_type, int minor);
void destroy_hdev(struct hl_device *hdev);
O
Oded Gabbay 已提交
1916 1917 1918 1919
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,
				u32 cb_size, u64 cb_ptr);
1920
int hl_hw_queue_schedule_cs(struct hl_cs *cs);
O
Oded Gabbay 已提交
1921 1922
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);
1923
void hl_int_hw_queue_update_ci(struct hl_cs *cs);
1924
void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
O
Oded Gabbay 已提交
1925 1926 1927 1928 1929 1930

#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);
1931 1932
int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
1933 1934
void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
1935 1936
irqreturn_t hl_irq_handler_cq(int irq, void *arg);
irqreturn_t hl_irq_handler_eq(int irq, void *arg);
1937 1938
u32 hl_cq_inc_ptr(u32 ptr);

1939 1940 1941 1942 1943 1944 1945 1946
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);
1947 1948
void hl_ctx_do_release(struct kref *ref);
void hl_ctx_get(struct hl_device *hdev,	struct hl_ctx *ctx);
1949
int hl_ctx_put(struct hl_ctx *ctx);
1950
struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
1951 1952
void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
1953

O
Oded Gabbay 已提交
1954 1955 1956 1957
int hl_device_init(struct hl_device *hdev, struct class *hclass);
void hl_device_fini(struct hl_device *hdev);
int hl_device_suspend(struct hl_device *hdev);
int hl_device_resume(struct hl_device *hdev);
1958 1959
int hl_device_reset(struct hl_device *hdev, bool hard_reset,
			bool from_hard_reset_thread);
1960 1961
void hl_hpriv_get(struct hl_fpriv *hpriv);
void hl_hpriv_put(struct hl_fpriv *hpriv);
1962
int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq);
1963
uint32_t hl_device_utilization(struct hl_device *hdev, uint32_t period_ms);
1964

1965
int hl_build_hwmon_channel_info(struct hl_device *hdev,
1966
		struct cpucp_sensor *sensors_arr);
1967 1968 1969 1970 1971 1972

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);
O
Oded Gabbay 已提交
1973

1974 1975
int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
			struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
1976
			bool map_cb, u64 *handle);
1977 1978 1979 1980 1981 1982 1983
int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
struct hl_cb *hl_cb_get(struct hl_device *hdev,	struct hl_cb_mgr *mgr,
			u32 handle);
void hl_cb_put(struct hl_cb *cb);
void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
1984 1985
struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
					bool internal_cb);
1986 1987
int hl_cb_pool_init(struct hl_device *hdev);
int hl_cb_pool_fini(struct hl_device *hdev);
1988 1989
int hl_cb_va_pool_init(struct hl_ctx *ctx);
void hl_cb_va_pool_fini(struct hl_ctx *ctx);
1990

1991
void hl_cs_rollback_all(struct hl_device *hdev);
T
Tomer Tayar 已提交
1992 1993
struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
		enum hl_queue_type queue_type, bool is_kernel_allocated_cb);
1994
void hl_sob_reset_error(struct kref *ref);
1995
int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask);
1996 1997
void hl_fence_put(struct hl_fence *fence);
void hl_fence_get(struct hl_fence *fence);
1998
void cs_get(struct hl_cs *cs);
1999

O
Oded Gabbay 已提交
2000
void goya_set_asic_funcs(struct hl_device *hdev);
2001
void gaudi_set_asic_funcs(struct hl_device *hdev);
O
Oded Gabbay 已提交
2002

2003 2004 2005 2006 2007 2008
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);

2009
int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
2010
			struct hl_userptr *userptr);
2011
void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
2012 2013 2014 2015 2016 2017
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);

2018 2019
int hl_mmu_init(struct hl_device *hdev);
void hl_mmu_fini(struct hl_device *hdev);
2020
int hl_mmu_ctx_init(struct hl_ctx *ctx);
2021
void hl_mmu_ctx_fini(struct hl_ctx *ctx);
2022 2023 2024 2025
int hl_mmu_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
		u32 page_size, bool flush_pte);
int hl_mmu_unmap(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
		bool flush_pte);
2026 2027
void hl_mmu_swap_out(struct hl_ctx *ctx);
void hl_mmu_swap_in(struct hl_ctx *ctx);
2028 2029
int hl_mmu_if_set_funcs(struct hl_device *hdev);
void hl_mmu_v1_set_funcs(struct hl_device *hdev);
2030

2031
int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
O
Ofir Bitton 已提交
2032
				void __iomem *dst, u32 src_offset, u32 size);
2033 2034 2035
int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode);
int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
				u16 len, u32 timeout, long *result);
2036 2037 2038
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);
2039 2040 2041 2042 2043 2044
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);
2045
int hl_fw_cpucp_info_get(struct hl_device *hdev);
2046
int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
2047
int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
2048
		struct hl_info_pci_counters *counters);
2049
int hl_fw_cpucp_total_energy_get(struct hl_device *hdev,
2050
			u64 *total_energy);
2051
int hl_fw_init_cpu(struct hl_device *hdev, u32 cpu_boot_status_reg,
2052 2053 2054
			u32 msg_to_cpu_reg, u32 cpu_msg_status_reg,
			u32 boot_err0_reg, bool skip_bmc,
			u32 cpu_timeout, u32 boot_fit_timeout);
2055 2056
int hl_fw_read_preboot_ver(struct hl_device *hdev, u32 cpu_boot_status_reg,
				u32 boot_err0_reg, u32 timeout);
2057

2058 2059 2060
int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
			bool is_wc[3]);
int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
O
Ofir Bitton 已提交
2061 2062 2063 2064
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);
2065 2066
int hl_pci_init(struct hl_device *hdev, u32 cpu_boot_status_reg,
		u32 boot_err0_reg, u32 preboot_ver_timeout);
2067 2068
void hl_pci_fini(struct hl_device *hdev);

2069 2070
long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr);
void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq);
2071 2072
int hl_get_temperature(struct hl_device *hdev,
		       int sensor_index, u32 attr, long *value);
2073
int hl_set_temperature(struct hl_device *hdev,
2074 2075 2076 2077 2078 2079 2080 2081 2082
		       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);
2083 2084 2085
void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
			long value);
u64 hl_get_max_power(struct hl_device *hdev);
2086
void hl_set_max_power(struct hl_device *hdev);
2087 2088 2089 2090
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);
2091

O
Oded Gabbay 已提交
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
#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);

#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)
{
}

#endif

2186 2187
/* IOCTLs */
long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
2188
long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
2189
int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
2190 2191
int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data);
2192
int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
2193

O
Oded Gabbay 已提交
2194
#endif /* HABANALABSP_H_ */