OSDN Git Service

Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[uclinux-h8/linux.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 #define pr_fmt(fmt)                     "habanalabs: " fmt
15
16 #include <linux/cdev.h>
17 #include <linux/iopoll.h>
18 #include <linux/irqreturn.h>
19 #include <linux/dma-fence.h>
20 #include <linux/dma-direction.h>
21 #include <linux/scatterlist.h>
22 #include <linux/hashtable.h>
23
24 #define HL_NAME                         "habanalabs"
25
26 #define HL_MMAP_CB_MASK                 (0x8000000000000000ull >> PAGE_SHIFT)
27
28 #define HL_PENDING_RESET_PER_SEC        5
29
30 #define HL_DEVICE_TIMEOUT_USEC          1000000 /* 1 s */
31
32 #define HL_HEARTBEAT_PER_USEC           5000000 /* 5 s */
33
34 #define HL_PLL_LOW_JOB_FREQ_USEC        5000000 /* 5 s */
35
36 #define HL_MAX_QUEUES                   128
37
38 #define HL_MAX_JOBS_PER_CS              64
39
40 /* MUST BE POWER OF 2 and larger than 1 */
41 #define HL_MAX_PENDING_CS               64
42
43 /* Memory */
44 #define MEM_HASH_TABLE_BITS             7 /* 1 << 7 buckets */
45
46 /* MMU */
47 #define MMU_HASH_TABLE_BITS             7 /* 1 << 7 buckets */
48
49 /**
50  * struct pgt_info - MMU hop page info.
51  * @node: hash linked-list node for the pgts hash of pgts.
52  * @addr: physical address of the pgt.
53  * @ctx: pointer to the owner ctx.
54  * @num_of_ptes: indicates how many ptes are used in the pgt.
55  *
56  * The MMU page tables hierarchy is placed on the DRAM. When a new level (hop)
57  * is needed during mapping, a new page is allocated and this structure holds
58  * its essential information. During unmapping, if no valid PTEs remained in the
59  * page, it is freed with its pgt_info structure.
60  */
61 struct pgt_info {
62         struct hlist_node node;
63         u64 addr;
64         struct hl_ctx *ctx;
65         int num_of_ptes;
66 };
67
68 struct hl_device;
69 struct hl_fpriv;
70
71 /**
72  * enum hl_queue_type - Supported QUEUE types.
73  * @QUEUE_TYPE_NA: queue is not available.
74  * @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the
75  *                  host.
76  * @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's
77  *                      memories and/or operates the compute engines.
78  * @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU.
79  */
80 enum hl_queue_type {
81         QUEUE_TYPE_NA,
82         QUEUE_TYPE_EXT,
83         QUEUE_TYPE_INT,
84         QUEUE_TYPE_CPU
85 };
86
87 /**
88  * struct hw_queue_properties - queue information.
89  * @type: queue type.
90  * @kmd_only: true if only KMD is allowed to send a job to this queue, false
91  *            otherwise.
92  */
93 struct hw_queue_properties {
94         enum hl_queue_type      type;
95         u8                      kmd_only;
96 };
97
98 /**
99  * enum vm_type_t - virtual memory mapping request information.
100  * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
101  * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
102  */
103 enum vm_type_t {
104         VM_TYPE_USERPTR,
105         VM_TYPE_PHYS_PACK
106 };
107
108 /**
109  * enum hl_device_hw_state - H/W device state. use this to understand whether
110  *                           to do reset before hw_init or not
111  * @HL_DEVICE_HW_STATE_CLEAN: H/W state is clean. i.e. after hard reset
112  * @HL_DEVICE_HW_STATE_DIRTY: H/W state is dirty. i.e. we started to execute
113  *                            hw_init
114  */
115 enum hl_device_hw_state {
116         HL_DEVICE_HW_STATE_CLEAN = 0,
117         HL_DEVICE_HW_STATE_DIRTY
118 };
119
120 /**
121  * struct asic_fixed_properties - ASIC specific immutable properties.
122  * @hw_queues_props: H/W queues properties.
123  * @armcp_info: received various information from ArmCP regarding the H/W. e.g.
124  *              available sensors.
125  * @uboot_ver: F/W U-boot version.
126  * @preboot_ver: F/W Preboot version.
127  * @sram_base_address: SRAM physical start address.
128  * @sram_end_address: SRAM physical end address.
129  * @sram_user_base_address - SRAM physical start address for user access.
130  * @dram_base_address: DRAM physical start address.
131  * @dram_end_address: DRAM physical end address.
132  * @dram_user_base_address: DRAM physical start address for user access.
133  * @dram_size: DRAM total size.
134  * @dram_pci_bar_size: size of PCI bar towards DRAM.
135  * @host_phys_base_address: base physical address of host memory for
136  *                              transactions that the device generates.
137  * @max_power_default: max power of the device after reset
138  * @va_space_host_start_address: base address of virtual memory range for
139  *                               mapping host memory.
140  * @va_space_host_end_address: end address of virtual memory range for
141  *                             mapping host memory.
142  * @va_space_dram_start_address: base address of virtual memory range for
143  *                               mapping DRAM memory.
144  * @va_space_dram_end_address: end address of virtual memory range for
145  *                             mapping DRAM memory.
146  * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
147  *                                      fault.
148  * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
149  * @mmu_dram_default_page_addr: DRAM default page physical address.
150  * @mmu_pgt_size: MMU page tables total size.
151  * @mmu_pte_size: PTE size in MMU page tables.
152  * @mmu_hop_table_size: MMU hop table size.
153  * @mmu_hop0_tables_total_size: total size of MMU hop0 tables.
154  * @dram_page_size: page size for MMU DRAM allocation.
155  * @cfg_size: configuration space size on SRAM.
156  * @sram_size: total size of SRAM.
157  * @max_asid: maximum number of open contexts (ASIDs).
158  * @num_of_events: number of possible internal H/W IRQs.
159  * @psoc_pci_pll_nr: PCI PLL NR value.
160  * @psoc_pci_pll_nf: PCI PLL NF value.
161  * @psoc_pci_pll_od: PCI PLL OD value.
162  * @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value.
163  * @completion_queues_count: number of completion queues.
164  * @high_pll: high PLL frequency used by the device.
165  * @cb_pool_cb_cnt: number of CBs in the CB pool.
166  * @cb_pool_cb_size: size of each CB in the CB pool.
167  * @tpc_enabled_mask: which TPCs are enabled.
168  */
169 struct asic_fixed_properties {
170         struct hw_queue_properties      hw_queues_props[HL_MAX_QUEUES];
171         struct armcp_info       armcp_info;
172         char                    uboot_ver[VERSION_MAX_LEN];
173         char                    preboot_ver[VERSION_MAX_LEN];
174         u64                     sram_base_address;
175         u64                     sram_end_address;
176         u64                     sram_user_base_address;
177         u64                     dram_base_address;
178         u64                     dram_end_address;
179         u64                     dram_user_base_address;
180         u64                     dram_size;
181         u64                     dram_pci_bar_size;
182         u64                     host_phys_base_address;
183         u64                     max_power_default;
184         u64                     va_space_host_start_address;
185         u64                     va_space_host_end_address;
186         u64                     va_space_dram_start_address;
187         u64                     va_space_dram_end_address;
188         u64                     dram_size_for_default_page_mapping;
189         u64                     mmu_pgt_addr;
190         u64                     mmu_dram_default_page_addr;
191         u32                     mmu_pgt_size;
192         u32                     mmu_pte_size;
193         u32                     mmu_hop_table_size;
194         u32                     mmu_hop0_tables_total_size;
195         u32                     dram_page_size;
196         u32                     cfg_size;
197         u32                     sram_size;
198         u32                     max_asid;
199         u32                     num_of_events;
200         u32                     psoc_pci_pll_nr;
201         u32                     psoc_pci_pll_nf;
202         u32                     psoc_pci_pll_od;
203         u32                     psoc_pci_pll_div_factor;
204         u32                     high_pll;
205         u32                     cb_pool_cb_cnt;
206         u32                     cb_pool_cb_size;
207         u8                      completion_queues_count;
208         u8                      tpc_enabled_mask;
209 };
210
211 /**
212  * struct hl_dma_fence - wrapper for fence object used by command submissions.
213  * @base_fence: kernel fence object.
214  * @lock: spinlock to protect fence.
215  * @hdev: habanalabs device structure.
216  * @cs_seq: command submission sequence number.
217  */
218 struct hl_dma_fence {
219         struct dma_fence        base_fence;
220         spinlock_t              lock;
221         struct hl_device        *hdev;
222         u64                     cs_seq;
223 };
224
225 /*
226  * Command Buffers
227  */
228
229 #define HL_MAX_CB_SIZE          0x200000        /* 2MB */
230
231 /**
232  * struct hl_cb_mgr - describes a Command Buffer Manager.
233  * @cb_lock: protects cb_handles.
234  * @cb_handles: an idr to hold all command buffer handles.
235  */
236 struct hl_cb_mgr {
237         spinlock_t              cb_lock;
238         struct idr              cb_handles; /* protected by cb_lock */
239 };
240
241 /**
242  * struct hl_cb - describes a Command Buffer.
243  * @refcount: reference counter for usage of the CB.
244  * @hdev: pointer to device this CB belongs to.
245  * @lock: spinlock to protect mmap/cs flows.
246  * @debugfs_list: node in debugfs list of command buffers.
247  * @pool_list: node in pool list of command buffers.
248  * @kernel_address: Holds the CB's kernel virtual address.
249  * @bus_address: Holds the CB's DMA address.
250  * @mmap_size: Holds the CB's size that was mmaped.
251  * @size: holds the CB's size.
252  * @id: the CB's ID.
253  * @cs_cnt: holds number of CS that this CB participates in.
254  * @ctx_id: holds the ID of the owner's context.
255  * @mmap: true if the CB is currently mmaped to user.
256  * @is_pool: true if CB was acquired from the pool, false otherwise.
257  */
258 struct hl_cb {
259         struct kref             refcount;
260         struct hl_device        *hdev;
261         spinlock_t              lock;
262         struct list_head        debugfs_list;
263         struct list_head        pool_list;
264         u64                     kernel_address;
265         dma_addr_t              bus_address;
266         u32                     mmap_size;
267         u32                     size;
268         u32                     id;
269         u32                     cs_cnt;
270         u32                     ctx_id;
271         u8                      mmap;
272         u8                      is_pool;
273 };
274
275
276 /*
277  * QUEUES
278  */
279
280 struct hl_cs_job;
281
282 /*
283  * Currently, there are two limitations on the maximum length of a queue:
284  *
285  * 1. The memory footprint of the queue. The current allocated space for the
286  *    queue is PAGE_SIZE. Because each entry in the queue is HL_BD_SIZE,
287  *    the maximum length of the queue can be PAGE_SIZE / HL_BD_SIZE,
288  *    which currently is 4096/16 = 256 entries.
289  *
290  *    To increase that, we need either to decrease the size of the
291  *    BD (difficult), or allocate more than a single page (easier).
292  *
293  * 2. Because the size of the JOB handle field in the BD CTL / completion queue
294  *    is 10-bit, we can have up to 1024 open jobs per hardware queue.
295  *    Therefore, each queue can hold up to 1024 entries.
296  *
297  * HL_QUEUE_LENGTH is in units of struct hl_bd.
298  * HL_QUEUE_LENGTH * sizeof(struct hl_bd) should be <= HL_PAGE_SIZE
299  */
300
301 #define HL_PAGE_SIZE                    4096 /* minimum page size */
302 /* Must be power of 2 (HL_PAGE_SIZE / HL_BD_SIZE) */
303 #define HL_QUEUE_LENGTH                 256
304 #define HL_QUEUE_SIZE_IN_BYTES          (HL_QUEUE_LENGTH * HL_BD_SIZE)
305
306 /*
307  * HL_CQ_LENGTH is in units of struct hl_cq_entry.
308  * HL_CQ_LENGTH should be <= HL_PAGE_SIZE
309  */
310 #define HL_CQ_LENGTH                    HL_QUEUE_LENGTH
311 #define HL_CQ_SIZE_IN_BYTES             (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)
312
313 /* Must be power of 2 (HL_PAGE_SIZE / HL_EQ_ENTRY_SIZE) */
314 #define HL_EQ_LENGTH                    64
315 #define HL_EQ_SIZE_IN_BYTES             (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
316
317
318 /**
319  * struct hl_hw_queue - describes a H/W transport queue.
320  * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
321  * @queue_type: type of queue.
322  * @kernel_address: holds the queue's kernel virtual address.
323  * @bus_address: holds the queue's DMA address.
324  * @pi: holds the queue's pi value.
325  * @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci).
326  * @hw_queue_id: the id of the H/W queue.
327  * @int_queue_len: length of internal queue (number of entries).
328  * @valid: is the queue valid (we have array of 32 queues, not all of them
329  *              exists).
330  */
331 struct hl_hw_queue {
332         struct hl_cs_job        **shadow_queue;
333         enum hl_queue_type      queue_type;
334         u64                     kernel_address;
335         dma_addr_t              bus_address;
336         u32                     pi;
337         u32                     ci;
338         u32                     hw_queue_id;
339         u16                     int_queue_len;
340         u8                      valid;
341 };
342
343 /**
344  * struct hl_cq - describes a completion queue
345  * @hdev: pointer to the device structure
346  * @kernel_address: holds the queue's kernel virtual address
347  * @bus_address: holds the queue's DMA address
348  * @hw_queue_id: the id of the matching H/W queue
349  * @ci: ci inside the queue
350  * @pi: pi inside the queue
351  * @free_slots_cnt: counter of free slots in queue
352  */
353 struct hl_cq {
354         struct hl_device        *hdev;
355         u64                     kernel_address;
356         dma_addr_t              bus_address;
357         u32                     hw_queue_id;
358         u32                     ci;
359         u32                     pi;
360         atomic_t                free_slots_cnt;
361 };
362
363 /**
364  * struct hl_eq - describes the event queue (single one per device)
365  * @hdev: pointer to the device structure
366  * @kernel_address: holds the queue's kernel virtual address
367  * @bus_address: holds the queue's DMA address
368  * @ci: ci inside the queue
369  */
370 struct hl_eq {
371         struct hl_device        *hdev;
372         u64                     kernel_address;
373         dma_addr_t              bus_address;
374         u32                     ci;
375 };
376
377
378 /*
379  * ASICs
380  */
381
382 /**
383  * enum hl_asic_type - supported ASIC types.
384  * @ASIC_AUTO_DETECT: ASIC type will be automatically set.
385  * @ASIC_GOYA: Goya device.
386  * @ASIC_INVALID: Invalid ASIC type.
387  */
388 enum hl_asic_type {
389         ASIC_AUTO_DETECT,
390         ASIC_GOYA,
391         ASIC_INVALID
392 };
393
394 struct hl_cs_parser;
395
396 /**
397  * enum hl_pm_mng_profile - power management profile.
398  * @PM_AUTO: internal clock is set by KMD.
399  * @PM_MANUAL: internal clock is set by the user.
400  * @PM_LAST: last power management type.
401  */
402 enum hl_pm_mng_profile {
403         PM_AUTO = 1,
404         PM_MANUAL,
405         PM_LAST
406 };
407
408 /**
409  * enum hl_pll_frequency - PLL frequency.
410  * @PLL_HIGH: high frequency.
411  * @PLL_LOW: low frequency.
412  * @PLL_LAST: last frequency values that were configured by the user.
413  */
414 enum hl_pll_frequency {
415         PLL_HIGH = 1,
416         PLL_LOW,
417         PLL_LAST
418 };
419
420 /**
421  * struct hl_asic_funcs - ASIC specific functions that are can be called from
422  *                        common code.
423  * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
424  * @early_fini: tears down what was done in early_init.
425  * @late_init: sets up late driver/hw state (post hw_init) - Optional.
426  * @late_fini: tears down what was done in late_init (pre hw_fini) - Optional.
427  * @sw_init: sets up driver state, does not configure H/W.
428  * @sw_fini: tears down driver state, does not configure H/W.
429  * @hw_init: sets up the H/W state.
430  * @hw_fini: tears down the H/W state.
431  * @halt_engines: halt engines, needed for reset sequence. This also disables
432  *                interrupts from the device. Should be called before
433  *                hw_fini and before CS rollback.
434  * @suspend: handles IP specific H/W or SW changes for suspend.
435  * @resume: handles IP specific H/W or SW changes for resume.
436  * @cb_mmap: maps a CB.
437  * @ring_doorbell: increment PI on a given QMAN.
438  * @flush_pq_write: flush PQ entry write if necessary, WARN if flushing failed.
439  * @dma_alloc_coherent: Allocate coherent DMA memory by calling
440  *                      dma_alloc_coherent(). This is ASIC function because its
441  *                      implementation is not trivial when the driver is loaded
442  *                      in simulation mode (not upstreamed).
443  * @dma_free_coherent: Free coherent DMA memory by calling dma_free_coherent().
444  *                     This is ASIC function because its implementation is not
445  *                     trivial when the driver is loaded in simulation mode
446  *                     (not upstreamed).
447  * @get_int_queue_base: get the internal queue base address.
448  * @test_queues: run simple test on all queues for sanity check.
449  * @dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool.
450  *                   size of allocation is HL_DMA_POOL_BLK_SIZE.
451  * @dma_pool_free: free small DMA allocation from pool.
452  * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
453  * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
454  * @hl_dma_unmap_sg: DMA unmap scatter-gather list.
455  * @cs_parser: parse Command Submission.
456  * @asic_dma_map_sg: DMA map scatter-gather list.
457  * @get_dma_desc_list_size: get number of LIN_DMA packets required for CB.
458  * @add_end_of_cb_packets: Add packets to the end of CB, if device requires it.
459  * @update_eq_ci: update event queue CI.
460  * @context_switch: called upon ASID context switch.
461  * @restore_phase_topology: clear all SOBs amd MONs.
462  * @debugfs_read32: debug interface for reading u32 from DRAM/SRAM.
463  * @debugfs_write32: debug interface for writing u32 to DRAM/SRAM.
464  * @add_device_attr: add ASIC specific device attributes.
465  * @handle_eqe: handle event queue entry (IRQ) from ArmCP.
466  * @set_pll_profile: change PLL profile (manual/automatic).
467  * @get_events_stat: retrieve event queue entries histogram.
468  * @read_pte: read MMU page table entry from DRAM.
469  * @write_pte: write MMU page table entry to DRAM.
470  * @mmu_invalidate_cache: flush MMU STLB cache, either with soft (L1 only) or
471  *                        hard (L0 & L1) flush.
472  * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with
473  *                              ASID-VA-size mask.
474  * @send_heartbeat: send is-alive packet to ArmCP and verify response.
475  * @enable_clock_gating: enable clock gating for reducing power consumption.
476  * @disable_clock_gating: disable clock for accessing registers on HBW.
477  * @is_device_idle: return true if device is idle, false otherwise.
478  * @soft_reset_late_init: perform certain actions needed after soft reset.
479  * @hw_queues_lock: acquire H/W queues lock.
480  * @hw_queues_unlock: release H/W queues lock.
481  * @get_pci_id: retrieve PCI ID.
482  * @get_eeprom_data: retrieve EEPROM data from F/W.
483  * @send_cpu_message: send buffer to ArmCP.
484  * @get_hw_state: retrieve the H/W state
485  */
486 struct hl_asic_funcs {
487         int (*early_init)(struct hl_device *hdev);
488         int (*early_fini)(struct hl_device *hdev);
489         int (*late_init)(struct hl_device *hdev);
490         void (*late_fini)(struct hl_device *hdev);
491         int (*sw_init)(struct hl_device *hdev);
492         int (*sw_fini)(struct hl_device *hdev);
493         int (*hw_init)(struct hl_device *hdev);
494         void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
495         void (*halt_engines)(struct hl_device *hdev, bool hard_reset);
496         int (*suspend)(struct hl_device *hdev);
497         int (*resume)(struct hl_device *hdev);
498         int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
499                         u64 kaddress, phys_addr_t paddress, u32 size);
500         void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
501         void (*flush_pq_write)(struct hl_device *hdev, u64 *pq, u64 exp_val);
502         void* (*dma_alloc_coherent)(struct hl_device *hdev, size_t size,
503                                         dma_addr_t *dma_handle, gfp_t flag);
504         void (*dma_free_coherent)(struct hl_device *hdev, size_t size,
505                                         void *cpu_addr, dma_addr_t dma_handle);
506         void* (*get_int_queue_base)(struct hl_device *hdev, u32 queue_id,
507                                 dma_addr_t *dma_handle, u16 *queue_len);
508         int (*test_queues)(struct hl_device *hdev);
509         void* (*dma_pool_zalloc)(struct hl_device *hdev, size_t size,
510                                 gfp_t mem_flags, dma_addr_t *dma_handle);
511         void (*dma_pool_free)(struct hl_device *hdev, void *vaddr,
512                                 dma_addr_t dma_addr);
513         void* (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev,
514                                 size_t size, dma_addr_t *dma_handle);
515         void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,
516                                 size_t size, void *vaddr);
517         void (*hl_dma_unmap_sg)(struct hl_device *hdev,
518                                 struct scatterlist *sg, int nents,
519                                 enum dma_data_direction dir);
520         int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
521         int (*asic_dma_map_sg)(struct hl_device *hdev,
522                                 struct scatterlist *sg, int nents,
523                                 enum dma_data_direction dir);
524         u32 (*get_dma_desc_list_size)(struct hl_device *hdev,
525                                         struct sg_table *sgt);
526         void (*add_end_of_cb_packets)(u64 kernel_address, u32 len, u64 cq_addr,
527                                         u32 cq_val, u32 msix_num);
528         void (*update_eq_ci)(struct hl_device *hdev, u32 val);
529         int (*context_switch)(struct hl_device *hdev, u32 asid);
530         void (*restore_phase_topology)(struct hl_device *hdev);
531         int (*debugfs_read32)(struct hl_device *hdev, u64 addr, u32 *val);
532         int (*debugfs_write32)(struct hl_device *hdev, u64 addr, u32 val);
533         void (*add_device_attr)(struct hl_device *hdev,
534                                 struct attribute_group *dev_attr_grp);
535         void (*handle_eqe)(struct hl_device *hdev,
536                                 struct hl_eq_entry *eq_entry);
537         void (*set_pll_profile)(struct hl_device *hdev,
538                         enum hl_pll_frequency freq);
539         void* (*get_events_stat)(struct hl_device *hdev, u32 *size);
540         u64 (*read_pte)(struct hl_device *hdev, u64 addr);
541         void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
542         void (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard);
543         void (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
544                         u32 asid, u64 va, u64 size);
545         int (*send_heartbeat)(struct hl_device *hdev);
546         void (*enable_clock_gating)(struct hl_device *hdev);
547         void (*disable_clock_gating)(struct hl_device *hdev);
548         bool (*is_device_idle)(struct hl_device *hdev);
549         int (*soft_reset_late_init)(struct hl_device *hdev);
550         void (*hw_queues_lock)(struct hl_device *hdev);
551         void (*hw_queues_unlock)(struct hl_device *hdev);
552         u32 (*get_pci_id)(struct hl_device *hdev);
553         int (*get_eeprom_data)(struct hl_device *hdev, void *data,
554                                 size_t max_size);
555         int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
556                                 u16 len, u32 timeout, long *result);
557         enum hl_device_hw_state (*get_hw_state)(struct hl_device *hdev);
558 };
559
560
561 /*
562  * CONTEXTS
563  */
564
565 #define HL_KERNEL_ASID_ID       0
566
567 /**
568  * struct hl_va_range - virtual addresses range.
569  * @lock: protects the virtual addresses list.
570  * @list: list of virtual addresses blocks available for mappings.
571  * @start_addr: range start address.
572  * @end_addr: range end address.
573  */
574 struct hl_va_range {
575         struct mutex            lock;
576         struct list_head        list;
577         u64                     start_addr;
578         u64                     end_addr;
579 };
580
581 /**
582  * struct hl_ctx - user/kernel context.
583  * @mem_hash: holds mapping from virtual address to virtual memory area
584  *              descriptor (hl_vm_phys_pg_list or hl_userptr).
585  * @mmu_hash: holds a mapping from virtual address to pgt_info structure.
586  * @hpriv: pointer to the private (KMD) data of the process (fd).
587  * @hdev: pointer to the device structure.
588  * @refcount: reference counter for the context. Context is released only when
589  *              this hits 0l. It is incremented on CS and CS_WAIT.
590  * @cs_pending: array of DMA fence objects representing pending CS.
591  * @host_va_range: holds available virtual addresses for host mappings.
592  * @dram_va_range: holds available virtual addresses for DRAM mappings.
593  * @mem_hash_lock: protects the mem_hash.
594  * @mmu_lock: protects the MMU page tables. Any change to the PGT, modifing the
595  *            MMU hash or walking the PGT requires talking this lock
596  * @debugfs_list: node in debugfs list of contexts.
597  * @cs_sequence: sequence number for CS. Value is assigned to a CS and passed
598  *                      to user so user could inquire about CS. It is used as
599  *                      index to cs_pending array.
600  * @dram_default_hops: array that holds all hops addresses needed for default
601  *                     DRAM mapping.
602  * @cs_lock: spinlock to protect cs_sequence.
603  * @dram_phys_mem: amount of used physical DRAM memory by this context.
604  * @thread_restore_token: token to prevent multiple threads of the same context
605  *                              from running the restore phase. Only one thread
606  *                              should run it.
607  * @thread_restore_wait_token: token to prevent the threads that didn't run
608  *                              the restore phase from moving to their execution
609  *                              phase before the restore phase has finished.
610  * @asid: context's unique address space ID in the device's MMU.
611  */
612 struct hl_ctx {
613         DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
614         DECLARE_HASHTABLE(mmu_hash, MMU_HASH_TABLE_BITS);
615         struct hl_fpriv         *hpriv;
616         struct hl_device        *hdev;
617         struct kref             refcount;
618         struct dma_fence        *cs_pending[HL_MAX_PENDING_CS];
619         struct hl_va_range      host_va_range;
620         struct hl_va_range      dram_va_range;
621         struct mutex            mem_hash_lock;
622         struct mutex            mmu_lock;
623         struct list_head        debugfs_list;
624         u64                     cs_sequence;
625         u64                     *dram_default_hops;
626         spinlock_t              cs_lock;
627         atomic64_t              dram_phys_mem;
628         atomic_t                thread_restore_token;
629         u32                     thread_restore_wait_token;
630         u32                     asid;
631 };
632
633 /**
634  * struct hl_ctx_mgr - for handling multiple contexts.
635  * @ctx_lock: protects ctx_handles.
636  * @ctx_handles: idr to hold all ctx handles.
637  */
638 struct hl_ctx_mgr {
639         struct mutex            ctx_lock;
640         struct idr              ctx_handles;
641 };
642
643
644
645 /*
646  * COMMAND SUBMISSIONS
647  */
648
649 /**
650  * struct hl_userptr - memory mapping chunk information
651  * @vm_type: type of the VM.
652  * @job_node: linked-list node for hanging the object on the Job's list.
653  * @vec: pointer to the frame vector.
654  * @sgt: pointer to the scatter-gather table that holds the pages.
655  * @dir: for DMA unmapping, the direction must be supplied, so save it.
656  * @debugfs_list: node in debugfs list of command submissions.
657  * @addr: user-space virtual pointer to the start of the memory area.
658  * @size: size of the memory area to pin & map.
659  * @dma_mapped: true if the SG was mapped to DMA addresses, false otherwise.
660  */
661 struct hl_userptr {
662         enum vm_type_t          vm_type; /* must be first */
663         struct list_head        job_node;
664         struct frame_vector     *vec;
665         struct sg_table         *sgt;
666         enum dma_data_direction dir;
667         struct list_head        debugfs_list;
668         u64                     addr;
669         u32                     size;
670         u8                      dma_mapped;
671 };
672
673 /**
674  * struct hl_cs - command submission.
675  * @jobs_in_queue_cnt: per each queue, maintain counter of submitted jobs.
676  * @ctx: the context this CS belongs to.
677  * @job_list: list of the CS's jobs in the various queues.
678  * @job_lock: spinlock for the CS's jobs list. Needed for free_job.
679  * @refcount: reference counter for usage of the CS.
680  * @fence: pointer to the fence object of this CS.
681  * @work_tdr: delayed work node for TDR.
682  * @mirror_node : node in device mirror list of command submissions.
683  * @debugfs_list: node in debugfs list of command submissions.
684  * @sequence: the sequence number of this CS.
685  * @submitted: true if CS was submitted to H/W.
686  * @completed: true if CS was completed by device.
687  * @timedout : true if CS was timedout.
688  * @tdr_active: true if TDR was activated for this CS (to prevent
689  *              double TDR activation).
690  * @aborted: true if CS was aborted due to some device error.
691  */
692 struct hl_cs {
693         u8                      jobs_in_queue_cnt[HL_MAX_QUEUES];
694         struct hl_ctx           *ctx;
695         struct list_head        job_list;
696         spinlock_t              job_lock;
697         struct kref             refcount;
698         struct dma_fence        *fence;
699         struct delayed_work     work_tdr;
700         struct list_head        mirror_node;
701         struct list_head        debugfs_list;
702         u64                     sequence;
703         u8                      submitted;
704         u8                      completed;
705         u8                      timedout;
706         u8                      tdr_active;
707         u8                      aborted;
708 };
709
710 /**
711  * struct hl_cs_job - command submission job.
712  * @cs_node: the node to hang on the CS jobs list.
713  * @cs: the CS this job belongs to.
714  * @user_cb: the CB we got from the user.
715  * @patched_cb: in case of patching, this is internal CB which is submitted on
716  *              the queue instead of the CB we got from the IOCTL.
717  * @finish_work: workqueue object to run when job is completed.
718  * @userptr_list: linked-list of userptr mappings that belong to this job and
719  *                      wait for completion.
720  * @debugfs_list: node in debugfs list of command submission jobs.
721  * @id: the id of this job inside a CS.
722  * @hw_queue_id: the id of the H/W queue this job is submitted to.
723  * @user_cb_size: the actual size of the CB we got from the user.
724  * @job_cb_size: the actual size of the CB that we put on the queue.
725  * @ext_queue: whether the job is for external queue or internal queue.
726  */
727 struct hl_cs_job {
728         struct list_head        cs_node;
729         struct hl_cs            *cs;
730         struct hl_cb            *user_cb;
731         struct hl_cb            *patched_cb;
732         struct work_struct      finish_work;
733         struct list_head        userptr_list;
734         struct list_head        debugfs_list;
735         u32                     id;
736         u32                     hw_queue_id;
737         u32                     user_cb_size;
738         u32                     job_cb_size;
739         u8                      ext_queue;
740 };
741
742 /**
743  * struct hl_cs_parser - command submission paerser properties.
744  * @user_cb: the CB we got from the user.
745  * @patched_cb: in case of patching, this is internal CB which is submitted on
746  *              the queue instead of the CB we got from the IOCTL.
747  * @job_userptr_list: linked-list of userptr mappings that belong to the related
748  *                      job and wait for completion.
749  * @cs_sequence: the sequence number of the related CS.
750  * @ctx_id: the ID of the context the related CS belongs to.
751  * @hw_queue_id: the id of the H/W queue this job is submitted to.
752  * @user_cb_size: the actual size of the CB we got from the user.
753  * @patched_cb_size: the size of the CB after parsing.
754  * @ext_queue: whether the job is for external queue or internal queue.
755  * @job_id: the id of the related job inside the related CS.
756  * @use_virt_addr: whether to treat the addresses in the CB as virtual during
757  *                      parsing.
758  */
759 struct hl_cs_parser {
760         struct hl_cb            *user_cb;
761         struct hl_cb            *patched_cb;
762         struct list_head        *job_userptr_list;
763         u64                     cs_sequence;
764         u32                     ctx_id;
765         u32                     hw_queue_id;
766         u32                     user_cb_size;
767         u32                     patched_cb_size;
768         u8                      ext_queue;
769         u8                      job_id;
770         u8                      use_virt_addr;
771 };
772
773
774 /*
775  * MEMORY STRUCTURE
776  */
777
778 /**
779  * struct hl_vm_hash_node - hash element from virtual address to virtual
780  *                              memory area descriptor (hl_vm_phys_pg_list or
781  *                              hl_userptr).
782  * @node: node to hang on the hash table in context object.
783  * @vaddr: key virtual address.
784  * @ptr: value pointer (hl_vm_phys_pg_list or hl_userptr).
785  */
786 struct hl_vm_hash_node {
787         struct hlist_node       node;
788         u64                     vaddr;
789         void                    *ptr;
790 };
791
792 /**
793  * struct hl_vm_phys_pg_pack - physical page pack.
794  * @vm_type: describes the type of the virtual area descriptor.
795  * @pages: the physical page array.
796  * @npages: num physical pages in the pack.
797  * @total_size: total size of all the pages in this list.
798  * @mapping_cnt: number of shared mappings.
799  * @asid: the context related to this list.
800  * @page_size: size of each page in the pack.
801  * @flags: HL_MEM_* flags related to this list.
802  * @handle: the provided handle related to this list.
803  * @offset: offset from the first page.
804  * @contiguous: is contiguous physical memory.
805  * @created_from_userptr: is product of host virtual address.
806  */
807 struct hl_vm_phys_pg_pack {
808         enum vm_type_t          vm_type; /* must be first */
809         u64                     *pages;
810         u64                     npages;
811         u64                     total_size;
812         atomic_t                mapping_cnt;
813         u32                     asid;
814         u32                     page_size;
815         u32                     flags;
816         u32                     handle;
817         u32                     offset;
818         u8                      contiguous;
819         u8                      created_from_userptr;
820 };
821
822 /**
823  * struct hl_vm_va_block - virtual range block information.
824  * @node: node to hang on the virtual range list in context object.
825  * @start: virtual range start address.
826  * @end: virtual range end address.
827  * @size: virtual range size.
828  */
829 struct hl_vm_va_block {
830         struct list_head        node;
831         u64                     start;
832         u64                     end;
833         u64                     size;
834 };
835
836 /**
837  * struct hl_vm - virtual memory manager for MMU.
838  * @dram_pg_pool: pool for DRAM physical pages of 2MB.
839  * @dram_pg_pool_refcount: reference counter for the pool usage.
840  * @idr_lock: protects the phys_pg_list_handles.
841  * @phys_pg_pack_handles: idr to hold all device allocations handles.
842  * @init_done: whether initialization was done. We need this because VM
843  *              initialization might be skipped during device initialization.
844  */
845 struct hl_vm {
846         struct gen_pool         *dram_pg_pool;
847         struct kref             dram_pg_pool_refcount;
848         spinlock_t              idr_lock;
849         struct idr              phys_pg_pack_handles;
850         u8                      init_done;
851 };
852
853 /*
854  * FILE PRIVATE STRUCTURE
855  */
856
857 /**
858  * struct hl_fpriv - process information stored in FD private data.
859  * @hdev: habanalabs device structure.
860  * @filp: pointer to the given file structure.
861  * @taskpid: current process ID.
862  * @ctx: current executing context.
863  * @ctx_mgr: context manager to handle multiple context for this FD.
864  * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
865  * @debugfs_list: list of relevant ASIC debugfs.
866  * @refcount: number of related contexts.
867  * @restore_phase_mutex: lock for context switch and restore phase.
868  */
869 struct hl_fpriv {
870         struct hl_device        *hdev;
871         struct file             *filp;
872         struct pid              *taskpid;
873         struct hl_ctx           *ctx; /* TODO: remove for multiple ctx */
874         struct hl_ctx_mgr       ctx_mgr;
875         struct hl_cb_mgr        cb_mgr;
876         struct list_head        debugfs_list;
877         struct kref             refcount;
878         struct mutex            restore_phase_mutex;
879 };
880
881
882 /*
883  * DebugFS
884  */
885
886 /**
887  * struct hl_info_list - debugfs file ops.
888  * @name: file name.
889  * @show: function to output information.
890  * @write: function to write to the file.
891  */
892 struct hl_info_list {
893         const char      *name;
894         int             (*show)(struct seq_file *s, void *data);
895         ssize_t         (*write)(struct file *file, const char __user *buf,
896                                 size_t count, loff_t *f_pos);
897 };
898
899 /**
900  * struct hl_debugfs_entry - debugfs dentry wrapper.
901  * @dent: base debugfs entry structure.
902  * @info_ent: dentry realted ops.
903  * @dev_entry: ASIC specific debugfs manager.
904  */
905 struct hl_debugfs_entry {
906         struct dentry                   *dent;
907         const struct hl_info_list       *info_ent;
908         struct hl_dbg_device_entry      *dev_entry;
909 };
910
911 /**
912  * struct hl_dbg_device_entry - ASIC specific debugfs manager.
913  * @root: root dentry.
914  * @hdev: habanalabs device structure.
915  * @entry_arr: array of available hl_debugfs_entry.
916  * @file_list: list of available debugfs files.
917  * @file_mutex: protects file_list.
918  * @cb_list: list of available CBs.
919  * @cb_spinlock: protects cb_list.
920  * @cs_list: list of available CSs.
921  * @cs_spinlock: protects cs_list.
922  * @cs_job_list: list of available CB jobs.
923  * @cs_job_spinlock: protects cs_job_list.
924  * @userptr_list: list of available userptrs (virtual memory chunk descriptor).
925  * @userptr_spinlock: protects userptr_list.
926  * @ctx_mem_hash_list: list of available contexts with MMU mappings.
927  * @ctx_mem_hash_spinlock: protects cb_list.
928  * @addr: next address to read/write from/to in read/write32.
929  * @mmu_addr: next virtual address to translate to physical address in mmu_show.
930  * @mmu_asid: ASID to use while translating in mmu_show.
931  * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
932  * @i2c_bus: generic u8 debugfs file for address value to use in i2c_data_read.
933  * @i2c_bus: generic u8 debugfs file for register value to use in i2c_data_read.
934  */
935 struct hl_dbg_device_entry {
936         struct dentry                   *root;
937         struct hl_device                *hdev;
938         struct hl_debugfs_entry         *entry_arr;
939         struct list_head                file_list;
940         struct mutex                    file_mutex;
941         struct list_head                cb_list;
942         spinlock_t                      cb_spinlock;
943         struct list_head                cs_list;
944         spinlock_t                      cs_spinlock;
945         struct list_head                cs_job_list;
946         spinlock_t                      cs_job_spinlock;
947         struct list_head                userptr_list;
948         spinlock_t                      userptr_spinlock;
949         struct list_head                ctx_mem_hash_list;
950         spinlock_t                      ctx_mem_hash_spinlock;
951         u64                             addr;
952         u64                             mmu_addr;
953         u32                             mmu_asid;
954         u8                              i2c_bus;
955         u8                              i2c_addr;
956         u8                              i2c_reg;
957 };
958
959
960 /*
961  * DEVICES
962  */
963
964 /* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
965  * x16 cards. In extereme cases, there are hosts that can accommodate 16 cards
966  */
967 #define HL_MAX_MINORS   256
968
969 /*
970  * Registers read & write functions.
971  */
972
973 u32 hl_rreg(struct hl_device *hdev, u32 reg);
974 void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
975
976 #define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
977         readl_poll_timeout(hdev->rmmio + addr, val, cond, sleep_us, timeout_us)
978
979 #define RREG32(reg) hl_rreg(hdev, (reg))
980 #define WREG32(reg, v) hl_wreg(hdev, (reg), (v))
981 #define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n",    \
982                                 hl_rreg(hdev, (reg)))
983
984 #define WREG32_P(reg, val, mask)                                \
985         do {                                                    \
986                 u32 tmp_ = RREG32(reg);                         \
987                 tmp_ &= (mask);                                 \
988                 tmp_ |= ((val) & ~(mask));                      \
989                 WREG32(reg, tmp_);                              \
990         } while (0)
991 #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
992 #define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
993
994 #define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
995 #define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
996 #define WREG32_FIELD(reg, field, val)   \
997         WREG32(mm##reg, (RREG32(mm##reg) & ~REG_FIELD_MASK(reg, field)) | \
998                         (val) << REG_FIELD_SHIFT(reg, field))
999
1000 struct hwmon_chip_info;
1001
1002 /**
1003  * struct hl_device_reset_work - reset workqueue task wrapper.
1004  * @reset_work: reset work to be done.
1005  * @hdev: habanalabs device structure.
1006  */
1007 struct hl_device_reset_work {
1008         struct work_struct              reset_work;
1009         struct hl_device                *hdev;
1010 };
1011
1012 /**
1013  * struct hl_device - habanalabs device structure.
1014  * @pdev: pointer to PCI device, can be NULL in case of simulator device.
1015  * @pcie_bar: array of available PCIe bars.
1016  * @rmmio: configuration area address on SRAM.
1017  * @cdev: related char device.
1018  * @dev: realted kernel basic device structure.
1019  * @work_freq: delayed work to lower device frequency if possible.
1020  * @work_heartbeat: delayed work for ArmCP is-alive check.
1021  * @asic_name: ASIC specific nmae.
1022  * @asic_type: ASIC specific type.
1023  * @completion_queue: array of hl_cq.
1024  * @cq_wq: work queue of completion queues for executing work in process context
1025  * @eq_wq: work queue of event queue for executing work in process context.
1026  * @kernel_ctx: KMD context structure.
1027  * @kernel_queues: array of hl_hw_queue.
1028  * @hw_queues_mirror_list: CS mirror list for TDR.
1029  * @hw_queues_mirror_lock: protects hw_queues_mirror_list.
1030  * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs.
1031  * @event_queue: event queue for IRQ from ArmCP.
1032  * @dma_pool: DMA pool for small allocations.
1033  * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
1034  * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
1035  * @cpu_accessible_dma_pool: KMD <-> ArmCP shared memory pool.
1036  * @asid_bitmap: holds used/available ASIDs.
1037  * @asid_mutex: protects asid_bitmap.
1038  * @fd_open_cnt_lock: lock for updating fd_open_cnt in hl_device_open. Although
1039  *                    fd_open_cnt is atomic, we need this lock to serialize
1040  *                    the open function because the driver currently supports
1041  *                    only a single process at a time. In addition, we need a
1042  *                    lock here so we can flush user processes which are opening
1043  *                    the device while we are trying to hard reset it
1044  * @send_cpu_message_lock: enforces only one message in KMD <-> ArmCP queue.
1045  * @asic_prop: ASIC specific immutable properties.
1046  * @asic_funcs: ASIC specific functions.
1047  * @asic_specific: ASIC specific information to use only from ASIC files.
1048  * @mmu_pgt_pool: pool of available MMU hops.
1049  * @vm: virtual memory manager for MMU.
1050  * @mmu_cache_lock: protects MMU cache invalidation as it can serve one context
1051  * @hwmon_dev: H/W monitor device.
1052  * @pm_mng_profile: current power management profile.
1053  * @hl_chip_info: ASIC's sensors information.
1054  * @hl_debugfs: device's debugfs manager.
1055  * @cb_pool: list of preallocated CBs.
1056  * @cb_pool_lock: protects the CB pool.
1057  * @user_ctx: current user context executing.
1058  * @dram_used_mem: current DRAM memory consumption.
1059  * @timeout_jiffies: device CS timeout value.
1060  * @max_power: the max power of the device, as configured by the sysadmin. This
1061  *             value is saved so in case of hard-reset, KMD will restore this
1062  *             value and update the F/W after the re-initialization
1063  * @in_reset: is device in reset flow.
1064  * @curr_pll_profile: current PLL profile.
1065  * @fd_open_cnt: number of open user processes.
1066  * @cs_active_cnt: number of active command submissions on this device (active
1067  *                 means already in H/W queues)
1068  * @major: habanalabs KMD major.
1069  * @high_pll: high PLL profile frequency.
1070  * @soft_reset_cnt: number of soft reset since KMD loading.
1071  * @hard_reset_cnt: number of hard reset since KMD loading.
1072  * @id: device minor.
1073  * @disabled: is device disabled.
1074  * @late_init_done: is late init stage was done during initialization.
1075  * @hwmon_initialized: is H/W monitor sensors was initialized.
1076  * @hard_reset_pending: is there a hard reset work pending.
1077  * @heartbeat: is heartbeat sanity check towards ArmCP enabled.
1078  * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
1079  *                   otherwise.
1080  * @dram_supports_virtual_memory: is MMU enabled towards DRAM.
1081  * @dram_default_page_mapping: is DRAM default page mapping enabled.
1082  * @init_done: is the initialization of the device done.
1083  * @mmu_enable: is MMU enabled.
1084  * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
1085  */
1086 struct hl_device {
1087         struct pci_dev                  *pdev;
1088         void __iomem                    *pcie_bar[6];
1089         void __iomem                    *rmmio;
1090         struct cdev                     cdev;
1091         struct device                   *dev;
1092         struct delayed_work             work_freq;
1093         struct delayed_work             work_heartbeat;
1094         char                            asic_name[16];
1095         enum hl_asic_type               asic_type;
1096         struct hl_cq                    *completion_queue;
1097         struct workqueue_struct         *cq_wq;
1098         struct workqueue_struct         *eq_wq;
1099         struct hl_ctx                   *kernel_ctx;
1100         struct hl_hw_queue              *kernel_queues;
1101         struct list_head                hw_queues_mirror_list;
1102         spinlock_t                      hw_queues_mirror_lock;
1103         struct hl_cb_mgr                kernel_cb_mgr;
1104         struct hl_eq                    event_queue;
1105         struct dma_pool                 *dma_pool;
1106         void                            *cpu_accessible_dma_mem;
1107         dma_addr_t                      cpu_accessible_dma_address;
1108         struct gen_pool                 *cpu_accessible_dma_pool;
1109         unsigned long                   *asid_bitmap;
1110         struct mutex                    asid_mutex;
1111         /* TODO: remove fd_open_cnt_lock for multiple process support */
1112         struct mutex                    fd_open_cnt_lock;
1113         struct mutex                    send_cpu_message_lock;
1114         struct asic_fixed_properties    asic_prop;
1115         const struct hl_asic_funcs      *asic_funcs;
1116         void                            *asic_specific;
1117         struct gen_pool                 *mmu_pgt_pool;
1118         struct hl_vm                    vm;
1119         struct mutex                    mmu_cache_lock;
1120         struct device                   *hwmon_dev;
1121         enum hl_pm_mng_profile          pm_mng_profile;
1122         struct hwmon_chip_info          *hl_chip_info;
1123
1124         struct hl_dbg_device_entry      hl_debugfs;
1125
1126         struct list_head                cb_pool;
1127         spinlock_t                      cb_pool_lock;
1128
1129         /* TODO: remove user_ctx for multiple process support */
1130         struct hl_ctx                   *user_ctx;
1131
1132         atomic64_t                      dram_used_mem;
1133         u64                             timeout_jiffies;
1134         u64                             max_power;
1135         atomic_t                        in_reset;
1136         atomic_t                        curr_pll_profile;
1137         atomic_t                        fd_open_cnt;
1138         atomic_t                        cs_active_cnt;
1139         u32                             major;
1140         u32                             high_pll;
1141         u32                             soft_reset_cnt;
1142         u32                             hard_reset_cnt;
1143         u16                             id;
1144         u8                              disabled;
1145         u8                              late_init_done;
1146         u8                              hwmon_initialized;
1147         u8                              hard_reset_pending;
1148         u8                              heartbeat;
1149         u8                              reset_on_lockup;
1150         u8                              dram_supports_virtual_memory;
1151         u8                              dram_default_page_mapping;
1152         u8                              init_done;
1153         u8                              device_cpu_disabled;
1154
1155         /* Parameters for bring-up */
1156         u8                              mmu_enable;
1157         u8                              cpu_enable;
1158         u8                              reset_pcilink;
1159         u8                              cpu_queues_enable;
1160         u8                              fw_loading;
1161         u8                              pldm;
1162 };
1163
1164
1165 /*
1166  * IOCTLs
1167  */
1168
1169 /**
1170  * typedef hl_ioctl_t - typedef for ioctl function in the driver
1171  * @hpriv: pointer to the FD's private data, which contains state of
1172  *              user process
1173  * @data: pointer to the input/output arguments structure of the IOCTL
1174  *
1175  * Return: 0 for success, negative value for error
1176  */
1177 typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
1178
1179 /**
1180  * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
1181  * @cmd: the IOCTL code as created by the kernel macros.
1182  * @func: pointer to the driver's function that should be called for this IOCTL.
1183  */
1184 struct hl_ioctl_desc {
1185         unsigned int cmd;
1186         hl_ioctl_t *func;
1187 };
1188
1189
1190 /*
1191  * Kernel module functions that can be accessed by entire module
1192  */
1193
1194 /**
1195  * hl_mem_area_inside_range() - Checks whether address+size are inside a range.
1196  * @address: The start address of the area we want to validate.
1197  * @size: The size in bytes of the area we want to validate.
1198  * @range_start_address: The start address of the valid range.
1199  * @range_end_address: The end address of the valid range.
1200  *
1201  * Return: true if the area is inside the valid range, false otherwise.
1202  */
1203 static inline bool hl_mem_area_inside_range(u64 address, u32 size,
1204                                 u64 range_start_address, u64 range_end_address)
1205 {
1206         u64 end_address = address + size;
1207
1208         if ((address >= range_start_address) &&
1209                         (end_address <= range_end_address) &&
1210                         (end_address > address))
1211                 return true;
1212
1213         return false;
1214 }
1215
1216 /**
1217  * hl_mem_area_crosses_range() - Checks whether address+size crossing a range.
1218  * @address: The start address of the area we want to validate.
1219  * @size: The size in bytes of the area we want to validate.
1220  * @range_start_address: The start address of the valid range.
1221  * @range_end_address: The end address of the valid range.
1222  *
1223  * Return: true if the area overlaps part or all of the valid range,
1224  *              false otherwise.
1225  */
1226 static inline bool hl_mem_area_crosses_range(u64 address, u32 size,
1227                                 u64 range_start_address, u64 range_end_address)
1228 {
1229         u64 end_address = address + size;
1230
1231         if ((address >= range_start_address) &&
1232                         (address < range_end_address))
1233                 return true;
1234
1235         if ((end_address >= range_start_address) &&
1236                         (end_address < range_end_address))
1237                 return true;
1238
1239         if ((address < range_start_address) &&
1240                         (end_address >= range_end_address))
1241                 return true;
1242
1243         return false;
1244 }
1245
1246 int hl_device_open(struct inode *inode, struct file *filp);
1247 bool hl_device_disabled_or_in_reset(struct hl_device *hdev);
1248 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
1249                 enum hl_asic_type asic_type, int minor);
1250 void destroy_hdev(struct hl_device *hdev);
1251 int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr, u32 timeout_us,
1252                                 u32 *val);
1253 int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
1254                                 u32 timeout_us, u32 *val);
1255 int hl_hw_queues_create(struct hl_device *hdev);
1256 void hl_hw_queues_destroy(struct hl_device *hdev);
1257 int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
1258                                 u32 cb_size, u64 cb_ptr);
1259 int hl_hw_queue_schedule_cs(struct hl_cs *cs);
1260 u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
1261 void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
1262 void hl_int_hw_queue_update_ci(struct hl_cs *cs);
1263 void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
1264
1265 #define hl_queue_inc_ptr(p)             hl_hw_queue_add_ptr(p, 1)
1266 #define hl_pi_2_offset(pi)              ((pi) & (HL_QUEUE_LENGTH - 1))
1267
1268 int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
1269 void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
1270 int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
1271 void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
1272 void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
1273 void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
1274 irqreturn_t hl_irq_handler_cq(int irq, void *arg);
1275 irqreturn_t hl_irq_handler_eq(int irq, void *arg);
1276 u32 hl_cq_inc_ptr(u32 ptr);
1277
1278 int hl_asid_init(struct hl_device *hdev);
1279 void hl_asid_fini(struct hl_device *hdev);
1280 unsigned long hl_asid_alloc(struct hl_device *hdev);
1281 void hl_asid_free(struct hl_device *hdev, unsigned long asid);
1282
1283 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
1284 void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
1285 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
1286 void hl_ctx_do_release(struct kref *ref);
1287 void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx);
1288 int hl_ctx_put(struct hl_ctx *ctx);
1289 struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
1290 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
1291 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
1292
1293 int hl_device_init(struct hl_device *hdev, struct class *hclass);
1294 void hl_device_fini(struct hl_device *hdev);
1295 int hl_device_suspend(struct hl_device *hdev);
1296 int hl_device_resume(struct hl_device *hdev);
1297 int hl_device_reset(struct hl_device *hdev, bool hard_reset,
1298                         bool from_hard_reset_thread);
1299 void hl_hpriv_get(struct hl_fpriv *hpriv);
1300 void hl_hpriv_put(struct hl_fpriv *hpriv);
1301 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq);
1302
1303 int hl_build_hwmon_channel_info(struct hl_device *hdev,
1304                 struct armcp_sensor *sensors_arr);
1305
1306 int hl_sysfs_init(struct hl_device *hdev);
1307 void hl_sysfs_fini(struct hl_device *hdev);
1308
1309 int hl_hwmon_init(struct hl_device *hdev);
1310 void hl_hwmon_fini(struct hl_device *hdev);
1311
1312 int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr, u32 cb_size,
1313                 u64 *handle, int ctx_id);
1314 int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
1315 int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
1316 struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
1317                         u32 handle);
1318 void hl_cb_put(struct hl_cb *cb);
1319 void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
1320 void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
1321 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size);
1322 int hl_cb_pool_init(struct hl_device *hdev);
1323 int hl_cb_pool_fini(struct hl_device *hdev);
1324
1325 void hl_cs_rollback_all(struct hl_device *hdev);
1326 struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev, bool ext_queue);
1327
1328 void goya_set_asic_funcs(struct hl_device *hdev);
1329
1330 int hl_vm_ctx_init(struct hl_ctx *ctx);
1331 void hl_vm_ctx_fini(struct hl_ctx *ctx);
1332
1333 int hl_vm_init(struct hl_device *hdev);
1334 void hl_vm_fini(struct hl_device *hdev);
1335
1336 int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
1337                         struct hl_userptr *userptr);
1338 int hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
1339 void hl_userptr_delete_list(struct hl_device *hdev,
1340                                 struct list_head *userptr_list);
1341 bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, u32 size,
1342                                 struct list_head *userptr_list,
1343                                 struct hl_userptr **userptr);
1344
1345 int hl_mmu_init(struct hl_device *hdev);
1346 void hl_mmu_fini(struct hl_device *hdev);
1347 int hl_mmu_ctx_init(struct hl_ctx *ctx);
1348 void hl_mmu_ctx_fini(struct hl_ctx *ctx);
1349 int hl_mmu_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size);
1350 int hl_mmu_unmap(struct hl_ctx *ctx, u64 virt_addr, u32 page_size);
1351 void hl_mmu_swap_out(struct hl_ctx *ctx);
1352 void hl_mmu_swap_in(struct hl_ctx *ctx);
1353
1354 long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr);
1355 void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq);
1356 long hl_get_temperature(struct hl_device *hdev, int sensor_index, u32 attr);
1357 long hl_get_voltage(struct hl_device *hdev, int sensor_index, u32 attr);
1358 long hl_get_current(struct hl_device *hdev, int sensor_index, u32 attr);
1359 long hl_get_fan_speed(struct hl_device *hdev, int sensor_index, u32 attr);
1360 long hl_get_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr);
1361 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
1362                         long value);
1363 u64 hl_get_max_power(struct hl_device *hdev);
1364 void hl_set_max_power(struct hl_device *hdev, u64 value);
1365
1366 #ifdef CONFIG_DEBUG_FS
1367
1368 void hl_debugfs_init(void);
1369 void hl_debugfs_fini(void);
1370 void hl_debugfs_add_device(struct hl_device *hdev);
1371 void hl_debugfs_remove_device(struct hl_device *hdev);
1372 void hl_debugfs_add_file(struct hl_fpriv *hpriv);
1373 void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
1374 void hl_debugfs_add_cb(struct hl_cb *cb);
1375 void hl_debugfs_remove_cb(struct hl_cb *cb);
1376 void hl_debugfs_add_cs(struct hl_cs *cs);
1377 void hl_debugfs_remove_cs(struct hl_cs *cs);
1378 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
1379 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
1380 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
1381 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1382                                 struct hl_userptr *userptr);
1383 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
1384 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
1385
1386 #else
1387
1388 static inline void __init hl_debugfs_init(void)
1389 {
1390 }
1391
1392 static inline void hl_debugfs_fini(void)
1393 {
1394 }
1395
1396 static inline void hl_debugfs_add_device(struct hl_device *hdev)
1397 {
1398 }
1399
1400 static inline void hl_debugfs_remove_device(struct hl_device *hdev)
1401 {
1402 }
1403
1404 static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1405 {
1406 }
1407
1408 static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1409 {
1410 }
1411
1412 static inline void hl_debugfs_add_cb(struct hl_cb *cb)
1413 {
1414 }
1415
1416 static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
1417 {
1418 }
1419
1420 static inline void hl_debugfs_add_cs(struct hl_cs *cs)
1421 {
1422 }
1423
1424 static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
1425 {
1426 }
1427
1428 static inline void hl_debugfs_add_job(struct hl_device *hdev,
1429                                         struct hl_cs_job *job)
1430 {
1431 }
1432
1433 static inline void hl_debugfs_remove_job(struct hl_device *hdev,
1434                                         struct hl_cs_job *job)
1435 {
1436 }
1437
1438 static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
1439                                         struct hl_userptr *userptr)
1440 {
1441 }
1442
1443 static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
1444                                         struct hl_userptr *userptr)
1445 {
1446 }
1447
1448 static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
1449                                         struct hl_ctx *ctx)
1450 {
1451 }
1452
1453 static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
1454                                         struct hl_ctx *ctx)
1455 {
1456 }
1457
1458 #endif
1459
1460 /* IOCTLs */
1461 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
1462 int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
1463 int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
1464 int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data);
1465 int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
1466
1467 #endif /* HABANALABSP_H_ */