treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / habanalabs / habanalabs.h
blob00c949f4ccd1c56438722b119e70e56beeac6035
1 /* SPDX-License-Identifier: GPL-2.0
3 * Copyright 2016-2019 HabanaLabs, Ltd.
4 * All Rights Reserved.
6 */
8 #ifndef HABANALABSP_H_
9 #define HABANALABSP_H_
11 #include "include/armcp_if.h"
12 #include "include/qman_if.h"
14 #include <linux/cdev.h>
15 #include <linux/iopoll.h>
16 #include <linux/irqreturn.h>
17 #include <linux/dma-fence.h>
18 #include <linux/dma-direction.h>
19 #include <linux/scatterlist.h>
20 #include <linux/hashtable.h>
22 #define HL_NAME "habanalabs"
24 #define HL_MMAP_CB_MASK (0x8000000000000000ull >> PAGE_SHIFT)
26 #define HL_PENDING_RESET_PER_SEC 5
28 #define HL_DEVICE_TIMEOUT_USEC 1000000 /* 1 s */
30 #define HL_HEARTBEAT_PER_USEC 5000000 /* 5 s */
32 #define HL_PLL_LOW_JOB_FREQ_USEC 5000000 /* 5 s */
34 #define HL_ARMCP_INFO_TIMEOUT_USEC 10000000 /* 10s */
35 #define HL_ARMCP_EEPROM_TIMEOUT_USEC 10000000 /* 10s */
37 #define HL_PCI_ELBI_TIMEOUT_MSEC 10 /* 10ms */
39 #define HL_SIM_MAX_TIMEOUT_US 10000000 /* 10s */
41 #define HL_MAX_QUEUES 128
43 /* MUST BE POWER OF 2 and larger than 1 */
44 #define HL_MAX_PENDING_CS 64
46 #define HL_IDLE_BUSY_TS_ARR_SIZE 4096
48 /* Memory */
49 #define MEM_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
51 /* MMU */
52 #define MMU_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
54 /**
55 * struct pgt_info - MMU hop page info.
56 * @node: hash linked-list node for the pgts shadow hash of pgts.
57 * @phys_addr: physical address of the pgt.
58 * @shadow_addr: shadow hop in the host.
59 * @ctx: pointer to the owner ctx.
60 * @num_of_ptes: indicates how many ptes are used in the pgt.
62 * The MMU page tables hierarchy is placed on the DRAM. When a new level (hop)
63 * is needed during mapping, a new page is allocated and this structure holds
64 * its essential information. During unmapping, if no valid PTEs remained in the
65 * page, it is freed with its pgt_info structure.
67 struct pgt_info {
68 struct hlist_node node;
69 u64 phys_addr;
70 u64 shadow_addr;
71 struct hl_ctx *ctx;
72 int num_of_ptes;
75 struct hl_device;
76 struct hl_fpriv;
78 /**
79 * enum hl_queue_type - Supported QUEUE types.
80 * @QUEUE_TYPE_NA: queue is not available.
81 * @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the
82 * host.
83 * @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's
84 * memories and/or operates the compute engines.
85 * @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU.
86 * @QUEUE_TYPE_HW: queue of DMA and compute engines jobs, for which completion
87 * notifications are sent by H/W.
89 enum hl_queue_type {
90 QUEUE_TYPE_NA,
91 QUEUE_TYPE_EXT,
92 QUEUE_TYPE_INT,
93 QUEUE_TYPE_CPU,
94 QUEUE_TYPE_HW
97 /**
98 * struct hw_queue_properties - queue information.
99 * @type: queue type.
100 * @driver_only: true if only the driver is allowed to send a job to this queue,
101 * false otherwise.
102 * @requires_kernel_cb: true if a CB handle must be provided for jobs on this
103 * queue, false otherwise (a CB address must be provided).
105 struct hw_queue_properties {
106 enum hl_queue_type type;
107 u8 driver_only;
108 u8 requires_kernel_cb;
112 * enum vm_type_t - virtual memory mapping request information.
113 * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
114 * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
116 enum vm_type_t {
117 VM_TYPE_USERPTR = 0x1,
118 VM_TYPE_PHYS_PACK = 0x2
122 * enum hl_device_hw_state - H/W device state. use this to understand whether
123 * to do reset before hw_init or not
124 * @HL_DEVICE_HW_STATE_CLEAN: H/W state is clean. i.e. after hard reset
125 * @HL_DEVICE_HW_STATE_DIRTY: H/W state is dirty. i.e. we started to execute
126 * hw_init
128 enum hl_device_hw_state {
129 HL_DEVICE_HW_STATE_CLEAN = 0,
130 HL_DEVICE_HW_STATE_DIRTY
134 * struct hl_mmu_properties - ASIC specific MMU address translation properties.
135 * @hop0_shift: shift of hop 0 mask.
136 * @hop1_shift: shift of hop 1 mask.
137 * @hop2_shift: shift of hop 2 mask.
138 * @hop3_shift: shift of hop 3 mask.
139 * @hop4_shift: shift of hop 4 mask.
140 * @hop0_mask: mask to get the PTE address in hop 0.
141 * @hop1_mask: mask to get the PTE address in hop 1.
142 * @hop2_mask: mask to get the PTE address in hop 2.
143 * @hop3_mask: mask to get the PTE address in hop 3.
144 * @hop4_mask: mask to get the PTE address in hop 4.
145 * @page_size: default page size used to allocate memory.
146 * @huge_page_size: page size used to allocate memory with huge pages.
148 struct hl_mmu_properties {
149 u64 hop0_shift;
150 u64 hop1_shift;
151 u64 hop2_shift;
152 u64 hop3_shift;
153 u64 hop4_shift;
154 u64 hop0_mask;
155 u64 hop1_mask;
156 u64 hop2_mask;
157 u64 hop3_mask;
158 u64 hop4_mask;
159 u32 page_size;
160 u32 huge_page_size;
164 * struct asic_fixed_properties - ASIC specific immutable properties.
165 * @hw_queues_props: H/W queues properties.
166 * @armcp_info: received various information from ArmCP regarding the H/W, e.g.
167 * available sensors.
168 * @uboot_ver: F/W U-boot version.
169 * @preboot_ver: F/W Preboot version.
170 * @dmmu: DRAM MMU address translation properties.
171 * @pmmu: PCI (host) MMU address translation properties.
172 * @sram_base_address: SRAM physical start address.
173 * @sram_end_address: SRAM physical end address.
174 * @sram_user_base_address - SRAM physical start address for user access.
175 * @dram_base_address: DRAM physical start address.
176 * @dram_end_address: DRAM physical end address.
177 * @dram_user_base_address: DRAM physical start address for user access.
178 * @dram_size: DRAM total size.
179 * @dram_pci_bar_size: size of PCI bar towards DRAM.
180 * @max_power_default: max power of the device after reset
181 * @va_space_host_start_address: base address of virtual memory range for
182 * mapping host memory.
183 * @va_space_host_end_address: end address of virtual memory range for
184 * mapping host memory.
185 * @va_space_dram_start_address: base address of virtual memory range for
186 * mapping DRAM memory.
187 * @va_space_dram_end_address: end address of virtual memory range for
188 * mapping DRAM memory.
189 * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
190 * fault.
191 * @pcie_dbi_base_address: Base address of the PCIE_DBI block.
192 * @pcie_aux_dbi_reg_addr: Address of the PCIE_AUX DBI register.
193 * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
194 * @mmu_dram_default_page_addr: DRAM default page physical address.
195 * @mmu_pgt_size: MMU page tables total size.
196 * @mmu_pte_size: PTE size in MMU page tables.
197 * @mmu_hop_table_size: MMU hop table size.
198 * @mmu_hop0_tables_total_size: total size of MMU hop0 tables.
199 * @dram_page_size: page size for MMU DRAM allocation.
200 * @cfg_size: configuration space size on SRAM.
201 * @sram_size: total size of SRAM.
202 * @max_asid: maximum number of open contexts (ASIDs).
203 * @num_of_events: number of possible internal H/W IRQs.
204 * @psoc_pci_pll_nr: PCI PLL NR value.
205 * @psoc_pci_pll_nf: PCI PLL NF value.
206 * @psoc_pci_pll_od: PCI PLL OD value.
207 * @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value.
208 * @high_pll: high PLL frequency used by the device.
209 * @cb_pool_cb_cnt: number of CBs in the CB pool.
210 * @cb_pool_cb_size: size of each CB in the CB pool.
211 * @tpc_enabled_mask: which TPCs are enabled.
212 * @completion_queues_count: number of completion queues.
214 struct asic_fixed_properties {
215 struct hw_queue_properties hw_queues_props[HL_MAX_QUEUES];
216 struct armcp_info armcp_info;
217 char uboot_ver[VERSION_MAX_LEN];
218 char preboot_ver[VERSION_MAX_LEN];
219 struct hl_mmu_properties dmmu;
220 struct hl_mmu_properties pmmu;
221 u64 sram_base_address;
222 u64 sram_end_address;
223 u64 sram_user_base_address;
224 u64 dram_base_address;
225 u64 dram_end_address;
226 u64 dram_user_base_address;
227 u64 dram_size;
228 u64 dram_pci_bar_size;
229 u64 max_power_default;
230 u64 va_space_host_start_address;
231 u64 va_space_host_end_address;
232 u64 va_space_dram_start_address;
233 u64 va_space_dram_end_address;
234 u64 dram_size_for_default_page_mapping;
235 u64 pcie_dbi_base_address;
236 u64 pcie_aux_dbi_reg_addr;
237 u64 mmu_pgt_addr;
238 u64 mmu_dram_default_page_addr;
239 u32 mmu_pgt_size;
240 u32 mmu_pte_size;
241 u32 mmu_hop_table_size;
242 u32 mmu_hop0_tables_total_size;
243 u32 dram_page_size;
244 u32 cfg_size;
245 u32 sram_size;
246 u32 max_asid;
247 u32 num_of_events;
248 u32 psoc_pci_pll_nr;
249 u32 psoc_pci_pll_nf;
250 u32 psoc_pci_pll_od;
251 u32 psoc_pci_pll_div_factor;
252 u32 high_pll;
253 u32 cb_pool_cb_cnt;
254 u32 cb_pool_cb_size;
255 u8 tpc_enabled_mask;
256 u8 completion_queues_count;
260 * struct hl_dma_fence - wrapper for fence object used by command submissions.
261 * @base_fence: kernel fence object.
262 * @lock: spinlock to protect fence.
263 * @hdev: habanalabs device structure.
264 * @cs_seq: command submission sequence number.
266 struct hl_dma_fence {
267 struct dma_fence base_fence;
268 spinlock_t lock;
269 struct hl_device *hdev;
270 u64 cs_seq;
274 * Command Buffers
278 * struct hl_cb_mgr - describes a Command Buffer Manager.
279 * @cb_lock: protects cb_handles.
280 * @cb_handles: an idr to hold all command buffer handles.
282 struct hl_cb_mgr {
283 spinlock_t cb_lock;
284 struct idr cb_handles; /* protected by cb_lock */
288 * struct hl_cb - describes a Command Buffer.
289 * @refcount: reference counter for usage of the CB.
290 * @hdev: pointer to device this CB belongs to.
291 * @lock: spinlock to protect mmap/cs flows.
292 * @debugfs_list: node in debugfs list of command buffers.
293 * @pool_list: node in pool list of command buffers.
294 * @kernel_address: Holds the CB's kernel virtual address.
295 * @bus_address: Holds the CB's DMA address.
296 * @mmap_size: Holds the CB's size that was mmaped.
297 * @size: holds the CB's size.
298 * @id: the CB's ID.
299 * @cs_cnt: holds number of CS that this CB participates in.
300 * @ctx_id: holds the ID of the owner's context.
301 * @mmap: true if the CB is currently mmaped to user.
302 * @is_pool: true if CB was acquired from the pool, false otherwise.
304 struct hl_cb {
305 struct kref refcount;
306 struct hl_device *hdev;
307 spinlock_t lock;
308 struct list_head debugfs_list;
309 struct list_head pool_list;
310 u64 kernel_address;
311 dma_addr_t bus_address;
312 u32 mmap_size;
313 u32 size;
314 u32 id;
315 u32 cs_cnt;
316 u32 ctx_id;
317 u8 mmap;
318 u8 is_pool;
323 * QUEUES
326 struct hl_cs_job;
329 * Currently, there are two limitations on the maximum length of a queue:
331 * 1. The memory footprint of the queue. The current allocated space for the
332 * queue is PAGE_SIZE. Because each entry in the queue is HL_BD_SIZE,
333 * the maximum length of the queue can be PAGE_SIZE / HL_BD_SIZE,
334 * which currently is 4096/16 = 256 entries.
336 * To increase that, we need either to decrease the size of the
337 * BD (difficult), or allocate more than a single page (easier).
339 * 2. Because the size of the JOB handle field in the BD CTL / completion queue
340 * is 10-bit, we can have up to 1024 open jobs per hardware queue.
341 * Therefore, each queue can hold up to 1024 entries.
343 * HL_QUEUE_LENGTH is in units of struct hl_bd.
344 * HL_QUEUE_LENGTH * sizeof(struct hl_bd) should be <= HL_PAGE_SIZE
347 #define HL_PAGE_SIZE 4096 /* minimum page size */
348 /* Must be power of 2 (HL_PAGE_SIZE / HL_BD_SIZE) */
349 #define HL_QUEUE_LENGTH 256
350 #define HL_QUEUE_SIZE_IN_BYTES (HL_QUEUE_LENGTH * HL_BD_SIZE)
353 * HL_CQ_LENGTH is in units of struct hl_cq_entry.
354 * HL_CQ_LENGTH should be <= HL_PAGE_SIZE
356 #define HL_CQ_LENGTH HL_QUEUE_LENGTH
357 #define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)
359 /* Must be power of 2 (HL_PAGE_SIZE / HL_EQ_ENTRY_SIZE) */
360 #define HL_EQ_LENGTH 64
361 #define HL_EQ_SIZE_IN_BYTES (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
363 /* Host <-> ArmCP shared memory size */
364 #define HL_CPU_ACCESSIBLE_MEM_SIZE SZ_2M
367 * struct hl_hw_queue - describes a H/W transport queue.
368 * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
369 * @queue_type: type of queue.
370 * @kernel_address: holds the queue's kernel virtual address.
371 * @bus_address: holds the queue's DMA address.
372 * @pi: holds the queue's pi value.
373 * @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci).
374 * @hw_queue_id: the id of the H/W queue.
375 * @int_queue_len: length of internal queue (number of entries).
376 * @valid: is the queue valid (we have array of 32 queues, not all of them
377 * exists).
379 struct hl_hw_queue {
380 struct hl_cs_job **shadow_queue;
381 enum hl_queue_type queue_type;
382 u64 kernel_address;
383 dma_addr_t bus_address;
384 u32 pi;
385 u32 ci;
386 u32 hw_queue_id;
387 u16 int_queue_len;
388 u8 valid;
392 * struct hl_cq - describes a completion queue
393 * @hdev: pointer to the device structure
394 * @kernel_address: holds the queue's kernel virtual address
395 * @bus_address: holds the queue's DMA address
396 * @hw_queue_id: the id of the matching H/W queue
397 * @ci: ci inside the queue
398 * @pi: pi inside the queue
399 * @free_slots_cnt: counter of free slots in queue
401 struct hl_cq {
402 struct hl_device *hdev;
403 u64 kernel_address;
404 dma_addr_t bus_address;
405 u32 hw_queue_id;
406 u32 ci;
407 u32 pi;
408 atomic_t free_slots_cnt;
412 * struct hl_eq - describes the event queue (single one per device)
413 * @hdev: pointer to the device structure
414 * @kernel_address: holds the queue's kernel virtual address
415 * @bus_address: holds the queue's DMA address
416 * @ci: ci inside the queue
418 struct hl_eq {
419 struct hl_device *hdev;
420 u64 kernel_address;
421 dma_addr_t bus_address;
422 u32 ci;
427 * ASICs
431 * enum hl_asic_type - supported ASIC types.
432 * @ASIC_INVALID: Invalid ASIC type.
433 * @ASIC_GOYA: Goya device.
435 enum hl_asic_type {
436 ASIC_INVALID,
437 ASIC_GOYA
440 struct hl_cs_parser;
443 * enum hl_pm_mng_profile - power management profile.
444 * @PM_AUTO: internal clock is set by the Linux driver.
445 * @PM_MANUAL: internal clock is set by the user.
446 * @PM_LAST: last power management type.
448 enum hl_pm_mng_profile {
449 PM_AUTO = 1,
450 PM_MANUAL,
451 PM_LAST
455 * enum hl_pll_frequency - PLL frequency.
456 * @PLL_HIGH: high frequency.
457 * @PLL_LOW: low frequency.
458 * @PLL_LAST: last frequency values that were configured by the user.
460 enum hl_pll_frequency {
461 PLL_HIGH = 1,
462 PLL_LOW,
463 PLL_LAST
467 * struct hl_asic_funcs - ASIC specific functions that are can be called from
468 * common code.
469 * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
470 * @early_fini: tears down what was done in early_init.
471 * @late_init: sets up late driver/hw state (post hw_init) - Optional.
472 * @late_fini: tears down what was done in late_init (pre hw_fini) - Optional.
473 * @sw_init: sets up driver state, does not configure H/W.
474 * @sw_fini: tears down driver state, does not configure H/W.
475 * @hw_init: sets up the H/W state.
476 * @hw_fini: tears down the H/W state.
477 * @halt_engines: halt engines, needed for reset sequence. This also disables
478 * interrupts from the device. Should be called before
479 * hw_fini and before CS rollback.
480 * @suspend: handles IP specific H/W or SW changes for suspend.
481 * @resume: handles IP specific H/W or SW changes for resume.
482 * @cb_mmap: maps a CB.
483 * @ring_doorbell: increment PI on a given QMAN.
484 * @pqe_write: Write the PQ entry to the PQ. This is ASIC-specific
485 * function because the PQs are located in different memory areas
486 * per ASIC (SRAM, DRAM, Host memory) and therefore, the method of
487 * writing the PQE must match the destination memory area
488 * properties.
489 * @asic_dma_alloc_coherent: Allocate coherent DMA memory by calling
490 * dma_alloc_coherent(). This is ASIC function because
491 * its implementation is not trivial when the driver
492 * is loaded in simulation mode (not upstreamed).
493 * @asic_dma_free_coherent: Free coherent DMA memory by calling
494 * dma_free_coherent(). This is ASIC function because
495 * its implementation is not trivial when the driver
496 * is loaded in simulation mode (not upstreamed).
497 * @get_int_queue_base: get the internal queue base address.
498 * @test_queues: run simple test on all queues for sanity check.
499 * @asic_dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool.
500 * size of allocation is HL_DMA_POOL_BLK_SIZE.
501 * @asic_dma_pool_free: free small DMA allocation from pool.
502 * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
503 * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
504 * @hl_dma_unmap_sg: DMA unmap scatter-gather list.
505 * @cs_parser: parse Command Submission.
506 * @asic_dma_map_sg: DMA map scatter-gather list.
507 * @get_dma_desc_list_size: get number of LIN_DMA packets required for CB.
508 * @add_end_of_cb_packets: Add packets to the end of CB, if device requires it.
509 * @update_eq_ci: update event queue CI.
510 * @context_switch: called upon ASID context switch.
511 * @restore_phase_topology: clear all SOBs amd MONs.
512 * @debugfs_read32: debug interface for reading u32 from DRAM/SRAM.
513 * @debugfs_write32: debug interface for writing u32 to DRAM/SRAM.
514 * @add_device_attr: add ASIC specific device attributes.
515 * @handle_eqe: handle event queue entry (IRQ) from ArmCP.
516 * @set_pll_profile: change PLL profile (manual/automatic).
517 * @get_events_stat: retrieve event queue entries histogram.
518 * @read_pte: read MMU page table entry from DRAM.
519 * @write_pte: write MMU page table entry to DRAM.
520 * @mmu_invalidate_cache: flush MMU STLB host/DRAM cache, either with soft
521 * (L1 only) or hard (L0 & L1) flush.
522 * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with
523 * ASID-VA-size mask.
524 * @send_heartbeat: send is-alive packet to ArmCP and verify response.
525 * @debug_coresight: perform certain actions on Coresight for debugging.
526 * @is_device_idle: return true if device is idle, false otherwise.
527 * @soft_reset_late_init: perform certain actions needed after soft reset.
528 * @hw_queues_lock: acquire H/W queues lock.
529 * @hw_queues_unlock: release H/W queues lock.
530 * @get_pci_id: retrieve PCI ID.
531 * @get_eeprom_data: retrieve EEPROM data from F/W.
532 * @send_cpu_message: send buffer to ArmCP.
533 * @get_hw_state: retrieve the H/W state
534 * @pci_bars_map: Map PCI BARs.
535 * @set_dram_bar_base: Set DRAM BAR to map specific device address. Returns
536 * old address the bar pointed to or U64_MAX for failure
537 * @init_iatu: Initialize the iATU unit inside the PCI controller.
538 * @rreg: Read a register. Needed for simulator support.
539 * @wreg: Write a register. Needed for simulator support.
540 * @halt_coresight: stop the ETF and ETR traces.
541 * @get_clk_rate: Retrieve the ASIC current and maximum clock rate in MHz
543 struct hl_asic_funcs {
544 int (*early_init)(struct hl_device *hdev);
545 int (*early_fini)(struct hl_device *hdev);
546 int (*late_init)(struct hl_device *hdev);
547 void (*late_fini)(struct hl_device *hdev);
548 int (*sw_init)(struct hl_device *hdev);
549 int (*sw_fini)(struct hl_device *hdev);
550 int (*hw_init)(struct hl_device *hdev);
551 void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
552 void (*halt_engines)(struct hl_device *hdev, bool hard_reset);
553 int (*suspend)(struct hl_device *hdev);
554 int (*resume)(struct hl_device *hdev);
555 int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
556 u64 kaddress, phys_addr_t paddress, u32 size);
557 void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
558 void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
559 struct hl_bd *bd);
560 void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
561 dma_addr_t *dma_handle, gfp_t flag);
562 void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
563 void *cpu_addr, dma_addr_t dma_handle);
564 void* (*get_int_queue_base)(struct hl_device *hdev, u32 queue_id,
565 dma_addr_t *dma_handle, u16 *queue_len);
566 int (*test_queues)(struct hl_device *hdev);
567 void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
568 gfp_t mem_flags, dma_addr_t *dma_handle);
569 void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
570 dma_addr_t dma_addr);
571 void* (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev,
572 size_t size, dma_addr_t *dma_handle);
573 void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,
574 size_t size, void *vaddr);
575 void (*hl_dma_unmap_sg)(struct hl_device *hdev,
576 struct scatterlist *sgl, int nents,
577 enum dma_data_direction dir);
578 int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
579 int (*asic_dma_map_sg)(struct hl_device *hdev,
580 struct scatterlist *sgl, int nents,
581 enum dma_data_direction dir);
582 u32 (*get_dma_desc_list_size)(struct hl_device *hdev,
583 struct sg_table *sgt);
584 void (*add_end_of_cb_packets)(struct hl_device *hdev,
585 u64 kernel_address, u32 len,
586 u64 cq_addr, u32 cq_val, u32 msix_num);
587 void (*update_eq_ci)(struct hl_device *hdev, u32 val);
588 int (*context_switch)(struct hl_device *hdev, u32 asid);
589 void (*restore_phase_topology)(struct hl_device *hdev);
590 int (*debugfs_read32)(struct hl_device *hdev, u64 addr, u32 *val);
591 int (*debugfs_write32)(struct hl_device *hdev, u64 addr, u32 val);
592 void (*add_device_attr)(struct hl_device *hdev,
593 struct attribute_group *dev_attr_grp);
594 void (*handle_eqe)(struct hl_device *hdev,
595 struct hl_eq_entry *eq_entry);
596 void (*set_pll_profile)(struct hl_device *hdev,
597 enum hl_pll_frequency freq);
598 void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
599 u32 *size);
600 u64 (*read_pte)(struct hl_device *hdev, u64 addr);
601 void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
602 void (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard,
603 u32 flags);
604 void (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
605 u32 asid, u64 va, u64 size);
606 int (*send_heartbeat)(struct hl_device *hdev);
607 int (*debug_coresight)(struct hl_device *hdev, void *data);
608 bool (*is_device_idle)(struct hl_device *hdev, u32 *mask,
609 struct seq_file *s);
610 int (*soft_reset_late_init)(struct hl_device *hdev);
611 void (*hw_queues_lock)(struct hl_device *hdev);
612 void (*hw_queues_unlock)(struct hl_device *hdev);
613 u32 (*get_pci_id)(struct hl_device *hdev);
614 int (*get_eeprom_data)(struct hl_device *hdev, void *data,
615 size_t max_size);
616 int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
617 u16 len, u32 timeout, long *result);
618 enum hl_device_hw_state (*get_hw_state)(struct hl_device *hdev);
619 int (*pci_bars_map)(struct hl_device *hdev);
620 u64 (*set_dram_bar_base)(struct hl_device *hdev, u64 addr);
621 int (*init_iatu)(struct hl_device *hdev);
622 u32 (*rreg)(struct hl_device *hdev, u32 reg);
623 void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
624 void (*halt_coresight)(struct hl_device *hdev);
625 int (*get_clk_rate)(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk);
630 * CONTEXTS
633 #define HL_KERNEL_ASID_ID 0
636 * struct hl_va_range - virtual addresses range.
637 * @lock: protects the virtual addresses list.
638 * @list: list of virtual addresses blocks available for mappings.
639 * @start_addr: range start address.
640 * @end_addr: range end address.
642 struct hl_va_range {
643 struct mutex lock;
644 struct list_head list;
645 u64 start_addr;
646 u64 end_addr;
650 * struct hl_ctx - user/kernel context.
651 * @mem_hash: holds mapping from virtual address to virtual memory area
652 * descriptor (hl_vm_phys_pg_list or hl_userptr).
653 * @mmu_phys_hash: holds a mapping from physical address to pgt_info structure.
654 * @mmu_shadow_hash: holds a mapping from shadow address to pgt_info structure.
655 * @hpriv: pointer to the private (Kernel Driver) data of the process (fd).
656 * @hdev: pointer to the device structure.
657 * @refcount: reference counter for the context. Context is released only when
658 * this hits 0l. It is incremented on CS and CS_WAIT.
659 * @cs_pending: array of DMA fence objects representing pending CS.
660 * @host_va_range: holds available virtual addresses for host mappings.
661 * @dram_va_range: holds available virtual addresses for DRAM mappings.
662 * @mem_hash_lock: protects the mem_hash.
663 * @mmu_lock: protects the MMU page tables. Any change to the PGT, modifing the
664 * MMU hash or walking the PGT requires talking this lock
665 * @debugfs_list: node in debugfs list of contexts.
666 * @cs_sequence: sequence number for CS. Value is assigned to a CS and passed
667 * to user so user could inquire about CS. It is used as
668 * index to cs_pending array.
669 * @dram_default_hops: array that holds all hops addresses needed for default
670 * DRAM mapping.
671 * @cs_lock: spinlock to protect cs_sequence.
672 * @dram_phys_mem: amount of used physical DRAM memory by this context.
673 * @thread_ctx_switch_token: token to prevent multiple threads of the same
674 * context from running the context switch phase.
675 * Only a single thread should run it.
676 * @thread_ctx_switch_wait_token: token to prevent the threads that didn't run
677 * the context switch phase from moving to their
678 * execution phase before the context switch phase
679 * has finished.
680 * @asid: context's unique address space ID in the device's MMU.
681 * @handle: context's opaque handle for user
683 struct hl_ctx {
684 DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
685 DECLARE_HASHTABLE(mmu_phys_hash, MMU_HASH_TABLE_BITS);
686 DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
687 struct hl_fpriv *hpriv;
688 struct hl_device *hdev;
689 struct kref refcount;
690 struct dma_fence *cs_pending[HL_MAX_PENDING_CS];
691 struct hl_va_range host_va_range;
692 struct hl_va_range dram_va_range;
693 struct mutex mem_hash_lock;
694 struct mutex mmu_lock;
695 struct list_head debugfs_list;
696 u64 cs_sequence;
697 u64 *dram_default_hops;
698 spinlock_t cs_lock;
699 atomic64_t dram_phys_mem;
700 atomic_t thread_ctx_switch_token;
701 u32 thread_ctx_switch_wait_token;
702 u32 asid;
703 u32 handle;
707 * struct hl_ctx_mgr - for handling multiple contexts.
708 * @ctx_lock: protects ctx_handles.
709 * @ctx_handles: idr to hold all ctx handles.
711 struct hl_ctx_mgr {
712 struct mutex ctx_lock;
713 struct idr ctx_handles;
719 * COMMAND SUBMISSIONS
723 * struct hl_userptr - memory mapping chunk information
724 * @vm_type: type of the VM.
725 * @job_node: linked-list node for hanging the object on the Job's list.
726 * @vec: pointer to the frame vector.
727 * @sgt: pointer to the scatter-gather table that holds the pages.
728 * @dir: for DMA unmapping, the direction must be supplied, so save it.
729 * @debugfs_list: node in debugfs list of command submissions.
730 * @addr: user-space virtual address of the start of the memory area.
731 * @size: size of the memory area to pin & map.
732 * @dma_mapped: true if the SG was mapped to DMA addresses, false otherwise.
734 struct hl_userptr {
735 enum vm_type_t vm_type; /* must be first */
736 struct list_head job_node;
737 struct frame_vector *vec;
738 struct sg_table *sgt;
739 enum dma_data_direction dir;
740 struct list_head debugfs_list;
741 u64 addr;
742 u32 size;
743 u8 dma_mapped;
747 * struct hl_cs - command submission.
748 * @jobs_in_queue_cnt: per each queue, maintain counter of submitted jobs.
749 * @ctx: the context this CS belongs to.
750 * @job_list: list of the CS's jobs in the various queues.
751 * @job_lock: spinlock for the CS's jobs list. Needed for free_job.
752 * @refcount: reference counter for usage of the CS.
753 * @fence: pointer to the fence object of this CS.
754 * @work_tdr: delayed work node for TDR.
755 * @mirror_node : node in device mirror list of command submissions.
756 * @debugfs_list: node in debugfs list of command submissions.
757 * @sequence: the sequence number of this CS.
758 * @submitted: true if CS was submitted to H/W.
759 * @completed: true if CS was completed by device.
760 * @timedout : true if CS was timedout.
761 * @tdr_active: true if TDR was activated for this CS (to prevent
762 * double TDR activation).
763 * @aborted: true if CS was aborted due to some device error.
765 struct hl_cs {
766 u8 jobs_in_queue_cnt[HL_MAX_QUEUES];
767 struct hl_ctx *ctx;
768 struct list_head job_list;
769 spinlock_t job_lock;
770 struct kref refcount;
771 struct dma_fence *fence;
772 struct delayed_work work_tdr;
773 struct list_head mirror_node;
774 struct list_head debugfs_list;
775 u64 sequence;
776 u8 submitted;
777 u8 completed;
778 u8 timedout;
779 u8 tdr_active;
780 u8 aborted;
784 * struct hl_cs_job - command submission job.
785 * @cs_node: the node to hang on the CS jobs list.
786 * @cs: the CS this job belongs to.
787 * @user_cb: the CB we got from the user.
788 * @patched_cb: in case of patching, this is internal CB which is submitted on
789 * the queue instead of the CB we got from the IOCTL.
790 * @finish_work: workqueue object to run when job is completed.
791 * @userptr_list: linked-list of userptr mappings that belong to this job and
792 * wait for completion.
793 * @debugfs_list: node in debugfs list of command submission jobs.
794 * @queue_type: the type of the H/W queue this job is submitted to.
795 * @id: the id of this job inside a CS.
796 * @hw_queue_id: the id of the H/W queue this job is submitted to.
797 * @user_cb_size: the actual size of the CB we got from the user.
798 * @job_cb_size: the actual size of the CB that we put on the queue.
799 * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
800 * handle to a kernel-allocated CB object, false
801 * otherwise (SRAM/DRAM/host address).
803 struct hl_cs_job {
804 struct list_head cs_node;
805 struct hl_cs *cs;
806 struct hl_cb *user_cb;
807 struct hl_cb *patched_cb;
808 struct work_struct finish_work;
809 struct list_head userptr_list;
810 struct list_head debugfs_list;
811 enum hl_queue_type queue_type;
812 u32 id;
813 u32 hw_queue_id;
814 u32 user_cb_size;
815 u32 job_cb_size;
816 u8 is_kernel_allocated_cb;
820 * struct hl_cs_parser - command submission parser properties.
821 * @user_cb: the CB we got from the user.
822 * @patched_cb: in case of patching, this is internal CB which is submitted on
823 * the queue instead of the CB we got from the IOCTL.
824 * @job_userptr_list: linked-list of userptr mappings that belong to the related
825 * job and wait for completion.
826 * @cs_sequence: the sequence number of the related CS.
827 * @queue_type: the type of the H/W queue this job is submitted to.
828 * @ctx_id: the ID of the context the related CS belongs to.
829 * @hw_queue_id: the id of the H/W queue this job is submitted to.
830 * @user_cb_size: the actual size of the CB we got from the user.
831 * @patched_cb_size: the size of the CB after parsing.
832 * @job_id: the id of the related job inside the related CS.
833 * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
834 * handle to a kernel-allocated CB object, false
835 * otherwise (SRAM/DRAM/host address).
837 struct hl_cs_parser {
838 struct hl_cb *user_cb;
839 struct hl_cb *patched_cb;
840 struct list_head *job_userptr_list;
841 u64 cs_sequence;
842 enum hl_queue_type queue_type;
843 u32 ctx_id;
844 u32 hw_queue_id;
845 u32 user_cb_size;
846 u32 patched_cb_size;
847 u8 job_id;
848 u8 is_kernel_allocated_cb;
853 * MEMORY STRUCTURE
857 * struct hl_vm_hash_node - hash element from virtual address to virtual
858 * memory area descriptor (hl_vm_phys_pg_list or
859 * hl_userptr).
860 * @node: node to hang on the hash table in context object.
861 * @vaddr: key virtual address.
862 * @ptr: value pointer (hl_vm_phys_pg_list or hl_userptr).
864 struct hl_vm_hash_node {
865 struct hlist_node node;
866 u64 vaddr;
867 void *ptr;
871 * struct hl_vm_phys_pg_pack - physical page pack.
872 * @vm_type: describes the type of the virtual area descriptor.
873 * @pages: the physical page array.
874 * @npages: num physical pages in the pack.
875 * @total_size: total size of all the pages in this list.
876 * @mapping_cnt: number of shared mappings.
877 * @asid: the context related to this list.
878 * @page_size: size of each page in the pack.
879 * @flags: HL_MEM_* flags related to this list.
880 * @handle: the provided handle related to this list.
881 * @offset: offset from the first page.
882 * @contiguous: is contiguous physical memory.
883 * @created_from_userptr: is product of host virtual address.
885 struct hl_vm_phys_pg_pack {
886 enum vm_type_t vm_type; /* must be first */
887 u64 *pages;
888 u64 npages;
889 u64 total_size;
890 atomic_t mapping_cnt;
891 u32 asid;
892 u32 page_size;
893 u32 flags;
894 u32 handle;
895 u32 offset;
896 u8 contiguous;
897 u8 created_from_userptr;
901 * struct hl_vm_va_block - virtual range block information.
902 * @node: node to hang on the virtual range list in context object.
903 * @start: virtual range start address.
904 * @end: virtual range end address.
905 * @size: virtual range size.
907 struct hl_vm_va_block {
908 struct list_head node;
909 u64 start;
910 u64 end;
911 u64 size;
915 * struct hl_vm - virtual memory manager for MMU.
916 * @dram_pg_pool: pool for DRAM physical pages of 2MB.
917 * @dram_pg_pool_refcount: reference counter for the pool usage.
918 * @idr_lock: protects the phys_pg_list_handles.
919 * @phys_pg_pack_handles: idr to hold all device allocations handles.
920 * @init_done: whether initialization was done. We need this because VM
921 * initialization might be skipped during device initialization.
923 struct hl_vm {
924 struct gen_pool *dram_pg_pool;
925 struct kref dram_pg_pool_refcount;
926 spinlock_t idr_lock;
927 struct idr phys_pg_pack_handles;
928 u8 init_done;
933 * DEBUG, PROFILING STRUCTURE
937 * struct hl_debug_params - Coresight debug parameters.
938 * @input: pointer to component specific input parameters.
939 * @output: pointer to component specific output parameters.
940 * @output_size: size of output buffer.
941 * @reg_idx: relevant register ID.
942 * @op: component operation to execute.
943 * @enable: true if to enable component debugging, false otherwise.
945 struct hl_debug_params {
946 void *input;
947 void *output;
948 u32 output_size;
949 u32 reg_idx;
950 u32 op;
951 bool enable;
955 * FILE PRIVATE STRUCTURE
959 * struct hl_fpriv - process information stored in FD private data.
960 * @hdev: habanalabs device structure.
961 * @filp: pointer to the given file structure.
962 * @taskpid: current process ID.
963 * @ctx: current executing context. TODO: remove for multiple ctx per process
964 * @ctx_mgr: context manager to handle multiple context for this FD.
965 * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
966 * @debugfs_list: list of relevant ASIC debugfs.
967 * @dev_node: node in the device list of file private data
968 * @refcount: number of related contexts.
969 * @restore_phase_mutex: lock for context switch and restore phase.
970 * @is_control: true for control device, false otherwise
972 struct hl_fpriv {
973 struct hl_device *hdev;
974 struct file *filp;
975 struct pid *taskpid;
976 struct hl_ctx *ctx;
977 struct hl_ctx_mgr ctx_mgr;
978 struct hl_cb_mgr cb_mgr;
979 struct list_head debugfs_list;
980 struct list_head dev_node;
981 struct kref refcount;
982 struct mutex restore_phase_mutex;
983 u8 is_control;
988 * DebugFS
992 * struct hl_info_list - debugfs file ops.
993 * @name: file name.
994 * @show: function to output information.
995 * @write: function to write to the file.
997 struct hl_info_list {
998 const char *name;
999 int (*show)(struct seq_file *s, void *data);
1000 ssize_t (*write)(struct file *file, const char __user *buf,
1001 size_t count, loff_t *f_pos);
1005 * struct hl_debugfs_entry - debugfs dentry wrapper.
1006 * @dent: base debugfs entry structure.
1007 * @info_ent: dentry realted ops.
1008 * @dev_entry: ASIC specific debugfs manager.
1010 struct hl_debugfs_entry {
1011 struct dentry *dent;
1012 const struct hl_info_list *info_ent;
1013 struct hl_dbg_device_entry *dev_entry;
1017 * struct hl_dbg_device_entry - ASIC specific debugfs manager.
1018 * @root: root dentry.
1019 * @hdev: habanalabs device structure.
1020 * @entry_arr: array of available hl_debugfs_entry.
1021 * @file_list: list of available debugfs files.
1022 * @file_mutex: protects file_list.
1023 * @cb_list: list of available CBs.
1024 * @cb_spinlock: protects cb_list.
1025 * @cs_list: list of available CSs.
1026 * @cs_spinlock: protects cs_list.
1027 * @cs_job_list: list of available CB jobs.
1028 * @cs_job_spinlock: protects cs_job_list.
1029 * @userptr_list: list of available userptrs (virtual memory chunk descriptor).
1030 * @userptr_spinlock: protects userptr_list.
1031 * @ctx_mem_hash_list: list of available contexts with MMU mappings.
1032 * @ctx_mem_hash_spinlock: protects cb_list.
1033 * @addr: next address to read/write from/to in read/write32.
1034 * @mmu_addr: next virtual address to translate to physical address in mmu_show.
1035 * @mmu_asid: ASID to use while translating in mmu_show.
1036 * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
1037 * @i2c_bus: generic u8 debugfs file for address value to use in i2c_data_read.
1038 * @i2c_bus: generic u8 debugfs file for register value to use in i2c_data_read.
1040 struct hl_dbg_device_entry {
1041 struct dentry *root;
1042 struct hl_device *hdev;
1043 struct hl_debugfs_entry *entry_arr;
1044 struct list_head file_list;
1045 struct mutex file_mutex;
1046 struct list_head cb_list;
1047 spinlock_t cb_spinlock;
1048 struct list_head cs_list;
1049 spinlock_t cs_spinlock;
1050 struct list_head cs_job_list;
1051 spinlock_t cs_job_spinlock;
1052 struct list_head userptr_list;
1053 spinlock_t userptr_spinlock;
1054 struct list_head ctx_mem_hash_list;
1055 spinlock_t ctx_mem_hash_spinlock;
1056 u64 addr;
1057 u64 mmu_addr;
1058 u32 mmu_asid;
1059 u8 i2c_bus;
1060 u8 i2c_addr;
1061 u8 i2c_reg;
1066 * DEVICES
1069 /* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
1070 * x16 cards. In extreme cases, there are hosts that can accommodate 16 cards.
1072 #define HL_MAX_MINORS 256
1075 * Registers read & write functions.
1078 u32 hl_rreg(struct hl_device *hdev, u32 reg);
1079 void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
1081 #define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
1082 #define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
1083 #define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n", \
1084 hdev->asic_funcs->rreg(hdev, (reg)))
1086 #define WREG32_P(reg, val, mask) \
1087 do { \
1088 u32 tmp_ = RREG32(reg); \
1089 tmp_ &= (mask); \
1090 tmp_ |= ((val) & ~(mask)); \
1091 WREG32(reg, tmp_); \
1092 } while (0)
1093 #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
1094 #define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
1096 #define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
1097 #define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
1098 #define WREG32_FIELD(reg, offset, field, val) \
1099 WREG32(mm##reg + offset, (RREG32(mm##reg + offset) & \
1100 ~REG_FIELD_MASK(reg, field)) | \
1101 (val) << REG_FIELD_SHIFT(reg, field))
1103 /* Timeout should be longer when working with simulator but cap the
1104 * increased timeout to some maximum
1106 #define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
1107 ({ \
1108 ktime_t __timeout; \
1109 if (hdev->pdev) \
1110 __timeout = ktime_add_us(ktime_get(), timeout_us); \
1111 else \
1112 __timeout = ktime_add_us(ktime_get(),\
1113 min((u64)(timeout_us * 10), \
1114 (u64) HL_SIM_MAX_TIMEOUT_US)); \
1115 might_sleep_if(sleep_us); \
1116 for (;;) { \
1117 (val) = RREG32(addr); \
1118 if (cond) \
1119 break; \
1120 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
1121 (val) = RREG32(addr); \
1122 break; \
1124 if (sleep_us) \
1125 usleep_range((sleep_us >> 2) + 1, sleep_us); \
1127 (cond) ? 0 : -ETIMEDOUT; \
1131 * address in this macro points always to a memory location in the
1132 * host's (server's) memory. That location is updated asynchronously
1133 * either by the direct access of the device or by another core.
1135 * To work both in LE and BE architectures, we need to distinguish between the
1136 * two states (device or another core updates the memory location). Therefore,
1137 * if mem_written_by_device is true, the host memory being polled will be
1138 * updated directly by the device. If false, the host memory being polled will
1139 * be updated by host CPU. Required so host knows whether or not the memory
1140 * might need to be byte-swapped before returning value to caller.
1142 #define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
1143 mem_written_by_device) \
1144 ({ \
1145 ktime_t __timeout; \
1146 if (hdev->pdev) \
1147 __timeout = ktime_add_us(ktime_get(), timeout_us); \
1148 else \
1149 __timeout = ktime_add_us(ktime_get(),\
1150 min((u64)(timeout_us * 10), \
1151 (u64) HL_SIM_MAX_TIMEOUT_US)); \
1152 might_sleep_if(sleep_us); \
1153 for (;;) { \
1154 /* Verify we read updates done by other cores or by device */ \
1155 mb(); \
1156 (val) = *((u32 *) (uintptr_t) (addr)); \
1157 if (mem_written_by_device) \
1158 (val) = le32_to_cpu(*(__le32 *) &(val)); \
1159 if (cond) \
1160 break; \
1161 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
1162 (val) = *((u32 *) (uintptr_t) (addr)); \
1163 if (mem_written_by_device) \
1164 (val) = le32_to_cpu(*(__le32 *) &(val)); \
1165 break; \
1167 if (sleep_us) \
1168 usleep_range((sleep_us >> 2) + 1, sleep_us); \
1170 (cond) ? 0 : -ETIMEDOUT; \
1173 #define hl_poll_timeout_device_memory(hdev, addr, val, cond, sleep_us, \
1174 timeout_us) \
1175 ({ \
1176 ktime_t __timeout; \
1177 if (hdev->pdev) \
1178 __timeout = ktime_add_us(ktime_get(), timeout_us); \
1179 else \
1180 __timeout = ktime_add_us(ktime_get(),\
1181 min((u64)(timeout_us * 10), \
1182 (u64) HL_SIM_MAX_TIMEOUT_US)); \
1183 might_sleep_if(sleep_us); \
1184 for (;;) { \
1185 (val) = readl(addr); \
1186 if (cond) \
1187 break; \
1188 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
1189 (val) = readl(addr); \
1190 break; \
1192 if (sleep_us) \
1193 usleep_range((sleep_us >> 2) + 1, sleep_us); \
1195 (cond) ? 0 : -ETIMEDOUT; \
1198 struct hwmon_chip_info;
1201 * struct hl_device_reset_work - reset workqueue task wrapper.
1202 * @reset_work: reset work to be done.
1203 * @hdev: habanalabs device structure.
1205 struct hl_device_reset_work {
1206 struct work_struct reset_work;
1207 struct hl_device *hdev;
1211 * struct hl_device_idle_busy_ts - used for calculating device utilization rate.
1212 * @idle_to_busy_ts: timestamp where device changed from idle to busy.
1213 * @busy_to_idle_ts: timestamp where device changed from busy to idle.
1215 struct hl_device_idle_busy_ts {
1216 ktime_t idle_to_busy_ts;
1217 ktime_t busy_to_idle_ts;
1221 * struct hl_device - habanalabs device structure.
1222 * @pdev: pointer to PCI device, can be NULL in case of simulator device.
1223 * @pcie_bar: array of available PCIe bars.
1224 * @rmmio: configuration area address on SRAM.
1225 * @cdev: related char device.
1226 * @cdev_ctrl: char device for control operations only (INFO IOCTL)
1227 * @dev: related kernel basic device structure.
1228 * @dev_ctrl: related kernel device structure for the control device
1229 * @work_freq: delayed work to lower device frequency if possible.
1230 * @work_heartbeat: delayed work for ArmCP is-alive check.
1231 * @asic_name: ASIC specific nmae.
1232 * @asic_type: ASIC specific type.
1233 * @completion_queue: array of hl_cq.
1234 * @cq_wq: work queue of completion queues for executing work in process context
1235 * @eq_wq: work queue of event queue for executing work in process context.
1236 * @kernel_ctx: Kernel driver context structure.
1237 * @kernel_queues: array of hl_hw_queue.
1238 * @hw_queues_mirror_list: CS mirror list for TDR.
1239 * @hw_queues_mirror_lock: protects hw_queues_mirror_list.
1240 * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs.
1241 * @event_queue: event queue for IRQ from ArmCP.
1242 * @dma_pool: DMA pool for small allocations.
1243 * @cpu_accessible_dma_mem: Host <-> ArmCP shared memory CPU address.
1244 * @cpu_accessible_dma_address: Host <-> ArmCP shared memory DMA address.
1245 * @cpu_accessible_dma_pool: Host <-> ArmCP shared memory pool.
1246 * @asid_bitmap: holds used/available ASIDs.
1247 * @asid_mutex: protects asid_bitmap.
1248 * @send_cpu_message_lock: enforces only one message in Host <-> ArmCP queue.
1249 * @debug_lock: protects critical section of setting debug mode for device
1250 * @asic_prop: ASIC specific immutable properties.
1251 * @asic_funcs: ASIC specific functions.
1252 * @asic_specific: ASIC specific information to use only from ASIC files.
1253 * @mmu_pgt_pool: pool of available MMU hops.
1254 * @vm: virtual memory manager for MMU.
1255 * @mmu_cache_lock: protects MMU cache invalidation as it can serve one context.
1256 * @mmu_shadow_hop0: shadow mapping of the MMU hop 0 zone.
1257 * @hwmon_dev: H/W monitor device.
1258 * @pm_mng_profile: current power management profile.
1259 * @hl_chip_info: ASIC's sensors information.
1260 * @hl_debugfs: device's debugfs manager.
1261 * @cb_pool: list of preallocated CBs.
1262 * @cb_pool_lock: protects the CB pool.
1263 * @fpriv_list: list of file private data structures. Each structure is created
1264 * when a user opens the device
1265 * @fpriv_list_lock: protects the fpriv_list
1266 * @compute_ctx: current compute context executing.
1267 * @idle_busy_ts_arr: array to hold time stamps of transitions from idle to busy
1268 * and vice-versa
1269 * @dram_used_mem: current DRAM memory consumption.
1270 * @timeout_jiffies: device CS timeout value.
1271 * @max_power: the max power of the device, as configured by the sysadmin. This
1272 * value is saved so in case of hard-reset, the driver will restore
1273 * this value and update the F/W after the re-initialization
1274 * @in_reset: is device in reset flow.
1275 * @curr_pll_profile: current PLL profile.
1276 * @cs_active_cnt: number of active command submissions on this device (active
1277 * means already in H/W queues)
1278 * @major: habanalabs kernel driver major.
1279 * @high_pll: high PLL profile frequency.
1280 * @soft_reset_cnt: number of soft reset since the driver was loaded.
1281 * @hard_reset_cnt: number of hard reset since the driver was loaded.
1282 * @idle_busy_ts_idx: index of current entry in idle_busy_ts_arr
1283 * @id: device minor.
1284 * @id_control: minor of the control device
1285 * @disabled: is device disabled.
1286 * @late_init_done: is late init stage was done during initialization.
1287 * @hwmon_initialized: is H/W monitor sensors was initialized.
1288 * @hard_reset_pending: is there a hard reset work pending.
1289 * @heartbeat: is heartbeat sanity check towards ArmCP enabled.
1290 * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
1291 * otherwise.
1292 * @dram_supports_virtual_memory: is MMU enabled towards DRAM.
1293 * @dram_default_page_mapping: is DRAM default page mapping enabled.
1294 * @init_done: is the initialization of the device done.
1295 * @mmu_enable: is MMU enabled.
1296 * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
1297 * @dma_mask: the dma mask that was set for this device
1298 * @in_debug: is device under debug. This, together with fpriv_list, enforces
1299 * that only a single user is configuring the debug infrastructure.
1300 * @cdev_sysfs_created: were char devices and sysfs nodes created.
1302 struct hl_device {
1303 struct pci_dev *pdev;
1304 void __iomem *pcie_bar[6];
1305 void __iomem *rmmio;
1306 struct cdev cdev;
1307 struct cdev cdev_ctrl;
1308 struct device *dev;
1309 struct device *dev_ctrl;
1310 struct delayed_work work_freq;
1311 struct delayed_work work_heartbeat;
1312 char asic_name[16];
1313 enum hl_asic_type asic_type;
1314 struct hl_cq *completion_queue;
1315 struct workqueue_struct *cq_wq;
1316 struct workqueue_struct *eq_wq;
1317 struct hl_ctx *kernel_ctx;
1318 struct hl_hw_queue *kernel_queues;
1319 struct list_head hw_queues_mirror_list;
1320 spinlock_t hw_queues_mirror_lock;
1321 struct hl_cb_mgr kernel_cb_mgr;
1322 struct hl_eq event_queue;
1323 struct dma_pool *dma_pool;
1324 void *cpu_accessible_dma_mem;
1325 dma_addr_t cpu_accessible_dma_address;
1326 struct gen_pool *cpu_accessible_dma_pool;
1327 unsigned long *asid_bitmap;
1328 struct mutex asid_mutex;
1329 struct mutex send_cpu_message_lock;
1330 struct mutex debug_lock;
1331 struct asic_fixed_properties asic_prop;
1332 const struct hl_asic_funcs *asic_funcs;
1333 void *asic_specific;
1334 struct gen_pool *mmu_pgt_pool;
1335 struct hl_vm vm;
1336 struct mutex mmu_cache_lock;
1337 void *mmu_shadow_hop0;
1338 struct device *hwmon_dev;
1339 enum hl_pm_mng_profile pm_mng_profile;
1340 struct hwmon_chip_info *hl_chip_info;
1342 struct hl_dbg_device_entry hl_debugfs;
1344 struct list_head cb_pool;
1345 spinlock_t cb_pool_lock;
1347 struct list_head fpriv_list;
1348 struct mutex fpriv_list_lock;
1350 struct hl_ctx *compute_ctx;
1352 struct hl_device_idle_busy_ts *idle_busy_ts_arr;
1354 atomic64_t dram_used_mem;
1355 u64 timeout_jiffies;
1356 u64 max_power;
1357 atomic_t in_reset;
1358 enum hl_pll_frequency curr_pll_profile;
1359 int cs_active_cnt;
1360 u32 major;
1361 u32 high_pll;
1362 u32 soft_reset_cnt;
1363 u32 hard_reset_cnt;
1364 u32 idle_busy_ts_idx;
1365 u16 id;
1366 u16 id_control;
1367 u8 disabled;
1368 u8 late_init_done;
1369 u8 hwmon_initialized;
1370 u8 hard_reset_pending;
1371 u8 heartbeat;
1372 u8 reset_on_lockup;
1373 u8 dram_supports_virtual_memory;
1374 u8 dram_default_page_mapping;
1375 u8 init_done;
1376 u8 device_cpu_disabled;
1377 u8 dma_mask;
1378 u8 in_debug;
1379 u8 cdev_sysfs_created;
1381 /* Parameters for bring-up */
1382 u8 mmu_enable;
1383 u8 cpu_enable;
1384 u8 reset_pcilink;
1385 u8 cpu_queues_enable;
1386 u8 fw_loading;
1387 u8 pldm;
1392 * IOCTLs
1396 * typedef hl_ioctl_t - typedef for ioctl function in the driver
1397 * @hpriv: pointer to the FD's private data, which contains state of
1398 * user process
1399 * @data: pointer to the input/output arguments structure of the IOCTL
1401 * Return: 0 for success, negative value for error
1403 typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
1406 * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
1407 * @cmd: the IOCTL code as created by the kernel macros.
1408 * @func: pointer to the driver's function that should be called for this IOCTL.
1410 struct hl_ioctl_desc {
1411 unsigned int cmd;
1412 hl_ioctl_t *func;
1417 * Kernel module functions that can be accessed by entire module
1421 * hl_mem_area_inside_range() - Checks whether address+size are inside a range.
1422 * @address: The start address of the area we want to validate.
1423 * @size: The size in bytes of the area we want to validate.
1424 * @range_start_address: The start address of the valid range.
1425 * @range_end_address: The end address of the valid range.
1427 * Return: true if the area is inside the valid range, false otherwise.
1429 static inline bool hl_mem_area_inside_range(u64 address, u32 size,
1430 u64 range_start_address, u64 range_end_address)
1432 u64 end_address = address + size;
1434 if ((address >= range_start_address) &&
1435 (end_address <= range_end_address) &&
1436 (end_address > address))
1437 return true;
1439 return false;
1443 * hl_mem_area_crosses_range() - Checks whether address+size crossing a range.
1444 * @address: The start address of the area we want to validate.
1445 * @size: The size in bytes of the area we want to validate.
1446 * @range_start_address: The start address of the valid range.
1447 * @range_end_address: The end address of the valid range.
1449 * Return: true if the area overlaps part or all of the valid range,
1450 * false otherwise.
1452 static inline bool hl_mem_area_crosses_range(u64 address, u32 size,
1453 u64 range_start_address, u64 range_end_address)
1455 u64 end_address = address + size;
1457 if ((address >= range_start_address) &&
1458 (address < range_end_address))
1459 return true;
1461 if ((end_address >= range_start_address) &&
1462 (end_address < range_end_address))
1463 return true;
1465 if ((address < range_start_address) &&
1466 (end_address >= range_end_address))
1467 return true;
1469 return false;
1472 int hl_device_open(struct inode *inode, struct file *filp);
1473 int hl_device_open_ctrl(struct inode *inode, struct file *filp);
1474 bool hl_device_disabled_or_in_reset(struct hl_device *hdev);
1475 enum hl_device_status hl_device_status(struct hl_device *hdev);
1476 int hl_device_set_debug_mode(struct hl_device *hdev, bool enable);
1477 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
1478 enum hl_asic_type asic_type, int minor);
1479 void destroy_hdev(struct hl_device *hdev);
1480 int hl_hw_queues_create(struct hl_device *hdev);
1481 void hl_hw_queues_destroy(struct hl_device *hdev);
1482 int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
1483 u32 cb_size, u64 cb_ptr);
1484 int hl_hw_queue_schedule_cs(struct hl_cs *cs);
1485 u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
1486 void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
1487 void hl_int_hw_queue_update_ci(struct hl_cs *cs);
1488 void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
1490 #define hl_queue_inc_ptr(p) hl_hw_queue_add_ptr(p, 1)
1491 #define hl_pi_2_offset(pi) ((pi) & (HL_QUEUE_LENGTH - 1))
1493 int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
1494 void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
1495 int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
1496 void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
1497 void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
1498 void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
1499 irqreturn_t hl_irq_handler_cq(int irq, void *arg);
1500 irqreturn_t hl_irq_handler_eq(int irq, void *arg);
1501 u32 hl_cq_inc_ptr(u32 ptr);
1503 int hl_asid_init(struct hl_device *hdev);
1504 void hl_asid_fini(struct hl_device *hdev);
1505 unsigned long hl_asid_alloc(struct hl_device *hdev);
1506 void hl_asid_free(struct hl_device *hdev, unsigned long asid);
1508 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
1509 void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
1510 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
1511 void hl_ctx_do_release(struct kref *ref);
1512 void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx);
1513 int hl_ctx_put(struct hl_ctx *ctx);
1514 struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
1515 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
1516 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
1518 int hl_device_init(struct hl_device *hdev, struct class *hclass);
1519 void hl_device_fini(struct hl_device *hdev);
1520 int hl_device_suspend(struct hl_device *hdev);
1521 int hl_device_resume(struct hl_device *hdev);
1522 int hl_device_reset(struct hl_device *hdev, bool hard_reset,
1523 bool from_hard_reset_thread);
1524 void hl_hpriv_get(struct hl_fpriv *hpriv);
1525 void hl_hpriv_put(struct hl_fpriv *hpriv);
1526 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq);
1527 uint32_t hl_device_utilization(struct hl_device *hdev, uint32_t period_ms);
1529 int hl_build_hwmon_channel_info(struct hl_device *hdev,
1530 struct armcp_sensor *sensors_arr);
1532 int hl_sysfs_init(struct hl_device *hdev);
1533 void hl_sysfs_fini(struct hl_device *hdev);
1535 int hl_hwmon_init(struct hl_device *hdev);
1536 void hl_hwmon_fini(struct hl_device *hdev);
1538 int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr, u32 cb_size,
1539 u64 *handle, int ctx_id);
1540 int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
1541 int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
1542 struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
1543 u32 handle);
1544 void hl_cb_put(struct hl_cb *cb);
1545 void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
1546 void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
1547 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size);
1548 int hl_cb_pool_init(struct hl_device *hdev);
1549 int hl_cb_pool_fini(struct hl_device *hdev);
1551 void hl_cs_rollback_all(struct hl_device *hdev);
1552 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
1553 enum hl_queue_type queue_type, bool is_kernel_allocated_cb);
1555 void goya_set_asic_funcs(struct hl_device *hdev);
1557 int hl_vm_ctx_init(struct hl_ctx *ctx);
1558 void hl_vm_ctx_fini(struct hl_ctx *ctx);
1560 int hl_vm_init(struct hl_device *hdev);
1561 void hl_vm_fini(struct hl_device *hdev);
1563 int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
1564 struct hl_userptr *userptr);
1565 void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
1566 void hl_userptr_delete_list(struct hl_device *hdev,
1567 struct list_head *userptr_list);
1568 bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, u32 size,
1569 struct list_head *userptr_list,
1570 struct hl_userptr **userptr);
1572 int hl_mmu_init(struct hl_device *hdev);
1573 void hl_mmu_fini(struct hl_device *hdev);
1574 int hl_mmu_ctx_init(struct hl_ctx *ctx);
1575 void hl_mmu_ctx_fini(struct hl_ctx *ctx);
1576 int hl_mmu_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size);
1577 int hl_mmu_unmap(struct hl_ctx *ctx, u64 virt_addr, u32 page_size);
1578 void hl_mmu_swap_out(struct hl_ctx *ctx);
1579 void hl_mmu_swap_in(struct hl_ctx *ctx);
1581 int hl_fw_push_fw_to_device(struct hl_device *hdev, const char *fw_name,
1582 void __iomem *dst);
1583 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode);
1584 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
1585 u16 len, u32 timeout, long *result);
1586 int hl_fw_test_cpu_queue(struct hl_device *hdev);
1587 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
1588 dma_addr_t *dma_handle);
1589 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
1590 void *vaddr);
1591 int hl_fw_send_heartbeat(struct hl_device *hdev);
1592 int hl_fw_armcp_info_get(struct hl_device *hdev);
1593 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
1595 int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
1596 bool is_wc[3]);
1597 int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
1598 int hl_pci_set_dram_bar_base(struct hl_device *hdev, u8 inbound_region, u8 bar,
1599 u64 addr);
1600 int hl_pci_init_iatu(struct hl_device *hdev, u64 sram_base_address,
1601 u64 dram_base_address, u64 host_phys_base_address,
1602 u64 host_phys_size);
1603 int hl_pci_init(struct hl_device *hdev, u8 dma_mask);
1604 void hl_pci_fini(struct hl_device *hdev);
1605 int hl_pci_set_dma_mask(struct hl_device *hdev, u8 dma_mask);
1607 long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr);
1608 void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq);
1609 long hl_get_temperature(struct hl_device *hdev, int sensor_index, u32 attr);
1610 long hl_get_voltage(struct hl_device *hdev, int sensor_index, u32 attr);
1611 long hl_get_current(struct hl_device *hdev, int sensor_index, u32 attr);
1612 long hl_get_fan_speed(struct hl_device *hdev, int sensor_index, u32 attr);
1613 long hl_get_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr);
1614 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
1615 long value);
1616 u64 hl_get_max_power(struct hl_device *hdev);
1617 void hl_set_max_power(struct hl_device *hdev, u64 value);
1619 #ifdef CONFIG_DEBUG_FS
1621 void hl_debugfs_init(void);
1622 void hl_debugfs_fini(void);
1623 void hl_debugfs_add_device(struct hl_device *hdev);
1624 void hl_debugfs_remove_device(struct hl_device *hdev);
1625 void hl_debugfs_add_file(struct hl_fpriv *hpriv);
1626 void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
1627 void hl_debugfs_add_cb(struct hl_cb *cb);
1628 void hl_debugfs_remove_cb(struct hl_cb *cb);
1629 void hl_debugfs_add_cs(struct hl_cs *cs);
1630 void hl_debugfs_remove_cs(struct hl_cs *cs);
1631 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
1632 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
1633 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
1634 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1635 struct hl_userptr *userptr);
1636 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
1637 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
1639 #else
1641 static inline void __init hl_debugfs_init(void)
1645 static inline void hl_debugfs_fini(void)
1649 static inline void hl_debugfs_add_device(struct hl_device *hdev)
1653 static inline void hl_debugfs_remove_device(struct hl_device *hdev)
1657 static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1661 static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1665 static inline void hl_debugfs_add_cb(struct hl_cb *cb)
1669 static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
1673 static inline void hl_debugfs_add_cs(struct hl_cs *cs)
1677 static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
1681 static inline void hl_debugfs_add_job(struct hl_device *hdev,
1682 struct hl_cs_job *job)
1686 static inline void hl_debugfs_remove_job(struct hl_device *hdev,
1687 struct hl_cs_job *job)
1691 static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
1692 struct hl_userptr *userptr)
1696 static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
1697 struct hl_userptr *userptr)
1701 static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
1702 struct hl_ctx *ctx)
1706 static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
1707 struct hl_ctx *ctx)
1711 #endif
1713 /* IOCTLs */
1714 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
1715 long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
1716 int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
1717 int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
1718 int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data);
1719 int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
1721 #endif /* HABANALABSP_H_ */