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