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