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