1 /* SPDX-License-Identifier: GPL-2.0
2 *
3 * Copyright 2016-2019 HabanaLabs, Ltd.
4 * All Rights Reserved.
5 *
6 */
7
8 #ifndef HABANALABSP_H_
9 #define HABANALABSP_H_
10
11 #include "../include/common/cpucp_if.h"
12 #include "../include/common/qman_if.h"
13 #include "../include/hw_ip/mmu/mmu_general.h"
14 #include <uapi/misc/habanalabs.h>
15
16 #include <linux/cdev.h>
17 #include <linux/iopoll.h>
18 #include <linux/irqreturn.h>
19 #include <linux/dma-direction.h>
20 #include <linux/scatterlist.h>
21 #include <linux/hashtable.h>
22 #include <linux/debugfs.h>
23 #include <linux/rwsem.h>
24 #include <linux/bitfield.h>
25 #include <linux/genalloc.h>
26 #include <linux/sched/signal.h>
27 #include <linux/io-64-nonatomic-lo-hi.h>
28 #include <linux/coresight.h>
29 #include <linux/dma-buf.h>
30
31 #define HL_NAME "habanalabs"
32
33 /* Use upper bits of mmap offset to store habana driver specific information.
34 * bits[63:61] - Encode mmap type
35 * bits[45:0] - mmap offset value
36 *
37 * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
38 * defines are w.r.t to PAGE_SIZE
39 */
40 #define HL_MMAP_TYPE_SHIFT (61 - PAGE_SHIFT)
41 #define HL_MMAP_TYPE_MASK (0x7ull << HL_MMAP_TYPE_SHIFT)
42 #define HL_MMAP_TYPE_BLOCK (0x4ull << HL_MMAP_TYPE_SHIFT)
43 #define HL_MMAP_TYPE_CB (0x2ull << HL_MMAP_TYPE_SHIFT)
44
45 #define HL_MMAP_OFFSET_VALUE_MASK (0x1FFFFFFFFFFFull >> PAGE_SHIFT)
46 #define HL_MMAP_OFFSET_VALUE_GET(off) (off & HL_MMAP_OFFSET_VALUE_MASK)
47
48 #define HL_PENDING_RESET_PER_SEC 10
49 #define HL_PENDING_RESET_MAX_TRIALS 60 /* 10 minutes */
50 #define HL_PENDING_RESET_LONG_SEC 60
51
52 #define HL_HARD_RESET_MAX_TIMEOUT 120
53 #define HL_PLDM_HARD_RESET_MAX_TIMEOUT (HL_HARD_RESET_MAX_TIMEOUT * 3)
54
55 #define HL_DEVICE_TIMEOUT_USEC 1000000 /* 1 s */
56
57 #define HL_HEARTBEAT_PER_USEC 5000000 /* 5 s */
58
59 #define HL_PLL_LOW_JOB_FREQ_USEC 5000000 /* 5 s */
60
61 #define HL_CPUCP_INFO_TIMEOUT_USEC 10000000 /* 10s */
62 #define HL_CPUCP_EEPROM_TIMEOUT_USEC 10000000 /* 10s */
63
64 #define HL_PCI_ELBI_TIMEOUT_MSEC 10 /* 10ms */
65
66 #define HL_SIM_MAX_TIMEOUT_US 10000000 /* 10s */
67
68 #define HL_COMMON_USER_INTERRUPT_ID 0xFFF
69
70 #define HL_STATE_DUMP_HIST_LEN 5
71
72 /* Default value for device reset trigger , an invalid value */
73 #define HL_RESET_TRIGGER_DEFAULT 0xFF
74
75 #define OBJ_NAMES_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
76 #define SYNC_TO_ENGINE_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
77
78 /* Memory */
79 #define MEM_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
80
81 /* MMU */
82 #define MMU_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
83
84 /**
85 * enum hl_mmu_page_table_locaion - mmu page table location
86 * @MMU_DR_PGT: page-table is located on device DRAM.
87 * @MMU_HR_PGT: page-table is located on host memory.
88 * @MMU_NUM_PGT_LOCATIONS: number of page-table locations currently supported.
89 */
90 enum hl_mmu_page_table_location {
91 MMU_DR_PGT = 0, /* device-dram-resident MMU PGT */
92 MMU_HR_PGT, /* host resident MMU PGT */
93 MMU_NUM_PGT_LOCATIONS /* num of PGT locations */
94 };
95
96 /*
97 * HL_RSVD_SOBS 'sync stream' reserved sync objects per QMAN stream
98 * HL_RSVD_MONS 'sync stream' reserved monitors per QMAN stream
99 */
100 #define HL_RSVD_SOBS 2
101 #define HL_RSVD_MONS 1
102
103 /*
104 * HL_COLLECTIVE_RSVD_MSTR_MONS 'collective' reserved monitors per QMAN stream
105 */
106 #define HL_COLLECTIVE_RSVD_MSTR_MONS 2
107
108 #define HL_MAX_SOB_VAL (1 << 15)
109
110 #define IS_POWER_OF_2(n) (n != 0 && ((n & (n - 1)) == 0))
111 #define IS_MAX_PENDING_CS_VALID(n) (IS_POWER_OF_2(n) && (n > 1))
112
113 #define HL_PCI_NUM_BARS 6
114
115 #define HL_MAX_DCORES 4
116
117 /*
118 * Reset Flags
119 *
120 * - HL_RESET_HARD
121 * If set do hard reset to all engines. If not set reset just
122 * compute/DMA engines.
123 *
124 * - HL_RESET_FROM_RESET_THREAD
125 * Set if the caller is the hard-reset thread
126 *
127 * - HL_RESET_HEARTBEAT
128 * Set if reset is due to heartbeat
129 *
130 * - HL_RESET_TDR
131 * Set if reset is due to TDR
132 *
133 * - HL_RESET_DEVICE_RELEASE
134 * Set if reset is due to device release
135 *
136 * - HL_RESET_FW
137 * F/W will perform the reset. No need to ask it to reset the device. This is relevant
138 * only when running with secured f/w
139 *
140 * - HL_RESET_FW_FATAL_ERR
141 * Set if reset is due to a fatal error from FW
142 */
143
144 #define HL_RESET_HARD (1 << 0)
145 #define HL_RESET_FROM_RESET_THREAD (1 << 1)
146 #define HL_RESET_HEARTBEAT (1 << 2)
147 #define HL_RESET_TDR (1 << 3)
148 #define HL_RESET_DEVICE_RELEASE (1 << 4)
149 #define HL_RESET_FW (1 << 5)
150 #define HL_RESET_FW_FATAL_ERR (1 << 6)
151
152 #define HL_MAX_SOBS_PER_MONITOR 8
153
154 /**
155 * struct hl_gen_wait_properties - properties for generating a wait CB
156 * @data: command buffer
157 * @q_idx: queue id is used to extract fence register address
158 * @size: offset in command buffer
159 * @sob_base: SOB base to use in this wait CB
160 * @sob_val: SOB value to wait for
161 * @mon_id: monitor to use in this wait CB
162 * @sob_mask: each bit represents a SOB offset from sob_base to be used
163 */
164 struct hl_gen_wait_properties {
165 void *data;
166 u32 q_idx;
167 u32 size;
168 u16 sob_base;
169 u16 sob_val;
170 u16 mon_id;
171 u8 sob_mask;
172 };
173
174 /**
175 * struct pgt_info - MMU hop page info.
176 * @node: hash linked-list node for the pgts shadow hash of pgts.
177 * @phys_addr: physical address of the pgt.
178 * @shadow_addr: shadow hop in the host.
179 * @ctx: pointer to the owner ctx.
180 * @num_of_ptes: indicates how many ptes are used in the pgt.
181 *
182 * The MMU page tables hierarchy is placed on the DRAM. When a new level (hop)
183 * is needed during mapping, a new page is allocated and this structure holds
184 * its essential information. During unmapping, if no valid PTEs remained in the
185 * page, it is freed with its pgt_info structure.
186 */
187 struct pgt_info {
188 struct hlist_node node;
189 u64 phys_addr;
190 u64 shadow_addr;
191 struct hl_ctx *ctx;
192 int num_of_ptes;
193 };
194
195 struct hl_device;
196 struct hl_fpriv;
197
198 /**
199 * enum hl_pci_match_mode - pci match mode per region
200 * @PCI_ADDRESS_MATCH_MODE: address match mode
201 * @PCI_BAR_MATCH_MODE: bar match mode
202 */
203 enum hl_pci_match_mode {
204 PCI_ADDRESS_MATCH_MODE,
205 PCI_BAR_MATCH_MODE
206 };
207
208 /**
209 * enum hl_fw_component - F/W components to read version through registers.
210 * @FW_COMP_BOOT_FIT: boot fit.
211 * @FW_COMP_PREBOOT: preboot.
212 * @FW_COMP_LINUX: linux.
213 */
214 enum hl_fw_component {
215 FW_COMP_BOOT_FIT,
216 FW_COMP_PREBOOT,
217 FW_COMP_LINUX,
218 };
219
220 /**
221 * enum hl_fw_types - F/W types present in the system
222 * @FW_TYPE_LINUX: Linux image for device CPU
223 * @FW_TYPE_BOOT_CPU: Boot image for device CPU
224 * @FW_TYPE_PREBOOT_CPU: Indicates pre-loaded CPUs are present in the system
225 * (preboot, ppboot etc...)
226 * @FW_TYPE_ALL_TYPES: Mask for all types
227 */
228 enum hl_fw_types {
229 FW_TYPE_LINUX = 0x1,
230 FW_TYPE_BOOT_CPU = 0x2,
231 FW_TYPE_PREBOOT_CPU = 0x4,
232 FW_TYPE_ALL_TYPES =
233 (FW_TYPE_LINUX | FW_TYPE_BOOT_CPU | FW_TYPE_PREBOOT_CPU)
234 };
235
236 /**
237 * enum hl_queue_type - Supported QUEUE types.
238 * @QUEUE_TYPE_NA: queue is not available.
239 * @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the
240 * host.
241 * @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's
242 * memories and/or operates the compute engines.
243 * @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU.
244 * @QUEUE_TYPE_HW: queue of DMA and compute engines jobs, for which completion
245 * notifications are sent by H/W.
246 */
247 enum hl_queue_type {
248 QUEUE_TYPE_NA,
249 QUEUE_TYPE_EXT,
250 QUEUE_TYPE_INT,
251 QUEUE_TYPE_CPU,
252 QUEUE_TYPE_HW
253 };
254
255 enum hl_cs_type {
256 CS_TYPE_DEFAULT,
257 CS_TYPE_SIGNAL,
258 CS_TYPE_WAIT,
259 CS_TYPE_COLLECTIVE_WAIT,
260 CS_RESERVE_SIGNALS,
261 CS_UNRESERVE_SIGNALS
262 };
263
264 /*
265 * struct hl_inbound_pci_region - inbound region descriptor
266 * @mode: pci match mode for this region
267 * @addr: region target address
268 * @size: region size in bytes
269 * @offset_in_bar: offset within bar (address match mode)
270 * @bar: bar id
271 */
272 struct hl_inbound_pci_region {
273 enum hl_pci_match_mode mode;
274 u64 addr;
275 u64 size;
276 u64 offset_in_bar;
277 u8 bar;
278 };
279
280 /*
281 * struct hl_outbound_pci_region - outbound region descriptor
282 * @addr: region target address
283 * @size: region size in bytes
284 */
285 struct hl_outbound_pci_region {
286 u64 addr;
287 u64 size;
288 };
289
290 /*
291 * enum queue_cb_alloc_flags - Indicates queue support for CBs that
292 * allocated by Kernel or by User
293 * @CB_ALLOC_KERNEL: support only CBs that allocated by Kernel
294 * @CB_ALLOC_USER: support only CBs that allocated by User
295 */
296 enum queue_cb_alloc_flags {
297 CB_ALLOC_KERNEL = 0x1,
298 CB_ALLOC_USER = 0x2
299 };
300
301 /*
302 * struct hl_hw_sob - H/W SOB info.
303 * @hdev: habanalabs device structure.
304 * @kref: refcount of this SOB. The SOB will reset once the refcount is zero.
305 * @sob_id: id of this SOB.
306 * @sob_addr: the sob offset from the base address.
307 * @q_idx: the H/W queue that uses this SOB.
308 * @need_reset: reset indication set when switching to the other sob.
309 */
310 struct hl_hw_sob {
311 struct hl_device *hdev;
312 struct kref kref;
313 u32 sob_id;
314 u32 sob_addr;
315 u32 q_idx;
316 bool need_reset;
317 };
318
319 enum hl_collective_mode {
320 HL_COLLECTIVE_NOT_SUPPORTED = 0x0,
321 HL_COLLECTIVE_MASTER = 0x1,
322 HL_COLLECTIVE_SLAVE = 0x2
323 };
324
325 /**
326 * struct hw_queue_properties - queue information.
327 * @type: queue type.
328 * @queue_cb_alloc_flags: bitmap which indicates if the hw queue supports CB
329 * that allocated by the Kernel driver and therefore,
330 * a CB handle can be provided for jobs on this queue.
331 * Otherwise, a CB address must be provided.
332 * @collective_mode: collective mode of current queue
333 * @driver_only: true if only the driver is allowed to send a job to this queue,
334 * false otherwise.
335 * @supports_sync_stream: True if queue supports sync stream
336 */
337 struct hw_queue_properties {
338 enum hl_queue_type type;
339 enum queue_cb_alloc_flags cb_alloc_flags;
340 enum hl_collective_mode collective_mode;
341 u8 driver_only;
342 u8 supports_sync_stream;
343 };
344
345 /**
346 * enum vm_type - virtual memory mapping request information.
347 * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
348 * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
349 */
350 enum vm_type {
351 VM_TYPE_USERPTR = 0x1,
352 VM_TYPE_PHYS_PACK = 0x2
353 };
354
355 /**
356 * enum hl_device_hw_state - H/W device state. use this to understand whether
357 * to do reset before hw_init or not
358 * @HL_DEVICE_HW_STATE_CLEAN: H/W state is clean. i.e. after hard reset
359 * @HL_DEVICE_HW_STATE_DIRTY: H/W state is dirty. i.e. we started to execute
360 * hw_init
361 */
362 enum hl_device_hw_state {
363 HL_DEVICE_HW_STATE_CLEAN = 0,
364 HL_DEVICE_HW_STATE_DIRTY
365 };
366
367 #define HL_MMU_VA_ALIGNMENT_NOT_NEEDED 0
368
369 /**
370 * struct hl_mmu_properties - ASIC specific MMU address translation properties.
371 * @start_addr: virtual start address of the memory region.
372 * @end_addr: virtual end address of the memory region.
373 * @hop0_shift: shift of hop 0 mask.
374 * @hop1_shift: shift of hop 1 mask.
375 * @hop2_shift: shift of hop 2 mask.
376 * @hop3_shift: shift of hop 3 mask.
377 * @hop4_shift: shift of hop 4 mask.
378 * @hop5_shift: shift of hop 5 mask.
379 * @hop0_mask: mask to get the PTE address in hop 0.
380 * @hop1_mask: mask to get the PTE address in hop 1.
381 * @hop2_mask: mask to get the PTE address in hop 2.
382 * @hop3_mask: mask to get the PTE address in hop 3.
383 * @hop4_mask: mask to get the PTE address in hop 4.
384 * @hop5_mask: mask to get the PTE address in hop 5.
385 * @page_size: default page size used to allocate memory.
386 * @num_hops: The amount of hops supported by the translation table.
387 * @host_resident: Should the MMU page table reside in host memory or in the
388 * device DRAM.
389 */
390 struct hl_mmu_properties {
391 u64 start_addr;
392 u64 end_addr;
393 u64 hop0_shift;
394 u64 hop1_shift;
395 u64 hop2_shift;
396 u64 hop3_shift;
397 u64 hop4_shift;
398 u64 hop5_shift;
399 u64 hop0_mask;
400 u64 hop1_mask;
401 u64 hop2_mask;
402 u64 hop3_mask;
403 u64 hop4_mask;
404 u64 hop5_mask;
405 u32 page_size;
406 u32 num_hops;
407 u8 host_resident;
408 };
409
410 /**
411 * struct hl_hints_range - hint addresses reserved va range.
412 * @start_addr: start address of the va range.
413 * @end_addr: end address of the va range.
414 */
415 struct hl_hints_range {
416 u64 start_addr;
417 u64 end_addr;
418 };
419
420 /**
421 * struct asic_fixed_properties - ASIC specific immutable properties.
422 * @hw_queues_props: H/W queues properties.
423 * @cpucp_info: received various information from CPU-CP regarding the H/W, e.g.
424 * available sensors.
425 * @uboot_ver: F/W U-boot version.
426 * @preboot_ver: F/W Preboot version.
427 * @dmmu: DRAM MMU address translation properties.
428 * @pmmu: PCI (host) MMU address translation properties.
429 * @pmmu_huge: PCI (host) MMU address translation properties for memory
430 * allocated with huge pages.
431 * @hints_dram_reserved_va_range: dram hint addresses reserved range.
432 * @hints_host_reserved_va_range: host hint addresses reserved range.
433 * @hints_host_hpage_reserved_va_range: host huge page hint addresses reserved
434 * range.
435 * @sram_base_address: SRAM physical start address.
436 * @sram_end_address: SRAM physical end address.
437 * @sram_user_base_address - SRAM physical start address for user access.
438 * @dram_base_address: DRAM physical start address.
439 * @dram_end_address: DRAM physical end address.
440 * @dram_user_base_address: DRAM physical start address for user access.
441 * @dram_size: DRAM total size.
442 * @dram_pci_bar_size: size of PCI bar towards DRAM.
443 * @max_power_default: max power of the device after reset
444 * @dc_power_default: power consumed by the device in mode idle.
445 * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
446 * fault.
447 * @pcie_dbi_base_address: Base address of the PCIE_DBI block.
448 * @pcie_aux_dbi_reg_addr: Address of the PCIE_AUX DBI register.
449 * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
450 * @mmu_dram_default_page_addr: DRAM default page physical address.
451 * @cb_va_start_addr: virtual start address of command buffers which are mapped
452 * to the device's MMU.
453 * @cb_va_end_addr: virtual end address of command buffers which are mapped to
454 * the device's MMU.
455 * @dram_hints_align_mask: dram va hint addresses alignment mask which is used
456 * for hints validity check.
457 * device_dma_offset_for_host_access: the offset to add to host DMA addresses
458 * to enable the device to access them.
459 * @max_freq_value: current max clk frequency.
460 * @clk_pll_index: clock PLL index that specify which PLL determines the clock
461 * we display to the user
462 * @mmu_pgt_size: MMU page tables total size.
463 * @mmu_pte_size: PTE size in MMU page tables.
464 * @mmu_hop_table_size: MMU hop table size.
465 * @mmu_hop0_tables_total_size: total size of MMU hop0 tables.
466 * @dram_page_size: page size for MMU DRAM allocation.
467 * @cfg_size: configuration space size on SRAM.
468 * @sram_size: total size of SRAM.
469 * @max_asid: maximum number of open contexts (ASIDs).
470 * @num_of_events: number of possible internal H/W IRQs.
471 * @psoc_pci_pll_nr: PCI PLL NR value.
472 * @psoc_pci_pll_nf: PCI PLL NF value.
473 * @psoc_pci_pll_od: PCI PLL OD value.
474 * @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value.
475 * @psoc_timestamp_frequency: frequency of the psoc timestamp clock.
476 * @high_pll: high PLL frequency used by the device.
477 * @cb_pool_cb_cnt: number of CBs in the CB pool.
478 * @cb_pool_cb_size: size of each CB in the CB pool.
479 * @max_pending_cs: maximum of concurrent pending command submissions
480 * @max_queues: maximum amount of queues in the system
481 * @fw_preboot_cpu_boot_dev_sts0: bitmap representation of preboot cpu
482 * capabilities reported by FW, bit description
483 * can be found in CPU_BOOT_DEV_STS0
484 * @fw_preboot_cpu_boot_dev_sts1: bitmap representation of preboot cpu
485 * capabilities reported by FW, bit description
486 * can be found in CPU_BOOT_DEV_STS1
487 * @fw_bootfit_cpu_boot_dev_sts0: bitmap representation of boot cpu security
488 * status reported by FW, bit description can be
489 * found in CPU_BOOT_DEV_STS0
490 * @fw_bootfit_cpu_boot_dev_sts1: bitmap representation of boot cpu security
491 * status reported by FW, bit description can be
492 * found in CPU_BOOT_DEV_STS1
493 * @fw_app_cpu_boot_dev_sts0: bitmap representation of application security
494 * status reported by FW, bit description can be
495 * found in CPU_BOOT_DEV_STS0
496 * @fw_app_cpu_boot_dev_sts1: bitmap representation of application security
497 * status reported by FW, bit description can be
498 * found in CPU_BOOT_DEV_STS1
499 * @collective_first_sob: first sync object available for collective use
500 * @collective_first_mon: first monitor available for collective use
501 * @sync_stream_first_sob: first sync object available for sync stream use
502 * @sync_stream_first_mon: first monitor available for sync stream use
503 * @first_available_user_sob: first sob available for the user
504 * @first_available_user_mon: first monitor available for the user
505 * @first_available_user_msix_interrupt: first available msix interrupt
506 * reserved for the user
507 * @first_available_cq: first available CQ for the user.
508 * @user_interrupt_count: number of user interrupts.
509 * @server_type: Server type that the ASIC is currently installed in.
510 * The value is according to enum hl_server_type in uapi file.
511 * @tpc_enabled_mask: which TPCs are enabled.
512 * @completion_queues_count: number of completion queues.
513 * @fw_security_enabled: true if security measures are enabled in firmware,
514 * false otherwise
515 * @fw_cpu_boot_dev_sts0_valid: status bits are valid and can be fetched from
516 * BOOT_DEV_STS0
517 * @fw_cpu_boot_dev_sts1_valid: status bits are valid and can be fetched from
518 * BOOT_DEV_STS1
519 * @dram_supports_virtual_memory: is there an MMU towards the DRAM
520 * @hard_reset_done_by_fw: true if firmware is handling hard reset flow
521 * @num_functional_hbms: number of functional HBMs in each DCORE.
522 * @hints_range_reservation: device support hint addresses range reservation.
523 * @iatu_done_by_fw: true if iATU configuration is being done by FW.
524 * @dynamic_fw_load: is dynamic FW load is supported.
525 * @gic_interrupts_enable: true if FW is not blocking GIC controller,
526 * false otherwise.
527 */
528 struct asic_fixed_properties {
529 struct hw_queue_properties *hw_queues_props;
530 struct cpucp_info cpucp_info;
531 char uboot_ver[VERSION_MAX_LEN];
532 char preboot_ver[VERSION_MAX_LEN];
533 struct hl_mmu_properties dmmu;
534 struct hl_mmu_properties pmmu;
535 struct hl_mmu_properties pmmu_huge;
536 struct hl_hints_range hints_dram_reserved_va_range;
537 struct hl_hints_range hints_host_reserved_va_range;
538 struct hl_hints_range hints_host_hpage_reserved_va_range;
539 u64 sram_base_address;
540 u64 sram_end_address;
541 u64 sram_user_base_address;
542 u64 dram_base_address;
543 u64 dram_end_address;
544 u64 dram_user_base_address;
545 u64 dram_size;
546 u64 dram_pci_bar_size;
547 u64 max_power_default;
548 u64 dc_power_default;
549 u64 dram_size_for_default_page_mapping;
550 u64 pcie_dbi_base_address;
551 u64 pcie_aux_dbi_reg_addr;
552 u64 mmu_pgt_addr;
553 u64 mmu_dram_default_page_addr;
554 u64 cb_va_start_addr;
555 u64 cb_va_end_addr;
556 u64 dram_hints_align_mask;
557 u64 device_dma_offset_for_host_access;
558 u64 max_freq_value;
559 u32 clk_pll_index;
560 u32 mmu_pgt_size;
561 u32 mmu_pte_size;
562 u32 mmu_hop_table_size;
563 u32 mmu_hop0_tables_total_size;
564 u32 dram_page_size;
565 u32 cfg_size;
566 u32 sram_size;
567 u32 max_asid;
568 u32 num_of_events;
569 u32 psoc_pci_pll_nr;
570 u32 psoc_pci_pll_nf;
571 u32 psoc_pci_pll_od;
572 u32 psoc_pci_pll_div_factor;
573 u32 psoc_timestamp_frequency;
574 u32 high_pll;
575 u32 cb_pool_cb_cnt;
576 u32 cb_pool_cb_size;
577 u32 max_pending_cs;
578 u32 max_queues;
579 u32 fw_preboot_cpu_boot_dev_sts0;
580 u32 fw_preboot_cpu_boot_dev_sts1;
581 u32 fw_bootfit_cpu_boot_dev_sts0;
582 u32 fw_bootfit_cpu_boot_dev_sts1;
583 u32 fw_app_cpu_boot_dev_sts0;
584 u32 fw_app_cpu_boot_dev_sts1;
585 u16 collective_first_sob;
586 u16 collective_first_mon;
587 u16 sync_stream_first_sob;
588 u16 sync_stream_first_mon;
589 u16 first_available_user_sob[HL_MAX_DCORES];
590 u16 first_available_user_mon[HL_MAX_DCORES];
591 u16 first_available_user_msix_interrupt;
592 u16 first_available_cq[HL_MAX_DCORES];
593 u16 user_interrupt_count;
594 u16 server_type;
595 u8 tpc_enabled_mask;
596 u8 completion_queues_count;
597 u8 fw_security_enabled;
598 u8 fw_cpu_boot_dev_sts0_valid;
599 u8 fw_cpu_boot_dev_sts1_valid;
600 u8 dram_supports_virtual_memory;
601 u8 hard_reset_done_by_fw;
602 u8 num_functional_hbms;
603 u8 hints_range_reservation;
604 u8 iatu_done_by_fw;
605 u8 dynamic_fw_load;
606 u8 gic_interrupts_enable;
607 };
608
609 /**
610 * struct hl_fence - software synchronization primitive
611 * @completion: fence is implemented using completion
612 * @refcount: refcount for this fence
613 * @cs_sequence: sequence of the corresponding command submission
614 * @stream_master_qid_map: streams masters QID bitmap to represent all streams
615 * masters QIDs that multi cs is waiting on
616 * @error: mark this fence with error
617 * @timestamp: timestamp upon completion
618 * @mcs_handling_done: indicates that corresponding command submission has
619 * finished msc handling, this does not mean it was part
620 * of the mcs
621 */
622 struct hl_fence {
623 struct completion completion;
624 struct kref refcount;
625 u64 cs_sequence;
626 u32 stream_master_qid_map;
627 int error;
628 ktime_t timestamp;
629 u8 mcs_handling_done;
630 };
631
632 /**
633 * struct hl_cs_compl - command submission completion object.
634 * @base_fence: hl fence object.
635 * @lock: spinlock to protect fence.
636 * @hdev: habanalabs device structure.
637 * @hw_sob: the H/W SOB used in this signal/wait CS.
638 * @encaps_sig_hdl: encaps signals hanlder.
639 * @cs_seq: command submission sequence number.
640 * @type: type of the CS - signal/wait.
641 * @sob_val: the SOB value that is used in this signal/wait CS.
642 * @sob_group: the SOB group that is used in this collective wait CS.
643 * @encaps_signals: indication whether it's a completion object of cs with
644 * encaps signals or not.
645 */
646 struct hl_cs_compl {
647 struct hl_fence base_fence;
648 spinlock_t lock;
649 struct hl_device *hdev;
650 struct hl_hw_sob *hw_sob;
651 struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
652 u64 cs_seq;
653 enum hl_cs_type type;
654 u16 sob_val;
655 u16 sob_group;
656 bool encaps_signals;
657 };
658
659 /*
660 * Command Buffers
661 */
662
663 /**
664 * struct hl_cb_mgr - describes a Command Buffer Manager.
665 * @cb_lock: protects cb_handles.
666 * @cb_handles: an idr to hold all command buffer handles.
667 */
668 struct hl_cb_mgr {
669 spinlock_t cb_lock;
670 struct idr cb_handles; /* protected by cb_lock */
671 };
672
673 /**
674 * struct hl_cb - describes a Command Buffer.
675 * @refcount: reference counter for usage of the CB.
676 * @hdev: pointer to device this CB belongs to.
677 * @ctx: pointer to the CB owner's context.
678 * @lock: spinlock to protect mmap flows.
679 * @debugfs_list: node in debugfs list of command buffers.
680 * @pool_list: node in pool list of command buffers.
681 * @va_block_list: list of virtual addresses blocks of the CB if it is mapped to
682 * the device's MMU.
683 * @id: the CB's ID.
684 * @kernel_address: Holds the CB's kernel virtual address.
685 * @bus_address: Holds the CB's DMA address.
686 * @mmap_size: Holds the CB's size that was mmaped.
687 * @size: holds the CB's size.
688 * @cs_cnt: holds number of CS that this CB participates in.
689 * @mmap: true if the CB is currently mmaped to user.
690 * @is_pool: true if CB was acquired from the pool, false otherwise.
691 * @is_internal: internaly allocated
692 * @is_mmu_mapped: true if the CB is mapped to the device's MMU.
693 */
694 struct hl_cb {
695 struct kref refcount;
696 struct hl_device *hdev;
697 struct hl_ctx *ctx;
698 spinlock_t lock;
699 struct list_head debugfs_list;
700 struct list_head pool_list;
701 struct list_head va_block_list;
702 u64 id;
703 void *kernel_address;
704 dma_addr_t bus_address;
705 u32 mmap_size;
706 u32 size;
707 atomic_t cs_cnt;
708 u8 mmap;
709 u8 is_pool;
710 u8 is_internal;
711 u8 is_mmu_mapped;
712 };
713
714
715 /*
716 * QUEUES
717 */
718
719 struct hl_cs;
720 struct hl_cs_job;
721
722 /* Queue length of external and HW queues */
723 #define HL_QUEUE_LENGTH 4096
724 #define HL_QUEUE_SIZE_IN_BYTES (HL_QUEUE_LENGTH * HL_BD_SIZE)
725
726 #if (HL_MAX_JOBS_PER_CS > HL_QUEUE_LENGTH)
727 #error "HL_QUEUE_LENGTH must be greater than HL_MAX_JOBS_PER_CS"
728 #endif
729
730 /* HL_CQ_LENGTH is in units of struct hl_cq_entry */
731 #define HL_CQ_LENGTH HL_QUEUE_LENGTH
732 #define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)
733
734 /* Must be power of 2 */
735 #define HL_EQ_LENGTH 64
736 #define HL_EQ_SIZE_IN_BYTES (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
737
738 /* Host <-> CPU-CP shared memory size */
739 #define HL_CPU_ACCESSIBLE_MEM_SIZE SZ_2M
740
741 /**
742 * struct hl_sync_stream_properties -
743 * describes a H/W queue sync stream properties
744 * @hw_sob: array of the used H/W SOBs by this H/W queue.
745 * @next_sob_val: the next value to use for the currently used SOB.
746 * @base_sob_id: the base SOB id of the SOBs used by this queue.
747 * @base_mon_id: the base MON id of the MONs used by this queue.
748 * @collective_mstr_mon_id: the MON ids of the MONs used by this master queue
749 * in order to sync with all slave queues.
750 * @collective_slave_mon_id: the MON id used by this slave queue in order to
751 * sync with its master queue.
752 * @collective_sob_id: current SOB id used by this collective slave queue
753 * to signal its collective master queue upon completion.
754 * @curr_sob_offset: the id offset to the currently used SOB from the
755 * HL_RSVD_SOBS that are being used by this queue.
756 */
757 struct hl_sync_stream_properties {
758 struct hl_hw_sob hw_sob[HL_RSVD_SOBS];
759 u16 next_sob_val;
760 u16 base_sob_id;
761 u16 base_mon_id;
762 u16 collective_mstr_mon_id[HL_COLLECTIVE_RSVD_MSTR_MONS];
763 u16 collective_slave_mon_id;
764 u16 collective_sob_id;
765 u8 curr_sob_offset;
766 };
767
768 /**
769 * struct hl_encaps_signals_mgr - describes sync stream encapsulated signals
770 * handlers manager
771 * @lock: protects handles.
772 * @handles: an idr to hold all encapsulated signals handles.
773 */
774 struct hl_encaps_signals_mgr {
775 spinlock_t lock;
776 struct idr handles;
777 };
778
779 /**
780 * struct hl_hw_queue - describes a H/W transport queue.
781 * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
782 * @sync_stream_prop: sync stream queue properties
783 * @queue_type: type of queue.
784 * @collective_mode: collective mode of current queue
785 * @kernel_address: holds the queue's kernel virtual address.
786 * @bus_address: holds the queue's DMA address.
787 * @pi: holds the queue's pi value.
788 * @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci).
789 * @hw_queue_id: the id of the H/W queue.
790 * @cq_id: the id for the corresponding CQ for this H/W queue.
791 * @msi_vec: the IRQ number of the H/W queue.
792 * @int_queue_len: length of internal queue (number of entries).
793 * @valid: is the queue valid (we have array of 32 queues, not all of them
794 * exist).
795 * @supports_sync_stream: True if queue supports sync stream
796 */
797 struct hl_hw_queue {
798 struct hl_cs_job **shadow_queue;
799 struct hl_sync_stream_properties sync_stream_prop;
800 enum hl_queue_type queue_type;
801 enum hl_collective_mode collective_mode;
802 void *kernel_address;
803 dma_addr_t bus_address;
804 u32 pi;
805 atomic_t ci;
806 u32 hw_queue_id;
807 u32 cq_id;
808 u32 msi_vec;
809 u16 int_queue_len;
810 u8 valid;
811 u8 supports_sync_stream;
812 };
813
814 /**
815 * struct hl_cq - describes a completion queue
816 * @hdev: pointer to the device structure
817 * @kernel_address: holds the queue's kernel virtual address
818 * @bus_address: holds the queue's DMA address
819 * @cq_idx: completion queue index in array
820 * @hw_queue_id: the id of the matching H/W queue
821 * @ci: ci inside the queue
822 * @pi: pi inside the queue
823 * @free_slots_cnt: counter of free slots in queue
824 */
825 struct hl_cq {
826 struct hl_device *hdev;
827 void *kernel_address;
828 dma_addr_t bus_address;
829 u32 cq_idx;
830 u32 hw_queue_id;
831 u32 ci;
832 u32 pi;
833 atomic_t free_slots_cnt;
834 };
835
836 /**
837 * struct hl_user_interrupt - holds user interrupt information
838 * @hdev: pointer to the device structure
839 * @wait_list_head: head to the list of user threads pending on this interrupt
840 * @wait_list_lock: protects wait_list_head
841 * @interrupt_id: msix interrupt id
842 */
843 struct hl_user_interrupt {
844 struct hl_device *hdev;
845 struct list_head wait_list_head;
846 spinlock_t wait_list_lock;
847 u32 interrupt_id;
848 };
849
850 /**
851 * struct hl_user_pending_interrupt - holds a context to a user thread
852 * pending on an interrupt
853 * @wait_list_node: node in the list of user threads pending on an interrupt
854 * @fence: hl fence object for interrupt completion
855 */
856 struct hl_user_pending_interrupt {
857 struct list_head wait_list_node;
858 struct hl_fence fence;
859 };
860
861 /**
862 * struct hl_eq - describes the event queue (single one per device)
863 * @hdev: pointer to the device structure
864 * @kernel_address: holds the queue's kernel virtual address
865 * @bus_address: holds the queue's DMA address
866 * @ci: ci inside the queue
867 * @prev_eqe_index: the index of the previous event queue entry. The index of
868 * the current entry's index must be +1 of the previous one.
869 * @check_eqe_index: do we need to check the index of the current entry vs. the
870 * previous one. This is for backward compatibility with older
871 * firmwares
872 */
873 struct hl_eq {
874 struct hl_device *hdev;
875 void *kernel_address;
876 dma_addr_t bus_address;
877 u32 ci;
878 u32 prev_eqe_index;
879 bool check_eqe_index;
880 };
881
882
883 /*
884 * ASICs
885 */
886
887 /**
888 * enum hl_asic_type - supported ASIC types.
889 * @ASIC_INVALID: Invalid ASIC type.
890 * @ASIC_GOYA: Goya device.
891 * @ASIC_GAUDI: Gaudi device.
892 * @ASIC_GAUDI_SEC: Gaudi secured device (HL-2000).
893 */
894 enum hl_asic_type {
895 ASIC_INVALID,
896 ASIC_GOYA,
897 ASIC_GAUDI,
898 ASIC_GAUDI_SEC
899 };
900
901 struct hl_cs_parser;
902
903 /**
904 * enum hl_pm_mng_profile - power management profile.
905 * @PM_AUTO: internal clock is set by the Linux driver.
906 * @PM_MANUAL: internal clock is set by the user.
907 * @PM_LAST: last power management type.
908 */
909 enum hl_pm_mng_profile {
910 PM_AUTO = 1,
911 PM_MANUAL,
912 PM_LAST
913 };
914
915 /**
916 * enum hl_pll_frequency - PLL frequency.
917 * @PLL_HIGH: high frequency.
918 * @PLL_LOW: low frequency.
919 * @PLL_LAST: last frequency values that were configured by the user.
920 */
921 enum hl_pll_frequency {
922 PLL_HIGH = 1,
923 PLL_LOW,
924 PLL_LAST
925 };
926
927 #define PLL_REF_CLK 50
928
929 enum div_select_defs {
930 DIV_SEL_REF_CLK = 0,
931 DIV_SEL_PLL_CLK = 1,
932 DIV_SEL_DIVIDED_REF = 2,
933 DIV_SEL_DIVIDED_PLL = 3,
934 };
935
936 enum pci_region {
937 PCI_REGION_CFG,
938 PCI_REGION_SRAM,
939 PCI_REGION_DRAM,
940 PCI_REGION_SP_SRAM,
941 PCI_REGION_NUMBER,
942 };
943
944 /**
945 * struct pci_mem_region - describe memory region in a PCI bar
946 * @region_base: region base address
947 * @region_size: region size
948 * @bar_size: size of the BAR
949 * @offset_in_bar: region offset into the bar
950 * @bar_id: bar ID of the region
951 * @used: if used 1, otherwise 0
952 */
953 struct pci_mem_region {
954 u64 region_base;
955 u64 region_size;
956 u64 bar_size;
957 u64 offset_in_bar;
958 u8 bar_id;
959 u8 used;
960 };
961
962 /**
963 * struct static_fw_load_mgr - static FW load manager
964 * @preboot_version_max_off: max offset to preboot version
965 * @boot_fit_version_max_off: max offset to boot fit version
966 * @kmd_msg_to_cpu_reg: register address for KDM->CPU messages
967 * @cpu_cmd_status_to_host_reg: register address for CPU command status response
968 * @cpu_boot_status_reg: boot status register
969 * @cpu_boot_dev_status0_reg: boot device status register 0
970 * @cpu_boot_dev_status1_reg: boot device status register 1
971 * @boot_err0_reg: boot error register 0
972 * @boot_err1_reg: boot error register 1
973 * @preboot_version_offset_reg: SRAM offset to preboot version register
974 * @boot_fit_version_offset_reg: SRAM offset to boot fit version register
975 * @sram_offset_mask: mask for getting offset into the SRAM
976 * @cpu_reset_wait_msec: used when setting WFE via kmd_msg_to_cpu_reg
977 */
978 struct static_fw_load_mgr {
979 u64 preboot_version_max_off;
980 u64 boot_fit_version_max_off;
981 u32 kmd_msg_to_cpu_reg;
982 u32 cpu_cmd_status_to_host_reg;
983 u32 cpu_boot_status_reg;
984 u32 cpu_boot_dev_status0_reg;
985 u32 cpu_boot_dev_status1_reg;
986 u32 boot_err0_reg;
987 u32 boot_err1_reg;
988 u32 preboot_version_offset_reg;
989 u32 boot_fit_version_offset_reg;
990 u32 sram_offset_mask;
991 u32 cpu_reset_wait_msec;
992 };
993
994 /**
995 * struct fw_response - FW response to LKD command
996 * @ram_offset: descriptor offset into the RAM
997 * @ram_type: RAM type containing the descriptor (SRAM/DRAM)
998 * @status: command status
999 */
1000 struct fw_response {
1001 u32 ram_offset;
1002 u8 ram_type;
1003 u8 status;
1004 };
1005
1006 /**
1007 * struct dynamic_fw_load_mgr - dynamic FW load manager
1008 * @response: FW to LKD response
1009 * @comm_desc: the communication descriptor with FW
1010 * @image_region: region to copy the FW image to
1011 * @fw_image_size: size of FW image to load
1012 * @wait_for_bl_timeout: timeout for waiting for boot loader to respond
1013 */
1014 struct dynamic_fw_load_mgr {
1015 struct fw_response response;
1016 struct lkd_fw_comms_desc comm_desc;
1017 struct pci_mem_region *image_region;
1018 size_t fw_image_size;
1019 u32 wait_for_bl_timeout;
1020 };
1021
1022 /**
1023 * struct fw_image_props - properties of FW image
1024 * @image_name: name of the image
1025 * @src_off: offset in src FW to copy from
1026 * @copy_size: amount of bytes to copy (0 to copy the whole binary)
1027 */
1028 struct fw_image_props {
1029 char *image_name;
1030 u32 src_off;
1031 u32 copy_size;
1032 };
1033
1034 /**
1035 * struct fw_load_mgr - manager FW loading process
1036 * @dynamic_loader: specific structure for dynamic load
1037 * @static_loader: specific structure for static load
1038 * @boot_fit_img: boot fit image properties
1039 * @linux_img: linux image properties
1040 * @cpu_timeout: CPU response timeout in usec
1041 * @boot_fit_timeout: Boot fit load timeout in usec
1042 * @skip_bmc: should BMC be skipped
1043 * @sram_bar_id: SRAM bar ID
1044 * @dram_bar_id: DRAM bar ID
1045 * @linux_loaded: true if linux was loaded so far
1046 */
1047 struct fw_load_mgr {
1048 union {
1049 struct dynamic_fw_load_mgr dynamic_loader;
1050 struct static_fw_load_mgr static_loader;
1051 };
1052 struct fw_image_props boot_fit_img;
1053 struct fw_image_props linux_img;
1054 u32 cpu_timeout;
1055 u32 boot_fit_timeout;
1056 u8 skip_bmc;
1057 u8 sram_bar_id;
1058 u8 dram_bar_id;
1059 u8 linux_loaded;
1060 };
1061
1062 /**
1063 * struct hl_asic_funcs - ASIC specific functions that are can be called from
1064 * common code.
1065 * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
1066 * @early_fini: tears down what was done in early_init.
1067 * @late_init: sets up late driver/hw state (post hw_init) - Optional.
1068 * @late_fini: tears down what was done in late_init (pre hw_fini) - Optional.
1069 * @sw_init: sets up driver state, does not configure H/W.
1070 * @sw_fini: tears down driver state, does not configure H/W.
1071 * @hw_init: sets up the H/W state.
1072 * @hw_fini: tears down the H/W state.
1073 * @halt_engines: halt engines, needed for reset sequence. This also disables
1074 * interrupts from the device. Should be called before
1075 * hw_fini and before CS rollback.
1076 * @suspend: handles IP specific H/W or SW changes for suspend.
1077 * @resume: handles IP specific H/W or SW changes for resume.
1078 * @mmap: maps a memory.
1079 * @ring_doorbell: increment PI on a given QMAN.
1080 * @pqe_write: Write the PQ entry to the PQ. This is ASIC-specific
1081 * function because the PQs are located in different memory areas
1082 * per ASIC (SRAM, DRAM, Host memory) and therefore, the method of
1083 * writing the PQE must match the destination memory area
1084 * properties.
1085 * @asic_dma_alloc_coherent: Allocate coherent DMA memory by calling
1086 * dma_alloc_coherent(). This is ASIC function because
1087 * its implementation is not trivial when the driver
1088 * is loaded in simulation mode (not upstreamed).
1089 * @asic_dma_free_coherent: Free coherent DMA memory by calling
1090 * dma_free_coherent(). This is ASIC function because
1091 * its implementation is not trivial when the driver
1092 * is loaded in simulation mode (not upstreamed).
1093 * @scrub_device_mem: Scrub device memory given an address and size
1094 * @get_int_queue_base: get the internal queue base address.
1095 * @test_queues: run simple test on all queues for sanity check.
1096 * @asic_dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool.
1097 * size of allocation is HL_DMA_POOL_BLK_SIZE.
1098 * @asic_dma_pool_free: free small DMA allocation from pool.
1099 * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
1100 * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
1101 * @hl_dma_unmap_sg: DMA unmap scatter-gather list.
1102 * @cs_parser: parse Command Submission.
1103 * @asic_dma_map_sg: DMA map scatter-gather list.
1104 * @get_dma_desc_list_size: get number of LIN_DMA packets required for CB.
1105 * @add_end_of_cb_packets: Add packets to the end of CB, if device requires it.
1106 * @update_eq_ci: update event queue CI.
1107 * @context_switch: called upon ASID context switch.
1108 * @restore_phase_topology: clear all SOBs amd MONs.
1109 * @debugfs_read32: debug interface for reading u32 from DRAM/SRAM/Host memory.
1110 * @debugfs_write32: debug interface for writing u32 to DRAM/SRAM/Host memory.
1111 * @debugfs_read64: debug interface for reading u64 from DRAM/SRAM/Host memory.
1112 * @debugfs_write64: debug interface for writing u64 to DRAM/SRAM/Host memory.
1113 * @debugfs_read_dma: debug interface for reading up to 2MB from the device's
1114 * internal memory via DMA engine.
1115 * @add_device_attr: add ASIC specific device attributes.
1116 * @handle_eqe: handle event queue entry (IRQ) from CPU-CP.
1117 * @set_pll_profile: change PLL profile (manual/automatic).
1118 * @get_events_stat: retrieve event queue entries histogram.
1119 * @read_pte: read MMU page table entry from DRAM.
1120 * @write_pte: write MMU page table entry to DRAM.
1121 * @mmu_invalidate_cache: flush MMU STLB host/DRAM cache, either with soft
1122 * (L1 only) or hard (L0 & L1) flush.
1123 * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with
1124 * ASID-VA-size mask.
1125 * @send_heartbeat: send is-alive packet to CPU-CP and verify response.
1126 * @set_clock_gating: enable/disable clock gating per engine according to
1127 * clock gating mask in hdev
1128 * @disable_clock_gating: disable clock gating completely
1129 * @debug_coresight: perform certain actions on Coresight for debugging.
1130 * @is_device_idle: return true if device is idle, false otherwise.
1131 * @soft_reset_late_init: perform certain actions needed after soft reset.
1132 * @hw_queues_lock: acquire H/W queues lock.
1133 * @hw_queues_unlock: release H/W queues lock.
1134 * @get_pci_id: retrieve PCI ID.
1135 * @get_eeprom_data: retrieve EEPROM data from F/W.
1136 * @send_cpu_message: send message to F/W. If the message is timedout, the
1137 * driver will eventually reset the device. The timeout can
1138 * be determined by the calling function or it can be 0 and
1139 * then the timeout is the default timeout for the specific
1140 * ASIC
1141 * @get_hw_state: retrieve the H/W state
1142 * @pci_bars_map: Map PCI BARs.
1143 * @init_iatu: Initialize the iATU unit inside the PCI controller.
1144 * @rreg: Read a register. Needed for simulator support.
1145 * @wreg: Write a register. Needed for simulator support.
1146 * @halt_coresight: stop the ETF and ETR traces.
1147 * @ctx_init: context dependent initialization.
1148 * @ctx_fini: context dependent cleanup.
1149 * @get_clk_rate: Retrieve the ASIC current and maximum clock rate in MHz
1150 * @get_queue_id_for_cq: Get the H/W queue id related to the given CQ index.
1151 * @load_firmware_to_device: load the firmware to the device's memory
1152 * @load_boot_fit_to_device: load boot fit to device's memory
1153 * @get_signal_cb_size: Get signal CB size.
1154 * @get_wait_cb_size: Get wait CB size.
1155 * @gen_signal_cb: Generate a signal CB.
1156 * @gen_wait_cb: Generate a wait CB.
1157 * @reset_sob: Reset a SOB.
1158 * @reset_sob_group: Reset SOB group
1159 * @set_dma_mask_from_fw: set the DMA mask in the driver according to the
1160 * firmware configuration
1161 * @get_device_time: Get the device time.
1162 * @collective_wait_init_cs: Generate collective master/slave packets
1163 * and place them in the relevant cs jobs
1164 * @collective_wait_create_jobs: allocate collective wait cs jobs
1165 * @scramble_addr: Routine to scramble the address prior of mapping it
1166 * in the MMU.
1167 * @descramble_addr: Routine to de-scramble the address prior of
1168 * showing it to users.
1169 * @ack_protection_bits_errors: ack and dump all security violations
1170 * @get_hw_block_id: retrieve a HW block id to be used by the user to mmap it.
1171 * also returns the size of the block if caller supplies
1172 * a valid pointer for it
1173 * @hw_block_mmap: mmap a HW block with a given id.
1174 * @enable_events_from_fw: send interrupt to firmware to notify them the
1175 * driver is ready to receive asynchronous events. This
1176 * function should be called during the first init and
1177 * after every hard-reset of the device
1178 * @get_msi_info: Retrieve asic-specific MSI ID of the f/w async event
1179 * @map_pll_idx_to_fw_idx: convert driver specific per asic PLL index to
1180 * generic f/w compatible PLL Indexes
1181 * @init_firmware_loader: initialize data for FW loader.
1182 * @init_cpu_scrambler_dram: Enable CPU specific DRAM scrambling
1183 * @state_dump_init: initialize constants required for state dump
1184 * @get_sob_addr: get SOB base address offset.
1185 * @set_pci_memory_regions: setting properties of PCI memory regions
1186 * @get_stream_master_qid_arr: get pointer to stream masters QID array
1187 */
1188 struct hl_asic_funcs {
1189 int (*early_init)(struct hl_device *hdev);
1190 int (*early_fini)(struct hl_device *hdev);
1191 int (*late_init)(struct hl_device *hdev);
1192 void (*late_fini)(struct hl_device *hdev);
1193 int (*sw_init)(struct hl_device *hdev);
1194 int (*sw_fini)(struct hl_device *hdev);
1195 int (*hw_init)(struct hl_device *hdev);
1196 void (*hw_fini)(struct hl_device *hdev, bool hard_reset, bool fw_reset);
1197 void (*halt_engines)(struct hl_device *hdev, bool hard_reset, bool fw_reset);
1198 int (*suspend)(struct hl_device *hdev);
1199 int (*resume)(struct hl_device *hdev);
1200 int (*mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
1201 void *cpu_addr, dma_addr_t dma_addr, size_t size);
1202 void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
1203 void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
1204 struct hl_bd *bd);
1205 void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
1206 dma_addr_t *dma_handle, gfp_t flag);
1207 void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
1208 void *cpu_addr, dma_addr_t dma_handle);
1209 int (*scrub_device_mem)(struct hl_device *hdev, u64 addr, u64 size);
1210 void* (*get_int_queue_base)(struct hl_device *hdev, u32 queue_id,
1211 dma_addr_t *dma_handle, u16 *queue_len);
1212 int (*test_queues)(struct hl_device *hdev);
1213 void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
1214 gfp_t mem_flags, dma_addr_t *dma_handle);
1215 void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
1216 dma_addr_t dma_addr);
1217 void* (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev,
1218 size_t size, dma_addr_t *dma_handle);
1219 void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,
1220 size_t size, void *vaddr);
1221 void (*hl_dma_unmap_sg)(struct hl_device *hdev,
1222 struct scatterlist *sgl, int nents,
1223 enum dma_data_direction dir);
1224 int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
1225 int (*asic_dma_map_sg)(struct hl_device *hdev,
1226 struct scatterlist *sgl, int nents,
1227 enum dma_data_direction dir);
1228 u32 (*get_dma_desc_list_size)(struct hl_device *hdev,
1229 struct sg_table *sgt);
1230 void (*add_end_of_cb_packets)(struct hl_device *hdev,
1231 void *kernel_address, u32 len,
1232 u64 cq_addr, u32 cq_val, u32 msix_num,
1233 bool eb);
1234 void (*update_eq_ci)(struct hl_device *hdev, u32 val);
1235 int (*context_switch)(struct hl_device *hdev, u32 asid);
1236 void (*restore_phase_topology)(struct hl_device *hdev);
1237 int (*debugfs_read32)(struct hl_device *hdev, u64 addr,
1238 bool user_address, u32 *val);
1239 int (*debugfs_write32)(struct hl_device *hdev, u64 addr,
1240 bool user_address, u32 val);
1241 int (*debugfs_read64)(struct hl_device *hdev, u64 addr,
1242 bool user_address, u64 *val);
1243 int (*debugfs_write64)(struct hl_device *hdev, u64 addr,
1244 bool user_address, u64 val);
1245 int (*debugfs_read_dma)(struct hl_device *hdev, u64 addr, u32 size,
1246 void *blob_addr);
1247 void (*add_device_attr)(struct hl_device *hdev,
1248 struct attribute_group *dev_attr_grp);
1249 void (*handle_eqe)(struct hl_device *hdev,
1250 struct hl_eq_entry *eq_entry);
1251 void (*set_pll_profile)(struct hl_device *hdev,
1252 enum hl_pll_frequency freq);
1253 void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
1254 u32 *size);
1255 u64 (*read_pte)(struct hl_device *hdev, u64 addr);
1256 void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
1257 int (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard,
1258 u32 flags);
1259 int (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
1260 u32 flags, u32 asid, u64 va, u64 size);
1261 int (*send_heartbeat)(struct hl_device *hdev);
1262 void (*set_clock_gating)(struct hl_device *hdev);
1263 void (*disable_clock_gating)(struct hl_device *hdev);
1264 int (*debug_coresight)(struct hl_device *hdev, void *data);
1265 bool (*is_device_idle)(struct hl_device *hdev, u64 *mask_arr,
1266 u8 mask_len, struct seq_file *s);
1267 int (*soft_reset_late_init)(struct hl_device *hdev);
1268 void (*hw_queues_lock)(struct hl_device *hdev);
1269 void (*hw_queues_unlock)(struct hl_device *hdev);
1270 u32 (*get_pci_id)(struct hl_device *hdev);
1271 int (*get_eeprom_data)(struct hl_device *hdev, void *data,
1272 size_t max_size);
1273 int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
1274 u16 len, u32 timeout, u64 *result);
1275 int (*pci_bars_map)(struct hl_device *hdev);
1276 int (*init_iatu)(struct hl_device *hdev);
1277 u32 (*rreg)(struct hl_device *hdev, u32 reg);
1278 void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
1279 void (*halt_coresight)(struct hl_device *hdev);
1280 int (*ctx_init)(struct hl_ctx *ctx);
1281 void (*ctx_fini)(struct hl_ctx *ctx);
1282 int (*get_clk_rate)(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk);
1283 u32 (*get_queue_id_for_cq)(struct hl_device *hdev, u32 cq_idx);
1284 int (*load_firmware_to_device)(struct hl_device *hdev);
1285 int (*load_boot_fit_to_device)(struct hl_device *hdev);
1286 u32 (*get_signal_cb_size)(struct hl_device *hdev);
1287 u32 (*get_wait_cb_size)(struct hl_device *hdev);
1288 u32 (*gen_signal_cb)(struct hl_device *hdev, void *data, u16 sob_id,
1289 u32 size, bool eb);
1290 u32 (*gen_wait_cb)(struct hl_device *hdev,
1291 struct hl_gen_wait_properties *prop);
1292 void (*reset_sob)(struct hl_device *hdev, void *data);
1293 void (*reset_sob_group)(struct hl_device *hdev, u16 sob_group);
1294 void (*set_dma_mask_from_fw)(struct hl_device *hdev);
1295 u64 (*get_device_time)(struct hl_device *hdev);
1296 int (*collective_wait_init_cs)(struct hl_cs *cs);
1297 int (*collective_wait_create_jobs)(struct hl_device *hdev,
1298 struct hl_ctx *ctx, struct hl_cs *cs,
1299 u32 wait_queue_id, u32 collective_engine_id,
1300 u32 encaps_signal_offset);
1301 u64 (*scramble_addr)(struct hl_device *hdev, u64 addr);
1302 u64 (*descramble_addr)(struct hl_device *hdev, u64 addr);
1303 void (*ack_protection_bits_errors)(struct hl_device *hdev);
1304 int (*get_hw_block_id)(struct hl_device *hdev, u64 block_addr,
1305 u32 *block_size, u32 *block_id);
1306 int (*hw_block_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
1307 u32 block_id, u32 block_size);
1308 void (*enable_events_from_fw)(struct hl_device *hdev);
1309 void (*get_msi_info)(__le32 *table);
1310 int (*map_pll_idx_to_fw_idx)(u32 pll_idx);
1311 void (*init_firmware_loader)(struct hl_device *hdev);
1312 void (*init_cpu_scrambler_dram)(struct hl_device *hdev);
1313 void (*state_dump_init)(struct hl_device *hdev);
1314 u32 (*get_sob_addr)(struct hl_device *hdev, u32 sob_id);
1315 void (*set_pci_memory_regions)(struct hl_device *hdev);
1316 u32* (*get_stream_master_qid_arr)(void);
1317 };
1318
1319
1320 /*
1321 * CONTEXTS
1322 */
1323
1324 #define HL_KERNEL_ASID_ID 0
1325
1326 /**
1327 * enum hl_va_range_type - virtual address range type.
1328 * @HL_VA_RANGE_TYPE_HOST: range type of host pages
1329 * @HL_VA_RANGE_TYPE_HOST_HUGE: range type of host huge pages
1330 * @HL_VA_RANGE_TYPE_DRAM: range type of dram pages
1331 */
1332 enum hl_va_range_type {
1333 HL_VA_RANGE_TYPE_HOST,
1334 HL_VA_RANGE_TYPE_HOST_HUGE,
1335 HL_VA_RANGE_TYPE_DRAM,
1336 HL_VA_RANGE_TYPE_MAX
1337 };
1338
1339 /**
1340 * struct hl_va_range - virtual addresses range.
1341 * @lock: protects the virtual addresses list.
1342 * @list: list of virtual addresses blocks available for mappings.
1343 * @start_addr: range start address.
1344 * @end_addr: range end address.
1345 * @page_size: page size of this va range.
1346 */
1347 struct hl_va_range {
1348 struct mutex lock;
1349 struct list_head list;
1350 u64 start_addr;
1351 u64 end_addr;
1352 u32 page_size;
1353 };
1354
1355 /**
1356 * struct hl_cs_counters_atomic - command submission counters
1357 * @out_of_mem_drop_cnt: dropped due to memory allocation issue
1358 * @parsing_drop_cnt: dropped due to error in packet parsing
1359 * @queue_full_drop_cnt: dropped due to queue full
1360 * @device_in_reset_drop_cnt: dropped due to device in reset
1361 * @max_cs_in_flight_drop_cnt: dropped due to maximum CS in-flight
1362 * @validation_drop_cnt: dropped due to error in validation
1363 */
1364 struct hl_cs_counters_atomic {
1365 atomic64_t out_of_mem_drop_cnt;
1366 atomic64_t parsing_drop_cnt;
1367 atomic64_t queue_full_drop_cnt;
1368 atomic64_t device_in_reset_drop_cnt;
1369 atomic64_t max_cs_in_flight_drop_cnt;
1370 atomic64_t validation_drop_cnt;
1371 };
1372
1373 /**
1374 * struct hl_dmabuf_priv - a dma-buf private object.
1375 * @dmabuf: pointer to dma-buf object.
1376 * @ctx: pointer to the dma-buf owner's context.
1377 * @phys_pg_pack: pointer to physical page pack if the dma-buf was exported for
1378 * memory allocation handle.
1379 * @device_address: physical address of the device's memory. Relevant only
1380 * if phys_pg_pack is NULL (dma-buf was exported from address).
1381 * The total size can be taken from the dmabuf object.
1382 */
1383 struct hl_dmabuf_priv {
1384 struct dma_buf *dmabuf;
1385 struct hl_ctx *ctx;
1386 struct hl_vm_phys_pg_pack *phys_pg_pack;
1387 uint64_t device_address;
1388 };
1389
1390 /**
1391 * struct hl_ctx - user/kernel context.
1392 * @mem_hash: holds mapping from virtual address to virtual memory area
1393 * descriptor (hl_vm_phys_pg_list or hl_userptr).
1394 * @mmu_shadow_hash: holds a mapping from shadow address to pgt_info structure.
1395 * @hpriv: pointer to the private (Kernel Driver) data of the process (fd).
1396 * @hdev: pointer to the device structure.
1397 * @refcount: reference counter for the context. Context is released only when
1398 * this hits 0l. It is incremented on CS and CS_WAIT.
1399 * @cs_pending: array of hl fence objects representing pending CS.
1400 * @va_range: holds available virtual addresses for host and dram mappings.
1401 * @mem_hash_lock: protects the mem_hash.
1402 * @mmu_lock: protects the MMU page tables. Any change to the PGT, modifying the
1403 * MMU hash or walking the PGT requires talking this lock.
1404 * @hw_block_list_lock: protects the HW block memory list.
1405 * @debugfs_list: node in debugfs list of contexts.
1406 * @hw_block_mem_list: list of HW block virtual mapped addresses.
1407 * @cs_counters: context command submission counters.
1408 * @cb_va_pool: device VA pool for command buffers which are mapped to the
1409 * device's MMU.
1410 * @sig_mgr: encaps signals handle manager.
1411 * @cs_sequence: sequence number for CS. Value is assigned to a CS and passed
1412 * to user so user could inquire about CS. It is used as
1413 * index to cs_pending array.
1414 * @dram_default_hops: array that holds all hops addresses needed for default
1415 * DRAM mapping.
1416 * @cs_lock: spinlock to protect cs_sequence.
1417 * @dram_phys_mem: amount of used physical DRAM memory by this context.
1418 * @thread_ctx_switch_token: token to prevent multiple threads of the same
1419 * context from running the context switch phase.
1420 * Only a single thread should run it.
1421 * @thread_ctx_switch_wait_token: token to prevent the threads that didn't run
1422 * the context switch phase from moving to their
1423 * execution phase before the context switch phase
1424 * has finished.
1425 * @asid: context's unique address space ID in the device's MMU.
1426 * @handle: context's opaque handle for user
1427 */
1428 struct hl_ctx {
1429 DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
1430 DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
1431 struct hl_fpriv *hpriv;
1432 struct hl_device *hdev;
1433 struct kref refcount;
1434 struct hl_fence **cs_pending;
1435 struct hl_va_range *va_range[HL_VA_RANGE_TYPE_MAX];
1436 struct mutex mem_hash_lock;
1437 struct mutex mmu_lock;
1438 struct mutex hw_block_list_lock;
1439 struct list_head debugfs_list;
1440 struct list_head hw_block_mem_list;
1441 struct hl_cs_counters_atomic cs_counters;
1442 struct gen_pool *cb_va_pool;
1443 struct hl_encaps_signals_mgr sig_mgr;
1444 u64 cs_sequence;
1445 u64 *dram_default_hops;
1446 spinlock_t cs_lock;
1447 atomic64_t dram_phys_mem;
1448 atomic_t thread_ctx_switch_token;
1449 u32 thread_ctx_switch_wait_token;
1450 u32 asid;
1451 u32 handle;
1452 };
1453
1454 /**
1455 * struct hl_ctx_mgr - for handling multiple contexts.
1456 * @ctx_lock: protects ctx_handles.
1457 * @ctx_handles: idr to hold all ctx handles.
1458 */
1459 struct hl_ctx_mgr {
1460 struct mutex ctx_lock;
1461 struct idr ctx_handles;
1462 };
1463
1464
1465
1466 /*
1467 * COMMAND SUBMISSIONS
1468 */
1469
1470 /**
1471 * struct hl_userptr - memory mapping chunk information
1472 * @vm_type: type of the VM.
1473 * @job_node: linked-list node for hanging the object on the Job's list.
1474 * @pages: pointer to struct page array
1475 * @npages: size of @pages array
1476 * @sgt: pointer to the scatter-gather table that holds the pages.
1477 * @dir: for DMA unmapping, the direction must be supplied, so save it.
1478 * @debugfs_list: node in debugfs list of command submissions.
1479 * @pid: the pid of the user process owning the memory
1480 * @addr: user-space virtual address of the start of the memory area.
1481 * @size: size of the memory area to pin & map.
1482 * @dma_mapped: true if the SG was mapped to DMA addresses, false otherwise.
1483 */
1484 struct hl_userptr {
1485 enum vm_type vm_type; /* must be first */
1486 struct list_head job_node;
1487 struct page **pages;
1488 unsigned int npages;
1489 struct sg_table *sgt;
1490 enum dma_data_direction dir;
1491 struct list_head debugfs_list;
1492 pid_t pid;
1493 u64 addr;
1494 u64 size;
1495 u8 dma_mapped;
1496 };
1497
1498 /**
1499 * struct hl_cs - command submission.
1500 * @jobs_in_queue_cnt: per each queue, maintain counter of submitted jobs.
1501 * @ctx: the context this CS belongs to.
1502 * @job_list: list of the CS's jobs in the various queues.
1503 * @job_lock: spinlock for the CS's jobs list. Needed for free_job.
1504 * @refcount: reference counter for usage of the CS.
1505 * @fence: pointer to the fence object of this CS.
1506 * @signal_fence: pointer to the fence object of the signal CS (used by wait
1507 * CS only).
1508 * @finish_work: workqueue object to run when CS is completed by H/W.
1509 * @work_tdr: delayed work node for TDR.
1510 * @mirror_node : node in device mirror list of command submissions.
1511 * @staged_cs_node: node in the staged cs list.
1512 * @debugfs_list: node in debugfs list of command submissions.
1513 * @encaps_sig_hdl: holds the encaps signals handle.
1514 * @sequence: the sequence number of this CS.
1515 * @staged_sequence: the sequence of the staged submission this CS is part of,
1516 * relevant only if staged_cs is set.
1517 * @timeout_jiffies: cs timeout in jiffies.
1518 * @submission_time_jiffies: submission time of the cs
1519 * @type: CS_TYPE_*.
1520 * @encaps_sig_hdl_id: encaps signals handle id, set for the first staged cs.
1521 * @submitted: true if CS was submitted to H/W.
1522 * @completed: true if CS was completed by device.
1523 * @timedout : true if CS was timedout.
1524 * @tdr_active: true if TDR was activated for this CS (to prevent
1525 * double TDR activation).
1526 * @aborted: true if CS was aborted due to some device error.
1527 * @timestamp: true if a timestmap must be captured upon completion.
1528 * @staged_last: true if this is the last staged CS and needs completion.
1529 * @staged_first: true if this is the first staged CS and we need to receive
1530 * timeout for this CS.
1531 * @staged_cs: true if this CS is part of a staged submission.
1532 * @skip_reset_on_timeout: true if we shall not reset the device in case
1533 * timeout occurs (debug scenario).
1534 * @encaps_signals: true if this CS has encaps reserved signals.
1535 */
1536 struct hl_cs {
1537 u16 *jobs_in_queue_cnt;
1538 struct hl_ctx *ctx;
1539 struct list_head job_list;
1540 spinlock_t job_lock;
1541 struct kref refcount;
1542 struct hl_fence *fence;
1543 struct hl_fence *signal_fence;
1544 struct work_struct finish_work;
1545 struct delayed_work work_tdr;
1546 struct list_head mirror_node;
1547 struct list_head staged_cs_node;
1548 struct list_head debugfs_list;
1549 struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
1550 u64 sequence;
1551 u64 staged_sequence;
1552 u64 timeout_jiffies;
1553 u64 submission_time_jiffies;
1554 enum hl_cs_type type;
1555 u32 encaps_sig_hdl_id;
1556 u8 submitted;
1557 u8 completed;
1558 u8 timedout;
1559 u8 tdr_active;
1560 u8 aborted;
1561 u8 timestamp;
1562 u8 staged_last;
1563 u8 staged_first;
1564 u8 staged_cs;
1565 u8 skip_reset_on_timeout;
1566 u8 encaps_signals;
1567 };
1568
1569 /**
1570 * struct hl_cs_job - command submission job.
1571 * @cs_node: the node to hang on the CS jobs list.
1572 * @cs: the CS this job belongs to.
1573 * @user_cb: the CB we got from the user.
1574 * @patched_cb: in case of patching, this is internal CB which is submitted on
1575 * the queue instead of the CB we got from the IOCTL.
1576 * @finish_work: workqueue object to run when job is completed.
1577 * @userptr_list: linked-list of userptr mappings that belong to this job and
1578 * wait for completion.
1579 * @debugfs_list: node in debugfs list of command submission jobs.
1580 * @refcount: reference counter for usage of the CS job.
1581 * @queue_type: the type of the H/W queue this job is submitted to.
1582 * @id: the id of this job inside a CS.
1583 * @hw_queue_id: the id of the H/W queue this job is submitted to.
1584 * @user_cb_size: the actual size of the CB we got from the user.
1585 * @job_cb_size: the actual size of the CB that we put on the queue.
1586 * @encaps_sig_wait_offset: encapsulated signals offset, which allow user
1587 * to wait on part of the reserved signals.
1588 * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
1589 * handle to a kernel-allocated CB object, false
1590 * otherwise (SRAM/DRAM/host address).
1591 * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
1592 * info is needed later, when adding the 2xMSG_PROT at the
1593 * end of the JOB, to know which barriers to put in the
1594 * MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
1595 * have streams so the engine can't be busy by another
1596 * stream.
1597 */
1598 struct hl_cs_job {
1599 struct list_head cs_node;
1600 struct hl_cs *cs;
1601 struct hl_cb *user_cb;
1602 struct hl_cb *patched_cb;
1603 struct work_struct finish_work;
1604 struct list_head userptr_list;
1605 struct list_head debugfs_list;
1606 struct kref refcount;
1607 enum hl_queue_type queue_type;
1608 u32 id;
1609 u32 hw_queue_id;
1610 u32 user_cb_size;
1611 u32 job_cb_size;
1612 u32 encaps_sig_wait_offset;
1613 u8 is_kernel_allocated_cb;
1614 u8 contains_dma_pkt;
1615 };
1616
1617 /**
1618 * struct hl_cs_parser - command submission parser properties.
1619 * @user_cb: the CB we got from the user.
1620 * @patched_cb: in case of patching, this is internal CB which is submitted on
1621 * the queue instead of the CB we got from the IOCTL.
1622 * @job_userptr_list: linked-list of userptr mappings that belong to the related
1623 * job and wait for completion.
1624 * @cs_sequence: the sequence number of the related CS.
1625 * @queue_type: the type of the H/W queue this job is submitted to.
1626 * @ctx_id: the ID of the context the related CS belongs to.
1627 * @hw_queue_id: the id of the H/W queue this job is submitted to.
1628 * @user_cb_size: the actual size of the CB we got from the user.
1629 * @patched_cb_size: the size of the CB after parsing.
1630 * @job_id: the id of the related job inside the related CS.
1631 * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
1632 * handle to a kernel-allocated CB object, false
1633 * otherwise (SRAM/DRAM/host address).
1634 * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
1635 * info is needed later, when adding the 2xMSG_PROT at the
1636 * end of the JOB, to know which barriers to put in the
1637 * MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
1638 * have streams so the engine can't be busy by another
1639 * stream.
1640 * @completion: true if we need completion for this CS.
1641 */
1642 struct hl_cs_parser {
1643 struct hl_cb *user_cb;
1644 struct hl_cb *patched_cb;
1645 struct list_head *job_userptr_list;
1646 u64 cs_sequence;
1647 enum hl_queue_type queue_type;
1648 u32 ctx_id;
1649 u32 hw_queue_id;
1650 u32 user_cb_size;
1651 u32 patched_cb_size;
1652 u8 job_id;
1653 u8 is_kernel_allocated_cb;
1654 u8 contains_dma_pkt;
1655 u8 completion;
1656 };
1657
1658 /*
1659 * MEMORY STRUCTURE
1660 */
1661
1662 /**
1663 * struct hl_vm_hash_node - hash element from virtual address to virtual
1664 * memory area descriptor (hl_vm_phys_pg_list or
1665 * hl_userptr).
1666 * @node: node to hang on the hash table in context object.
1667 * @vaddr: key virtual address.
1668 * @ptr: value pointer (hl_vm_phys_pg_list or hl_userptr).
1669 */
1670 struct hl_vm_hash_node {
1671 struct hlist_node node;
1672 u64 vaddr;
1673 void *ptr;
1674 };
1675
1676 /**
1677 * struct hl_vm_hw_block_list_node - list element from user virtual address to
1678 * HW block id.
1679 * @node: node to hang on the list in context object.
1680 * @ctx: the context this node belongs to.
1681 * @vaddr: virtual address of the HW block.
1682 * @size: size of the block.
1683 * @id: HW block id (handle).
1684 */
1685 struct hl_vm_hw_block_list_node {
1686 struct list_head node;
1687 struct hl_ctx *ctx;
1688 unsigned long vaddr;
1689 u32 size;
1690 u32 id;
1691 };
1692
1693 /**
1694 * struct hl_vm_phys_pg_pack - physical page pack.
1695 * @vm_type: describes the type of the virtual area descriptor.
1696 * @pages: the physical page array.
1697 * @npages: num physical pages in the pack.
1698 * @total_size: total size of all the pages in this list.
1699 * @mapping_cnt: number of shared mappings.
1700 * @exporting_cnt: number of dma-buf exporting.
1701 * @asid: the context related to this list.
1702 * @page_size: size of each page in the pack.
1703 * @flags: HL_MEM_* flags related to this list.
1704 * @handle: the provided handle related to this list.
1705 * @offset: offset from the first page.
1706 * @contiguous: is contiguous physical memory.
1707 * @created_from_userptr: is product of host virtual address.
1708 */
1709 struct hl_vm_phys_pg_pack {
1710 enum vm_type vm_type; /* must be first */
1711 u64 *pages;
1712 u64 npages;
1713 u64 total_size;
1714 atomic_t mapping_cnt;
1715 u32 exporting_cnt;
1716 u32 asid;
1717 u32 page_size;
1718 u32 flags;
1719 u32 handle;
1720 u32 offset;
1721 u8 contiguous;
1722 u8 created_from_userptr;
1723 };
1724
1725 /**
1726 * struct hl_vm_va_block - virtual range block information.
1727 * @node: node to hang on the virtual range list in context object.
1728 * @start: virtual range start address.
1729 * @end: virtual range end address.
1730 * @size: virtual range size.
1731 */
1732 struct hl_vm_va_block {
1733 struct list_head node;
1734 u64 start;
1735 u64 end;
1736 u64 size;
1737 };
1738
1739 /**
1740 * struct hl_vm - virtual memory manager for MMU.
1741 * @dram_pg_pool: pool for DRAM physical pages of 2MB.
1742 * @dram_pg_pool_refcount: reference counter for the pool usage.
1743 * @idr_lock: protects the phys_pg_list_handles.
1744 * @phys_pg_pack_handles: idr to hold all device allocations handles.
1745 * @init_done: whether initialization was done. We need this because VM
1746 * initialization might be skipped during device initialization.
1747 */
1748 struct hl_vm {
1749 struct gen_pool *dram_pg_pool;
1750 struct kref dram_pg_pool_refcount;
1751 spinlock_t idr_lock;
1752 struct idr phys_pg_pack_handles;
1753 u8 init_done;
1754 };
1755
1756
1757 /*
1758 * DEBUG, PROFILING STRUCTURE
1759 */
1760
1761 /**
1762 * struct hl_debug_params - Coresight debug parameters.
1763 * @input: pointer to component specific input parameters.
1764 * @output: pointer to component specific output parameters.
1765 * @output_size: size of output buffer.
1766 * @reg_idx: relevant register ID.
1767 * @op: component operation to execute.
1768 * @enable: true if to enable component debugging, false otherwise.
1769 */
1770 struct hl_debug_params {
1771 void *input;
1772 void *output;
1773 u32 output_size;
1774 u32 reg_idx;
1775 u32 op;
1776 bool enable;
1777 };
1778
1779 /*
1780 * FILE PRIVATE STRUCTURE
1781 */
1782
1783 /**
1784 * struct hl_fpriv - process information stored in FD private data.
1785 * @hdev: habanalabs device structure.
1786 * @filp: pointer to the given file structure.
1787 * @taskpid: current process ID.
1788 * @ctx: current executing context. TODO: remove for multiple ctx per process
1789 * @ctx_mgr: context manager to handle multiple context for this FD.
1790 * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
1791 * @debugfs_list: list of relevant ASIC debugfs.
1792 * @dev_node: node in the device list of file private data
1793 * @refcount: number of related contexts.
1794 * @restore_phase_mutex: lock for context switch and restore phase.
1795 * @is_control: true for control device, false otherwise
1796 */
1797 struct hl_fpriv {
1798 struct hl_device *hdev;
1799 struct file *filp;
1800 struct pid *taskpid;
1801 struct hl_ctx *ctx;
1802 struct hl_ctx_mgr ctx_mgr;
1803 struct hl_cb_mgr cb_mgr;
1804 struct list_head debugfs_list;
1805 struct list_head dev_node;
1806 struct kref refcount;
1807 struct mutex restore_phase_mutex;
1808 u8 is_control;
1809 };
1810
1811
1812 /*
1813 * DebugFS
1814 */
1815
1816 /**
1817 * struct hl_info_list - debugfs file ops.
1818 * @name: file name.
1819 * @show: function to output information.
1820 * @write: function to write to the file.
1821 */
1822 struct hl_info_list {
1823 const char *name;
1824 int (*show)(struct seq_file *s, void *data);
1825 ssize_t (*write)(struct file *file, const char __user *buf,
1826 size_t count, loff_t *f_pos);
1827 };
1828
1829 /**
1830 * struct hl_debugfs_entry - debugfs dentry wrapper.
1831 * @info_ent: dentry realted ops.
1832 * @dev_entry: ASIC specific debugfs manager.
1833 */
1834 struct hl_debugfs_entry {
1835 const struct hl_info_list *info_ent;
1836 struct hl_dbg_device_entry *dev_entry;
1837 };
1838
1839 /**
1840 * struct hl_dbg_device_entry - ASIC specific debugfs manager.
1841 * @root: root dentry.
1842 * @hdev: habanalabs device structure.
1843 * @entry_arr: array of available hl_debugfs_entry.
1844 * @file_list: list of available debugfs files.
1845 * @file_mutex: protects file_list.
1846 * @cb_list: list of available CBs.
1847 * @cb_spinlock: protects cb_list.
1848 * @cs_list: list of available CSs.
1849 * @cs_spinlock: protects cs_list.
1850 * @cs_job_list: list of available CB jobs.
1851 * @cs_job_spinlock: protects cs_job_list.
1852 * @userptr_list: list of available userptrs (virtual memory chunk descriptor).
1853 * @userptr_spinlock: protects userptr_list.
1854 * @ctx_mem_hash_list: list of available contexts with MMU mappings.
1855 * @ctx_mem_hash_spinlock: protects cb_list.
1856 * @blob_desc: descriptor of blob
1857 * @state_dump: data of the system states in case of a bad cs.
1858 * @state_dump_sem: protects state_dump.
1859 * @addr: next address to read/write from/to in read/write32.
1860 * @mmu_addr: next virtual address to translate to physical address in mmu_show.
1861 * @userptr_lookup: the target user ptr to look up for on demand.
1862 * @mmu_asid: ASID to use while translating in mmu_show.
1863 * @state_dump_head: index of the latest state dump
1864 * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
1865 * @i2c_addr: generic u8 debugfs file for address value to use in i2c_data_read.
1866 * @i2c_reg: generic u8 debugfs file for register value to use in i2c_data_read.
1867 */
1868 struct hl_dbg_device_entry {
1869 struct dentry *root;
1870 struct hl_device *hdev;
1871 struct hl_debugfs_entry *entry_arr;
1872 struct list_head file_list;
1873 struct mutex file_mutex;
1874 struct list_head cb_list;
1875 spinlock_t cb_spinlock;
1876 struct list_head cs_list;
1877 spinlock_t cs_spinlock;
1878 struct list_head cs_job_list;
1879 spinlock_t cs_job_spinlock;
1880 struct list_head userptr_list;
1881 spinlock_t userptr_spinlock;
1882 struct list_head ctx_mem_hash_list;
1883 spinlock_t ctx_mem_hash_spinlock;
1884 struct debugfs_blob_wrapper blob_desc;
1885 char *state_dump[HL_STATE_DUMP_HIST_LEN];
1886 struct rw_semaphore state_dump_sem;
1887 u64 addr;
1888 u64 mmu_addr;
1889 u64 userptr_lookup;
1890 u32 mmu_asid;
1891 u32 state_dump_head;
1892 u8 i2c_bus;
1893 u8 i2c_addr;
1894 u8 i2c_reg;
1895 };
1896
1897 /**
1898 * struct hl_hw_obj_name_entry - single hw object name, member of
1899 * hl_state_dump_specs
1900 * @node: link to the containing hash table
1901 * @name: hw object name
1902 * @id: object identifier
1903 */
1904 struct hl_hw_obj_name_entry {
1905 struct hlist_node node;
1906 const char *name;
1907 u32 id;
1908 };
1909
1910 enum hl_state_dump_specs_props {
1911 SP_SYNC_OBJ_BASE_ADDR,
1912 SP_NEXT_SYNC_OBJ_ADDR,
1913 SP_SYNC_OBJ_AMOUNT,
1914 SP_MON_OBJ_WR_ADDR_LOW,
1915 SP_MON_OBJ_WR_ADDR_HIGH,
1916 SP_MON_OBJ_WR_DATA,
1917 SP_MON_OBJ_ARM_DATA,
1918 SP_MON_OBJ_STATUS,
1919 SP_MONITORS_AMOUNT,
1920 SP_TPC0_CMDQ,
1921 SP_TPC0_CFG_SO,
1922 SP_NEXT_TPC,
1923 SP_MME_CMDQ,
1924 SP_MME_CFG_SO,
1925 SP_NEXT_MME,
1926 SP_DMA_CMDQ,
1927 SP_DMA_CFG_SO,
1928 SP_DMA_QUEUES_OFFSET,
1929 SP_NUM_OF_MME_ENGINES,
1930 SP_SUB_MME_ENG_NUM,
1931 SP_NUM_OF_DMA_ENGINES,
1932 SP_NUM_OF_TPC_ENGINES,
1933 SP_ENGINE_NUM_OF_QUEUES,
1934 SP_ENGINE_NUM_OF_STREAMS,
1935 SP_ENGINE_NUM_OF_FENCES,
1936 SP_FENCE0_CNT_OFFSET,
1937 SP_FENCE0_RDATA_OFFSET,
1938 SP_CP_STS_OFFSET,
1939 SP_NUM_CORES,
1940
1941 SP_MAX
1942 };
1943
1944 enum hl_sync_engine_type {
1945 ENGINE_TPC,
1946 ENGINE_DMA,
1947 ENGINE_MME,
1948 };
1949
1950 /**
1951 * struct hl_mon_state_dump - represents a state dump of a single monitor
1952 * @id: monitor id
1953 * @wr_addr_low: address monitor will write to, low bits
1954 * @wr_addr_high: address monitor will write to, high bits
1955 * @wr_data: data monitor will write
1956 * @arm_data: register value containing monitor configuration
1957 * @status: monitor status
1958 */
1959 struct hl_mon_state_dump {
1960 u32 id;
1961 u32 wr_addr_low;
1962 u32 wr_addr_high;
1963 u32 wr_data;
1964 u32 arm_data;
1965 u32 status;
1966 };
1967
1968 /**
1969 * struct hl_sync_to_engine_map_entry - sync object id to engine mapping entry
1970 * @engine_type: type of the engine
1971 * @engine_id: id of the engine
1972 * @sync_id: id of the sync object
1973 */
1974 struct hl_sync_to_engine_map_entry {
1975 struct hlist_node node;
1976 enum hl_sync_engine_type engine_type;
1977 u32 engine_id;
1978 u32 sync_id;
1979 };
1980
1981 /**
1982 * struct hl_sync_to_engine_map - maps sync object id to associated engine id
1983 * @tb: hash table containing the mapping, each element is of type
1984 * struct hl_sync_to_engine_map_entry
1985 */
1986 struct hl_sync_to_engine_map {
1987 DECLARE_HASHTABLE(tb, SYNC_TO_ENGINE_HASH_TABLE_BITS);
1988 };
1989
1990 /**
1991 * struct hl_state_dump_specs_funcs - virtual functions used by the state dump
1992 * @gen_sync_to_engine_map: generate a hash map from sync obj id to its engine
1993 * @print_single_monitor: format monitor data as string
1994 * @monitor_valid: return true if given monitor dump is valid
1995 * @print_fences_single_engine: format fences data as string
1996 */
1997 struct hl_state_dump_specs_funcs {
1998 int (*gen_sync_to_engine_map)(struct hl_device *hdev,
1999 struct hl_sync_to_engine_map *map);
2000 int (*print_single_monitor)(char **buf, size_t *size, size_t *offset,
2001 struct hl_device *hdev,
2002 struct hl_mon_state_dump *mon);
2003 int (*monitor_valid)(struct hl_mon_state_dump *mon);
2004 int (*print_fences_single_engine)(struct hl_device *hdev,
2005 u64 base_offset,
2006 u64 status_base_offset,
2007 enum hl_sync_engine_type engine_type,
2008 u32 engine_id, char **buf,
2009 size_t *size, size_t *offset);
2010 };
2011
2012 /**
2013 * struct hl_state_dump_specs - defines ASIC known hw objects names
2014 * @so_id_to_str_tb: sync objects names index table
2015 * @monitor_id_to_str_tb: monitors names index table
2016 * @funcs: virtual functions used for state dump
2017 * @sync_namager_names: readable names for sync manager if available (ex: N_E)
2018 * @props: pointer to a per asic const props array required for state dump
2019 */
2020 struct hl_state_dump_specs {
2021 DECLARE_HASHTABLE(so_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
2022 DECLARE_HASHTABLE(monitor_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
2023 struct hl_state_dump_specs_funcs funcs;
2024 const char * const *sync_namager_names;
2025 s64 *props;
2026 };
2027
2028
2029 /*
2030 * DEVICES
2031 */
2032
2033 #define HL_STR_MAX 32
2034
2035 #define HL_DEV_STS_MAX (HL_DEVICE_STATUS_LAST + 1)
2036
2037 /* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
2038 * x16 cards. In extreme cases, there are hosts that can accommodate 16 cards.
2039 */
2040 #define HL_MAX_MINORS 256
2041
2042 /*
2043 * Registers read & write functions.
2044 */
2045
2046 u32 hl_rreg(struct hl_device *hdev, u32 reg);
2047 void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
2048
2049 #define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
2050 #define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
2051 #define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n", \
2052 hdev->asic_funcs->rreg(hdev, (reg)))
2053
2054 #define WREG32_P(reg, val, mask) \
2055 do { \
2056 u32 tmp_ = RREG32(reg); \
2057 tmp_ &= (mask); \
2058 tmp_ |= ((val) & ~(mask)); \
2059 WREG32(reg, tmp_); \
2060 } while (0)
2061 #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
2062 #define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
2063
2064 #define RMWREG32(reg, val, mask) \
2065 do { \
2066 u32 tmp_ = RREG32(reg); \
2067 tmp_ &= ~(mask); \
2068 tmp_ |= ((val) << __ffs(mask)); \
2069 WREG32(reg, tmp_); \
2070 } while (0)
2071
2072 #define RREG32_MASK(reg, mask) ((RREG32(reg) & mask) >> __ffs(mask))
2073
2074 #define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
2075 #define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
2076 #define WREG32_FIELD(reg, offset, field, val) \
2077 WREG32(mm##reg + offset, (RREG32(mm##reg + offset) & \
2078 ~REG_FIELD_MASK(reg, field)) | \
2079 (val) << REG_FIELD_SHIFT(reg, field))
2080
2081 /* Timeout should be longer when working with simulator but cap the
2082 * increased timeout to some maximum
2083 */
2084 #define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
2085 ({ \
2086 ktime_t __timeout; \
2087 if (hdev->pdev) \
2088 __timeout = ktime_add_us(ktime_get(), timeout_us); \
2089 else \
2090 __timeout = ktime_add_us(ktime_get(),\
2091 min((u64)(timeout_us * 10), \
2092 (u64) HL_SIM_MAX_TIMEOUT_US)); \
2093 might_sleep_if(sleep_us); \
2094 for (;;) { \
2095 (val) = RREG32(addr); \
2096 if (cond) \
2097 break; \
2098 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2099 (val) = RREG32(addr); \
2100 break; \
2101 } \
2102 if (sleep_us) \
2103 usleep_range((sleep_us >> 2) + 1, sleep_us); \
2104 } \
2105 (cond) ? 0 : -ETIMEDOUT; \
2106 })
2107
2108 /*
2109 * address in this macro points always to a memory location in the
2110 * host's (server's) memory. That location is updated asynchronously
2111 * either by the direct access of the device or by another core.
2112 *
2113 * To work both in LE and BE architectures, we need to distinguish between the
2114 * two states (device or another core updates the memory location). Therefore,
2115 * if mem_written_by_device is true, the host memory being polled will be
2116 * updated directly by the device. If false, the host memory being polled will
2117 * be updated by host CPU. Required so host knows whether or not the memory
2118 * might need to be byte-swapped before returning value to caller.
2119 */
2120 #define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
2121 mem_written_by_device) \
2122 ({ \
2123 ktime_t __timeout; \
2124 if (hdev->pdev) \
2125 __timeout = ktime_add_us(ktime_get(), timeout_us); \
2126 else \
2127 __timeout = ktime_add_us(ktime_get(),\
2128 min((u64)(timeout_us * 10), \
2129 (u64) HL_SIM_MAX_TIMEOUT_US)); \
2130 might_sleep_if(sleep_us); \
2131 for (;;) { \
2132 /* Verify we read updates done by other cores or by device */ \
2133 mb(); \
2134 (val) = *((u32 *)(addr)); \
2135 if (mem_written_by_device) \
2136 (val) = le32_to_cpu(*(__le32 *) &(val)); \
2137 if (cond) \
2138 break; \
2139 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2140 (val) = *((u32 *)(addr)); \
2141 if (mem_written_by_device) \
2142 (val) = le32_to_cpu(*(__le32 *) &(val)); \
2143 break; \
2144 } \
2145 if (sleep_us) \
2146 usleep_range((sleep_us >> 2) + 1, sleep_us); \
2147 } \
2148 (cond) ? 0 : -ETIMEDOUT; \
2149 })
2150
2151 #define hl_poll_timeout_device_memory(hdev, addr, val, cond, sleep_us, \
2152 timeout_us) \
2153 ({ \
2154 ktime_t __timeout; \
2155 if (hdev->pdev) \
2156 __timeout = ktime_add_us(ktime_get(), timeout_us); \
2157 else \
2158 __timeout = ktime_add_us(ktime_get(),\
2159 min((u64)(timeout_us * 10), \
2160 (u64) HL_SIM_MAX_TIMEOUT_US)); \
2161 might_sleep_if(sleep_us); \
2162 for (;;) { \
2163 (val) = readl(addr); \
2164 if (cond) \
2165 break; \
2166 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2167 (val) = readl(addr); \
2168 break; \
2169 } \
2170 if (sleep_us) \
2171 usleep_range((sleep_us >> 2) + 1, sleep_us); \
2172 } \
2173 (cond) ? 0 : -ETIMEDOUT; \
2174 })
2175
2176 struct hwmon_chip_info;
2177
2178 /**
2179 * struct hl_device_reset_work - reset workqueue task wrapper.
2180 * @wq: work queue for device reset procedure.
2181 * @reset_work: reset work to be done.
2182 * @hdev: habanalabs device structure.
2183 * @fw_reset: whether f/w will do the reset without us sending them a message to do it.
2184 */
2185 struct hl_device_reset_work {
2186 struct workqueue_struct *wq;
2187 struct delayed_work reset_work;
2188 struct hl_device *hdev;
2189 bool fw_reset;
2190 };
2191
2192 /**
2193 * struct hr_mmu_hop_addrs - used for holding per-device host-resident mmu hop
2194 * information.
2195 * @virt_addr: the virtual address of the hop.
2196 * @phys-addr: the physical address of the hop (used by the device-mmu).
2197 * @shadow_addr: The shadow of the hop used by the driver for walking the hops.
2198 */
2199 struct hr_mmu_hop_addrs {
2200 u64 virt_addr;
2201 u64 phys_addr;
2202 u64 shadow_addr;
2203 };
2204
2205 /**
2206 * struct hl_mmu_hr_pgt_priv - used for holding per-device mmu host-resident
2207 * page-table internal information.
2208 * @mmu_pgt_pool: pool of page tables used by MMU for allocating hops.
2209 * @mmu_shadow_hop0: shadow array of hop0 tables.
2210 */
2211 struct hl_mmu_hr_priv {
2212 struct gen_pool *mmu_pgt_pool;
2213 struct hr_mmu_hop_addrs *mmu_shadow_hop0;
2214 };
2215
2216 /**
2217 * struct hl_mmu_dr_pgt_priv - used for holding per-device mmu device-resident
2218 * page-table internal information.
2219 * @mmu_pgt_pool: pool of page tables used by MMU for allocating hops.
2220 * @mmu_shadow_hop0: shadow array of hop0 tables.
2221 */
2222 struct hl_mmu_dr_priv {
2223 struct gen_pool *mmu_pgt_pool;
2224 void *mmu_shadow_hop0;
2225 };
2226
2227 /**
2228 * struct hl_mmu_priv - used for holding per-device mmu internal information.
2229 * @dr: information on the device-resident MMU, when exists.
2230 * @hr: information on the host-resident MMU, when exists.
2231 */
2232 struct hl_mmu_priv {
2233 struct hl_mmu_dr_priv dr;
2234 struct hl_mmu_hr_priv hr;
2235 };
2236
2237 /**
2238 * struct hl_mmu_per_hop_info - A structure describing one TLB HOP and its entry
2239 * that was created in order to translate a virtual address to a
2240 * physical one.
2241 * @hop_addr: The address of the hop.
2242 * @hop_pte_addr: The address of the hop entry.
2243 * @hop_pte_val: The value in the hop entry.
2244 */
2245 struct hl_mmu_per_hop_info {
2246 u64 hop_addr;
2247 u64 hop_pte_addr;
2248 u64 hop_pte_val;
2249 };
2250
2251 /**
2252 * struct hl_mmu_hop_info - A structure describing the TLB hops and their
2253 * hop-entries that were created in order to translate a virtual address to a
2254 * physical one.
2255 * @scrambled_vaddr: The value of the virtual address after scrambling. This
2256 * address replaces the original virtual-address when mapped
2257 * in the MMU tables.
2258 * @unscrambled_paddr: The un-scrambled physical address.
2259 * @hop_info: Array holding the per-hop information used for the translation.
2260 * @used_hops: The number of hops used for the translation.
2261 * @range_type: virtual address range type.
2262 */
2263 struct hl_mmu_hop_info {
2264 u64 scrambled_vaddr;
2265 u64 unscrambled_paddr;
2266 struct hl_mmu_per_hop_info hop_info[MMU_ARCH_5_HOPS];
2267 u32 used_hops;
2268 enum hl_va_range_type range_type;
2269 };
2270
2271 /**
2272 * struct hl_mmu_funcs - Device related MMU functions.
2273 * @init: initialize the MMU module.
2274 * @fini: release the MMU module.
2275 * @ctx_init: Initialize a context for using the MMU module.
2276 * @ctx_fini: disable a ctx from using the mmu module.
2277 * @map: maps a virtual address to physical address for a context.
2278 * @unmap: unmap a virtual address of a context.
2279 * @flush: flush all writes from all cores to reach device MMU.
2280 * @swap_out: marks all mapping of the given context as swapped out.
2281 * @swap_in: marks all mapping of the given context as swapped in.
2282 * @get_tlb_info: returns the list of hops and hop-entries used that were
2283 * created in order to translate the giver virtual address to a
2284 * physical one.
2285 */
2286 struct hl_mmu_funcs {
2287 int (*init)(struct hl_device *hdev);
2288 void (*fini)(struct hl_device *hdev);
2289 int (*ctx_init)(struct hl_ctx *ctx);
2290 void (*ctx_fini)(struct hl_ctx *ctx);
2291 int (*map)(struct hl_ctx *ctx,
2292 u64 virt_addr, u64 phys_addr, u32 page_size,
2293 bool is_dram_addr);
2294 int (*unmap)(struct hl_ctx *ctx,
2295 u64 virt_addr, bool is_dram_addr);
2296 void (*flush)(struct hl_ctx *ctx);
2297 void (*swap_out)(struct hl_ctx *ctx);
2298 void (*swap_in)(struct hl_ctx *ctx);
2299 int (*get_tlb_info)(struct hl_ctx *ctx,
2300 u64 virt_addr, struct hl_mmu_hop_info *hops);
2301 };
2302
2303 /**
2304 * number of user contexts allowed to call wait_for_multi_cs ioctl in
2305 * parallel
2306 */
2307 #define MULTI_CS_MAX_USER_CTX 2
2308
2309 /**
2310 * struct multi_cs_completion - multi CS wait completion.
2311 * @completion: completion of any of the CS in the list
2312 * @lock: spinlock for the completion structure
2313 * @timestamp: timestamp for the multi-CS completion
2314 * @stream_master_qid_map: bitmap of all stream masters on which the multi-CS
2315 * is waiting
2316 * @used: 1 if in use, otherwise 0
2317 */
2318 struct multi_cs_completion {
2319 struct completion completion;
2320 spinlock_t lock;
2321 s64 timestamp;
2322 u32 stream_master_qid_map;
2323 u8 used;
2324 };
2325
2326 /**
2327 * struct multi_cs_data - internal data for multi CS call
2328 * @ctx: pointer to the context structure
2329 * @fence_arr: array of fences of all CSs
2330 * @seq_arr: array of CS sequence numbers
2331 * @timeout_us: timeout in usec for waiting for CS to complete
2332 * @timestamp: timestamp of first completed CS
2333 * @wait_status: wait for CS status
2334 * @completion_bitmap: bitmap of completed CSs (1- completed, otherwise 0)
2335 * @stream_master_qid_map: bitmap of all stream master QIDs on which the
2336 * multi-CS is waiting
2337 * @arr_len: fence_arr and seq_arr array length
2338 * @gone_cs: indication of gone CS (1- there was gone CS, otherwise 0)
2339 * @update_ts: update timestamp. 1- update the timestamp, otherwise 0.
2340 */
2341 struct multi_cs_data {
2342 struct hl_ctx *ctx;
2343 struct hl_fence **fence_arr;
2344 u64 *seq_arr;
2345 s64 timeout_us;
2346 s64 timestamp;
2347 long wait_status;
2348 u32 completion_bitmap;
2349 u32 stream_master_qid_map;
2350 u8 arr_len;
2351 u8 gone_cs;
2352 u8 update_ts;
2353 };
2354
2355 /**
2356 * struct hl_device - habanalabs device structure.
2357 * @pdev: pointer to PCI device, can be NULL in case of simulator device.
2358 * @pcie_bar_phys: array of available PCIe bars physical addresses.
2359 * (required only for PCI address match mode)
2360 * @pcie_bar: array of available PCIe bars virtual addresses.
2361 * @rmmio: configuration area address on SRAM.
2362 * @cdev: related char device.
2363 * @cdev_ctrl: char device for control operations only (INFO IOCTL)
2364 * @dev: related kernel basic device structure.
2365 * @dev_ctrl: related kernel device structure for the control device
2366 * @work_freq: delayed work to lower device frequency if possible.
2367 * @work_heartbeat: delayed work for CPU-CP is-alive check.
2368 * @device_reset_work: delayed work which performs hard reset
2369 * @asic_name: ASIC specific name.
2370 * @asic_type: ASIC specific type.
2371 * @completion_queue: array of hl_cq.
2372 * @user_interrupt: array of hl_user_interrupt. upon the corresponding user
2373 * interrupt, driver will monitor the list of fences
2374 * registered to this interrupt.
2375 * @common_user_interrupt: common user interrupt for all user interrupts.
2376 * upon any user interrupt, driver will monitor the
2377 * list of fences registered to this common structure.
2378 * @cq_wq: work queues of completion queues for executing work in process
2379 * context.
2380 * @eq_wq: work queue of event queue for executing work in process context.
2381 * @sob_reset_wq: work queue for sob reset executions.
2382 * @kernel_ctx: Kernel driver context structure.
2383 * @kernel_queues: array of hl_hw_queue.
2384 * @cs_mirror_list: CS mirror list for TDR.
2385 * @cs_mirror_lock: protects cs_mirror_list.
2386 * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CBs.
2387 * @event_queue: event queue for IRQ from CPU-CP.
2388 * @dma_pool: DMA pool for small allocations.
2389 * @cpu_accessible_dma_mem: Host <-> CPU-CP shared memory CPU address.
2390 * @cpu_accessible_dma_address: Host <-> CPU-CP shared memory DMA address.
2391 * @cpu_accessible_dma_pool: Host <-> CPU-CP shared memory pool.
2392 * @asid_bitmap: holds used/available ASIDs.
2393 * @asid_mutex: protects asid_bitmap.
2394 * @send_cpu_message_lock: enforces only one message in Host <-> CPU-CP queue.
2395 * @debug_lock: protects critical section of setting debug mode for device
2396 * @asic_prop: ASIC specific immutable properties.
2397 * @asic_funcs: ASIC specific functions.
2398 * @asic_specific: ASIC specific information to use only from ASIC files.
2399 * @vm: virtual memory manager for MMU.
2400 * @hwmon_dev: H/W monitor device.
2401 * @pm_mng_profile: current power management profile.
2402 * @hl_chip_info: ASIC's sensors information.
2403 * @device_status_description: device status description.
2404 * @hl_debugfs: device's debugfs manager.
2405 * @cb_pool: list of preallocated CBs.
2406 * @cb_pool_lock: protects the CB pool.
2407 * @internal_cb_pool_virt_addr: internal command buffer pool virtual address.
2408 * @internal_cb_pool_dma_addr: internal command buffer pool dma address.
2409 * @internal_cb_pool: internal command buffer memory pool.
2410 * @internal_cb_va_base: internal cb pool mmu virtual address base
2411 * @fpriv_list: list of file private data structures. Each structure is created
2412 * when a user opens the device
2413 * @fpriv_list_lock: protects the fpriv_list
2414 * @compute_ctx: current compute context executing.
2415 * @aggregated_cs_counters: aggregated cs counters among all contexts
2416 * @mmu_priv: device-specific MMU data.
2417 * @mmu_func: device-related MMU functions.
2418 * @fw_loader: FW loader manager.
2419 * @pci_mem_region: array of memory regions in the PCI
2420 * @state_dump_specs: constants and dictionaries needed to dump system state.
2421 * @multi_cs_completion: array of multi-CS completion.
2422 * @dram_used_mem: current DRAM memory consumption.
2423 * @timeout_jiffies: device CS timeout value.
2424 * @max_power: the max power of the device, as configured by the sysadmin. This
2425 * value is saved so in case of hard-reset, the driver will restore
2426 * this value and update the F/W after the re-initialization
2427 * @clock_gating_mask: is clock gating enabled. bitmask that represents the
2428 * different engines. See debugfs-driver-habanalabs for
2429 * details.
2430 * @boot_error_status_mask: contains a mask of the device boot error status.
2431 * Each bit represents a different error, according to
2432 * the defines in hl_boot_if.h. If the bit is cleared,
2433 * the error will be ignored by the driver during
2434 * device initialization. Mainly used to debug and
2435 * workaround firmware bugs
2436 * @dram_pci_bar_start: start bus address of PCIe bar towards DRAM.
2437 * @last_successful_open_jif: timestamp (jiffies) of the last successful
2438 * device open.
2439 * @last_open_session_duration_jif: duration (jiffies) of the last device open
2440 * session.
2441 * @open_counter: number of successful device open operations.
2442 * @in_reset: is device in reset flow.
2443 * @curr_pll_profile: current PLL profile.
2444 * @card_type: Various ASICs have several card types. This indicates the card
2445 * type of the current device.
2446 * @major: habanalabs kernel driver major.
2447 * @high_pll: high PLL profile frequency.
2448 * @soft_reset_cnt: number of soft reset since the driver was loaded.
2449 * @hard_reset_cnt: number of hard reset since the driver was loaded.
2450 * @clk_throttling_reason: bitmask represents the current clk throttling reasons
2451 * @id: device minor.
2452 * @id_control: minor of the control device
2453 * @cpu_pci_msb_addr: 50-bit extension bits for the device CPU's 40-bit
2454 * addresses.
2455 * @disabled: is device disabled.
2456 * @late_init_done: is late init stage was done during initialization.
2457 * @hwmon_initialized: is H/W monitor sensors was initialized.
2458 * @hard_reset_pending: is there a hard reset work pending.
2459 * @heartbeat: is heartbeat sanity check towards CPU-CP enabled.
2460 * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
2461 * otherwise.
2462 * @dram_default_page_mapping: is DRAM default page mapping enabled.
2463 * @memory_scrub: true to perform device memory scrub in various locations,
2464 * such as context-switch, context close, page free, etc.
2465 * @pmmu_huge_range: is a different virtual addresses range used for PMMU with
2466 * huge pages.
2467 * @init_done: is the initialization of the device done.
2468 * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
2469 * @dma_mask: the dma mask that was set for this device
2470 * @in_debug: is device under debug. This, together with fpriv_list, enforces
2471 * that only a single user is configuring the debug infrastructure.
2472 * @power9_64bit_dma_enable: true to enable 64-bit DMA mask support. Relevant
2473 * only to POWER9 machines.
2474 * @cdev_sysfs_created: were char devices and sysfs nodes created.
2475 * @stop_on_err: true if engines should stop on error.
2476 * @supports_sync_stream: is sync stream supported.
2477 * @sync_stream_queue_idx: helper index for sync stream queues initialization.
2478 * @collective_mon_idx: helper index for collective initialization
2479 * @supports_coresight: is CoreSight supported.
2480 * @supports_soft_reset: is soft reset supported.
2481 * @allow_inference_soft_reset: true if the ASIC supports soft reset that is
2482 * initiated by user or TDR. This is only true
2483 * in inference ASICs, as there is no real-world
2484 * use-case of doing soft-reset in training (due
2485 * to the fact that training runs on multiple
2486 * devices)
2487 * @supports_cb_mapping: is mapping a CB to the device's MMU supported.
2488 * @needs_reset: true if reset_on_lockup is false and device should be reset
2489 * due to lockup.
2490 * @process_kill_trial_cnt: number of trials reset thread tried killing
2491 * user processes
2492 * @device_fini_pending: true if device_fini was called and might be
2493 * waiting for the reset thread to finish
2494 * @supports_staged_submission: true if staged submissions are supported
2495 * @curr_reset_cause: saves an enumerated reset cause when a hard reset is
2496 * triggered, and cleared after it is shared with preboot.
2497 * @prev_reset_trigger: saves the previous trigger which caused a reset, overidden
2498 * with a new value on next reset
2499 * @reset_trigger_repeated: set if device reset is triggered more than once with
2500 * same cause.
2501 * @skip_reset_on_timeout: Skip device reset if CS has timed out, wait for it to
2502 * complete instead.
2503 * @device_cpu_is_halted: Flag to indicate whether the device CPU was already
2504 * halted. We can't halt it again because the COMMS
2505 * protocol will throw an error. Relevant only for
2506 * cases where Linux was not loaded to device CPU
2507 * @supports_wait_for_multi_cs: true if wait for multi CS is supported
2508 */
2509 struct hl_device {
2510 struct pci_dev *pdev;
2511 u64 pcie_bar_phys[HL_PCI_NUM_BARS];
2512 void __iomem *pcie_bar[HL_PCI_NUM_BARS];
2513 void __iomem *rmmio;
2514 struct cdev cdev;
2515 struct cdev cdev_ctrl;
2516 struct device *dev;
2517 struct device *dev_ctrl;
2518 struct delayed_work work_freq;
2519 struct delayed_work work_heartbeat;
2520 struct hl_device_reset_work device_reset_work;
2521 char asic_name[HL_STR_MAX];
2522 char status[HL_DEV_STS_MAX][HL_STR_MAX];
2523 enum hl_asic_type asic_type;
2524 struct hl_cq *completion_queue;
2525 struct hl_user_interrupt *user_interrupt;
2526 struct hl_user_interrupt common_user_interrupt;
2527 struct workqueue_struct **cq_wq;
2528 struct workqueue_struct *eq_wq;
2529 struct workqueue_struct *sob_reset_wq;
2530 struct hl_ctx *kernel_ctx;
2531 struct hl_hw_queue *kernel_queues;
2532 struct list_head cs_mirror_list;
2533 spinlock_t cs_mirror_lock;
2534 struct hl_cb_mgr kernel_cb_mgr;
2535 struct hl_eq event_queue;
2536 struct dma_pool *dma_pool;
2537 void *cpu_accessible_dma_mem;
2538 dma_addr_t cpu_accessible_dma_address;
2539 struct gen_pool *cpu_accessible_dma_pool;
2540 unsigned long *asid_bitmap;
2541 struct mutex asid_mutex;
2542 struct mutex send_cpu_message_lock;
2543 struct mutex debug_lock;
2544 struct asic_fixed_properties asic_prop;
2545 const struct hl_asic_funcs *asic_funcs;
2546 void *asic_specific;
2547 struct hl_vm vm;
2548 struct device *hwmon_dev;
2549 enum hl_pm_mng_profile pm_mng_profile;
2550 struct hwmon_chip_info *hl_chip_info;
2551
2552 struct hl_dbg_device_entry hl_debugfs;
2553
2554 struct list_head cb_pool;
2555 spinlock_t cb_pool_lock;
2556
2557 void *internal_cb_pool_virt_addr;
2558 dma_addr_t internal_cb_pool_dma_addr;
2559 struct gen_pool *internal_cb_pool;
2560 u64 internal_cb_va_base;
2561
2562 struct list_head fpriv_list;
2563 struct mutex fpriv_list_lock;
2564
2565 struct hl_ctx *compute_ctx;
2566
2567 struct hl_cs_counters_atomic aggregated_cs_counters;
2568
2569 struct hl_mmu_priv mmu_priv;
2570 struct hl_mmu_funcs mmu_func[MMU_NUM_PGT_LOCATIONS];
2571
2572 struct fw_load_mgr fw_loader;
2573
2574 struct pci_mem_region pci_mem_region[PCI_REGION_NUMBER];
2575
2576 struct hl_state_dump_specs state_dump_specs;
2577
2578 struct multi_cs_completion multi_cs_completion[
2579 MULTI_CS_MAX_USER_CTX];
2580 u32 *stream_master_qid_arr;
2581 atomic64_t dram_used_mem;
2582 u64 timeout_jiffies;
2583 u64 max_power;
2584 u64 clock_gating_mask;
2585 u64 boot_error_status_mask;
2586 u64 dram_pci_bar_start;
2587 u64 last_successful_open_jif;
2588 u64 last_open_session_duration_jif;
2589 u64 open_counter;
2590 atomic_t in_reset;
2591 enum hl_pll_frequency curr_pll_profile;
2592 enum cpucp_card_types card_type;
2593 u32 major;
2594 u32 high_pll;
2595 u32 soft_reset_cnt;
2596 u32 hard_reset_cnt;
2597 u32 clk_throttling_reason;
2598 u16 id;
2599 u16 id_control;
2600 u16 cpu_pci_msb_addr;
2601 u8 disabled;
2602 u8 late_init_done;
2603 u8 hwmon_initialized;
2604 u8 hard_reset_pending;
2605 u8 heartbeat;
2606 u8 reset_on_lockup;
2607 u8 dram_default_page_mapping;
2608 u8 memory_scrub;
2609 u8 pmmu_huge_range;
2610 u8 init_done;
2611 u8 device_cpu_disabled;
2612 u8 dma_mask;
2613 u8 in_debug;
2614 u8 power9_64bit_dma_enable;
2615 u8 cdev_sysfs_created;
2616 u8 stop_on_err;
2617 u8 supports_sync_stream;
2618 u8 sync_stream_queue_idx;
2619 u8 collective_mon_idx;
2620 u8 supports_coresight;
2621 u8 supports_soft_reset;
2622 u8 allow_inference_soft_reset;
2623 u8 supports_cb_mapping;
2624 u8 needs_reset;
2625 u8 process_kill_trial_cnt;
2626 u8 device_fini_pending;
2627 u8 supports_staged_submission;
2628 u8 curr_reset_cause;
2629 u8 prev_reset_trigger;
2630 u8 reset_trigger_repeated;
2631 u8 skip_reset_on_timeout;
2632 u8 device_cpu_is_halted;
2633 u8 supports_wait_for_multi_cs;
2634 u8 stream_master_qid_arr_size;
2635
2636 /* Parameters for bring-up */
2637 u64 nic_ports_mask;
2638 u64 fw_components;
2639 u8 mmu_enable;
2640 u8 mmu_huge_page_opt;
2641 u8 reset_pcilink;
2642 u8 cpu_queues_enable;
2643 u8 pldm;
2644 u8 axi_drain;
2645 u8 sram_scrambler_enable;
2646 u8 dram_scrambler_enable;
2647 u8 hard_reset_on_fw_events;
2648 u8 bmc_enable;
2649 u8 rl_enable;
2650 u8 reset_on_preboot_fail;
2651 u8 reset_upon_device_release;
2652 u8 reset_if_device_not_idle;
2653 };
2654
2655
2656 /**
2657 * struct hl_cs_encaps_sig_handle - encapsulated signals handle structure
2658 * @refcount: refcount used to protect removing this id when several
2659 * wait cs are used to wait of the reserved encaps signals.
2660 * @hdev: pointer to habanalabs device structure.
2661 * @hw_sob: pointer to H/W SOB used in the reservation.
2662 * @cs_seq: staged cs sequence which contains encapsulated signals
2663 * @id: idr handler id to be used to fetch the handler info
2664 * @q_idx: stream queue index
2665 * @pre_sob_val: current SOB value before reservation
2666 * @count: signals number
2667 */
2668 struct hl_cs_encaps_sig_handle {
2669 struct kref refcount;
2670 struct hl_device *hdev;
2671 struct hl_hw_sob *hw_sob;
2672 u64 cs_seq;
2673 u32 id;
2674 u32 q_idx;
2675 u32 pre_sob_val;
2676 u32 count;
2677 };
2678
2679 /*
2680 * IOCTLs
2681 */
2682
2683 /**
2684 * typedef hl_ioctl_t - typedef for ioctl function in the driver
2685 * @hpriv: pointer to the FD's private data, which contains state of
2686 * user process
2687 * @data: pointer to the input/output arguments structure of the IOCTL
2688 *
2689 * Return: 0 for success, negative value for error
2690 */
2691 typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
2692
2693 /**
2694 * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
2695 * @cmd: the IOCTL code as created by the kernel macros.
2696 * @func: pointer to the driver's function that should be called for this IOCTL.
2697 */
2698 struct hl_ioctl_desc {
2699 unsigned int cmd;
2700 hl_ioctl_t *func;
2701 };
2702
2703
2704 /*
2705 * Kernel module functions that can be accessed by entire module
2706 */
2707
2708 /**
2709 * hl_get_sg_info() - get number of pages and the DMA address from SG list.
2710 * @sg: the SG list.
2711 * @dma_addr: pointer to DMA address to return.
2712 *
2713 * Calculate the number of consecutive pages described by the SG list. Take the
2714 * offset of the address in the first page, add to it the length and round it up
2715 * to the number of needed pages.
2716 */
hl_get_sg_info(struct scatterlist * sg,dma_addr_t * dma_addr)2717 static inline u32 hl_get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr)
2718 {
2719 *dma_addr = sg_dma_address(sg);
2720
2721 return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) +
2722 (PAGE_SIZE - 1)) >> PAGE_SHIFT;
2723 }
2724
2725 /**
2726 * hl_mem_area_inside_range() - Checks whether address+size are inside a range.
2727 * @address: The start address of the area we want to validate.
2728 * @size: The size in bytes of the area we want to validate.
2729 * @range_start_address: The start address of the valid range.
2730 * @range_end_address: The end address of the valid range.
2731 *
2732 * Return: true if the area is inside the valid range, false otherwise.
2733 */
hl_mem_area_inside_range(u64 address,u64 size,u64 range_start_address,u64 range_end_address)2734 static inline bool hl_mem_area_inside_range(u64 address, u64 size,
2735 u64 range_start_address, u64 range_end_address)
2736 {
2737 u64 end_address = address + size;
2738
2739 if ((address >= range_start_address) &&
2740 (end_address <= range_end_address) &&
2741 (end_address > address))
2742 return true;
2743
2744 return false;
2745 }
2746
2747 /**
2748 * hl_mem_area_crosses_range() - Checks whether address+size crossing a range.
2749 * @address: The start address of the area we want to validate.
2750 * @size: The size in bytes of the area we want to validate.
2751 * @range_start_address: The start address of the valid range.
2752 * @range_end_address: The end address of the valid range.
2753 *
2754 * Return: true if the area overlaps part or all of the valid range,
2755 * false otherwise.
2756 */
hl_mem_area_crosses_range(u64 address,u32 size,u64 range_start_address,u64 range_end_address)2757 static inline bool hl_mem_area_crosses_range(u64 address, u32 size,
2758 u64 range_start_address, u64 range_end_address)
2759 {
2760 u64 end_address = address + size;
2761
2762 if ((address >= range_start_address) &&
2763 (address < range_end_address))
2764 return true;
2765
2766 if ((end_address >= range_start_address) &&
2767 (end_address < range_end_address))
2768 return true;
2769
2770 if ((address < range_start_address) &&
2771 (end_address >= range_end_address))
2772 return true;
2773
2774 return false;
2775 }
2776
2777 int hl_device_open(struct inode *inode, struct file *filp);
2778 int hl_device_open_ctrl(struct inode *inode, struct file *filp);
2779 bool hl_device_operational(struct hl_device *hdev,
2780 enum hl_device_status *status);
2781 enum hl_device_status hl_device_status(struct hl_device *hdev);
2782 int hl_device_set_debug_mode(struct hl_device *hdev, bool enable);
2783 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
2784 enum hl_asic_type asic_type, int minor);
2785 void destroy_hdev(struct hl_device *hdev);
2786 int hl_hw_queues_create(struct hl_device *hdev);
2787 void hl_hw_queues_destroy(struct hl_device *hdev);
2788 int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
2789 u32 cb_size, u64 cb_ptr);
2790 void hl_hw_queue_submit_bd(struct hl_device *hdev, struct hl_hw_queue *q,
2791 u32 ctl, u32 len, u64 ptr);
2792 int hl_hw_queue_schedule_cs(struct hl_cs *cs);
2793 u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
2794 void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
2795 void hl_hw_queue_update_ci(struct hl_cs *cs);
2796 void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
2797
2798 #define hl_queue_inc_ptr(p) hl_hw_queue_add_ptr(p, 1)
2799 #define hl_pi_2_offset(pi) ((pi) & (HL_QUEUE_LENGTH - 1))
2800
2801 int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
2802 void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
2803 int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
2804 void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
2805 void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
2806 void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
2807 irqreturn_t hl_irq_handler_cq(int irq, void *arg);
2808 irqreturn_t hl_irq_handler_eq(int irq, void *arg);
2809 irqreturn_t hl_irq_handler_user_cq(int irq, void *arg);
2810 irqreturn_t hl_irq_handler_default(int irq, void *arg);
2811 u32 hl_cq_inc_ptr(u32 ptr);
2812
2813 int hl_asid_init(struct hl_device *hdev);
2814 void hl_asid_fini(struct hl_device *hdev);
2815 unsigned long hl_asid_alloc(struct hl_device *hdev);
2816 void hl_asid_free(struct hl_device *hdev, unsigned long asid);
2817
2818 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
2819 void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
2820 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
2821 void hl_ctx_do_release(struct kref *ref);
2822 void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx);
2823 int hl_ctx_put(struct hl_ctx *ctx);
2824 struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
2825 int hl_ctx_get_fences(struct hl_ctx *ctx, u64 *seq_arr,
2826 struct hl_fence **fence, u32 arr_len);
2827 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
2828 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
2829
2830 int hl_device_init(struct hl_device *hdev, struct class *hclass);
2831 void hl_device_fini(struct hl_device *hdev);
2832 int hl_device_suspend(struct hl_device *hdev);
2833 int hl_device_resume(struct hl_device *hdev);
2834 int hl_device_reset(struct hl_device *hdev, u32 flags);
2835 void hl_hpriv_get(struct hl_fpriv *hpriv);
2836 int hl_hpriv_put(struct hl_fpriv *hpriv);
2837 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq);
2838 int hl_device_utilization(struct hl_device *hdev, u32 *utilization);
2839
2840 int hl_build_hwmon_channel_info(struct hl_device *hdev,
2841 struct cpucp_sensor *sensors_arr);
2842
2843 int hl_sysfs_init(struct hl_device *hdev);
2844 void hl_sysfs_fini(struct hl_device *hdev);
2845
2846 int hl_hwmon_init(struct hl_device *hdev);
2847 void hl_hwmon_fini(struct hl_device *hdev);
2848
2849 int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
2850 struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
2851 bool map_cb, u64 *handle);
2852 int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
2853 int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
2854 int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
2855 struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
2856 u32 handle);
2857 void hl_cb_put(struct hl_cb *cb);
2858 void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
2859 void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
2860 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
2861 bool internal_cb);
2862 int hl_cb_pool_init(struct hl_device *hdev);
2863 int hl_cb_pool_fini(struct hl_device *hdev);
2864 int hl_cb_va_pool_init(struct hl_ctx *ctx);
2865 void hl_cb_va_pool_fini(struct hl_ctx *ctx);
2866
2867 void hl_cs_rollback_all(struct hl_device *hdev);
2868 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
2869 enum hl_queue_type queue_type, bool is_kernel_allocated_cb);
2870 void hl_sob_reset_error(struct kref *ref);
2871 int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask);
2872 void hl_fence_put(struct hl_fence *fence);
2873 void hl_fences_put(struct hl_fence **fence, int len);
2874 void hl_fence_get(struct hl_fence *fence);
2875 void cs_get(struct hl_cs *cs);
2876 bool cs_needs_completion(struct hl_cs *cs);
2877 bool cs_needs_timeout(struct hl_cs *cs);
2878 bool is_staged_cs_last_exists(struct hl_device *hdev, struct hl_cs *cs);
2879 struct hl_cs *hl_staged_cs_find_first(struct hl_device *hdev, u64 cs_seq);
2880 void hl_multi_cs_completion_init(struct hl_device *hdev);
2881
2882 void goya_set_asic_funcs(struct hl_device *hdev);
2883 void gaudi_set_asic_funcs(struct hl_device *hdev);
2884
2885 int hl_vm_ctx_init(struct hl_ctx *ctx);
2886 void hl_vm_ctx_fini(struct hl_ctx *ctx);
2887
2888 int hl_vm_init(struct hl_device *hdev);
2889 void hl_vm_fini(struct hl_device *hdev);
2890
2891 void hl_hw_block_mem_init(struct hl_ctx *ctx);
2892 void hl_hw_block_mem_fini(struct hl_ctx *ctx);
2893
2894 u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
2895 enum hl_va_range_type type, u32 size, u32 alignment);
2896 int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
2897 u64 start_addr, u64 size);
2898 int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
2899 struct hl_userptr *userptr);
2900 void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
2901 void hl_userptr_delete_list(struct hl_device *hdev,
2902 struct list_head *userptr_list);
2903 bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, u32 size,
2904 struct list_head *userptr_list,
2905 struct hl_userptr **userptr);
2906
2907 int hl_mmu_init(struct hl_device *hdev);
2908 void hl_mmu_fini(struct hl_device *hdev);
2909 int hl_mmu_ctx_init(struct hl_ctx *ctx);
2910 void hl_mmu_ctx_fini(struct hl_ctx *ctx);
2911 int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
2912 u32 page_size, bool flush_pte);
2913 int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
2914 bool flush_pte);
2915 int hl_mmu_map_contiguous(struct hl_ctx *ctx, u64 virt_addr,
2916 u64 phys_addr, u32 size);
2917 int hl_mmu_unmap_contiguous(struct hl_ctx *ctx, u64 virt_addr, u32 size);
2918 void hl_mmu_swap_out(struct hl_ctx *ctx);
2919 void hl_mmu_swap_in(struct hl_ctx *ctx);
2920 int hl_mmu_if_set_funcs(struct hl_device *hdev);
2921 void hl_mmu_v1_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
2922 int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr);
2923 int hl_mmu_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
2924 struct hl_mmu_hop_info *hops);
2925 u64 hl_mmu_scramble_addr(struct hl_device *hdev, u64 addr);
2926 u64 hl_mmu_descramble_addr(struct hl_device *hdev, u64 addr);
2927 bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr);
2928
2929 int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
2930 void __iomem *dst, u32 src_offset, u32 size);
2931 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode);
2932 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
2933 u16 len, u32 timeout, u64 *result);
2934 int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type);
2935 int hl_fw_unmask_irq_arr(struct hl_device *hdev, const u32 *irq_arr,
2936 size_t irq_arr_size);
2937 int hl_fw_test_cpu_queue(struct hl_device *hdev);
2938 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
2939 dma_addr_t *dma_handle);
2940 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
2941 void *vaddr);
2942 int hl_fw_send_heartbeat(struct hl_device *hdev);
2943 int hl_fw_cpucp_info_get(struct hl_device *hdev,
2944 u32 sts_boot_dev_sts0_reg,
2945 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
2946 u32 boot_err1_reg);
2947 int hl_fw_cpucp_handshake(struct hl_device *hdev,
2948 u32 sts_boot_dev_sts0_reg,
2949 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
2950 u32 boot_err1_reg);
2951 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
2952 int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
2953 struct hl_info_pci_counters *counters);
2954 int hl_fw_cpucp_total_energy_get(struct hl_device *hdev,
2955 u64 *total_energy);
2956 int get_used_pll_index(struct hl_device *hdev, u32 input_pll_index,
2957 enum pll_index *pll_index);
2958 int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
2959 u16 *pll_freq_arr);
2960 int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power);
2961 void hl_fw_ask_hard_reset_without_linux(struct hl_device *hdev);
2962 void hl_fw_ask_halt_machine_without_linux(struct hl_device *hdev);
2963 int hl_fw_init_cpu(struct hl_device *hdev);
2964 int hl_fw_read_preboot_status(struct hl_device *hdev, u32 cpu_boot_status_reg,
2965 u32 sts_boot_dev_sts0_reg,
2966 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
2967 u32 boot_err1_reg, u32 timeout);
2968 int hl_fw_dynamic_send_protocol_cmd(struct hl_device *hdev,
2969 struct fw_load_mgr *fw_loader,
2970 enum comms_cmd cmd, unsigned int size,
2971 bool wait_ok, u32 timeout);
2972 int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
2973 bool is_wc[3]);
2974 int hl_pci_elbi_read(struct hl_device *hdev, u64 addr, u32 *data);
2975 int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
2976 int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
2977 struct hl_inbound_pci_region *pci_region);
2978 int hl_pci_set_outbound_region(struct hl_device *hdev,
2979 struct hl_outbound_pci_region *pci_region);
2980 enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr);
2981 int hl_pci_init(struct hl_device *hdev);
2982 void hl_pci_fini(struct hl_device *hdev);
2983
2984 long hl_get_frequency(struct hl_device *hdev, u32 pll_index,
2985 bool curr);
2986 void hl_set_frequency(struct hl_device *hdev, u32 pll_index,
2987 u64 freq);
2988 int hl_get_temperature(struct hl_device *hdev,
2989 int sensor_index, u32 attr, long *value);
2990 int hl_set_temperature(struct hl_device *hdev,
2991 int sensor_index, u32 attr, long value);
2992 int hl_get_voltage(struct hl_device *hdev,
2993 int sensor_index, u32 attr, long *value);
2994 int hl_get_current(struct hl_device *hdev,
2995 int sensor_index, u32 attr, long *value);
2996 int hl_get_fan_speed(struct hl_device *hdev,
2997 int sensor_index, u32 attr, long *value);
2998 int hl_get_pwm_info(struct hl_device *hdev,
2999 int sensor_index, u32 attr, long *value);
3000 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
3001 long value);
3002 u64 hl_get_max_power(struct hl_device *hdev);
3003 void hl_set_max_power(struct hl_device *hdev);
3004 int hl_set_voltage(struct hl_device *hdev,
3005 int sensor_index, u32 attr, long value);
3006 int hl_set_current(struct hl_device *hdev,
3007 int sensor_index, u32 attr, long value);
3008 int hl_set_power(struct hl_device *hdev,
3009 int sensor_index, u32 attr, long value);
3010 int hl_get_power(struct hl_device *hdev,
3011 int sensor_index, u32 attr, long *value);
3012 int hl_get_clk_rate(struct hl_device *hdev,
3013 u32 *cur_clk, u32 *max_clk);
3014 void hl_set_pll_profile(struct hl_device *hdev, enum hl_pll_frequency freq);
3015 void hl_add_device_attr(struct hl_device *hdev,
3016 struct attribute_group *dev_attr_grp);
3017 void hw_sob_get(struct hl_hw_sob *hw_sob);
3018 void hw_sob_put(struct hl_hw_sob *hw_sob);
3019 void hl_encaps_handle_do_release(struct kref *ref);
3020 void hl_hw_queue_encaps_sig_set_sob_info(struct hl_device *hdev,
3021 struct hl_cs *cs, struct hl_cs_job *job,
3022 struct hl_cs_compl *cs_cmpl);
3023 void hl_release_pending_user_interrupts(struct hl_device *hdev);
3024 int hl_cs_signal_sob_wraparound_handler(struct hl_device *hdev, u32 q_idx,
3025 struct hl_hw_sob **hw_sob, u32 count, bool encaps_sig);
3026
3027 int hl_state_dump(struct hl_device *hdev);
3028 const char *hl_state_dump_get_sync_name(struct hl_device *hdev, u32 sync_id);
3029 const char *hl_state_dump_get_monitor_name(struct hl_device *hdev,
3030 struct hl_mon_state_dump *mon);
3031 void hl_state_dump_free_sync_to_engine_map(struct hl_sync_to_engine_map *map);
3032 __printf(4, 5) int hl_snprintf_resize(char **buf, size_t *size, size_t *offset,
3033 const char *format, ...);
3034 char *hl_format_as_binary(char *buf, size_t buf_len, u32 n);
3035 const char *hl_sync_engine_to_string(enum hl_sync_engine_type engine_type);
3036
3037 #ifdef CONFIG_DEBUG_FS
3038
3039 void hl_debugfs_init(void);
3040 void hl_debugfs_fini(void);
3041 void hl_debugfs_add_device(struct hl_device *hdev);
3042 void hl_debugfs_remove_device(struct hl_device *hdev);
3043 void hl_debugfs_add_file(struct hl_fpriv *hpriv);
3044 void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
3045 void hl_debugfs_add_cb(struct hl_cb *cb);
3046 void hl_debugfs_remove_cb(struct hl_cb *cb);
3047 void hl_debugfs_add_cs(struct hl_cs *cs);
3048 void hl_debugfs_remove_cs(struct hl_cs *cs);
3049 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
3050 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
3051 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
3052 void hl_debugfs_remove_userptr(struct hl_device *hdev,
3053 struct hl_userptr *userptr);
3054 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
3055 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
3056 void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
3057 unsigned long length);
3058
3059 #else
3060
hl_debugfs_init(void)3061 static inline void __init hl_debugfs_init(void)
3062 {
3063 }
3064
hl_debugfs_fini(void)3065 static inline void hl_debugfs_fini(void)
3066 {
3067 }
3068
hl_debugfs_add_device(struct hl_device * hdev)3069 static inline void hl_debugfs_add_device(struct hl_device *hdev)
3070 {
3071 }
3072
hl_debugfs_remove_device(struct hl_device * hdev)3073 static inline void hl_debugfs_remove_device(struct hl_device *hdev)
3074 {
3075 }
3076
hl_debugfs_add_file(struct hl_fpriv * hpriv)3077 static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
3078 {
3079 }
3080
hl_debugfs_remove_file(struct hl_fpriv * hpriv)3081 static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
3082 {
3083 }
3084
hl_debugfs_add_cb(struct hl_cb * cb)3085 static inline void hl_debugfs_add_cb(struct hl_cb *cb)
3086 {
3087 }
3088
hl_debugfs_remove_cb(struct hl_cb * cb)3089 static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
3090 {
3091 }
3092
hl_debugfs_add_cs(struct hl_cs * cs)3093 static inline void hl_debugfs_add_cs(struct hl_cs *cs)
3094 {
3095 }
3096
hl_debugfs_remove_cs(struct hl_cs * cs)3097 static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
3098 {
3099 }
3100
hl_debugfs_add_job(struct hl_device * hdev,struct hl_cs_job * job)3101 static inline void hl_debugfs_add_job(struct hl_device *hdev,
3102 struct hl_cs_job *job)
3103 {
3104 }
3105
hl_debugfs_remove_job(struct hl_device * hdev,struct hl_cs_job * job)3106 static inline void hl_debugfs_remove_job(struct hl_device *hdev,
3107 struct hl_cs_job *job)
3108 {
3109 }
3110
hl_debugfs_add_userptr(struct hl_device * hdev,struct hl_userptr * userptr)3111 static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
3112 struct hl_userptr *userptr)
3113 {
3114 }
3115
hl_debugfs_remove_userptr(struct hl_device * hdev,struct hl_userptr * userptr)3116 static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
3117 struct hl_userptr *userptr)
3118 {
3119 }
3120
hl_debugfs_add_ctx_mem_hash(struct hl_device * hdev,struct hl_ctx * ctx)3121 static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
3122 struct hl_ctx *ctx)
3123 {
3124 }
3125
hl_debugfs_remove_ctx_mem_hash(struct hl_device * hdev,struct hl_ctx * ctx)3126 static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
3127 struct hl_ctx *ctx)
3128 {
3129 }
3130
hl_debugfs_set_state_dump(struct hl_device * hdev,char * data,unsigned long length)3131 static inline void hl_debugfs_set_state_dump(struct hl_device *hdev,
3132 char *data, unsigned long length)
3133 {
3134 }
3135
3136 #endif
3137
3138 /* IOCTLs */
3139 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
3140 long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
3141 int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
3142 int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
3143 int hl_wait_ioctl(struct hl_fpriv *hpriv, void *data);
3144 int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
3145
3146 #endif /* HABANALABSP_H_ */
3147