blob: a0f3c580b58b9e6c777eb8b461ab5527f31189e3 [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 Gabbay2f553422020-08-15 16:28:10 +030011#include "../include/common/cpucp_if.h"
Greg Kroah-Hartman7b16a152020-07-28 19:18:51 +020012#include "../include/common/qman_if.h"
Moti Haimovski00e1b592020-10-27 10:55:42 +020013#include "../include/hw_ip/mmu/mmu_general.h"
Ofir Bittondb491e42020-06-18 09:51:16 +030014#include <uapi/misc/habanalabs.h>
Oded Gabbay839c4802019-02-16 00:39:16 +020015
Oded Gabbayc4d66342019-02-16 00:39:11 +020016#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-direction.h>
20#include <linux/scatterlist.h>
Omer Shpigelman0feaf862019-02-16 00:39:22 +020021#include <linux/hashtable.h>
Oded Gabbay639781d2021-04-02 01:43:18 +030022#include <linux/debugfs.h>
Yuri Nudelman938b7932021-06-06 10:28:51 +030023#include <linux/rwsem.h>
Tomer Tayar56004702020-08-18 15:06:56 +030024#include <linux/bitfield.h>
Tomer Tayarba7e3892020-10-25 17:47:22 +020025#include <linux/genalloc.h>
26#include <linux/sched/signal.h>
27#include <linux/io-64-nonatomic-lo-hi.h>
28#include <linux/coresight.h>
Oded Gabbayc4d66342019-02-16 00:39:11 +020029
30#define HL_NAME "habanalabs"
31
Oded Gabbay3174ac92020-08-29 11:51:39 +030032/* Use upper bits of mmap offset to store habana driver specific information.
Ofir Bittond00697f2021-01-05 12:55:06 +020033 * bits[63:61] - Encode mmap type
Oded Gabbay3174ac92020-08-29 11:51:39 +030034 * bits[45:0] - mmap offset value
35 *
36 * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
37 * defines are w.r.t to PAGE_SIZE
38 */
Ofir Bittond00697f2021-01-05 12:55:06 +020039#define HL_MMAP_TYPE_SHIFT (61 - PAGE_SHIFT)
40#define HL_MMAP_TYPE_MASK (0x7ull << HL_MMAP_TYPE_SHIFT)
41#define HL_MMAP_TYPE_BLOCK (0x4ull << HL_MMAP_TYPE_SHIFT)
Oded Gabbay3174ac92020-08-29 11:51:39 +030042#define HL_MMAP_TYPE_CB (0x2ull << HL_MMAP_TYPE_SHIFT)
43
Ofir Bittond00697f2021-01-05 12:55:06 +020044#define HL_MMAP_OFFSET_VALUE_MASK (0x1FFFFFFFFFFFull >> PAGE_SHIFT)
Oded Gabbay3174ac92020-08-29 11:51:39 +030045#define HL_MMAP_OFFSET_VALUE_GET(off) (off & HL_MMAP_OFFSET_VALUE_MASK)
Oded Gabbaybe5d9262019-02-16 00:39:15 +020046
Ofir Bittonadb51292020-10-08 10:27:42 +030047#define HL_PENDING_RESET_PER_SEC 10
48#define HL_PENDING_RESET_MAX_TRIALS 60 /* 10 minutes */
49#define HL_PENDING_RESET_LONG_SEC 60
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +020050
Omer Shpigelmane09498b2020-05-09 12:18:01 +030051#define HL_HARD_RESET_MAX_TIMEOUT 120
Omer Shpigelman4efb6b22021-06-09 10:17:49 +030052#define HL_PLDM_HARD_RESET_MAX_TIMEOUT (HL_HARD_RESET_MAX_TIMEOUT * 3)
Omer Shpigelmane09498b2020-05-09 12:18:01 +030053
Oded Gabbay839c4802019-02-16 00:39:16 +020054#define HL_DEVICE_TIMEOUT_USEC 1000000 /* 1 s */
55
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +020056#define HL_HEARTBEAT_PER_USEC 5000000 /* 5 s */
57
Oded Gabbayd91389b2019-02-16 00:39:19 +020058#define HL_PLL_LOW_JOB_FREQ_USEC 5000000 /* 5 s */
59
Oded Gabbay2f553422020-08-15 16:28:10 +030060#define HL_CPUCP_INFO_TIMEOUT_USEC 10000000 /* 10s */
61#define HL_CPUCP_EEPROM_TIMEOUT_USEC 10000000 /* 10s */
Tomer Tayar3110c602019-03-04 10:22:09 +020062
Omer Shpigelmana1e537b2019-05-13 14:44:50 +030063#define HL_PCI_ELBI_TIMEOUT_MSEC 10 /* 10ms */
64
Oded Gabbayed0fc502019-07-18 11:14:37 +030065#define HL_SIM_MAX_TIMEOUT_US 10000000 /* 10s */
66
Ofir Bittonab5f5c32021-01-12 18:37:19 +020067#define HL_COMMON_USER_INTERRUPT_ID 0xFFF
68
Yuri Nudelman938b7932021-06-06 10:28:51 +030069#define HL_STATE_DUMP_HIST_LEN 5
70
71#define OBJ_NAMES_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
72#define SYNC_TO_ENGINE_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
73
Omer Shpigelman0feaf862019-02-16 00:39:22 +020074/* Memory */
75#define MEM_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
76
77/* MMU */
78#define MMU_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
79
Moti Haimovskiccf979e2020-10-05 17:59:29 +030080/**
81 * enum hl_mmu_page_table_locaion - mmu page table location
82 * @MMU_DR_PGT: page-table is located on device DRAM.
83 * @MMU_HR_PGT: page-table is located on host memory.
84 * @MMU_NUM_PGT_LOCATIONS: number of page-table locations currently supported.
85 */
86enum hl_mmu_page_table_location {
87 MMU_DR_PGT = 0, /* device-dram-resident MMU PGT */
88 MMU_HR_PGT, /* host resident MMU PGT */
89 MMU_NUM_PGT_LOCATIONS /* num of PGT locations */
90};
91
Ofir Bitton21e7a342020-05-14 18:25:47 +030092/*
93 * HL_RSVD_SOBS 'sync stream' reserved sync objects per QMAN stream
94 * HL_RSVD_MONS 'sync stream' reserved monitors per QMAN stream
95 */
Ofir Bitton5de406c2020-09-10 10:56:26 +030096#define HL_RSVD_SOBS 2
97#define HL_RSVD_MONS 1
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +030098
Ofir Bitton5fe1c172020-09-10 10:10:55 +030099/*
100 * HL_COLLECTIVE_RSVD_MSTR_MONS 'collective' reserved monitors per QMAN stream
101 */
102#define HL_COLLECTIVE_RSVD_MSTR_MONS 2
103
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300104#define HL_MAX_SOB_VAL (1 << 15)
105
Ofir Bittonc16d45f2020-06-02 12:28:27 +0300106#define IS_POWER_OF_2(n) (n != 0 && ((n & (n - 1)) == 0))
107#define IS_MAX_PENDING_CS_VALID(n) (IS_POWER_OF_2(n) && (n > 1))
108
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300109#define HL_PCI_NUM_BARS 6
110
Ofir Bitton843839b2020-07-19 11:08:09 +0300111#define HL_MAX_DCORES 4
112
Ohad Sharabie42a6400f2021-02-17 20:42:48 +0200113/*
114 * Reset Flags
115 *
116 * - HL_RESET_HARD
117 * If set do hard reset to all engines. If not set reset just
118 * compute/DMA engines.
119 *
120 * - HL_RESET_FROM_RESET_THREAD
121 * Set if the caller is the hard-reset thread
122 *
123 * - HL_RESET_HEARTBEAT
124 * Set if reset is due to heartbeat
Koby Elbaz3e0ca9f2021-05-04 20:10:47 +0300125 *
126 * - HL_RESET_TDR
127 * Set if reset is due to TDR
Ofir Bitton23bace62021-06-08 17:24:52 +0300128 *
129 * - HL_RESET_DEVICE_RELEASE
130 * Set if reset is due to device release
Ohad Sharabie42a6400f2021-02-17 20:42:48 +0200131 */
132#define HL_RESET_HARD (1 << 0)
133#define HL_RESET_FROM_RESET_THREAD (1 << 1)
134#define HL_RESET_HEARTBEAT (1 << 2)
Koby Elbaz3e0ca9f2021-05-04 20:10:47 +0300135#define HL_RESET_TDR (1 << 3)
Ofir Bitton23bace62021-06-08 17:24:52 +0300136#define HL_RESET_DEVICE_RELEASE (1 << 4)
Ohad Sharabie42a6400f2021-02-17 20:42:48 +0200137
Ofir Bitton2992c1d2020-09-10 09:40:35 +0300138#define HL_MAX_SOBS_PER_MONITOR 8
139
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200140/**
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300141 * struct hl_gen_wait_properties - properties for generating a wait CB
142 * @data: command buffer
143 * @q_idx: queue id is used to extract fence register address
Ofir Bitton2992c1d2020-09-10 09:40:35 +0300144 * @size: offset in command buffer
145 * @sob_base: SOB base to use in this wait CB
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300146 * @sob_val: SOB value to wait for
147 * @mon_id: monitor to use in this wait CB
Ofir Bitton2992c1d2020-09-10 09:40:35 +0300148 * @sob_mask: each bit represents a SOB offset from sob_base to be used
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300149 */
150struct hl_gen_wait_properties {
151 void *data;
152 u32 q_idx;
Ofir Bitton2992c1d2020-09-10 09:40:35 +0300153 u32 size;
154 u16 sob_base;
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300155 u16 sob_val;
156 u16 mon_id;
Ofir Bitton2992c1d2020-09-10 09:40:35 +0300157 u8 sob_mask;
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300158};
159
160/**
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200161 * struct pgt_info - MMU hop page info.
Omer Shpigelman66542c32019-02-24 09:17:55 +0200162 * @node: hash linked-list node for the pgts shadow hash of pgts.
163 * @phys_addr: physical address of the pgt.
164 * @shadow_addr: shadow hop in the host.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200165 * @ctx: pointer to the owner ctx.
166 * @num_of_ptes: indicates how many ptes are used in the pgt.
167 *
168 * The MMU page tables hierarchy is placed on the DRAM. When a new level (hop)
169 * is needed during mapping, a new page is allocated and this structure holds
170 * its essential information. During unmapping, if no valid PTEs remained in the
171 * page, it is freed with its pgt_info structure.
172 */
173struct pgt_info {
Omer Shpigelman66542c32019-02-24 09:17:55 +0200174 struct hlist_node node;
175 u64 phys_addr;
176 u64 shadow_addr;
177 struct hl_ctx *ctx;
178 int num_of_ptes;
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200179};
180
Oded Gabbayc4d66342019-02-16 00:39:11 +0200181struct hl_device;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200182struct hl_fpriv;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200183
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200184/**
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300185 * enum hl_pci_match_mode - pci match mode per region
186 * @PCI_ADDRESS_MATCH_MODE: address match mode
187 * @PCI_BAR_MATCH_MODE: bar match mode
188 */
189enum hl_pci_match_mode {
190 PCI_ADDRESS_MATCH_MODE,
191 PCI_BAR_MATCH_MODE
192};
193
194/**
Oded Gabbay7e1c07d2020-03-26 12:32:56 +0200195 * enum hl_fw_component - F/W components to read version through registers.
Ohad Sharabi50f036d2021-04-11 15:26:28 +0300196 * @FW_COMP_BOOT_FIT: boot fit.
Oded Gabbay7e1c07d2020-03-26 12:32:56 +0200197 * @FW_COMP_PREBOOT: preboot.
Ohad Sharabib31e59b2021-04-22 10:01:22 +0300198 * @FW_COMP_LINUX: linux.
Oded Gabbay7e1c07d2020-03-26 12:32:56 +0200199 */
200enum hl_fw_component {
Ohad Sharabi50f036d2021-04-11 15:26:28 +0300201 FW_COMP_BOOT_FIT,
Ohad Sharabib31e59b2021-04-22 10:01:22 +0300202 FW_COMP_PREBOOT,
203 FW_COMP_LINUX,
Oded Gabbay7e1c07d2020-03-26 12:32:56 +0200204};
205
206/**
Ofir Bitton6a2f5d72021-02-15 13:23:04 +0200207 * enum hl_fw_types - F/W types present in the system
Oded Gabbay596553d2020-10-01 13:46:37 +0300208 * @FW_TYPE_LINUX: Linux image for device CPU
209 * @FW_TYPE_BOOT_CPU: Boot image for device CPU
Ofir Bitton6a2f5d72021-02-15 13:23:04 +0200210 * @FW_TYPE_PREBOOT_CPU: Indicates pre-loaded CPUs are present in the system
211 * (preboot, ppboot etc...)
Oded Gabbay596553d2020-10-01 13:46:37 +0300212 * @FW_TYPE_ALL_TYPES: Mask for all types
213 */
214enum hl_fw_types {
215 FW_TYPE_LINUX = 0x1,
216 FW_TYPE_BOOT_CPU = 0x2,
Ofir Bitton6a2f5d72021-02-15 13:23:04 +0200217 FW_TYPE_PREBOOT_CPU = 0x4,
218 FW_TYPE_ALL_TYPES =
219 (FW_TYPE_LINUX | FW_TYPE_BOOT_CPU | FW_TYPE_PREBOOT_CPU)
Oded Gabbay596553d2020-10-01 13:46:37 +0300220};
221
222/**
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200223 * enum hl_queue_type - Supported QUEUE types.
224 * @QUEUE_TYPE_NA: queue is not available.
225 * @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the
226 * host.
227 * @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's
228 * memories and/or operates the compute engines.
229 * @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU.
Tomer Tayarcb596ae2019-10-03 15:22:36 +0000230 * @QUEUE_TYPE_HW: queue of DMA and compute engines jobs, for which completion
231 * notifications are sent by H/W.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200232 */
233enum hl_queue_type {
234 QUEUE_TYPE_NA,
235 QUEUE_TYPE_EXT,
236 QUEUE_TYPE_INT,
Tomer Tayarcb596ae2019-10-03 15:22:36 +0000237 QUEUE_TYPE_CPU,
238 QUEUE_TYPE_HW
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200239};
240
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300241enum hl_cs_type {
242 CS_TYPE_DEFAULT,
243 CS_TYPE_SIGNAL,
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300244 CS_TYPE_WAIT,
245 CS_TYPE_COLLECTIVE_WAIT
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300246};
247
248/*
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300249 * struct hl_inbound_pci_region - inbound region descriptor
250 * @mode: pci match mode for this region
251 * @addr: region target address
252 * @size: region size in bytes
253 * @offset_in_bar: offset within bar (address match mode)
254 * @bar: bar id
255 */
256struct hl_inbound_pci_region {
257 enum hl_pci_match_mode mode;
258 u64 addr;
259 u64 size;
260 u64 offset_in_bar;
261 u8 bar;
262};
263
264/*
265 * struct hl_outbound_pci_region - outbound region descriptor
266 * @addr: region target address
267 * @size: region size in bytes
268 */
269struct hl_outbound_pci_region {
270 u64 addr;
271 u64 size;
272};
273
274/*
Tal Cohen4bb1f2f2020-06-03 09:25:27 +0300275 * enum queue_cb_alloc_flags - Indicates queue support for CBs that
276 * allocated by Kernel or by User
277 * @CB_ALLOC_KERNEL: support only CBs that allocated by Kernel
278 * @CB_ALLOC_USER: support only CBs that allocated by User
279 */
280enum queue_cb_alloc_flags {
281 CB_ALLOC_KERNEL = 0x1,
282 CB_ALLOC_USER = 0x2
283};
284
285/*
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300286 * struct hl_hw_sob - H/W SOB info.
287 * @hdev: habanalabs device structure.
288 * @kref: refcount of this SOB. The SOB will reset once the refcount is zero.
289 * @sob_id: id of this SOB.
290 * @q_idx: the H/W queue that uses this SOB.
291 */
292struct hl_hw_sob {
293 struct hl_device *hdev;
294 struct kref kref;
295 u32 sob_id;
296 u32 q_idx;
297};
298
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300299enum hl_collective_mode {
300 HL_COLLECTIVE_NOT_SUPPORTED = 0x0,
301 HL_COLLECTIVE_MASTER = 0x1,
302 HL_COLLECTIVE_SLAVE = 0x2
303};
304
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200305/**
306 * struct hw_queue_properties - queue information.
307 * @type: queue type.
Tal Cohen4bb1f2f2020-06-03 09:25:27 +0300308 * @queue_cb_alloc_flags: bitmap which indicates if the hw queue supports CB
309 * that allocated by the Kernel driver and therefore,
310 * a CB handle can be provided for jobs on this queue.
311 * Otherwise, a CB address must be provided.
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300312 * @collective_mode: collective mode of current queue
Oded Gabbay4c172bb2019-08-30 16:59:33 +0300313 * @driver_only: true if only the driver is allowed to send a job to this queue,
314 * false otherwise.
Ofir Bitton21e7a342020-05-14 18:25:47 +0300315 * @supports_sync_stream: True if queue supports sync stream
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200316 */
317struct hw_queue_properties {
318 enum hl_queue_type type;
Tal Cohen4bb1f2f2020-06-03 09:25:27 +0300319 enum queue_cb_alloc_flags cb_alloc_flags;
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300320 enum hl_collective_mode collective_mode;
Oded Gabbay4c172bb2019-08-30 16:59:33 +0300321 u8 driver_only;
Ofir Bitton21e7a342020-05-14 18:25:47 +0300322 u8 supports_sync_stream;
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200323};
Oded Gabbayc4d66342019-02-16 00:39:11 +0200324
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200325/**
Oded Gabbay82629c72021-06-29 18:08:05 +0300326 * enum vm_type - virtual memory mapping request information.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200327 * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200328 * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200329 */
Oded Gabbay82629c72021-06-29 18:08:05 +0300330enum vm_type {
Omer Shpigelman7b6e4ea2019-11-14 18:23:53 +0000331 VM_TYPE_USERPTR = 0x1,
332 VM_TYPE_PHYS_PACK = 0x2
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200333};
334
335/**
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200336 * enum hl_device_hw_state - H/W device state. use this to understand whether
337 * to do reset before hw_init or not
338 * @HL_DEVICE_HW_STATE_CLEAN: H/W state is clean. i.e. after hard reset
339 * @HL_DEVICE_HW_STATE_DIRTY: H/W state is dirty. i.e. we started to execute
340 * hw_init
341 */
342enum hl_device_hw_state {
343 HL_DEVICE_HW_STATE_CLEAN = 0,
344 HL_DEVICE_HW_STATE_DIRTY
345};
346
Ofir Bitton412c41f2020-11-04 15:18:55 +0200347#define HL_MMU_VA_ALIGNMENT_NOT_NEEDED 0
348
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200349/**
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000350 * struct hl_mmu_properties - ASIC specific MMU address translation properties.
Omer Shpigelman64a7e292020-01-05 09:05:45 +0000351 * @start_addr: virtual start address of the memory region.
352 * @end_addr: virtual end address of the memory region.
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000353 * @hop0_shift: shift of hop 0 mask.
354 * @hop1_shift: shift of hop 1 mask.
355 * @hop2_shift: shift of hop 2 mask.
356 * @hop3_shift: shift of hop 3 mask.
357 * @hop4_shift: shift of hop 4 mask.
Moti Haimovski7edf3412020-08-23 13:23:13 +0300358 * @hop5_shift: shift of hop 5 mask.
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000359 * @hop0_mask: mask to get the PTE address in hop 0.
360 * @hop1_mask: mask to get the PTE address in hop 1.
361 * @hop2_mask: mask to get the PTE address in hop 2.
362 * @hop3_mask: mask to get the PTE address in hop 3.
363 * @hop4_mask: mask to get the PTE address in hop 4.
Moti Haimovski7edf3412020-08-23 13:23:13 +0300364 * @hop5_mask: mask to get the PTE address in hop 5.
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000365 * @page_size: default page size used to allocate memory.
Moti Haimovski7edf3412020-08-23 13:23:13 +0300366 * @num_hops: The amount of hops supported by the translation table.
Moti Haimovskiccf979e2020-10-05 17:59:29 +0300367 * @host_resident: Should the MMU page table reside in host memory or in the
368 * device DRAM.
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000369 */
370struct hl_mmu_properties {
Omer Shpigelman64a7e292020-01-05 09:05:45 +0000371 u64 start_addr;
372 u64 end_addr;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000373 u64 hop0_shift;
374 u64 hop1_shift;
375 u64 hop2_shift;
376 u64 hop3_shift;
377 u64 hop4_shift;
Moti Haimovski7edf3412020-08-23 13:23:13 +0300378 u64 hop5_shift;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000379 u64 hop0_mask;
380 u64 hop1_mask;
381 u64 hop2_mask;
382 u64 hop3_mask;
383 u64 hop4_mask;
Moti Haimovski7edf3412020-08-23 13:23:13 +0300384 u64 hop5_mask;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000385 u32 page_size;
Moti Haimovski7edf3412020-08-23 13:23:13 +0300386 u32 num_hops;
Moti Haimovskiccf979e2020-10-05 17:59:29 +0300387 u8 host_resident;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000388};
389
390/**
farah kassabri1ae32b92021-01-31 18:56:03 +0200391 * struct hl_hints_range - hint addresses reserved va range.
392 * @start_addr: start address of the va range.
393 * @end_addr: end address of the va range.
394 */
395struct hl_hints_range {
396 u64 start_addr;
397 u64 end_addr;
398};
399
400/**
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200401 * struct asic_fixed_properties - ASIC specific immutable properties.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200402 * @hw_queues_props: H/W queues properties.
Oded Gabbay2f553422020-08-15 16:28:10 +0300403 * @cpucp_info: received various information from CPU-CP regarding the H/W, e.g.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200404 * available sensors.
Oded Gabbay839c4802019-02-16 00:39:16 +0200405 * @uboot_ver: F/W U-boot version.
406 * @preboot_ver: F/W Preboot version.
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000407 * @dmmu: DRAM MMU address translation properties.
408 * @pmmu: PCI (host) MMU address translation properties.
Omer Shpigelman64a7e292020-01-05 09:05:45 +0000409 * @pmmu_huge: PCI (host) MMU address translation properties for memory
410 * allocated with huge pages.
farah kassabri1ae32b92021-01-31 18:56:03 +0200411 * @hints_dram_reserved_va_range: dram hint addresses reserved range.
412 * @hints_host_reserved_va_range: host hint addresses reserved range.
413 * @hints_host_hpage_reserved_va_range: host huge page hint addresses reserved
414 * range.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200415 * @sram_base_address: SRAM physical start address.
416 * @sram_end_address: SRAM physical end address.
417 * @sram_user_base_address - SRAM physical start address for user access.
418 * @dram_base_address: DRAM physical start address.
419 * @dram_end_address: DRAM physical end address.
420 * @dram_user_base_address: DRAM physical start address for user access.
421 * @dram_size: DRAM total size.
422 * @dram_pci_bar_size: size of PCI bar towards DRAM.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200423 * @max_power_default: max power of the device after reset
Koby Elbazcd5def82021-02-23 21:31:27 +0200424 * @dc_power_default: power consumed by the device in mode idle.
Omer Shpigelman27ca384c2019-02-28 10:46:11 +0200425 * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
426 * fault.
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200427 * @pcie_dbi_base_address: Base address of the PCIE_DBI block.
428 * @pcie_aux_dbi_reg_addr: Address of the PCIE_AUX DBI register.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200429 * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
Omer Shpigelman27ca384c2019-02-28 10:46:11 +0200430 * @mmu_dram_default_page_addr: DRAM default page physical address.
Tomer Tayaref6a0f62020-07-09 16:17:48 +0300431 * @cb_va_start_addr: virtual start address of command buffers which are mapped
432 * to the device's MMU.
433 * @cb_va_end_addr: virtual end address of command buffers which are mapped to
434 * the device's MMU.
farah kassabri1ae32b92021-01-31 18:56:03 +0200435 * @dram_hints_align_mask: dram va hint addresses alignment mask which is used
436 * for hints validity check.
Oded Gabbay2b5bbef2021-07-10 21:12:45 +0300437 * device_dma_offset_for_host_access: the offset to add to host DMA addresses
438 * to enable the device to access them.
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200439 * @mmu_pgt_size: MMU page tables total size.
440 * @mmu_pte_size: PTE size in MMU page tables.
441 * @mmu_hop_table_size: MMU hop table size.
442 * @mmu_hop0_tables_total_size: total size of MMU hop0 tables.
443 * @dram_page_size: page size for MMU DRAM allocation.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200444 * @cfg_size: configuration space size on SRAM.
445 * @sram_size: total size of SRAM.
446 * @max_asid: maximum number of open contexts (ASIDs).
Oded Gabbay1251f232019-02-16 00:39:18 +0200447 * @num_of_events: number of possible internal H/W IRQs.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200448 * @psoc_pci_pll_nr: PCI PLL NR value.
449 * @psoc_pci_pll_nf: PCI PLL NF value.
450 * @psoc_pci_pll_od: PCI PLL OD value.
451 * @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value.
Adam Aharone8edded2020-05-26 11:04:30 +0300452 * @psoc_timestamp_frequency: frequency of the psoc timestamp clock.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200453 * @high_pll: high PLL frequency used by the device.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200454 * @cb_pool_cb_cnt: number of CBs in the CB pool.
455 * @cb_pool_cb_size: size of each CB in the CB pool.
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300456 * @max_pending_cs: maximum of concurrent pending command submissions
457 * @max_queues: maximum amount of queues in the system
Ohad Sharabie67a60402021-05-02 15:45:21 +0300458 * @fw_preboot_cpu_boot_dev_sts0: bitmap representation of preboot cpu
459 * capabilities reported by FW, bit description
460 * can be found in CPU_BOOT_DEV_STS0
461 * @fw_preboot_cpu_boot_dev_sts1: bitmap representation of preboot cpu
462 * capabilities reported by FW, bit description
463 * can be found in CPU_BOOT_DEV_STS1
464 * @fw_bootfit_cpu_boot_dev_sts0: bitmap representation of boot cpu security
465 * status reported by FW, bit description can be
466 * found in CPU_BOOT_DEV_STS0
467 * @fw_bootfit_cpu_boot_dev_sts1: bitmap representation of boot cpu security
468 * status reported by FW, bit description can be
469 * found in CPU_BOOT_DEV_STS1
470 * @fw_app_cpu_boot_dev_sts0: bitmap representation of application security
471 * status reported by FW, bit description can be
472 * found in CPU_BOOT_DEV_STS0
473 * @fw_app_cpu_boot_dev_sts1: bitmap representation of application security
474 * status reported by FW, bit description can be
475 * found in CPU_BOOT_DEV_STS1
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300476 * @collective_first_sob: first sync object available for collective use
477 * @collective_first_mon: first monitor available for collective use
Ofir Bitton21e7a342020-05-14 18:25:47 +0300478 * @sync_stream_first_sob: first sync object available for sync stream use
479 * @sync_stream_first_mon: first monitor available for sync stream use
Ofir Bitton843839b2020-07-19 11:08:09 +0300480 * @first_available_user_sob: first sob available for the user
481 * @first_available_user_mon: first monitor available for the user
Ofir Bittone1fa7242021-01-06 15:40:37 +0200482 * @first_available_user_msix_interrupt: first available msix interrupt
483 * reserved for the user
Ofir Bittone52606d2021-01-27 16:34:37 +0200484 * @first_available_cq: first available CQ for the user.
Ofir Bitton2d44c6f2021-01-12 14:43:09 +0200485 * @user_interrupt_count: number of user interrupts.
Ofir Bitton21e7a342020-05-14 18:25:47 +0300486 * @tpc_enabled_mask: which TPCs are enabled.
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000487 * @completion_queues_count: number of completion queues.
Ohad Sharabi4cb45082021-05-20 09:09:03 +0300488 * @fw_security_enabled: true if security measures are enabled in firmware,
489 * false otherwise
Ohad Sharabie67a60402021-05-02 15:45:21 +0300490 * @fw_cpu_boot_dev_sts0_valid: status bits are valid and can be fetched from
491 * BOOT_DEV_STS0
492 * @fw_cpu_boot_dev_sts1_valid: status bits are valid and can be fetched from
493 * BOOT_DEV_STS1
Oded Gabbay7f070c92020-11-09 09:48:31 +0200494 * @dram_supports_virtual_memory: is there an MMU towards the DRAM
Ofir Bittond611b9f2020-11-08 13:10:09 +0200495 * @hard_reset_done_by_fw: true if firmware is handling hard reset flow
Oded Gabbay1530d462020-12-18 19:12:56 +0200496 * @num_functional_hbms: number of functional HBMs in each DCORE.
farah kassabri1ae32b92021-01-31 18:56:03 +0200497 * @hints_range_reservation: device support hint addresses range reservation.
Ofir Bitton41f458f2021-03-10 15:08:44 +0200498 * @iatu_done_by_fw: true if iATU configuration is being done by FW.
Ohad Sharabi6e16ab62021-03-25 10:06:26 +0200499 * @dynamic_fw_load: is dynamic FW load is supported.
Koby Elbaz81217362021-05-03 23:03:15 +0300500 * @gic_interrupts_enable: true if FW is not blocking GIC controller,
501 * false otherwise.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200502 */
503struct asic_fixed_properties {
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300504 struct hw_queue_properties *hw_queues_props;
Oded Gabbay2f553422020-08-15 16:28:10 +0300505 struct cpucp_info cpucp_info;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000506 char uboot_ver[VERSION_MAX_LEN];
507 char preboot_ver[VERSION_MAX_LEN];
508 struct hl_mmu_properties dmmu;
509 struct hl_mmu_properties pmmu;
Omer Shpigelman64a7e292020-01-05 09:05:45 +0000510 struct hl_mmu_properties pmmu_huge;
farah kassabri1ae32b92021-01-31 18:56:03 +0200511 struct hl_hints_range hints_dram_reserved_va_range;
512 struct hl_hints_range hints_host_reserved_va_range;
513 struct hl_hints_range hints_host_hpage_reserved_va_range;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000514 u64 sram_base_address;
515 u64 sram_end_address;
516 u64 sram_user_base_address;
517 u64 dram_base_address;
518 u64 dram_end_address;
519 u64 dram_user_base_address;
520 u64 dram_size;
521 u64 dram_pci_bar_size;
522 u64 max_power_default;
Koby Elbazcd5def82021-02-23 21:31:27 +0200523 u64 dc_power_default;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000524 u64 dram_size_for_default_page_mapping;
525 u64 pcie_dbi_base_address;
526 u64 pcie_aux_dbi_reg_addr;
527 u64 mmu_pgt_addr;
528 u64 mmu_dram_default_page_addr;
Tomer Tayaref6a0f62020-07-09 16:17:48 +0300529 u64 cb_va_start_addr;
530 u64 cb_va_end_addr;
farah kassabri1ae32b92021-01-31 18:56:03 +0200531 u64 dram_hints_align_mask;
Oded Gabbay2b5bbef2021-07-10 21:12:45 +0300532 u64 device_dma_offset_for_host_access;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000533 u32 mmu_pgt_size;
534 u32 mmu_pte_size;
535 u32 mmu_hop_table_size;
536 u32 mmu_hop0_tables_total_size;
537 u32 dram_page_size;
538 u32 cfg_size;
539 u32 sram_size;
540 u32 max_asid;
541 u32 num_of_events;
542 u32 psoc_pci_pll_nr;
543 u32 psoc_pci_pll_nf;
544 u32 psoc_pci_pll_od;
545 u32 psoc_pci_pll_div_factor;
Adam Aharone8edded2020-05-26 11:04:30 +0300546 u32 psoc_timestamp_frequency;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000547 u32 high_pll;
548 u32 cb_pool_cb_cnt;
549 u32 cb_pool_cb_size;
Ofir Bittonc16d45f2020-06-02 12:28:27 +0300550 u32 max_pending_cs;
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300551 u32 max_queues;
Ohad Sharabie67a60402021-05-02 15:45:21 +0300552 u32 fw_preboot_cpu_boot_dev_sts0;
553 u32 fw_preboot_cpu_boot_dev_sts1;
554 u32 fw_bootfit_cpu_boot_dev_sts0;
555 u32 fw_bootfit_cpu_boot_dev_sts1;
556 u32 fw_app_cpu_boot_dev_sts0;
557 u32 fw_app_cpu_boot_dev_sts1;
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300558 u16 collective_first_sob;
559 u16 collective_first_mon;
Ofir Bitton21e7a342020-05-14 18:25:47 +0300560 u16 sync_stream_first_sob;
561 u16 sync_stream_first_mon;
Ofir Bitton843839b2020-07-19 11:08:09 +0300562 u16 first_available_user_sob[HL_MAX_DCORES];
563 u16 first_available_user_mon[HL_MAX_DCORES];
Ofir Bittone1fa7242021-01-06 15:40:37 +0200564 u16 first_available_user_msix_interrupt;
Ofir Bittone52606d2021-01-27 16:34:37 +0200565 u16 first_available_cq[HL_MAX_DCORES];
Ofir Bitton2d44c6f2021-01-12 14:43:09 +0200566 u16 user_interrupt_count;
Omer Shpigelman54bb6742019-11-14 18:23:55 +0000567 u8 tpc_enabled_mask;
568 u8 completion_queues_count;
Ohad Sharabi4cb45082021-05-20 09:09:03 +0300569 u8 fw_security_enabled;
Ohad Sharabie67a60402021-05-02 15:45:21 +0300570 u8 fw_cpu_boot_dev_sts0_valid;
571 u8 fw_cpu_boot_dev_sts1_valid;
Oded Gabbay7f070c92020-11-09 09:48:31 +0200572 u8 dram_supports_virtual_memory;
Ofir Bittond611b9f2020-11-08 13:10:09 +0200573 u8 hard_reset_done_by_fw;
Oded Gabbay1530d462020-12-18 19:12:56 +0200574 u8 num_functional_hbms;
farah kassabri1ae32b92021-01-31 18:56:03 +0200575 u8 hints_range_reservation;
Ofir Bitton41f458f2021-03-10 15:08:44 +0200576 u8 iatu_done_by_fw;
Ohad Sharabi6e16ab62021-03-25 10:06:26 +0200577 u8 dynamic_fw_load;
Koby Elbaz81217362021-05-03 23:03:15 +0300578 u8 gic_interrupts_enable;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200579};
580
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200581/**
Ofir Bittona98d73c2020-07-15 08:52:39 +0300582 * struct hl_fence - software synchronization primitive
583 * @completion: fence is implemented using completion
584 * @refcount: refcount for this fence
Ofir Bitton0811b392020-12-06 17:18:02 +0200585 * @cs_sequence: sequence of the corresponding command submission
Ofir Bittona98d73c2020-07-15 08:52:39 +0300586 * @error: mark this fence with error
Ofir Bittonbd2f4772020-11-10 17:26:22 +0200587 * @timestamp: timestamp upon completion
Ofir Bittona98d73c2020-07-15 08:52:39 +0300588 *
589 */
590struct hl_fence {
591 struct completion completion;
592 struct kref refcount;
Ofir Bitton0811b392020-12-06 17:18:02 +0200593 u64 cs_sequence;
Ofir Bittona98d73c2020-07-15 08:52:39 +0300594 int error;
Ofir Bittonbd2f4772020-11-10 17:26:22 +0200595 ktime_t timestamp;
Ofir Bittona98d73c2020-07-15 08:52:39 +0300596};
597
598/**
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300599 * struct hl_cs_compl - command submission completion object.
Ofir Bitton8445dde2021-03-18 17:36:57 +0200600 * @sob_reset_work: workqueue object to run SOB reset flow.
Ofir Bittona98d73c2020-07-15 08:52:39 +0300601 * @base_fence: hl fence object.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200602 * @lock: spinlock to protect fence.
603 * @hdev: habanalabs device structure.
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300604 * @hw_sob: the H/W SOB used in this signal/wait CS.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200605 * @cs_seq: command submission sequence number.
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300606 * @type: type of the CS - signal/wait.
607 * @sob_val: the SOB value that is used in this signal/wait CS.
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300608 * @sob_group: the SOB group that is used in this collective wait CS.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200609 */
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300610struct hl_cs_compl {
Ofir Bitton8445dde2021-03-18 17:36:57 +0200611 struct work_struct sob_reset_work;
Ofir Bittona98d73c2020-07-15 08:52:39 +0300612 struct hl_fence base_fence;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200613 spinlock_t lock;
614 struct hl_device *hdev;
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300615 struct hl_hw_sob *hw_sob;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200616 u64 cs_seq;
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300617 enum hl_cs_type type;
618 u16 sob_val;
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300619 u16 sob_group;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200620};
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200621
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200622/*
623 * Command Buffers
624 */
625
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200626/**
627 * struct hl_cb_mgr - describes a Command Buffer Manager.
628 * @cb_lock: protects cb_handles.
629 * @cb_handles: an idr to hold all command buffer handles.
630 */
631struct hl_cb_mgr {
632 spinlock_t cb_lock;
633 struct idr cb_handles; /* protected by cb_lock */
634};
635
636/**
637 * struct hl_cb - describes a Command Buffer.
638 * @refcount: reference counter for usage of the CB.
639 * @hdev: pointer to device this CB belongs to.
Tomer Tayarfa8641a12020-09-07 17:36:41 +0300640 * @ctx: pointer to the CB owner's context.
Tomer Tayarf07486742020-08-02 22:51:31 +0300641 * @lock: spinlock to protect mmap flows.
Oded Gabbayc2164772019-02-16 00:39:24 +0200642 * @debugfs_list: node in debugfs list of command buffers.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200643 * @pool_list: node in pool list of command buffers.
Tomer Tayaref6a0f62020-07-09 16:17:48 +0300644 * @va_block_list: list of virtual addresses blocks of the CB if it is mapped to
645 * the device's MMU.
Oded Gabbayf5b9c8c2020-08-12 10:11:20 +0300646 * @id: the CB's ID.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200647 * @kernel_address: Holds the CB's kernel virtual address.
648 * @bus_address: Holds the CB's DMA address.
649 * @mmap_size: Holds the CB's size that was mmaped.
650 * @size: holds the CB's size.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200651 * @cs_cnt: holds number of CS that this CB participates in.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200652 * @mmap: true if the CB is currently mmaped to user.
653 * @is_pool: true if CB was acquired from the pool, false otherwise.
Ofir Bittona04b7cd2020-07-13 13:36:55 +0300654 * @is_internal: internaly allocated
Tomer Tayaref6a0f62020-07-09 16:17:48 +0300655 * @is_mmu_mapped: true if the CB is mapped to the device's MMU.
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200656 */
657struct hl_cb {
658 struct kref refcount;
659 struct hl_device *hdev;
Tomer Tayarfa8641a12020-09-07 17:36:41 +0300660 struct hl_ctx *ctx;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200661 spinlock_t lock;
Oded Gabbayc2164772019-02-16 00:39:24 +0200662 struct list_head debugfs_list;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200663 struct list_head pool_list;
Tomer Tayaref6a0f62020-07-09 16:17:48 +0300664 struct list_head va_block_list;
Oded Gabbayf5b9c8c2020-08-12 10:11:20 +0300665 u64 id;
Arnd Bergmann82948e62020-10-26 17:08:06 +0100666 void *kernel_address;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200667 dma_addr_t bus_address;
668 u32 mmap_size;
669 u32 size;
Tomer Tayarf07486742020-08-02 22:51:31 +0300670 atomic_t cs_cnt;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200671 u8 mmap;
672 u8 is_pool;
Ofir Bittona04b7cd2020-07-13 13:36:55 +0300673 u8 is_internal;
Tomer Tayaref6a0f62020-07-09 16:17:48 +0300674 u8 is_mmu_mapped;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200675};
676
677
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200678/*
679 * QUEUES
680 */
681
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300682struct hl_cs;
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200683struct hl_cs_job;
684
Ofir Bittondd9efab2020-06-15 12:09:50 +0300685/* Queue length of external and HW queues */
686#define HL_QUEUE_LENGTH 4096
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200687#define HL_QUEUE_SIZE_IN_BYTES (HL_QUEUE_LENGTH * HL_BD_SIZE)
688
Ofir Bitton22cb8552020-07-08 10:16:27 +0300689#if (HL_MAX_JOBS_PER_CS > HL_QUEUE_LENGTH)
690#error "HL_QUEUE_LENGTH must be greater than HL_MAX_JOBS_PER_CS"
691#endif
692
Ofir Bittondd9efab2020-06-15 12:09:50 +0300693/* HL_CQ_LENGTH is in units of struct hl_cq_entry */
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200694#define HL_CQ_LENGTH HL_QUEUE_LENGTH
695#define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)
696
Ofir Bittondd9efab2020-06-15 12:09:50 +0300697/* Must be power of 2 */
Oded Gabbay1251f232019-02-16 00:39:18 +0200698#define HL_EQ_LENGTH 64
699#define HL_EQ_SIZE_IN_BYTES (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200700
Oded Gabbay6138bbe2020-09-04 20:18:16 +0300701/* Host <-> CPU-CP shared memory size */
Oded Gabbay95b5a8b2019-05-29 17:30:04 +0300702#define HL_CPU_ACCESSIBLE_MEM_SIZE SZ_2M
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200703
704/**
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300705 * struct hl_sync_stream_properties -
706 * describes a H/W queue sync stream properties
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300707 * @hw_sob: array of the used H/W SOBs by this H/W queue.
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300708 * @next_sob_val: the next value to use for the currently used SOB.
709 * @base_sob_id: the base SOB id of the SOBs used by this queue.
710 * @base_mon_id: the base MON id of the MONs used by this queue.
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300711 * @collective_mstr_mon_id: the MON ids of the MONs used by this master queue
712 * in order to sync with all slave queues.
713 * @collective_slave_mon_id: the MON id used by this slave queue in order to
714 * sync with its master queue.
715 * @collective_sob_id: current SOB id used by this collective slave queue
716 * to signal its collective master queue upon completion.
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300717 * @curr_sob_offset: the id offset to the currently used SOB from the
718 * HL_RSVD_SOBS that are being used by this queue.
719 */
720struct hl_sync_stream_properties {
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300721 struct hl_hw_sob hw_sob[HL_RSVD_SOBS];
722 u16 next_sob_val;
723 u16 base_sob_id;
724 u16 base_mon_id;
725 u16 collective_mstr_mon_id[HL_COLLECTIVE_RSVD_MSTR_MONS];
726 u16 collective_slave_mon_id;
727 u16 collective_sob_id;
728 u8 curr_sob_offset;
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300729};
730
731/**
732 * struct hl_hw_queue - describes a H/W transport queue.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200733 * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300734 * @sync_stream_prop: sync stream queue properties
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200735 * @queue_type: type of queue.
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300736 * @collective_mode: collective mode of current queue
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200737 * @kernel_address: holds the queue's kernel virtual address.
738 * @bus_address: holds the queue's DMA address.
739 * @pi: holds the queue's pi value.
740 * @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci).
741 * @hw_queue_id: the id of the H/W queue.
Omer Shpigelman1fa185c2020-03-01 19:59:39 +0200742 * @cq_id: the id for the corresponding CQ for this H/W queue.
743 * @msi_vec: the IRQ number of the H/W queue.
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200744 * @int_queue_len: length of internal queue (number of entries).
745 * @valid: is the queue valid (we have array of 32 queues, not all of them
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +0300746 * exist).
Ofir Bitton21e7a342020-05-14 18:25:47 +0300747 * @supports_sync_stream: True if queue supports sync stream
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200748 */
749struct hl_hw_queue {
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300750 struct hl_cs_job **shadow_queue;
751 struct hl_sync_stream_properties sync_stream_prop;
752 enum hl_queue_type queue_type;
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300753 enum hl_collective_mode collective_mode;
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300754 void *kernel_address;
755 dma_addr_t bus_address;
756 u32 pi;
757 atomic_t ci;
758 u32 hw_queue_id;
759 u32 cq_id;
760 u32 msi_vec;
761 u16 int_queue_len;
762 u8 valid;
763 u8 supports_sync_stream;
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200764};
765
766/**
767 * struct hl_cq - describes a completion queue
768 * @hdev: pointer to the device structure
769 * @kernel_address: holds the queue's kernel virtual address
770 * @bus_address: holds the queue's DMA address
Ofir Bitton5574cb22020-07-05 13:35:51 +0300771 * @cq_idx: completion queue index in array
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200772 * @hw_queue_id: the id of the matching H/W queue
773 * @ci: ci inside the queue
774 * @pi: pi inside the queue
775 * @free_slots_cnt: counter of free slots in queue
776 */
777struct hl_cq {
778 struct hl_device *hdev;
Arnd Bergmann82948e62020-10-26 17:08:06 +0100779 void *kernel_address;
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200780 dma_addr_t bus_address;
Ofir Bitton5574cb22020-07-05 13:35:51 +0300781 u32 cq_idx;
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200782 u32 hw_queue_id;
783 u32 ci;
784 u32 pi;
785 atomic_t free_slots_cnt;
786};
Oded Gabbay0861e412019-02-16 00:39:14 +0200787
Oded Gabbay1251f232019-02-16 00:39:18 +0200788/**
Ofir Bitton2d44c6f2021-01-12 14:43:09 +0200789 * struct hl_user_interrupt - holds user interrupt information
790 * @hdev: pointer to the device structure
Ofir Bittonab5f5c32021-01-12 18:37:19 +0200791 * @wait_list_head: head to the list of user threads pending on this interrupt
792 * @wait_list_lock: protects wait_list_head
Ofir Bitton2d44c6f2021-01-12 14:43:09 +0200793 * @interrupt_id: msix interrupt id
794 */
795struct hl_user_interrupt {
796 struct hl_device *hdev;
Ofir Bittonab5f5c32021-01-12 18:37:19 +0200797 struct list_head wait_list_head;
798 spinlock_t wait_list_lock;
Ofir Bitton2d44c6f2021-01-12 14:43:09 +0200799 u32 interrupt_id;
800};
801
802/**
Ofir Bittonab5f5c32021-01-12 18:37:19 +0200803 * struct hl_user_pending_interrupt - holds a context to a user thread
804 * pending on an interrupt
805 * @wait_list_node: node in the list of user threads pending on an interrupt
806 * @fence: hl fence object for interrupt completion
807 */
808struct hl_user_pending_interrupt {
809 struct list_head wait_list_node;
810 struct hl_fence fence;
811};
812
813/**
Oded Gabbay1251f232019-02-16 00:39:18 +0200814 * struct hl_eq - describes the event queue (single one per device)
815 * @hdev: pointer to the device structure
816 * @kernel_address: holds the queue's kernel virtual address
817 * @bus_address: holds the queue's DMA address
818 * @ci: ci inside the queue
Oded Gabbay1242e9f2021-05-19 14:52:14 +0300819 * @prev_eqe_index: the index of the previous event queue entry. The index of
820 * the current entry's index must be +1 of the previous one.
821 * @check_eqe_index: do we need to check the index of the current entry vs. the
822 * previous one. This is for backward compatibility with older
823 * firmwares
Oded Gabbay1251f232019-02-16 00:39:18 +0200824 */
825struct hl_eq {
826 struct hl_device *hdev;
Arnd Bergmann82948e62020-10-26 17:08:06 +0100827 void *kernel_address;
Oded Gabbay1251f232019-02-16 00:39:18 +0200828 dma_addr_t bus_address;
829 u32 ci;
Oded Gabbay1242e9f2021-05-19 14:52:14 +0300830 u32 prev_eqe_index;
831 bool check_eqe_index;
Oded Gabbay1251f232019-02-16 00:39:18 +0200832};
833
Oded Gabbay0861e412019-02-16 00:39:14 +0200834
Oded Gabbayc4d66342019-02-16 00:39:11 +0200835/*
836 * ASICs
837 */
838
839/**
840 * enum hl_asic_type - supported ASIC types.
Oded Gabbayc4d66342019-02-16 00:39:11 +0200841 * @ASIC_INVALID: Invalid ASIC type.
Oded Gabbay29593842019-04-04 14:33:34 +0300842 * @ASIC_GOYA: Goya device.
Oded Gabbay6966d9e2020-03-21 10:58:32 +0200843 * @ASIC_GAUDI: Gaudi device.
Ofir Bittone5042a62021-04-01 13:43:40 +0300844 * @ASIC_GAUDI_SEC: Gaudi secured device (HL-2000).
Oded Gabbayc4d66342019-02-16 00:39:11 +0200845 */
846enum hl_asic_type {
Oded Gabbay29593842019-04-04 14:33:34 +0300847 ASIC_INVALID,
Oded Gabbay6966d9e2020-03-21 10:58:32 +0200848 ASIC_GOYA,
Ofir Bittone5042a62021-04-01 13:43:40 +0300849 ASIC_GAUDI,
850 ASIC_GAUDI_SEC
Oded Gabbayc4d66342019-02-16 00:39:11 +0200851};
852
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200853struct hl_cs_parser;
854
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200855/**
Oded Gabbayd91389b2019-02-16 00:39:19 +0200856 * enum hl_pm_mng_profile - power management profile.
Oded Gabbay4c172bb2019-08-30 16:59:33 +0300857 * @PM_AUTO: internal clock is set by the Linux driver.
Oded Gabbayd91389b2019-02-16 00:39:19 +0200858 * @PM_MANUAL: internal clock is set by the user.
859 * @PM_LAST: last power management type.
860 */
861enum hl_pm_mng_profile {
862 PM_AUTO = 1,
863 PM_MANUAL,
864 PM_LAST
865};
866
867/**
868 * enum hl_pll_frequency - PLL frequency.
869 * @PLL_HIGH: high frequency.
870 * @PLL_LOW: low frequency.
871 * @PLL_LAST: last frequency values that were configured by the user.
872 */
873enum hl_pll_frequency {
874 PLL_HIGH = 1,
875 PLL_LOW,
876 PLL_LAST
877};
878
Adam Aharone8edded2020-05-26 11:04:30 +0300879#define PLL_REF_CLK 50
880
881enum div_select_defs {
882 DIV_SEL_REF_CLK = 0,
883 DIV_SEL_PLL_CLK = 1,
884 DIV_SEL_DIVIDED_REF = 2,
885 DIV_SEL_DIVIDED_PLL = 3,
886};
887
Ohad Sharabic592c272021-04-21 13:03:21 +0300888enum pci_region {
889 PCI_REGION_CFG,
890 PCI_REGION_SRAM,
891 PCI_REGION_DRAM,
892 PCI_REGION_SP_SRAM,
893 PCI_REGION_NUMBER,
894};
895
896/**
897 * struct pci_mem_region - describe memory region in a PCI bar
898 * @region_base: region base address
899 * @region_size: region size
Ohad Sharabib31e59b2021-04-22 10:01:22 +0300900 * @bar_size: size of the BAR
Ohad Sharabic592c272021-04-21 13:03:21 +0300901 * @offset_in_bar: region offset into the bar
902 * @bar_id: bar ID of the region
903 * @used: if used 1, otherwise 0
904 */
905struct pci_mem_region {
906 u64 region_base;
907 u64 region_size;
Ohad Sharabib31e59b2021-04-22 10:01:22 +0300908 u64 bar_size;
Ohad Sharabic592c272021-04-21 13:03:21 +0300909 u32 offset_in_bar;
910 u8 bar_id;
911 u8 used;
912};
913
Oded Gabbayd91389b2019-02-16 00:39:19 +0200914/**
Ohad Sharabi22a795b2021-04-08 13:42:00 +0300915 * struct static_fw_load_mgr - static FW load manager
Ohad Sharabi50f036d2021-04-11 15:26:28 +0300916 * @preboot_version_max_off: max offset to preboot version
917 * @boot_fit_version_max_off: max offset to boot fit version
Ohad Sharabi8a43c832021-04-11 10:32:18 +0300918 * @kmd_msg_to_cpu_reg: register address for KDM->CPU messages
919 * @cpu_cmd_status_to_host_reg: register address for CPU command status response
Ohad Sharabia22f0ec2021-04-11 23:06:46 +0300920 * @cpu_boot_status_reg: boot status register
Ohad Sharabie67a60402021-05-02 15:45:21 +0300921 * @cpu_boot_dev_status0_reg: boot device status register 0
922 * @cpu_boot_dev_status1_reg: boot device status register 1
923 * @boot_err0_reg: boot error register 0
924 * @boot_err1_reg: boot error register 1
Ohad Sharabi50f036d2021-04-11 15:26:28 +0300925 * @preboot_version_offset_reg: SRAM offset to preboot version register
926 * @boot_fit_version_offset_reg: SRAM offset to boot fit version register
927 * @sram_offset_mask: mask for getting offset into the SRAM
Koby Elbazb7a71fd2021-06-15 17:07:02 +0300928 * @cpu_reset_wait_msec: used when setting WFE via kmd_msg_to_cpu_reg
Ohad Sharabia22f0ec2021-04-11 23:06:46 +0300929 */
Ohad Sharabi22a795b2021-04-08 13:42:00 +0300930struct static_fw_load_mgr {
Ohad Sharabi50f036d2021-04-11 15:26:28 +0300931 u64 preboot_version_max_off;
932 u64 boot_fit_version_max_off;
Ohad Sharabi8a43c832021-04-11 10:32:18 +0300933 u32 kmd_msg_to_cpu_reg;
934 u32 cpu_cmd_status_to_host_reg;
Ohad Sharabia22f0ec2021-04-11 23:06:46 +0300935 u32 cpu_boot_status_reg;
Ohad Sharabie67a60402021-05-02 15:45:21 +0300936 u32 cpu_boot_dev_status0_reg;
937 u32 cpu_boot_dev_status1_reg;
Ohad Sharabia22f0ec2021-04-11 23:06:46 +0300938 u32 boot_err0_reg;
Ohad Sharabie67a60402021-05-02 15:45:21 +0300939 u32 boot_err1_reg;
Ohad Sharabi50f036d2021-04-11 15:26:28 +0300940 u32 preboot_version_offset_reg;
941 u32 boot_fit_version_offset_reg;
942 u32 sram_offset_mask;
Koby Elbazb7a71fd2021-06-15 17:07:02 +0300943 u32 cpu_reset_wait_msec;
Ohad Sharabi22a795b2021-04-08 13:42:00 +0300944};
945
946/**
Ohad Sharabi8a43c832021-04-11 10:32:18 +0300947 * struct fw_response - FW response to LKD command
948 * @ram_offset: descriptor offset into the RAM
949 * @ram_type: RAM type containing the descriptor (SRAM/DRAM)
950 * @status: command status
951 */
952struct fw_response {
953 u32 ram_offset;
954 u8 ram_type;
955 u8 status;
956};
957
958/**
Ohad Sharabi22a795b2021-04-08 13:42:00 +0300959 * struct dynamic_fw_load_mgr - dynamic FW load manager
Ohad Sharabi8a43c832021-04-11 10:32:18 +0300960 * @response: FW to LKD response
961 * @comm_desc: the communication descriptor with FW
962 * @image_region: region to copy the FW image to
Ohad Sharabib31e59b2021-04-22 10:01:22 +0300963 * @fw_image_size: size of FW image to load
964 * @wait_for_bl_timeout: timeout for waiting for boot loader to respond
Ohad Sharabi22a795b2021-04-08 13:42:00 +0300965 */
966struct dynamic_fw_load_mgr {
Ohad Sharabi8a43c832021-04-11 10:32:18 +0300967 struct fw_response response;
968 struct lkd_fw_comms_desc comm_desc;
969 struct pci_mem_region *image_region;
970 size_t fw_image_size;
Ohad Sharabib31e59b2021-04-22 10:01:22 +0300971 u32 wait_for_bl_timeout;
Ohad Sharabi8a43c832021-04-11 10:32:18 +0300972};
973
974/**
975 * struct fw_image_props - properties of FW image
976 * @image_name: name of the image
977 * @src_off: offset in src FW to copy from
978 * @copy_size: amount of bytes to copy (0 to copy the whole binary)
979 */
980struct fw_image_props {
981 char *image_name;
982 u32 src_off;
983 u32 copy_size;
Ohad Sharabi22a795b2021-04-08 13:42:00 +0300984};
985
986/**
987 * struct fw_load_mgr - manager FW loading process
Ohad Sharabi8a43c832021-04-11 10:32:18 +0300988 * @dynamic_loader: specific structure for dynamic load
989 * @static_loader: specific structure for static load
990 * @boot_fit_img: boot fit image properties
991 * @linux_img: linux image properties
Ohad Sharabi22a795b2021-04-08 13:42:00 +0300992 * @cpu_timeout: CPU response timeout in usec
993 * @boot_fit_timeout: Boot fit load timeout in usec
994 * @skip_bmc: should BMC be skipped
995 * @sram_bar_id: SRAM bar ID
Ohad Sharabi8a43c832021-04-11 10:32:18 +0300996 * @dram_bar_id: DRAM bar ID
Koby Elbaz3649eae2021-05-18 15:43:47 +0300997 * @linux_loaded: true if linux was loaded so far
Ohad Sharabi22a795b2021-04-08 13:42:00 +0300998 */
999struct fw_load_mgr {
Ohad Sharabi8a43c832021-04-11 10:32:18 +03001000 union {
1001 struct dynamic_fw_load_mgr dynamic_loader;
1002 struct static_fw_load_mgr static_loader;
1003 };
1004 struct fw_image_props boot_fit_img;
1005 struct fw_image_props linux_img;
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03001006 u32 cpu_timeout;
1007 u32 boot_fit_timeout;
1008 u8 skip_bmc;
Ohad Sharabi50f036d2021-04-11 15:26:28 +03001009 u8 sram_bar_id;
Ohad Sharabi8a43c832021-04-11 10:32:18 +03001010 u8 dram_bar_id;
Koby Elbaz3649eae2021-05-18 15:43:47 +03001011 u8 linux_loaded;
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03001012};
1013
1014/**
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001015 * struct hl_asic_funcs - ASIC specific functions that are can be called from
1016 * common code.
1017 * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
1018 * @early_fini: tears down what was done in early_init.
Oded Gabbayd91389b2019-02-16 00:39:19 +02001019 * @late_init: sets up late driver/hw state (post hw_init) - Optional.
1020 * @late_fini: tears down what was done in late_init (pre hw_fini) - Optional.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001021 * @sw_init: sets up driver state, does not configure H/W.
1022 * @sw_fini: tears down driver state, does not configure H/W.
Oded Gabbay839c4802019-02-16 00:39:16 +02001023 * @hw_init: sets up the H/W state.
1024 * @hw_fini: tears down the H/W state.
Oded Gabbay1251f232019-02-16 00:39:18 +02001025 * @halt_engines: halt engines, needed for reset sequence. This also disables
1026 * interrupts from the device. Should be called before
1027 * hw_fini and before CS rollback.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001028 * @suspend: handles IP specific H/W or SW changes for suspend.
1029 * @resume: handles IP specific H/W or SW changes for resume.
Zvika Yehudai1ee8e2b2021-07-06 13:50:32 +03001030 * @mmap: maps a memory.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001031 * @ring_doorbell: increment PI on a given QMAN.
Oded Gabbayb9040c92019-08-08 15:45:58 +03001032 * @pqe_write: Write the PQ entry to the PQ. This is ASIC-specific
1033 * function because the PQs are located in different memory areas
1034 * per ASIC (SRAM, DRAM, Host memory) and therefore, the method of
1035 * writing the PQE must match the destination memory area
1036 * properties.
Oded Gabbayd9c3aa82019-05-01 11:47:04 +03001037 * @asic_dma_alloc_coherent: Allocate coherent DMA memory by calling
1038 * dma_alloc_coherent(). This is ASIC function because
1039 * its implementation is not trivial when the driver
1040 * is loaded in simulation mode (not upstreamed).
1041 * @asic_dma_free_coherent: Free coherent DMA memory by calling
1042 * dma_free_coherent(). This is ASIC function because
1043 * its implementation is not trivial when the driver
1044 * is loaded in simulation mode (not upstreamed).
farah kassabri03df1362020-05-06 11:17:38 +03001045 * @scrub_device_mem: Scrub device memory given an address and size
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001046 * @get_int_queue_base: get the internal queue base address.
1047 * @test_queues: run simple test on all queues for sanity check.
Oded Gabbayd9c3aa82019-05-01 11:47:04 +03001048 * @asic_dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool.
1049 * size of allocation is HL_DMA_POOL_BLK_SIZE.
1050 * @asic_dma_pool_free: free small DMA allocation from pool.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001051 * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
1052 * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001053 * @hl_dma_unmap_sg: DMA unmap scatter-gather list.
1054 * @cs_parser: parse Command Submission.
1055 * @asic_dma_map_sg: DMA map scatter-gather list.
1056 * @get_dma_desc_list_size: get number of LIN_DMA packets required for CB.
1057 * @add_end_of_cb_packets: Add packets to the end of CB, if device requires it.
Oded Gabbay1251f232019-02-16 00:39:18 +02001058 * @update_eq_ci: update event queue CI.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001059 * @context_switch: called upon ASID context switch.
1060 * @restore_phase_topology: clear all SOBs amd MONs.
Oded Gabbay639781d2021-04-02 01:43:18 +03001061 * @debugfs_read32: debug interface for reading u32 from DRAM/SRAM/Host memory.
1062 * @debugfs_write32: debug interface for writing u32 to DRAM/SRAM/Host memory.
1063 * @debugfs_read64: debug interface for reading u64 from DRAM/SRAM/Host memory.
1064 * @debugfs_write64: debug interface for writing u64 to DRAM/SRAM/Host memory.
1065 * @debugfs_read_dma: debug interface for reading up to 2MB from the device's
1066 * internal memory via DMA engine.
Oded Gabbayd91389b2019-02-16 00:39:19 +02001067 * @add_device_attr: add ASIC specific device attributes.
Oded Gabbay6138bbe2020-09-04 20:18:16 +03001068 * @handle_eqe: handle event queue entry (IRQ) from CPU-CP.
Oded Gabbayd91389b2019-02-16 00:39:19 +02001069 * @set_pll_profile: change PLL profile (manual/automatic).
Oded Gabbay1251f232019-02-16 00:39:18 +02001070 * @get_events_stat: retrieve event queue entries histogram.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001071 * @read_pte: read MMU page table entry from DRAM.
1072 * @write_pte: write MMU page table entry to DRAM.
Omer Shpigelman7b6e4ea2019-11-14 18:23:53 +00001073 * @mmu_invalidate_cache: flush MMU STLB host/DRAM cache, either with soft
1074 * (L1 only) or hard (L0 & L1) flush.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001075 * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with
1076 * ASID-VA-size mask.
Oded Gabbay6138bbe2020-09-04 20:18:16 +03001077 * @send_heartbeat: send is-alive packet to CPU-CP and verify response.
Oded Gabbaye38bfd32020-07-03 20:46:12 +03001078 * @set_clock_gating: enable/disable clock gating per engine according to
1079 * clock gating mask in hdev
1080 * @disable_clock_gating: disable clock gating completely
Omer Shpigelman315bc052019-04-01 22:31:22 +03001081 * @debug_coresight: perform certain actions on Coresight for debugging.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001082 * @is_device_idle: return true if device is idle, false otherwise.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001083 * @soft_reset_late_init: perform certain actions needed after soft reset.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001084 * @hw_queues_lock: acquire H/W queues lock.
1085 * @hw_queues_unlock: release H/W queues lock.
Oded Gabbayd8dd7b02019-02-16 00:39:23 +02001086 * @get_pci_id: retrieve PCI ID.
Oded Gabbayd91389b2019-02-16 00:39:19 +02001087 * @get_eeprom_data: retrieve EEPROM data from F/W.
Oded Gabbay788cacf2020-07-07 17:30:13 +03001088 * @send_cpu_message: send message to F/W. If the message is timedout, the
1089 * driver will eventually reset the device. The timeout can
1090 * be determined by the calling function or it can be 0 and
1091 * then the timeout is the default timeout for the specific
1092 * ASIC
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001093 * @get_hw_state: retrieve the H/W state
Tomer Tayarb6f897d2019-03-05 16:48:42 +02001094 * @pci_bars_map: Map PCI BARs.
Tomer Tayarb6f897d2019-03-05 16:48:42 +02001095 * @init_iatu: Initialize the iATU unit inside the PCI controller.
Oded Gabbayb2377e02019-04-22 11:49:06 +03001096 * @rreg: Read a register. Needed for simulator support.
1097 * @wreg: Write a register. Needed for simulator support.
Omer Shpigelman89225ce2019-05-01 14:38:38 +03001098 * @halt_coresight: stop the ETF and ETR traces.
Ofir Bittona04b7cd2020-07-13 13:36:55 +03001099 * @ctx_init: context dependent initialization.
Ofir Bitton5de406c2020-09-10 10:56:26 +03001100 * @ctx_fini: context dependent cleanup.
Oded Gabbay62c1e122019-10-10 15:48:59 +03001101 * @get_clk_rate: Retrieve the ASIC current and maximum clock rate in MHz
Omer Shpigelman1fa185c2020-03-01 19:59:39 +02001102 * @get_queue_id_for_cq: Get the H/W queue id related to the given CQ index.
Oded Gabbay7e1c07d2020-03-26 12:32:56 +02001103 * @load_firmware_to_device: load the firmware to the device's memory
Ofir Bitton47f6b412020-04-16 13:47:15 +03001104 * @load_boot_fit_to_device: load boot fit to device's memory
Omer Shpigelmanec2f8a32020-05-07 13:43:05 +03001105 * @get_signal_cb_size: Get signal CB size.
1106 * @get_wait_cb_size: Get wait CB size.
1107 * @gen_signal_cb: Generate a signal CB.
1108 * @gen_wait_cb: Generate a wait CB.
1109 * @reset_sob: Reset a SOB.
Ofir Bitton5fe1c172020-09-10 10:10:55 +03001110 * @reset_sob_group: Reset SOB group
Oded Gabbaycb056b92020-03-29 13:18:30 +03001111 * @set_dma_mask_from_fw: set the DMA mask in the driver according to the
1112 * firmware configuration
Tomer Tayar25e7aeb2020-03-31 22:46:36 +03001113 * @get_device_time: Get the device time.
Ofir Bitton5fe1c172020-09-10 10:10:55 +03001114 * @collective_wait_init_cs: Generate collective master/slave packets
1115 * and place them in the relevant cs jobs
1116 * @collective_wait_create_jobs: allocate collective wait cs jobs
farah kassabri89473a12021-01-12 17:24:00 +02001117 * @scramble_addr: Routine to scramble the address prior of mapping it
Oded Gabbay28bcf1f2021-02-01 21:23:43 +02001118 * in the MMU.
farah kassabri89473a12021-01-12 17:24:00 +02001119 * @descramble_addr: Routine to de-scramble the address prior of
Oded Gabbay28bcf1f2021-02-01 21:23:43 +02001120 * showing it to users.
Ofir Bittond2b980f2021-01-07 12:14:17 +02001121 * @ack_protection_bits_errors: ack and dump all security violations
Ofir Bittond00697f2021-01-05 12:55:06 +02001122 * @get_hw_block_id: retrieve a HW block id to be used by the user to mmap it.
Oded Gabbay6df50d22021-02-05 16:04:34 +02001123 * also returns the size of the block if caller supplies
1124 * a valid pointer for it
Ofir Bittond00697f2021-01-05 12:55:06 +02001125 * @hw_block_mmap: mmap a HW block with a given id.
Oded Gabbay28bcf1f2021-02-01 21:23:43 +02001126 * @enable_events_from_fw: send interrupt to firmware to notify them the
1127 * driver is ready to receive asynchronous events. This
1128 * function should be called during the first init and
1129 * after every hard-reset of the device
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02001130 * @get_msi_info: Retrieve asic-specific MSI ID of the f/w async event
1131 * @map_pll_idx_to_fw_idx: convert driver specific per asic PLL index to
1132 * generic f/w compatible PLL Indexes
Bharat Jauhari4b099012021-05-28 13:14:34 +03001133 * @init_firmware_loader: initialize data for FW loader.
1134 * @init_cpu_scrambler_dram: Enable CPU specific DRAM scrambling
Yuri Nudelman938b7932021-06-06 10:28:51 +03001135 * @state_dump_init: initialize constants required for state dump
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001136 */
1137struct hl_asic_funcs {
1138 int (*early_init)(struct hl_device *hdev);
1139 int (*early_fini)(struct hl_device *hdev);
Oded Gabbayd91389b2019-02-16 00:39:19 +02001140 int (*late_init)(struct hl_device *hdev);
1141 void (*late_fini)(struct hl_device *hdev);
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001142 int (*sw_init)(struct hl_device *hdev);
1143 int (*sw_fini)(struct hl_device *hdev);
Oded Gabbay839c4802019-02-16 00:39:16 +02001144 int (*hw_init)(struct hl_device *hdev);
1145 void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
Oded Gabbay1251f232019-02-16 00:39:18 +02001146 void (*halt_engines)(struct hl_device *hdev, bool hard_reset);
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001147 int (*suspend)(struct hl_device *hdev);
1148 int (*resume)(struct hl_device *hdev);
Zvika Yehudai1ee8e2b2021-07-06 13:50:32 +03001149 int (*mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
Hillf Danton0db57532020-08-23 07:32:42 +08001150 void *cpu_addr, dma_addr_t dma_addr, size_t size);
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001151 void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
Oded Gabbayb9040c92019-08-08 15:45:58 +03001152 void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
1153 struct hl_bd *bd);
Oded Gabbayd9c3aa82019-05-01 11:47:04 +03001154 void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001155 dma_addr_t *dma_handle, gfp_t flag);
Oded Gabbayd9c3aa82019-05-01 11:47:04 +03001156 void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001157 void *cpu_addr, dma_addr_t dma_handle);
farah kassabri03df1362020-05-06 11:17:38 +03001158 int (*scrub_device_mem)(struct hl_device *hdev, u64 addr, u64 size);
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001159 void* (*get_int_queue_base)(struct hl_device *hdev, u32 queue_id,
1160 dma_addr_t *dma_handle, u16 *queue_len);
1161 int (*test_queues)(struct hl_device *hdev);
Oded Gabbayd9c3aa82019-05-01 11:47:04 +03001162 void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001163 gfp_t mem_flags, dma_addr_t *dma_handle);
Oded Gabbayd9c3aa82019-05-01 11:47:04 +03001164 void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001165 dma_addr_t dma_addr);
1166 void* (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev,
1167 size_t size, dma_addr_t *dma_handle);
1168 void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,
1169 size_t size, void *vaddr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001170 void (*hl_dma_unmap_sg)(struct hl_device *hdev,
Tomer Tayar94cb6692019-05-01 11:28:15 +03001171 struct scatterlist *sgl, int nents,
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001172 enum dma_data_direction dir);
1173 int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
1174 int (*asic_dma_map_sg)(struct hl_device *hdev,
Tomer Tayar94cb6692019-05-01 11:28:15 +03001175 struct scatterlist *sgl, int nents,
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001176 enum dma_data_direction dir);
1177 u32 (*get_dma_desc_list_size)(struct hl_device *hdev,
1178 struct sg_table *sgt);
Oded Gabbay921a4652019-05-12 16:53:16 +03001179 void (*add_end_of_cb_packets)(struct hl_device *hdev,
Arnd Bergmann82948e62020-10-26 17:08:06 +01001180 void *kernel_address, u32 len,
Oded Gabbay926ba4c2020-03-31 11:57:11 +03001181 u64 cq_addr, u32 cq_val, u32 msix_num,
1182 bool eb);
Oded Gabbay1251f232019-02-16 00:39:18 +02001183 void (*update_eq_ci)(struct hl_device *hdev, u32 val);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001184 int (*context_switch)(struct hl_device *hdev, u32 asid);
1185 void (*restore_phase_topology)(struct hl_device *hdev);
Ofir Bittona5778d12021-02-24 11:51:40 +02001186 int (*debugfs_read32)(struct hl_device *hdev, u64 addr,
1187 bool user_address, u32 *val);
1188 int (*debugfs_write32)(struct hl_device *hdev, u64 addr,
1189 bool user_address, u32 val);
1190 int (*debugfs_read64)(struct hl_device *hdev, u64 addr,
1191 bool user_address, u64 *val);
1192 int (*debugfs_write64)(struct hl_device *hdev, u64 addr,
1193 bool user_address, u64 val);
Oded Gabbay639781d2021-04-02 01:43:18 +03001194 int (*debugfs_read_dma)(struct hl_device *hdev, u64 addr, u32 size,
1195 void *blob_addr);
Oded Gabbayd91389b2019-02-16 00:39:19 +02001196 void (*add_device_attr)(struct hl_device *hdev,
1197 struct attribute_group *dev_attr_grp);
Oded Gabbay1251f232019-02-16 00:39:18 +02001198 void (*handle_eqe)(struct hl_device *hdev,
1199 struct hl_eq_entry *eq_entry);
Oded Gabbayd91389b2019-02-16 00:39:19 +02001200 void (*set_pll_profile)(struct hl_device *hdev,
1201 enum hl_pll_frequency freq);
Oded Gabbaye9730762019-08-28 21:51:52 +03001202 void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
1203 u32 *size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001204 u64 (*read_pte)(struct hl_device *hdev, u64 addr);
1205 void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001206 int (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard,
Omer Shpigelman7b6e4ea2019-11-14 18:23:53 +00001207 u32 flags);
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001208 int (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
Alon Mizrahi08c03a12021-04-08 15:30:59 +03001209 u32 flags, u32 asid, u64 va, u64 size);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001210 int (*send_heartbeat)(struct hl_device *hdev);
Oded Gabbaye38bfd32020-07-03 20:46:12 +03001211 void (*set_clock_gating)(struct hl_device *hdev);
Oded Gabbayca624332020-05-09 12:17:21 +03001212 void (*disable_clock_gating)(struct hl_device *hdev);
Omer Shpigelman315bc052019-04-01 22:31:22 +03001213 int (*debug_coresight)(struct hl_device *hdev, void *data);
Ohad Sharabicf303392021-01-17 16:01:56 +02001214 bool (*is_device_idle)(struct hl_device *hdev, u64 *mask_arr,
1215 u8 mask_len, struct seq_file *s);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02001216 int (*soft_reset_late_init)(struct hl_device *hdev);
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001217 void (*hw_queues_lock)(struct hl_device *hdev);
1218 void (*hw_queues_unlock)(struct hl_device *hdev);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +02001219 u32 (*get_pci_id)(struct hl_device *hdev);
Oded Gabbayd91389b2019-02-16 00:39:19 +02001220 int (*get_eeprom_data)(struct hl_device *hdev, void *data,
1221 size_t max_size);
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001222 int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
Alon Mizrahi439bc472020-11-10 13:49:10 +02001223 u16 len, u32 timeout, u64 *result);
Tomer Tayarb6f897d2019-03-05 16:48:42 +02001224 int (*pci_bars_map)(struct hl_device *hdev);
Tomer Tayarb6f897d2019-03-05 16:48:42 +02001225 int (*init_iatu)(struct hl_device *hdev);
Oded Gabbayb2377e02019-04-22 11:49:06 +03001226 u32 (*rreg)(struct hl_device *hdev, u32 reg);
1227 void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
Omer Shpigelman89225ce2019-05-01 14:38:38 +03001228 void (*halt_coresight)(struct hl_device *hdev);
Ofir Bittona04b7cd2020-07-13 13:36:55 +03001229 int (*ctx_init)(struct hl_ctx *ctx);
Ofir Bitton5de406c2020-09-10 10:56:26 +03001230 void (*ctx_fini)(struct hl_ctx *ctx);
Oded Gabbay62c1e122019-10-10 15:48:59 +03001231 int (*get_clk_rate)(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk);
Omer Shpigelman1fa185c2020-03-01 19:59:39 +02001232 u32 (*get_queue_id_for_cq)(struct hl_device *hdev, u32 cq_idx);
Oded Gabbay7e1c07d2020-03-26 12:32:56 +02001233 int (*load_firmware_to_device)(struct hl_device *hdev);
Ofir Bitton47f6b412020-04-16 13:47:15 +03001234 int (*load_boot_fit_to_device)(struct hl_device *hdev);
Omer Shpigelmanec2f8a32020-05-07 13:43:05 +03001235 u32 (*get_signal_cb_size)(struct hl_device *hdev);
1236 u32 (*get_wait_cb_size)(struct hl_device *hdev);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03001237 u32 (*gen_signal_cb)(struct hl_device *hdev, void *data, u16 sob_id,
Alon Mizrahi72ab9ca52020-12-02 19:55:30 +02001238 u32 size, bool eb);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03001239 u32 (*gen_wait_cb)(struct hl_device *hdev,
Ofir Bitton3cf74b32020-09-10 09:17:50 +03001240 struct hl_gen_wait_properties *prop);
Omer Shpigelmanec2f8a32020-05-07 13:43:05 +03001241 void (*reset_sob)(struct hl_device *hdev, void *data);
Ofir Bitton5fe1c172020-09-10 10:10:55 +03001242 void (*reset_sob_group)(struct hl_device *hdev, u16 sob_group);
Oded Gabbaycb056b92020-03-29 13:18:30 +03001243 void (*set_dma_mask_from_fw)(struct hl_device *hdev);
Tomer Tayar25e7aeb2020-03-31 22:46:36 +03001244 u64 (*get_device_time)(struct hl_device *hdev);
Ofir Bitton5fe1c172020-09-10 10:10:55 +03001245 void (*collective_wait_init_cs)(struct hl_cs *cs);
1246 int (*collective_wait_create_jobs)(struct hl_device *hdev,
1247 struct hl_ctx *ctx, struct hl_cs *cs, u32 wait_queue_id,
1248 u32 collective_engine_id);
farah kassabri89473a12021-01-12 17:24:00 +02001249 u64 (*scramble_addr)(struct hl_device *hdev, u64 addr);
1250 u64 (*descramble_addr)(struct hl_device *hdev, u64 addr);
Ofir Bittond2b980f2021-01-07 12:14:17 +02001251 void (*ack_protection_bits_errors)(struct hl_device *hdev);
Ofir Bittond00697f2021-01-05 12:55:06 +02001252 int (*get_hw_block_id)(struct hl_device *hdev, u64 block_addr,
Oded Gabbay6df50d22021-02-05 16:04:34 +02001253 u32 *block_size, u32 *block_id);
Ofir Bittond00697f2021-01-05 12:55:06 +02001254 int (*hw_block_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
1255 u32 block_id, u32 block_size);
Oded Gabbay28bcf1f2021-02-01 21:23:43 +02001256 void (*enable_events_from_fw)(struct hl_device *hdev);
Oded Gabbay90bd4792021-04-23 15:57:39 +03001257 void (*get_msi_info)(__le32 *table);
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02001258 int (*map_pll_idx_to_fw_idx)(u32 pll_idx);
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03001259 void (*init_firmware_loader)(struct hl_device *hdev);
Bharat Jauhari4b099012021-05-28 13:14:34 +03001260 void (*init_cpu_scrambler_dram)(struct hl_device *hdev);
Yuri Nudelman938b7932021-06-06 10:28:51 +03001261 void (*state_dump_init)(struct hl_device *hdev);
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001262};
Oded Gabbayc4d66342019-02-16 00:39:11 +02001263
Oded Gabbay0861e412019-02-16 00:39:14 +02001264
1265/*
1266 * CONTEXTS
1267 */
1268
1269#define HL_KERNEL_ASID_ID 0
1270
1271/**
Ofir Bitton784b9162020-10-22 11:05:55 +03001272 * enum hl_va_range_type - virtual address range type.
1273 * @HL_VA_RANGE_TYPE_HOST: range type of host pages
1274 * @HL_VA_RANGE_TYPE_HOST_HUGE: range type of host huge pages
1275 * @HL_VA_RANGE_TYPE_DRAM: range type of dram pages
1276 */
1277enum hl_va_range_type {
1278 HL_VA_RANGE_TYPE_HOST,
1279 HL_VA_RANGE_TYPE_HOST_HUGE,
1280 HL_VA_RANGE_TYPE_DRAM,
1281 HL_VA_RANGE_TYPE_MAX
1282};
1283
1284/**
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001285 * struct hl_va_range - virtual addresses range.
1286 * @lock: protects the virtual addresses list.
1287 * @list: list of virtual addresses blocks available for mappings.
1288 * @start_addr: range start address.
1289 * @end_addr: range end address.
Ofir Bitton784b9162020-10-22 11:05:55 +03001290 * @page_size: page size of this va range.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001291 */
1292struct hl_va_range {
1293 struct mutex lock;
1294 struct list_head list;
1295 u64 start_addr;
1296 u64 end_addr;
Ofir Bitton784b9162020-10-22 11:05:55 +03001297 u32 page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001298};
1299
1300/**
farah kassabrie7536432020-10-12 14:30:26 +03001301 * struct hl_cs_counters_atomic - command submission counters
1302 * @out_of_mem_drop_cnt: dropped due to memory allocation issue
1303 * @parsing_drop_cnt: dropped due to error in packet parsing
1304 * @queue_full_drop_cnt: dropped due to queue full
1305 * @device_in_reset_drop_cnt: dropped due to device in reset
1306 * @max_cs_in_flight_drop_cnt: dropped due to maximum CS in-flight
Alon Mizrahia3fd2832020-12-08 16:14:01 +02001307 * @validation_drop_cnt: dropped due to error in validation
farah kassabrie7536432020-10-12 14:30:26 +03001308 */
1309struct hl_cs_counters_atomic {
1310 atomic64_t out_of_mem_drop_cnt;
1311 atomic64_t parsing_drop_cnt;
1312 atomic64_t queue_full_drop_cnt;
1313 atomic64_t device_in_reset_drop_cnt;
1314 atomic64_t max_cs_in_flight_drop_cnt;
Alon Mizrahia3fd2832020-12-08 16:14:01 +02001315 atomic64_t validation_drop_cnt;
farah kassabrie7536432020-10-12 14:30:26 +03001316};
1317
1318/**
Ofir Bittond3f139c2020-11-18 15:46:57 +02001319 * struct hl_pending_cb - pending command buffer structure
1320 * @cb_node: cb node in pending cb list
1321 * @cb: command buffer to send in next submission
1322 * @cb_size: command buffer size
1323 * @hw_queue_id: destination queue id
1324 */
1325struct hl_pending_cb {
1326 struct list_head cb_node;
1327 struct hl_cb *cb;
1328 u32 cb_size;
1329 u32 hw_queue_id;
1330};
1331
1332/**
Oded Gabbay0861e412019-02-16 00:39:14 +02001333 * struct hl_ctx - user/kernel context.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001334 * @mem_hash: holds mapping from virtual address to virtual memory area
1335 * descriptor (hl_vm_phys_pg_list or hl_userptr).
Omer Shpigelman66542c32019-02-24 09:17:55 +02001336 * @mmu_shadow_hash: holds a mapping from shadow address to pgt_info structure.
Oded Gabbay4c172bb2019-08-30 16:59:33 +03001337 * @hpriv: pointer to the private (Kernel Driver) data of the process (fd).
Oded Gabbay0861e412019-02-16 00:39:14 +02001338 * @hdev: pointer to the device structure.
1339 * @refcount: reference counter for the context. Context is released only when
1340 * this hits 0l. It is incremented on CS and CS_WAIT.
Ofir Bittona98d73c2020-07-15 08:52:39 +03001341 * @cs_pending: array of hl fence objects representing pending CS.
Ofir Bitton784b9162020-10-22 11:05:55 +03001342 * @va_range: holds available virtual addresses for host and dram mappings.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001343 * @mem_hash_lock: protects the mem_hash.
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03001344 * @mmu_lock: protects the MMU page tables. Any change to the PGT, modifying the
1345 * MMU hash or walking the PGT requires talking this lock.
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001346 * @hw_block_list_lock: protects the HW block memory list.
Oded Gabbayc2164772019-02-16 00:39:24 +02001347 * @debugfs_list: node in debugfs list of contexts.
Ofir Bittond3f139c2020-11-18 15:46:57 +02001348 * pending_cb_list: list of pending command buffers waiting to be sent upon
1349 * next user command submission context.
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001350 * @hw_block_mem_list: list of HW block virtual mapped addresses.
farah kassabrie7536432020-10-12 14:30:26 +03001351 * @cs_counters: context command submission counters.
Tomer Tayaref6a0f62020-07-09 16:17:48 +03001352 * @cb_va_pool: device VA pool for command buffers which are mapped to the
1353 * device's MMU.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001354 * @cs_sequence: sequence number for CS. Value is assigned to a CS and passed
1355 * to user so user could inquire about CS. It is used as
1356 * index to cs_pending array.
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02001357 * @dram_default_hops: array that holds all hops addresses needed for default
1358 * DRAM mapping.
Ofir Bittond3f139c2020-11-18 15:46:57 +02001359 * @pending_cb_lock: spinlock to protect pending cb list
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001360 * @cs_lock: spinlock to protect cs_sequence.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001361 * @dram_phys_mem: amount of used physical DRAM memory by this context.
Oded Gabbay027d35d2019-04-25 20:15:42 +03001362 * @thread_ctx_switch_token: token to prevent multiple threads of the same
1363 * context from running the context switch phase.
1364 * Only a single thread should run it.
Ofir Bittond3f139c2020-11-18 15:46:57 +02001365 * @thread_pending_cb_token: token to prevent multiple threads from processing
1366 * the pending CB list. Only a single thread should
1367 * process the list since it is protected by a
1368 * spinlock and we don't want to halt the entire
1369 * command submission sequence.
Oded Gabbay027d35d2019-04-25 20:15:42 +03001370 * @thread_ctx_switch_wait_token: token to prevent the threads that didn't run
1371 * the context switch phase from moving to their
1372 * execution phase before the context switch phase
1373 * has finished.
Oded Gabbay0861e412019-02-16 00:39:14 +02001374 * @asid: context's unique address space ID in the device's MMU.
Oded Gabbayb8887512019-07-15 21:55:57 +03001375 * @handle: context's opaque handle for user
Oded Gabbay0861e412019-02-16 00:39:14 +02001376 */
1377struct hl_ctx {
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001378 DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
Omer Shpigelman66542c32019-02-24 09:17:55 +02001379 DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
farah kassabrie7536432020-10-12 14:30:26 +03001380 struct hl_fpriv *hpriv;
1381 struct hl_device *hdev;
1382 struct kref refcount;
1383 struct hl_fence **cs_pending;
Ofir Bitton784b9162020-10-22 11:05:55 +03001384 struct hl_va_range *va_range[HL_VA_RANGE_TYPE_MAX];
farah kassabrie7536432020-10-12 14:30:26 +03001385 struct mutex mem_hash_lock;
1386 struct mutex mmu_lock;
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001387 struct mutex hw_block_list_lock;
farah kassabrie7536432020-10-12 14:30:26 +03001388 struct list_head debugfs_list;
Ofir Bittond3f139c2020-11-18 15:46:57 +02001389 struct list_head pending_cb_list;
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001390 struct list_head hw_block_mem_list;
farah kassabrie7536432020-10-12 14:30:26 +03001391 struct hl_cs_counters_atomic cs_counters;
1392 struct gen_pool *cb_va_pool;
1393 u64 cs_sequence;
1394 u64 *dram_default_hops;
Ofir Bittond3f139c2020-11-18 15:46:57 +02001395 spinlock_t pending_cb_lock;
farah kassabrie7536432020-10-12 14:30:26 +03001396 spinlock_t cs_lock;
1397 atomic64_t dram_phys_mem;
1398 atomic_t thread_ctx_switch_token;
Ofir Bittond3f139c2020-11-18 15:46:57 +02001399 atomic_t thread_pending_cb_token;
farah kassabrie7536432020-10-12 14:30:26 +03001400 u32 thread_ctx_switch_wait_token;
1401 u32 asid;
1402 u32 handle;
Oded Gabbay0861e412019-02-16 00:39:14 +02001403};
1404
1405/**
1406 * struct hl_ctx_mgr - for handling multiple contexts.
1407 * @ctx_lock: protects ctx_handles.
1408 * @ctx_handles: idr to hold all ctx handles.
1409 */
1410struct hl_ctx_mgr {
1411 struct mutex ctx_lock;
1412 struct idr ctx_handles;
1413};
1414
1415
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001416
1417/*
1418 * COMMAND SUBMISSIONS
1419 */
1420
1421/**
1422 * struct hl_userptr - memory mapping chunk information
1423 * @vm_type: type of the VM.
1424 * @job_node: linked-list node for hanging the object on the Job's list.
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001425 * @pages: pointer to struct page array
1426 * @npages: size of @pages array
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001427 * @sgt: pointer to the scatter-gather table that holds the pages.
1428 * @dir: for DMA unmapping, the direction must be supplied, so save it.
1429 * @debugfs_list: node in debugfs list of command submissions.
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03001430 * @addr: user-space virtual address of the start of the memory area.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001431 * @size: size of the memory area to pin & map.
1432 * @dma_mapped: true if the SG was mapped to DMA addresses, false otherwise.
1433 */
1434struct hl_userptr {
Oded Gabbay82629c72021-06-29 18:08:05 +03001435 enum vm_type vm_type; /* must be first */
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001436 struct list_head job_node;
Daniel Vetterd4cb1922020-11-27 17:41:17 +01001437 struct page **pages;
1438 unsigned int npages;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001439 struct sg_table *sgt;
1440 enum dma_data_direction dir;
1441 struct list_head debugfs_list;
1442 u64 addr;
Oded Gabbay00ce0652021-06-29 18:12:46 +03001443 u64 size;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001444 u8 dma_mapped;
1445};
1446
1447/**
1448 * struct hl_cs - command submission.
1449 * @jobs_in_queue_cnt: per each queue, maintain counter of submitted jobs.
1450 * @ctx: the context this CS belongs to.
1451 * @job_list: list of the CS's jobs in the various queues.
1452 * @job_lock: spinlock for the CS's jobs list. Needed for free_job.
1453 * @refcount: reference counter for usage of the CS.
1454 * @fence: pointer to the fence object of this CS.
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +03001455 * @signal_fence: pointer to the fence object of the signal CS (used by wait
1456 * CS only).
Omer Shpigelmanb75f2252020-05-07 14:31:49 +03001457 * @finish_work: workqueue object to run when CS is completed by H/W.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001458 * @work_tdr: delayed work node for TDR.
1459 * @mirror_node : node in device mirror list of command submissions.
Ofir Bitton2795c882020-12-08 13:47:05 +02001460 * @staged_cs_node: node in the staged cs list.
Oded Gabbayc2164772019-02-16 00:39:24 +02001461 * @debugfs_list: node in debugfs list of command submissions.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001462 * @sequence: the sequence number of this CS.
Ofir Bitton2795c882020-12-08 13:47:05 +02001463 * @staged_sequence: the sequence of the staged submission this CS is part of,
1464 * relevant only if staged_cs is set.
Alon Mizrahicf393952021-02-22 15:53:24 +02001465 * @timeout_jiffies: cs timeout in jiffies.
Yuri Nudelman8e8125f2021-05-25 14:49:52 +03001466 * @submission_time_jiffies: submission time of the cs
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +03001467 * @type: CS_TYPE_*.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001468 * @submitted: true if CS was submitted to H/W.
1469 * @completed: true if CS was completed by device.
1470 * @timedout : true if CS was timedout.
1471 * @tdr_active: true if TDR was activated for this CS (to prevent
1472 * double TDR activation).
1473 * @aborted: true if CS was aborted due to some device error.
Ofir Bitton0811b392020-12-06 17:18:02 +02001474 * @timestamp: true if a timestmap must be captured upon completion.
1475 * @staged_last: true if this is the last staged CS and needs completion.
1476 * @staged_first: true if this is the first staged CS and we need to receive
1477 * timeout for this CS.
1478 * @staged_cs: true if this CS is part of a staged submission.
Yuri Nudelman8e8125f2021-05-25 14:49:52 +03001479 * @skip_reset_on_timeout: true if we shall not reset the device in case
1480 * timeout occurs (debug scenario).
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001481 */
1482struct hl_cs {
Ofir Bitton3abc99b2020-06-23 14:50:39 +03001483 u16 *jobs_in_queue_cnt;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001484 struct hl_ctx *ctx;
1485 struct list_head job_list;
1486 spinlock_t job_lock;
1487 struct kref refcount;
Ofir Bittona98d73c2020-07-15 08:52:39 +03001488 struct hl_fence *fence;
1489 struct hl_fence *signal_fence;
Omer Shpigelmanb75f2252020-05-07 14:31:49 +03001490 struct work_struct finish_work;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001491 struct delayed_work work_tdr;
1492 struct list_head mirror_node;
Ofir Bitton2795c882020-12-08 13:47:05 +02001493 struct list_head staged_cs_node;
Oded Gabbayc2164772019-02-16 00:39:24 +02001494 struct list_head debugfs_list;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001495 u64 sequence;
Ofir Bitton2795c882020-12-08 13:47:05 +02001496 u64 staged_sequence;
Alon Mizrahicf393952021-02-22 15:53:24 +02001497 u64 timeout_jiffies;
Yuri Nudelman8e8125f2021-05-25 14:49:52 +03001498 u64 submission_time_jiffies;
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +03001499 enum hl_cs_type type;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001500 u8 submitted;
1501 u8 completed;
1502 u8 timedout;
1503 u8 tdr_active;
1504 u8 aborted;
Ofir Bittonbd2f4772020-11-10 17:26:22 +02001505 u8 timestamp;
Ofir Bitton0811b392020-12-06 17:18:02 +02001506 u8 staged_last;
1507 u8 staged_first;
1508 u8 staged_cs;
Yuri Nudelman8e8125f2021-05-25 14:49:52 +03001509 u8 skip_reset_on_timeout;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001510};
1511
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001512/**
1513 * struct hl_cs_job - command submission job.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001514 * @cs_node: the node to hang on the CS jobs list.
1515 * @cs: the CS this job belongs to.
1516 * @user_cb: the CB we got from the user.
1517 * @patched_cb: in case of patching, this is internal CB which is submitted on
1518 * the queue instead of the CB we got from the IOCTL.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001519 * @finish_work: workqueue object to run when job is completed.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001520 * @userptr_list: linked-list of userptr mappings that belong to this job and
1521 * wait for completion.
Oded Gabbayc2164772019-02-16 00:39:24 +02001522 * @debugfs_list: node in debugfs list of command submission jobs.
Tomer Tayar649c4592020-08-10 17:30:35 +03001523 * @refcount: reference counter for usage of the CS job.
Tomer Tayarcb596ae2019-10-03 15:22:36 +00001524 * @queue_type: the type of the H/W queue this job is submitted to.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001525 * @id: the id of this job inside a CS.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001526 * @hw_queue_id: the id of the H/W queue this job is submitted to.
1527 * @user_cb_size: the actual size of the CB we got from the user.
1528 * @job_cb_size: the actual size of the CB that we put on the queue.
Tomer Tayarcb596ae2019-10-03 15:22:36 +00001529 * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
1530 * handle to a kernel-allocated CB object, false
1531 * otherwise (SRAM/DRAM/host address).
Oded Gabbay926ba4c2020-03-31 11:57:11 +03001532 * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
1533 * info is needed later, when adding the 2xMSG_PROT at the
1534 * end of the JOB, to know which barriers to put in the
1535 * MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
1536 * have streams so the engine can't be busy by another
1537 * stream.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001538 */
1539struct hl_cs_job {
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001540 struct list_head cs_node;
1541 struct hl_cs *cs;
1542 struct hl_cb *user_cb;
1543 struct hl_cb *patched_cb;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001544 struct work_struct finish_work;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001545 struct list_head userptr_list;
Oded Gabbayc2164772019-02-16 00:39:24 +02001546 struct list_head debugfs_list;
Tomer Tayar649c4592020-08-10 17:30:35 +03001547 struct kref refcount;
Tomer Tayarcb596ae2019-10-03 15:22:36 +00001548 enum hl_queue_type queue_type;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001549 u32 id;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001550 u32 hw_queue_id;
1551 u32 user_cb_size;
1552 u32 job_cb_size;
Tomer Tayarcb596ae2019-10-03 15:22:36 +00001553 u8 is_kernel_allocated_cb;
Oded Gabbay926ba4c2020-03-31 11:57:11 +03001554 u8 contains_dma_pkt;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001555};
1556
1557/**
Tomer Tayarf4356142019-10-02 13:53:52 +00001558 * struct hl_cs_parser - command submission parser properties.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001559 * @user_cb: the CB we got from the user.
1560 * @patched_cb: in case of patching, this is internal CB which is submitted on
1561 * the queue instead of the CB we got from the IOCTL.
1562 * @job_userptr_list: linked-list of userptr mappings that belong to the related
1563 * job and wait for completion.
1564 * @cs_sequence: the sequence number of the related CS.
Tomer Tayarcb596ae2019-10-03 15:22:36 +00001565 * @queue_type: the type of the H/W queue this job is submitted to.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001566 * @ctx_id: the ID of the context the related CS belongs to.
1567 * @hw_queue_id: the id of the H/W queue this job is submitted to.
1568 * @user_cb_size: the actual size of the CB we got from the user.
1569 * @patched_cb_size: the size of the CB after parsing.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001570 * @job_id: the id of the related job inside the related CS.
Tomer Tayarcb596ae2019-10-03 15:22:36 +00001571 * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
1572 * handle to a kernel-allocated CB object, false
1573 * otherwise (SRAM/DRAM/host address).
Oded Gabbay926ba4c2020-03-31 11:57:11 +03001574 * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
1575 * info is needed later, when adding the 2xMSG_PROT at the
1576 * end of the JOB, to know which barriers to put in the
1577 * MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
1578 * have streams so the engine can't be busy by another
1579 * stream.
Ofir Bittonac6fdbf2020-12-03 16:59:28 +02001580 * @completion: true if we need completion for this CS.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001581 */
1582struct hl_cs_parser {
1583 struct hl_cb *user_cb;
1584 struct hl_cb *patched_cb;
1585 struct list_head *job_userptr_list;
1586 u64 cs_sequence;
Tomer Tayarcb596ae2019-10-03 15:22:36 +00001587 enum hl_queue_type queue_type;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001588 u32 ctx_id;
1589 u32 hw_queue_id;
1590 u32 user_cb_size;
1591 u32 patched_cb_size;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001592 u8 job_id;
Tomer Tayarcb596ae2019-10-03 15:22:36 +00001593 u8 is_kernel_allocated_cb;
Oded Gabbay926ba4c2020-03-31 11:57:11 +03001594 u8 contains_dma_pkt;
Ofir Bittonac6fdbf2020-12-03 16:59:28 +02001595 u8 completion;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02001596};
Oded Gabbayd91389b2019-02-16 00:39:19 +02001597
Oded Gabbayc4d66342019-02-16 00:39:11 +02001598/*
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001599 * MEMORY STRUCTURE
1600 */
1601
1602/**
1603 * struct hl_vm_hash_node - hash element from virtual address to virtual
1604 * memory area descriptor (hl_vm_phys_pg_list or
1605 * hl_userptr).
1606 * @node: node to hang on the hash table in context object.
1607 * @vaddr: key virtual address.
1608 * @ptr: value pointer (hl_vm_phys_pg_list or hl_userptr).
1609 */
1610struct hl_vm_hash_node {
1611 struct hlist_node node;
1612 u64 vaddr;
1613 void *ptr;
1614};
1615
1616/**
Sagiv Ozeria4371c12021-02-23 11:01:08 +02001617 * struct hl_vm_hw_block_list_node - list element from user virtual address to
1618 * HW block id.
1619 * @node: node to hang on the list in context object.
1620 * @ctx: the context this node belongs to.
1621 * @vaddr: virtual address of the HW block.
1622 * @size: size of the block.
1623 * @id: HW block id (handle).
1624 */
1625struct hl_vm_hw_block_list_node {
1626 struct list_head node;
1627 struct hl_ctx *ctx;
1628 unsigned long vaddr;
1629 u32 size;
1630 u32 id;
1631};
1632
1633/**
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001634 * struct hl_vm_phys_pg_pack - physical page pack.
1635 * @vm_type: describes the type of the virtual area descriptor.
1636 * @pages: the physical page array.
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +02001637 * @npages: num physical pages in the pack.
1638 * @total_size: total size of all the pages in this list.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001639 * @mapping_cnt: number of shared mappings.
1640 * @asid: the context related to this list.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001641 * @page_size: size of each page in the pack.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001642 * @flags: HL_MEM_* flags related to this list.
1643 * @handle: the provided handle related to this list.
1644 * @offset: offset from the first page.
1645 * @contiguous: is contiguous physical memory.
1646 * @created_from_userptr: is product of host virtual address.
1647 */
1648struct hl_vm_phys_pg_pack {
Oded Gabbay82629c72021-06-29 18:08:05 +03001649 enum vm_type vm_type; /* must be first */
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001650 u64 *pages;
Omer Shpigelmanbfb1ce12019-03-05 10:59:16 +02001651 u64 npages;
1652 u64 total_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001653 atomic_t mapping_cnt;
1654 u32 asid;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001655 u32 page_size;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001656 u32 flags;
1657 u32 handle;
1658 u32 offset;
1659 u8 contiguous;
1660 u8 created_from_userptr;
1661};
1662
1663/**
1664 * struct hl_vm_va_block - virtual range block information.
1665 * @node: node to hang on the virtual range list in context object.
1666 * @start: virtual range start address.
1667 * @end: virtual range end address.
1668 * @size: virtual range size.
1669 */
1670struct hl_vm_va_block {
1671 struct list_head node;
1672 u64 start;
1673 u64 end;
1674 u64 size;
1675};
1676
1677/**
1678 * struct hl_vm - virtual memory manager for MMU.
1679 * @dram_pg_pool: pool for DRAM physical pages of 2MB.
1680 * @dram_pg_pool_refcount: reference counter for the pool usage.
1681 * @idr_lock: protects the phys_pg_list_handles.
1682 * @phys_pg_pack_handles: idr to hold all device allocations handles.
1683 * @init_done: whether initialization was done. We need this because VM
1684 * initialization might be skipped during device initialization.
1685 */
1686struct hl_vm {
1687 struct gen_pool *dram_pg_pool;
1688 struct kref dram_pg_pool_refcount;
1689 spinlock_t idr_lock;
1690 struct idr phys_pg_pack_handles;
1691 u8 init_done;
1692};
1693
Omer Shpigelman315bc052019-04-01 22:31:22 +03001694
1695/*
1696 * DEBUG, PROFILING STRUCTURE
1697 */
1698
1699/**
1700 * struct hl_debug_params - Coresight debug parameters.
1701 * @input: pointer to component specific input parameters.
1702 * @output: pointer to component specific output parameters.
1703 * @output_size: size of output buffer.
1704 * @reg_idx: relevant register ID.
1705 * @op: component operation to execute.
1706 * @enable: true if to enable component debugging, false otherwise.
1707 */
1708struct hl_debug_params {
1709 void *input;
1710 void *output;
1711 u32 output_size;
1712 u32 reg_idx;
1713 u32 op;
1714 bool enable;
1715};
1716
Omer Shpigelman0feaf862019-02-16 00:39:22 +02001717/*
Oded Gabbayc4d66342019-02-16 00:39:11 +02001718 * FILE PRIVATE STRUCTURE
1719 */
1720
1721/**
1722 * struct hl_fpriv - process information stored in FD private data.
1723 * @hdev: habanalabs device structure.
1724 * @filp: pointer to the given file structure.
1725 * @taskpid: current process ID.
Oded Gabbay86d53072019-07-30 11:49:36 +03001726 * @ctx: current executing context. TODO: remove for multiple ctx per process
Oded Gabbay0861e412019-02-16 00:39:14 +02001727 * @ctx_mgr: context manager to handle multiple context for this FD.
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001728 * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
Oded Gabbayc2164772019-02-16 00:39:24 +02001729 * @debugfs_list: list of relevant ASIC debugfs.
Oded Gabbayeb7caf82019-07-30 11:56:09 +03001730 * @dev_node: node in the device list of file private data
Oded Gabbayc4d66342019-02-16 00:39:11 +02001731 * @refcount: number of related contexts.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001732 * @restore_phase_mutex: lock for context switch and restore phase.
Oded Gabbay4d6a7752019-07-30 09:10:50 +03001733 * @is_control: true for control device, false otherwise
Oded Gabbayc4d66342019-02-16 00:39:11 +02001734 */
1735struct hl_fpriv {
1736 struct hl_device *hdev;
1737 struct file *filp;
1738 struct pid *taskpid;
Oded Gabbay86d53072019-07-30 11:49:36 +03001739 struct hl_ctx *ctx;
Oded Gabbay0861e412019-02-16 00:39:14 +02001740 struct hl_ctx_mgr ctx_mgr;
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001741 struct hl_cb_mgr cb_mgr;
Oded Gabbayc2164772019-02-16 00:39:24 +02001742 struct list_head debugfs_list;
Oded Gabbayeb7caf82019-07-30 11:56:09 +03001743 struct list_head dev_node;
Oded Gabbayc4d66342019-02-16 00:39:11 +02001744 struct kref refcount;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02001745 struct mutex restore_phase_mutex;
Oded Gabbay4d6a7752019-07-30 09:10:50 +03001746 u8 is_control;
Oded Gabbayc4d66342019-02-16 00:39:11 +02001747};
1748
1749
1750/*
Oded Gabbayc2164772019-02-16 00:39:24 +02001751 * DebugFS
1752 */
1753
1754/**
1755 * struct hl_info_list - debugfs file ops.
1756 * @name: file name.
1757 * @show: function to output information.
1758 * @write: function to write to the file.
1759 */
1760struct hl_info_list {
1761 const char *name;
1762 int (*show)(struct seq_file *s, void *data);
1763 ssize_t (*write)(struct file *file, const char __user *buf,
1764 size_t count, loff_t *f_pos);
1765};
1766
1767/**
1768 * struct hl_debugfs_entry - debugfs dentry wrapper.
Oded Gabbayc2164772019-02-16 00:39:24 +02001769 * @info_ent: dentry realted ops.
1770 * @dev_entry: ASIC specific debugfs manager.
1771 */
1772struct hl_debugfs_entry {
Oded Gabbayc2164772019-02-16 00:39:24 +02001773 const struct hl_info_list *info_ent;
1774 struct hl_dbg_device_entry *dev_entry;
1775};
1776
1777/**
1778 * struct hl_dbg_device_entry - ASIC specific debugfs manager.
1779 * @root: root dentry.
1780 * @hdev: habanalabs device structure.
1781 * @entry_arr: array of available hl_debugfs_entry.
1782 * @file_list: list of available debugfs files.
1783 * @file_mutex: protects file_list.
1784 * @cb_list: list of available CBs.
1785 * @cb_spinlock: protects cb_list.
1786 * @cs_list: list of available CSs.
1787 * @cs_spinlock: protects cs_list.
1788 * @cs_job_list: list of available CB jobs.
1789 * @cs_job_spinlock: protects cs_job_list.
1790 * @userptr_list: list of available userptrs (virtual memory chunk descriptor).
1791 * @userptr_spinlock: protects userptr_list.
1792 * @ctx_mem_hash_list: list of available contexts with MMU mappings.
1793 * @ctx_mem_hash_spinlock: protects cb_list.
Oded Gabbay639781d2021-04-02 01:43:18 +03001794 * @blob_desc: descriptor of blob
Yuri Nudelman938b7932021-06-06 10:28:51 +03001795 * @state_dump: data of the system states in case of a bad cs.
1796 * @state_dump_sem: protects state_dump.
Oded Gabbayc2164772019-02-16 00:39:24 +02001797 * @addr: next address to read/write from/to in read/write32.
1798 * @mmu_addr: next virtual address to translate to physical address in mmu_show.
1799 * @mmu_asid: ASID to use while translating in mmu_show.
Yuri Nudelman938b7932021-06-06 10:28:51 +03001800 * @state_dump_head: index of the latest state dump
Oded Gabbayc2164772019-02-16 00:39:24 +02001801 * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
Oded Gabbay639781d2021-04-02 01:43:18 +03001802 * @i2c_addr: generic u8 debugfs file for address value to use in i2c_data_read.
1803 * @i2c_reg: generic u8 debugfs file for register value to use in i2c_data_read.
Oded Gabbayc2164772019-02-16 00:39:24 +02001804 */
1805struct hl_dbg_device_entry {
1806 struct dentry *root;
1807 struct hl_device *hdev;
1808 struct hl_debugfs_entry *entry_arr;
1809 struct list_head file_list;
1810 struct mutex file_mutex;
1811 struct list_head cb_list;
1812 spinlock_t cb_spinlock;
1813 struct list_head cs_list;
1814 spinlock_t cs_spinlock;
1815 struct list_head cs_job_list;
1816 spinlock_t cs_job_spinlock;
1817 struct list_head userptr_list;
1818 spinlock_t userptr_spinlock;
1819 struct list_head ctx_mem_hash_list;
1820 spinlock_t ctx_mem_hash_spinlock;
Oded Gabbay639781d2021-04-02 01:43:18 +03001821 struct debugfs_blob_wrapper blob_desc;
Yuri Nudelman938b7932021-06-06 10:28:51 +03001822 char *state_dump[HL_STATE_DUMP_HIST_LEN];
1823 struct rw_semaphore state_dump_sem;
Oded Gabbayc2164772019-02-16 00:39:24 +02001824 u64 addr;
1825 u64 mmu_addr;
1826 u32 mmu_asid;
Yuri Nudelman938b7932021-06-06 10:28:51 +03001827 u32 state_dump_head;
Oded Gabbayc2164772019-02-16 00:39:24 +02001828 u8 i2c_bus;
1829 u8 i2c_addr;
1830 u8 i2c_reg;
1831};
1832
Yuri Nudelman938b7932021-06-06 10:28:51 +03001833/**
1834 * struct hl_hw_obj_name_entry - single hw object name, member of
1835 * hl_state_dump_specs
1836 * @node: link to the containing hash table
1837 * @name: hw object name
1838 * @id: object identifier
1839 */
1840struct hl_hw_obj_name_entry {
1841 struct hlist_node node;
1842 const char *name;
1843 u32 id;
1844};
1845
1846enum hl_state_dump_specs_props {
1847 SP_SYNC_OBJ_BASE_ADDR,
1848 SP_NEXT_SYNC_OBJ_ADDR,
1849 SP_SYNC_OBJ_AMOUNT,
1850 SP_MON_OBJ_WR_ADDR_LOW,
1851 SP_MON_OBJ_WR_ADDR_HIGH,
1852 SP_MON_OBJ_WR_DATA,
1853 SP_MON_OBJ_ARM_DATA,
1854 SP_MON_OBJ_STATUS,
1855 SP_MONITORS_AMOUNT,
1856 SP_TPC0_CMDQ,
1857 SP_TPC0_CFG_SO,
1858 SP_NEXT_TPC,
1859 SP_MME_CMDQ,
1860 SP_MME_CFG_SO,
1861 SP_NEXT_MME,
1862 SP_DMA_CMDQ,
1863 SP_DMA_CFG_SO,
1864 SP_DMA_QUEUES_OFFSET,
1865 SP_NUM_OF_MME_ENGINES,
1866 SP_SUB_MME_ENG_NUM,
1867 SP_NUM_OF_DMA_ENGINES,
1868 SP_NUM_OF_TPC_ENGINES,
1869 SP_ENGINE_NUM_OF_QUEUES,
1870 SP_ENGINE_NUM_OF_STREAMS,
1871 SP_ENGINE_NUM_OF_FENCES,
1872 SP_FENCE0_CNT_OFFSET,
1873 SP_FENCE0_RDATA_OFFSET,
1874 SP_CP_STS_OFFSET,
1875 SP_NUM_CORES,
1876
1877 SP_MAX
1878};
1879
1880enum hl_sync_engine_type {
1881 ENGINE_TPC,
1882 ENGINE_DMA,
1883 ENGINE_MME,
1884};
1885
1886/**
Yuri Nudelmanfd2010b2021-06-09 14:04:26 +03001887 * struct hl_mon_state_dump - represents a state dump of a single monitor
1888 * @id: monitor id
1889 * @wr_addr_low: address monitor will write to, low bits
1890 * @wr_addr_high: address monitor will write to, high bits
1891 * @wr_data: data monitor will write
1892 * @arm_data: register value containing monitor configuration
1893 * @status: monitor status
1894 */
1895struct hl_mon_state_dump {
1896 u32 id;
1897 u32 wr_addr_low;
1898 u32 wr_addr_high;
1899 u32 wr_data;
1900 u32 arm_data;
1901 u32 status;
1902};
1903
1904/**
Yuri Nudelman938b7932021-06-06 10:28:51 +03001905 * struct hl_sync_to_engine_map_entry - sync object id to engine mapping entry
1906 * @engine_type: type of the engine
1907 * @engine_id: id of the engine
1908 * @sync_id: id of the sync object
1909 */
1910struct hl_sync_to_engine_map_entry {
1911 struct hlist_node node;
1912 enum hl_sync_engine_type engine_type;
1913 u32 engine_id;
1914 u32 sync_id;
1915};
1916
1917/**
1918 * struct hl_sync_to_engine_map - maps sync object id to associated engine id
1919 * @tb: hash table containing the mapping, each element is of type
1920 * struct hl_sync_to_engine_map_entry
1921 */
1922struct hl_sync_to_engine_map {
1923 DECLARE_HASHTABLE(tb, SYNC_TO_ENGINE_HASH_TABLE_BITS);
1924};
1925
1926/**
1927 * struct hl_state_dump_specs_funcs - virtual functions used by the state dump
1928 * @gen_sync_to_engine_map: generate a hash map from sync obj id to its engine
Yuri Nudelmanfd2010b2021-06-09 14:04:26 +03001929 * @print_single_monitor: format monitor data as string
1930 * @monitor_valid: return true if given monitor dump is valid
1931 * @print_fences_single_engine: format fences data as string
Yuri Nudelman938b7932021-06-06 10:28:51 +03001932 */
1933struct hl_state_dump_specs_funcs {
1934 int (*gen_sync_to_engine_map)(struct hl_device *hdev,
1935 struct hl_sync_to_engine_map *map);
Yuri Nudelmanfd2010b2021-06-09 14:04:26 +03001936 int (*print_single_monitor)(char **buf, size_t *size, size_t *offset,
1937 struct hl_device *hdev,
1938 struct hl_mon_state_dump *mon);
1939 int (*monitor_valid)(struct hl_mon_state_dump *mon);
1940 int (*print_fences_single_engine)(struct hl_device *hdev,
1941 u64 base_offset,
1942 u64 status_base_offset,
1943 enum hl_sync_engine_type engine_type,
1944 u32 engine_id, char **buf,
1945 size_t *size, size_t *offset);
Yuri Nudelman938b7932021-06-06 10:28:51 +03001946};
1947
1948/**
1949 * struct hl_state_dump_specs - defines ASIC known hw objects names
1950 * @so_id_to_str_tb: sync objects names index table
1951 * @monitor_id_to_str_tb: monitors names index table
1952 * @funcs: virtual functions used for state dump
1953 * @sync_namager_names: readable names for sync manager if available (ex: N_E)
1954 * @props: pointer to a per asic const props array required for state dump
1955 */
1956struct hl_state_dump_specs {
1957 DECLARE_HASHTABLE(so_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
1958 DECLARE_HASHTABLE(monitor_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
1959 struct hl_state_dump_specs_funcs funcs;
1960 const char * const *sync_namager_names;
1961 s64 *props;
1962};
1963
Oded Gabbayc2164772019-02-16 00:39:24 +02001964
1965/*
Oded Gabbayc4d66342019-02-16 00:39:11 +02001966 * DEVICES
1967 */
1968
Ofir Bitton66a76402020-10-05 14:40:10 +03001969#define HL_STR_MAX 32
1970
1971#define HL_DEV_STS_MAX (HL_DEVICE_STATUS_NEEDS_RESET + 1)
1972
Oded Gabbayc4d66342019-02-16 00:39:11 +02001973/* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
Oded Gabbay4d6a7752019-07-30 09:10:50 +03001974 * x16 cards. In extreme cases, there are hosts that can accommodate 16 cards.
Oded Gabbayc4d66342019-02-16 00:39:11 +02001975 */
1976#define HL_MAX_MINORS 256
1977
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001978/*
1979 * Registers read & write functions.
1980 */
1981
1982u32 hl_rreg(struct hl_device *hdev, u32 reg);
1983void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
1984
Oded Gabbayb2377e02019-04-22 11:49:06 +03001985#define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
1986#define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001987#define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n", \
Oded Gabbayb2377e02019-04-22 11:49:06 +03001988 hdev->asic_funcs->rreg(hdev, (reg)))
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02001989
1990#define WREG32_P(reg, val, mask) \
1991 do { \
1992 u32 tmp_ = RREG32(reg); \
1993 tmp_ &= (mask); \
1994 tmp_ |= ((val) & ~(mask)); \
1995 WREG32(reg, tmp_); \
1996 } while (0)
1997#define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
1998#define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
1999
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002000#define RMWREG32(reg, val, mask) \
2001 do { \
2002 u32 tmp_ = RREG32(reg); \
2003 tmp_ &= ~(mask); \
2004 tmp_ |= ((val) << __ffs(mask)); \
2005 WREG32(reg, tmp_); \
2006 } while (0)
2007
2008#define RREG32_MASK(reg, mask) ((RREG32(reg) & mask) >> __ffs(mask))
2009
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002010#define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
2011#define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
Oded Gabbay8fdacf22019-10-02 14:14:08 +03002012#define WREG32_FIELD(reg, offset, field, val) \
2013 WREG32(mm##reg + offset, (RREG32(mm##reg + offset) & \
2014 ~REG_FIELD_MASK(reg, field)) | \
2015 (val) << REG_FIELD_SHIFT(reg, field))
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002016
Oded Gabbayed0fc502019-07-18 11:14:37 +03002017/* Timeout should be longer when working with simulator but cap the
2018 * increased timeout to some maximum
2019 */
Oded Gabbayb2377e02019-04-22 11:49:06 +03002020#define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
2021({ \
Dalit Ben Zoorb1b53772019-04-30 17:18:51 +03002022 ktime_t __timeout; \
Dalit Ben Zoorb1b53772019-04-30 17:18:51 +03002023 if (hdev->pdev) \
2024 __timeout = ktime_add_us(ktime_get(), timeout_us); \
2025 else \
Oded Gabbayed0fc502019-07-18 11:14:37 +03002026 __timeout = ktime_add_us(ktime_get(),\
2027 min((u64)(timeout_us * 10), \
2028 (u64) HL_SIM_MAX_TIMEOUT_US)); \
Oded Gabbayb2377e02019-04-22 11:49:06 +03002029 might_sleep_if(sleep_us); \
2030 for (;;) { \
2031 (val) = RREG32(addr); \
2032 if (cond) \
2033 break; \
2034 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2035 (val) = RREG32(addr); \
2036 break; \
2037 } \
2038 if (sleep_us) \
2039 usleep_range((sleep_us >> 2) + 1, sleep_us); \
2040 } \
2041 (cond) ? 0 : -ETIMEDOUT; \
2042})
2043
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002044/*
2045 * address in this macro points always to a memory location in the
2046 * host's (server's) memory. That location is updated asynchronously
Ben Segal2aa4e412019-07-18 12:27:00 +00002047 * either by the direct access of the device or by another core.
2048 *
2049 * To work both in LE and BE architectures, we need to distinguish between the
2050 * two states (device or another core updates the memory location). Therefore,
2051 * if mem_written_by_device is true, the host memory being polled will be
2052 * updated directly by the device. If false, the host memory being polled will
2053 * be updated by host CPU. Required so host knows whether or not the memory
2054 * might need to be byte-swapped before returning value to caller.
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002055 */
Ben Segal2aa4e412019-07-18 12:27:00 +00002056#define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
2057 mem_written_by_device) \
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002058({ \
2059 ktime_t __timeout; \
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002060 if (hdev->pdev) \
2061 __timeout = ktime_add_us(ktime_get(), timeout_us); \
2062 else \
Oded Gabbayed0fc502019-07-18 11:14:37 +03002063 __timeout = ktime_add_us(ktime_get(),\
2064 min((u64)(timeout_us * 10), \
2065 (u64) HL_SIM_MAX_TIMEOUT_US)); \
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002066 might_sleep_if(sleep_us); \
2067 for (;;) { \
2068 /* Verify we read updates done by other cores or by device */ \
2069 mb(); \
Arnd Bergmann82948e62020-10-26 17:08:06 +01002070 (val) = *((u32 *)(addr)); \
Ben Segal2aa4e412019-07-18 12:27:00 +00002071 if (mem_written_by_device) \
Oded Gabbay6dc66f72019-09-03 11:33:55 +03002072 (val) = le32_to_cpu(*(__le32 *) &(val)); \
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002073 if (cond) \
2074 break; \
2075 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
Arnd Bergmann82948e62020-10-26 17:08:06 +01002076 (val) = *((u32 *)(addr)); \
Ben Segal2aa4e412019-07-18 12:27:00 +00002077 if (mem_written_by_device) \
Oded Gabbay6dc66f72019-09-03 11:33:55 +03002078 (val) = le32_to_cpu(*(__le32 *) &(val)); \
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002079 break; \
2080 } \
2081 if (sleep_us) \
2082 usleep_range((sleep_us >> 2) + 1, sleep_us); \
2083 } \
2084 (cond) ? 0 : -ETIMEDOUT; \
2085})
2086
2087#define hl_poll_timeout_device_memory(hdev, addr, val, cond, sleep_us, \
2088 timeout_us) \
2089({ \
2090 ktime_t __timeout; \
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002091 if (hdev->pdev) \
2092 __timeout = ktime_add_us(ktime_get(), timeout_us); \
2093 else \
Oded Gabbayed0fc502019-07-18 11:14:37 +03002094 __timeout = ktime_add_us(ktime_get(),\
2095 min((u64)(timeout_us * 10), \
2096 (u64) HL_SIM_MAX_TIMEOUT_US)); \
Oded Gabbaya08b51a2019-05-09 01:48:23 +03002097 might_sleep_if(sleep_us); \
2098 for (;;) { \
2099 (val) = readl(addr); \
2100 if (cond) \
2101 break; \
2102 if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
2103 (val) = readl(addr); \
2104 break; \
2105 } \
2106 if (sleep_us) \
2107 usleep_range((sleep_us >> 2) + 1, sleep_us); \
2108 } \
2109 (cond) ? 0 : -ETIMEDOUT; \
2110})
Oded Gabbayb2377e02019-04-22 11:49:06 +03002111
Oded Gabbayd91389b2019-02-16 00:39:19 +02002112struct hwmon_chip_info;
2113
Oded Gabbayc4d66342019-02-16 00:39:11 +02002114/**
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002115 * struct hl_device_reset_work - reset workqueue task wrapper.
Ofir Bittonadb51292020-10-08 10:27:42 +03002116 * @wq: work queue for device reset procedure.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002117 * @reset_work: reset work to be done.
2118 * @hdev: habanalabs device structure.
2119 */
2120struct hl_device_reset_work {
Ofir Bittonadb51292020-10-08 10:27:42 +03002121 struct workqueue_struct *wq;
2122 struct delayed_work reset_work;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002123 struct hl_device *hdev;
2124};
2125
2126/**
Moti Haimovskiccf979e2020-10-05 17:59:29 +03002127 * struct hr_mmu_hop_addrs - used for holding per-device host-resident mmu hop
2128 * information.
2129 * @virt_addr: the virtual address of the hop.
2130 * @phys-addr: the physical address of the hop (used by the device-mmu).
2131 * @shadow_addr: The shadow of the hop used by the driver for walking the hops.
2132 */
2133struct hr_mmu_hop_addrs {
2134 u64 virt_addr;
2135 u64 phys_addr;
2136 u64 shadow_addr;
2137};
Moti Haimovskid83fe662020-08-12 13:33:44 +03002138
2139/**
Moti Haimovskiccf979e2020-10-05 17:59:29 +03002140 * struct hl_mmu_hr_pgt_priv - used for holding per-device mmu host-resident
2141 * page-table internal information.
Moti Haimovskid83fe662020-08-12 13:33:44 +03002142 * @mmu_pgt_pool: pool of page tables used by MMU for allocating hops.
2143 * @mmu_shadow_hop0: shadow array of hop0 tables.
2144 */
Moti Haimovskiccf979e2020-10-05 17:59:29 +03002145struct hl_mmu_hr_priv {
2146 struct gen_pool *mmu_pgt_pool;
2147 struct hr_mmu_hop_addrs *mmu_shadow_hop0;
2148};
2149
2150/**
2151 * struct hl_mmu_dr_pgt_priv - used for holding per-device mmu device-resident
2152 * page-table internal information.
2153 * @mmu_pgt_pool: pool of page tables used by MMU for allocating hops.
2154 * @mmu_shadow_hop0: shadow array of hop0 tables.
2155 */
2156struct hl_mmu_dr_priv {
Moti Haimovskid83fe662020-08-12 13:33:44 +03002157 struct gen_pool *mmu_pgt_pool;
2158 void *mmu_shadow_hop0;
2159};
2160
2161/**
Moti Haimovskiccf979e2020-10-05 17:59:29 +03002162 * struct hl_mmu_priv - used for holding per-device mmu internal information.
2163 * @dr: information on the device-resident MMU, when exists.
2164 * @hr: information on the host-resident MMU, when exists.
2165 */
2166struct hl_mmu_priv {
2167 struct hl_mmu_dr_priv dr;
2168 struct hl_mmu_hr_priv hr;
2169};
2170
2171/**
Moti Haimovski00e1b592020-10-27 10:55:42 +02002172 * struct hl_mmu_per_hop_info - A structure describing one TLB HOP and its entry
2173 * that was created in order to translate a virtual address to a
2174 * physical one.
2175 * @hop_addr: The address of the hop.
2176 * @hop_pte_addr: The address of the hop entry.
2177 * @hop_pte_val: The value in the hop entry.
2178 */
2179struct hl_mmu_per_hop_info {
2180 u64 hop_addr;
2181 u64 hop_pte_addr;
2182 u64 hop_pte_val;
2183};
2184
2185/**
2186 * struct hl_mmu_hop_info - A structure describing the TLB hops and their
2187 * hop-entries that were created in order to translate a virtual address to a
2188 * physical one.
Moti Haimovskib19dc672020-11-18 20:15:29 +02002189 * @scrambled_vaddr: The value of the virtual address after scrambling. This
2190 * address replaces the original virtual-address when mapped
2191 * in the MMU tables.
farah kassabri89473a12021-01-12 17:24:00 +02002192 * @unscrambled_paddr: The un-scrambled physical address.
Moti Haimovski00e1b592020-10-27 10:55:42 +02002193 * @hop_info: Array holding the per-hop information used for the translation.
2194 * @used_hops: The number of hops used for the translation.
farah kassabri89473a12021-01-12 17:24:00 +02002195 * @range_type: virtual address range type.
Moti Haimovski00e1b592020-10-27 10:55:42 +02002196 */
2197struct hl_mmu_hop_info {
Moti Haimovskib19dc672020-11-18 20:15:29 +02002198 u64 scrambled_vaddr;
farah kassabri89473a12021-01-12 17:24:00 +02002199 u64 unscrambled_paddr;
Moti Haimovski00e1b592020-10-27 10:55:42 +02002200 struct hl_mmu_per_hop_info hop_info[MMU_ARCH_5_HOPS];
2201 u32 used_hops;
farah kassabri89473a12021-01-12 17:24:00 +02002202 enum hl_va_range_type range_type;
Moti Haimovski00e1b592020-10-27 10:55:42 +02002203};
2204
2205/**
Moti Haimovskid83fe662020-08-12 13:33:44 +03002206 * struct hl_mmu_funcs - Device related MMU functions.
2207 * @init: initialize the MMU module.
2208 * @fini: release the MMU module.
2209 * @ctx_init: Initialize a context for using the MMU module.
2210 * @ctx_fini: disable a ctx from using the mmu module.
2211 * @map: maps a virtual address to physical address for a context.
2212 * @unmap: unmap a virtual address of a context.
2213 * @flush: flush all writes from all cores to reach device MMU.
2214 * @swap_out: marks all mapping of the given context as swapped out.
2215 * @swap_in: marks all mapping of the given context as swapped in.
Moti Haimovski00e1b592020-10-27 10:55:42 +02002216 * @get_tlb_info: returns the list of hops and hop-entries used that were
2217 * created in order to translate the giver virtual address to a
2218 * physical one.
Moti Haimovskid83fe662020-08-12 13:33:44 +03002219 */
2220struct hl_mmu_funcs {
2221 int (*init)(struct hl_device *hdev);
2222 void (*fini)(struct hl_device *hdev);
2223 int (*ctx_init)(struct hl_ctx *ctx);
2224 void (*ctx_fini)(struct hl_ctx *ctx);
2225 int (*map)(struct hl_ctx *ctx,
2226 u64 virt_addr, u64 phys_addr, u32 page_size,
2227 bool is_dram_addr);
2228 int (*unmap)(struct hl_ctx *ctx,
2229 u64 virt_addr, bool is_dram_addr);
2230 void (*flush)(struct hl_ctx *ctx);
2231 void (*swap_out)(struct hl_ctx *ctx);
2232 void (*swap_in)(struct hl_ctx *ctx);
Moti Haimovski00e1b592020-10-27 10:55:42 +02002233 int (*get_tlb_info)(struct hl_ctx *ctx,
2234 u64 virt_addr, struct hl_mmu_hop_info *hops);
Moti Haimovskid83fe662020-08-12 13:33:44 +03002235};
2236
Oded Gabbay75b3cb22019-08-28 17:32:04 +03002237/**
Oded Gabbayc4d66342019-02-16 00:39:11 +02002238 * struct hl_device - habanalabs device structure.
2239 * @pdev: pointer to PCI device, can be NULL in case of simulator device.
Ofir Bittonf4cbfd22020-06-15 17:45:12 +03002240 * @pcie_bar_phys: array of available PCIe bars physical addresses.
2241 * (required only for PCI address match mode)
2242 * @pcie_bar: array of available PCIe bars virtual addresses.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002243 * @rmmio: configuration area address on SRAM.
Oded Gabbayc4d66342019-02-16 00:39:11 +02002244 * @cdev: related char device.
Oded Gabbay4d6a7752019-07-30 09:10:50 +03002245 * @cdev_ctrl: char device for control operations only (INFO IOCTL)
2246 * @dev: related kernel basic device structure.
2247 * @dev_ctrl: related kernel device structure for the control device
Oded Gabbayd91389b2019-02-16 00:39:19 +02002248 * @work_freq: delayed work to lower device frequency if possible.
Oded Gabbay6138bbe2020-09-04 20:18:16 +03002249 * @work_heartbeat: delayed work for CPU-CP is-alive check.
Ofir Bittonadb51292020-10-08 10:27:42 +03002250 * @device_reset_work: delayed work which performs hard reset
Oded Gabbay6138bbe2020-09-04 20:18:16 +03002251 * @asic_name: ASIC specific name.
Oded Gabbayc4d66342019-02-16 00:39:11 +02002252 * @asic_type: ASIC specific type.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002253 * @completion_queue: array of hl_cq.
Ofir Bittonab5f5c32021-01-12 18:37:19 +02002254 * @user_interrupt: array of hl_user_interrupt. upon the corresponding user
2255 * interrupt, driver will monitor the list of fences
2256 * registered to this interrupt.
2257 * @common_user_interrupt: common user interrupt for all user interrupts.
2258 * upon any user interrupt, driver will monitor the
2259 * list of fences registered to this common structure.
Ofir Bitton5574cb22020-07-05 13:35:51 +03002260 * @cq_wq: work queues of completion queues for executing work in process
2261 * context.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002262 * @eq_wq: work queue of event queue for executing work in process context.
Ofir Bitton8445dde2021-03-18 17:36:57 +02002263 * @sob_reset_wq: work queue for sob reset executions.
Oded Gabbay4c172bb2019-08-30 16:59:33 +03002264 * @kernel_ctx: Kernel driver context structure.
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002265 * @kernel_queues: array of hl_hw_queue.
Tomer Tayar804a72272020-10-30 11:16:23 +02002266 * @cs_mirror_list: CS mirror list for TDR.
2267 * @cs_mirror_lock: protects cs_mirror_list.
Zvika Yehudai38e19d02021-06-15 14:12:20 +03002268 * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CBs.
Oded Gabbay6138bbe2020-09-04 20:18:16 +03002269 * @event_queue: event queue for IRQ from CPU-CP.
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002270 * @dma_pool: DMA pool for small allocations.
Oded Gabbay6138bbe2020-09-04 20:18:16 +03002271 * @cpu_accessible_dma_mem: Host <-> CPU-CP shared memory CPU address.
2272 * @cpu_accessible_dma_address: Host <-> CPU-CP shared memory DMA address.
2273 * @cpu_accessible_dma_pool: Host <-> CPU-CP shared memory pool.
Oded Gabbay0861e412019-02-16 00:39:14 +02002274 * @asid_bitmap: holds used/available ASIDs.
2275 * @asid_mutex: protects asid_bitmap.
Oded Gabbay6138bbe2020-09-04 20:18:16 +03002276 * @send_cpu_message_lock: enforces only one message in Host <-> CPU-CP queue.
Oded Gabbay19734972019-05-04 17:36:06 +03002277 * @debug_lock: protects critical section of setting debug mode for device
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002278 * @asic_prop: ASIC specific immutable properties.
2279 * @asic_funcs: ASIC specific functions.
2280 * @asic_specific: ASIC specific information to use only from ASIC files.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002281 * @vm: virtual memory manager for MMU.
Oded Gabbayd91389b2019-02-16 00:39:19 +02002282 * @hwmon_dev: H/W monitor device.
2283 * @pm_mng_profile: current power management profile.
2284 * @hl_chip_info: ASIC's sensors information.
Ofir Bitton66a76402020-10-05 14:40:10 +03002285 * @device_status_description: device status description.
Oded Gabbayc2164772019-02-16 00:39:24 +02002286 * @hl_debugfs: device's debugfs manager.
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002287 * @cb_pool: list of preallocated CBs.
2288 * @cb_pool_lock: protects the CB pool.
Ofir Bittona04b7cd2020-07-13 13:36:55 +03002289 * @internal_cb_pool_virt_addr: internal command buffer pool virtual address.
2290 * @internal_cb_pool_dma_addr: internal command buffer pool dma address.
2291 * @internal_cb_pool: internal command buffer memory pool.
2292 * @internal_cb_va_base: internal cb pool mmu virtual address base
Oded Gabbayeb7caf82019-07-30 11:56:09 +03002293 * @fpriv_list: list of file private data structures. Each structure is created
2294 * when a user opens the device
2295 * @fpriv_list_lock: protects the fpriv_list
Oded Gabbay86d53072019-07-30 11:49:36 +03002296 * @compute_ctx: current compute context executing.
Ofir Bittondb491e42020-06-18 09:51:16 +03002297 * @aggregated_cs_counters: aggregated cs counters among all contexts
Moti Haimovskid83fe662020-08-12 13:33:44 +03002298 * @mmu_priv: device-specific MMU data.
2299 * @mmu_func: device-related MMU functions.
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03002300 * @fw_loader: FW loader manager.
Ohad Sharabic592c272021-04-21 13:03:21 +03002301 * @pci_mem_region: array of memory regions in the PCI
Yuri Nudelman938b7932021-06-06 10:28:51 +03002302 * @state_dump_specs: constants and dictionaries needed to dump system state.
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002303 * @dram_used_mem: current DRAM memory consumption.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002304 * @timeout_jiffies: device CS timeout value.
Oded Gabbayd91389b2019-02-16 00:39:19 +02002305 * @max_power: the max power of the device, as configured by the sysadmin. This
Oded Gabbay4c172bb2019-08-30 16:59:33 +03002306 * value is saved so in case of hard-reset, the driver will restore
2307 * this value and update the F/W after the re-initialization
Oded Gabbaye38bfd32020-07-03 20:46:12 +03002308 * @clock_gating_mask: is clock gating enabled. bitmask that represents the
2309 * different engines. See debugfs-driver-habanalabs for
2310 * details.
Oded Gabbay27a9e352021-04-12 09:52:05 +03002311 * @boot_error_status_mask: contains a mask of the device boot error status.
2312 * Each bit represents a different error, according to
2313 * the defines in hl_boot_if.h. If the bit is cleared,
2314 * the error will be ignored by the driver during
2315 * device initialization. Mainly used to debug and
2316 * workaround firmware bugs
Yuri Nudelmane307b302021-05-24 11:25:21 +03002317 * @last_successful_open_jif: timestamp (jiffies) of the last successful
2318 * device open.
2319 * @last_open_session_duration_jif: duration (jiffies) of the last device open
2320 * session.
2321 * @open_counter: number of successful device open operations.
Oded Gabbaycbaa99e2019-03-03 15:13:15 +02002322 * @in_reset: is device in reset flow.
Oded Gabbay75b3cb22019-08-28 17:32:04 +03002323 * @curr_pll_profile: current PLL profile.
Oded Gabbay58361aa2020-08-08 23:34:47 +03002324 * @card_type: Various ASICs have several card types. This indicates the card
2325 * type of the current device.
Oded Gabbay4c172bb2019-08-30 16:59:33 +03002326 * @major: habanalabs kernel driver major.
Oded Gabbayd91389b2019-02-16 00:39:19 +02002327 * @high_pll: high PLL profile frequency.
Oded Gabbay4c172bb2019-08-30 16:59:33 +03002328 * @soft_reset_cnt: number of soft reset since the driver was loaded.
2329 * @hard_reset_cnt: number of hard reset since the driver was loaded.
Ofir Bitton0a068ad2020-07-21 10:49:51 +03002330 * @clk_throttling_reason: bitmask represents the current clk throttling reasons
Oded Gabbayc4d66342019-02-16 00:39:11 +02002331 * @id: device minor.
Oded Gabbay4d6a7752019-07-30 09:10:50 +03002332 * @id_control: minor of the control device
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002333 * @cpu_pci_msb_addr: 50-bit extension bits for the device CPU's 40-bit
2334 * addresses.
Oded Gabbayc4d66342019-02-16 00:39:11 +02002335 * @disabled: is device disabled.
Oded Gabbayd91389b2019-02-16 00:39:19 +02002336 * @late_init_done: is late init stage was done during initialization.
2337 * @hwmon_initialized: is H/W monitor sensors was initialized.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002338 * @hard_reset_pending: is there a hard reset work pending.
Oded Gabbay6138bbe2020-09-04 20:18:16 +03002339 * @heartbeat: is heartbeat sanity check towards CPU-CP enabled.
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002340 * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
2341 * otherwise.
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02002342 * @dram_default_page_mapping: is DRAM default page mapping enabled.
farah kassabri03df1362020-05-06 11:17:38 +03002343 * @memory_scrub: true to perform device memory scrub in various locations,
2344 * such as context-switch, context close, page free, etc.
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002345 * @pmmu_huge_range: is a different virtual addresses range used for PMMU with
2346 * huge pages.
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002347 * @init_done: is the initialization of the device done.
Oded Gabbaya28ce422019-02-28 10:46:12 +02002348 * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
Oded Gabbayd9973872019-03-07 18:03:23 +02002349 * @dma_mask: the dma mask that was set for this device
Oded Gabbayeb7caf82019-07-30 11:56:09 +03002350 * @in_debug: is device under debug. This, together with fpriv_list, enforces
Oded Gabbay19734972019-05-04 17:36:06 +03002351 * that only a single user is configuring the debug infrastructure.
Oded Gabbaycb056b92020-03-29 13:18:30 +03002352 * @power9_64bit_dma_enable: true to enable 64-bit DMA mask support. Relevant
2353 * only to POWER9 machines.
Tomer Tayarea451f82019-08-08 12:25:52 +00002354 * @cdev_sysfs_created: were char devices and sysfs nodes created.
Omer Shpigelman76cedc72020-03-22 21:12:51 +02002355 * @stop_on_err: true if engines should stop on error.
Omer Shpigelmanf9e5f292020-05-07 13:41:16 +03002356 * @supports_sync_stream: is sync stream supported.
Ofir Bitton21e7a342020-05-14 18:25:47 +03002357 * @sync_stream_queue_idx: helper index for sync stream queues initialization.
Ofir Bitton5fe1c172020-09-10 10:10:55 +03002358 * @collective_mon_idx: helper index for collective initialization
Omer Shpigelman9e5e49c2020-05-10 14:10:15 +03002359 * @supports_coresight: is CoreSight supported.
Oded Gabbay66446822020-05-18 16:48:01 +03002360 * @supports_soft_reset: is soft reset supported.
Ofir Bitton23bace62021-06-08 17:24:52 +03002361 * @allow_external_soft_reset: true if soft reset initiated by user or TDR is
2362 * allowed.
Tomer Tayaref6a0f62020-07-09 16:17:48 +03002363 * @supports_cb_mapping: is mapping a CB to the device's MMU supported.
Ofir Bitton66a76402020-10-05 14:40:10 +03002364 * @needs_reset: true if reset_on_lockup is false and device should be reset
2365 * due to lockup.
Ofir Bittonadb51292020-10-08 10:27:42 +03002366 * @process_kill_trial_cnt: number of trials reset thread tried killing
2367 * user processes
2368 * @device_fini_pending: true if device_fini was called and might be
2369 * waiting for the reset thread to finish
Ofir Bitton2795c882020-12-08 13:47:05 +02002370 * @supports_staged_submission: true if staged submissions are supported
Koby Elbaz3e0ca9f2021-05-04 20:10:47 +03002371 * @curr_reset_cause: saves an enumerated reset cause when a hard reset is
2372 * triggered, and cleared after it is shared with preboot.
Yuri Nudelman4d041212021-06-13 09:22:20 +03002373 * @skip_reset_on_timeout: Skip device reset if CS has timed out, wait for it to
2374 * complete instead.
Koby Elbazb7a71fd2021-06-15 17:07:02 +03002375 * @device_cpu_is_halted: Flag to indicate whether the device CPU was already
2376 * halted. We can't halt it again because the COMMS
2377 * protocol will throw an error. Relevant only for
2378 * cases where Linux was not loaded to device CPU
Oded Gabbayc4d66342019-02-16 00:39:11 +02002379 */
2380struct hl_device {
2381 struct pci_dev *pdev;
Ofir Bittonf4cbfd22020-06-15 17:45:12 +03002382 u64 pcie_bar_phys[HL_PCI_NUM_BARS];
2383 void __iomem *pcie_bar[HL_PCI_NUM_BARS];
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002384 void __iomem *rmmio;
Oded Gabbayc4d66342019-02-16 00:39:11 +02002385 struct cdev cdev;
Oded Gabbay4d6a7752019-07-30 09:10:50 +03002386 struct cdev cdev_ctrl;
Oded Gabbayc4d66342019-02-16 00:39:11 +02002387 struct device *dev;
Oded Gabbay4d6a7752019-07-30 09:10:50 +03002388 struct device *dev_ctrl;
Oded Gabbayd91389b2019-02-16 00:39:19 +02002389 struct delayed_work work_freq;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002390 struct delayed_work work_heartbeat;
Ofir Bittonadb51292020-10-08 10:27:42 +03002391 struct hl_device_reset_work device_reset_work;
Ofir Bitton66a76402020-10-05 14:40:10 +03002392 char asic_name[HL_STR_MAX];
2393 char status[HL_DEV_STS_MAX][HL_STR_MAX];
Oded Gabbayc4d66342019-02-16 00:39:11 +02002394 enum hl_asic_type asic_type;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002395 struct hl_cq *completion_queue;
Ofir Bitton2d44c6f2021-01-12 14:43:09 +02002396 struct hl_user_interrupt *user_interrupt;
Ofir Bittonab5f5c32021-01-12 18:37:19 +02002397 struct hl_user_interrupt common_user_interrupt;
Ofir Bitton5574cb22020-07-05 13:35:51 +03002398 struct workqueue_struct **cq_wq;
Oded Gabbay1251f232019-02-16 00:39:18 +02002399 struct workqueue_struct *eq_wq;
Ofir Bitton8445dde2021-03-18 17:36:57 +02002400 struct workqueue_struct *sob_reset_wq;
Oded Gabbay0861e412019-02-16 00:39:14 +02002401 struct hl_ctx *kernel_ctx;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002402 struct hl_hw_queue *kernel_queues;
Tomer Tayar804a72272020-10-30 11:16:23 +02002403 struct list_head cs_mirror_list;
2404 spinlock_t cs_mirror_lock;
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002405 struct hl_cb_mgr kernel_cb_mgr;
Oded Gabbay1251f232019-02-16 00:39:18 +02002406 struct hl_eq event_queue;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002407 struct dma_pool *dma_pool;
2408 void *cpu_accessible_dma_mem;
2409 dma_addr_t cpu_accessible_dma_address;
2410 struct gen_pool *cpu_accessible_dma_pool;
Oded Gabbay0861e412019-02-16 00:39:14 +02002411 unsigned long *asid_bitmap;
2412 struct mutex asid_mutex;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002413 struct mutex send_cpu_message_lock;
Oded Gabbay19734972019-05-04 17:36:06 +03002414 struct mutex debug_lock;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002415 struct asic_fixed_properties asic_prop;
2416 const struct hl_asic_funcs *asic_funcs;
2417 void *asic_specific;
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002418 struct hl_vm vm;
Oded Gabbayd91389b2019-02-16 00:39:19 +02002419 struct device *hwmon_dev;
2420 enum hl_pm_mng_profile pm_mng_profile;
2421 struct hwmon_chip_info *hl_chip_info;
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002422
Oded Gabbayc2164772019-02-16 00:39:24 +02002423 struct hl_dbg_device_entry hl_debugfs;
2424
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002425 struct list_head cb_pool;
2426 spinlock_t cb_pool_lock;
2427
Ofir Bittona04b7cd2020-07-13 13:36:55 +03002428 void *internal_cb_pool_virt_addr;
2429 dma_addr_t internal_cb_pool_dma_addr;
2430 struct gen_pool *internal_cb_pool;
2431 u64 internal_cb_va_base;
2432
Oded Gabbayeb7caf82019-07-30 11:56:09 +03002433 struct list_head fpriv_list;
2434 struct mutex fpriv_list_lock;
2435
Oded Gabbay86d53072019-07-30 11:49:36 +03002436 struct hl_ctx *compute_ctx;
Oded Gabbayd91389b2019-02-16 00:39:19 +02002437
Oded Gabbay23c15ae2020-09-30 14:25:55 +03002438 struct hl_cs_counters_atomic aggregated_cs_counters;
Ofir Bittondb491e42020-06-18 09:51:16 +03002439
Moti Haimovskid83fe662020-08-12 13:33:44 +03002440 struct hl_mmu_priv mmu_priv;
Moti Haimovskiccf979e2020-10-05 17:59:29 +03002441 struct hl_mmu_funcs mmu_func[MMU_NUM_PGT_LOCATIONS];
Moti Haimovskid83fe662020-08-12 13:33:44 +03002442
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03002443 struct fw_load_mgr fw_loader;
2444
Ohad Sharabic592c272021-04-21 13:03:21 +03002445 struct pci_mem_region pci_mem_region[PCI_REGION_NUMBER];
2446
Yuri Nudelman938b7932021-06-06 10:28:51 +03002447 struct hl_state_dump_specs state_dump_specs;
2448
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002449 atomic64_t dram_used_mem;
Oded Gabbaycbaa99e2019-03-03 15:13:15 +02002450 u64 timeout_jiffies;
2451 u64 max_power;
Oded Gabbaye38bfd32020-07-03 20:46:12 +03002452 u64 clock_gating_mask;
Oded Gabbay27a9e352021-04-12 09:52:05 +03002453 u64 boot_error_status_mask;
Yuri Nudelmane307b302021-05-24 11:25:21 +03002454 u64 last_successful_open_jif;
2455 u64 last_open_session_duration_jif;
2456 u64 open_counter;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002457 atomic_t in_reset;
Oded Gabbayeb7caf82019-07-30 11:56:09 +03002458 enum hl_pll_frequency curr_pll_profile;
Oded Gabbay2f553422020-08-15 16:28:10 +03002459 enum cpucp_card_types card_type;
Oded Gabbayc4d66342019-02-16 00:39:11 +02002460 u32 major;
Oded Gabbayd91389b2019-02-16 00:39:19 +02002461 u32 high_pll;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002462 u32 soft_reset_cnt;
2463 u32 hard_reset_cnt;
Ofir Bitton0a068ad2020-07-21 10:49:51 +03002464 u32 clk_throttling_reason;
Oded Gabbayc4d66342019-02-16 00:39:11 +02002465 u16 id;
Oded Gabbay4d6a7752019-07-30 09:10:50 +03002466 u16 id_control;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002467 u16 cpu_pci_msb_addr;
Oded Gabbayc4d66342019-02-16 00:39:11 +02002468 u8 disabled;
Oded Gabbayd91389b2019-02-16 00:39:19 +02002469 u8 late_init_done;
2470 u8 hwmon_initialized;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002471 u8 hard_reset_pending;
2472 u8 heartbeat;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002473 u8 reset_on_lockup;
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02002474 u8 dram_default_page_mapping;
farah kassabri03df1362020-05-06 11:17:38 +03002475 u8 memory_scrub;
Omer Shpigelman64a7e292020-01-05 09:05:45 +00002476 u8 pmmu_huge_range;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002477 u8 init_done;
Oded Gabbaya28ce422019-02-28 10:46:12 +02002478 u8 device_cpu_disabled;
Oded Gabbayd9973872019-03-07 18:03:23 +02002479 u8 dma_mask;
Oded Gabbay19734972019-05-04 17:36:06 +03002480 u8 in_debug;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002481 u8 power9_64bit_dma_enable;
Tomer Tayarea451f82019-08-08 12:25:52 +00002482 u8 cdev_sysfs_created;
Omer Shpigelman76cedc72020-03-22 21:12:51 +02002483 u8 stop_on_err;
Omer Shpigelmanf9e5f292020-05-07 13:41:16 +03002484 u8 supports_sync_stream;
Ofir Bitton21e7a342020-05-14 18:25:47 +03002485 u8 sync_stream_queue_idx;
Ofir Bitton5fe1c172020-09-10 10:10:55 +03002486 u8 collective_mon_idx;
Omer Shpigelman9e5e49c2020-05-10 14:10:15 +03002487 u8 supports_coresight;
Oded Gabbay66446822020-05-18 16:48:01 +03002488 u8 supports_soft_reset;
Ofir Bitton23bace62021-06-08 17:24:52 +03002489 u8 allow_external_soft_reset;
Tomer Tayaref6a0f62020-07-09 16:17:48 +03002490 u8 supports_cb_mapping;
Ofir Bitton66a76402020-10-05 14:40:10 +03002491 u8 needs_reset;
Ofir Bittonadb51292020-10-08 10:27:42 +03002492 u8 process_kill_trial_cnt;
2493 u8 device_fini_pending;
Ofir Bitton2795c882020-12-08 13:47:05 +02002494 u8 supports_staged_submission;
Koby Elbaz3e0ca9f2021-05-04 20:10:47 +03002495 u8 curr_reset_cause;
Yuri Nudelman4d041212021-06-13 09:22:20 +03002496 u8 skip_reset_on_timeout;
Koby Elbazb7a71fd2021-06-15 17:07:02 +03002497 u8 device_cpu_is_halted;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002498
2499 /* Parameters for bring-up */
Oded Gabbay3c681572020-11-02 21:10:39 +02002500 u64 nic_ports_mask;
Ofir Bitton6a2f5d72021-02-15 13:23:04 +02002501 u64 fw_components;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002502 u8 mmu_enable;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002503 u8 mmu_huge_page_opt;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002504 u8 reset_pcilink;
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002505 u8 cpu_queues_enable;
Oded Gabbay839c4802019-02-16 00:39:16 +02002506 u8 pldm;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002507 u8 axi_drain;
2508 u8 sram_scrambler_enable;
2509 u8 dram_scrambler_enable;
2510 u8 hard_reset_on_fw_events;
2511 u8 bmc_enable;
2512 u8 rl_enable;
Ofir Bittond1ddd902020-10-19 17:04:20 +03002513 u8 reset_on_preboot_fail;
Oded Gabbayd3ee6812021-02-16 22:49:27 +02002514 u8 reset_upon_device_release;
Ofir Bitton84586de2021-05-20 13:30:31 +03002515 u8 reset_if_device_not_idle;
Oded Gabbayc4d66342019-02-16 00:39:11 +02002516};
2517
2518
2519/*
2520 * IOCTLs
2521 */
2522
2523/**
2524 * typedef hl_ioctl_t - typedef for ioctl function in the driver
2525 * @hpriv: pointer to the FD's private data, which contains state of
2526 * user process
2527 * @data: pointer to the input/output arguments structure of the IOCTL
2528 *
2529 * Return: 0 for success, negative value for error
2530 */
2531typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
2532
2533/**
2534 * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
2535 * @cmd: the IOCTL code as created by the kernel macros.
2536 * @func: pointer to the driver's function that should be called for this IOCTL.
2537 */
2538struct hl_ioctl_desc {
2539 unsigned int cmd;
2540 hl_ioctl_t *func;
2541};
2542
2543
2544/*
2545 * Kernel module functions that can be accessed by entire module
2546 */
2547
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002548/**
2549 * hl_mem_area_inside_range() - Checks whether address+size are inside a range.
2550 * @address: The start address of the area we want to validate.
2551 * @size: The size in bytes of the area we want to validate.
2552 * @range_start_address: The start address of the valid range.
2553 * @range_end_address: The end address of the valid range.
2554 *
2555 * Return: true if the area is inside the valid range, false otherwise.
2556 */
Ofir Bitton36545272020-08-06 11:33:27 +03002557static inline bool hl_mem_area_inside_range(u64 address, u64 size,
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002558 u64 range_start_address, u64 range_end_address)
2559{
2560 u64 end_address = address + size;
2561
2562 if ((address >= range_start_address) &&
2563 (end_address <= range_end_address) &&
2564 (end_address > address))
2565 return true;
2566
2567 return false;
2568}
2569
2570/**
2571 * hl_mem_area_crosses_range() - Checks whether address+size crossing a range.
2572 * @address: The start address of the area we want to validate.
2573 * @size: The size in bytes of the area we want to validate.
2574 * @range_start_address: The start address of the valid range.
2575 * @range_end_address: The end address of the valid range.
2576 *
2577 * Return: true if the area overlaps part or all of the valid range,
2578 * false otherwise.
2579 */
2580static inline bool hl_mem_area_crosses_range(u64 address, u32 size,
2581 u64 range_start_address, u64 range_end_address)
2582{
2583 u64 end_address = address + size;
2584
2585 if ((address >= range_start_address) &&
2586 (address < range_end_address))
2587 return true;
2588
2589 if ((end_address >= range_start_address) &&
2590 (end_address < range_end_address))
2591 return true;
2592
2593 if ((address < range_start_address) &&
2594 (end_address >= range_end_address))
2595 return true;
2596
2597 return false;
2598}
2599
Oded Gabbayc4d66342019-02-16 00:39:11 +02002600int hl_device_open(struct inode *inode, struct file *filp);
Oded Gabbay4d6a7752019-07-30 09:10:50 +03002601int hl_device_open_ctrl(struct inode *inode, struct file *filp);
Ofir Bitton66a76402020-10-05 14:40:10 +03002602bool hl_device_operational(struct hl_device *hdev,
2603 enum hl_device_status *status);
Dalit Ben Zooraa957082019-03-24 10:15:44 +02002604enum hl_device_status hl_device_status(struct hl_device *hdev);
Oded Gabbay19734972019-05-04 17:36:06 +03002605int hl_device_set_debug_mode(struct hl_device *hdev, bool enable);
Oded Gabbayc4d66342019-02-16 00:39:11 +02002606int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
2607 enum hl_asic_type asic_type, int minor);
2608void destroy_hdev(struct hl_device *hdev);
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002609int hl_hw_queues_create(struct hl_device *hdev);
2610void hl_hw_queues_destroy(struct hl_device *hdev);
2611int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
Koby Elbaz8bb8b502021-07-06 20:50:33 +03002612 u32 cb_size, u64 cb_ptr);
2613void hl_hw_queue_submit_bd(struct hl_device *hdev, struct hl_hw_queue *q,
2614 u32 ctl, u32 len, u64 ptr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002615int hl_hw_queue_schedule_cs(struct hl_cs *cs);
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002616u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
2617void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
Ofir Bitton0811b392020-12-06 17:18:02 +02002618void hl_hw_queue_update_ci(struct hl_cs *cs);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002619void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
Oded Gabbayc4d66342019-02-16 00:39:11 +02002620
Oded Gabbay9494a8d2019-02-16 00:39:17 +02002621#define hl_queue_inc_ptr(p) hl_hw_queue_add_ptr(p, 1)
2622#define hl_pi_2_offset(pi) ((pi) & (HL_QUEUE_LENGTH - 1))
2623
2624int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
2625void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
Oded Gabbay1251f232019-02-16 00:39:18 +02002626int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
2627void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +02002628void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
2629void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
Oded Gabbay1251f232019-02-16 00:39:18 +02002630irqreturn_t hl_irq_handler_cq(int irq, void *arg);
2631irqreturn_t hl_irq_handler_eq(int irq, void *arg);
Ofir Bitton2d44c6f2021-01-12 14:43:09 +02002632irqreturn_t hl_irq_handler_user_cq(int irq, void *arg);
2633irqreturn_t hl_irq_handler_default(int irq, void *arg);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002634u32 hl_cq_inc_ptr(u32 ptr);
2635
Oded Gabbay0861e412019-02-16 00:39:14 +02002636int hl_asid_init(struct hl_device *hdev);
2637void hl_asid_fini(struct hl_device *hdev);
2638unsigned long hl_asid_alloc(struct hl_device *hdev);
2639void hl_asid_free(struct hl_device *hdev, unsigned long asid);
2640
2641int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
2642void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
2643int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002644void hl_ctx_do_release(struct kref *ref);
2645void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx);
Oded Gabbay0861e412019-02-16 00:39:14 +02002646int hl_ctx_put(struct hl_ctx *ctx);
Ofir Bittona98d73c2020-07-15 08:52:39 +03002647struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
Oded Gabbay0861e412019-02-16 00:39:14 +02002648void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
2649void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002650
Oded Gabbayc4d66342019-02-16 00:39:11 +02002651int hl_device_init(struct hl_device *hdev, struct class *hclass);
2652void hl_device_fini(struct hl_device *hdev);
2653int hl_device_suspend(struct hl_device *hdev);
2654int hl_device_resume(struct hl_device *hdev);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02002655int hl_device_reset(struct hl_device *hdev, u32 flags);
Oded Gabbay0861e412019-02-16 00:39:14 +02002656void hl_hpriv_get(struct hl_fpriv *hpriv);
Oded Gabbay278b5f72021-02-18 10:24:02 +02002657int hl_hpriv_put(struct hl_fpriv *hpriv);
Oded Gabbayd91389b2019-02-16 00:39:19 +02002658int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq);
Koby Elbazcd5def82021-02-23 21:31:27 +02002659int hl_device_utilization(struct hl_device *hdev, u32 *utilization);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002660
Oded Gabbayd91389b2019-02-16 00:39:19 +02002661int hl_build_hwmon_channel_info(struct hl_device *hdev,
Oded Gabbay2f553422020-08-15 16:28:10 +03002662 struct cpucp_sensor *sensors_arr);
Oded Gabbayd91389b2019-02-16 00:39:19 +02002663
2664int hl_sysfs_init(struct hl_device *hdev);
2665void hl_sysfs_fini(struct hl_device *hdev);
2666
2667int hl_hwmon_init(struct hl_device *hdev);
2668void hl_hwmon_fini(struct hl_device *hdev);
Oded Gabbayc4d66342019-02-16 00:39:11 +02002669
Tomer Tayarfa8641a12020-09-07 17:36:41 +03002670int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
2671 struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
Tomer Tayaref6a0f62020-07-09 16:17:48 +03002672 bool map_cb, u64 *handle);
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002673int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
2674int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
Ofir Bittond00697f2021-01-05 12:55:06 +02002675int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002676struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
2677 u32 handle);
2678void hl_cb_put(struct hl_cb *cb);
2679void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
2680void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
Ofir Bittona04b7cd2020-07-13 13:36:55 +03002681struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
2682 bool internal_cb);
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002683int hl_cb_pool_init(struct hl_device *hdev);
2684int hl_cb_pool_fini(struct hl_device *hdev);
Tomer Tayaref6a0f62020-07-09 16:17:48 +03002685int hl_cb_va_pool_init(struct hl_ctx *ctx);
2686void hl_cb_va_pool_fini(struct hl_ctx *ctx);
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002687
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002688void hl_cs_rollback_all(struct hl_device *hdev);
Ofir Bittond3f139c2020-11-18 15:46:57 +02002689void hl_pending_cb_list_flush(struct hl_ctx *ctx);
Tomer Tayarcb596ae2019-10-03 15:22:36 +00002690struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
2691 enum hl_queue_type queue_type, bool is_kernel_allocated_cb);
Omer Shpigelmanb0b5d922020-05-07 13:57:36 +03002692void hl_sob_reset_error(struct kref *ref);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03002693int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask);
Ofir Bittona98d73c2020-07-15 08:52:39 +03002694void hl_fence_put(struct hl_fence *fence);
2695void hl_fence_get(struct hl_fence *fence);
Ofir Bitton5de406c2020-09-10 10:56:26 +03002696void cs_get(struct hl_cs *cs);
Ofir Bitton0811b392020-12-06 17:18:02 +02002697bool cs_needs_completion(struct hl_cs *cs);
2698bool cs_needs_timeout(struct hl_cs *cs);
Ofir Bitton2795c882020-12-08 13:47:05 +02002699bool is_staged_cs_last_exists(struct hl_device *hdev, struct hl_cs *cs);
2700struct hl_cs *hl_staged_cs_find_first(struct hl_device *hdev, u64 cs_seq);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002701
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002702void goya_set_asic_funcs(struct hl_device *hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002703void gaudi_set_asic_funcs(struct hl_device *hdev);
Oded Gabbay99b9d7b2019-02-16 00:39:13 +02002704
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002705int hl_vm_ctx_init(struct hl_ctx *ctx);
2706void hl_vm_ctx_fini(struct hl_ctx *ctx);
2707
2708int hl_vm_init(struct hl_device *hdev);
2709void hl_vm_fini(struct hl_device *hdev);
2710
Sagiv Ozeria4371c12021-02-23 11:01:08 +02002711void hl_hw_block_mem_init(struct hl_ctx *ctx);
2712void hl_hw_block_mem_fini(struct hl_ctx *ctx);
2713
Ofir Bittonbe91b912020-10-22 15:04:10 +03002714u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
Ofir Bitton412c41f2020-11-04 15:18:55 +02002715 enum hl_va_range_type type, u32 size, u32 alignment);
Ofir Bittonbe91b912020-10-22 15:04:10 +03002716int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
2717 u64 start_addr, u64 size);
Oded Gabbay230afe72019-02-27 00:19:18 +02002718int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002719 struct hl_userptr *userptr);
Omer Shpigelman7f74d4d2019-08-12 11:48:46 +03002720void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002721void hl_userptr_delete_list(struct hl_device *hdev,
2722 struct list_head *userptr_list);
2723bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, u32 size,
2724 struct list_head *userptr_list,
2725 struct hl_userptr **userptr);
2726
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002727int hl_mmu_init(struct hl_device *hdev);
2728void hl_mmu_fini(struct hl_device *hdev);
Omer Shpigelman27ca384c2019-02-28 10:46:11 +02002729int hl_mmu_ctx_init(struct hl_ctx *ctx);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002730void hl_mmu_ctx_fini(struct hl_ctx *ctx);
Ofir Bitton5c054872020-10-22 15:13:10 +03002731int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
Pawel Piskorski7fc40bc2019-12-06 17:32:38 +02002732 u32 page_size, bool flush_pte);
Ofir Bitton5c054872020-10-22 15:13:10 +03002733int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
Pawel Piskorski7fc40bc2019-12-06 17:32:38 +02002734 bool flush_pte);
Ofir Bitton5c054872020-10-22 15:13:10 +03002735int hl_mmu_map_contiguous(struct hl_ctx *ctx, u64 virt_addr,
2736 u64 phys_addr, u32 size);
2737int hl_mmu_unmap_contiguous(struct hl_ctx *ctx, u64 virt_addr, u32 size);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002738void hl_mmu_swap_out(struct hl_ctx *ctx);
2739void hl_mmu_swap_in(struct hl_ctx *ctx);
Moti Haimovskid83fe662020-08-12 13:33:44 +03002740int hl_mmu_if_set_funcs(struct hl_device *hdev);
Moti Haimovskiccf979e2020-10-05 17:59:29 +03002741void hl_mmu_v1_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
Moti Haimovski00e1b592020-10-27 10:55:42 +02002742int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr);
2743int hl_mmu_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
2744 struct hl_mmu_hop_info *hops);
farah kassabri89473a12021-01-12 17:24:00 +02002745u64 hl_mmu_scramble_addr(struct hl_device *hdev, u64 addr);
2746u64 hl_mmu_descramble_addr(struct hl_device *hdev, u64 addr);
Oded Gabbay94883072021-01-11 17:49:30 +02002747bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002748
Oded Gabbay7e1c07d2020-03-26 12:32:56 +02002749int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
Ofir Bitton9bb86b62020-10-20 10:45:37 +03002750 void __iomem *dst, u32 src_offset, u32 size);
Tomer Tayar3110c602019-03-04 10:22:09 +02002751int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode);
2752int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
Alon Mizrahi439bc472020-11-10 13:49:10 +02002753 u16 len, u32 timeout, u64 *result);
Ofir Bittonebd8d122020-05-10 13:41:28 +03002754int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type);
2755int hl_fw_unmask_irq_arr(struct hl_device *hdev, const u32 *irq_arr,
2756 size_t irq_arr_size);
Tomer Tayar3110c602019-03-04 10:22:09 +02002757int hl_fw_test_cpu_queue(struct hl_device *hdev);
2758void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
2759 dma_addr_t *dma_handle);
2760void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
2761 void *vaddr);
2762int hl_fw_send_heartbeat(struct hl_device *hdev);
Alon Mizrahi41478642020-11-17 14:25:14 +02002763int hl_fw_cpucp_info_get(struct hl_device *hdev,
Ohad Sharabie67a60402021-05-02 15:45:21 +03002764 u32 sts_boot_dev_sts0_reg,
2765 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
2766 u32 boot_err1_reg);
Ohad Sharabie9c20032021-03-22 14:30:52 +02002767int hl_fw_cpucp_handshake(struct hl_device *hdev,
Ohad Sharabie67a60402021-05-02 15:45:21 +03002768 u32 sts_boot_dev_sts0_reg,
2769 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
2770 u32 boot_err1_reg);
Tomer Tayar3110c602019-03-04 10:22:09 +02002771int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
Oded Gabbay2f553422020-08-15 16:28:10 +03002772int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
Ofir Bitton0a068ad2020-07-21 10:49:51 +03002773 struct hl_info_pci_counters *counters);
Oded Gabbay2f553422020-08-15 16:28:10 +03002774int hl_fw_cpucp_total_energy_get(struct hl_device *hdev,
farah kassabri9f306492020-08-09 16:25:53 +03002775 u64 *total_energy);
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02002776int get_used_pll_index(struct hl_device *hdev, u32 input_pll_index,
Ohad Sharabie8f93922021-03-15 20:49:28 +02002777 enum pll_index *pll_index);
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02002778int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
Alon Mizrahi41478642020-11-17 14:25:14 +02002779 u16 *pll_freq_arr);
Sagiv Ozeri586f2ca2021-02-23 18:00:05 +02002780int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power);
Koby Elbazb7a71fd2021-06-15 17:07:02 +03002781void hl_fw_ask_hard_reset_without_linux(struct hl_device *hdev);
2782void hl_fw_ask_halt_machine_without_linux(struct hl_device *hdev);
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03002783int hl_fw_init_cpu(struct hl_device *hdev);
Ofir Bitton323b7262020-10-04 09:09:19 +03002784int hl_fw_read_preboot_status(struct hl_device *hdev, u32 cpu_boot_status_reg,
Ohad Sharabie67a60402021-05-02 15:45:21 +03002785 u32 sts_boot_dev_sts0_reg,
2786 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
2787 u32 boot_err1_reg, u32 timeout);
Koby Elbaz40803082021-05-20 12:45:58 +03002788int hl_fw_dynamic_send_protocol_cmd(struct hl_device *hdev,
2789 struct fw_load_mgr *fw_loader,
2790 enum comms_cmd cmd, unsigned int size,
2791 bool wait_ok, u32 timeout);
Tomer Tayarb6f897d2019-03-05 16:48:42 +02002792int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
2793 bool is_wc[3]);
Ofir Bitton41f458f2021-03-10 15:08:44 +02002794int hl_pci_elbi_read(struct hl_device *hdev, u64 addr, u32 *data);
Tomer Tayarb6f897d2019-03-05 16:48:42 +02002795int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
Ofir Bittonf4cbfd22020-06-15 17:45:12 +03002796int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
2797 struct hl_inbound_pci_region *pci_region);
2798int hl_pci_set_outbound_region(struct hl_device *hdev,
2799 struct hl_outbound_pci_region *pci_region);
Ohad Sharabic592c272021-04-21 13:03:21 +03002800enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr);
Ofir Bittond1ddd902020-10-19 17:04:20 +03002801int hl_pci_init(struct hl_device *hdev);
Tomer Tayarb6f897d2019-03-05 16:48:42 +02002802void hl_pci_fini(struct hl_device *hdev);
2803
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02002804long hl_get_frequency(struct hl_device *hdev, u32 pll_index,
Ohad Sharabie8f93922021-03-15 20:49:28 +02002805 bool curr);
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02002806void hl_set_frequency(struct hl_device *hdev, u32 pll_index,
Ohad Sharabie8f93922021-03-15 20:49:28 +02002807 u64 freq);
Moti Haimovskid57b83c2020-01-23 18:03:04 +02002808int hl_get_temperature(struct hl_device *hdev,
2809 int sensor_index, u32 attr, long *value);
Moti Haimovski5557b132020-01-21 15:02:06 +02002810int hl_set_temperature(struct hl_device *hdev,
Moti Haimovskid57b83c2020-01-23 18:03:04 +02002811 int sensor_index, u32 attr, long value);
2812int hl_get_voltage(struct hl_device *hdev,
2813 int sensor_index, u32 attr, long *value);
2814int hl_get_current(struct hl_device *hdev,
2815 int sensor_index, u32 attr, long *value);
2816int hl_get_fan_speed(struct hl_device *hdev,
2817 int sensor_index, u32 attr, long *value);
2818int hl_get_pwm_info(struct hl_device *hdev,
2819 int sensor_index, u32 attr, long *value);
Oded Gabbayd91389b2019-02-16 00:39:19 +02002820void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
2821 long value);
2822u64 hl_get_max_power(struct hl_device *hdev);
Oded Gabbay58361aa2020-08-08 23:34:47 +03002823void hl_set_max_power(struct hl_device *hdev);
Christine Gharzuzi8e708af2020-04-16 16:43:26 +03002824int hl_set_voltage(struct hl_device *hdev,
2825 int sensor_index, u32 attr, long value);
2826int hl_set_current(struct hl_device *hdev,
2827 int sensor_index, u32 attr, long value);
Ofir Bittonab5f5c32021-01-12 18:37:19 +02002828void hl_release_pending_user_interrupts(struct hl_device *hdev);
farah kassabri3817b352021-06-01 16:44:28 +03002829int hl_cs_signal_sob_wraparound_handler(struct hl_device *hdev, u32 q_idx,
2830 struct hl_hw_sob **hw_sob, u32 count);
Oded Gabbayd91389b2019-02-16 00:39:19 +02002831
Yuri Nudelman938b7932021-06-06 10:28:51 +03002832int hl_state_dump(struct hl_device *hdev);
2833const char *hl_state_dump_get_sync_name(struct hl_device *hdev, u32 sync_id);
Yuri Nudelmanfd2010b2021-06-09 14:04:26 +03002834const char *hl_state_dump_get_monitor_name(struct hl_device *hdev,
2835 struct hl_mon_state_dump *mon);
Yuri Nudelman938b7932021-06-06 10:28:51 +03002836void hl_state_dump_free_sync_to_engine_map(struct hl_sync_to_engine_map *map);
2837__printf(4, 5) int hl_snprintf_resize(char **buf, size_t *size, size_t *offset,
2838 const char *format, ...);
2839char *hl_format_as_binary(char *buf, size_t buf_len, u32 n);
2840const char *hl_sync_engine_to_string(enum hl_sync_engine_type engine_type);
2841
Oded Gabbayc2164772019-02-16 00:39:24 +02002842#ifdef CONFIG_DEBUG_FS
2843
2844void hl_debugfs_init(void);
2845void hl_debugfs_fini(void);
2846void hl_debugfs_add_device(struct hl_device *hdev);
2847void hl_debugfs_remove_device(struct hl_device *hdev);
2848void hl_debugfs_add_file(struct hl_fpriv *hpriv);
2849void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
2850void hl_debugfs_add_cb(struct hl_cb *cb);
2851void hl_debugfs_remove_cb(struct hl_cb *cb);
2852void hl_debugfs_add_cs(struct hl_cs *cs);
2853void hl_debugfs_remove_cs(struct hl_cs *cs);
2854void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
2855void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
2856void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
2857void hl_debugfs_remove_userptr(struct hl_device *hdev,
2858 struct hl_userptr *userptr);
2859void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
2860void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
Yuri Nudelman938b7932021-06-06 10:28:51 +03002861void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
2862 unsigned long length);
Oded Gabbayc2164772019-02-16 00:39:24 +02002863
2864#else
2865
2866static inline void __init hl_debugfs_init(void)
2867{
2868}
2869
2870static inline void hl_debugfs_fini(void)
2871{
2872}
2873
2874static inline void hl_debugfs_add_device(struct hl_device *hdev)
2875{
2876}
2877
2878static inline void hl_debugfs_remove_device(struct hl_device *hdev)
2879{
2880}
2881
2882static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
2883{
2884}
2885
2886static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
2887{
2888}
2889
2890static inline void hl_debugfs_add_cb(struct hl_cb *cb)
2891{
2892}
2893
2894static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
2895{
2896}
2897
2898static inline void hl_debugfs_add_cs(struct hl_cs *cs)
2899{
2900}
2901
2902static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
2903{
2904}
2905
2906static inline void hl_debugfs_add_job(struct hl_device *hdev,
2907 struct hl_cs_job *job)
2908{
2909}
2910
2911static inline void hl_debugfs_remove_job(struct hl_device *hdev,
2912 struct hl_cs_job *job)
2913{
2914}
2915
2916static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
2917 struct hl_userptr *userptr)
2918{
2919}
2920
2921static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
2922 struct hl_userptr *userptr)
2923{
2924}
2925
2926static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
2927 struct hl_ctx *ctx)
2928{
2929}
2930
2931static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
2932 struct hl_ctx *ctx)
2933{
2934}
2935
Yuri Nudelmanfd2010b2021-06-09 14:04:26 +03002936static inline void hl_debugfs_set_state_dump(struct hl_device *hdev,
2937 char *data, unsigned long length)
Yuri Nudelman938b7932021-06-06 10:28:51 +03002938{
2939}
2940
Oded Gabbayc2164772019-02-16 00:39:24 +02002941#endif
2942
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002943/* IOCTLs */
2944long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
Oded Gabbay4d6a7752019-07-30 09:10:50 +03002945long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002946int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +02002947int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
Ofir Bittonab5f5c32021-01-12 18:37:19 +02002948int hl_wait_ioctl(struct hl_fpriv *hpriv, void *data);
Omer Shpigelman0feaf862019-02-16 00:39:22 +02002949int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
Oded Gabbaybe5d9262019-02-16 00:39:15 +02002950
Oded Gabbayc4d66342019-02-16 00:39:11 +02002951#endif /* HABANALABSP_H_ */