habanalabs.h 89.8 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 "../include/hw_ip/mmu/mmu_general.h"
14
#include <uapi/misc/habanalabs.h>
15

O
Oded Gabbay 已提交
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/bitfield.h>
24 25 26 27
#include <linux/genalloc.h>
#include <linux/sched/signal.h>
#include <linux/io-64-nonatomic-lo-hi.h>
#include <linux/coresight.h>
O
Oded Gabbay 已提交
28 29 30

#define HL_NAME				"habanalabs"

O
Oded Gabbay 已提交
31
/* Use upper bits of mmap offset to store habana driver specific information.
32
 * bits[63:61] - Encode mmap type
O
Oded Gabbay 已提交
33 34 35 36 37
 * 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
 */
38 39 40
#define HL_MMAP_TYPE_SHIFT		(61 - PAGE_SHIFT)
#define HL_MMAP_TYPE_MASK		(0x7ull << HL_MMAP_TYPE_SHIFT)
#define HL_MMAP_TYPE_BLOCK		(0x4ull << HL_MMAP_TYPE_SHIFT)
O
Oded Gabbay 已提交
41 42
#define HL_MMAP_TYPE_CB			(0x2ull << HL_MMAP_TYPE_SHIFT)

43
#define HL_MMAP_OFFSET_VALUE_MASK	(0x1FFFFFFFFFFFull >> PAGE_SHIFT)
O
Oded Gabbay 已提交
44
#define HL_MMAP_OFFSET_VALUE_GET(off)	(off & HL_MMAP_OFFSET_VALUE_MASK)
45

46 47 48
#define HL_PENDING_RESET_PER_SEC	10
#define HL_PENDING_RESET_MAX_TRIALS	60 /* 10 minutes */
#define HL_PENDING_RESET_LONG_SEC	60
49

50 51
#define HL_HARD_RESET_MAX_TIMEOUT	120

52 53
#define HL_DEVICE_TIMEOUT_USEC		1000000 /* 1 s */

54 55
#define HL_HEARTBEAT_PER_USEC		5000000 /* 5 s */

56 57
#define HL_PLL_LOW_JOB_FREQ_USEC	5000000 /* 5 s */

58 59
#define HL_CPUCP_INFO_TIMEOUT_USEC	10000000 /* 10s */
#define HL_CPUCP_EEPROM_TIMEOUT_USEC	10000000 /* 10s */
60

61 62
#define HL_PCI_ELBI_TIMEOUT_MSEC	10 /* 10ms */

O
Oded Gabbay 已提交
63 64
#define HL_SIM_MAX_TIMEOUT_US		10000000 /* 10s */

65 66
#define HL_COMMON_USER_INTERRUPT_ID	0xFFF

67 68 69 70 71 72
/* Memory */
#define MEM_HASH_TABLE_BITS		7 /* 1 << 7 buckets */

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

73 74 75 76 77 78 79 80 81 82 83 84
/**
 * enum hl_mmu_page_table_locaion - mmu page table location
 * @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 */
};

85 86 87 88
/*
 * HL_RSVD_SOBS 'sync stream' reserved sync objects per QMAN stream
 * HL_RSVD_MONS 'sync stream' reserved monitors per QMAN stream
 */
89 90
#define HL_RSVD_SOBS			2
#define HL_RSVD_MONS			1
91

92 93 94 95 96
/*
 * HL_COLLECTIVE_RSVD_MSTR_MONS 'collective' reserved monitors per QMAN stream
 */
#define HL_COLLECTIVE_RSVD_MSTR_MONS	2

97 98
#define HL_MAX_SOB_VAL			(1 << 15)

99 100 101
#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 已提交
102 103
#define HL_PCI_NUM_BARS			6

104 105
#define HL_MAX_DCORES			4

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/*
 * Reset Flags
 *
 * - HL_RESET_HARD
 *       If set do hard reset to all engines. If not set reset just
 *       compute/DMA engines.
 *
 * - HL_RESET_FROM_RESET_THREAD
 *       Set if the caller is the hard-reset thread
 *
 * - HL_RESET_HEARTBEAT
 *       Set if reset is due to heartbeat
 */
#define HL_RESET_HARD			(1 << 0)
#define HL_RESET_FROM_RESET_THREAD	(1 << 1)
#define HL_RESET_HEARTBEAT		(1 << 2)

123 124
#define HL_MAX_SOBS_PER_MONITOR	8

125 126 127 128
/**
 * 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
129 130
 * @size: offset in command buffer
 * @sob_base: SOB base to use in this wait CB
131 132
 * @sob_val: SOB value to wait for
 * @mon_id: monitor to use in this wait CB
133
 * @sob_mask: each bit represents a SOB offset from sob_base to be used
134 135 136 137
 */
struct hl_gen_wait_properties {
	void	*data;
	u32	q_idx;
138 139
	u32	size;
	u16	sob_base;
140 141
	u16	sob_val;
	u16	mon_id;
142
	u8	sob_mask;
143 144
};

145 146
/**
 * struct pgt_info - MMU hop page info.
147 148 149
 * @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.
150 151 152 153 154 155 156 157 158
 * @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 {
159 160 161 162 163
	struct hlist_node	node;
	u64			phys_addr;
	u64			shadow_addr;
	struct hl_ctx		*ctx;
	int			num_of_ptes;
164 165
};

O
Oded Gabbay 已提交
166
struct hl_device;
167
struct hl_fpriv;
O
Oded Gabbay 已提交
168

O
Ofir Bitton 已提交
169 170 171 172 173 174 175 176 177 178
/**
 * 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
};

179 180 181 182 183 184 185 186 187 188
/**
 * 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
};

189
/**
190
 * enum hl_fw_types - F/W types present in the system
191 192
 * @FW_TYPE_LINUX: Linux image for device CPU
 * @FW_TYPE_BOOT_CPU: Boot image for device CPU
193 194
 * @FW_TYPE_PREBOOT_CPU: Indicates pre-loaded CPUs are present in the system
 *                       (preboot, ppboot etc...)
195 196 197 198 199
 * @FW_TYPE_ALL_TYPES: Mask for all types
 */
enum hl_fw_types {
	FW_TYPE_LINUX = 0x1,
	FW_TYPE_BOOT_CPU = 0x2,
200 201 202
	FW_TYPE_PREBOOT_CPU = 0x4,
	FW_TYPE_ALL_TYPES =
		(FW_TYPE_LINUX | FW_TYPE_BOOT_CPU | FW_TYPE_PREBOOT_CPU)
203 204
};

O
Oded Gabbay 已提交
205 206 207 208 209 210 211 212
/**
 * 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 已提交
213 214
 * @QUEUE_TYPE_HW: queue of DMA and compute engines jobs, for which completion
 *                 notifications are sent by H/W.
O
Oded Gabbay 已提交
215 216 217 218 219
 */
enum hl_queue_type {
	QUEUE_TYPE_NA,
	QUEUE_TYPE_EXT,
	QUEUE_TYPE_INT,
T
Tomer Tayar 已提交
220 221
	QUEUE_TYPE_CPU,
	QUEUE_TYPE_HW
O
Oded Gabbay 已提交
222 223
};

224 225 226
enum hl_cs_type {
	CS_TYPE_DEFAULT,
	CS_TYPE_SIGNAL,
227 228
	CS_TYPE_WAIT,
	CS_TYPE_COLLECTIVE_WAIT
229 230
};

O
Ofir Bitton 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
/*
 * 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;
};

257 258 259 260 261 262 263 264 265 266 267
/*
 * 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
};

268 269 270 271 272 273 274 275 276 277 278 279 280 281
/*
 * 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;
};

282 283 284 285 286 287
enum hl_collective_mode {
	HL_COLLECTIVE_NOT_SUPPORTED = 0x0,
	HL_COLLECTIVE_MASTER = 0x1,
	HL_COLLECTIVE_SLAVE = 0x2
};

O
Oded Gabbay 已提交
288 289 290
/**
 * struct hw_queue_properties - queue information.
 * @type: queue type.
291 292 293 294
 * @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.
295
 * @collective_mode: collective mode of current queue
296 297
 * @driver_only: true if only the driver is allowed to send a job to this queue,
 *               false otherwise.
298
 * @supports_sync_stream: True if queue supports sync stream
O
Oded Gabbay 已提交
299 300 301
 */
struct hw_queue_properties {
	enum hl_queue_type	type;
302
	enum queue_cb_alloc_flags cb_alloc_flags;
303
	enum hl_collective_mode	collective_mode;
304
	u8			driver_only;
305
	u8			supports_sync_stream;
O
Oded Gabbay 已提交
306
};
O
Oded Gabbay 已提交
307

308 309 310
/**
 * enum vm_type_t - virtual memory mapping request information.
 * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
311
 * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
312 313
 */
enum vm_type_t {
314 315
	VM_TYPE_USERPTR = 0x1,
	VM_TYPE_PHYS_PACK = 0x2
316 317
};

318 319 320 321 322 323 324 325 326 327 328 329
/**
 * 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
};

330 331
#define HL_MMU_VA_ALIGNMENT_NOT_NEEDED 0

332 333
/**
 * struct hl_mmu_properties - ASIC specific MMU address translation properties.
334 335
 * @start_addr: virtual start address of the memory region.
 * @end_addr: virtual end address of the memory region.
336 337 338 339 340
 * @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.
341
 * @hop5_shift: shift of hop 5 mask.
342 343 344 345 346
 * @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.
347
 * @hop5_mask: mask to get the PTE address in hop 5.
348
 * @page_size: default page size used to allocate memory.
349
 * @num_hops: The amount of hops supported by the translation table.
350 351
 * @host_resident: Should the MMU page table reside in host memory or in the
 *                 device DRAM.
352 353
 */
struct hl_mmu_properties {
354 355
	u64	start_addr;
	u64	end_addr;
356 357 358 359 360
	u64	hop0_shift;
	u64	hop1_shift;
	u64	hop2_shift;
	u64	hop3_shift;
	u64	hop4_shift;
361
	u64	hop5_shift;
362 363 364 365 366
	u64	hop0_mask;
	u64	hop1_mask;
	u64	hop2_mask;
	u64	hop3_mask;
	u64	hop4_mask;
367
	u64	hop5_mask;
368
	u32	page_size;
369
	u32	num_hops;
370
	u8	host_resident;
371 372
};

O
Oded Gabbay 已提交
373 374
/**
 * struct asic_fixed_properties - ASIC specific immutable properties.
O
Oded Gabbay 已提交
375
 * @hw_queues_props: H/W queues properties.
376
 * @cpucp_info: received various information from CPU-CP regarding the H/W, e.g.
377
 *		available sensors.
378 379
 * @uboot_ver: F/W U-boot version.
 * @preboot_ver: F/W Preboot version.
380 381
 * @dmmu: DRAM MMU address translation properties.
 * @pmmu: PCI (host) MMU address translation properties.
382 383
 * @pmmu_huge: PCI (host) MMU address translation properties for memory
 *              allocated with huge pages.
O
Oded Gabbay 已提交
384 385 386 387 388 389 390 391
 * @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.
392
 * @max_power_default: max power of the device after reset
393
 * @dc_power_default: power consumed by the device in mode idle.
394 395
 * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
 *                                      fault.
396 397
 * @pcie_dbi_base_address: Base address of the PCIE_DBI block.
 * @pcie_aux_dbi_reg_addr: Address of the PCIE_AUX DBI register.
398
 * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
399
 * @mmu_dram_default_page_addr: DRAM default page physical address.
400 401 402 403
 * @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.
404 405 406 407 408
 * @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 已提交
409 410 411
 * @cfg_size: configuration space size on SRAM.
 * @sram_size: total size of SRAM.
 * @max_asid: maximum number of open contexts (ASIDs).
412
 * @num_of_events: number of possible internal H/W IRQs.
413 414 415 416
 * @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.
417
 * @psoc_timestamp_frequency: frequency of the psoc timestamp clock.
O
Oded Gabbay 已提交
418
 * @high_pll: high PLL frequency used by the device.
419 420
 * @cb_pool_cb_cnt: number of CBs in the CB pool.
 * @cb_pool_cb_size: size of each CB in the CB pool.
421 422
 * @max_pending_cs: maximum of concurrent pending command submissions
 * @max_queues: maximum amount of queues in the system
423 424 425 426 427 428
 * @fw_boot_cpu_security_map: bitmap representation of boot cpu security status
 *                            reported by FW, bit description can be found in
 *                            CPU_BOOT_DEV_STS*
 * @fw_app_security_map: bitmap representation of application security status
 *                       reported by FW, bit description can be found in
 *                       CPU_BOOT_DEV_STS*
429 430
 * @collective_first_sob: first sync object available for collective use
 * @collective_first_mon: first monitor available for collective use
431 432
 * @sync_stream_first_sob: first sync object available for sync stream use
 * @sync_stream_first_mon: first monitor available for sync stream use
433 434
 * @first_available_user_sob: first sob available for the user
 * @first_available_user_mon: first monitor available for the user
435 436
 * @first_available_user_msix_interrupt: first available msix interrupt
 *                                       reserved for the user
437
 * @first_available_cq: first available CQ for the user.
438
 * @user_interrupt_count: number of user interrupts.
439
 * @tpc_enabled_mask: which TPCs are enabled.
440
 * @completion_queues_count: number of completion queues.
441 442
 * @fw_security_disabled: true if security measures are disabled in firmware,
 *                        false otherwise
443 444
 * @fw_security_status_valid: security status bits are valid and can be fetched
 *                            from BOOT_DEV_STS0
445
 * @dram_supports_virtual_memory: is there an MMU towards the DRAM
446
 * @hard_reset_done_by_fw: true if firmware is handling hard reset flow
447
 * @num_functional_hbms: number of functional HBMs in each DCORE.
448
 * @iatu_done_by_fw: true if iATU configuration is being done by FW.
O
Oded Gabbay 已提交
449 450
 */
struct asic_fixed_properties {
451
	struct hw_queue_properties	*hw_queues_props;
452
	struct cpucp_info		cpucp_info;
453 454 455 456
	char				uboot_ver[VERSION_MAX_LEN];
	char				preboot_ver[VERSION_MAX_LEN];
	struct hl_mmu_properties	dmmu;
	struct hl_mmu_properties	pmmu;
457
	struct hl_mmu_properties	pmmu_huge;
458 459 460 461 462 463 464 465 466
	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;
467
	u64				dc_power_default;
468 469 470 471 472
	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;
473 474
	u64				cb_va_start_addr;
	u64				cb_va_end_addr;
475 476 477 478 479 480 481 482 483 484 485 486 487
	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;
488
	u32				psoc_timestamp_frequency;
489 490 491
	u32				high_pll;
	u32				cb_pool_cb_cnt;
	u32				cb_pool_cb_size;
492
	u32				max_pending_cs;
493
	u32				max_queues;
494 495
	u32				fw_boot_cpu_security_map;
	u32				fw_app_security_map;
496 497
	u16				collective_first_sob;
	u16				collective_first_mon;
498 499
	u16				sync_stream_first_sob;
	u16				sync_stream_first_mon;
500 501
	u16				first_available_user_sob[HL_MAX_DCORES];
	u16				first_available_user_mon[HL_MAX_DCORES];
502
	u16				first_available_user_msix_interrupt;
503
	u16				first_available_cq[HL_MAX_DCORES];
504
	u16				user_interrupt_count;
505 506
	u8				tpc_enabled_mask;
	u8				completion_queues_count;
507
	u8				fw_security_disabled;
508
	u8				fw_security_status_valid;
509
	u8				dram_supports_virtual_memory;
510
	u8				hard_reset_done_by_fw;
511
	u8				num_functional_hbms;
512
	u8				iatu_done_by_fw;
O
Oded Gabbay 已提交
513 514
};

515 516 517 518
/**
 * struct hl_fence - software synchronization primitive
 * @completion: fence is implemented using completion
 * @refcount: refcount for this fence
519
 * @cs_sequence: sequence of the corresponding command submission
520
 * @error: mark this fence with error
521
 * @timestamp: timestamp upon completion
522 523 524 525 526
 *
 */
struct hl_fence {
	struct completion	completion;
	struct kref		refcount;
527
	u64			cs_sequence;
528
	int			error;
529
	ktime_t			timestamp;
530 531
};

532
/**
533
 * struct hl_cs_compl - command submission completion object.
534
 * @sob_reset_work: workqueue object to run SOB reset flow.
535
 * @base_fence: hl fence object.
536 537
 * @lock: spinlock to protect fence.
 * @hdev: habanalabs device structure.
538
 * @hw_sob: the H/W SOB used in this signal/wait CS.
539
 * @cs_seq: command submission sequence number.
540 541
 * @type: type of the CS - signal/wait.
 * @sob_val: the SOB value that is used in this signal/wait CS.
542
 * @sob_group: the SOB group that is used in this collective wait CS.
543
 */
544
struct hl_cs_compl {
545
	struct work_struct	sob_reset_work;
546
	struct hl_fence		base_fence;
547 548
	spinlock_t		lock;
	struct hl_device	*hdev;
549
	struct hl_hw_sob	*hw_sob;
550
	u64			cs_seq;
551 552
	enum hl_cs_type		type;
	u16			sob_val;
553
	u16			sob_group;
554
};
O
Oded Gabbay 已提交
555

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
/*
 * 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.
574
 * @ctx: pointer to the CB owner's context.
575
 * @lock: spinlock to protect mmap flows.
O
Oded Gabbay 已提交
576
 * @debugfs_list: node in debugfs list of command buffers.
577
 * @pool_list: node in pool list of command buffers.
578 579
 * @va_block_list: list of virtual addresses blocks of the CB if it is mapped to
 *                 the device's MMU.
580
 * @id: the CB's ID.
581 582 583 584
 * @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.
585
 * @cs_cnt: holds number of CS that this CB participates in.
586 587
 * @mmap: true if the CB is currently mmaped to user.
 * @is_pool: true if CB was acquired from the pool, false otherwise.
588
 * @is_internal: internaly allocated
589
 * @is_mmu_mapped: true if the CB is mapped to the device's MMU.
590 591 592 593
 */
struct hl_cb {
	struct kref		refcount;
	struct hl_device	*hdev;
594
	struct hl_ctx		*ctx;
595
	spinlock_t		lock;
O
Oded Gabbay 已提交
596
	struct list_head	debugfs_list;
597
	struct list_head	pool_list;
598
	struct list_head	va_block_list;
599
	u64			id;
600
	void			*kernel_address;
601 602 603
	dma_addr_t		bus_address;
	u32			mmap_size;
	u32			size;
604
	atomic_t		cs_cnt;
605 606
	u8			mmap;
	u8			is_pool;
607
	u8			is_internal;
608
	u8			is_mmu_mapped;
609 610 611
};


O
Oded Gabbay 已提交
612 613 614 615
/*
 * QUEUES
 */

616
struct hl_cs;
O
Oded Gabbay 已提交
617 618
struct hl_cs_job;

O
Ofir Bitton 已提交
619 620
/* Queue length of external and HW queues */
#define HL_QUEUE_LENGTH			4096
O
Oded Gabbay 已提交
621 622
#define HL_QUEUE_SIZE_IN_BYTES		(HL_QUEUE_LENGTH * HL_BD_SIZE)

623 624 625 626
#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 已提交
627
/* HL_CQ_LENGTH is in units of struct hl_cq_entry */
O
Oded Gabbay 已提交
628 629 630
#define HL_CQ_LENGTH			HL_QUEUE_LENGTH
#define HL_CQ_SIZE_IN_BYTES		(HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)

O
Ofir Bitton 已提交
631
/* Must be power of 2 */
632 633
#define HL_EQ_LENGTH			64
#define HL_EQ_SIZE_IN_BYTES		(HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
O
Oded Gabbay 已提交
634

O
Oded Gabbay 已提交
635
/* Host <-> CPU-CP shared memory size */
636
#define HL_CPU_ACCESSIBLE_MEM_SIZE	SZ_2M
O
Oded Gabbay 已提交
637 638

/**
639 640
 * struct hl_sync_stream_properties -
 *     describes a H/W queue sync stream properties
641
 * @hw_sob: array of the used H/W SOBs by this H/W queue.
642 643 644
 * @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.
645 646 647 648 649 650
 * @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.
651 652 653 654
 * @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 {
655 656 657 658 659 660 661 662
	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;
663 664 665 666
};

/**
 * struct hl_hw_queue - describes a H/W transport queue.
O
Oded Gabbay 已提交
667
 * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
668
 * @sync_stream_prop: sync stream queue properties
O
Oded Gabbay 已提交
669
 * @queue_type: type of queue.
670
 * @collective_mode: collective mode of current queue
O
Oded Gabbay 已提交
671 672 673 674 675
 * @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.
676 677
 * @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 已提交
678 679
 * @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
680
 *         exist).
681
 * @supports_sync_stream: True if queue supports sync stream
O
Oded Gabbay 已提交
682 683
 */
struct hl_hw_queue {
684 685 686
	struct hl_cs_job			**shadow_queue;
	struct hl_sync_stream_properties	sync_stream_prop;
	enum hl_queue_type			queue_type;
687
	enum hl_collective_mode			collective_mode;
688 689 690 691 692 693 694 695 696 697
	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 已提交
698 699 700 701 702 703 704
};

/**
 * 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
705
 * @cq_idx: completion queue index in array
O
Oded Gabbay 已提交
706 707 708 709 710 711 712
 * @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;
713
	void			*kernel_address;
O
Oded Gabbay 已提交
714
	dma_addr_t		bus_address;
715
	u32			cq_idx;
O
Oded Gabbay 已提交
716 717 718 719 720
	u32			hw_queue_id;
	u32			ci;
	u32			pi;
	atomic_t		free_slots_cnt;
};
721

722 723 724
/**
 * struct hl_user_interrupt - holds user interrupt information
 * @hdev: pointer to the device structure
725 726
 * @wait_list_head: head to the list of user threads pending on this interrupt
 * @wait_list_lock: protects wait_list_head
727 728 729 730
 * @interrupt_id: msix interrupt id
 */
struct hl_user_interrupt {
	struct hl_device	*hdev;
731 732
	struct list_head	wait_list_head;
	spinlock_t		wait_list_lock;
733 734 735
	u32			interrupt_id;
};

736 737 738 739 740 741 742 743 744 745 746
/**
 * struct hl_user_pending_interrupt - holds a context to a user thread
 *                                    pending on an interrupt
 * @wait_list_node: node in the list of user threads pending on an interrupt
 * @fence: hl fence object for interrupt completion
 */
struct hl_user_pending_interrupt {
	struct list_head	wait_list_node;
	struct hl_fence		fence;
};

747 748 749 750 751 752 753 754 755
/**
 * 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;
756
	void			*kernel_address;
757 758 759 760
	dma_addr_t		bus_address;
	u32			ci;
};

761

O
Oded Gabbay 已提交
762 763 764 765 766 767 768
/*
 * ASICs
 */

/**
 * enum hl_asic_type - supported ASIC types.
 * @ASIC_INVALID: Invalid ASIC type.
769
 * @ASIC_GOYA: Goya device.
770
 * @ASIC_GAUDI: Gaudi device.
771
 * @ASIC_GAUDI_SEC: Gaudi secured device (HL-2000).
O
Oded Gabbay 已提交
772 773
 */
enum hl_asic_type {
774
	ASIC_INVALID,
775
	ASIC_GOYA,
776 777
	ASIC_GAUDI,
	ASIC_GAUDI_SEC
O
Oded Gabbay 已提交
778 779
};

780 781
struct hl_cs_parser;

782 783
/**
 * enum hl_pm_mng_profile - power management profile.
784
 * @PM_AUTO: internal clock is set by the Linux driver.
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
 * @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
};

806 807 808 809 810 811 812 813 814
#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 已提交
815 816 817 818 819
/**
 * 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.
820 821
 * @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 已提交
822 823
 * @sw_init: sets up driver state, does not configure H/W.
 * @sw_fini: tears down driver state, does not configure H/W.
824 825
 * @hw_init: sets up the H/W state.
 * @hw_fini: tears down the H/W state.
826 827 828
 * @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 已提交
829 830
 * @suspend: handles IP specific H/W or SW changes for suspend.
 * @resume: handles IP specific H/W or SW changes for resume.
831
 * @cb_mmap: maps a CB.
O
Oded Gabbay 已提交
832
 * @ring_doorbell: increment PI on a given QMAN.
833 834 835 836 837
 * @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.
838 839 840 841 842 843 844 845
 * @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).
846
 * @scrub_device_mem: Scrub device memory given an address and size
O
Oded Gabbay 已提交
847 848
 * @get_int_queue_base: get the internal queue base address.
 * @test_queues: run simple test on all queues for sanity check.
849 850 851
 * @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 已提交
852 853
 * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
 * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
854 855 856 857 858
 * @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.
859
 * @update_eq_ci: update event queue CI.
860 861
 * @context_switch: called upon ASID context switch.
 * @restore_phase_topology: clear all SOBs amd MONs.
862 863 864 865 866 867
 * @debugfs_read32: debug interface for reading u32 from DRAM/SRAM/Host memory.
 * @debugfs_write32: debug interface for writing u32 to DRAM/SRAM/Host memory.
 * @debugfs_read64: debug interface for reading u64 from DRAM/SRAM/Host memory.
 * @debugfs_write64: debug interface for writing u64 to DRAM/SRAM/Host memory.
 * @debugfs_read_dma: debug interface for reading up to 2MB from the device's
 *                    internal memory via DMA engine.
868
 * @add_device_attr: add ASIC specific device attributes.
O
Oded Gabbay 已提交
869
 * @handle_eqe: handle event queue entry (IRQ) from CPU-CP.
870
 * @set_pll_profile: change PLL profile (manual/automatic).
871
 * @get_events_stat: retrieve event queue entries histogram.
872 873
 * @read_pte: read MMU page table entry from DRAM.
 * @write_pte: write MMU page table entry to DRAM.
874 875
 * @mmu_invalidate_cache: flush MMU STLB host/DRAM cache, either with soft
 *                        (L1 only) or hard (L0 & L1) flush.
876 877
 * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with
 *                              ASID-VA-size mask.
O
Oded Gabbay 已提交
878
 * @send_heartbeat: send is-alive packet to CPU-CP and verify response.
879 880 881
 * @set_clock_gating: enable/disable clock gating per engine according to
 *                    clock gating mask in hdev
 * @disable_clock_gating: disable clock gating completely
882
 * @debug_coresight: perform certain actions on Coresight for debugging.
883
 * @is_device_idle: return true if device is idle, false otherwise.
884
 * @soft_reset_late_init: perform certain actions needed after soft reset.
O
Oded Gabbay 已提交
885 886
 * @hw_queues_lock: acquire H/W queues lock.
 * @hw_queues_unlock: release H/W queues lock.
O
Oded Gabbay 已提交
887
 * @get_pci_id: retrieve PCI ID.
888
 * @get_eeprom_data: retrieve EEPROM data from F/W.
889 890 891 892 893
 * @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
894
 * @get_hw_state: retrieve the H/W state
895 896
 * @pci_bars_map: Map PCI BARs.
 * @init_iatu: Initialize the iATU unit inside the PCI controller.
897 898
 * @rreg: Read a register. Needed for simulator support.
 * @wreg: Write a register. Needed for simulator support.
899
 * @halt_coresight: stop the ETF and ETR traces.
900
 * @ctx_init: context dependent initialization.
901
 * @ctx_fini: context dependent cleanup.
902
 * @get_clk_rate: Retrieve the ASIC current and maximum clock rate in MHz
903
 * @get_queue_id_for_cq: Get the H/W queue id related to the given CQ index.
904 905 906
 * @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
907
 * @load_boot_fit_to_device: load boot fit to device's memory
908 909 910 911 912
 * @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.
913
 * @reset_sob_group: Reset SOB group
914 915
 * @set_dma_mask_from_fw: set the DMA mask in the driver according to the
 *                        firmware configuration
916
 * @get_device_time: Get the device time.
917 918 919
 * @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
920
 * @scramble_addr: Routine to scramble the address prior of mapping it
921
 *                 in the MMU.
922
 * @descramble_addr: Routine to de-scramble the address prior of
923
 *                   showing it to users.
924
 * @ack_protection_bits_errors: ack and dump all security violations
925
 * @get_hw_block_id: retrieve a HW block id to be used by the user to mmap it.
926 927
 *                   also returns the size of the block if caller supplies
 *                   a valid pointer for it
928
 * @hw_block_mmap: mmap a HW block with a given id.
929 930 931 932
 * @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
933 934 935
 * @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
O
Oded Gabbay 已提交
936 937 938 939
 */
struct hl_asic_funcs {
	int (*early_init)(struct hl_device *hdev);
	int (*early_fini)(struct hl_device *hdev);
940 941
	int (*late_init)(struct hl_device *hdev);
	void (*late_fini)(struct hl_device *hdev);
O
Oded Gabbay 已提交
942 943
	int (*sw_init)(struct hl_device *hdev);
	int (*sw_fini)(struct hl_device *hdev);
944 945
	int (*hw_init)(struct hl_device *hdev);
	void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
946
	void (*halt_engines)(struct hl_device *hdev, bool hard_reset);
O
Oded Gabbay 已提交
947 948
	int (*suspend)(struct hl_device *hdev);
	int (*resume)(struct hl_device *hdev);
949
	int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
950
			void *cpu_addr, dma_addr_t dma_addr, size_t size);
O
Oded Gabbay 已提交
951
	void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
952 953
	void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
			struct hl_bd *bd);
954
	void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
O
Oded Gabbay 已提交
955
					dma_addr_t *dma_handle, gfp_t flag);
956
	void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
O
Oded Gabbay 已提交
957
					void *cpu_addr, dma_addr_t dma_handle);
958
	int (*scrub_device_mem)(struct hl_device *hdev, u64 addr, u64 size);
O
Oded Gabbay 已提交
959 960 961
	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);
962
	void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
O
Oded Gabbay 已提交
963
				gfp_t mem_flags, dma_addr_t *dma_handle);
964
	void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
O
Oded Gabbay 已提交
965 966 967 968 969
				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);
970
	void (*hl_dma_unmap_sg)(struct hl_device *hdev,
971
				struct scatterlist *sgl, int nents,
972 973 974
				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,
975
				struct scatterlist *sgl, int nents,
976 977 978
				enum dma_data_direction dir);
	u32 (*get_dma_desc_list_size)(struct hl_device *hdev,
					struct sg_table *sgt);
979
	void (*add_end_of_cb_packets)(struct hl_device *hdev,
980
					void *kernel_address, u32 len,
981 982
					u64 cq_addr, u32 cq_val, u32 msix_num,
					bool eb);
983
	void (*update_eq_ci)(struct hl_device *hdev, u32 val);
984 985
	int (*context_switch)(struct hl_device *hdev, u32 asid);
	void (*restore_phase_topology)(struct hl_device *hdev);
986 987 988 989 990 991 992 993
	int (*debugfs_read32)(struct hl_device *hdev, u64 addr,
				bool user_address, u32 *val);
	int (*debugfs_write32)(struct hl_device *hdev, u64 addr,
				bool user_address, u32 val);
	int (*debugfs_read64)(struct hl_device *hdev, u64 addr,
				bool user_address, u64 *val);
	int (*debugfs_write64)(struct hl_device *hdev, u64 addr,
				bool user_address, u64 val);
994 995
	int (*debugfs_read_dma)(struct hl_device *hdev, u64 addr, u32 size,
				void *blob_addr);
996 997
	void (*add_device_attr)(struct hl_device *hdev,
				struct attribute_group *dev_attr_grp);
998 999
	void (*handle_eqe)(struct hl_device *hdev,
				struct hl_eq_entry *eq_entry);
1000 1001
	void (*set_pll_profile)(struct hl_device *hdev,
			enum hl_pll_frequency freq);
1002 1003
	void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
				u32 *size);
1004 1005
	u64 (*read_pte)(struct hl_device *hdev, u64 addr);
	void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
1006
	int (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard,
1007
					u32 flags);
1008
	int (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
1009
			u32 asid, u64 va, u64 size);
1010
	int (*send_heartbeat)(struct hl_device *hdev);
1011
	void (*set_clock_gating)(struct hl_device *hdev);
1012
	void (*disable_clock_gating)(struct hl_device *hdev);
1013
	int (*debug_coresight)(struct hl_device *hdev, void *data);
1014 1015
	bool (*is_device_idle)(struct hl_device *hdev, u64 *mask_arr,
					u8 mask_len, struct seq_file *s);
1016
	int (*soft_reset_late_init)(struct hl_device *hdev);
O
Oded Gabbay 已提交
1017 1018
	void (*hw_queues_lock)(struct hl_device *hdev);
	void (*hw_queues_unlock)(struct hl_device *hdev);
O
Oded Gabbay 已提交
1019
	u32 (*get_pci_id)(struct hl_device *hdev);
1020 1021
	int (*get_eeprom_data)(struct hl_device *hdev, void *data,
				size_t max_size);
O
Oded Gabbay 已提交
1022
	int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
1023
				u16 len, u32 timeout, u64 *result);
1024 1025
	int (*pci_bars_map)(struct hl_device *hdev);
	int (*init_iatu)(struct hl_device *hdev);
1026 1027
	u32 (*rreg)(struct hl_device *hdev, u32 reg);
	void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
1028
	void (*halt_coresight)(struct hl_device *hdev);
1029
	int (*ctx_init)(struct hl_ctx *ctx);
1030
	void (*ctx_fini)(struct hl_ctx *ctx);
1031
	int (*get_clk_rate)(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk);
1032
	u32 (*get_queue_id_for_cq)(struct hl_device *hdev, u32 cq_idx);
1033
	int (*read_device_fw_version)(struct hl_device *hdev,
1034 1035
					enum hl_fw_component fwc);
	int (*load_firmware_to_device)(struct hl_device *hdev);
1036
	int (*load_boot_fit_to_device)(struct hl_device *hdev);
1037 1038
	u32 (*get_signal_cb_size)(struct hl_device *hdev);
	u32 (*get_wait_cb_size)(struct hl_device *hdev);
1039
	u32 (*gen_signal_cb)(struct hl_device *hdev, void *data, u16 sob_id,
1040
			u32 size, bool eb);
1041
	u32 (*gen_wait_cb)(struct hl_device *hdev,
1042
			struct hl_gen_wait_properties *prop);
1043
	void (*reset_sob)(struct hl_device *hdev, void *data);
1044
	void (*reset_sob_group)(struct hl_device *hdev, u16 sob_group);
1045
	void (*set_dma_mask_from_fw)(struct hl_device *hdev);
1046
	u64 (*get_device_time)(struct hl_device *hdev);
1047 1048 1049 1050
	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);
1051 1052
	u64 (*scramble_addr)(struct hl_device *hdev, u64 addr);
	u64 (*descramble_addr)(struct hl_device *hdev, u64 addr);
1053
	void (*ack_protection_bits_errors)(struct hl_device *hdev);
1054
	int (*get_hw_block_id)(struct hl_device *hdev, u64 block_addr,
1055
				u32 *block_size, u32 *block_id);
1056 1057
	int (*hw_block_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
			u32 block_id, u32 block_size);
1058
	void (*enable_events_from_fw)(struct hl_device *hdev);
1059
	void (*get_msi_info)(u32 *table);
1060
	int (*map_pll_idx_to_fw_idx)(u32 pll_idx);
O
Oded Gabbay 已提交
1061
};
O
Oded Gabbay 已提交
1062

1063 1064 1065 1066 1067 1068 1069

/*
 * CONTEXTS
 */

#define HL_KERNEL_ASID_ID	0

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
/**
 * 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
};

1083 1084 1085 1086 1087 1088
/**
 * 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.
1089
 * @page_size: page size of this va range.
1090 1091 1092 1093 1094 1095
 */
struct hl_va_range {
	struct mutex		lock;
	struct list_head	list;
	u64			start_addr;
	u64			end_addr;
1096
	u32			page_size;
1097 1098
};

1099 1100 1101 1102 1103 1104 1105
/**
 * 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
1106
 * @validation_drop_cnt: dropped due to error in validation
1107 1108 1109 1110 1111 1112 1113
 */
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;
1114
	atomic64_t validation_drop_cnt;
1115 1116
};

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
/**
 * struct hl_pending_cb - pending command buffer structure
 * @cb_node: cb node in pending cb list
 * @cb: command buffer to send in next submission
 * @cb_size: command buffer size
 * @hw_queue_id: destination queue id
 */
struct hl_pending_cb {
	struct list_head	cb_node;
	struct hl_cb		*cb;
	u32			cb_size;
	u32			hw_queue_id;
};

1131 1132
/**
 * struct hl_ctx - user/kernel context.
1133 1134
 * @mem_hash: holds mapping from virtual address to virtual memory area
 *		descriptor (hl_vm_phys_pg_list or hl_userptr).
1135
 * @mmu_shadow_hash: holds a mapping from shadow address to pgt_info structure.
1136
 * @hpriv: pointer to the private (Kernel Driver) data of the process (fd).
1137 1138 1139
 * @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.
1140
 * @cs_pending: array of hl fence objects representing pending CS.
1141
 * @va_range: holds available virtual addresses for host and dram mappings.
1142
 * @mem_hash_lock: protects the mem_hash.
1143 1144
 * @mmu_lock: protects the MMU page tables. Any change to the PGT, modifying the
 *            MMU hash or walking the PGT requires talking this lock.
1145
 * @hw_block_list_lock: protects the HW block memory list.
O
Oded Gabbay 已提交
1146
 * @debugfs_list: node in debugfs list of contexts.
1147 1148
 * pending_cb_list: list of pending command buffers waiting to be sent upon
 *                  next user command submission context.
1149
 * @hw_block_mem_list: list of HW block virtual mapped addresses.
1150
 * @cs_counters: context command submission counters.
1151 1152
 * @cb_va_pool: device VA pool for command buffers which are mapped to the
 *              device's MMU.
1153 1154 1155
 * @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.
1156 1157
 * @dram_default_hops: array that holds all hops addresses needed for default
 *                     DRAM mapping.
1158
 * @pending_cb_lock: spinlock to protect pending cb list
1159
 * @cs_lock: spinlock to protect cs_sequence.
1160
 * @dram_phys_mem: amount of used physical DRAM memory by this context.
1161 1162 1163
 * @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.
1164 1165 1166 1167 1168
 * @thread_pending_cb_token: token to prevent multiple threads from processing
 *				the pending CB list. Only a single thread should
 *				process the list since it is protected by a
 *				spinlock and we don't want to halt the entire
 *				command submission sequence.
1169 1170 1171 1172
 * @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.
1173
 * @asid: context's unique address space ID in the device's MMU.
1174
 * @handle: context's opaque handle for user
1175 1176
 */
struct hl_ctx {
1177
	DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
1178
	DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
1179 1180 1181 1182
	struct hl_fpriv			*hpriv;
	struct hl_device		*hdev;
	struct kref			refcount;
	struct hl_fence			**cs_pending;
1183
	struct hl_va_range		*va_range[HL_VA_RANGE_TYPE_MAX];
1184 1185
	struct mutex			mem_hash_lock;
	struct mutex			mmu_lock;
1186
	struct mutex			hw_block_list_lock;
1187
	struct list_head		debugfs_list;
1188
	struct list_head		pending_cb_list;
1189
	struct list_head		hw_block_mem_list;
1190 1191 1192 1193
	struct hl_cs_counters_atomic	cs_counters;
	struct gen_pool			*cb_va_pool;
	u64				cs_sequence;
	u64				*dram_default_hops;
1194
	spinlock_t			pending_cb_lock;
1195 1196 1197
	spinlock_t			cs_lock;
	atomic64_t			dram_phys_mem;
	atomic_t			thread_ctx_switch_token;
1198
	atomic_t			thread_pending_cb_token;
1199 1200 1201
	u32				thread_ctx_switch_wait_token;
	u32				asid;
	u32				handle;
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
};

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


1215 1216 1217 1218 1219 1220 1221 1222 1223

/*
 * 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.
1224 1225
 * @pages: pointer to struct page array
 * @npages: size of @pages array
1226 1227 1228
 * @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.
1229
 * @addr: user-space virtual address of the start of the memory area.
1230 1231 1232 1233 1234 1235
 * @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;
1236 1237
	struct page		**pages;
	unsigned int		npages;
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	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.
1254 1255
 * @signal_fence: pointer to the fence object of the signal CS (used by wait
 *                CS only).
1256
 * @finish_work: workqueue object to run when CS is completed by H/W.
1257 1258
 * @work_tdr: delayed work node for TDR.
 * @mirror_node : node in device mirror list of command submissions.
1259
 * @staged_cs_node: node in the staged cs list.
O
Oded Gabbay 已提交
1260
 * @debugfs_list: node in debugfs list of command submissions.
1261
 * @sequence: the sequence number of this CS.
1262 1263
 * @staged_sequence: the sequence of the staged submission this CS is part of,
 *                   relevant only if staged_cs is set.
1264
 * @timeout_jiffies: cs timeout in jiffies.
1265
 * @type: CS_TYPE_*.
1266 1267 1268 1269 1270 1271
 * @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.
1272 1273 1274 1275 1276
 * @timestamp: true if a timestmap must be captured upon completion.
 * @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.
1277 1278
 */
struct hl_cs {
1279
	u16			*jobs_in_queue_cnt;
1280 1281 1282 1283
	struct hl_ctx		*ctx;
	struct list_head	job_list;
	spinlock_t		job_lock;
	struct kref		refcount;
1284 1285
	struct hl_fence		*fence;
	struct hl_fence		*signal_fence;
1286
	struct work_struct	finish_work;
1287 1288
	struct delayed_work	work_tdr;
	struct list_head	mirror_node;
1289
	struct list_head	staged_cs_node;
O
Oded Gabbay 已提交
1290
	struct list_head	debugfs_list;
1291
	u64			sequence;
1292
	u64			staged_sequence;
1293
	u64			timeout_jiffies;
1294
	enum hl_cs_type		type;
1295 1296 1297 1298 1299
	u8			submitted;
	u8			completed;
	u8			timedout;
	u8			tdr_active;
	u8			aborted;
1300
	u8			timestamp;
1301 1302 1303
	u8			staged_last;
	u8			staged_first;
	u8			staged_cs;
1304 1305
};

O
Oded Gabbay 已提交
1306 1307
/**
 * struct hl_cs_job - command submission job.
1308 1309 1310 1311 1312
 * @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 已提交
1313
 * @finish_work: workqueue object to run when job is completed.
1314 1315
 * @userptr_list: linked-list of userptr mappings that belong to this job and
 *			wait for completion.
O
Oded Gabbay 已提交
1316
 * @debugfs_list: node in debugfs list of command submission jobs.
1317
 * @refcount: reference counter for usage of the CS job.
T
Tomer Tayar 已提交
1318
 * @queue_type: the type of the H/W queue this job is submitted to.
O
Oded Gabbay 已提交
1319
 * @id: the id of this job inside a CS.
1320 1321 1322
 * @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 已提交
1323 1324 1325
 * @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).
1326 1327 1328 1329 1330 1331
 * @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 已提交
1332 1333
 */
struct hl_cs_job {
1334 1335 1336 1337
	struct list_head	cs_node;
	struct hl_cs		*cs;
	struct hl_cb		*user_cb;
	struct hl_cb		*patched_cb;
O
Oded Gabbay 已提交
1338
	struct work_struct	finish_work;
1339
	struct list_head	userptr_list;
O
Oded Gabbay 已提交
1340
	struct list_head	debugfs_list;
1341
	struct kref		refcount;
T
Tomer Tayar 已提交
1342
	enum hl_queue_type	queue_type;
O
Oded Gabbay 已提交
1343
	u32			id;
1344 1345 1346
	u32			hw_queue_id;
	u32			user_cb_size;
	u32			job_cb_size;
T
Tomer Tayar 已提交
1347
	u8			is_kernel_allocated_cb;
1348
	u8			contains_dma_pkt;
1349 1350 1351
};

/**
T
Tomer Tayar 已提交
1352
 * struct hl_cs_parser - command submission parser properties.
1353 1354 1355 1356 1357 1358
 * @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 已提交
1359
 * @queue_type: the type of the H/W queue this job is submitted to.
1360 1361 1362 1363 1364
 * @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 已提交
1365 1366 1367
 * @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).
1368 1369 1370 1371 1372 1373
 * @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.
1374
 * @completion: true if we need completion for this CS.
1375 1376 1377 1378 1379 1380
 */
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 已提交
1381
	enum hl_queue_type	queue_type;
1382 1383 1384 1385 1386
	u32			ctx_id;
	u32			hw_queue_id;
	u32			user_cb_size;
	u32			patched_cb_size;
	u8			job_id;
T
Tomer Tayar 已提交
1387
	u8			is_kernel_allocated_cb;
1388
	u8			contains_dma_pkt;
1389
	u8			completion;
O
Oded Gabbay 已提交
1390
};
1391

1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
/*
 * 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;
};

1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
/**
 * 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.
 * @size: size of the block.
 * @id: HW block id (handle).
 */
struct hl_vm_hw_block_list_node {
	struct list_head	node;
	struct hl_ctx		*ctx;
	unsigned long		vaddr;
	u32			size;
	u32			id;
};

1427 1428 1429 1430
/**
 * struct hl_vm_phys_pg_pack - physical page pack.
 * @vm_type: describes the type of the virtual area descriptor.
 * @pages: the physical page array.
1431 1432
 * @npages: num physical pages in the pack.
 * @total_size: total size of all the pages in this list.
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
 * @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;
1445 1446
	u64			npages;
	u64			total_size;
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
	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;
};

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510

/*
 * 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 已提交
1511 1512 1513 1514 1515 1516 1517 1518 1519
/*
 * 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.
1520
 * @ctx: current executing context. TODO: remove for multiple ctx per process
1521
 * @ctx_mgr: context manager to handle multiple context for this FD.
1522
 * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
O
Oded Gabbay 已提交
1523
 * @debugfs_list: list of relevant ASIC debugfs.
1524
 * @dev_node: node in the device list of file private data
O
Oded Gabbay 已提交
1525
 * @refcount: number of related contexts.
1526
 * @restore_phase_mutex: lock for context switch and restore phase.
1527
 * @is_control: true for control device, false otherwise
O
Oded Gabbay 已提交
1528 1529 1530 1531 1532
 */
struct hl_fpriv {
	struct hl_device	*hdev;
	struct file		*filp;
	struct pid		*taskpid;
1533
	struct hl_ctx		*ctx;
1534
	struct hl_ctx_mgr	ctx_mgr;
1535
	struct hl_cb_mgr	cb_mgr;
O
Oded Gabbay 已提交
1536
	struct list_head	debugfs_list;
1537
	struct list_head	dev_node;
O
Oded Gabbay 已提交
1538
	struct kref		refcount;
1539
	struct mutex		restore_phase_mutex;
1540
	u8			is_control;
O
Oded Gabbay 已提交
1541 1542 1543
};


O
Oded Gabbay 已提交
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 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
/*
 * 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.
 * @info_ent: dentry realted ops.
 * @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.
 * @ctx_mem_hash_spinlock: protects cb_list.
1588
 * @blob_desc: descriptor of blob
O
Oded Gabbay 已提交
1589 1590 1591 1592
 * @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.
1593 1594
 * @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.
O
Oded Gabbay 已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
 */
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;
1612
	struct debugfs_blob_wrapper	blob_desc;
O
Oded Gabbay 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621
	u64				addr;
	u64				mmu_addr;
	u32				mmu_asid;
	u8				i2c_bus;
	u8				i2c_addr;
	u8				i2c_reg;
};


O
Oded Gabbay 已提交
1622 1623 1624 1625
/*
 * DEVICES
 */

1626 1627 1628 1629
#define HL_STR_MAX	32

#define HL_DEV_STS_MAX (HL_DEVICE_STATUS_NEEDS_RESET + 1)

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

O
Oded Gabbay 已提交
1635 1636 1637 1638 1639 1640 1641
/*
 * Registers read & write functions.
 */

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

1642 1643
#define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
#define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
O
Oded Gabbay 已提交
1644
#define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n",	\
1645
			hdev->asic_funcs->rreg(hdev, (reg)))
O
Oded Gabbay 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656

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

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
#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 已提交
1667 1668
#define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
#define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
1669 1670 1671 1672
#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 已提交
1673

O
Oded Gabbay 已提交
1674 1675 1676
/* Timeout should be longer when working with simulator but cap the
 * increased timeout to some maximum
 */
1677 1678
#define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
({ \
1679 1680 1681 1682
	ktime_t __timeout; \
	if (hdev->pdev) \
		__timeout = ktime_add_us(ktime_get(), timeout_us); \
	else \
O
Oded Gabbay 已提交
1683 1684 1685
		__timeout = ktime_add_us(ktime_get(),\
				min((u64)(timeout_us * 10), \
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
	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; \
})

1701 1702 1703
/*
 * address in this macro points always to a memory location in the
 * host's (server's) memory. That location is updated asynchronously
1704 1705 1706 1707 1708 1709 1710 1711
 * 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.
1712
 */
1713 1714
#define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
				mem_written_by_device) \
1715 1716 1717 1718 1719
({ \
	ktime_t __timeout; \
	if (hdev->pdev) \
		__timeout = ktime_add_us(ktime_get(), timeout_us); \
	else \
O
Oded Gabbay 已提交
1720 1721 1722
		__timeout = ktime_add_us(ktime_get(),\
				min((u64)(timeout_us * 10), \
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
1723 1724 1725 1726
	might_sleep_if(sleep_us); \
	for (;;) { \
		/* Verify we read updates done by other cores or by device */ \
		mb(); \
1727
		(val) = *((u32 *)(addr)); \
1728
		if (mem_written_by_device) \
1729
			(val) = le32_to_cpu(*(__le32 *) &(val)); \
1730 1731 1732
		if (cond) \
			break; \
		if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
1733
			(val) = *((u32 *)(addr)); \
1734
			if (mem_written_by_device) \
1735
				(val) = le32_to_cpu(*(__le32 *) &(val)); \
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
			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 已提交
1751 1752 1753
		__timeout = ktime_add_us(ktime_get(),\
				min((u64)(timeout_us * 10), \
					(u64) HL_SIM_MAX_TIMEOUT_US)); \
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
	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; \
})
1768

1769 1770
struct hwmon_chip_info;

1771 1772
/**
 * struct hl_device_reset_work - reset workqueue task wrapper.
1773
 * @wq: work queue for device reset procedure.
1774 1775 1776 1777
 * @reset_work: reset work to be done.
 * @hdev: habanalabs device structure.
 */
struct hl_device_reset_work {
1778 1779
	struct workqueue_struct		*wq;
	struct delayed_work		reset_work;
1780 1781 1782
	struct hl_device		*hdev;
};

1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
/**
 * struct hr_mmu_hop_addrs - used for holding per-device host-resident mmu hop
 * information.
 * @virt_addr: the virtual address of the hop.
 * @phys-addr: the physical address of the hop (used by the device-mmu).
 * @shadow_addr: The shadow of the hop used by the driver for walking the hops.
 */
struct hr_mmu_hop_addrs {
	u64 virt_addr;
	u64 phys_addr;
	u64 shadow_addr;
};
1795 1796

/**
1797 1798
 * struct hl_mmu_hr_pgt_priv - used for holding per-device mmu host-resident
 * page-table internal information.
1799 1800 1801
 * @mmu_pgt_pool: pool of page tables used by MMU for allocating hops.
 * @mmu_shadow_hop0: shadow array of hop0 tables.
 */
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
struct hl_mmu_hr_priv {
	struct gen_pool *mmu_pgt_pool;
	struct hr_mmu_hop_addrs *mmu_shadow_hop0;
};

/**
 * 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 {
1814 1815 1816 1817
	struct gen_pool *mmu_pgt_pool;
	void *mmu_shadow_hop0;
};

1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
/**
 * 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;
};

1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
/**
 * 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.
1846 1847 1848
 * @scrambled_vaddr: The value of the virtual address after scrambling. This
 *                   address replaces the original virtual-address when mapped
 *                   in the MMU tables.
1849
 * @unscrambled_paddr: The un-scrambled physical address.
1850 1851
 * @hop_info: Array holding the per-hop information used for the translation.
 * @used_hops: The number of hops used for the translation.
1852
 * @range_type: virtual address range type.
1853 1854
 */
struct hl_mmu_hop_info {
1855
	u64 scrambled_vaddr;
1856
	u64 unscrambled_paddr;
1857 1858
	struct hl_mmu_per_hop_info hop_info[MMU_ARCH_5_HOPS];
	u32 used_hops;
1859
	enum hl_va_range_type range_type;
1860 1861
};

1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
/**
 * 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.
1873 1874 1875
 * @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.
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
 */
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);
1890 1891
	int (*get_tlb_info)(struct hl_ctx *ctx,
			u64 virt_addr, struct hl_mmu_hop_info *hops);
1892 1893
};

O
Oded Gabbay 已提交
1894 1895 1896
/**
 * struct hl_device - habanalabs device structure.
 * @pdev: pointer to PCI device, can be NULL in case of simulator device.
O
Ofir Bitton 已提交
1897 1898 1899
 * @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 已提交
1900
 * @rmmio: configuration area address on SRAM.
O
Oded Gabbay 已提交
1901
 * @cdev: related char device.
1902 1903 1904
 * @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
1905
 * @work_freq: delayed work to lower device frequency if possible.
O
Oded Gabbay 已提交
1906
 * @work_heartbeat: delayed work for CPU-CP is-alive check.
1907
 * @device_reset_work: delayed work which performs hard reset
O
Oded Gabbay 已提交
1908
 * @asic_name: ASIC specific name.
O
Oded Gabbay 已提交
1909
 * @asic_type: ASIC specific type.
O
Oded Gabbay 已提交
1910
 * @completion_queue: array of hl_cq.
1911 1912 1913 1914 1915 1916
 * @user_interrupt: array of hl_user_interrupt. upon the corresponding user
 *                  interrupt, driver will monitor the list of fences
 *                  registered to this interrupt.
 * @common_user_interrupt: common user interrupt for all user interrupts.
 *                         upon any user interrupt, driver will monitor the
 *                         list of fences registered to this common structure.
1917 1918
 * @cq_wq: work queues of completion queues for executing work in process
 *         context.
O
Oded Gabbay 已提交
1919
 * @eq_wq: work queue of event queue for executing work in process context.
1920
 * @sob_reset_wq: work queue for sob reset executions.
1921
 * @kernel_ctx: Kernel driver context structure.
O
Oded Gabbay 已提交
1922
 * @kernel_queues: array of hl_hw_queue.
1923 1924
 * @cs_mirror_list: CS mirror list for TDR.
 * @cs_mirror_lock: protects cs_mirror_list.
1925
 * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs.
O
Oded Gabbay 已提交
1926
 * @event_queue: event queue for IRQ from CPU-CP.
O
Oded Gabbay 已提交
1927
 * @dma_pool: DMA pool for small allocations.
O
Oded Gabbay 已提交
1928 1929 1930
 * @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.
1931 1932
 * @asid_bitmap: holds used/available ASIDs.
 * @asid_mutex: protects asid_bitmap.
O
Oded Gabbay 已提交
1933
 * @send_cpu_message_lock: enforces only one message in Host <-> CPU-CP queue.
1934
 * @debug_lock: protects critical section of setting debug mode for device
O
Oded Gabbay 已提交
1935 1936 1937
 * @asic_prop: ASIC specific immutable properties.
 * @asic_funcs: ASIC specific functions.
 * @asic_specific: ASIC specific information to use only from ASIC files.
1938
 * @vm: virtual memory manager for MMU.
1939 1940 1941
 * @hwmon_dev: H/W monitor device.
 * @pm_mng_profile: current power management profile.
 * @hl_chip_info: ASIC's sensors information.
1942
 * @device_status_description: device status description.
O
Oded Gabbay 已提交
1943
 * @hl_debugfs: device's debugfs manager.
1944 1945
 * @cb_pool: list of preallocated CBs.
 * @cb_pool_lock: protects the CB pool.
1946 1947 1948 1949
 * @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
1950 1951 1952
 * @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
1953
 * @compute_ctx: current compute context executing.
1954
 * @aggregated_cs_counters: aggregated cs counters among all contexts
1955 1956
 * @mmu_priv: device-specific MMU data.
 * @mmu_func: device-related MMU functions.
1957
 * @dram_used_mem: current DRAM memory consumption.
1958
 * @timeout_jiffies: device CS timeout value.
1959
 * @max_power: the max power of the device, as configured by the sysadmin. This
1960 1961
 *             value is saved so in case of hard-reset, the driver will restore
 *             this value and update the F/W after the re-initialization
1962 1963 1964
 * @clock_gating_mask: is clock gating enabled. bitmask that represents the
 *                     different engines. See debugfs-driver-habanalabs for
 *                     details.
1965
 * @in_reset: is device in reset flow.
1966
 * @curr_pll_profile: current PLL profile.
1967 1968
 * @card_type: Various ASICs have several card types. This indicates the card
 *             type of the current device.
1969
 * @major: habanalabs kernel driver major.
1970
 * @high_pll: high PLL profile frequency.
1971 1972
 * @soft_reset_cnt: number of soft reset since the driver was loaded.
 * @hard_reset_cnt: number of hard reset since the driver was loaded.
1973
 * @clk_throttling_reason: bitmask represents the current clk throttling reasons
O
Oded Gabbay 已提交
1974
 * @id: device minor.
1975
 * @id_control: minor of the control device
1976 1977
 * @cpu_pci_msb_addr: 50-bit extension bits for the device CPU's 40-bit
 *                    addresses.
O
Oded Gabbay 已提交
1978
 * @disabled: is device disabled.
1979 1980
 * @late_init_done: is late init stage was done during initialization.
 * @hwmon_initialized: is H/W monitor sensors was initialized.
1981
 * @hard_reset_pending: is there a hard reset work pending.
O
Oded Gabbay 已提交
1982
 * @heartbeat: is heartbeat sanity check towards CPU-CP enabled.
1983 1984
 * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
 *                   otherwise.
1985
 * @dram_default_page_mapping: is DRAM default page mapping enabled.
1986 1987
 * @memory_scrub: true to perform device memory scrub in various locations,
 *                such as context-switch, context close, page free, etc.
1988 1989
 * @pmmu_huge_range: is a different virtual addresses range used for PMMU with
 *                   huge pages.
1990
 * @init_done: is the initialization of the device done.
1991
 * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
1992
 * @dma_mask: the dma mask that was set for this device
1993
 * @in_debug: is device under debug. This, together with fpriv_list, enforces
1994
 *            that only a single user is configuring the debug infrastructure.
1995 1996
 * @power9_64bit_dma_enable: true to enable 64-bit DMA mask support. Relevant
 *                           only to POWER9 machines.
1997
 * @cdev_sysfs_created: were char devices and sysfs nodes created.
1998
 * @stop_on_err: true if engines should stop on error.
1999
 * @supports_sync_stream: is sync stream supported.
2000
 * @sync_stream_queue_idx: helper index for sync stream queues initialization.
2001
 * @collective_mon_idx: helper index for collective initialization
2002
 * @supports_coresight: is CoreSight supported.
2003
 * @supports_soft_reset: is soft reset supported.
2004
 * @supports_cb_mapping: is mapping a CB to the device's MMU supported.
2005 2006
 * @needs_reset: true if reset_on_lockup is false and device should be reset
 *               due to lockup.
2007 2008 2009 2010
 * @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
2011
 * @supports_staged_submission: true if staged submissions are supported
O
Oded Gabbay 已提交
2012 2013 2014
 */
struct hl_device {
	struct pci_dev			*pdev;
O
Ofir Bitton 已提交
2015 2016
	u64				pcie_bar_phys[HL_PCI_NUM_BARS];
	void __iomem			*pcie_bar[HL_PCI_NUM_BARS];
O
Oded Gabbay 已提交
2017
	void __iomem			*rmmio;
O
Oded Gabbay 已提交
2018
	struct cdev			cdev;
2019
	struct cdev			cdev_ctrl;
O
Oded Gabbay 已提交
2020
	struct device			*dev;
2021
	struct device			*dev_ctrl;
2022
	struct delayed_work		work_freq;
2023
	struct delayed_work		work_heartbeat;
2024
	struct hl_device_reset_work	device_reset_work;
2025 2026
	char				asic_name[HL_STR_MAX];
	char				status[HL_DEV_STS_MAX][HL_STR_MAX];
O
Oded Gabbay 已提交
2027
	enum hl_asic_type		asic_type;
O
Oded Gabbay 已提交
2028
	struct hl_cq			*completion_queue;
2029
	struct hl_user_interrupt	*user_interrupt;
2030
	struct hl_user_interrupt	common_user_interrupt;
2031
	struct workqueue_struct		**cq_wq;
2032
	struct workqueue_struct		*eq_wq;
2033
	struct workqueue_struct		*sob_reset_wq;
2034
	struct hl_ctx			*kernel_ctx;
O
Oded Gabbay 已提交
2035
	struct hl_hw_queue		*kernel_queues;
2036 2037
	struct list_head		cs_mirror_list;
	spinlock_t			cs_mirror_lock;
2038
	struct hl_cb_mgr		kernel_cb_mgr;
2039
	struct hl_eq			event_queue;
O
Oded Gabbay 已提交
2040 2041 2042 2043
	struct dma_pool			*dma_pool;
	void				*cpu_accessible_dma_mem;
	dma_addr_t			cpu_accessible_dma_address;
	struct gen_pool			*cpu_accessible_dma_pool;
2044 2045
	unsigned long			*asid_bitmap;
	struct mutex			asid_mutex;
O
Oded Gabbay 已提交
2046
	struct mutex			send_cpu_message_lock;
2047
	struct mutex			debug_lock;
O
Oded Gabbay 已提交
2048 2049 2050
	struct asic_fixed_properties	asic_prop;
	const struct hl_asic_funcs	*asic_funcs;
	void				*asic_specific;
2051
	struct hl_vm			vm;
2052 2053 2054
	struct device			*hwmon_dev;
	enum hl_pm_mng_profile		pm_mng_profile;
	struct hwmon_chip_info		*hl_chip_info;
2055

O
Oded Gabbay 已提交
2056 2057
	struct hl_dbg_device_entry	hl_debugfs;

2058 2059 2060
	struct list_head		cb_pool;
	spinlock_t			cb_pool_lock;

2061 2062 2063 2064 2065
	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;

2066 2067 2068
	struct list_head		fpriv_list;
	struct mutex			fpriv_list_lock;

2069
	struct hl_ctx			*compute_ctx;
2070

2071
	struct hl_cs_counters_atomic	aggregated_cs_counters;
2072

2073
	struct hl_mmu_priv		mmu_priv;
2074
	struct hl_mmu_funcs		mmu_func[MMU_NUM_PGT_LOCATIONS];
2075

2076
	atomic64_t			dram_used_mem;
2077 2078
	u64				timeout_jiffies;
	u64				max_power;
2079
	u64				clock_gating_mask;
2080
	atomic_t			in_reset;
2081
	enum hl_pll_frequency		curr_pll_profile;
2082
	enum cpucp_card_types		card_type;
O
Oded Gabbay 已提交
2083
	u32				major;
2084
	u32				high_pll;
2085 2086
	u32				soft_reset_cnt;
	u32				hard_reset_cnt;
2087
	u32				clk_throttling_reason;
O
Oded Gabbay 已提交
2088
	u16				id;
2089
	u16				id_control;
2090
	u16				cpu_pci_msb_addr;
O
Oded Gabbay 已提交
2091
	u8				disabled;
2092 2093
	u8				late_init_done;
	u8				hwmon_initialized;
2094 2095
	u8				hard_reset_pending;
	u8				heartbeat;
2096
	u8				reset_on_lockup;
2097
	u8				dram_default_page_mapping;
2098
	u8				memory_scrub;
2099
	u8				pmmu_huge_range;
2100
	u8				init_done;
2101
	u8				device_cpu_disabled;
2102
	u8				dma_mask;
2103
	u8				in_debug;
2104
	u8				power9_64bit_dma_enable;
2105
	u8				cdev_sysfs_created;
2106
	u8				stop_on_err;
2107
	u8				supports_sync_stream;
2108
	u8				sync_stream_queue_idx;
2109
	u8				collective_mon_idx;
2110
	u8				supports_coresight;
2111
	u8				supports_soft_reset;
2112
	u8				supports_cb_mapping;
2113
	u8				needs_reset;
2114 2115
	u8				process_kill_trial_cnt;
	u8				device_fini_pending;
2116
	u8				supports_staged_submission;
O
Oded Gabbay 已提交
2117 2118

	/* Parameters for bring-up */
2119
	u64				nic_ports_mask;
2120
	u64				fw_components;
2121
	u8				mmu_enable;
2122
	u8				mmu_huge_page_opt;
O
Oded Gabbay 已提交
2123
	u8				reset_pcilink;
O
Oded Gabbay 已提交
2124
	u8				cpu_queues_enable;
2125
	u8				pldm;
2126 2127 2128 2129 2130 2131
	u8				axi_drain;
	u8				sram_scrambler_enable;
	u8				dram_scrambler_enable;
	u8				hard_reset_on_fw_events;
	u8				bmc_enable;
	u8				rl_enable;
2132
	u8				reset_on_preboot_fail;
2133
	u8				reset_upon_device_release;
O
Oded Gabbay 已提交
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
};


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

2166 2167 2168 2169 2170 2171 2172 2173 2174
/**
 * 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.
 */
2175
static inline bool hl_mem_area_inside_range(u64 address, u64 size,
2176 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
				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 已提交
2218
int hl_device_open(struct inode *inode, struct file *filp);
2219
int hl_device_open_ctrl(struct inode *inode, struct file *filp);
2220 2221
bool hl_device_operational(struct hl_device *hdev,
		enum hl_device_status *status);
2222
enum hl_device_status hl_device_status(struct hl_device *hdev);
2223
int hl_device_set_debug_mode(struct hl_device *hdev, bool enable);
O
Oded Gabbay 已提交
2224 2225 2226
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 已提交
2227 2228 2229 2230
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);
2231
int hl_hw_queue_schedule_cs(struct hl_cs *cs);
O
Oded Gabbay 已提交
2232 2233
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);
2234
void hl_hw_queue_update_ci(struct hl_cs *cs);
2235
void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
O
Oded Gabbay 已提交
2236 2237 2238 2239 2240 2241

#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);
2242 2243
int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
2244 2245
void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
2246 2247
irqreturn_t hl_irq_handler_cq(int irq, void *arg);
irqreturn_t hl_irq_handler_eq(int irq, void *arg);
2248 2249
irqreturn_t hl_irq_handler_user_cq(int irq, void *arg);
irqreturn_t hl_irq_handler_default(int irq, void *arg);
2250 2251
u32 hl_cq_inc_ptr(u32 ptr);

2252 2253 2254 2255 2256 2257 2258 2259
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);
2260 2261
void hl_ctx_do_release(struct kref *ref);
void hl_ctx_get(struct hl_device *hdev,	struct hl_ctx *ctx);
2262
int hl_ctx_put(struct hl_ctx *ctx);
2263
struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
2264 2265
void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
2266

O
Oded Gabbay 已提交
2267 2268 2269 2270
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);
2271
int hl_device_reset(struct hl_device *hdev, u32 flags);
2272
void hl_hpriv_get(struct hl_fpriv *hpriv);
2273
int hl_hpriv_put(struct hl_fpriv *hpriv);
2274
int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq);
2275
int hl_device_utilization(struct hl_device *hdev, u32 *utilization);
2276

2277
int hl_build_hwmon_channel_info(struct hl_device *hdev,
2278
		struct cpucp_sensor *sensors_arr);
2279 2280 2281 2282 2283 2284

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 已提交
2285

2286 2287
int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
			struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
2288
			bool map_cb, u64 *handle);
2289 2290
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);
2291
int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
2292 2293 2294 2295 2296
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);
2297 2298
struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
					bool internal_cb);
2299 2300
int hl_cb_pool_init(struct hl_device *hdev);
int hl_cb_pool_fini(struct hl_device *hdev);
2301 2302
int hl_cb_va_pool_init(struct hl_ctx *ctx);
void hl_cb_va_pool_fini(struct hl_ctx *ctx);
2303

2304
void hl_cs_rollback_all(struct hl_device *hdev);
2305
void hl_pending_cb_list_flush(struct hl_ctx *ctx);
T
Tomer Tayar 已提交
2306 2307
struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
		enum hl_queue_type queue_type, bool is_kernel_allocated_cb);
2308
void hl_sob_reset_error(struct kref *ref);
2309
int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask);
2310 2311
void hl_fence_put(struct hl_fence *fence);
void hl_fence_get(struct hl_fence *fence);
2312
void cs_get(struct hl_cs *cs);
2313 2314
bool cs_needs_completion(struct hl_cs *cs);
bool cs_needs_timeout(struct hl_cs *cs);
2315 2316
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);
2317

O
Oded Gabbay 已提交
2318
void goya_set_asic_funcs(struct hl_device *hdev);
2319
void gaudi_set_asic_funcs(struct hl_device *hdev);
O
Oded Gabbay 已提交
2320

2321 2322 2323 2324 2325 2326
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);

2327 2328 2329
void hl_hw_block_mem_init(struct hl_ctx *ctx);
void hl_hw_block_mem_fini(struct hl_ctx *ctx);

2330
u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
2331
		enum hl_va_range_type type, u32 size, u32 alignment);
2332 2333
int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
		u64 start_addr, u64 size);
2334
int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
2335
			struct hl_userptr *userptr);
2336
void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
2337 2338 2339 2340 2341 2342
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);

2343 2344
int hl_mmu_init(struct hl_device *hdev);
void hl_mmu_fini(struct hl_device *hdev);
2345
int hl_mmu_ctx_init(struct hl_ctx *ctx);
2346
void hl_mmu_ctx_fini(struct hl_ctx *ctx);
2347
int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
2348
		u32 page_size, bool flush_pte);
2349
int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
2350
		bool flush_pte);
2351 2352 2353
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);
2354 2355
void hl_mmu_swap_out(struct hl_ctx *ctx);
void hl_mmu_swap_in(struct hl_ctx *ctx);
2356
int hl_mmu_if_set_funcs(struct hl_device *hdev);
2357
void hl_mmu_v1_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
2358 2359 2360
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);
2361 2362
u64 hl_mmu_scramble_addr(struct hl_device *hdev, u64 addr);
u64 hl_mmu_descramble_addr(struct hl_device *hdev, u64 addr);
2363
bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr);
2364

2365
int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
O
Ofir Bitton 已提交
2366
				void __iomem *dst, u32 src_offset, u32 size);
2367 2368
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,
2369
				u16 len, u32 timeout, u64 *result);
2370 2371 2372
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);
2373 2374 2375 2376 2377 2378
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);
2379
int hl_fw_cpucp_info_get(struct hl_device *hdev,
2380 2381
			u32 cpu_security_boot_status_reg,
			u32 boot_err0_reg);
2382 2383 2384
int hl_fw_cpucp_handshake(struct hl_device *hdev,
			u32 cpu_security_boot_status_reg,
			u32 boot_err0_reg);
2385
int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
2386
int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
2387
		struct hl_info_pci_counters *counters);
2388
int hl_fw_cpucp_total_energy_get(struct hl_device *hdev,
2389
			u64 *total_energy);
2390
int get_used_pll_index(struct hl_device *hdev, u32 input_pll_index,
2391
						enum pll_index *pll_index);
2392
int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
2393
		u16 *pll_freq_arr);
2394
int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power);
2395
int hl_fw_init_cpu(struct hl_device *hdev, u32 cpu_boot_status_reg,
2396
			u32 msg_to_cpu_reg, u32 cpu_msg_status_reg,
2397 2398 2399 2400 2401
			u32 cpu_security_boot_status_reg, u32 boot_err0_reg,
			bool skip_bmc, u32 cpu_timeout, u32 boot_fit_timeout);
int hl_fw_read_preboot_status(struct hl_device *hdev, u32 cpu_boot_status_reg,
		u32 cpu_security_boot_status_reg, u32 boot_err0_reg,
		u32 timeout);
2402

2403 2404
int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
			bool is_wc[3]);
2405
int hl_pci_elbi_read(struct hl_device *hdev, u64 addr, u32 *data);
2406
int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
O
Ofir Bitton 已提交
2407 2408 2409 2410
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);
2411
int hl_pci_init(struct hl_device *hdev);
2412 2413
void hl_pci_fini(struct hl_device *hdev);

2414
long hl_get_frequency(struct hl_device *hdev, u32 pll_index,
2415
								bool curr);
2416
void hl_set_frequency(struct hl_device *hdev, u32 pll_index,
2417
								u64 freq);
2418 2419
int hl_get_temperature(struct hl_device *hdev,
		       int sensor_index, u32 attr, long *value);
2420
int hl_set_temperature(struct hl_device *hdev,
2421 2422 2423 2424 2425 2426 2427 2428 2429
		       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);
2430 2431 2432
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);
2433
void hl_set_max_power(struct hl_device *hdev);
2434 2435 2436 2437
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);
2438
void hl_release_pending_user_interrupts(struct hl_device *hdev);
2439

O
Oded Gabbay 已提交
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 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 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
#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

2534 2535
/* IOCTLs */
long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
2536
long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
2537
int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
2538
int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
2539
int hl_wait_ioctl(struct hl_fpriv *hpriv, void *data);
2540
int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
2541

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