2019-02-15 22:39:11 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
/*
|
2022-01-08 20:35:09 +00:00
|
|
|
* Copyright 2016-2022 HabanaLabs, Ltd.
|
2019-02-15 22:39:11 +00:00
|
|
|
* All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2019-04-10 12:18:46 +00:00
|
|
|
#define pr_fmt(fmt) "habanalabs: " fmt
|
|
|
|
|
2022-12-20 12:12:19 +00:00
|
|
|
#include <uapi/drm/habanalabs_accel.h>
|
2019-02-15 22:39:11 +00:00
|
|
|
#include "habanalabs.h"
|
|
|
|
|
|
|
|
#include <linux/pci.h>
|
2019-02-15 22:39:19 +00:00
|
|
|
#include <linux/hwmon.h>
|
2022-09-18 18:37:31 +00:00
|
|
|
#include <linux/vmalloc.h>
|
2019-02-15 22:39:11 +00:00
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
#include <trace/events/habanalabs.h>
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
#define HL_RESET_DELAY_USEC 10000 /* 10ms */
|
|
|
|
|
|
|
|
#define HL_DEVICE_RELEASE_WATCHDOG_TIMEOUT_SEC 5
|
2022-02-23 12:46:37 +00:00
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
enum dma_alloc_type {
|
|
|
|
DMA_ALLOC_COHERENT,
|
|
|
|
DMA_ALLOC_CPU_ACCESSIBLE,
|
|
|
|
DMA_ALLOC_POOL,
|
|
|
|
};
|
|
|
|
|
2022-06-27 12:05:28 +00:00
|
|
|
#define MEM_SCRUB_DEFAULT_VAL 0x1122334455667788
|
|
|
|
|
2022-03-23 15:11:45 +00:00
|
|
|
/*
|
|
|
|
* hl_set_dram_bar- sets the bar to allow later access to address
|
|
|
|
*
|
2022-09-01 13:37:08 +00:00
|
|
|
* @hdev: pointer to habanalabs device structure.
|
2022-03-23 15:11:45 +00:00
|
|
|
* @addr: the address the caller wants to access.
|
2022-09-01 13:37:08 +00:00
|
|
|
* @region: the PCI region.
|
2022-09-14 05:53:29 +00:00
|
|
|
* @new_bar_region_base: the new BAR region base address.
|
2022-03-23 15:11:45 +00:00
|
|
|
*
|
|
|
|
* @return: the old BAR base address on success, U64_MAX for failure.
|
|
|
|
* The caller should set it back to the old address after use.
|
|
|
|
*
|
|
|
|
* In case the bar space does not cover the whole address space,
|
|
|
|
* the bar base address should be set to allow access to a given address.
|
|
|
|
* This function can be called also if the bar doesn't need to be set,
|
|
|
|
* in that case it just won't change the base.
|
|
|
|
*/
|
2022-09-14 05:53:29 +00:00
|
|
|
static u64 hl_set_dram_bar(struct hl_device *hdev, u64 addr, struct pci_mem_region *region,
|
|
|
|
u64 *new_bar_region_base)
|
2022-03-23 15:11:45 +00:00
|
|
|
{
|
|
|
|
struct asic_fixed_properties *prop = &hdev->asic_prop;
|
2022-09-01 13:37:08 +00:00
|
|
|
u64 bar_base_addr, old_base;
|
2022-03-23 15:11:45 +00:00
|
|
|
|
2022-08-15 17:13:30 +00:00
|
|
|
if (is_power_of_2(prop->dram_pci_bar_size))
|
|
|
|
bar_base_addr = addr & ~(prop->dram_pci_bar_size - 0x1ull);
|
|
|
|
else
|
|
|
|
bar_base_addr = DIV_ROUND_DOWN_ULL(addr, prop->dram_pci_bar_size) *
|
|
|
|
prop->dram_pci_bar_size;
|
2022-03-23 15:11:45 +00:00
|
|
|
|
2022-09-01 13:37:08 +00:00
|
|
|
old_base = hdev->asic_funcs->set_dram_bar_base(hdev, bar_base_addr);
|
|
|
|
|
|
|
|
/* in case of success we need to update the new BAR base */
|
2022-09-14 05:53:29 +00:00
|
|
|
if ((old_base != U64_MAX) && new_bar_region_base)
|
|
|
|
*new_bar_region_base = bar_base_addr;
|
2022-03-23 15:11:45 +00:00
|
|
|
|
2022-09-01 13:37:08 +00:00
|
|
|
return old_base;
|
|
|
|
}
|
2022-03-23 15:11:45 +00:00
|
|
|
|
2022-09-14 05:53:29 +00:00
|
|
|
int hl_access_sram_dram_region(struct hl_device *hdev, u64 addr, u64 *val,
|
|
|
|
enum debugfs_access_type acc_type, enum pci_region region_type, bool set_dram_bar)
|
2022-03-23 15:11:45 +00:00
|
|
|
{
|
|
|
|
struct pci_mem_region *region = &hdev->pci_mem_region[region_type];
|
2022-10-23 22:14:18 +00:00
|
|
|
u64 old_base = 0, rc, bar_region_base = region->region_base;
|
2022-09-01 13:37:08 +00:00
|
|
|
void __iomem *acc_addr;
|
2022-03-23 15:11:45 +00:00
|
|
|
|
2022-09-14 05:53:29 +00:00
|
|
|
if (set_dram_bar) {
|
2022-10-23 22:14:18 +00:00
|
|
|
old_base = hl_set_dram_bar(hdev, addr, region, &bar_region_base);
|
2022-03-23 15:11:45 +00:00
|
|
|
if (old_base == U64_MAX)
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2022-09-14 05:53:29 +00:00
|
|
|
acc_addr = hdev->pcie_bar[region->bar_id] + region->offset_in_bar +
|
2022-10-23 22:14:18 +00:00
|
|
|
(addr - bar_region_base);
|
2022-09-14 05:53:29 +00:00
|
|
|
|
2022-03-23 15:11:45 +00:00
|
|
|
switch (acc_type) {
|
2022-04-05 08:45:51 +00:00
|
|
|
case DEBUGFS_READ8:
|
2022-09-01 13:37:08 +00:00
|
|
|
*val = readb(acc_addr);
|
2022-04-05 08:45:51 +00:00
|
|
|
break;
|
|
|
|
case DEBUGFS_WRITE8:
|
2022-09-01 13:37:08 +00:00
|
|
|
writeb(*val, acc_addr);
|
2022-04-05 08:45:51 +00:00
|
|
|
break;
|
2022-03-23 15:11:45 +00:00
|
|
|
case DEBUGFS_READ32:
|
2022-09-01 13:37:08 +00:00
|
|
|
*val = readl(acc_addr);
|
2022-03-23 15:11:45 +00:00
|
|
|
break;
|
|
|
|
case DEBUGFS_WRITE32:
|
2022-09-01 13:37:08 +00:00
|
|
|
writel(*val, acc_addr);
|
2022-03-23 15:11:45 +00:00
|
|
|
break;
|
|
|
|
case DEBUGFS_READ64:
|
2022-09-01 13:37:08 +00:00
|
|
|
*val = readq(acc_addr);
|
2022-03-23 15:11:45 +00:00
|
|
|
break;
|
|
|
|
case DEBUGFS_WRITE64:
|
2022-09-01 13:37:08 +00:00
|
|
|
writeq(*val, acc_addr);
|
2022-03-23 15:11:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-14 05:53:29 +00:00
|
|
|
if (set_dram_bar) {
|
|
|
|
rc = hl_set_dram_bar(hdev, old_base, region, NULL);
|
2022-03-23 15:11:45 +00:00
|
|
|
if (rc == U64_MAX)
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
static void *hl_dma_alloc_common(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle,
|
2022-06-12 12:00:29 +00:00
|
|
|
gfp_t flag, enum dma_alloc_type alloc_type,
|
|
|
|
const char *caller)
|
2022-06-12 12:00:29 +00:00
|
|
|
{
|
2022-06-12 12:00:29 +00:00
|
|
|
void *ptr = NULL;
|
2022-06-12 12:00:29 +00:00
|
|
|
|
|
|
|
switch (alloc_type) {
|
|
|
|
case DMA_ALLOC_COHERENT:
|
|
|
|
ptr = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, size, dma_handle, flag);
|
|
|
|
break;
|
|
|
|
case DMA_ALLOC_CPU_ACCESSIBLE:
|
|
|
|
ptr = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, size, dma_handle);
|
|
|
|
break;
|
|
|
|
case DMA_ALLOC_POOL:
|
|
|
|
ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, size, flag, dma_handle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
if (trace_habanalabs_dma_alloc_enabled() && !ZERO_OR_NULL_PTR(ptr))
|
|
|
|
trace_habanalabs_dma_alloc(hdev->dev, (u64) (uintptr_t) ptr, *dma_handle, size,
|
|
|
|
caller);
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hl_asic_dma_free_common(struct hl_device *hdev, size_t size, void *cpu_addr,
|
2022-06-12 12:00:29 +00:00
|
|
|
dma_addr_t dma_handle, enum dma_alloc_type alloc_type,
|
|
|
|
const char *caller)
|
2022-06-12 12:00:29 +00:00
|
|
|
{
|
2022-10-18 05:51:33 +00:00
|
|
|
/* this is needed to avoid warning on using freed pointer */
|
|
|
|
u64 store_cpu_addr = (u64) (uintptr_t) cpu_addr;
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
switch (alloc_type) {
|
|
|
|
case DMA_ALLOC_COHERENT:
|
|
|
|
hdev->asic_funcs->asic_dma_free_coherent(hdev, size, cpu_addr, dma_handle);
|
|
|
|
break;
|
|
|
|
case DMA_ALLOC_CPU_ACCESSIBLE:
|
|
|
|
hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, size, cpu_addr);
|
|
|
|
break;
|
|
|
|
case DMA_ALLOC_POOL:
|
|
|
|
hdev->asic_funcs->asic_dma_pool_free(hdev, cpu_addr, dma_handle);
|
|
|
|
break;
|
|
|
|
}
|
2022-06-12 12:00:29 +00:00
|
|
|
|
2022-10-18 05:51:33 +00:00
|
|
|
trace_habanalabs_dma_free(hdev->dev, store_cpu_addr, dma_handle, size, caller);
|
2022-06-12 12:00:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
void *hl_asic_dma_alloc_coherent_caller(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle,
|
|
|
|
gfp_t flag, const char *caller)
|
2022-06-12 12:00:29 +00:00
|
|
|
{
|
2022-06-12 12:00:29 +00:00
|
|
|
return hl_dma_alloc_common(hdev, size, dma_handle, flag, DMA_ALLOC_COHERENT, caller);
|
2022-06-12 12:00:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
void hl_asic_dma_free_coherent_caller(struct hl_device *hdev, size_t size, void *cpu_addr,
|
|
|
|
dma_addr_t dma_handle, const char *caller)
|
2022-06-12 12:00:29 +00:00
|
|
|
{
|
2022-06-12 12:00:29 +00:00
|
|
|
hl_asic_dma_free_common(hdev, size, cpu_addr, dma_handle, DMA_ALLOC_COHERENT, caller);
|
2022-06-12 12:00:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
void *hl_cpu_accessible_dma_pool_alloc_caller(struct hl_device *hdev, size_t size,
|
|
|
|
dma_addr_t *dma_handle, const char *caller)
|
2022-06-12 12:00:29 +00:00
|
|
|
{
|
2022-06-12 12:00:29 +00:00
|
|
|
return hl_dma_alloc_common(hdev, size, dma_handle, 0, DMA_ALLOC_CPU_ACCESSIBLE, caller);
|
2022-06-12 12:00:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
void hl_cpu_accessible_dma_pool_free_caller(struct hl_device *hdev, size_t size, void *vaddr,
|
|
|
|
const char *caller)
|
2022-06-12 12:00:29 +00:00
|
|
|
{
|
2022-06-12 12:00:29 +00:00
|
|
|
hl_asic_dma_free_common(hdev, size, vaddr, 0, DMA_ALLOC_CPU_ACCESSIBLE, caller);
|
2022-06-12 12:00:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
void *hl_asic_dma_pool_zalloc_caller(struct hl_device *hdev, size_t size, gfp_t mem_flags,
|
|
|
|
dma_addr_t *dma_handle, const char *caller)
|
2022-06-12 12:00:29 +00:00
|
|
|
{
|
2022-06-12 12:00:29 +00:00
|
|
|
return hl_dma_alloc_common(hdev, size, dma_handle, mem_flags, DMA_ALLOC_POOL, caller);
|
2022-06-12 12:00:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:00:29 +00:00
|
|
|
void hl_asic_dma_pool_free_caller(struct hl_device *hdev, void *vaddr, dma_addr_t dma_addr,
|
|
|
|
const char *caller)
|
2022-06-12 12:00:29 +00:00
|
|
|
{
|
2022-06-12 12:00:29 +00:00
|
|
|
hl_asic_dma_free_common(hdev, 0, vaddr, dma_addr, DMA_ALLOC_POOL, caller);
|
2022-06-12 12:00:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 14:34:49 +00:00
|
|
|
int hl_dma_map_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
struct asic_fixed_properties *prop = &hdev->asic_prop;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int rc, i;
|
|
|
|
|
|
|
|
rc = dma_map_sgtable(&hdev->pdev->dev, sgt, dir, 0);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Shift to the device's base physical address of host memory if necessary */
|
|
|
|
if (prop->device_dma_offset_for_host_access)
|
|
|
|
for_each_sgtable_dma_sg(sgt, sg, i)
|
|
|
|
sg->dma_address += prop->device_dma_offset_for_host_access;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hl_dma_unmap_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
struct asic_fixed_properties *prop = &hdev->asic_prop;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Cancel the device's base physical address of host memory if necessary */
|
|
|
|
if (prop->device_dma_offset_for_host_access)
|
|
|
|
for_each_sgtable_dma_sg(sgt, sg, i)
|
|
|
|
sg->dma_address -= prop->device_dma_offset_for_host_access;
|
|
|
|
|
|
|
|
dma_unmap_sgtable(&hdev->pdev->dev, sgt, dir, 0);
|
|
|
|
}
|
|
|
|
|
2022-03-23 15:11:45 +00:00
|
|
|
/*
|
|
|
|
* hl_access_cfg_region - access the config region
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
* @addr: the address to access
|
|
|
|
* @val: the value to write from or read to
|
|
|
|
* @acc_type: the type of access (read/write 64/32)
|
|
|
|
*/
|
|
|
|
int hl_access_cfg_region(struct hl_device *hdev, u64 addr, u64 *val,
|
|
|
|
enum debugfs_access_type acc_type)
|
|
|
|
{
|
|
|
|
struct pci_mem_region *cfg_region = &hdev->pci_mem_region[PCI_REGION_CFG];
|
|
|
|
u32 val_h, val_l;
|
|
|
|
|
2022-03-16 17:45:24 +00:00
|
|
|
if (!IS_ALIGNED(addr, sizeof(u32))) {
|
|
|
|
dev_err(hdev->dev, "address %#llx not a multiple of %zu\n", addr, sizeof(u32));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-03-23 15:11:45 +00:00
|
|
|
switch (acc_type) {
|
|
|
|
case DEBUGFS_READ32:
|
|
|
|
*val = RREG32(addr - cfg_region->region_base);
|
|
|
|
break;
|
|
|
|
case DEBUGFS_WRITE32:
|
|
|
|
WREG32(addr - cfg_region->region_base, *val);
|
|
|
|
break;
|
|
|
|
case DEBUGFS_READ64:
|
|
|
|
val_l = RREG32(addr - cfg_region->region_base);
|
|
|
|
val_h = RREG32(addr + sizeof(u32) - cfg_region->region_base);
|
|
|
|
|
|
|
|
*val = (((u64) val_h) << 32) | val_l;
|
|
|
|
break;
|
|
|
|
case DEBUGFS_WRITE64:
|
|
|
|
WREG32(addr - cfg_region->region_base, lower_32_bits(*val));
|
|
|
|
WREG32(addr + sizeof(u32) - cfg_region->region_base, upper_32_bits(*val));
|
|
|
|
break;
|
2022-04-05 08:45:51 +00:00
|
|
|
default:
|
|
|
|
dev_err(hdev->dev, "access type %d is not supported\n", acc_type);
|
|
|
|
return -EOPNOTSUPP;
|
2022-03-23 15:11:45 +00:00
|
|
|
}
|
2022-04-05 08:45:51 +00:00
|
|
|
|
2022-03-23 15:11:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hl_access_dev_mem - access device memory
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
* @region_type: the type of the region the address belongs to
|
|
|
|
* @addr: the address to access
|
|
|
|
* @val: the value to write from or read to
|
|
|
|
* @acc_type: the type of access (r/w, 32/64)
|
|
|
|
*/
|
2022-06-27 13:59:02 +00:00
|
|
|
int hl_access_dev_mem(struct hl_device *hdev, enum pci_region region_type,
|
|
|
|
u64 addr, u64 *val, enum debugfs_access_type acc_type)
|
2022-03-23 15:11:45 +00:00
|
|
|
{
|
|
|
|
switch (region_type) {
|
|
|
|
case PCI_REGION_CFG:
|
|
|
|
return hl_access_cfg_region(hdev, addr, val, acc_type);
|
|
|
|
case PCI_REGION_SRAM:
|
|
|
|
case PCI_REGION_DRAM:
|
|
|
|
return hl_access_sram_dram_region(hdev, addr, val, acc_type,
|
2022-09-14 05:53:29 +00:00
|
|
|
region_type, (region_type == PCI_REGION_DRAM));
|
2022-03-23 15:11:45 +00:00
|
|
|
default:
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-31 06:10:24 +00:00
|
|
|
void hl_engine_data_sprintf(struct engines_data *e, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int str_size;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
/* Calculate formatted string length. Assuming each string is null terminated, hence
|
|
|
|
* increment result by 1
|
|
|
|
*/
|
|
|
|
str_size = vsnprintf(NULL, 0, fmt, args) + 1;
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if ((e->actual_size + str_size) < e->allocated_buf_size) {
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(e->buf + e->actual_size, str_size, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Need to update the size even when not updating destination buffer to get the exact size
|
|
|
|
* of all input strings
|
|
|
|
*/
|
|
|
|
e->actual_size += str_size;
|
|
|
|
}
|
|
|
|
|
2019-03-24 08:15:44 +00:00
|
|
|
enum hl_device_status hl_device_status(struct hl_device *hdev)
|
|
|
|
{
|
|
|
|
enum hl_device_status status;
|
|
|
|
|
2022-07-07 08:42:15 +00:00
|
|
|
if (hdev->reset_info.in_reset) {
|
2022-07-07 09:00:24 +00:00
|
|
|
if (hdev->reset_info.in_compute_reset)
|
2022-07-07 08:42:15 +00:00
|
|
|
status = HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE;
|
|
|
|
else
|
|
|
|
status = HL_DEVICE_STATUS_IN_RESET;
|
|
|
|
} else if (hdev->reset_info.needs_reset) {
|
2020-10-05 11:40:10 +00:00
|
|
|
status = HL_DEVICE_STATUS_NEEDS_RESET;
|
2022-07-07 08:42:15 +00:00
|
|
|
} else if (hdev->disabled) {
|
2020-12-22 13:21:07 +00:00
|
|
|
status = HL_DEVICE_STATUS_MALFUNCTION;
|
2022-07-07 08:42:15 +00:00
|
|
|
} else if (!hdev->init_done) {
|
2021-08-16 10:27:12 +00:00
|
|
|
status = HL_DEVICE_STATUS_IN_DEVICE_CREATION;
|
2022-07-07 08:42:15 +00:00
|
|
|
} else {
|
2019-03-24 08:15:44 +00:00
|
|
|
status = HL_DEVICE_STATUS_OPERATIONAL;
|
2022-07-07 08:42:15 +00:00
|
|
|
}
|
2019-03-24 08:15:44 +00:00
|
|
|
|
|
|
|
return status;
|
2020-01-07 21:44:32 +00:00
|
|
|
}
|
2019-03-24 08:15:44 +00:00
|
|
|
|
2020-10-05 11:40:10 +00:00
|
|
|
bool hl_device_operational(struct hl_device *hdev,
|
|
|
|
enum hl_device_status *status)
|
|
|
|
{
|
|
|
|
enum hl_device_status current_status;
|
|
|
|
|
|
|
|
current_status = hl_device_status(hdev);
|
|
|
|
if (status)
|
|
|
|
*status = current_status;
|
|
|
|
|
|
|
|
switch (current_status) {
|
|
|
|
case HL_DEVICE_STATUS_IN_RESET:
|
2022-07-07 08:42:15 +00:00
|
|
|
case HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE:
|
2020-10-05 11:40:10 +00:00
|
|
|
case HL_DEVICE_STATUS_MALFUNCTION:
|
|
|
|
case HL_DEVICE_STATUS_NEEDS_RESET:
|
|
|
|
return false;
|
|
|
|
case HL_DEVICE_STATUS_OPERATIONAL:
|
2021-08-16 10:27:12 +00:00
|
|
|
case HL_DEVICE_STATUS_IN_DEVICE_CREATION:
|
2020-10-05 11:40:10 +00:00
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:14:14 +00:00
|
|
|
bool hl_ctrl_device_operational(struct hl_device *hdev,
|
|
|
|
enum hl_device_status *status)
|
|
|
|
{
|
|
|
|
enum hl_device_status current_status;
|
|
|
|
|
|
|
|
current_status = hl_device_status(hdev);
|
|
|
|
if (status)
|
|
|
|
*status = current_status;
|
|
|
|
|
|
|
|
switch (current_status) {
|
|
|
|
case HL_DEVICE_STATUS_MALFUNCTION:
|
|
|
|
return false;
|
|
|
|
case HL_DEVICE_STATUS_IN_RESET:
|
|
|
|
case HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE:
|
|
|
|
case HL_DEVICE_STATUS_NEEDS_RESET:
|
|
|
|
case HL_DEVICE_STATUS_OPERATIONAL:
|
|
|
|
case HL_DEVICE_STATUS_IN_DEVICE_CREATION:
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:57:49 +00:00
|
|
|
static void print_idle_status_mask(struct hl_device *hdev, const char *message,
|
|
|
|
u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE])
|
|
|
|
{
|
|
|
|
u32 pad_width[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {};
|
|
|
|
|
|
|
|
BUILD_BUG_ON(HL_BUSY_ENGINES_MASK_EXT_SIZE != 4);
|
|
|
|
|
|
|
|
pad_width[3] = idle_mask[3] ? 16 : 0;
|
|
|
|
pad_width[2] = idle_mask[2] || pad_width[3] ? 16 : 0;
|
|
|
|
pad_width[1] = idle_mask[1] || pad_width[2] ? 16 : 0;
|
|
|
|
pad_width[0] = idle_mask[0] || pad_width[1] ? 16 : 0;
|
|
|
|
|
|
|
|
dev_err(hdev->dev, "%s (mask %0*llx_%0*llx_%0*llx_%0*llx)\n",
|
|
|
|
message, pad_width[3], idle_mask[3], pad_width[2], idle_mask[2],
|
|
|
|
pad_width[1], idle_mask[1], pad_width[0], idle_mask[0]);
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
static void hpriv_release(struct kref *ref)
|
|
|
|
{
|
2021-05-20 10:30:31 +00:00
|
|
|
u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {0};
|
2022-09-30 12:08:13 +00:00
|
|
|
bool reset_device, device_is_idle = true;
|
2019-02-15 22:39:11 +00:00
|
|
|
struct hl_fpriv *hpriv;
|
|
|
|
struct hl_device *hdev;
|
|
|
|
|
|
|
|
hpriv = container_of(ref, struct hl_fpriv, refcount);
|
|
|
|
|
|
|
|
hdev = hpriv->hdev;
|
|
|
|
|
2022-08-28 09:46:27 +00:00
|
|
|
hdev->asic_funcs->send_device_activity(hdev, false);
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
put_pid(hpriv->taskpid);
|
|
|
|
|
2019-02-15 22:39:24 +00:00
|
|
|
hl_debugfs_remove_file(hpriv);
|
|
|
|
|
2022-05-22 06:43:54 +00:00
|
|
|
mutex_destroy(&hpriv->ctx_lock);
|
2019-02-15 22:39:21 +00:00
|
|
|
mutex_destroy(&hpriv->restore_phase_mutex);
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
/* Device should be reset if reset-upon-device-release is enabled, or if there is a pending
|
|
|
|
* reset that waits for device release.
|
|
|
|
*/
|
|
|
|
reset_device = hdev->reset_upon_device_release || hdev->reset_info.watchdog_active;
|
|
|
|
|
2022-11-30 10:07:06 +00:00
|
|
|
/* Check the device idle status and reset if not idle.
|
|
|
|
* Skip it if already in reset, or if device is going to be reset in any case.
|
|
|
|
*/
|
|
|
|
if (!hdev->reset_info.in_reset && !reset_device && hdev->pdev && !hdev->pldm)
|
2022-09-30 11:09:32 +00:00
|
|
|
device_is_idle = hdev->asic_funcs->is_device_idle(hdev, idle_mask,
|
|
|
|
HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL);
|
2022-09-30 12:08:13 +00:00
|
|
|
if (!device_is_idle) {
|
2022-11-17 16:57:49 +00:00
|
|
|
print_idle_status_mask(hdev, "device is not idle after user context is closed",
|
|
|
|
idle_mask);
|
2022-09-30 12:08:13 +00:00
|
|
|
reset_device = true;
|
|
|
|
}
|
2021-05-20 10:30:31 +00:00
|
|
|
|
2021-09-30 08:22:32 +00:00
|
|
|
/* We need to remove the user from the list to make sure the reset process won't
|
|
|
|
* try to kill the user process. Because, if we got here, it means there are no
|
|
|
|
* more driver/device resources that the user process is occupying so there is
|
|
|
|
* no need to kill it
|
|
|
|
*
|
|
|
|
* However, we can't set the compute_ctx to NULL at this stage. This is to prevent
|
|
|
|
* a race between the release and opening the device again. We don't want to let
|
|
|
|
* a user open the device while there a reset is about to happen.
|
|
|
|
*/
|
|
|
|
mutex_lock(&hdev->fpriv_list_lock);
|
|
|
|
list_del(&hpriv->dev_node);
|
|
|
|
mutex_unlock(&hdev->fpriv_list_lock);
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
if (reset_device) {
|
2021-09-16 11:00:38 +00:00
|
|
|
hl_device_reset(hdev, HL_DRV_RESET_DEV_RELEASE);
|
2022-05-12 12:20:55 +00:00
|
|
|
} else {
|
2022-09-30 12:08:13 +00:00
|
|
|
/* Scrubbing is handled within hl_device_reset(), so here need to do it directly */
|
2022-05-12 12:20:55 +00:00
|
|
|
int rc = hdev->asic_funcs->scrub_device_mem(hdev);
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
dev_err(hdev->dev, "failed to scrub memory from hpriv release (%d)\n", rc);
|
|
|
|
}
|
2021-09-30 08:22:32 +00:00
|
|
|
|
2021-11-30 21:08:21 +00:00
|
|
|
/* Now we can mark the compute_ctx as not active. Even if a reset is running in a different
|
2021-09-30 08:22:32 +00:00
|
|
|
* thread, we don't care because the in_reset is marked so if a user will try to open
|
2021-11-30 21:08:21 +00:00
|
|
|
* the device it will fail on that, even if compute_ctx is false.
|
2021-09-30 08:22:32 +00:00
|
|
|
*/
|
|
|
|
mutex_lock(&hdev->fpriv_list_lock);
|
2021-11-30 21:08:21 +00:00
|
|
|
hdev->is_compute_ctx_active = false;
|
2021-09-30 08:22:32 +00:00
|
|
|
mutex_unlock(&hdev->fpriv_list_lock);
|
|
|
|
|
2022-02-28 14:08:20 +00:00
|
|
|
hdev->compute_ctx_in_release = 0;
|
|
|
|
|
2022-04-28 10:45:18 +00:00
|
|
|
/* release the eventfd */
|
2022-05-11 07:06:39 +00:00
|
|
|
if (hpriv->notifier_event.eventfd)
|
2022-04-28 10:45:18 +00:00
|
|
|
eventfd_ctx_put(hpriv->notifier_event.eventfd);
|
|
|
|
|
|
|
|
mutex_destroy(&hpriv->notifier_event.lock);
|
|
|
|
|
2021-09-30 08:22:32 +00:00
|
|
|
kfree(hpriv);
|
2019-02-15 22:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hl_hpriv_get(struct hl_fpriv *hpriv)
|
|
|
|
{
|
|
|
|
kref_get(&hpriv->refcount);
|
|
|
|
}
|
|
|
|
|
2021-02-18 08:24:02 +00:00
|
|
|
int hl_hpriv_put(struct hl_fpriv *hpriv)
|
2019-02-15 22:39:11 +00:00
|
|
|
{
|
2021-02-18 08:24:02 +00:00
|
|
|
return kref_put(&hpriv->refcount, hpriv_release);
|
2019-02-15 22:39:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 15:35:17 +00:00
|
|
|
static void compose_device_in_use_info(char **buf, size_t *buf_size, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct va_format vaf;
|
|
|
|
va_list args;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vaf.fmt = fmt;
|
|
|
|
vaf.va = &args;
|
|
|
|
|
|
|
|
size = snprintf(*buf, *buf_size, "%pV", &vaf);
|
|
|
|
if (size >= *buf_size)
|
|
|
|
size = *buf_size;
|
|
|
|
|
|
|
|
*buf += size;
|
|
|
|
*buf_size -= size;
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_device_in_use_info(struct hl_device *hdev, const char *message)
|
|
|
|
{
|
|
|
|
u32 active_cs_num, dmabuf_export_cnt;
|
|
|
|
char buf[64], *buf_ptr = buf;
|
|
|
|
size_t buf_size = sizeof(buf);
|
|
|
|
bool unknown_reason = true;
|
|
|
|
|
|
|
|
active_cs_num = hl_get_active_cs_num(hdev);
|
|
|
|
if (active_cs_num) {
|
|
|
|
unknown_reason = false;
|
|
|
|
compose_device_in_use_info(&buf_ptr, &buf_size, " [%u active CS]", active_cs_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
dmabuf_export_cnt = atomic_read(&hdev->dmabuf_export_cnt);
|
|
|
|
if (dmabuf_export_cnt) {
|
|
|
|
unknown_reason = false;
|
|
|
|
compose_device_in_use_info(&buf_ptr, &buf_size, " [%u exported dma-buf]",
|
|
|
|
dmabuf_export_cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unknown_reason)
|
|
|
|
compose_device_in_use_info(&buf_ptr, &buf_size, " [unknown reason]");
|
|
|
|
|
|
|
|
dev_notice(hdev->dev, "%s%s\n", message, buf);
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
/*
|
|
|
|
* hl_device_release - release function for habanalabs device
|
|
|
|
*
|
|
|
|
* @inode: pointer to inode structure
|
|
|
|
* @filp: pointer to file structure
|
|
|
|
*
|
|
|
|
* Called when process closes an habanalabs device
|
|
|
|
*/
|
|
|
|
static int hl_device_release(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct hl_fpriv *hpriv = filp->private_data;
|
2021-02-01 17:44:34 +00:00
|
|
|
struct hl_device *hdev = hpriv->hdev;
|
|
|
|
|
|
|
|
filp->private_data = NULL;
|
|
|
|
|
|
|
|
if (!hdev) {
|
|
|
|
pr_crit("Closing FD after device was removed. Memory leak will occur and it is advised to reboot.\n");
|
|
|
|
put_pid(hpriv->taskpid);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-15 22:39:11 +00:00
|
|
|
|
2021-02-10 12:29:33 +00:00
|
|
|
hl_ctx_mgr_fini(hdev, &hpriv->ctx_mgr);
|
2022-06-27 12:05:28 +00:00
|
|
|
hl_mem_mgr_fini(&hpriv->mem_mgr);
|
2021-02-10 12:29:33 +00:00
|
|
|
|
2022-02-28 14:08:20 +00:00
|
|
|
hdev->compute_ctx_in_release = 1;
|
|
|
|
|
2022-11-09 16:08:38 +00:00
|
|
|
if (!hl_hpriv_put(hpriv)) {
|
2023-01-18 15:35:17 +00:00
|
|
|
print_device_in_use_info(hdev, "User process closed FD but device still in use");
|
2022-11-09 16:08:38 +00:00
|
|
|
hl_device_reset(hdev, HL_DRV_RESET_HARD);
|
|
|
|
}
|
2019-02-15 22:39:11 +00:00
|
|
|
|
2023-01-18 15:35:17 +00:00
|
|
|
hdev->last_open_session_duration_jif = jiffies - hdev->last_successful_open_jif;
|
2021-05-24 08:25:21 +00:00
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-30 06:10:50 +00:00
|
|
|
static int hl_device_release_ctrl(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct hl_fpriv *hpriv = filp->private_data;
|
2021-02-01 17:44:34 +00:00
|
|
|
struct hl_device *hdev = hpriv->hdev;
|
2019-07-30 06:10:50 +00:00
|
|
|
|
|
|
|
filp->private_data = NULL;
|
|
|
|
|
2021-02-01 17:44:34 +00:00
|
|
|
if (!hdev) {
|
|
|
|
pr_err("Closing FD after device was removed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2019-07-30 06:10:50 +00:00
|
|
|
|
2021-12-08 07:52:03 +00:00
|
|
|
mutex_lock(&hdev->fpriv_ctrl_list_lock);
|
2019-07-30 06:10:50 +00:00
|
|
|
list_del(&hpriv->dev_node);
|
2021-12-08 07:52:03 +00:00
|
|
|
mutex_unlock(&hdev->fpriv_ctrl_list_lock);
|
2021-02-01 17:44:34 +00:00
|
|
|
out:
|
2022-04-28 10:45:18 +00:00
|
|
|
/* release the eventfd */
|
2022-05-11 07:06:39 +00:00
|
|
|
if (hpriv->notifier_event.eventfd)
|
2022-04-28 10:45:18 +00:00
|
|
|
eventfd_ctx_put(hpriv->notifier_event.eventfd);
|
|
|
|
|
|
|
|
mutex_destroy(&hpriv->notifier_event.lock);
|
2021-02-19 12:05:33 +00:00
|
|
|
put_pid(hpriv->taskpid);
|
|
|
|
|
2019-07-30 06:10:50 +00:00
|
|
|
kfree(hpriv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:15 +00:00
|
|
|
/*
|
|
|
|
* hl_mmap - mmap function for habanalabs device
|
|
|
|
*
|
|
|
|
* @*filp: pointer to file structure
|
|
|
|
* @*vma: pointer to vm_area_struct of the process
|
|
|
|
*
|
2022-06-27 12:05:28 +00:00
|
|
|
* Called when process does an mmap on habanalabs device. Call the relevant mmap
|
2019-02-15 22:39:15 +00:00
|
|
|
* function at the end of the common code.
|
|
|
|
*/
|
|
|
|
static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct hl_fpriv *hpriv = filp->private_data;
|
2021-02-01 17:44:34 +00:00
|
|
|
struct hl_device *hdev = hpriv->hdev;
|
2020-08-29 08:51:39 +00:00
|
|
|
unsigned long vm_pgoff;
|
2019-02-15 22:39:15 +00:00
|
|
|
|
2021-02-01 17:44:34 +00:00
|
|
|
if (!hdev) {
|
|
|
|
pr_err_ratelimited("Trying to mmap after device was removed! Please close FD\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2020-08-29 08:51:39 +00:00
|
|
|
vm_pgoff = vma->vm_pgoff;
|
|
|
|
|
|
|
|
switch (vm_pgoff & HL_MMAP_TYPE_MASK) {
|
2021-01-05 10:55:06 +00:00
|
|
|
case HL_MMAP_TYPE_BLOCK:
|
habanalabs: hide memory manager page shift
The new unified memory manager uses page offset to pass buffer handle
during the mmap operation. One problem with this approach is that it
requires the handle to always be divisible by the page size, else, the
user would not be able to pass it correctly as an argument to the mmap
system call.
Previously, this was achieved by shifting the handle left after alloc
operation, and shifting it right before get operation. This was done in
the user code. This creates code duplication, and, what's worse,
requires some knowledge from the user regarding the handle internal
structure, hurting the encapsulation.
This patch encloses all the page shifts inside memory manager functions.
This way, the user can take the handle as a black box, and simply use
it, without any concert about how it actually works.
Signed-off-by: Yuri Nudelman <ynudelman@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-03-23 13:08:22 +00:00
|
|
|
vma->vm_pgoff = HL_MMAP_OFFSET_VALUE_GET(vm_pgoff);
|
2021-01-05 10:55:06 +00:00
|
|
|
return hl_hw_block_mmap(hpriv, vma);
|
2021-12-23 11:24:34 +00:00
|
|
|
|
2022-03-29 11:16:04 +00:00
|
|
|
case HL_MMAP_TYPE_CB:
|
2021-12-23 11:24:34 +00:00
|
|
|
case HL_MMAP_TYPE_TS_BUFF:
|
2022-03-20 14:15:01 +00:00
|
|
|
return hl_mem_mgr_mmap(&hpriv->mem_mgr, vma, NULL);
|
2019-02-15 22:39:15 +00:00
|
|
|
}
|
2019-02-27 10:15:16 +00:00
|
|
|
return -EINVAL;
|
2019-02-15 22:39:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
static const struct file_operations hl_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = hl_device_open,
|
2019-02-15 22:39:15 +00:00
|
|
|
.release = hl_device_release,
|
|
|
|
.mmap = hl_mmap,
|
|
|
|
.unlocked_ioctl = hl_ioctl,
|
|
|
|
.compat_ioctl = hl_ioctl
|
2019-02-15 22:39:11 +00:00
|
|
|
};
|
|
|
|
|
2019-07-30 06:10:50 +00:00
|
|
|
static const struct file_operations hl_ctrl_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = hl_device_open_ctrl,
|
|
|
|
.release = hl_device_release_ctrl,
|
|
|
|
.unlocked_ioctl = hl_ioctl_control,
|
|
|
|
.compat_ioctl = hl_ioctl_control
|
|
|
|
};
|
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
static void device_release_func(struct device *dev)
|
|
|
|
{
|
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
/*
|
2019-08-08 12:25:52 +00:00
|
|
|
* device_init_cdev - Initialize cdev and device for habanalabs device
|
2019-02-15 22:39:11 +00:00
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
2023-01-22 15:11:31 +00:00
|
|
|
* @class: pointer to the class object of the device
|
2019-02-15 22:39:11 +00:00
|
|
|
* @minor: minor number of the specific device
|
2019-07-30 06:10:02 +00:00
|
|
|
* @fpos: file operations to install for this device
|
|
|
|
* @name: name of the device as it will appear in the filesystem
|
2019-08-08 12:25:52 +00:00
|
|
|
* @cdev: pointer to the char device object that will be initialized
|
|
|
|
* @dev: pointer to the device object that will be initialized
|
2019-02-15 22:39:11 +00:00
|
|
|
*
|
2019-08-08 12:25:52 +00:00
|
|
|
* Initialize a cdev and a Linux device for habanalabs's device.
|
2019-02-15 22:39:11 +00:00
|
|
|
*/
|
2023-01-22 15:11:31 +00:00
|
|
|
static int device_init_cdev(struct hl_device *hdev, struct class *class,
|
2019-07-30 06:10:02 +00:00
|
|
|
int minor, const struct file_operations *fops,
|
|
|
|
char *name, struct cdev *cdev,
|
|
|
|
struct device **dev)
|
2019-02-15 22:39:11 +00:00
|
|
|
{
|
2019-07-30 06:10:02 +00:00
|
|
|
cdev_init(cdev, fops);
|
|
|
|
cdev->owner = THIS_MODULE;
|
2019-08-08 12:25:52 +00:00
|
|
|
|
|
|
|
*dev = kzalloc(sizeof(**dev), GFP_KERNEL);
|
|
|
|
if (!*dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
device_initialize(*dev);
|
|
|
|
(*dev)->devt = MKDEV(hdev->major, minor);
|
2023-01-22 15:11:31 +00:00
|
|
|
(*dev)->class = class;
|
2019-08-08 12:25:52 +00:00
|
|
|
(*dev)->release = device_release_func;
|
|
|
|
dev_set_drvdata(*dev, hdev);
|
|
|
|
dev_set_name(*dev, "%s", name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int device_cdev_sysfs_add(struct hl_device *hdev)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = cdev_device_add(&hdev->cdev, hdev->dev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"failed to add a char device to the system\n");
|
|
|
|
return rc;
|
2019-02-15 22:39:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
rc = cdev_device_add(&hdev->cdev_ctrl, hdev->dev_ctrl);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"failed to add a control char device to the system\n");
|
|
|
|
goto delete_cdev_device;
|
2019-02-15 22:39:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
/* hl_sysfs_init() must be done after adding the device to the system */
|
|
|
|
rc = hl_sysfs_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "failed to initialize sysfs\n");
|
|
|
|
goto delete_ctrl_cdev_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
hdev->cdev_sysfs_created = true;
|
2019-02-15 22:39:11 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
delete_ctrl_cdev_device:
|
|
|
|
cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
|
|
|
|
delete_cdev_device:
|
|
|
|
cdev_device_del(&hdev->cdev, hdev->dev);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_cdev_sysfs_del(struct hl_device *hdev)
|
|
|
|
{
|
2020-11-29 07:34:12 +00:00
|
|
|
if (!hdev->cdev_sysfs_created)
|
|
|
|
goto put_devices;
|
2019-08-08 12:25:52 +00:00
|
|
|
|
|
|
|
hl_sysfs_fini(hdev);
|
|
|
|
cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
|
|
|
|
cdev_device_del(&hdev->cdev, hdev->dev);
|
2020-11-29 07:34:12 +00:00
|
|
|
|
|
|
|
put_devices:
|
|
|
|
put_device(hdev->dev);
|
|
|
|
put_device(hdev->dev_ctrl);
|
2019-02-15 22:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
static void device_hard_reset_pending(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct hl_device_reset_work *device_reset_work =
|
2021-11-22 10:29:22 +00:00
|
|
|
container_of(work, struct hl_device_reset_work, reset_work.work);
|
2020-10-08 07:27:42 +00:00
|
|
|
struct hl_device *hdev = device_reset_work->hdev;
|
2021-08-09 19:43:37 +00:00
|
|
|
u32 flags;
|
2020-10-08 07:27:42 +00:00
|
|
|
int rc;
|
|
|
|
|
2021-11-22 10:29:22 +00:00
|
|
|
flags = device_reset_work->flags | HL_DRV_RESET_FROM_RESET_THR;
|
2021-08-09 19:43:37 +00:00
|
|
|
|
|
|
|
rc = hl_device_reset(hdev, flags);
|
2022-11-14 11:26:21 +00:00
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
if ((rc == -EBUSY) && !hdev->device_fini_pending) {
|
2022-11-14 11:26:21 +00:00
|
|
|
struct hl_ctx *ctx = hl_get_compute_ctx(hdev);
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
/* The read refcount value should subtracted by one, because the read is
|
|
|
|
* protected with hl_get_compute_ctx().
|
|
|
|
*/
|
|
|
|
dev_info(hdev->dev,
|
|
|
|
"Could not reset device (compute_ctx refcount %u). will try again in %u seconds",
|
|
|
|
kref_read(&ctx->refcount) - 1, HL_PENDING_RESET_PER_SEC);
|
|
|
|
hl_ctx_put(ctx);
|
|
|
|
} else {
|
|
|
|
dev_info(hdev->dev, "Could not reset device. will try again in %u seconds",
|
|
|
|
HL_PENDING_RESET_PER_SEC);
|
|
|
|
}
|
2020-10-08 07:27:42 +00:00
|
|
|
|
2022-09-30 11:36:27 +00:00
|
|
|
queue_delayed_work(hdev->reset_wq, &device_reset_work->reset_work,
|
|
|
|
msecs_to_jiffies(HL_PENDING_RESET_PER_SEC * 1000));
|
2020-10-08 07:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
static void device_release_watchdog_func(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct hl_device_reset_work *device_release_watchdog_work =
|
|
|
|
container_of(work, struct hl_device_reset_work, reset_work.work);
|
|
|
|
struct hl_device *hdev = device_release_watchdog_work->hdev;
|
|
|
|
u32 flags;
|
|
|
|
|
|
|
|
dev_dbg(hdev->dev, "Device wasn't released in time. Initiate device reset.\n");
|
|
|
|
|
|
|
|
flags = device_release_watchdog_work->flags | HL_DRV_RESET_FROM_WD_THR;
|
|
|
|
|
|
|
|
hl_device_reset(hdev, flags);
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
/*
|
|
|
|
* device_early_init - do some early initialization for the habanalabs device
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
*
|
|
|
|
* Install the relevant function pointers and call the early_init function,
|
|
|
|
* if such a function exists
|
|
|
|
*/
|
|
|
|
static int device_early_init(struct hl_device *hdev)
|
|
|
|
{
|
2020-07-05 10:35:51 +00:00
|
|
|
int i, rc;
|
|
|
|
char workq_name[32];
|
2019-02-15 22:39:13 +00:00
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
switch (hdev->asic_type) {
|
|
|
|
case ASIC_GOYA:
|
2019-02-15 22:39:13 +00:00
|
|
|
goya_set_asic_funcs(hdev);
|
2021-04-03 13:37:29 +00:00
|
|
|
strscpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
|
2019-02-15 22:39:11 +00:00
|
|
|
break;
|
2020-05-11 07:47:05 +00:00
|
|
|
case ASIC_GAUDI:
|
|
|
|
gaudi_set_asic_funcs(hdev);
|
2021-04-03 13:37:29 +00:00
|
|
|
strscpy(hdev->asic_name, "GAUDI", sizeof(hdev->asic_name));
|
2020-05-11 07:47:05 +00:00
|
|
|
break;
|
2021-04-01 10:43:40 +00:00
|
|
|
case ASIC_GAUDI_SEC:
|
|
|
|
gaudi_set_asic_funcs(hdev);
|
|
|
|
strscpy(hdev->asic_name, "GAUDI SEC", sizeof(hdev->asic_name));
|
|
|
|
break;
|
2022-06-27 19:32:07 +00:00
|
|
|
case ASIC_GAUDI2:
|
|
|
|
gaudi2_set_asic_funcs(hdev);
|
|
|
|
strscpy(hdev->asic_name, "GAUDI2", sizeof(hdev->asic_name));
|
|
|
|
break;
|
2022-10-26 13:20:45 +00:00
|
|
|
case ASIC_GAUDI2B:
|
|
|
|
gaudi2_set_asic_funcs(hdev);
|
|
|
|
strscpy(hdev->asic_name, "GAUDI2B", sizeof(hdev->asic_name));
|
|
|
|
break;
|
2022-06-27 19:32:07 +00:00
|
|
|
break;
|
2019-02-15 22:39:11 +00:00
|
|
|
default:
|
|
|
|
dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
|
|
|
|
hdev->asic_type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:13 +00:00
|
|
|
rc = hdev->asic_funcs->early_init(hdev);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2019-02-15 22:39:14 +00:00
|
|
|
rc = hl_asid_init(hdev);
|
|
|
|
if (rc)
|
|
|
|
goto early_fini;
|
|
|
|
|
2020-07-05 10:35:51 +00:00
|
|
|
if (hdev->asic_prop.completion_queues_count) {
|
|
|
|
hdev->cq_wq = kcalloc(hdev->asic_prop.completion_queues_count,
|
2022-09-19 21:08:40 +00:00
|
|
|
sizeof(struct workqueue_struct *),
|
2021-02-14 13:35:56 +00:00
|
|
|
GFP_KERNEL);
|
2020-07-05 10:35:51 +00:00
|
|
|
if (!hdev->cq_wq) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto asid_fini;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) {
|
2020-08-12 07:15:27 +00:00
|
|
|
snprintf(workq_name, 32, "hl-free-jobs-%u", (u32) i);
|
2020-07-05 10:35:51 +00:00
|
|
|
hdev->cq_wq[i] = create_singlethread_workqueue(workq_name);
|
2020-07-30 08:20:22 +00:00
|
|
|
if (hdev->cq_wq[i] == NULL) {
|
2020-07-05 10:35:51 +00:00
|
|
|
dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto free_cq_wq;
|
|
|
|
}
|
2019-02-15 22:39:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 17:38:26 +00:00
|
|
|
hdev->eq_wq = create_singlethread_workqueue("hl-events");
|
2019-02-15 22:39:18 +00:00
|
|
|
if (hdev->eq_wq == NULL) {
|
|
|
|
dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto free_cq_wq;
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:05:28 +00:00
|
|
|
hdev->cs_cmplt_wq = alloc_workqueue("hl-cs-completions", WQ_UNBOUND, 0);
|
|
|
|
if (!hdev->cs_cmplt_wq) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Failed to allocate CS completions workqueue\n");
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto free_eq_wq;
|
|
|
|
}
|
|
|
|
|
2021-12-23 11:24:34 +00:00
|
|
|
hdev->ts_free_obj_wq = alloc_workqueue("hl-ts-free-obj", WQ_UNBOUND, 0);
|
|
|
|
if (!hdev->ts_free_obj_wq) {
|
2021-03-18 15:36:57 +00:00
|
|
|
dev_err(hdev->dev,
|
2021-12-23 11:24:34 +00:00
|
|
|
"Failed to allocate Timestamp registration free workqueue\n");
|
2021-03-18 15:36:57 +00:00
|
|
|
rc = -ENOMEM;
|
2022-06-27 12:05:28 +00:00
|
|
|
goto free_cs_cmplt_wq;
|
2021-03-18 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 08:38:00 +00:00
|
|
|
hdev->prefetch_wq = alloc_workqueue("hl-prefetch", WQ_UNBOUND, 0);
|
|
|
|
if (!hdev->prefetch_wq) {
|
2022-04-11 06:31:32 +00:00
|
|
|
dev_err(hdev->dev, "Failed to allocate MMU prefetch workqueue\n");
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto free_ts_free_wq;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:19 +00:00
|
|
|
hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!hdev->hl_chip_info) {
|
|
|
|
rc = -ENOMEM;
|
2022-09-28 08:38:00 +00:00
|
|
|
goto free_prefetch_wq;
|
2019-02-15 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 10:33:44 +00:00
|
|
|
rc = hl_mmu_if_set_funcs(hdev);
|
|
|
|
if (rc)
|
2021-02-23 19:31:27 +00:00
|
|
|
goto free_chip_info;
|
2020-08-12 10:33:44 +00:00
|
|
|
|
2022-12-06 17:54:10 +00:00
|
|
|
hl_mem_mgr_init(hdev->dev, &hdev->kernel_mem_mgr);
|
2019-02-15 22:39:15 +00:00
|
|
|
|
2022-09-30 11:36:27 +00:00
|
|
|
hdev->reset_wq = create_singlethread_workqueue("hl_device_reset");
|
|
|
|
if (!hdev->reset_wq) {
|
2020-10-08 07:27:42 +00:00
|
|
|
rc = -ENOMEM;
|
|
|
|
dev_err(hdev->dev, "Failed to create device reset WQ\n");
|
|
|
|
goto free_cb_mgr;
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
INIT_DELAYED_WORK(&hdev->device_reset_work.reset_work, device_hard_reset_pending);
|
2020-10-08 07:27:42 +00:00
|
|
|
hdev->device_reset_work.hdev = hdev;
|
|
|
|
hdev->device_fini_pending = 0;
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
INIT_DELAYED_WORK(&hdev->device_release_watchdog_work.reset_work,
|
|
|
|
device_release_watchdog_func);
|
|
|
|
hdev->device_release_watchdog_work.hdev = hdev;
|
|
|
|
|
2019-02-15 22:39:17 +00:00
|
|
|
mutex_init(&hdev->send_cpu_message_lock);
|
2019-05-04 14:36:06 +00:00
|
|
|
mutex_init(&hdev->debug_lock);
|
2020-10-30 09:16:23 +00:00
|
|
|
INIT_LIST_HEAD(&hdev->cs_mirror_list);
|
|
|
|
spin_lock_init(&hdev->cs_mirror_lock);
|
2021-11-23 13:15:22 +00:00
|
|
|
spin_lock_init(&hdev->reset_info.lock);
|
2019-07-30 08:56:09 +00:00
|
|
|
INIT_LIST_HEAD(&hdev->fpriv_list);
|
2021-12-08 07:52:03 +00:00
|
|
|
INIT_LIST_HEAD(&hdev->fpriv_ctrl_list);
|
2019-07-30 08:56:09 +00:00
|
|
|
mutex_init(&hdev->fpriv_list_lock);
|
2021-12-08 07:52:03 +00:00
|
|
|
mutex_init(&hdev->fpriv_ctrl_list_lock);
|
2021-10-25 06:47:04 +00:00
|
|
|
mutex_init(&hdev->clk_throttling.lock);
|
2019-02-15 22:39:14 +00:00
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
return 0;
|
2019-02-15 22:39:14 +00:00
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
free_cb_mgr:
|
2022-03-29 11:16:04 +00:00
|
|
|
hl_mem_mgr_fini(&hdev->kernel_mem_mgr);
|
2019-08-28 14:32:04 +00:00
|
|
|
free_chip_info:
|
|
|
|
kfree(hdev->hl_chip_info);
|
2022-09-28 08:38:00 +00:00
|
|
|
free_prefetch_wq:
|
|
|
|
destroy_workqueue(hdev->prefetch_wq);
|
2021-12-23 11:24:34 +00:00
|
|
|
free_ts_free_wq:
|
|
|
|
destroy_workqueue(hdev->ts_free_obj_wq);
|
2022-06-27 12:05:28 +00:00
|
|
|
free_cs_cmplt_wq:
|
|
|
|
destroy_workqueue(hdev->cs_cmplt_wq);
|
2019-02-15 22:39:19 +00:00
|
|
|
free_eq_wq:
|
|
|
|
destroy_workqueue(hdev->eq_wq);
|
2019-02-15 22:39:18 +00:00
|
|
|
free_cq_wq:
|
2020-07-05 10:35:51 +00:00
|
|
|
for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
|
|
|
|
if (hdev->cq_wq[i])
|
|
|
|
destroy_workqueue(hdev->cq_wq[i]);
|
|
|
|
kfree(hdev->cq_wq);
|
2019-02-15 22:39:17 +00:00
|
|
|
asid_fini:
|
|
|
|
hl_asid_fini(hdev);
|
2019-02-15 22:39:14 +00:00
|
|
|
early_fini:
|
|
|
|
if (hdev->asic_funcs->early_fini)
|
|
|
|
hdev->asic_funcs->early_fini(hdev);
|
|
|
|
|
|
|
|
return rc;
|
2019-02-15 22:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* device_early_fini - finalize all that was done in device_early_init
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void device_early_fini(struct hl_device *hdev)
|
|
|
|
{
|
2020-07-05 10:35:51 +00:00
|
|
|
int i;
|
|
|
|
|
2019-05-04 14:36:06 +00:00
|
|
|
mutex_destroy(&hdev->debug_lock);
|
2019-02-15 22:39:17 +00:00
|
|
|
mutex_destroy(&hdev->send_cpu_message_lock);
|
2019-02-15 22:39:13 +00:00
|
|
|
|
2019-07-30 08:56:09 +00:00
|
|
|
mutex_destroy(&hdev->fpriv_list_lock);
|
2021-12-08 07:52:03 +00:00
|
|
|
mutex_destroy(&hdev->fpriv_ctrl_list_lock);
|
2019-07-30 08:56:09 +00:00
|
|
|
|
2021-10-25 06:47:04 +00:00
|
|
|
mutex_destroy(&hdev->clk_throttling.lock);
|
|
|
|
|
2022-03-29 11:16:04 +00:00
|
|
|
hl_mem_mgr_fini(&hdev->kernel_mem_mgr);
|
2019-02-15 22:39:15 +00:00
|
|
|
|
2019-02-15 22:39:19 +00:00
|
|
|
kfree(hdev->hl_chip_info);
|
|
|
|
|
2022-09-28 08:38:00 +00:00
|
|
|
destroy_workqueue(hdev->prefetch_wq);
|
2021-12-23 11:24:34 +00:00
|
|
|
destroy_workqueue(hdev->ts_free_obj_wq);
|
2022-06-27 12:05:28 +00:00
|
|
|
destroy_workqueue(hdev->cs_cmplt_wq);
|
2019-02-15 22:39:18 +00:00
|
|
|
destroy_workqueue(hdev->eq_wq);
|
2022-09-30 11:36:27 +00:00
|
|
|
destroy_workqueue(hdev->reset_wq);
|
2020-07-05 10:35:51 +00:00
|
|
|
|
|
|
|
for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
|
|
|
|
destroy_workqueue(hdev->cq_wq[i]);
|
|
|
|
kfree(hdev->cq_wq);
|
2019-02-15 22:39:17 +00:00
|
|
|
|
2019-02-15 22:39:14 +00:00
|
|
|
hl_asid_fini(hdev);
|
|
|
|
|
2019-02-15 22:39:13 +00:00
|
|
|
if (hdev->asic_funcs->early_fini)
|
|
|
|
hdev->asic_funcs->early_fini(hdev);
|
2019-02-15 22:39:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
static void hl_device_heartbeat(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct hl_device *hdev = container_of(work, struct hl_device,
|
|
|
|
work_heartbeat.work);
|
2023-01-10 15:35:31 +00:00
|
|
|
struct hl_info_fw_err_info info = {0};
|
|
|
|
u64 event_mask = HL_NOTIFIER_EVENT_DEVICE_RESET | HL_NOTIFIER_EVENT_DEVICE_UNAVAILABLE;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2020-10-05 11:40:10 +00:00
|
|
|
if (!hl_device_operational(hdev, NULL))
|
2019-02-15 22:39:20 +00:00
|
|
|
goto reschedule;
|
|
|
|
|
|
|
|
if (!hdev->asic_funcs->send_heartbeat(hdev))
|
|
|
|
goto reschedule;
|
|
|
|
|
2021-11-13 15:58:43 +00:00
|
|
|
if (hl_device_operational(hdev, NULL))
|
|
|
|
dev_err(hdev->dev, "Device heartbeat failed!\n");
|
|
|
|
|
2023-01-10 15:35:31 +00:00
|
|
|
info.err_type = HL_INFO_FW_HEARTBEAT_ERR;
|
|
|
|
info.event_mask = &event_mask;
|
|
|
|
hl_handle_fw_err(hdev, &info);
|
|
|
|
hl_device_cond_reset(hdev, HL_DRV_RESET_HARD | HL_DRV_RESET_HEARTBEAT, event_mask);
|
2019-02-15 22:39:20 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
reschedule:
|
2021-06-21 06:57:19 +00:00
|
|
|
/*
|
|
|
|
* prev_reset_trigger tracks consecutive fatal h/w errors until first
|
|
|
|
* heartbeat immediately post reset.
|
|
|
|
* If control reached here, then at least one heartbeat work has been
|
|
|
|
* scheduled since last reset/init cycle.
|
|
|
|
* So if the device is not already in reset cycle, reset the flag
|
2021-09-16 11:00:38 +00:00
|
|
|
* prev_reset_trigger as no reset occurred with HL_DRV_RESET_FW_FATAL_ERR
|
2021-06-21 06:57:19 +00:00
|
|
|
* status for at least one heartbeat. From this point driver restarts
|
|
|
|
* tracking future consecutive fatal errors.
|
|
|
|
*/
|
2021-11-23 13:15:22 +00:00
|
|
|
if (!hdev->reset_info.in_reset)
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.prev_reset_trigger = HL_RESET_TRIGGER_DEFAULT;
|
2021-06-21 06:57:19 +00:00
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
schedule_delayed_work(&hdev->work_heartbeat,
|
|
|
|
usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:19 +00:00
|
|
|
/*
|
|
|
|
* device_late_init - do late stuff initialization for the habanalabs device
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
*
|
|
|
|
* Do stuff that either needs the device H/W queues to be active or needs
|
|
|
|
* to happen after all the rest of the initialization is finished
|
|
|
|
*/
|
|
|
|
static int device_late_init(struct hl_device *hdev)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2019-05-29 11:24:51 +00:00
|
|
|
if (hdev->asic_funcs->late_init) {
|
|
|
|
rc = hdev->asic_funcs->late_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"failed late initialization for the H/W\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:19 +00:00
|
|
|
hdev->high_pll = hdev->asic_prop.high_pll;
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
if (hdev->heartbeat) {
|
|
|
|
INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
|
|
|
|
schedule_delayed_work(&hdev->work_heartbeat,
|
|
|
|
usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:19 +00:00
|
|
|
hdev->late_init_done = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* device_late_fini - finalize all that was done in device_late_init
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void device_late_fini(struct hl_device *hdev)
|
|
|
|
{
|
|
|
|
if (!hdev->late_init_done)
|
|
|
|
return;
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
if (hdev->heartbeat)
|
|
|
|
cancel_delayed_work_sync(&hdev->work_heartbeat);
|
2019-02-15 22:39:19 +00:00
|
|
|
|
|
|
|
if (hdev->asic_funcs->late_fini)
|
|
|
|
hdev->asic_funcs->late_fini(hdev);
|
|
|
|
|
|
|
|
hdev->late_init_done = false;
|
|
|
|
}
|
|
|
|
|
2021-02-23 19:31:27 +00:00
|
|
|
int hl_device_utilization(struct hl_device *hdev, u32 *utilization)
|
2019-08-28 14:32:04 +00:00
|
|
|
{
|
2022-10-23 11:46:08 +00:00
|
|
|
u64 max_power, curr_power, dc_power, dividend, divisor;
|
2021-02-23 19:31:27 +00:00
|
|
|
int rc;
|
2019-08-28 14:32:04 +00:00
|
|
|
|
2022-01-17 22:21:49 +00:00
|
|
|
max_power = hdev->max_power;
|
2021-02-23 19:31:27 +00:00
|
|
|
dc_power = hdev->asic_prop.dc_power_default;
|
2022-10-23 11:46:08 +00:00
|
|
|
divisor = max_power - dc_power;
|
|
|
|
if (!divisor) {
|
|
|
|
dev_warn(hdev->dev, "device utilization is not supported\n");
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2021-02-23 19:31:27 +00:00
|
|
|
rc = hl_fw_cpucp_power_get(hdev, &curr_power);
|
2019-08-28 14:32:04 +00:00
|
|
|
|
2021-02-23 19:31:27 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
2019-08-28 14:32:04 +00:00
|
|
|
|
2021-02-23 19:31:27 +00:00
|
|
|
curr_power = clamp(curr_power, dc_power, max_power);
|
2019-08-28 14:32:04 +00:00
|
|
|
|
2021-02-23 19:31:27 +00:00
|
|
|
dividend = (curr_power - dc_power) * 100;
|
2022-10-23 11:46:08 +00:00
|
|
|
*utilization = (u32) div_u64(dividend, divisor);
|
2019-08-28 14:32:04 +00:00
|
|
|
|
2021-02-23 19:31:27 +00:00
|
|
|
return 0;
|
2019-08-28 14:32:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 20:32:13 +00:00
|
|
|
int hl_device_set_debug_mode(struct hl_device *hdev, struct hl_ctx *ctx, bool enable)
|
2019-05-04 14:36:06 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
mutex_lock(&hdev->debug_lock);
|
|
|
|
|
|
|
|
if (!enable) {
|
|
|
|
if (!hdev->in_debug) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Failed to disable debug mode because device was not in debug mode\n");
|
|
|
|
rc = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
if (!hdev->reset_info.hard_reset_pending)
|
2021-11-30 20:32:13 +00:00
|
|
|
hdev->asic_funcs->halt_coresight(hdev, ctx);
|
2020-01-05 09:05:45 +00:00
|
|
|
|
2019-05-04 14:36:06 +00:00
|
|
|
hdev->in_debug = 0;
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdev->in_debug) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Failed to enable debug mode because device is already in debug mode\n");
|
|
|
|
rc = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
hdev->in_debug = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&hdev->debug_lock);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-07-06 17:50:33 +00:00
|
|
|
static void take_release_locks(struct hl_device *hdev)
|
|
|
|
{
|
|
|
|
/* Flush anyone that is inside the critical section of enqueue
|
|
|
|
* jobs to the H/W
|
|
|
|
*/
|
|
|
|
hdev->asic_funcs->hw_queues_lock(hdev);
|
|
|
|
hdev->asic_funcs->hw_queues_unlock(hdev);
|
|
|
|
|
|
|
|
/* Flush processes that are sending message to CPU */
|
|
|
|
mutex_lock(&hdev->send_cpu_message_lock);
|
|
|
|
mutex_unlock(&hdev->send_cpu_message_lock);
|
|
|
|
|
|
|
|
/* Flush anyone that is inside device open */
|
|
|
|
mutex_lock(&hdev->fpriv_list_lock);
|
|
|
|
mutex_unlock(&hdev->fpriv_list_lock);
|
2021-12-08 07:52:03 +00:00
|
|
|
mutex_lock(&hdev->fpriv_ctrl_list_lock);
|
|
|
|
mutex_unlock(&hdev->fpriv_ctrl_list_lock);
|
2021-07-06 17:50:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 14:15:26 +00:00
|
|
|
static void cleanup_resources(struct hl_device *hdev, bool hard_reset, bool fw_reset,
|
|
|
|
bool skip_wq_flush)
|
2021-07-06 17:50:33 +00:00
|
|
|
{
|
|
|
|
if (hard_reset)
|
|
|
|
device_late_fini(hdev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Halt the engines and disable interrupts so we won't get any more
|
|
|
|
* completions from H/W and we won't have any accesses from the
|
|
|
|
* H/W to the host machine
|
|
|
|
*/
|
2021-08-09 19:43:37 +00:00
|
|
|
hdev->asic_funcs->halt_engines(hdev, hard_reset, fw_reset);
|
2021-07-06 17:50:33 +00:00
|
|
|
|
|
|
|
/* Go over all the queues, release all CS and their jobs */
|
2022-02-17 14:15:26 +00:00
|
|
|
hl_cs_rollback_all(hdev, skip_wq_flush);
|
2021-07-06 17:50:33 +00:00
|
|
|
|
2022-04-11 06:31:32 +00:00
|
|
|
/* flush the MMU prefetch workqueue */
|
2022-09-28 08:38:00 +00:00
|
|
|
flush_workqueue(hdev->prefetch_wq);
|
2022-04-11 06:31:32 +00:00
|
|
|
|
2021-07-06 17:50:33 +00:00
|
|
|
/* Release all pending user interrupts, each pending user interrupt
|
|
|
|
* holds a reference to user context
|
|
|
|
*/
|
|
|
|
hl_release_pending_user_interrupts(hdev);
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
/*
|
|
|
|
* hl_device_suspend - initiate device suspend
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
*
|
|
|
|
* Puts the hw in the suspend state (all asics).
|
|
|
|
* Returns 0 for success or an error on failure.
|
|
|
|
* Called at driver suspend.
|
|
|
|
*/
|
|
|
|
int hl_device_suspend(struct hl_device *hdev)
|
|
|
|
{
|
2019-02-15 22:39:13 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
pci_save_state(hdev->pdev);
|
|
|
|
|
2019-03-03 20:29:20 +00:00
|
|
|
/* Block future CS/VM/JOB completion operations */
|
2021-11-23 13:15:22 +00:00
|
|
|
spin_lock(&hdev->reset_info.lock);
|
|
|
|
if (hdev->reset_info.in_reset) {
|
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
2019-03-03 20:29:20 +00:00
|
|
|
dev_err(hdev->dev, "Can't suspend while in reset\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.in_reset = 1;
|
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
2019-03-03 20:29:20 +00:00
|
|
|
|
|
|
|
/* This blocks all other stuff that is not blocked by in_reset */
|
|
|
|
hdev->disabled = true;
|
|
|
|
|
2021-07-06 17:50:33 +00:00
|
|
|
take_release_locks(hdev);
|
2019-03-03 20:29:20 +00:00
|
|
|
|
2019-02-15 22:39:13 +00:00
|
|
|
rc = hdev->asic_funcs->suspend(hdev);
|
|
|
|
if (rc)
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Failed to disable PCI access of device CPU\n");
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
/* Shut down the device */
|
|
|
|
pci_disable_device(hdev->pdev);
|
|
|
|
pci_set_power_state(hdev->pdev, PCI_D3hot);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hl_device_resume - initiate device resume
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
*
|
|
|
|
* Bring the hw back to operating state (all asics).
|
|
|
|
* Returns 0 for success or an error on failure.
|
|
|
|
* Called at driver resume.
|
|
|
|
*/
|
|
|
|
int hl_device_resume(struct hl_device *hdev)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
pci_set_power_state(hdev->pdev, PCI_D0);
|
|
|
|
pci_restore_state(hdev->pdev);
|
2019-03-03 20:29:20 +00:00
|
|
|
rc = pci_enable_device_mem(hdev->pdev);
|
2019-02-15 22:39:11 +00:00
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Failed to enable PCI device in resume\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:29:20 +00:00
|
|
|
pci_set_master(hdev->pdev);
|
|
|
|
|
2019-02-15 22:39:13 +00:00
|
|
|
rc = hdev->asic_funcs->resume(hdev);
|
|
|
|
if (rc) {
|
2019-03-03 20:29:20 +00:00
|
|
|
dev_err(hdev->dev, "Failed to resume device after suspend\n");
|
|
|
|
goto disable_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
/* 'in_reset' was set to true during suspend, now we must clear it in order
|
|
|
|
* for hard reset to be performed
|
|
|
|
*/
|
2022-07-19 06:01:53 +00:00
|
|
|
spin_lock(&hdev->reset_info.lock);
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.in_reset = 0;
|
2022-07-19 06:01:53 +00:00
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
2019-03-03 20:29:20 +00:00
|
|
|
|
2021-09-16 11:00:38 +00:00
|
|
|
rc = hl_device_reset(hdev, HL_DRV_RESET_HARD);
|
2019-03-03 20:29:20 +00:00
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "Failed to reset device during resume\n");
|
|
|
|
goto disable_device;
|
2019-02-15 22:39:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
return 0;
|
2019-03-03 20:29:20 +00:00
|
|
|
|
|
|
|
disable_device:
|
|
|
|
pci_clear_master(hdev->pdev);
|
|
|
|
pci_disable_device(hdev->pdev);
|
|
|
|
|
|
|
|
return rc;
|
2019-02-15 22:39:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 07:52:03 +00:00
|
|
|
static int device_kill_open_processes(struct hl_device *hdev, u32 timeout, bool control_dev)
|
2019-02-15 22:39:20 +00:00
|
|
|
{
|
|
|
|
struct task_struct *task = NULL;
|
2021-12-08 07:52:03 +00:00
|
|
|
struct list_head *fd_list;
|
|
|
|
struct hl_fpriv *hpriv;
|
|
|
|
struct mutex *fd_lock;
|
2020-10-08 07:27:42 +00:00
|
|
|
u32 pending_cnt;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2021-12-08 07:52:03 +00:00
|
|
|
fd_lock = control_dev ? &hdev->fpriv_ctrl_list_lock : &hdev->fpriv_list_lock;
|
|
|
|
fd_list = control_dev ? &hdev->fpriv_ctrl_list : &hdev->fpriv_list;
|
2019-03-13 11:36:28 +00:00
|
|
|
|
2019-07-30 08:56:09 +00:00
|
|
|
/* Giving time for user to close FD, and for processes that are inside
|
|
|
|
* hl_device_open to finish
|
|
|
|
*/
|
2021-12-08 07:52:03 +00:00
|
|
|
if (!list_empty(fd_list))
|
2019-02-15 22:39:20 +00:00
|
|
|
ssleep(1);
|
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
if (timeout) {
|
|
|
|
pending_cnt = timeout;
|
|
|
|
} else {
|
|
|
|
if (hdev->process_kill_trial_cnt) {
|
|
|
|
/* Processes have been already killed */
|
|
|
|
pending_cnt = 1;
|
|
|
|
goto wait_for_processes;
|
|
|
|
} else {
|
|
|
|
/* Wait a small period after process kill */
|
|
|
|
pending_cnt = HL_PENDING_RESET_PER_SEC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:52:03 +00:00
|
|
|
mutex_lock(fd_lock);
|
2019-07-30 08:56:09 +00:00
|
|
|
|
|
|
|
/* This section must be protected because we are dereferencing
|
|
|
|
* pointers that are freed if the process exits
|
|
|
|
*/
|
2021-12-08 07:52:03 +00:00
|
|
|
list_for_each_entry(hpriv, fd_list, dev_node) {
|
2019-07-30 08:56:09 +00:00
|
|
|
task = get_pid_task(hpriv->taskpid, PIDTYPE_PID);
|
2019-02-15 22:39:20 +00:00
|
|
|
if (task) {
|
2019-07-30 06:10:50 +00:00
|
|
|
dev_info(hdev->dev, "Killing user process pid=%d\n",
|
|
|
|
task_pid_nr(task));
|
2019-02-15 22:39:20 +00:00
|
|
|
send_sig(SIGKILL, task, 1);
|
2019-07-30 08:56:09 +00:00
|
|
|
usleep_range(1000, 10000);
|
2019-02-15 22:39:20 +00:00
|
|
|
|
|
|
|
put_task_struct(task);
|
2021-07-03 08:50:32 +00:00
|
|
|
} else {
|
2022-04-27 11:14:17 +00:00
|
|
|
/*
|
|
|
|
* If we got here, it means that process was killed from outside the driver
|
|
|
|
* right after it started looping on fd_list and before get_pid_task, thus
|
|
|
|
* we don't need to kill it.
|
|
|
|
*/
|
|
|
|
dev_dbg(hdev->dev,
|
|
|
|
"Can't get task struct for user process, assuming process was killed from outside the driver\n");
|
2019-02-15 22:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:52:03 +00:00
|
|
|
mutex_unlock(fd_lock);
|
2019-07-30 08:56:09 +00:00
|
|
|
|
2020-10-31 20:03:55 +00:00
|
|
|
/*
|
|
|
|
* We killed the open users, but that doesn't mean they are closed.
|
|
|
|
* It could be that they are running a long cleanup phase in the driver
|
|
|
|
* e.g. MMU unmappings, or running other long teardown flow even before
|
|
|
|
* our cleanup.
|
|
|
|
* Therefore we need to wait again to make sure they are closed before
|
|
|
|
* continuing with the reset.
|
2019-04-06 10:23:54 +00:00
|
|
|
*/
|
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
wait_for_processes:
|
2021-12-08 07:52:03 +00:00
|
|
|
while ((!list_empty(fd_list)) && (pending_cnt)) {
|
2020-10-08 07:27:42 +00:00
|
|
|
dev_dbg(hdev->dev,
|
|
|
|
"Waiting for all unmap operations to finish before hard reset\n");
|
2019-03-13 11:36:28 +00:00
|
|
|
|
|
|
|
pending_cnt--;
|
|
|
|
|
|
|
|
ssleep(1);
|
|
|
|
}
|
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
/* All processes exited successfully */
|
2021-12-08 07:52:03 +00:00
|
|
|
if (list_empty(fd_list))
|
2020-10-08 07:27:42 +00:00
|
|
|
return 0;
|
2019-04-06 10:23:54 +00:00
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
/* Give up waiting for processes to exit */
|
|
|
|
if (hdev->process_kill_trial_cnt == HL_PENDING_RESET_MAX_TRIALS)
|
|
|
|
return -ETIME;
|
2019-04-06 10:23:54 +00:00
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
hdev->process_kill_trial_cnt++;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
return -EBUSY;
|
2019-02-15 22:39:20 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 07:52:03 +00:00
|
|
|
static void device_disable_open_processes(struct hl_device *hdev, bool control_dev)
|
2021-02-01 17:44:34 +00:00
|
|
|
{
|
2021-12-08 07:52:03 +00:00
|
|
|
struct list_head *fd_list;
|
2021-02-01 17:44:34 +00:00
|
|
|
struct hl_fpriv *hpriv;
|
2021-12-08 07:52:03 +00:00
|
|
|
struct mutex *fd_lock;
|
2021-02-01 17:44:34 +00:00
|
|
|
|
2021-12-08 07:52:03 +00:00
|
|
|
fd_lock = control_dev ? &hdev->fpriv_ctrl_list_lock : &hdev->fpriv_list_lock;
|
|
|
|
fd_list = control_dev ? &hdev->fpriv_ctrl_list : &hdev->fpriv_list;
|
|
|
|
|
|
|
|
mutex_lock(fd_lock);
|
|
|
|
list_for_each_entry(hpriv, fd_list, dev_node)
|
2021-02-01 17:44:34 +00:00
|
|
|
hpriv->hdev = NULL;
|
2021-12-08 07:52:03 +00:00
|
|
|
mutex_unlock(fd_lock);
|
2021-02-01 17:44:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 06:57:19 +00:00
|
|
|
static void handle_reset_trigger(struct hl_device *hdev, u32 flags)
|
|
|
|
{
|
|
|
|
u32 cur_reset_trigger = HL_RESET_TRIGGER_DEFAULT;
|
|
|
|
|
2022-10-18 14:35:06 +00:00
|
|
|
/* No consecutive mechanism when user context exists */
|
|
|
|
if (hdev->is_compute_ctx_active)
|
|
|
|
return;
|
|
|
|
|
2021-06-21 06:57:19 +00:00
|
|
|
/*
|
|
|
|
* 'reset cause' is being updated here, because getting here
|
|
|
|
* means that it's the 1st time and the last time we're here
|
|
|
|
* ('in_reset' makes sure of it). This makes sure that
|
|
|
|
* 'reset_cause' will continue holding its 1st recorded reason!
|
|
|
|
*/
|
2021-09-16 11:00:38 +00:00
|
|
|
if (flags & HL_DRV_RESET_HEARTBEAT) {
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_HEARTBEAT;
|
2021-09-16 11:00:38 +00:00
|
|
|
cur_reset_trigger = HL_DRV_RESET_HEARTBEAT;
|
|
|
|
} else if (flags & HL_DRV_RESET_TDR) {
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_TDR;
|
2021-09-16 11:00:38 +00:00
|
|
|
cur_reset_trigger = HL_DRV_RESET_TDR;
|
|
|
|
} else if (flags & HL_DRV_RESET_FW_FATAL_ERR) {
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;
|
2021-09-16 11:00:38 +00:00
|
|
|
cur_reset_trigger = HL_DRV_RESET_FW_FATAL_ERR;
|
2021-06-21 06:57:19 +00:00
|
|
|
} else {
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;
|
2021-06-21 06:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If reset cause is same twice, then reset_trigger_repeated
|
|
|
|
* is set and if this reset is due to a fatal FW error
|
|
|
|
* device is set to an unstable state.
|
|
|
|
*/
|
2021-11-23 13:15:22 +00:00
|
|
|
if (hdev->reset_info.prev_reset_trigger != cur_reset_trigger) {
|
|
|
|
hdev->reset_info.prev_reset_trigger = cur_reset_trigger;
|
|
|
|
hdev->reset_info.reset_trigger_repeated = 0;
|
2021-06-21 06:57:19 +00:00
|
|
|
} else {
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.reset_trigger_repeated = 1;
|
2021-06-21 06:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If reset is due to heartbeat, device CPU is no responsive in
|
|
|
|
* which case no point sending PCI disable message to it.
|
|
|
|
*
|
|
|
|
* If F/W is performing the reset, no need to send it a message to disable
|
|
|
|
* PCI access
|
|
|
|
*/
|
2021-09-16 11:00:38 +00:00
|
|
|
if ((flags & HL_DRV_RESET_HARD) &&
|
|
|
|
!(flags & (HL_DRV_RESET_HEARTBEAT | HL_DRV_RESET_BYPASS_REQ_TO_FW))) {
|
2021-06-21 06:57:19 +00:00
|
|
|
/* Disable PCI access from device F/W so he won't send
|
|
|
|
* us additional interrupts. We disable MSI/MSI-X at
|
|
|
|
* the halt_engines function and we can't have the F/W
|
|
|
|
* sending us interrupts after that. We need to disable
|
|
|
|
* the access here because if the device is marked
|
|
|
|
* disable, the message won't be send. Also, in case
|
|
|
|
* of heartbeat, the device CPU is marked as disable
|
|
|
|
* so this message won't be sent
|
|
|
|
*/
|
2022-07-06 07:20:54 +00:00
|
|
|
if (hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0))
|
2021-06-21 06:57:19 +00:00
|
|
|
dev_warn(hdev->dev,
|
|
|
|
"Failed to disable PCI access by F/W\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/*
|
|
|
|
* hl_device_reset - reset the device
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
2021-02-17 18:42:48 +00:00
|
|
|
* @flags: reset flags.
|
2019-02-15 22:39:20 +00:00
|
|
|
*
|
|
|
|
* Block future CS and wait for pending CS to be enqueued
|
|
|
|
* Call ASIC H/W fini
|
|
|
|
* Flush all completions
|
|
|
|
* Re-initialize all internal data structures
|
|
|
|
* Call ASIC H/W init, late_init
|
|
|
|
* Test queues
|
|
|
|
* Enable device
|
|
|
|
*
|
|
|
|
* Returns 0 for success or an error on failure.
|
|
|
|
*/
|
2021-02-17 18:42:48 +00:00
|
|
|
int hl_device_reset(struct hl_device *hdev, u32 flags)
|
2019-02-15 22:39:20 +00:00
|
|
|
{
|
2021-11-16 07:46:02 +00:00
|
|
|
bool hard_reset, from_hard_reset_thread, fw_reset, hard_instead_soft = false,
|
habanalabs: block soft-reset on an unusable device
A device with status malfunction indicates that it can't be used.
In such a case we do not support certain reset types, e.g.,
all kinds of soft-resets (compute reset, inference soft-reset),
and reset upon device release.
A hard-reset is the only way that an unusable device can change its
status. All other reset procedures can't put the device in a reset
procedure, which might ultimately cause the device to change its
status, unintentionally, to become operational again.
Such a scenario has recently occurred, when a user requested
a hard-reset while another heavy user workload was ongoing (reset
request is queued).
Since the workload couldn't finish within reset's timeout limits, the
reset has failed and set a device status malfunction.
Eventually, when the user released the FD, an unsuccessful soft-reset
occurred, hence followed by an additional hard-reset that changed the
ASICs status back to be operational.
Signed-off-by: Koby Elbaz <kelbaz@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
2023-01-11 13:43:21 +00:00
|
|
|
reset_upon_device_release = false, schedule_hard_reset = false,
|
|
|
|
delay_reset, from_dev_release, from_watchdog_thread;
|
2021-11-15 15:13:37 +00:00
|
|
|
u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {0};
|
2021-11-30 21:02:21 +00:00
|
|
|
struct hl_ctx *ctx;
|
2019-02-15 22:39:20 +00:00
|
|
|
int i, rc;
|
|
|
|
|
|
|
|
if (!hdev->init_done) {
|
2021-11-16 07:46:02 +00:00
|
|
|
dev_err(hdev->dev, "Can't reset before initialization is done\n");
|
2019-02-15 22:39:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:00:38 +00:00
|
|
|
hard_reset = !!(flags & HL_DRV_RESET_HARD);
|
|
|
|
from_hard_reset_thread = !!(flags & HL_DRV_RESET_FROM_RESET_THR);
|
|
|
|
fw_reset = !!(flags & HL_DRV_RESET_BYPASS_REQ_TO_FW);
|
2022-09-30 12:08:13 +00:00
|
|
|
from_dev_release = !!(flags & HL_DRV_RESET_DEV_RELEASE);
|
2022-02-23 12:46:37 +00:00
|
|
|
delay_reset = !!(flags & HL_DRV_RESET_DELAY);
|
2022-09-30 12:08:13 +00:00
|
|
|
from_watchdog_thread = !!(flags & HL_DRV_RESET_FROM_WD_THR);
|
2021-02-17 18:42:48 +00:00
|
|
|
|
habanalabs: block soft-reset on an unusable device
A device with status malfunction indicates that it can't be used.
In such a case we do not support certain reset types, e.g.,
all kinds of soft-resets (compute reset, inference soft-reset),
and reset upon device release.
A hard-reset is the only way that an unusable device can change its
status. All other reset procedures can't put the device in a reset
procedure, which might ultimately cause the device to change its
status, unintentionally, to become operational again.
Such a scenario has recently occurred, when a user requested
a hard-reset while another heavy user workload was ongoing (reset
request is queued).
Since the workload couldn't finish within reset's timeout limits, the
reset has failed and set a device status malfunction.
Eventually, when the user released the FD, an unsuccessful soft-reset
occurred, hence followed by an additional hard-reset that changed the
ASICs status back to be operational.
Signed-off-by: Koby Elbaz <kelbaz@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
2023-01-11 13:43:21 +00:00
|
|
|
if (!hard_reset && (hl_device_status(hdev) == HL_DEVICE_STATUS_MALFUNCTION)) {
|
|
|
|
dev_dbg(hdev->dev, "soft-reset isn't supported on a malfunctioning device\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-07 09:00:24 +00:00
|
|
|
if (!hard_reset && !hdev->asic_prop.supports_compute_reset) {
|
2021-06-08 14:24:52 +00:00
|
|
|
hard_instead_soft = true;
|
2020-05-18 13:48:01 +00:00
|
|
|
hard_reset = true;
|
|
|
|
}
|
|
|
|
|
habanalabs: block soft-reset on an unusable device
A device with status malfunction indicates that it can't be used.
In such a case we do not support certain reset types, e.g.,
all kinds of soft-resets (compute reset, inference soft-reset),
and reset upon device release.
A hard-reset is the only way that an unusable device can change its
status. All other reset procedures can't put the device in a reset
procedure, which might ultimately cause the device to change its
status, unintentionally, to become operational again.
Such a scenario has recently occurred, when a user requested
a hard-reset while another heavy user workload was ongoing (reset
request is queued).
Since the workload couldn't finish within reset's timeout limits, the
reset has failed and set a device status malfunction.
Eventually, when the user released the FD, an unsuccessful soft-reset
occurred, hence followed by an additional hard-reset that changed the
ASICs status back to be operational.
Signed-off-by: Koby Elbaz <kelbaz@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
2023-01-11 13:43:21 +00:00
|
|
|
if (hdev->reset_upon_device_release && from_dev_release) {
|
2021-11-15 15:13:37 +00:00
|
|
|
if (hard_reset) {
|
|
|
|
dev_crit(hdev->dev,
|
|
|
|
"Aborting reset because hard-reset is mutually exclusive with reset-on-device-release\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-11-16 07:46:02 +00:00
|
|
|
reset_upon_device_release = true;
|
|
|
|
|
2021-06-08 14:24:52 +00:00
|
|
|
goto do_reset;
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
if (!hard_reset && !hdev->asic_prop.allow_inference_soft_reset) {
|
2021-06-08 14:24:52 +00:00
|
|
|
hard_instead_soft = true;
|
|
|
|
hard_reset = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hard_instead_soft)
|
2022-07-07 09:00:24 +00:00
|
|
|
dev_dbg(hdev->dev, "Doing hard-reset instead of compute reset\n");
|
2021-06-08 14:24:52 +00:00
|
|
|
|
|
|
|
do_reset:
|
2020-10-08 07:27:42 +00:00
|
|
|
/* Re-entry of reset thread */
|
|
|
|
if (from_hard_reset_thread && hdev->process_kill_trial_cnt)
|
|
|
|
goto kill_processes;
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/*
|
|
|
|
* Prevent concurrency in this function - only one reset should be
|
|
|
|
* done at any given time. Only need to perform this if we didn't
|
|
|
|
* get from the dedicated hard reset thread
|
|
|
|
*/
|
|
|
|
if (!from_hard_reset_thread) {
|
|
|
|
/* Block future CS/VM/JOB completion operations */
|
2021-11-23 13:15:22 +00:00
|
|
|
spin_lock(&hdev->reset_info.lock);
|
|
|
|
if (hdev->reset_info.in_reset) {
|
2022-07-07 09:00:24 +00:00
|
|
|
/* We only allow scheduling of a hard reset during compute reset */
|
|
|
|
if (hard_reset && hdev->reset_info.in_compute_reset)
|
2021-11-23 14:34:28 +00:00
|
|
|
hdev->reset_info.hard_reset_schedule_flags = flags;
|
2021-11-23 13:15:22 +00:00
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
2019-02-15 22:39:20 +00:00
|
|
|
return 0;
|
2021-11-23 13:15:22 +00:00
|
|
|
}
|
2022-07-07 08:39:19 +00:00
|
|
|
|
|
|
|
/* This still allows the completion of some KDMA ops
|
2022-07-07 09:00:24 +00:00
|
|
|
* Update this before in_reset because in_compute_reset implies we are in reset
|
2022-07-07 08:39:19 +00:00
|
|
|
*/
|
2022-07-07 09:00:24 +00:00
|
|
|
hdev->reset_info.in_compute_reset = !hard_reset;
|
2022-07-07 08:39:19 +00:00
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.in_reset = 1;
|
2022-07-07 08:39:19 +00:00
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
/* Cancel the device release watchdog work if required.
|
|
|
|
* In case of reset-upon-device-release while the release watchdog work is
|
|
|
|
* scheduled, do hard-reset instead of compute-reset.
|
|
|
|
*/
|
|
|
|
if ((hard_reset || from_dev_release) && hdev->reset_info.watchdog_active) {
|
|
|
|
hdev->reset_info.watchdog_active = 0;
|
|
|
|
if (!from_watchdog_thread)
|
|
|
|
cancel_delayed_work_sync(
|
|
|
|
&hdev->device_release_watchdog_work.reset_work);
|
|
|
|
|
|
|
|
if (from_dev_release) {
|
2023-01-17 17:45:24 +00:00
|
|
|
hdev->reset_info.in_compute_reset = 0;
|
2022-09-30 12:08:13 +00:00
|
|
|
flags |= HL_DRV_RESET_HARD;
|
|
|
|
flags &= ~HL_DRV_RESET_DEV_RELEASE;
|
|
|
|
hard_reset = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 12:46:37 +00:00
|
|
|
if (delay_reset)
|
|
|
|
usleep_range(HL_RESET_DELAY_USEC, HL_RESET_DELAY_USEC << 1);
|
|
|
|
|
2021-06-21 06:57:19 +00:00
|
|
|
handle_reset_trigger(hdev, flags);
|
2020-07-05 12:48:34 +00:00
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/* This also blocks future CS/VM/JOB completion operations */
|
|
|
|
hdev->disabled = true;
|
|
|
|
|
2021-07-01 07:09:28 +00:00
|
|
|
take_release_locks(hdev);
|
2019-07-30 08:56:09 +00:00
|
|
|
|
2021-09-30 08:53:37 +00:00
|
|
|
if (hard_reset)
|
|
|
|
dev_info(hdev->dev, "Going to reset device\n");
|
2021-11-16 07:46:02 +00:00
|
|
|
else if (reset_upon_device_release)
|
2022-04-02 19:09:16 +00:00
|
|
|
dev_dbg(hdev->dev, "Going to reset device after release by user\n");
|
2021-09-30 08:53:37 +00:00
|
|
|
else
|
2022-04-02 19:09:16 +00:00
|
|
|
dev_dbg(hdev->dev, "Going to reset engines of inference device\n");
|
2019-02-15 22:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
|
|
|
if ((hard_reset) && (!from_hard_reset_thread)) {
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.hard_reset_pending = true;
|
2019-04-06 12:41:35 +00:00
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
hdev->process_kill_trial_cnt = 0;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2021-11-22 10:29:22 +00:00
|
|
|
hdev->device_reset_work.flags = flags;
|
2021-08-09 19:43:37 +00:00
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/*
|
2021-05-04 17:10:47 +00:00
|
|
|
* Because the reset function can't run from heartbeat work,
|
|
|
|
* we need to call the reset function from a dedicated work.
|
2019-02-15 22:39:20 +00:00
|
|
|
*/
|
2022-09-30 11:36:27 +00:00
|
|
|
queue_delayed_work(hdev->reset_wq, &hdev->device_reset_work.reset_work, 0);
|
2019-02-15 22:39:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
cleanup_resources(hdev, hard_reset, fw_reset, from_dev_release);
|
2021-01-12 16:37:19 +00:00
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
kill_processes:
|
2019-11-17 15:41:57 +00:00
|
|
|
if (hard_reset) {
|
|
|
|
/* Kill processes here after CS rollback. This is because the
|
|
|
|
* process can't really exit until all its CSs are done, which
|
|
|
|
* is what we do in cs rollback
|
|
|
|
*/
|
2021-12-08 07:52:03 +00:00
|
|
|
rc = device_kill_open_processes(hdev, 0, false);
|
2020-10-08 07:27:42 +00:00
|
|
|
|
|
|
|
if (rc == -EBUSY) {
|
|
|
|
if (hdev->device_fini_pending) {
|
|
|
|
dev_crit(hdev->dev,
|
2022-12-29 10:44:09 +00:00
|
|
|
"%s Failed to kill all open processes, stopping hard reset\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2020-10-08 07:27:42 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* signal reset thread to reschedule */
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:27:46 +00:00
|
|
|
if (rc) {
|
|
|
|
dev_crit(hdev->dev,
|
2022-12-29 10:44:09 +00:00
|
|
|
"%s Failed to kill all open processes, stopping hard reset\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2020-05-18 19:27:46 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
2019-07-22 14:37:22 +00:00
|
|
|
|
2019-11-17 15:41:57 +00:00
|
|
|
/* Flush the Event queue workers to make sure no other thread is
|
|
|
|
* reading or writing to registers during the reset
|
|
|
|
*/
|
|
|
|
flush_workqueue(hdev->eq_wq);
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/* Reset the H/W. It will be in idle state after this returns */
|
2021-08-09 19:43:37 +00:00
|
|
|
hdev->asic_funcs->hw_fini(hdev, hard_reset, fw_reset);
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2019-02-15 22:39:22 +00:00
|
|
|
if (hard_reset) {
|
2021-10-21 08:24:41 +00:00
|
|
|
hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE;
|
2021-07-09 14:34:45 +00:00
|
|
|
|
2020-09-23 12:06:52 +00:00
|
|
|
/* Release kernel context */
|
2021-01-11 13:00:38 +00:00
|
|
|
if (hdev->kernel_ctx && hl_ctx_put(hdev->kernel_ctx) == 1)
|
2020-09-23 12:06:52 +00:00
|
|
|
hdev->kernel_ctx = NULL;
|
2021-07-09 14:34:45 +00:00
|
|
|
|
2019-02-15 22:39:22 +00:00
|
|
|
hl_vm_fini(hdev);
|
2019-05-29 11:43:04 +00:00
|
|
|
hl_mmu_fini(hdev);
|
2019-02-15 22:39:20 +00:00
|
|
|
hl_eq_reset(hdev, &hdev->event_queue);
|
2019-02-15 22:39:22 +00:00
|
|
|
}
|
2019-02-15 22:39:20 +00:00
|
|
|
|
|
|
|
/* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
|
|
|
|
hl_hw_queue_reset(hdev, hard_reset);
|
|
|
|
for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
|
|
|
|
hl_cq_reset(hdev, &hdev->completion_queue[i]);
|
|
|
|
|
2019-04-25 17:15:42 +00:00
|
|
|
/* Make sure the context switch phase will run again */
|
2021-11-30 21:02:21 +00:00
|
|
|
ctx = hl_get_compute_ctx(hdev);
|
|
|
|
if (ctx) {
|
|
|
|
atomic_set(&ctx->thread_ctx_switch_token, 1);
|
|
|
|
ctx->thread_ctx_switch_wait_token = 0;
|
|
|
|
hl_ctx_put(ctx);
|
2019-02-15 22:39:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/* Finished tear-down, starting to re-initialize */
|
|
|
|
|
|
|
|
if (hard_reset) {
|
2019-02-28 08:46:12 +00:00
|
|
|
hdev->device_cpu_disabled = false;
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.hard_reset_pending = false;
|
2019-02-28 08:46:12 +00:00
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
if (hdev->reset_info.reset_trigger_repeated &&
|
|
|
|
(hdev->reset_info.prev_reset_trigger ==
|
|
|
|
HL_DRV_RESET_FW_FATAL_ERR)) {
|
2021-06-21 06:57:19 +00:00
|
|
|
/* if there 2 back to back resets from FW,
|
|
|
|
* ensure driver puts the driver in a unusable state
|
|
|
|
*/
|
|
|
|
dev_crit(hdev->dev,
|
2022-12-29 10:44:09 +00:00
|
|
|
"%s Consecutive FW fatal errors received, stopping hard reset\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2021-06-21 06:57:19 +00:00
|
|
|
rc = -EIO;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2019-03-17 07:12:29 +00:00
|
|
|
if (hdev->kernel_ctx) {
|
|
|
|
dev_crit(hdev->dev,
|
2022-12-29 10:44:09 +00:00
|
|
|
"%s kernel ctx was alive during hard reset, something is terribly wrong\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2019-03-17 07:12:29 +00:00
|
|
|
rc = -EBUSY;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:43:04 +00:00
|
|
|
rc = hl_mmu_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Failed to initialize MMU S/W after hard reset\n");
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/* Allocate the kernel context */
|
|
|
|
hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!hdev->kernel_ctx) {
|
|
|
|
rc = -ENOMEM;
|
2020-12-26 07:27:14 +00:00
|
|
|
hl_mmu_fini(hdev);
|
2019-02-15 22:39:20 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:08:21 +00:00
|
|
|
hdev->is_compute_ctx_active = false;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
|
|
|
rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"failed to init kernel ctx in hard reset\n");
|
|
|
|
kfree(hdev->kernel_ctx);
|
|
|
|
hdev->kernel_ctx = NULL;
|
2020-12-26 07:27:14 +00:00
|
|
|
hl_mmu_fini(hdev);
|
2019-02-15 22:39:20 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:59:32 +00:00
|
|
|
/* Device is now enabled as part of the initialization requires
|
|
|
|
* communication with the device firmware to get information that
|
|
|
|
* is required for the initialization itself
|
|
|
|
*/
|
|
|
|
hdev->disabled = false;
|
|
|
|
|
2022-09-01 11:12:56 +00:00
|
|
|
/* F/W security enabled indication might be updated after hard-reset */
|
|
|
|
if (hard_reset) {
|
|
|
|
rc = hl_fw_read_preboot_status(hdev);
|
|
|
|
if (rc)
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
rc = hdev->asic_funcs->hw_init(hdev);
|
|
|
|
if (rc) {
|
2021-11-16 07:46:02 +00:00
|
|
|
dev_err(hdev->dev, "failed to initialize the H/W after reset\n");
|
2019-02-15 22:39:20 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2021-02-16 20:46:17 +00:00
|
|
|
/* If device is not idle fail the reset process */
|
|
|
|
if (!hdev->asic_funcs->is_device_idle(hdev, idle_mask,
|
2022-11-17 16:57:49 +00:00
|
|
|
HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL)) {
|
|
|
|
print_idle_status_mask(hdev, "device is not idle after reset", idle_mask);
|
2021-02-16 20:46:17 +00:00
|
|
|
rc = -EIO;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/* Check that the communication with the device is working */
|
|
|
|
rc = hdev->asic_funcs->test_queues(hdev);
|
|
|
|
if (rc) {
|
2021-11-16 07:46:02 +00:00
|
|
|
dev_err(hdev->dev, "Failed to detect if device is alive after reset\n");
|
2019-02-15 22:39:20 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hard_reset) {
|
|
|
|
rc = device_late_init(hdev);
|
|
|
|
if (rc) {
|
2021-11-16 07:46:02 +00:00
|
|
|
dev_err(hdev->dev, "Failed late init after hard reset\n");
|
2019-02-15 22:39:20 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:22 +00:00
|
|
|
rc = hl_vm_init(hdev);
|
|
|
|
if (rc) {
|
2021-11-16 07:46:02 +00:00
|
|
|
dev_err(hdev->dev, "Failed to init memory module after hard reset\n");
|
2019-02-15 22:39:22 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2022-06-30 11:37:03 +00:00
|
|
|
if (!hdev->asic_prop.fw_security_enabled)
|
|
|
|
hl_fw_set_max_power(hdev);
|
2019-02-15 22:39:20 +00:00
|
|
|
} else {
|
2022-07-13 12:08:09 +00:00
|
|
|
rc = hdev->asic_funcs->compute_reset_late_init(hdev);
|
2019-02-15 22:39:20 +00:00
|
|
|
if (rc) {
|
2021-11-16 07:46:02 +00:00
|
|
|
if (reset_upon_device_release)
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Failed late init in reset after device release\n");
|
|
|
|
else
|
2022-07-07 09:00:24 +00:00
|
|
|
dev_err(hdev->dev, "Failed late init after compute reset\n");
|
2019-02-15 22:39:20 +00:00
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 12:20:55 +00:00
|
|
|
rc = hdev->asic_funcs->scrub_device_mem(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "scrub mem failed from device reset (%d)\n", rc);
|
2023-01-17 13:50:41 +00:00
|
|
|
goto out_err;
|
2022-05-12 12:20:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 14:34:28 +00:00
|
|
|
spin_lock(&hdev->reset_info.lock);
|
2022-07-07 09:00:24 +00:00
|
|
|
hdev->reset_info.in_compute_reset = 0;
|
2021-11-23 14:34:28 +00:00
|
|
|
|
|
|
|
/* Schedule hard reset only if requested and if not already in hard reset.
|
|
|
|
* We keep 'in_reset' enabled, so no other reset can go in during the hard
|
|
|
|
* reset schedule
|
|
|
|
*/
|
|
|
|
if (!hard_reset && hdev->reset_info.hard_reset_schedule_flags)
|
|
|
|
schedule_hard_reset = true;
|
|
|
|
else
|
|
|
|
hdev->reset_info.in_reset = 0;
|
|
|
|
|
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.needs_reset = false;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2022-04-02 19:09:16 +00:00
|
|
|
if (hard_reset)
|
2022-12-29 10:44:09 +00:00
|
|
|
dev_info(hdev->dev,
|
|
|
|
"Successfully finished resetting the %s device\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2022-04-02 19:09:16 +00:00
|
|
|
else
|
2022-12-29 10:44:09 +00:00
|
|
|
dev_dbg(hdev->dev,
|
|
|
|
"Successfully finished resetting the %s device\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2021-02-01 19:23:43 +00:00
|
|
|
|
|
|
|
if (hard_reset) {
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.hard_reset_cnt++;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2021-02-01 19:23:43 +00:00
|
|
|
/* After reset is done, we are ready to receive events from
|
|
|
|
* the F/W. We can't do it before because we will ignore events
|
|
|
|
* and if those events are fatal, we won't know about it and
|
|
|
|
* the device will be operational although it shouldn't be
|
|
|
|
*/
|
|
|
|
hdev->asic_funcs->enable_events_from_fw(hdev);
|
2022-11-10 15:05:24 +00:00
|
|
|
} else {
|
|
|
|
if (!reset_upon_device_release)
|
|
|
|
hdev->reset_info.compute_reset_cnt++;
|
|
|
|
|
|
|
|
if (schedule_hard_reset) {
|
|
|
|
dev_info(hdev->dev, "Performing hard reset scheduled during compute reset\n");
|
|
|
|
flags = hdev->reset_info.hard_reset_schedule_flags;
|
|
|
|
hdev->reset_info.hard_reset_schedule_flags = 0;
|
|
|
|
hdev->disabled = true;
|
|
|
|
hard_reset = true;
|
|
|
|
handle_reset_trigger(hdev, flags);
|
|
|
|
goto again;
|
|
|
|
}
|
2021-11-23 14:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_err:
|
|
|
|
hdev->disabled = true;
|
2022-07-07 08:39:19 +00:00
|
|
|
|
|
|
|
spin_lock(&hdev->reset_info.lock);
|
2022-07-07 09:00:24 +00:00
|
|
|
hdev->reset_info.in_compute_reset = 0;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
|
|
|
if (hard_reset) {
|
2022-12-29 10:44:09 +00:00
|
|
|
dev_err(hdev->dev,
|
|
|
|
"%s Failed to reset! Device is NOT usable\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.hard_reset_cnt++;
|
2021-11-16 07:46:02 +00:00
|
|
|
} else if (reset_upon_device_release) {
|
2022-07-07 08:39:19 +00:00
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
2021-11-16 07:46:02 +00:00
|
|
|
dev_err(hdev->dev, "Failed to reset device after user release\n");
|
2022-01-11 09:10:20 +00:00
|
|
|
flags |= HL_DRV_RESET_HARD;
|
|
|
|
flags &= ~HL_DRV_RESET_DEV_RELEASE;
|
2021-11-16 07:46:02 +00:00
|
|
|
hard_reset = true;
|
|
|
|
goto again;
|
2019-02-15 22:39:20 +00:00
|
|
|
} else {
|
2022-07-07 08:39:19 +00:00
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
2022-07-07 09:00:24 +00:00
|
|
|
dev_err(hdev->dev, "Failed to do compute reset\n");
|
|
|
|
hdev->reset_info.compute_reset_cnt++;
|
2022-01-11 09:10:20 +00:00
|
|
|
flags |= HL_DRV_RESET_HARD;
|
2019-02-15 22:39:20 +00:00
|
|
|
hard_reset = true;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.in_reset = 0;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2022-07-07 08:39:19 +00:00
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
/*
|
|
|
|
* hl_device_cond_reset() - conditionally reset the device.
|
|
|
|
* @hdev: pointer to habanalabs device structure.
|
|
|
|
* @reset_flags: reset flags.
|
|
|
|
* @event_mask: events to notify user about.
|
|
|
|
*
|
|
|
|
* Conditionally reset the device, or alternatively schedule a watchdog work to reset the device
|
|
|
|
* unless another reset precedes it.
|
|
|
|
*/
|
|
|
|
int hl_device_cond_reset(struct hl_device *hdev, u32 flags, u64 event_mask)
|
|
|
|
{
|
|
|
|
struct hl_ctx *ctx = NULL;
|
|
|
|
|
|
|
|
/* Device release watchdog is only for hard reset */
|
|
|
|
if (!(flags & HL_DRV_RESET_HARD) && hdev->asic_prop.allow_inference_soft_reset)
|
|
|
|
goto device_reset;
|
|
|
|
|
|
|
|
/* F/W reset cannot be postponed */
|
|
|
|
if (flags & HL_DRV_RESET_BYPASS_REQ_TO_FW)
|
|
|
|
goto device_reset;
|
|
|
|
|
|
|
|
/* Device release watchdog is relevant only if user exists and gets a reset notification */
|
|
|
|
if (!(event_mask & HL_NOTIFIER_EVENT_DEVICE_RESET)) {
|
|
|
|
dev_err(hdev->dev, "Resetting device without a reset indication to user\n");
|
|
|
|
goto device_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = hl_get_compute_ctx(hdev);
|
|
|
|
if (!ctx || !ctx->hpriv->notifier_event.eventfd)
|
|
|
|
goto device_reset;
|
|
|
|
|
|
|
|
/* Schedule the device release watchdog work unless reset is already in progress or if the
|
|
|
|
* work is already scheduled.
|
|
|
|
*/
|
|
|
|
spin_lock(&hdev->reset_info.lock);
|
|
|
|
if (hdev->reset_info.in_reset) {
|
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
|
|
|
goto device_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdev->reset_info.watchdog_active)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
hdev->device_release_watchdog_work.flags = flags;
|
|
|
|
dev_dbg(hdev->dev, "Device is going to be reset in %u sec unless being released\n",
|
|
|
|
hdev->device_release_watchdog_timeout_sec);
|
|
|
|
schedule_delayed_work(&hdev->device_release_watchdog_work.reset_work,
|
|
|
|
msecs_to_jiffies(hdev->device_release_watchdog_timeout_sec * 1000));
|
|
|
|
hdev->reset_info.watchdog_active = 1;
|
|
|
|
out:
|
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
|
|
|
|
|
|
|
hl_notifier_event_send_all(hdev, event_mask);
|
|
|
|
|
|
|
|
hl_ctx_put(ctx);
|
|
|
|
|
2022-11-08 12:34:43 +00:00
|
|
|
hl_abort_waitings_for_completion(hdev);
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
device_reset:
|
|
|
|
if (event_mask)
|
|
|
|
hl_notifier_event_send_all(hdev, event_mask);
|
|
|
|
if (ctx)
|
|
|
|
hl_ctx_put(ctx);
|
|
|
|
|
|
|
|
return hl_device_reset(hdev, flags);
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:02:39 +00:00
|
|
|
static void hl_notifier_event_send(struct hl_notifier_event *notifier_event, u64 event_mask)
|
2022-04-28 10:45:18 +00:00
|
|
|
{
|
|
|
|
mutex_lock(¬ifier_event->lock);
|
2022-05-11 15:02:39 +00:00
|
|
|
notifier_event->events_mask |= event_mask;
|
|
|
|
|
2022-04-28 10:45:18 +00:00
|
|
|
if (notifier_event->eventfd)
|
|
|
|
eventfd_signal(notifier_event->eventfd, 1);
|
|
|
|
|
|
|
|
mutex_unlock(¬ifier_event->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hl_notifier_event_send_all - notify all user processes via eventfd
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
2022-05-11 15:02:39 +00:00
|
|
|
* @event_mask: the occurred event/s
|
2022-04-28 10:45:18 +00:00
|
|
|
* Returns 0 for success or an error on failure.
|
|
|
|
*/
|
2022-05-11 15:02:39 +00:00
|
|
|
void hl_notifier_event_send_all(struct hl_device *hdev, u64 event_mask)
|
2022-04-28 10:45:18 +00:00
|
|
|
{
|
|
|
|
struct hl_fpriv *hpriv;
|
|
|
|
|
2022-10-03 10:55:50 +00:00
|
|
|
if (!event_mask) {
|
|
|
|
dev_warn(hdev->dev, "Skip sending zero event");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-28 10:45:18 +00:00
|
|
|
mutex_lock(&hdev->fpriv_list_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node)
|
2022-05-11 15:02:39 +00:00
|
|
|
hl_notifier_event_send(&hpriv->notifier_event, event_mask);
|
2022-04-28 10:45:18 +00:00
|
|
|
|
|
|
|
mutex_unlock(&hdev->fpriv_list_lock);
|
|
|
|
|
|
|
|
/* control device */
|
|
|
|
mutex_lock(&hdev->fpriv_ctrl_list_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(hpriv, &hdev->fpriv_ctrl_list, dev_node)
|
2022-05-11 15:02:39 +00:00
|
|
|
hl_notifier_event_send(&hpriv->notifier_event, event_mask);
|
2022-04-28 10:45:18 +00:00
|
|
|
|
|
|
|
mutex_unlock(&hdev->fpriv_ctrl_list_lock);
|
|
|
|
}
|
|
|
|
|
2023-01-22 15:11:31 +00:00
|
|
|
static int create_cdev(struct hl_device *hdev)
|
2019-02-15 22:39:11 +00:00
|
|
|
{
|
2019-07-30 06:10:02 +00:00
|
|
|
char *name;
|
2023-01-22 12:59:11 +00:00
|
|
|
int rc;
|
2019-07-30 06:10:02 +00:00
|
|
|
|
2022-08-04 14:32:30 +00:00
|
|
|
hdev->cdev_idx = hdev->id / 2;
|
|
|
|
|
|
|
|
name = kasprintf(GFP_KERNEL, "hl%d", hdev->cdev_idx);
|
2019-08-08 12:25:52 +00:00
|
|
|
if (!name) {
|
|
|
|
rc = -ENOMEM;
|
2023-01-22 12:59:11 +00:00
|
|
|
goto out_err;
|
2019-08-08 12:25:52 +00:00
|
|
|
}
|
2019-02-15 22:39:11 +00:00
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
/* Initialize cdev and device structures */
|
2023-01-22 15:11:31 +00:00
|
|
|
rc = device_init_cdev(hdev, hdev->hclass, hdev->id, &hl_ops, name,
|
2019-07-30 06:10:02 +00:00
|
|
|
&hdev->cdev, &hdev->dev);
|
|
|
|
|
|
|
|
kfree(name);
|
2019-02-15 22:39:11 +00:00
|
|
|
|
|
|
|
if (rc)
|
2023-01-22 12:59:11 +00:00
|
|
|
goto out_err;
|
2019-02-15 22:39:11 +00:00
|
|
|
|
2022-08-04 14:32:30 +00:00
|
|
|
name = kasprintf(GFP_KERNEL, "hl_controlD%d", hdev->cdev_idx);
|
2019-07-30 06:10:50 +00:00
|
|
|
if (!name) {
|
|
|
|
rc = -ENOMEM;
|
2019-08-08 12:25:52 +00:00
|
|
|
goto free_dev;
|
2019-07-30 06:10:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
/* Initialize cdev and device structures for control device */
|
2023-01-22 15:11:31 +00:00
|
|
|
rc = device_init_cdev(hdev, hdev->hclass, hdev->id_control, &hl_ctrl_ops,
|
2019-07-30 06:10:50 +00:00
|
|
|
name, &hdev->cdev_ctrl, &hdev->dev_ctrl);
|
|
|
|
|
|
|
|
kfree(name);
|
|
|
|
|
|
|
|
if (rc)
|
2019-08-08 12:25:52 +00:00
|
|
|
goto free_dev;
|
2019-07-30 06:10:50 +00:00
|
|
|
|
2023-01-22 12:59:11 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
free_dev:
|
|
|
|
put_device(hdev->dev);
|
|
|
|
out_err:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hl_device_init - main initialization function for habanalabs device
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
*
|
|
|
|
* Allocate an id for the device, do early initialization and then call the
|
|
|
|
* ASIC specific initialization functions. Finally, create the cdev and the
|
|
|
|
* Linux device to expose it to the user
|
|
|
|
*/
|
2023-01-22 15:11:31 +00:00
|
|
|
int hl_device_init(struct hl_device *hdev)
|
2023-01-22 12:59:11 +00:00
|
|
|
{
|
|
|
|
int i, rc, cq_cnt, user_interrupt_cnt, cq_ready_cnt;
|
|
|
|
bool add_cdev_sysfs_on_err = false;
|
|
|
|
|
2023-01-22 15:11:31 +00:00
|
|
|
rc = create_cdev(hdev);
|
2023-01-22 12:59:11 +00:00
|
|
|
if (rc)
|
|
|
|
goto out_disabled;
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
/* Initialize ASIC function pointers and perform early init */
|
|
|
|
rc = device_early_init(hdev);
|
|
|
|
if (rc)
|
2023-01-22 12:59:11 +00:00
|
|
|
goto free_dev;
|
2019-02-15 22:39:11 +00:00
|
|
|
|
2022-06-27 12:05:28 +00:00
|
|
|
user_interrupt_cnt = hdev->asic_prop.user_dec_intr_count +
|
|
|
|
hdev->asic_prop.user_interrupt_count;
|
2021-01-12 16:37:19 +00:00
|
|
|
|
|
|
|
if (user_interrupt_cnt) {
|
2022-06-27 12:05:28 +00:00
|
|
|
hdev->user_interrupt = kcalloc(user_interrupt_cnt, sizeof(*hdev->user_interrupt),
|
|
|
|
GFP_KERNEL);
|
2021-01-12 16:37:19 +00:00
|
|
|
if (!hdev->user_interrupt) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto early_fini;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:13 +00:00
|
|
|
/*
|
|
|
|
* Start calling ASIC initialization. First S/W then H/W and finally
|
|
|
|
* late init
|
|
|
|
*/
|
|
|
|
rc = hdev->asic_funcs->sw_init(hdev);
|
|
|
|
if (rc)
|
2022-06-27 12:05:28 +00:00
|
|
|
goto free_usr_intr_mem;
|
2019-02-15 22:39:13 +00:00
|
|
|
|
2021-06-14 19:18:41 +00:00
|
|
|
|
|
|
|
/* initialize completion structure for multi CS wait */
|
|
|
|
hl_multi_cs_completion_init(hdev);
|
|
|
|
|
2019-02-15 22:39:17 +00:00
|
|
|
/*
|
|
|
|
* Initialize the H/W queues. Must be done before hw_init, because
|
|
|
|
* there the addresses of the kernel queue are being written to the
|
|
|
|
* registers of the device
|
|
|
|
*/
|
|
|
|
rc = hl_hw_queues_create(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "failed to initialize kernel queues\n");
|
|
|
|
goto sw_fini;
|
|
|
|
}
|
|
|
|
|
2020-03-01 17:59:39 +00:00
|
|
|
cq_cnt = hdev->asic_prop.completion_queues_count;
|
|
|
|
|
2019-02-15 22:39:17 +00:00
|
|
|
/*
|
|
|
|
* Initialize the completion queues. Must be done before hw_init,
|
|
|
|
* because there the addresses of the completion queues are being
|
|
|
|
* passed as arguments to request_irq
|
|
|
|
*/
|
2020-06-24 11:49:43 +00:00
|
|
|
if (cq_cnt) {
|
|
|
|
hdev->completion_queue = kcalloc(cq_cnt,
|
|
|
|
sizeof(*hdev->completion_queue),
|
|
|
|
GFP_KERNEL);
|
2019-02-15 22:39:17 +00:00
|
|
|
|
2020-06-24 11:49:43 +00:00
|
|
|
if (!hdev->completion_queue) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"failed to allocate completion queues\n");
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto hw_queues_destroy;
|
|
|
|
}
|
2019-02-15 22:39:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 17:59:39 +00:00
|
|
|
for (i = 0, cq_ready_cnt = 0 ; i < cq_cnt ; i++, cq_ready_cnt++) {
|
|
|
|
rc = hl_cq_init(hdev, &hdev->completion_queue[i],
|
|
|
|
hdev->asic_funcs->get_queue_id_for_cq(hdev, i));
|
2019-02-15 22:39:17 +00:00
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"failed to initialize completion queue\n");
|
|
|
|
goto cq_fini;
|
|
|
|
}
|
2020-07-05 10:35:51 +00:00
|
|
|
hdev->completion_queue[i].cq_idx = i;
|
2019-02-15 22:39:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 12:05:28 +00:00
|
|
|
hdev->shadow_cs_queue = kcalloc(hdev->asic_prop.max_pending_cs,
|
2022-09-19 21:08:40 +00:00
|
|
|
sizeof(struct hl_cs *), GFP_KERNEL);
|
2022-06-27 12:05:28 +00:00
|
|
|
if (!hdev->shadow_cs_queue) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto cq_fini;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:18 +00:00
|
|
|
/*
|
|
|
|
* Initialize the event queue. Must be done before hw_init,
|
|
|
|
* because there the address of the event queue is being
|
|
|
|
* passed as argument to request_irq
|
|
|
|
*/
|
|
|
|
rc = hl_eq_init(hdev, &hdev->event_queue);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "failed to initialize event queue\n");
|
2022-06-27 12:05:28 +00:00
|
|
|
goto free_shadow_cs_queue;
|
2019-02-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 11:43:04 +00:00
|
|
|
/* MMU S/W must be initialized before kernel context is created */
|
|
|
|
rc = hl_mmu_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n");
|
|
|
|
goto eq_fini;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:14 +00:00
|
|
|
/* Allocate the kernel context */
|
|
|
|
hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
|
|
|
|
if (!hdev->kernel_ctx) {
|
|
|
|
rc = -ENOMEM;
|
2019-05-29 11:43:04 +00:00
|
|
|
goto mmu_fini;
|
2019-02-15 22:39:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 21:08:21 +00:00
|
|
|
hdev->is_compute_ctx_active = false;
|
2019-02-15 22:39:14 +00:00
|
|
|
|
2021-06-06 07:28:51 +00:00
|
|
|
hdev->asic_funcs->state_dump_init(hdev);
|
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
hdev->device_release_watchdog_timeout_sec = HL_DEVICE_RELEASE_WATCHDOG_TIMEOUT_SEC;
|
|
|
|
|
2022-05-12 13:16:25 +00:00
|
|
|
hdev->memory_scrub_val = MEM_SCRUB_DEFAULT_VAL;
|
2020-11-12 09:03:32 +00:00
|
|
|
hl_debugfs_add_device(hdev);
|
|
|
|
|
|
|
|
/* debugfs nodes are created in hl_ctx_init so it must be called after
|
|
|
|
* hl_debugfs_add_device.
|
|
|
|
*/
|
2019-02-15 22:39:14 +00:00
|
|
|
rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "failed to initialize kernel context\n");
|
2019-08-01 13:57:36 +00:00
|
|
|
kfree(hdev->kernel_ctx);
|
2020-11-12 09:03:32 +00:00
|
|
|
goto remove_device_from_debugfs;
|
2019-02-15 22:39:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:15 +00:00
|
|
|
rc = hl_cb_pool_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "failed to initialize CB pool\n");
|
|
|
|
goto release_ctx;
|
|
|
|
}
|
|
|
|
|
2022-06-26 15:20:03 +00:00
|
|
|
rc = hl_dec_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "Failed to initialize the decoder module\n");
|
|
|
|
goto cb_pool_fini;
|
|
|
|
}
|
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
/*
|
2021-06-10 06:01:57 +00:00
|
|
|
* From this point, override rc (=0) in case of an error to allow
|
|
|
|
* debugging (by adding char devices and create sysfs nodes as part of
|
|
|
|
* the error flow).
|
2019-08-08 12:25:52 +00:00
|
|
|
*/
|
|
|
|
add_cdev_sysfs_on_err = true;
|
|
|
|
|
2020-07-15 18:59:32 +00:00
|
|
|
/* Device is now enabled as part of the initialization requires
|
|
|
|
* communication with the device firmware to get information that
|
|
|
|
* is required for the initialization itself
|
|
|
|
*/
|
|
|
|
hdev->disabled = false;
|
|
|
|
|
2019-02-15 22:39:16 +00:00
|
|
|
rc = hdev->asic_funcs->hw_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "failed to initialize the H/W\n");
|
|
|
|
rc = 0;
|
|
|
|
goto out_disabled;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:17 +00:00
|
|
|
/* Check that the communication with the device is working */
|
|
|
|
rc = hdev->asic_funcs->test_queues(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "Failed to detect if device is alive\n");
|
|
|
|
rc = 0;
|
|
|
|
goto out_disabled;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:19 +00:00
|
|
|
rc = device_late_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "Failed late initialization\n");
|
|
|
|
rc = 0;
|
|
|
|
goto out_disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
|
|
|
|
hdev->asic_name,
|
2021-06-03 10:18:20 +00:00
|
|
|
hdev->asic_prop.dram_size / SZ_1G);
|
2019-02-15 22:39:19 +00:00
|
|
|
|
2019-02-15 22:39:22 +00:00
|
|
|
rc = hl_vm_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "Failed to initialize memory module\n");
|
|
|
|
rc = 0;
|
|
|
|
goto out_disabled;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:19 +00:00
|
|
|
/*
|
2019-08-08 12:25:52 +00:00
|
|
|
* Expose devices and sysfs nodes to user.
|
|
|
|
* From here there is no need to add char devices and create sysfs nodes
|
|
|
|
* in case of an error.
|
|
|
|
*/
|
|
|
|
add_cdev_sysfs_on_err = false;
|
|
|
|
rc = device_cdev_sysfs_add(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Failed to add char devices and sysfs nodes\n");
|
|
|
|
rc = 0;
|
|
|
|
goto out_disabled;
|
|
|
|
}
|
|
|
|
|
2020-08-08 20:34:47 +00:00
|
|
|
/* Need to call this again because the max power might change,
|
|
|
|
* depending on card type for certain ASICs
|
|
|
|
*/
|
2022-06-30 11:37:03 +00:00
|
|
|
if (hdev->asic_prop.set_max_power_on_device_init &&
|
|
|
|
!hdev->asic_prop.fw_security_enabled)
|
2022-01-18 17:27:14 +00:00
|
|
|
hl_fw_set_max_power(hdev);
|
2020-08-08 20:34:47 +00:00
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
/*
|
|
|
|
* hl_hwmon_init() must be called after device_late_init(), because only
|
2019-02-15 22:39:19 +00:00
|
|
|
* there we get the information from the device about which
|
2019-08-08 12:25:52 +00:00
|
|
|
* hwmon-related sensors the device supports.
|
|
|
|
* Furthermore, it must be done after adding the device to the system.
|
2019-02-15 22:39:19 +00:00
|
|
|
*/
|
|
|
|
rc = hl_hwmon_init(hdev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(hdev->dev, "Failed to initialize hwmon\n");
|
|
|
|
rc = 0;
|
|
|
|
goto out_disabled;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
dev_notice(hdev->dev,
|
2022-12-29 10:44:09 +00:00
|
|
|
"Successfully added device %s to habanalabs driver\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2019-02-15 22:39:11 +00:00
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
hdev->init_done = true;
|
|
|
|
|
2021-02-01 19:23:43 +00:00
|
|
|
/* After initialization is done, we are ready to receive events from
|
|
|
|
* the F/W. We can't do it before because we will ignore events and if
|
|
|
|
* those events are fatal, we won't know about it and the device will
|
|
|
|
* be operational although it shouldn't be
|
|
|
|
*/
|
|
|
|
hdev->asic_funcs->enable_events_from_fw(hdev);
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
return 0;
|
|
|
|
|
2022-06-26 15:20:03 +00:00
|
|
|
cb_pool_fini:
|
|
|
|
hl_cb_pool_fini(hdev);
|
2019-02-15 22:39:15 +00:00
|
|
|
release_ctx:
|
|
|
|
if (hl_ctx_put(hdev->kernel_ctx) != 1)
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"kernel ctx is still alive on initialization failure\n");
|
2020-11-12 09:03:32 +00:00
|
|
|
remove_device_from_debugfs:
|
|
|
|
hl_debugfs_remove_device(hdev);
|
2019-05-29 11:43:04 +00:00
|
|
|
mmu_fini:
|
|
|
|
hl_mmu_fini(hdev);
|
2019-02-15 22:39:18 +00:00
|
|
|
eq_fini:
|
|
|
|
hl_eq_fini(hdev, &hdev->event_queue);
|
2022-06-27 12:05:28 +00:00
|
|
|
free_shadow_cs_queue:
|
|
|
|
kfree(hdev->shadow_cs_queue);
|
2019-02-15 22:39:17 +00:00
|
|
|
cq_fini:
|
|
|
|
for (i = 0 ; i < cq_ready_cnt ; i++)
|
|
|
|
hl_cq_fini(hdev, &hdev->completion_queue[i]);
|
|
|
|
kfree(hdev->completion_queue);
|
|
|
|
hw_queues_destroy:
|
|
|
|
hl_hw_queues_destroy(hdev);
|
2019-02-15 22:39:14 +00:00
|
|
|
sw_fini:
|
|
|
|
hdev->asic_funcs->sw_fini(hdev);
|
2022-06-27 12:05:28 +00:00
|
|
|
free_usr_intr_mem:
|
2021-01-12 16:37:19 +00:00
|
|
|
kfree(hdev->user_interrupt);
|
2019-02-15 22:39:13 +00:00
|
|
|
early_fini:
|
|
|
|
device_early_fini(hdev);
|
2019-08-08 12:25:52 +00:00
|
|
|
free_dev:
|
2023-01-22 12:59:11 +00:00
|
|
|
put_device(hdev->dev_ctrl);
|
2020-11-29 07:34:12 +00:00
|
|
|
put_device(hdev->dev);
|
2019-02-15 22:39:11 +00:00
|
|
|
out_disabled:
|
|
|
|
hdev->disabled = true;
|
2019-08-08 12:25:52 +00:00
|
|
|
if (add_cdev_sysfs_on_err)
|
|
|
|
device_cdev_sysfs_add(hdev);
|
2019-02-15 22:39:11 +00:00
|
|
|
if (hdev->pdev)
|
|
|
|
dev_err(&hdev->pdev->dev,
|
2022-12-29 10:44:09 +00:00
|
|
|
"Failed to initialize hl%d. Device %s is NOT usable !\n",
|
|
|
|
hdev->cdev_idx, dev_name(&(hdev)->pdev->dev));
|
2019-02-15 22:39:11 +00:00
|
|
|
else
|
2022-12-29 10:44:09 +00:00
|
|
|
pr_err("Failed to initialize hl%d. Device %s is NOT usable !\n",
|
|
|
|
hdev->cdev_idx, dev_name(&(hdev)->pdev->dev));
|
2019-02-15 22:39:11 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hl_device_fini - main tear-down function for habanalabs device
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
*
|
|
|
|
* Destroy the device, call ASIC fini functions and release the id
|
|
|
|
*/
|
|
|
|
void hl_device_fini(struct hl_device *hdev)
|
|
|
|
{
|
2021-11-23 13:15:22 +00:00
|
|
|
bool device_in_reset;
|
2019-02-15 22:39:20 +00:00
|
|
|
ktime_t timeout;
|
2021-06-09 07:17:49 +00:00
|
|
|
u64 reset_sec;
|
2020-10-08 07:27:42 +00:00
|
|
|
int i, rc;
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
dev_info(hdev->dev, "Removing device\n");
|
|
|
|
|
2020-10-08 07:27:42 +00:00
|
|
|
hdev->device_fini_pending = 1;
|
|
|
|
flush_delayed_work(&hdev->device_reset_work.reset_work);
|
|
|
|
|
2021-06-09 07:17:49 +00:00
|
|
|
if (hdev->pldm)
|
|
|
|
reset_sec = HL_PLDM_HARD_RESET_MAX_TIMEOUT;
|
|
|
|
else
|
|
|
|
reset_sec = HL_HARD_RESET_MAX_TIMEOUT;
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
/*
|
|
|
|
* This function is competing with the reset function, so try to
|
|
|
|
* take the reset atomic and if we are already in middle of reset,
|
|
|
|
* wait until reset function is finished. Reset function is designed
|
2020-05-09 09:18:01 +00:00
|
|
|
* to always finish. However, in Gaudi, because of all the network
|
|
|
|
* ports, the hard reset could take between 10-30 seconds
|
2019-02-15 22:39:20 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-09 07:17:49 +00:00
|
|
|
timeout = ktime_add_us(ktime_get(), reset_sec * 1000 * 1000);
|
2021-11-23 13:15:22 +00:00
|
|
|
|
|
|
|
spin_lock(&hdev->reset_info.lock);
|
|
|
|
device_in_reset = !!hdev->reset_info.in_reset;
|
|
|
|
if (!device_in_reset)
|
|
|
|
hdev->reset_info.in_reset = 1;
|
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
|
|
|
|
|
|
|
while (device_in_reset) {
|
2019-02-15 22:39:20 +00:00
|
|
|
usleep_range(50, 200);
|
2021-11-23 13:15:22 +00:00
|
|
|
|
|
|
|
spin_lock(&hdev->reset_info.lock);
|
|
|
|
device_in_reset = !!hdev->reset_info.in_reset;
|
|
|
|
if (!device_in_reset)
|
|
|
|
hdev->reset_info.in_reset = 1;
|
|
|
|
spin_unlock(&hdev->reset_info.lock);
|
|
|
|
|
2019-02-15 22:39:20 +00:00
|
|
|
if (ktime_compare(ktime_get(), timeout) > 0) {
|
2020-12-03 15:32:19 +00:00
|
|
|
dev_crit(hdev->dev,
|
2022-12-29 10:44:09 +00:00
|
|
|
"%s Failed to remove device because reset function did not finish\n",
|
|
|
|
dev_name(&(hdev)->pdev->dev));
|
2019-02-15 22:39:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-02 12:46:02 +00:00
|
|
|
}
|
2019-02-15 22:39:20 +00:00
|
|
|
|
2022-09-30 12:08:13 +00:00
|
|
|
cancel_delayed_work_sync(&hdev->device_release_watchdog_work.reset_work);
|
|
|
|
|
2021-01-18 19:39:46 +00:00
|
|
|
/* Disable PCI access from device F/W so it won't send us additional
|
|
|
|
* interrupts. We disable MSI/MSI-X at the halt_engines function and we
|
|
|
|
* can't have the F/W sending us interrupts after that. We need to
|
|
|
|
* disable the access here because if the device is marked disable, the
|
|
|
|
* message won't be send. Also, in case of heartbeat, the device CPU is
|
|
|
|
* marked as disable so this message won't be sent
|
|
|
|
*/
|
2022-07-06 07:20:54 +00:00
|
|
|
hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0);
|
2021-01-18 19:39:46 +00:00
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
/* Mark device as disabled */
|
|
|
|
hdev->disabled = true;
|
|
|
|
|
2021-07-01 07:09:28 +00:00
|
|
|
take_release_locks(hdev);
|
2019-07-30 08:56:09 +00:00
|
|
|
|
2021-11-23 13:15:22 +00:00
|
|
|
hdev->reset_info.hard_reset_pending = true;
|
2019-04-06 12:41:35 +00:00
|
|
|
|
2019-02-15 22:39:19 +00:00
|
|
|
hl_hwmon_fini(hdev);
|
|
|
|
|
2022-02-17 14:15:26 +00:00
|
|
|
cleanup_resources(hdev, true, false, false);
|
2019-02-15 22:39:21 +00:00
|
|
|
|
2019-07-22 14:37:22 +00:00
|
|
|
/* Kill processes here after CS rollback. This is because the process
|
|
|
|
* can't really exit until all its CSs are done, which is what we
|
|
|
|
* do in cs rollback
|
|
|
|
*/
|
2020-10-08 07:27:42 +00:00
|
|
|
dev_info(hdev->dev,
|
|
|
|
"Waiting for all processes to exit (timeout of %u seconds)",
|
2022-11-10 15:24:02 +00:00
|
|
|
HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI);
|
2020-10-08 07:27:42 +00:00
|
|
|
|
2022-11-10 15:24:02 +00:00
|
|
|
hdev->process_kill_trial_cnt = 0;
|
|
|
|
rc = device_kill_open_processes(hdev, HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI, false);
|
2021-02-01 17:44:34 +00:00
|
|
|
if (rc) {
|
2020-05-18 19:27:46 +00:00
|
|
|
dev_crit(hdev->dev, "Failed to kill all open processes\n");
|
2021-12-08 07:52:03 +00:00
|
|
|
device_disable_open_processes(hdev, false);
|
|
|
|
}
|
|
|
|
|
2022-11-10 15:24:02 +00:00
|
|
|
hdev->process_kill_trial_cnt = 0;
|
2021-12-08 07:52:03 +00:00
|
|
|
rc = device_kill_open_processes(hdev, 0, true);
|
|
|
|
if (rc) {
|
|
|
|
dev_crit(hdev->dev, "Failed to kill all control device open processes\n");
|
|
|
|
device_disable_open_processes(hdev, true);
|
2021-02-01 17:44:34 +00:00
|
|
|
}
|
2019-07-22 14:37:22 +00:00
|
|
|
|
2019-02-15 22:39:15 +00:00
|
|
|
hl_cb_pool_fini(hdev);
|
|
|
|
|
2020-09-23 12:06:52 +00:00
|
|
|
/* Reset the H/W. It will be in idle state after this returns */
|
2021-08-09 19:43:37 +00:00
|
|
|
hdev->asic_funcs->hw_fini(hdev, true, false);
|
2020-09-23 12:06:52 +00:00
|
|
|
|
2021-10-21 08:24:41 +00:00
|
|
|
hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE;
|
2021-07-09 14:34:45 +00:00
|
|
|
|
2019-02-15 22:39:14 +00:00
|
|
|
/* Release kernel context */
|
|
|
|
if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
|
|
|
|
dev_err(hdev->dev, "kernel ctx is still alive\n");
|
|
|
|
|
2020-11-12 09:03:32 +00:00
|
|
|
hl_debugfs_remove_device(hdev);
|
|
|
|
|
2022-06-26 15:20:03 +00:00
|
|
|
hl_dec_fini(hdev);
|
|
|
|
|
2019-02-15 22:39:22 +00:00
|
|
|
hl_vm_fini(hdev);
|
|
|
|
|
2019-05-29 11:43:04 +00:00
|
|
|
hl_mmu_fini(hdev);
|
|
|
|
|
2022-12-25 10:43:04 +00:00
|
|
|
vfree(hdev->captured_err_info.page_fault_info.user_mappings);
|
2022-09-18 18:37:31 +00:00
|
|
|
|
2019-02-15 22:39:18 +00:00
|
|
|
hl_eq_fini(hdev, &hdev->event_queue);
|
|
|
|
|
2022-06-27 12:05:28 +00:00
|
|
|
kfree(hdev->shadow_cs_queue);
|
|
|
|
|
2019-02-15 22:39:17 +00:00
|
|
|
for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
|
|
|
|
hl_cq_fini(hdev, &hdev->completion_queue[i]);
|
|
|
|
kfree(hdev->completion_queue);
|
2021-01-12 12:43:09 +00:00
|
|
|
kfree(hdev->user_interrupt);
|
2019-02-15 22:39:17 +00:00
|
|
|
|
|
|
|
hl_hw_queues_destroy(hdev);
|
|
|
|
|
2019-02-15 22:39:13 +00:00
|
|
|
/* Call ASIC S/W finalize function */
|
|
|
|
hdev->asic_funcs->sw_fini(hdev);
|
|
|
|
|
2019-02-15 22:39:11 +00:00
|
|
|
device_early_fini(hdev);
|
|
|
|
|
2019-08-08 12:25:52 +00:00
|
|
|
/* Hide devices and sysfs nodes from user */
|
|
|
|
device_cdev_sysfs_del(hdev);
|
2019-02-15 22:39:11 +00:00
|
|
|
|
|
|
|
pr_info("removed device successfully\n");
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:39:13 +00:00
|
|
|
/*
|
|
|
|
* MMIO register access helper functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hl_rreg - Read an MMIO register
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
* @reg: MMIO register offset (in bytes)
|
|
|
|
*
|
|
|
|
* Returns the value of the MMIO register we are asked to read
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
|
|
|
|
{
|
2022-11-30 12:41:49 +00:00
|
|
|
u32 val = readl(hdev->rmmio + reg);
|
|
|
|
|
|
|
|
if (unlikely(trace_habanalabs_rreg32_enabled()))
|
|
|
|
trace_habanalabs_rreg32(hdev->dev, reg, val);
|
|
|
|
|
|
|
|
return val;
|
2019-02-15 22:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hl_wreg - Write to an MMIO register
|
|
|
|
*
|
|
|
|
* @hdev: pointer to habanalabs device structure
|
|
|
|
* @reg: MMIO register offset (in bytes)
|
|
|
|
* @val: 32-bit value
|
|
|
|
*
|
|
|
|
* Writes the 32-bit value into the MMIO register
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
|
|
|
|
{
|
2022-11-30 12:41:49 +00:00
|
|
|
if (unlikely(trace_habanalabs_wreg32_enabled()))
|
|
|
|
trace_habanalabs_wreg32(hdev->dev, reg, val);
|
|
|
|
|
2019-02-15 22:39:13 +00:00
|
|
|
writel(val, hdev->rmmio + reg);
|
|
|
|
}
|
2022-09-19 15:51:59 +00:00
|
|
|
|
|
|
|
void hl_capture_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines,
|
|
|
|
u8 flags)
|
|
|
|
{
|
2022-12-25 10:43:04 +00:00
|
|
|
struct razwi_info *razwi_info = &hdev->captured_err_info.razwi_info;
|
|
|
|
|
2022-09-19 15:51:59 +00:00
|
|
|
if (num_of_engines > HL_RAZWI_MAX_NUM_OF_ENGINES_PER_RTR) {
|
|
|
|
dev_err(hdev->dev,
|
|
|
|
"Number of possible razwi initiators (%u) exceeded limit (%u)\n",
|
|
|
|
num_of_engines, HL_RAZWI_MAX_NUM_OF_ENGINES_PER_RTR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In case it's the first razwi since the device was opened, capture its parameters */
|
2022-12-25 10:43:04 +00:00
|
|
|
if (atomic_cmpxchg(&hdev->captured_err_info.razwi_info.razwi_detected, 0, 1))
|
2022-09-19 15:51:59 +00:00
|
|
|
return;
|
|
|
|
|
2022-12-25 10:43:04 +00:00
|
|
|
razwi_info->razwi.timestamp = ktime_to_ns(ktime_get());
|
|
|
|
razwi_info->razwi.addr = addr;
|
|
|
|
razwi_info->razwi.num_of_possible_engines = num_of_engines;
|
|
|
|
memcpy(&razwi_info->razwi.engine_id[0], &engine_id[0],
|
2022-09-19 15:51:59 +00:00
|
|
|
num_of_engines * sizeof(u16));
|
2022-12-25 10:43:04 +00:00
|
|
|
razwi_info->razwi.flags = flags;
|
2022-12-23 13:02:05 +00:00
|
|
|
|
|
|
|
razwi_info->razwi_info_available = true;
|
2022-09-19 15:51:59 +00:00
|
|
|
}
|
2022-10-30 11:08:37 +00:00
|
|
|
|
|
|
|
void hl_handle_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines,
|
|
|
|
u8 flags, u64 *event_mask)
|
|
|
|
{
|
|
|
|
hl_capture_razwi(hdev, addr, engine_id, num_of_engines, flags);
|
2022-10-30 12:46:19 +00:00
|
|
|
|
|
|
|
if (event_mask)
|
|
|
|
*event_mask |= HL_NOTIFIER_EVENT_RAZWI;
|
2022-10-30 11:08:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 07:21:28 +00:00
|
|
|
static void hl_capture_user_mappings(struct hl_device *hdev, bool is_pmmu)
|
2022-09-18 18:37:31 +00:00
|
|
|
{
|
2022-12-25 10:43:04 +00:00
|
|
|
struct page_fault_info *pgf_info = &hdev->captured_err_info.page_fault_info;
|
2022-09-29 07:21:28 +00:00
|
|
|
struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
|
2022-09-18 18:37:31 +00:00
|
|
|
struct hl_vm_hash_node *hnode;
|
|
|
|
struct hl_userptr *userptr;
|
2022-09-29 07:21:28 +00:00
|
|
|
enum vm_type *vm_type;
|
2022-09-18 18:37:31 +00:00
|
|
|
struct hl_ctx *ctx;
|
|
|
|
u32 map_idx = 0;
|
|
|
|
int i;
|
|
|
|
|
2022-09-29 07:21:28 +00:00
|
|
|
/* Reset previous session count*/
|
|
|
|
pgf_info->num_of_user_mappings = 0;
|
|
|
|
|
2022-09-18 18:37:31 +00:00
|
|
|
ctx = hl_get_compute_ctx(hdev);
|
|
|
|
if (!ctx) {
|
|
|
|
dev_err(hdev->dev, "Can't get user context for user mappings\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&ctx->mem_hash_lock);
|
2022-10-19 17:24:55 +00:00
|
|
|
hash_for_each(ctx->mem_hash, i, hnode, node) {
|
|
|
|
vm_type = hnode->ptr;
|
|
|
|
if (((*vm_type == VM_TYPE_USERPTR) && is_pmmu) ||
|
|
|
|
((*vm_type == VM_TYPE_PHYS_PACK) && !is_pmmu))
|
|
|
|
pgf_info->num_of_user_mappings++;
|
|
|
|
|
|
|
|
}
|
2022-09-18 18:37:31 +00:00
|
|
|
|
|
|
|
if (!pgf_info->num_of_user_mappings)
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
/* In case we already allocated in previous session, need to release it before
|
|
|
|
* allocating new buffer.
|
|
|
|
*/
|
|
|
|
vfree(pgf_info->user_mappings);
|
|
|
|
pgf_info->user_mappings =
|
2022-09-29 07:21:28 +00:00
|
|
|
vzalloc(pgf_info->num_of_user_mappings * sizeof(struct hl_user_mapping));
|
2022-09-18 18:37:31 +00:00
|
|
|
if (!pgf_info->user_mappings) {
|
|
|
|
pgf_info->num_of_user_mappings = 0;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_for_each(ctx->mem_hash, i, hnode, node) {
|
2022-09-29 07:21:28 +00:00
|
|
|
vm_type = hnode->ptr;
|
|
|
|
if ((*vm_type == VM_TYPE_USERPTR) && (is_pmmu)) {
|
|
|
|
userptr = hnode->ptr;
|
|
|
|
pgf_info->user_mappings[map_idx].dev_va = hnode->vaddr;
|
|
|
|
pgf_info->user_mappings[map_idx].size = userptr->size;
|
|
|
|
map_idx++;
|
|
|
|
} else if ((*vm_type == VM_TYPE_PHYS_PACK) && (!is_pmmu)) {
|
|
|
|
phys_pg_pack = hnode->ptr;
|
|
|
|
pgf_info->user_mappings[map_idx].dev_va = hnode->vaddr;
|
|
|
|
pgf_info->user_mappings[map_idx].size = phys_pg_pack->total_size;
|
|
|
|
map_idx++;
|
|
|
|
}
|
2022-09-18 18:37:31 +00:00
|
|
|
}
|
|
|
|
finish:
|
|
|
|
mutex_unlock(&ctx->mem_hash_lock);
|
|
|
|
hl_ctx_put(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void hl_capture_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu)
|
|
|
|
{
|
2022-12-25 10:43:04 +00:00
|
|
|
struct page_fault_info *pgf_info = &hdev->captured_err_info.page_fault_info;
|
|
|
|
|
2022-09-18 18:37:31 +00:00
|
|
|
/* Capture only the first page fault */
|
2022-12-25 10:43:04 +00:00
|
|
|
if (atomic_cmpxchg(&pgf_info->page_fault_detected, 0, 1))
|
2022-09-18 18:37:31 +00:00
|
|
|
return;
|
|
|
|
|
2022-12-25 10:43:04 +00:00
|
|
|
pgf_info->page_fault.timestamp = ktime_to_ns(ktime_get());
|
|
|
|
pgf_info->page_fault.addr = addr;
|
|
|
|
pgf_info->page_fault.engine_id = eng_id;
|
2022-09-29 07:21:28 +00:00
|
|
|
hl_capture_user_mappings(hdev, is_pmmu);
|
2022-12-23 13:02:05 +00:00
|
|
|
|
|
|
|
pgf_info->page_fault_info_available = true;
|
2022-09-18 18:37:31 +00:00
|
|
|
}
|
2022-10-31 09:44:45 +00:00
|
|
|
|
|
|
|
void hl_handle_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu,
|
|
|
|
u64 *event_mask)
|
|
|
|
{
|
|
|
|
hl_capture_page_fault(hdev, addr, eng_id, is_pmmu);
|
|
|
|
|
|
|
|
if (event_mask)
|
|
|
|
*event_mask |= HL_NOTIFIER_EVENT_PAGE_FAULT;
|
|
|
|
}
|
2023-01-10 15:35:31 +00:00
|
|
|
|
|
|
|
void hl_capture_hw_err(struct hl_device *hdev, u16 event_id)
|
|
|
|
{
|
|
|
|
struct hw_err_info *info = &hdev->captured_err_info.hw_err;
|
|
|
|
|
|
|
|
/* Capture only the first HW err */
|
|
|
|
if (atomic_cmpxchg(&info->event_detected, 0, 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
info->event.timestamp = ktime_to_ns(ktime_get());
|
|
|
|
info->event.event_id = event_id;
|
|
|
|
|
|
|
|
info->event_info_available = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hl_handle_critical_hw_err(struct hl_device *hdev, u16 event_id, u64 *event_mask)
|
|
|
|
{
|
|
|
|
hl_capture_hw_err(hdev, event_id);
|
|
|
|
|
|
|
|
if (event_mask)
|
|
|
|
*event_mask |= HL_NOTIFIER_EVENT_CRITICL_HW_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hl_capture_fw_err(struct hl_device *hdev, struct hl_info_fw_err_info *fw_info)
|
|
|
|
{
|
|
|
|
struct fw_err_info *info = &hdev->captured_err_info.fw_err;
|
|
|
|
|
|
|
|
/* Capture only the first FW error */
|
|
|
|
if (atomic_cmpxchg(&info->event_detected, 0, 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
info->event.timestamp = ktime_to_ns(ktime_get());
|
|
|
|
info->event.err_type = fw_info->err_type;
|
|
|
|
if (fw_info->err_type == HL_INFO_FW_REPORTED_ERR)
|
|
|
|
info->event.event_id = fw_info->event_id;
|
|
|
|
|
|
|
|
info->event_info_available = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hl_handle_fw_err(struct hl_device *hdev, struct hl_info_fw_err_info *info)
|
|
|
|
{
|
|
|
|
hl_capture_fw_err(hdev, info);
|
|
|
|
|
|
|
|
if (info->event_mask)
|
|
|
|
*info->event_mask |= HL_NOTIFIER_EVENT_CRITICL_FW_ERR;
|
|
|
|
}
|