habanalabs: expose ASIC specific PCI info to common code
authorOhad Sharabi <osharabi@habana.ai>
Wed, 21 Apr 2021 10:03:21 +0000 (13:03 +0300)
committerOded Gabbay <ogabbay@kernel.org>
Fri, 18 Jun 2021 12:23:39 +0000 (15:23 +0300)
LKD has interfaces in which it receives device address.
For instance the debugfs_read/write variants receives device address for
CFG/SRAM/DRAM for read/write and need to translate to the mapped PCI BAR
address.

In addition, the dynamic FW load protocol dictates that the address to
which the LKD will copy the image for the next FW component will be
received as a device address and can be placed either in SRAM or DRAM.

We need to distinguish those regions as the access methods to those
regions are different (in DRAM we possibly need to set the BAR base).

Looking forward this code will be used to remove duplicated code in the
debugfs_read/write that search the memory region for the input device
address.

Signed-off-by: Ohad Sharabi <osharabi@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
drivers/misc/habanalabs/common/habanalabs.h
drivers/misc/habanalabs/common/pci/pci.c
drivers/misc/habanalabs/gaudi/gaudi.c
drivers/misc/habanalabs/goya/goya.c

index b1517ed..4dd7108 100644 (file)
@@ -818,6 +818,30 @@ enum div_select_defs {
        DIV_SEL_DIVIDED_PLL = 3,
 };
 
+enum pci_region {
+       PCI_REGION_CFG,
+       PCI_REGION_SRAM,
+       PCI_REGION_DRAM,
+       PCI_REGION_SP_SRAM,
+       PCI_REGION_NUMBER,
+};
+
+/**
+ * struct pci_mem_region - describe memory region in a PCI bar
+ * @region_base: region base address
+ * @region_size: region size
+ * @offset_in_bar: region offset into the bar
+ * @bar_id: bar ID of the region
+ * @used: if used 1, otherwise 0
+ */
+struct pci_mem_region {
+       u64 region_base;
+       u64 region_size;
+       u32 offset_in_bar;
+       u8 bar_id;
+       u8 used;
+};
+
 /**
  * struct static_fw_load_mgr - static FW load manager
  * @preboot_version_max_off: max offset to preboot version
@@ -2014,6 +2038,7 @@ struct hl_mmu_funcs {
  * @mmu_priv: device-specific MMU data.
  * @mmu_func: device-related MMU functions.
  * @fw_loader: FW loader manager.
+ * @pci_mem_region: array of memory regions in the PCI
  * @dram_used_mem: current DRAM memory consumption.
  * @timeout_jiffies: device CS timeout value.
  * @max_power: the max power of the device, as configured by the sysadmin. This
@@ -2141,6 +2166,8 @@ struct hl_device {
 
        struct fw_load_mgr              fw_loader;
 
+       struct pci_mem_region           pci_mem_region[PCI_REGION_NUMBER];
+
        atomic64_t                      dram_used_mem;
        u64                             timeout_jiffies;
        u64                             max_power;
@@ -2474,6 +2501,7 @@ int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
                struct hl_inbound_pci_region *pci_region);
 int hl_pci_set_outbound_region(struct hl_device *hdev,
                struct hl_outbound_pci_region *pci_region);
+enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr);
 int hl_pci_init(struct hl_device *hdev);
 void hl_pci_fini(struct hl_device *hdev);
 
index 5d07ca5..9ef6c46 100644 (file)
@@ -360,6 +360,32 @@ int hl_pci_set_outbound_region(struct hl_device *hdev,
 }
 
 /**
+ * hl_get_pci_memory_region() - get PCI region for given address
+ * @hdev: Pointer to hl_device structure.
+ * @addr: device address
+ *
+ * @return region index on success, otherwise PCI_REGION_NUMBER (invalid
+ *         region index)
+ */
+enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr)
+{
+       int i;
+
+       for  (i = 0 ; i < PCI_REGION_NUMBER ; i++) {
+               struct pci_mem_region *region = &hdev->pci_mem_region[i];
+
+               if (!region->used)
+                       continue;
+
+               if ((addr >= region->region_base) &&
+                       (addr < region->region_base + region->region_size))
+                       return i;
+       }
+
+       return PCI_REGION_NUMBER;
+}
+
+/**
  * hl_pci_init() - PCI initialization code.
  * @hdev: Pointer to hl_device structure.
  *
index 245ced9..8b1bd11 100644 (file)
@@ -1590,6 +1590,43 @@ free_internal_qmans_pq_mem:
        return rc;
 }
 
+static void gaudi_set_pci_memory_regions(struct hl_device *hdev)
+{
+       struct pci_mem_region *region;
+
+       /* CFG */
+       region = &hdev->pci_mem_region[PCI_REGION_CFG];
+       region->region_base = CFG_BASE;
+       region->region_size = CFG_SIZE;
+       region->offset_in_bar = CFG_BASE - SPI_FLASH_BASE_ADDR;
+       region->bar_id = CFG_BAR_ID;
+       region->used = 1;
+
+       /* SRAM */
+       region = &hdev->pci_mem_region[PCI_REGION_SRAM];
+       region->region_base = SRAM_BASE_ADDR;
+       region->region_size = SRAM_SIZE;
+       region->offset_in_bar = 0;
+       region->bar_id = SRAM_BAR_ID;
+       region->used = 1;
+
+       /* DRAM */
+       region = &hdev->pci_mem_region[PCI_REGION_DRAM];
+       region->region_base = DRAM_PHYS_BASE;
+       region->region_size = hdev->asic_prop.dram_size;
+       region->offset_in_bar = 0;
+       region->bar_id = HBM_BAR_ID;
+       region->used = 1;
+
+       /* SP SRAM */
+       region = &hdev->pci_mem_region[PCI_REGION_SP_SRAM];
+       region->region_base = PSOC_SCRATCHPAD_ADDR;
+       region->region_size = PSOC_SCRATCHPAD_SIZE;
+       region->offset_in_bar = PSOC_SCRATCHPAD_ADDR - SPI_FLASH_BASE_ADDR;
+       region->bar_id = CFG_BAR_ID;
+       region->used = 1;
+}
+
 static int gaudi_sw_init(struct hl_device *hdev)
 {
        struct gaudi_device *gaudi;
@@ -1664,6 +1701,8 @@ static int gaudi_sw_init(struct hl_device *hdev)
        hdev->supports_coresight = true;
        hdev->supports_staged_submission = true;
 
+       gaudi_set_pci_memory_regions(hdev);
+
        return 0;
 
 free_cpu_accessible_dma_pool:
index dc56593..c3a2412 100644 (file)
@@ -849,6 +849,35 @@ void goya_late_fini(struct hl_device *hdev)
        hdev->hl_chip_info->info = NULL;
 }
 
+static void goya_set_pci_memory_regions(struct hl_device *hdev)
+{
+       struct pci_mem_region *region;
+
+       /* CFG */
+       region = &hdev->pci_mem_region[PCI_REGION_CFG];
+       region->region_base = CFG_BASE;
+       region->region_size = CFG_SIZE;
+       region->offset_in_bar = CFG_BASE - SRAM_BASE_ADDR;
+       region->bar_id = SRAM_CFG_BAR_ID;
+       region->used = 1;
+
+       /* SRAM */
+       region = &hdev->pci_mem_region[PCI_REGION_SRAM];
+       region->region_base = SRAM_BASE_ADDR;
+       region->region_size = SRAM_SIZE;
+       region->offset_in_bar = 0;
+       region->bar_id = SRAM_CFG_BAR_ID;
+       region->used = 1;
+
+       /* DRAM */
+       region = &hdev->pci_mem_region[PCI_REGION_DRAM];
+       region->region_base = DRAM_PHYS_BASE;
+       region->region_size = hdev->asic_prop.dram_size;
+       region->offset_in_bar = 0;
+       region->bar_id = DDR_BAR_ID;
+       region->used = 1;
+}
+
 /*
  * goya_sw_init - Goya software initialization code
  *
@@ -919,6 +948,8 @@ static int goya_sw_init(struct hl_device *hdev)
        hdev->supports_coresight = true;
        hdev->supports_soft_reset = true;
 
+       goya_set_pci_memory_regions(hdev);
+
        return 0;
 
 free_cpu_accessible_dma_pool: