* [PATCH] habanalabs: move asic property to correct structure
@ 2020-11-09 17:08 Oded Gabbay
2020-11-09 17:08 ` [PATCH] habanalabs: use host va range for internal pools Oded Gabbay
0 siblings, 1 reply; 2+ messages in thread
From: Oded Gabbay @ 2020-11-09 17:08 UTC (permalink / raw)
To: linux-kernel; +Cc: SW_Drivers
Whether an ASIC has MMU towards its DRAM is an ASIC property, so
move it to the asic fixed properties structure.
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
---
drivers/misc/habanalabs/common/debugfs.c | 2 +-
drivers/misc/habanalabs/common/habanalabs.h | 4 ++--
drivers/misc/habanalabs/common/memory.c | 6 +++---
drivers/misc/habanalabs/common/mmu_v1.c | 4 ++--
drivers/misc/habanalabs/gaudi/gaudi.c | 3 +--
drivers/misc/habanalabs/goya/goya.c | 2 +-
6 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/drivers/misc/habanalabs/common/debugfs.c b/drivers/misc/habanalabs/common/debugfs.c
index 104b9686e57b..3d9bd86312e2 100644
--- a/drivers/misc/habanalabs/common/debugfs.c
+++ b/drivers/misc/habanalabs/common/debugfs.c
@@ -598,7 +598,7 @@ static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
if (!hdev->mmu_enable)
goto out;
- if (hdev->dram_supports_virtual_memory &&
+ if (prop->dram_supports_virtual_memory &&
(addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
return true;
diff --git a/drivers/misc/habanalabs/common/habanalabs.h b/drivers/misc/habanalabs/common/habanalabs.h
index 822f90087e04..a1d82de60ef6 100644
--- a/drivers/misc/habanalabs/common/habanalabs.h
+++ b/drivers/misc/habanalabs/common/habanalabs.h
@@ -411,6 +411,7 @@ struct hl_mmu_properties {
* false otherwise
* @fw_security_status_valid: security status bits are valid and can be fetched
* from BOOT_DEV_STS0
+ * @dram_supports_virtual_memory: is there an MMU towards the DRAM
*/
struct asic_fixed_properties {
struct hw_queue_properties *hw_queues_props;
@@ -467,6 +468,7 @@ struct asic_fixed_properties {
u8 completion_queues_count;
u8 fw_security_disabled;
u8 fw_security_status_valid;
+ u8 dram_supports_virtual_memory;
};
/**
@@ -1772,7 +1774,6 @@ struct hl_mmu_funcs {
* @heartbeat: is heartbeat sanity check towards CPU-CP enabled.
* @reset_on_lockup: true if a reset should be done in case of stuck CS, false
* otherwise.
- * @dram_supports_virtual_memory: is MMU enabled towards DRAM.
* @dram_default_page_mapping: is DRAM default page mapping enabled.
* @memory_scrub: true to perform device memory scrub in various locations,
* such as context-switch, context close, page free, etc.
@@ -1886,7 +1887,6 @@ struct hl_device {
u8 hard_reset_pending;
u8 heartbeat;
u8 reset_on_lockup;
- u8 dram_supports_virtual_memory;
u8 dram_default_page_mapping;
u8 memory_scrub;
u8 pmmu_huge_range;
diff --git a/drivers/misc/habanalabs/common/memory.c b/drivers/misc/habanalabs/common/memory.c
index 01e93e83d3af..f27ca80d3c3c 100644
--- a/drivers/misc/habanalabs/common/memory.c
+++ b/drivers/misc/habanalabs/common/memory.c
@@ -1351,7 +1351,7 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
* the user's input, the driver can't ensure the validity
* of this accounting.
*/
- if (!hdev->dram_supports_virtual_memory) {
+ if (!hdev->asic_prop.dram_supports_virtual_memory) {
atomic64_add(args->in.alloc.mem_size,
&ctx->dram_phys_mem);
atomic64_add(args->in.alloc.mem_size,
@@ -1380,7 +1380,7 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
* the user's input, the driver can't ensure the validity
* of this accounting.
*/
- if (!hdev->dram_supports_virtual_memory) {
+ if (!hdev->asic_prop.dram_supports_virtual_memory) {
atomic64_sub(args->in.alloc.mem_size,
&ctx->dram_phys_mem);
atomic64_sub(args->in.alloc.mem_size,
@@ -1915,7 +1915,7 @@ void hl_vm_ctx_fini(struct hl_ctx *ctx)
* because the user notifies us on allocations. If the user is no more,
* all DRAM is available
*/
- if (!ctx->hdev->dram_supports_virtual_memory)
+ if (!ctx->hdev->asic_prop.dram_supports_virtual_memory)
atomic64_set(&ctx->hdev->dram_used_mem, 0);
}
diff --git a/drivers/misc/habanalabs/common/mmu_v1.c b/drivers/misc/habanalabs/common/mmu_v1.c
index ac3784523baa..5f62cb158eef 100644
--- a/drivers/misc/habanalabs/common/mmu_v1.c
+++ b/drivers/misc/habanalabs/common/mmu_v1.c
@@ -262,7 +262,7 @@ static int dram_default_mapping_init(struct hl_ctx *ctx)
hop2_pte_addr, hop3_pte_addr, pte_val;
int rc, i, j, hop3_allocated = 0;
- if ((!hdev->dram_supports_virtual_memory) ||
+ if ((!prop->dram_supports_virtual_memory) ||
(!hdev->dram_default_page_mapping) ||
(ctx->asid == HL_KERNEL_ASID_ID))
return 0;
@@ -362,7 +362,7 @@ static void dram_default_mapping_fini(struct hl_ctx *ctx)
hop2_pte_addr, hop3_pte_addr;
int i, j;
- if ((!hdev->dram_supports_virtual_memory) ||
+ if ((!prop->dram_supports_virtual_memory) ||
(!hdev->dram_default_page_mapping) ||
(ctx->asid == HL_KERNEL_ASID_ID))
return;
diff --git a/drivers/misc/habanalabs/gaudi/gaudi.c b/drivers/misc/habanalabs/gaudi/gaudi.c
index 1135b2cc5860..8d6cffd28381 100644
--- a/drivers/misc/habanalabs/gaudi/gaudi.c
+++ b/drivers/misc/habanalabs/gaudi/gaudi.c
@@ -462,6 +462,7 @@ static int gaudi_get_fixed_properties(struct hl_device *hdev)
prop->mmu_hop_table_size = HOP_TABLE_SIZE;
prop->mmu_hop0_tables_total_size = HOP0_TABLES_TOTAL_SIZE;
prop->dram_page_size = PAGE_SIZE_2MB;
+ prop->dram_supports_virtual_memory = false;
prop->pmmu.hop0_shift = HOP0_SHIFT;
prop->pmmu.hop1_shift = HOP1_SHIFT;
@@ -3573,8 +3574,6 @@ static int gaudi_mmu_init(struct hl_device *hdev)
if (gaudi->hw_cap_initialized & HW_CAP_MMU)
return 0;
- hdev->dram_supports_virtual_memory = false;
-
for (i = 0 ; i < prop->max_asid ; i++) {
hop0_addr = prop->mmu_pgt_addr +
(i * prop->mmu_hop_table_size);
diff --git a/drivers/misc/habanalabs/goya/goya.c b/drivers/misc/habanalabs/goya/goya.c
index 002fc53fb39d..bf21f05f7849 100644
--- a/drivers/misc/habanalabs/goya/goya.c
+++ b/drivers/misc/habanalabs/goya/goya.c
@@ -410,6 +410,7 @@ int goya_get_fixed_properties(struct hl_device *hdev)
prop->mmu_hop_table_size = HOP_TABLE_SIZE;
prop->mmu_hop0_tables_total_size = HOP0_TABLES_TOTAL_SIZE;
prop->dram_page_size = PAGE_SIZE_2MB;
+ prop->dram_supports_virtual_memory = true;
prop->dmmu.hop0_shift = HOP0_SHIFT;
prop->dmmu.hop1_shift = HOP1_SHIFT;
@@ -2481,7 +2482,6 @@ int goya_mmu_init(struct hl_device *hdev)
if (goya->hw_cap_initialized & HW_CAP_MMU)
return 0;
- hdev->dram_supports_virtual_memory = true;
hdev->dram_default_page_mapping = true;
for (i = 0 ; i < prop->max_asid ; i++) {
--
2.17.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH] habanalabs: use host va range for internal pools
2020-11-09 17:08 [PATCH] habanalabs: move asic property to correct structure Oded Gabbay
@ 2020-11-09 17:08 ` Oded Gabbay
0 siblings, 0 replies; 2+ messages in thread
From: Oded Gabbay @ 2020-11-09 17:08 UTC (permalink / raw)
To: linux-kernel; +Cc: SW_Drivers, Ofir Bitton
From: Ofir Bitton <obitton@habana.ai>
Instead of using a dedicated va range for each internal pool,
we introduce a new way for reserving a va block from an existing
va range. This is a more generic way of reserving va blocks for
future use.
Signed-off-by: Ofir Bitton <obitton@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
---
drivers/misc/habanalabs/common/habanalabs.h | 4 ++
drivers/misc/habanalabs/common/memory.c | 79 +++++++++++++++++++++
drivers/misc/habanalabs/gaudi/gaudi.c | 18 +++--
drivers/misc/habanalabs/gaudi/gaudiP.h | 5 +-
4 files changed, 98 insertions(+), 8 deletions(-)
diff --git a/drivers/misc/habanalabs/common/habanalabs.h b/drivers/misc/habanalabs/common/habanalabs.h
index 87060cd2c525..822f90087e04 100644
--- a/drivers/misc/habanalabs/common/habanalabs.h
+++ b/drivers/misc/habanalabs/common/habanalabs.h
@@ -2109,6 +2109,10 @@ void hl_vm_ctx_fini(struct hl_ctx *ctx);
int hl_vm_init(struct hl_device *hdev);
void hl_vm_fini(struct hl_device *hdev);
+u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
+ enum hl_va_range_type type, u32 size);
+int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
+ u64 start_addr, u64 size);
int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
struct hl_userptr *userptr);
void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
diff --git a/drivers/misc/habanalabs/common/memory.c b/drivers/misc/habanalabs/common/memory.c
index 02233899336f..01e93e83d3af 100644
--- a/drivers/misc/habanalabs/common/memory.c
+++ b/drivers/misc/habanalabs/common/memory.c
@@ -620,6 +620,85 @@ static u64 get_va_block(struct hl_device *hdev, struct hl_va_range *va_range,
return res_valid_start;
}
+/*
+ * hl_reserve_va_block() - reserve a virtual block of a given size.
+ * @hdev: pointer to the habanalabs device structure.
+ * @ctx: current context
+ * @type: virtual addresses range type.
+ * @size: requested block size.
+ *
+ * This function does the following:
+ * - Iterate on the virtual block list to find a suitable virtual block for the
+ * given size.
+ * - Reserve the requested block and update the list.
+ * - Return the start address of the virtual block.
+ */
+u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
+ enum hl_va_range_type type, u32 size)
+{
+ return get_va_block(hdev, ctx->va_range[type], size, 0,
+ ctx->va_range[type]->page_size);
+}
+
+/**
+ * hl_get_va_range_type() - get va_range type for the given address and size.
+ * @address: The start address of the area we want to validate.
+ * @size: The size in bytes of the area we want to validate.
+ * @type: returned va_range type
+ *
+ * Return: true if the area is inside a valid range, false otherwise.
+ */
+static int hl_get_va_range_type(struct hl_ctx *ctx, u64 address, u64 size,
+ enum hl_va_range_type *type)
+{
+ int i;
+
+ for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX; i++) {
+ if (hl_mem_area_inside_range(address, size,
+ ctx->va_range[i]->start_addr,
+ ctx->va_range[i]->end_addr)) {
+ *type = i;
+ return 0;
+ }
+ }
+
+ return -EINVAL;
+}
+
+/*
+ * hl_unreserve_va_block - wrapper for add_va_block for unreserving a va block
+ *
+ * @hdev: pointer to the habanalabs device structure
+ * @ctx: current context
+ * @start: start virtual address
+ * @end: end virtual address
+ *
+ * This function does the following:
+ * - Takes the list lock and calls add_va_block_locked
+ */
+int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
+ u64 start_addr, u64 size)
+{
+ enum hl_va_range_type type;
+ int rc;
+
+ rc = hl_get_va_range_type(ctx, start_addr, size, &type);
+ if (rc) {
+ dev_err(hdev->dev,
+ "cannot find va_range for va %#llx size %llu",
+ start_addr, size);
+ return rc;
+ }
+
+ rc = add_va_block(hdev, ctx->va_range[type], start_addr,
+ start_addr + size - 1);
+ if (rc)
+ dev_warn(hdev->dev,
+ "add va block failed for vaddr: 0x%llx\n", start_addr);
+
+ return rc;
+}
+
/*
* get_sg_info - get number of pages and the DMA address from SG list
*
diff --git a/drivers/misc/habanalabs/gaudi/gaudi.c b/drivers/misc/habanalabs/gaudi/gaudi.c
index 086d8363f3ae..1135b2cc5860 100644
--- a/drivers/misc/habanalabs/gaudi/gaudi.c
+++ b/drivers/misc/habanalabs/gaudi/gaudi.c
@@ -7768,7 +7768,11 @@ static int gaudi_internal_cb_pool_init(struct hl_device *hdev,
goto destroy_internal_cb_pool;
}
- hdev->internal_cb_va_base = VA_HOST_SPACE_INTERNAL_CB_START;
+ hdev->internal_cb_va_base = hl_reserve_va_block(hdev, ctx,
+ HL_VA_RANGE_TYPE_HOST, HOST_SPACE_INTERNAL_CB_SZ);
+
+ if (!hdev->internal_cb_va_base)
+ goto destroy_internal_cb_pool;
mutex_lock(&ctx->mmu_lock);
@@ -7776,7 +7780,7 @@ static int gaudi_internal_cb_pool_init(struct hl_device *hdev,
* is aligned to HOST_SPACE_INTERNAL_CB_SZ
*/
for (off = 0 ; off < HOST_SPACE_INTERNAL_CB_SZ ; off += PAGE_SIZE_4KB) {
- va = VA_HOST_SPACE_INTERNAL_CB_START + off;
+ va = hdev->internal_cb_va_base + off;
pa = hdev->internal_cb_pool_dma_addr + off;
flush_pte = (off + PAGE_SIZE_4KB) >= HOST_SPACE_INTERNAL_CB_SZ;
rc = hl_mmu_map(ctx, va, pa, PAGE_SIZE_4KB, flush_pte);
@@ -7796,13 +7800,16 @@ static int gaudi_internal_cb_pool_init(struct hl_device *hdev,
unmap:
for (; off >= 0 ; off -= PAGE_SIZE_4KB) {
- va = VA_HOST_SPACE_INTERNAL_CB_START + off;
+ va = hdev->internal_cb_va_base + off;
flush_pte = (off - (s32) PAGE_SIZE_4KB) < 0;
if (hl_mmu_unmap(ctx, va, PAGE_SIZE_4KB, flush_pte))
dev_warn_ratelimited(hdev->dev,
"failed to unmap va 0x%llx\n", va);
}
+ hl_unreserve_va_block(hdev, ctx, hdev->internal_cb_va_base,
+ HOST_SPACE_INTERNAL_CB_SZ);
+
hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
mutex_unlock(&ctx->mmu_lock);
@@ -7832,7 +7839,7 @@ static void gaudi_internal_cb_pool_fini(struct hl_device *hdev,
mutex_lock(&ctx->mmu_lock);
for (off = 0 ; off < HOST_SPACE_INTERNAL_CB_SZ ; off += PAGE_SIZE_4KB) {
- va = VA_HOST_SPACE_INTERNAL_CB_START + off;
+ va = hdev->internal_cb_va_base + off;
if (off + PAGE_SIZE_4KB >= HOST_SPACE_INTERNAL_CB_SZ)
flush_pte = true;
@@ -7842,6 +7849,9 @@ static void gaudi_internal_cb_pool_fini(struct hl_device *hdev,
"failed to unmap va 0x%llx\n", va);
}
+ hl_unreserve_va_block(hdev, ctx, hdev->internal_cb_va_base,
+ HOST_SPACE_INTERNAL_CB_SZ);
+
hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
mutex_unlock(&ctx->mmu_lock);
diff --git a/drivers/misc/habanalabs/gaudi/gaudiP.h b/drivers/misc/habanalabs/gaudi/gaudiP.h
index e882cf7f8cf7..1a5e681c720d 100644
--- a/drivers/misc/habanalabs/gaudi/gaudiP.h
+++ b/drivers/misc/habanalabs/gaudi/gaudiP.h
@@ -153,12 +153,9 @@
/* Virtual address space */
#define VA_HOST_SPACE_START 0x1000000000000ull /* 256TB */
-#define VA_HOST_SPACE_END 0x3FF7FFFE00000ull /* 1PB - 1TB */
+#define VA_HOST_SPACE_END 0x3FF8000000000ull /* 1PB - 512GB */
#define VA_HOST_SPACE_SIZE (VA_HOST_SPACE_END - \
VA_HOST_SPACE_START) /* 767TB */
-
-#define VA_HOST_SPACE_INTERNAL_CB_START 0x3FF7FFFE00000ull /* 1PB - 1TB - 2MB */
-#define VA_HOST_SPACE_INTERNAL_CB_END 0x3FF8000000000ull /* 1PB - 1TB */
#define HOST_SPACE_INTERNAL_CB_SZ SZ_2M
#define HW_CAP_PLL BIT(0)
--
2.17.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-11-09 17:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-09 17:08 [PATCH] habanalabs: move asic property to correct structure Oded Gabbay
2020-11-09 17:08 ` [PATCH] habanalabs: use host va range for internal pools Oded Gabbay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).