blob: 18a03657780ae5d8b8be4d4cab73db477fc85970 [file] [log] [blame]
Oded Gabbayc4d66342019-02-16 00:39:11 +02001/* 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 Gabbay839c4802019-02-16 00:39:16 +020011#include "include/armcp_if.h"
Oded Gabbay9494a8d2019-02-16 00:39:17 +020012#include "include/qman_if.h"
Oded Gabbay839c4802019-02-16 00:39:16 +020013
Oded Gabbayc4d66342019-02-16 00:39:11 +020014#define pr_fmt(fmt) "habanalabs: " fmt
15
16#include <linux/cdev.h>
Oded Gabbay839c4802019-02-16 00:39:16 +020017#include <linux/iopoll.h>
Oded Gabbay1251f232019-02-16 00:39:18 +020018#include <linux/irqreturn.h>
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020019#include <linux/dma-fence.h>
20#include <linux/dma-direction.h>
21#include <linux/scatterlist.h>
Omer Shpigelman0feaf862019-02-16 00:39:22 +020022#include <linux/hashtable.h>
Oded Gabbayc4d66342019-02-16 00:39:11 +020023
24#define HL_NAME "habanalabs"
25
Oded Gabbaybe5d9262019-02-16 00:39:15 +020026#define HL_MMAP_CB_MASK (0x8000000000000000ull >> PAGE_SHIFT)
27
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +020028#define HL_PENDING_RESET_PER_SEC 5
29
Oded Gabbay839c4802019-02-16 00:39:16 +020030#define HL_DEVICE_TIMEOUT_USEC 1000000 /* 1 s */
31
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +020032#define HL_HEARTBEAT_PER_USEC 5000000 /* 5 s */
33
Oded Gabbayd91389b2019-02-16 00:39:19 +020034#define HL_PLL_LOW_JOB_FREQ_USEC 5000000 /* 5 s */
35
Tomer Tayar3110c602019-03-04 10:22:09 +020036#define HL_ARMCP_INFO_TIMEOUT_USEC 10000000 /* 10s */
37#define HL_ARMCP_EEPROM_TIMEOUT_USEC 10000000 /* 10s */
38
Oded Gabbay99b9d7b2019-02-16 00:39:13 +020039#define HL_MAX_QUEUES 128
40
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020041#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 Shpigelman0feaf862019-02-16 00:39:22 +020046/* 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 Shpigelman66542c32019-02-24 09:17:55 +020054 * @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 Shpigelman0feaf862019-02-16 00:39:22 +020057 * @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 */
65struct pgt_info {
Omer Shpigelman66542c32019-02-24 09:17:55 +020066 struct hlist_node node;
67 u64 phys_addr;
68 u64 shadow_addr;
69 struct hl_ctx *ctx;
70 int num_of_ptes;
Omer Shpigelman0feaf862019-02-16 00:39:22 +020071};
72
Oded Gabbayc4d66342019-02-16 00:39:11 +020073struct hl_device;
Oded Gabbaybe5d9262019-02-16 00:39:15 +020074struct hl_fpriv;
Oded Gabbayc4d66342019-02-16 00:39:11 +020075
Oded Gabbay9494a8d2019-02-16 00:39:17 +020076/**
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 */
85enum 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 */
98struct hw_queue_properties {
99 enum hl_queue_type type;
100 u8 kmd_only;
101};
Oded Gabbayc4d66342019-02-16 00:39:11 +0200102
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200103/**
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200104 * enum vm_type_t - virtual memory mapping request information.
105 * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200106 * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200107 */
108enum vm_type_t {
109 VM_TYPE_USERPTR,
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200110 VM_TYPE_PHYS_PACK
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200111};
112
113/**
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200114 * 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 */
120enum hl_device_hw_state {
121 HL_DEVICE_HW_STATE_CLEAN = 0,
122 HL_DEVICE_HW_STATE_DIRTY
123};
124
125/**
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200126 * struct asic_fixed_properties - ASIC specific immutable properties.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200127 * @hw_queues_props: H/W queues properties.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200128 * @armcp_info: received various information from ArmCP regarding the H/W. e.g.
129 * available sensors.
Oded Gabbay839c4802019-02-16 00:39:16 +0200130 * @uboot_ver: F/W U-boot version.
131 * @preboot_ver: F/W Preboot version.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200132 * @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 Gabbayd91389b2019-02-16 00:39:19 +0200142 * @max_power_default: max power of the device after reset
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200143 * @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 Shpigelman27ca384c2019-02-28 10:46:11 +0200151 * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
152 * fault.
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200153 * @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 Shpigelman0feaf862019-02-16 00:39:22 +0200155 * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
Omer Shpigelman27ca384c2019-02-28 10:46:11 +0200156 * @mmu_dram_default_page_addr: DRAM default page physical address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200157 * @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 Gabbay99b9d7b2019-02-16 00:39:13 +0200162 * @cfg_size: configuration space size on SRAM.
163 * @sram_size: total size of SRAM.
164 * @max_asid: maximum number of open contexts (ASIDs).
Oded Gabbay1251f232019-02-16 00:39:18 +0200165 * @num_of_events: number of possible internal H/W IRQs.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200166 * @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 Gabbay99b9d7b2019-02-16 00:39:13 +0200170 * @completion_queues_count: number of completion queues.
171 * @high_pll: high PLL frequency used by the device.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200172 * @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 Gabbay99b9d7b2019-02-16 00:39:13 +0200174 * @tpc_enabled_mask: which TPCs are enabled.
175 */
176struct asic_fixed_properties {
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200177 struct hw_queue_properties hw_queues_props[HL_MAX_QUEUES];
Oded Gabbayd91389b2019-02-16 00:39:19 +0200178 struct armcp_info armcp_info;
Oded Gabbay839c4802019-02-16 00:39:16 +0200179 char uboot_ver[VERSION_MAX_LEN];
180 char preboot_ver[VERSION_MAX_LEN];
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200181 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 Gabbayd91389b2019-02-16 00:39:19 +0200190 u64 max_power_default;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200191 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 Shpigelman27ca384c2019-02-28 10:46:11 +0200195 u64 dram_size_for_default_page_mapping;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200196 u64 pcie_dbi_base_address;
197 u64 pcie_aux_dbi_reg_addr;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200198 u64 mmu_pgt_addr;
Omer Shpigelman27ca384c2019-02-28 10:46:11 +0200199 u64 mmu_dram_default_page_addr;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200200 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 Gabbay99b9d7b2019-02-16 00:39:13 +0200205 u32 cfg_size;
206 u32 sram_size;
207 u32 max_asid;
Oded Gabbay1251f232019-02-16 00:39:18 +0200208 u32 num_of_events;
Oded Gabbayd91389b2019-02-16 00:39:19 +0200209 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 Gabbay99b9d7b2019-02-16 00:39:13 +0200213 u32 high_pll;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200214 u32 cb_pool_cb_cnt;
215 u32 cb_pool_cb_size;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200216 u8 completion_queues_count;
217 u8 tpc_enabled_mask;
218};
219
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200220/**
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 */
227struct hl_dma_fence {
228 struct dma_fence base_fence;
229 spinlock_t lock;
230 struct hl_device *hdev;
231 u64 cs_seq;
232};
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200233
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200234/*
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 */
245struct 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 Gabbayc2164772019-02-16 00:39:24 +0200255 * @debugfs_list: node in debugfs list of command buffers.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200256 * @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 Gabbayeff6f4a2019-02-16 00:39:21 +0200262 * @cs_cnt: holds number of CS that this CB participates in.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200263 * @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 */
267struct hl_cb {
268 struct kref refcount;
269 struct hl_device *hdev;
270 spinlock_t lock;
Oded Gabbayc2164772019-02-16 00:39:24 +0200271 struct list_head debugfs_list;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200272 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 Gabbayeff6f4a2019-02-16 00:39:21 +0200278 u32 cs_cnt;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200279 u32 ctx_id;
280 u8 mmap;
281 u8 is_pool;
282};
283
284
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200285/*
286 * QUEUES
287 */
288
289struct 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 Gabbay99b9d7b2019-02-16 00:39:13 +0200312#define HL_QUEUE_LENGTH 256
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200313#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 Gabbay1251f232019-02-16 00:39:18 +0200322/* 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 Gabbay9494a8d2019-02-16 00:39:17 +0200325
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 */
340struct 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 */
362struct 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 Gabbay0861e412019-02-16 00:39:14 +0200371
Oded Gabbay1251f232019-02-16 00:39:18 +0200372/**
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 */
379struct hl_eq {
380 struct hl_device *hdev;
381 u64 kernel_address;
382 dma_addr_t bus_address;
383 u32 ci;
384};
385
Oded Gabbay0861e412019-02-16 00:39:14 +0200386
Oded Gabbayc4d66342019-02-16 00:39:11 +0200387/*
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 */
397enum hl_asic_type {
398 ASIC_AUTO_DETECT,
399 ASIC_GOYA,
400 ASIC_INVALID
401};
402
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200403struct hl_cs_parser;
404
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200405/**
Oded Gabbayd91389b2019-02-16 00:39:19 +0200406 * 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 */
411enum 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 */
423enum hl_pll_frequency {
424 PLL_HIGH = 1,
425 PLL_LOW,
426 PLL_LAST
427};
428
429/**
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200430 * 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 Gabbayd91389b2019-02-16 00:39:19 +0200434 * @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 Gabbay99b9d7b2019-02-16 00:39:13 +0200436 * @sw_init: sets up driver state, does not configure H/W.
437 * @sw_fini: tears down driver state, does not configure H/W.
Oded Gabbay839c4802019-02-16 00:39:16 +0200438 * @hw_init: sets up the H/W state.
439 * @hw_fini: tears down the H/W state.
Oded Gabbay1251f232019-02-16 00:39:18 +0200440 * @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 Gabbay99b9d7b2019-02-16 00:39:13 +0200443 * @suspend: handles IP specific H/W or SW changes for suspend.
444 * @resume: handles IP specific H/W or SW changes for resume.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200445 * @cb_mmap: maps a CB.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200446 * @ring_doorbell: increment PI on a given QMAN.
447 * @flush_pq_write: flush PQ entry write if necessary, WARN if flushing failed.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200448 * @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 Gabbay9494a8d2019-02-16 00:39:17 +0200456 * @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 Gabbayeff6f4a2019-02-16 00:39:21 +0200463 * @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 Gabbay1251f232019-02-16 00:39:18 +0200468 * @update_eq_ci: update event queue CI.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200469 * @context_switch: called upon ASID context switch.
470 * @restore_phase_topology: clear all SOBs amd MONs.
Oded Gabbayc2164772019-02-16 00:39:24 +0200471 * @debugfs_read32: debug interface for reading u32 from DRAM/SRAM.
472 * @debugfs_write32: debug interface for writing u32 to DRAM/SRAM.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200473 * @add_device_attr: add ASIC specific device attributes.
Oded Gabbay1251f232019-02-16 00:39:18 +0200474 * @handle_eqe: handle event queue entry (IRQ) from ArmCP.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200475 * @set_pll_profile: change PLL profile (manual/automatic).
Oded Gabbay1251f232019-02-16 00:39:18 +0200476 * @get_events_stat: retrieve event queue entries histogram.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200477 * @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 Gabbayf8c8c7d52019-02-16 00:39:20 +0200483 * @send_heartbeat: send is-alive packet to ArmCP and verify response.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200484 * @enable_clock_gating: enable clock gating for reducing power consumption.
485 * @disable_clock_gating: disable clock for accessing registers on HBW.
Omer Shpigelman315bc052019-04-01 22:31:22 +0300486 * @debug_coresight: perform certain actions on Coresight for debugging.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200487 * @is_device_idle: return true if device is idle, false otherwise.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200488 * @soft_reset_late_init: perform certain actions needed after soft reset.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200489 * @hw_queues_lock: acquire H/W queues lock.
490 * @hw_queues_unlock: release H/W queues lock.
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200491 * @get_pci_id: retrieve PCI ID.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200492 * @get_eeprom_data: retrieve EEPROM data from F/W.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200493 * @send_cpu_message: send buffer to ArmCP.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200494 * @get_hw_state: retrieve the H/W state
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200495 * @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 Gabbay99b9d7b2019-02-16 00:39:13 +0200498 */
499struct hl_asic_funcs {
500 int (*early_init)(struct hl_device *hdev);
501 int (*early_fini)(struct hl_device *hdev);
Oded Gabbayd91389b2019-02-16 00:39:19 +0200502 int (*late_init)(struct hl_device *hdev);
503 void (*late_fini)(struct hl_device *hdev);
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200504 int (*sw_init)(struct hl_device *hdev);
505 int (*sw_fini)(struct hl_device *hdev);
Oded Gabbay839c4802019-02-16 00:39:16 +0200506 int (*hw_init)(struct hl_device *hdev);
507 void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
Oded Gabbay1251f232019-02-16 00:39:18 +0200508 void (*halt_engines)(struct hl_device *hdev, bool hard_reset);
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200509 int (*suspend)(struct hl_device *hdev);
510 int (*resume)(struct hl_device *hdev);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200511 int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
512 u64 kaddress, phys_addr_t paddress, u32 size);
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200513 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 Gabbay99b9d7b2019-02-16 00:39:13 +0200515 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 Gabbay9494a8d2019-02-16 00:39:17 +0200519 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 Gabbayeff6f4a2019-02-16 00:39:21 +0200530 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 Gabbay1251f232019-02-16 00:39:18 +0200541 void (*update_eq_ci)(struct hl_device *hdev, u32 val);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200542 int (*context_switch)(struct hl_device *hdev, u32 asid);
543 void (*restore_phase_topology)(struct hl_device *hdev);
Oded Gabbayc2164772019-02-16 00:39:24 +0200544 int (*debugfs_read32)(struct hl_device *hdev, u64 addr, u32 *val);
545 int (*debugfs_write32)(struct hl_device *hdev, u64 addr, u32 val);
Oded Gabbayd91389b2019-02-16 00:39:19 +0200546 void (*add_device_attr)(struct hl_device *hdev,
547 struct attribute_group *dev_attr_grp);
Oded Gabbay1251f232019-02-16 00:39:18 +0200548 void (*handle_eqe)(struct hl_device *hdev,
549 struct hl_eq_entry *eq_entry);
Oded Gabbayd91389b2019-02-16 00:39:19 +0200550 void (*set_pll_profile)(struct hl_device *hdev,
551 enum hl_pll_frequency freq);
Oded Gabbay1251f232019-02-16 00:39:18 +0200552 void* (*get_events_stat)(struct hl_device *hdev, u32 *size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200553 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 Gabbayf8c8c7d52019-02-16 00:39:20 +0200558 int (*send_heartbeat)(struct hl_device *hdev);
Oded Gabbayd91389b2019-02-16 00:39:19 +0200559 void (*enable_clock_gating)(struct hl_device *hdev);
560 void (*disable_clock_gating)(struct hl_device *hdev);
Omer Shpigelman315bc052019-04-01 22:31:22 +0300561 int (*debug_coresight)(struct hl_device *hdev, void *data);
Tomer Tayarc811f7b2019-03-07 14:26:02 +0200562 bool (*is_device_idle)(struct hl_device *hdev, char *buf, size_t size);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200563 int (*soft_reset_late_init)(struct hl_device *hdev);
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200564 void (*hw_queues_lock)(struct hl_device *hdev);
565 void (*hw_queues_unlock)(struct hl_device *hdev);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200566 u32 (*get_pci_id)(struct hl_device *hdev);
Oded Gabbayd91389b2019-02-16 00:39:19 +0200567 int (*get_eeprom_data)(struct hl_device *hdev, void *data,
568 size_t max_size);
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200569 int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
570 u16 len, u32 timeout, long *result);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200571 enum hl_device_hw_state (*get_hw_state)(struct hl_device *hdev);
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200572 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 Gabbay99b9d7b2019-02-16 00:39:13 +0200575};
Oded Gabbayc4d66342019-02-16 00:39:11 +0200576
Oded Gabbay0861e412019-02-16 00:39:14 +0200577
578/*
579 * CONTEXTS
580 */
581
582#define HL_KERNEL_ASID_ID 0
583
584/**
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200585 * 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 */
591struct hl_va_range {
592 struct mutex lock;
593 struct list_head list;
594 u64 start_addr;
595 u64 end_addr;
596};
597
598/**
Oded Gabbay0861e412019-02-16 00:39:14 +0200599 * struct hl_ctx - user/kernel context.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200600 * @mem_hash: holds mapping from virtual address to virtual memory area
601 * descriptor (hl_vm_phys_pg_list or hl_userptr).
Omer Shpigelman66542c32019-02-24 09:17:55 +0200602 * @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 Gabbay0861e412019-02-16 00:39:14 +0200604 * @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 Gabbayeff6f4a2019-02-16 00:39:21 +0200608 * @cs_pending: array of DMA fence objects representing pending CS.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200609 * @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 Gabbayc2164772019-02-16 00:39:24 +0200614 * @debugfs_list: node in debugfs list of contexts.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200615 * @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 Shpigelman27ca384c2019-02-28 10:46:11 +0200618 * @dram_default_hops: array that holds all hops addresses needed for default
619 * DRAM mapping.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200620 * @cs_lock: spinlock to protect cs_sequence.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200621 * @dram_phys_mem: amount of used physical DRAM memory by this context.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200622 * @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 Gabbay0861e412019-02-16 00:39:14 +0200628 * @asid: context's unique address space ID in the device's MMU.
629 */
630struct hl_ctx {
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200631 DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
Omer Shpigelman66542c32019-02-24 09:17:55 +0200632 DECLARE_HASHTABLE(mmu_phys_hash, MMU_HASH_TABLE_BITS);
633 DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
Oded Gabbay0861e412019-02-16 00:39:14 +0200634 struct hl_fpriv *hpriv;
635 struct hl_device *hdev;
636 struct kref refcount;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200637 struct dma_fence *cs_pending[HL_MAX_PENDING_CS];
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200638 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 Gabbayc2164772019-02-16 00:39:24 +0200642 struct list_head debugfs_list;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200643 u64 cs_sequence;
Omer Shpigelman27ca384c2019-02-28 10:46:11 +0200644 u64 *dram_default_hops;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200645 spinlock_t cs_lock;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200646 atomic64_t dram_phys_mem;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200647 atomic_t thread_restore_token;
648 u32 thread_restore_wait_token;
Oded Gabbay0861e412019-02-16 00:39:14 +0200649 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 */
657struct hl_ctx_mgr {
658 struct mutex ctx_lock;
659 struct idr ctx_handles;
660};
661
662
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200663
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 */
680struct 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 Gabbayc2164772019-02-16 00:39:24 +0200702 * @debugfs_list: node in debugfs list of command submissions.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200703 * @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 */
711struct 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 Gabbayc2164772019-02-16 00:39:24 +0200720 struct list_head debugfs_list;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200721 u64 sequence;
722 u8 submitted;
723 u8 completed;
724 u8 timedout;
725 u8 tdr_active;
726 u8 aborted;
727};
728
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200729/**
730 * struct hl_cs_job - command submission job.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200731 * @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 Gabbay9494a8d2019-02-16 00:39:17 +0200736 * @finish_work: workqueue object to run when job is completed.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200737 * @userptr_list: linked-list of userptr mappings that belong to this job and
738 * wait for completion.
Oded Gabbayc2164772019-02-16 00:39:24 +0200739 * @debugfs_list: node in debugfs list of command submission jobs.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200740 * @id: the id of this job inside a CS.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200741 * @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 Gabbay9494a8d2019-02-16 00:39:17 +0200745 */
746struct hl_cs_job {
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200747 struct list_head cs_node;
748 struct hl_cs *cs;
749 struct hl_cb *user_cb;
750 struct hl_cb *patched_cb;
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200751 struct work_struct finish_work;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200752 struct list_head userptr_list;
Oded Gabbayc2164772019-02-16 00:39:24 +0200753 struct list_head debugfs_list;
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200754 u32 id;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200755 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 */
778struct 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 Gabbay9494a8d2019-02-16 00:39:17 +0200790};
Oded Gabbayd91389b2019-02-16 00:39:19 +0200791
792
Oded Gabbayc4d66342019-02-16 00:39:11 +0200793/*
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200794 * 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 */
805struct 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 Shpigelmanbfb1ce12019-03-05 10:59:16 +0200815 * @npages: num physical pages in the pack.
816 * @total_size: total size of all the pages in this list.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200817 * @mapping_cnt: number of shared mappings.
818 * @asid: the context related to this list.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200819 * @page_size: size of each page in the pack.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200820 * @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 */
826struct hl_vm_phys_pg_pack {
827 enum vm_type_t vm_type; /* must be first */
828 u64 *pages;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +0200829 u64 npages;
830 u64 total_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200831 atomic_t mapping_cnt;
832 u32 asid;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200833 u32 page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200834 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 */
848struct 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 */
864struct 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 Shpigelman315bc052019-04-01 22:31:22 +0300872
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 */
886struct 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 Shpigelman0feaf862019-02-16 00:39:22 +0200895/*
Oded Gabbayc4d66342019-02-16 00:39:11 +0200896 * 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 Gabbay0861e412019-02-16 00:39:14 +0200904 * @ctx: current executing context.
905 * @ctx_mgr: context manager to handle multiple context for this FD.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200906 * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
Oded Gabbayc2164772019-02-16 00:39:24 +0200907 * @debugfs_list: list of relevant ASIC debugfs.
Oded Gabbayc4d66342019-02-16 00:39:11 +0200908 * @refcount: number of related contexts.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200909 * @restore_phase_mutex: lock for context switch and restore phase.
Oded Gabbayc4d66342019-02-16 00:39:11 +0200910 */
911struct hl_fpriv {
912 struct hl_device *hdev;
913 struct file *filp;
914 struct pid *taskpid;
Oded Gabbay0861e412019-02-16 00:39:14 +0200915 struct hl_ctx *ctx; /* TODO: remove for multiple ctx */
916 struct hl_ctx_mgr ctx_mgr;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200917 struct hl_cb_mgr cb_mgr;
Oded Gabbayc2164772019-02-16 00:39:24 +0200918 struct list_head debugfs_list;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200919 struct kref refcount;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200920 struct mutex restore_phase_mutex;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200921};
922
923
924/*
Oded Gabbayc2164772019-02-16 00:39:24 +0200925 * 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 */
934struct 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 */
947struct 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 */
977struct 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 Gabbayc4d66342019-02-16 00:39:11 +02001003 * 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 Gabbay99b9d7b2019-02-16 00:39:13 +02001011/*
1012 * Registers read & write functions.
1013 */
1014
1015u32 hl_rreg(struct hl_device *hdev, u32 reg);
1016void 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 Tayarc811f7b2019-03-07 14:26:02 +02001042#define HL_ENG_BUSY(buf, size, fmt, ...) ({ \
1043 if (buf) \
1044 snprintf(buf, size, fmt, ##__VA_ARGS__); \
1045 false; \
1046 })
1047
Oded Gabbayd91389b2019-02-16 00:39:19 +02001048struct hwmon_chip_info;
1049
Oded Gabbayc4d66342019-02-16 00:39:11 +02001050/**
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001051 * struct hl_device_reset_work - reset workqueue task wrapper.
1052 * @reset_work: reset work to be done.
1053 * @hdev: habanalabs device structure.
1054 */
1055struct hl_device_reset_work {
1056 struct work_struct reset_work;
1057 struct hl_device *hdev;
1058};
1059
1060/**
Oded Gabbayc4d66342019-02-16 00:39:11 +02001061 * struct hl_device - habanalabs device structure.
1062 * @pdev: pointer to PCI device, can be NULL in case of simulator device.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001063 * @pcie_bar: array of available PCIe bars.
1064 * @rmmio: configuration area address on SRAM.
Oded Gabbayc4d66342019-02-16 00:39:11 +02001065 * @cdev: related char device.
1066 * @dev: realted kernel basic device structure.
Oded Gabbayd91389b2019-02-16 00:39:19 +02001067 * @work_freq: delayed work to lower device frequency if possible.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001068 * @work_heartbeat: delayed work for ArmCP is-alive check.
Oded Gabbayc4d66342019-02-16 00:39:11 +02001069 * @asic_name: ASIC specific nmae.
1070 * @asic_type: ASIC specific type.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001071 * @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 Gabbay0861e412019-02-16 00:39:14 +02001074 * @kernel_ctx: KMD context structure.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001075 * @kernel_queues: array of hl_hw_queue.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001076 * @hw_queues_mirror_list: CS mirror list for TDR.
1077 * @hw_queues_mirror_lock: protects hw_queues_mirror_list.
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001078 * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs.
Oded Gabbay1251f232019-02-16 00:39:18 +02001079 * @event_queue: event queue for IRQ from ArmCP.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001080 * @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 Gabbay0861e412019-02-16 00:39:14 +02001084 * @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 Gabbay9494a8d2019-02-16 00:39:17 +02001092 * @send_cpu_message_lock: enforces only one message in KMD <-> ArmCP queue.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001093 * @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 Shpigelman0feaf862019-02-16 00:39:22 +02001096 * @mmu_pgt_pool: pool of available MMU hops.
1097 * @vm: virtual memory manager for MMU.
Omer Shpigelman66542c32019-02-24 09:17:55 +02001098 * @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 Gabbayd91389b2019-02-16 00:39:19 +02001100 * @hwmon_dev: H/W monitor device.
1101 * @pm_mng_profile: current power management profile.
1102 * @hl_chip_info: ASIC's sensors information.
Oded Gabbayc2164772019-02-16 00:39:24 +02001103 * @hl_debugfs: device's debugfs manager.
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001104 * @cb_pool: list of preallocated CBs.
1105 * @cb_pool_lock: protects the CB pool.
Oded Gabbay0861e412019-02-16 00:39:14 +02001106 * @user_ctx: current user context executing.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001107 * @dram_used_mem: current DRAM memory consumption.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001108 * @timeout_jiffies: device CS timeout value.
Oded Gabbayd91389b2019-02-16 00:39:19 +02001109 * @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 Gabbaycbaa99e2019-03-03 15:13:15 +02001112 * @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 Gabbayc4d66342019-02-16 00:39:11 +02001117 * @major: habanalabs KMD major.
Oded Gabbayd91389b2019-02-16 00:39:19 +02001118 * @high_pll: high PLL profile frequency.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001119 * @soft_reset_cnt: number of soft reset since KMD loading.
1120 * @hard_reset_cnt: number of hard reset since KMD loading.
Oded Gabbayc4d66342019-02-16 00:39:11 +02001121 * @id: device minor.
1122 * @disabled: is device disabled.
Oded Gabbayd91389b2019-02-16 00:39:19 +02001123 * @late_init_done: is late init stage was done during initialization.
1124 * @hwmon_initialized: is H/W monitor sensors was initialized.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001125 * @hard_reset_pending: is there a hard reset work pending.
1126 * @heartbeat: is heartbeat sanity check towards ArmCP enabled.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001127 * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
1128 * otherwise.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001129 * @dram_supports_virtual_memory: is MMU enabled towards DRAM.
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001130 * @dram_default_page_mapping: is DRAM default page mapping enabled.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001131 * @init_done: is the initialization of the device done.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001132 * @mmu_enable: is MMU enabled.
Oded Gabbaya28ce422019-02-28 10:46:12 +02001133 * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
Oded Gabbayd9973872019-03-07 18:03:23 +02001134 * @dma_mask: the dma mask that was set for this device
Oded Gabbayc4d66342019-02-16 00:39:11 +02001135 */
1136struct hl_device {
1137 struct pci_dev *pdev;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001138 void __iomem *pcie_bar[6];
1139 void __iomem *rmmio;
Oded Gabbayc4d66342019-02-16 00:39:11 +02001140 struct cdev cdev;
1141 struct device *dev;
Oded Gabbayd91389b2019-02-16 00:39:19 +02001142 struct delayed_work work_freq;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001143 struct delayed_work work_heartbeat;
Oded Gabbayc4d66342019-02-16 00:39:11 +02001144 char asic_name[16];
1145 enum hl_asic_type asic_type;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001146 struct hl_cq *completion_queue;
1147 struct workqueue_struct *cq_wq;
Oded Gabbay1251f232019-02-16 00:39:18 +02001148 struct workqueue_struct *eq_wq;
Oded Gabbay0861e412019-02-16 00:39:14 +02001149 struct hl_ctx *kernel_ctx;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001150 struct hl_hw_queue *kernel_queues;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001151 struct list_head hw_queues_mirror_list;
1152 spinlock_t hw_queues_mirror_lock;
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001153 struct hl_cb_mgr kernel_cb_mgr;
Oded Gabbay1251f232019-02-16 00:39:18 +02001154 struct hl_eq event_queue;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001155 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 Gabbay0861e412019-02-16 00:39:14 +02001159 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 Gabbay9494a8d2019-02-16 00:39:17 +02001163 struct mutex send_cpu_message_lock;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001164 struct asic_fixed_properties asic_prop;
1165 const struct hl_asic_funcs *asic_funcs;
1166 void *asic_specific;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001167 struct gen_pool *mmu_pgt_pool;
1168 struct hl_vm vm;
1169 struct mutex mmu_cache_lock;
Omer Shpigelman66542c32019-02-24 09:17:55 +02001170 void *mmu_shadow_hop0;
Oded Gabbayd91389b2019-02-16 00:39:19 +02001171 struct device *hwmon_dev;
1172 enum hl_pm_mng_profile pm_mng_profile;
1173 struct hwmon_chip_info *hl_chip_info;
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001174
Oded Gabbayc2164772019-02-16 00:39:24 +02001175 struct hl_dbg_device_entry hl_debugfs;
1176
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001177 struct list_head cb_pool;
1178 spinlock_t cb_pool_lock;
1179
Oded Gabbay0861e412019-02-16 00:39:14 +02001180 /* TODO: remove user_ctx for multiple process support */
1181 struct hl_ctx *user_ctx;
Oded Gabbayd91389b2019-02-16 00:39:19 +02001182
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001183 atomic64_t dram_used_mem;
Oded Gabbaycbaa99e2019-03-03 15:13:15 +02001184 u64 timeout_jiffies;
1185 u64 max_power;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001186 atomic_t in_reset;
Oded Gabbayd91389b2019-02-16 00:39:19 +02001187 atomic_t curr_pll_profile;
Oded Gabbay0861e412019-02-16 00:39:14 +02001188 atomic_t fd_open_cnt;
Oded Gabbaycbaa99e2019-03-03 15:13:15 +02001189 atomic_t cs_active_cnt;
Oded Gabbayc4d66342019-02-16 00:39:11 +02001190 u32 major;
Oded Gabbayd91389b2019-02-16 00:39:19 +02001191 u32 high_pll;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001192 u32 soft_reset_cnt;
1193 u32 hard_reset_cnt;
Oded Gabbayc4d66342019-02-16 00:39:11 +02001194 u16 id;
1195 u8 disabled;
Oded Gabbayd91389b2019-02-16 00:39:19 +02001196 u8 late_init_done;
1197 u8 hwmon_initialized;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001198 u8 hard_reset_pending;
1199 u8 heartbeat;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001200 u8 reset_on_lockup;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001201 u8 dram_supports_virtual_memory;
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001202 u8 dram_default_page_mapping;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001203 u8 init_done;
Oded Gabbaya28ce422019-02-28 10:46:12 +02001204 u8 device_cpu_disabled;
Oded Gabbayd9973872019-03-07 18:03:23 +02001205 u8 dma_mask;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001206
1207 /* Parameters for bring-up */
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001208 u8 mmu_enable;
Oded Gabbay839c4802019-02-16 00:39:16 +02001209 u8 cpu_enable;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001210 u8 reset_pcilink;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001211 u8 cpu_queues_enable;
Oded Gabbay839c4802019-02-16 00:39:16 +02001212 u8 fw_loading;
1213 u8 pldm;
Oded Gabbayc4d66342019-02-16 00:39:11 +02001214};
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 */
1229typedef 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 */
1236struct 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 Gabbayeff6f4a2019-02-16 00:39:21 +02001246/**
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 */
1255static 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 */
1278static 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 Gabbayc4d66342019-02-16 00:39:11 +02001298int hl_device_open(struct inode *inode, struct file *filp);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001299bool hl_device_disabled_or_in_reset(struct hl_device *hdev);
Dalit Ben Zooraa957082019-03-24 10:15:44 +02001300enum hl_device_status hl_device_status(struct hl_device *hdev);
Oded Gabbayc4d66342019-02-16 00:39:11 +02001301int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
1302 enum hl_asic_type asic_type, int minor);
1303void destroy_hdev(struct hl_device *hdev);
1304int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr, u32 timeout_us,
1305 u32 *val);
1306int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
1307 u32 timeout_us, u32 *val);
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001308int hl_hw_queues_create(struct hl_device *hdev);
1309void hl_hw_queues_destroy(struct hl_device *hdev);
1310int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
1311 u32 cb_size, u64 cb_ptr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001312int hl_hw_queue_schedule_cs(struct hl_cs *cs);
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001313u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
1314void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001315void hl_int_hw_queue_update_ci(struct hl_cs *cs);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001316void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
Oded Gabbayc4d66342019-02-16 00:39:11 +02001317
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001318#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
1321int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
1322void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
Oded Gabbay1251f232019-02-16 00:39:18 +02001323int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
1324void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001325void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
1326void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
Oded Gabbay1251f232019-02-16 00:39:18 +02001327irqreturn_t hl_irq_handler_cq(int irq, void *arg);
1328irqreturn_t hl_irq_handler_eq(int irq, void *arg);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001329u32 hl_cq_inc_ptr(u32 ptr);
1330
Oded Gabbay0861e412019-02-16 00:39:14 +02001331int hl_asid_init(struct hl_device *hdev);
1332void hl_asid_fini(struct hl_device *hdev);
1333unsigned long hl_asid_alloc(struct hl_device *hdev);
1334void hl_asid_free(struct hl_device *hdev, unsigned long asid);
1335
1336int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
1337void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
1338int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001339void hl_ctx_do_release(struct kref *ref);
1340void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx);
Oded Gabbay0861e412019-02-16 00:39:14 +02001341int hl_ctx_put(struct hl_ctx *ctx);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001342struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
Oded Gabbay0861e412019-02-16 00:39:14 +02001343void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
1344void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001345
Oded Gabbayc4d66342019-02-16 00:39:11 +02001346int hl_device_init(struct hl_device *hdev, struct class *hclass);
1347void hl_device_fini(struct hl_device *hdev);
1348int hl_device_suspend(struct hl_device *hdev);
1349int hl_device_resume(struct hl_device *hdev);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001350int hl_device_reset(struct hl_device *hdev, bool hard_reset,
1351 bool from_hard_reset_thread);
Oded Gabbay0861e412019-02-16 00:39:14 +02001352void hl_hpriv_get(struct hl_fpriv *hpriv);
1353void hl_hpriv_put(struct hl_fpriv *hpriv);
Oded Gabbayd91389b2019-02-16 00:39:19 +02001354int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001355
Oded Gabbayd91389b2019-02-16 00:39:19 +02001356int hl_build_hwmon_channel_info(struct hl_device *hdev,
1357 struct armcp_sensor *sensors_arr);
1358
1359int hl_sysfs_init(struct hl_device *hdev);
1360void hl_sysfs_fini(struct hl_device *hdev);
1361
1362int hl_hwmon_init(struct hl_device *hdev);
1363void hl_hwmon_fini(struct hl_device *hdev);
Oded Gabbayc4d66342019-02-16 00:39:11 +02001364
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001365int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr, u32 cb_size,
1366 u64 *handle, int ctx_id);
1367int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
1368int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
1369struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
1370 u32 handle);
1371void hl_cb_put(struct hl_cb *cb);
1372void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
1373void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
1374struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size);
1375int hl_cb_pool_init(struct hl_device *hdev);
1376int hl_cb_pool_fini(struct hl_device *hdev);
1377
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001378void hl_cs_rollback_all(struct hl_device *hdev);
1379struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev, bool ext_queue);
1380
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001381void goya_set_asic_funcs(struct hl_device *hdev);
1382
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001383int hl_vm_ctx_init(struct hl_ctx *ctx);
1384void hl_vm_ctx_fini(struct hl_ctx *ctx);
1385
1386int hl_vm_init(struct hl_device *hdev);
1387void hl_vm_fini(struct hl_device *hdev);
1388
Oded Gabbay230afe72019-02-27 00:19:18 +02001389int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001390 struct hl_userptr *userptr);
1391int hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
1392void hl_userptr_delete_list(struct hl_device *hdev,
1393 struct list_head *userptr_list);
1394bool 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 Shpigelman0feaf862019-02-16 00:39:22 +02001398int hl_mmu_init(struct hl_device *hdev);
1399void hl_mmu_fini(struct hl_device *hdev);
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001400int hl_mmu_ctx_init(struct hl_ctx *ctx);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001401void hl_mmu_ctx_fini(struct hl_ctx *ctx);
1402int hl_mmu_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size);
1403int hl_mmu_unmap(struct hl_ctx *ctx, u64 virt_addr, u32 page_size);
1404void hl_mmu_swap_out(struct hl_ctx *ctx);
1405void hl_mmu_swap_in(struct hl_ctx *ctx);
1406
Tomer Tayar3110c602019-03-04 10:22:09 +02001407int hl_fw_push_fw_to_device(struct hl_device *hdev, const char *fw_name,
1408 void __iomem *dst);
1409int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode);
1410int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
1411 u16 len, u32 timeout, long *result);
1412int hl_fw_test_cpu_queue(struct hl_device *hdev);
1413void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
1414 dma_addr_t *dma_handle);
1415void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
1416 void *vaddr);
1417int hl_fw_send_heartbeat(struct hl_device *hdev);
1418int hl_fw_armcp_info_get(struct hl_device *hdev);
1419int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
1420
Tomer Tayarb6f897d2019-03-05 16:48:42 +02001421int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
1422 bool is_wc[3]);
1423int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
1424int hl_pci_set_dram_bar_base(struct hl_device *hdev, u8 inbound_region, u8 bar,
1425 u64 addr);
1426int hl_pci_init_iatu(struct hl_device *hdev, u64 sram_base_address,
1427 u64 dram_base_address, u64 host_phys_size);
Oded Gabbayd9973872019-03-07 18:03:23 +02001428int hl_pci_init(struct hl_device *hdev, u8 dma_mask);
Tomer Tayarb6f897d2019-03-05 16:48:42 +02001429void hl_pci_fini(struct hl_device *hdev);
Oded Gabbayd9973872019-03-07 18:03:23 +02001430int hl_pci_set_dma_mask(struct hl_device *hdev, u8 dma_mask);
Tomer Tayarb6f897d2019-03-05 16:48:42 +02001431
Oded Gabbayd91389b2019-02-16 00:39:19 +02001432long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr);
1433void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq);
1434long hl_get_temperature(struct hl_device *hdev, int sensor_index, u32 attr);
1435long hl_get_voltage(struct hl_device *hdev, int sensor_index, u32 attr);
1436long hl_get_current(struct hl_device *hdev, int sensor_index, u32 attr);
1437long hl_get_fan_speed(struct hl_device *hdev, int sensor_index, u32 attr);
1438long hl_get_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr);
1439void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
1440 long value);
1441u64 hl_get_max_power(struct hl_device *hdev);
1442void hl_set_max_power(struct hl_device *hdev, u64 value);
1443
Oded Gabbayc2164772019-02-16 00:39:24 +02001444#ifdef CONFIG_DEBUG_FS
1445
1446void hl_debugfs_init(void);
1447void hl_debugfs_fini(void);
1448void hl_debugfs_add_device(struct hl_device *hdev);
1449void hl_debugfs_remove_device(struct hl_device *hdev);
1450void hl_debugfs_add_file(struct hl_fpriv *hpriv);
1451void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
1452void hl_debugfs_add_cb(struct hl_cb *cb);
1453void hl_debugfs_remove_cb(struct hl_cb *cb);
1454void hl_debugfs_add_cs(struct hl_cs *cs);
1455void hl_debugfs_remove_cs(struct hl_cs *cs);
1456void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
1457void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
1458void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
1459void hl_debugfs_remove_userptr(struct hl_device *hdev,
1460 struct hl_userptr *userptr);
1461void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
1462void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
1463
1464#else
1465
1466static inline void __init hl_debugfs_init(void)
1467{
1468}
1469
1470static inline void hl_debugfs_fini(void)
1471{
1472}
1473
1474static inline void hl_debugfs_add_device(struct hl_device *hdev)
1475{
1476}
1477
1478static inline void hl_debugfs_remove_device(struct hl_device *hdev)
1479{
1480}
1481
1482static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1483{
1484}
1485
1486static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1487{
1488}
1489
1490static inline void hl_debugfs_add_cb(struct hl_cb *cb)
1491{
1492}
1493
1494static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
1495{
1496}
1497
1498static inline void hl_debugfs_add_cs(struct hl_cs *cs)
1499{
1500}
1501
1502static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
1503{
1504}
1505
1506static inline void hl_debugfs_add_job(struct hl_device *hdev,
1507 struct hl_cs_job *job)
1508{
1509}
1510
1511static inline void hl_debugfs_remove_job(struct hl_device *hdev,
1512 struct hl_cs_job *job)
1513{
1514}
1515
1516static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
1517 struct hl_userptr *userptr)
1518{
1519}
1520
1521static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
1522 struct hl_userptr *userptr)
1523{
1524}
1525
1526static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
1527 struct hl_ctx *ctx)
1528{
1529}
1530
1531static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
1532 struct hl_ctx *ctx)
1533{
1534}
1535
1536#endif
1537
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001538/* IOCTLs */
1539long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
1540int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001541int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
1542int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001543int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001544
Oded Gabbayc4d66342019-02-16 00:39:11 +02001545#endif /* HABANALABSP_H_ */