habanalabs: refactor dma asic-specific functions
PATCH 1/2 habanalabs: refactor dma asic-specific functions
From: Ohad Sharabi <[email protected]>
This is a pre-requisite patch for adding tracepoints to the DMA memory operations (allocation/free) in the driver.
The main purpose is to be able to cross data with the map operations and determine whether memory violation occurred, for example free DMA allocation before unmapping it from device memory.
To achieve this the DMA alloc/free code flows were refactored so that a single DMA tracepoint will catch many flows.
Signed-off-by Ohad Sharabi <[email protected]>
Reviewed-by Oded Gabbay <[email protected]>
Signed-off-by Oded Gabbay <[email protected]>
--- .../misc/habanalabs/common/command_buffer.c | 12 +-- drivers/misc/habanalabs/common/device.c | 75 ++++++++++++++++ drivers/misc/habanalabs/common/firmware_if.c | 39 ++++---- drivers/misc/habanalabs/common/habanalabs.h | 9 ++ drivers/misc/habanalabs/common/hw_queue.c | 34 +++---- drivers/misc/habanalabs/common/irq.c | 16 ++-- drivers/misc/habanalabs/gaudi/gaudi.c | 89 +++++++------------ drivers/misc/habanalabs/goya/goya.c | 40 +++------ 8 files changed, 162 insertions(+), 152 deletions(-)
drivers/misc/habanalabs/common/command_buffer.c +4 -8
@@ -143,8 +143,7 @@ static void cb_fini(struct hl_device *hdev, struct hl_cb *cb)gen_pool_free (hdev->internal_cb_pool, (uintptr_t )cb->kernel_address, cb->size); else - hdev->asic_funcs->asic_dma_free_coherent(hdev, cb->size, - cb->kernel_address, cb->bus_address); + hl_asic_dma_free_coherent(hdev, cb->size, cb->kernel_address, cb->bus_address); kfree(cb); }
@@ -195,14 +194,11 @@ static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size, cb->is_internal = true; cb->bus_address = hdev->internal_cb_va_base + cb_offset; }else if (ctx_id ==HL_KERNEL_ASID_ID ) { - p = hdev->asic_funcs->asic_dma_alloc_coherent (hdev, cb_size, - &cb->bus_address,GFP_ATOMIC ); + p =hl_asic_dma_alloc_coherent (hdev, cb_size, &cb->bus_address,GFP_ATOMIC );if (!p) - p = hdev->asic_funcs->asic_dma_alloc_coherent (hdev, - cb_size, &cb->bus_address,GFP_KERNEL ); + p =hl_asic_dma_alloc_coherent (hdev, cb_size, &cb->bus_address,GFP_KERNEL ); }else { - p = hdev->asic_funcs->asic_dma_alloc_coherent (hdev, cb_size, - &cb->bus_address, + p =hl_asic_dma_alloc_coherent (hdev, cb_size, &cb->bus_address,GFP_USER | __GFP_ZERO); }
drivers/misc/habanalabs/common/device.c +75 -0
87 lines not on this page · raw message
drivers/misc/habanalabs/common/firmware_if.c +15 -24
@@ -218,8 +218,7 @@ int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,u32 tmp, expected_ack_val, pi;int rc; - pkt = hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, len, - &pkt_dma_addr); + pkt =hl_cpu_accessible_dma_pool_alloc (hdev, len, &pkt_dma_addr);if (!pkt) {dev_err (hdev->dev,"Failed to allocate DMA memory for packet to CPU\n" );
@@ -303,7 +302,7 @@ int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg, out:mutex_unlock (&hdev->send_cpu_message_lock); - hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, len, pkt); +hl_cpu_accessible_dma_pool_free (hdev, len, pkt);return rc; }
@@ -644,10 +643,8 @@ int hl_fw_cpucp_info_get(struct hl_device *hdev,u64 result;int rc; - cpucp_info_cpu_addr = - hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, -sizeof (struct cpucp_info ), - &cpucp_info_dma_addr); + cpucp_info_cpu_addr =hl_cpu_accessible_dma_pool_alloc (hdev,sizeof (struct cpucp_info ), + &cpucp_info_dma_addr);if (!cpucp_info_cpu_addr) {dev_err (hdev->dev,"Failed to allocate DMA memory for CPU-CP info packet\n" );
@@ -708,8 +705,7 @@ int hl_fw_cpucp_info_get(struct hl_device *hdev, prop->fw_app_cpu_boot_dev_sts1 =RREG32 (sts_boot_dev_sts1_reg); out: - hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, -sizeof (struct cpucp_info ), cpucp_info_cpu_addr); +hl_cpu_accessible_dma_pool_free (hdev,sizeof (struct cpucp_info ), cpucp_info_cpu_addr);return rc; }
@@ -792,9 +788,8 @@ int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)u64 result;int rc; - eeprom_info_cpu_addr = - hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, - max_size, &eeprom_info_dma_addr); + eeprom_info_cpu_addr =hl_cpu_accessible_dma_pool_alloc (hdev, max_size, + &eeprom_info_dma_addr);if (!eeprom_info_cpu_addr) {dev_err (hdev->dev,"Failed to allocate DMA memory for CPU-CP EEPROM packet\n" );
@@ -822,8 +817,7 @@ int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)memcpy (data, eeprom_info_cpu_addr,min ((size_t )result, max_size)); out: - hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, max_size, - eeprom_info_cpu_addr); +hl_cpu_accessible_dma_pool_free (hdev, max_size, eeprom_info_cpu_addr);return rc; }
@@ -840,8 +834,7 @@ int hl_fw_get_monitor_dump(struct hl_device *hdev, void *data)int i, rc; data_size =sizeof (struct cpucp_monitor_dump ); - mon_dump_cpu_addr = hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, data_size, - &mon_dump_dma_addr); + mon_dump_cpu_addr =hl_cpu_accessible_dma_pool_alloc (hdev, data_size, &mon_dump_dma_addr);if (!mon_dump_cpu_addr) {dev_err (hdev->dev,"Failed to allocate DMA memory for CPU-CP monitor-dump packet\n" );
@@ -871,7 +864,7 @@ int hl_fw_get_monitor_dump(struct hl_device *hdev, void *data) } out: - hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, data_size, mon_dump_cpu_addr); +hl_cpu_accessible_dma_pool_free (hdev, data_size, mon_dump_cpu_addr);return rc; }
@@ -1064,10 +1057,9 @@ int hl_fw_dram_replaced_row_get(struct hl_device *hdev,u64 result;int rc; - cpucp_repl_rows_info_cpu_addr = - hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, -sizeof (struct cpucp_hbm_row_info ), - &cpucp_repl_rows_info_dma_addr); + cpucp_repl_rows_info_cpu_addr =hl_cpu_accessible_dma_pool_alloc (hdev, +sizeof (struct cpucp_hbm_row_info ), + &cpucp_repl_rows_info_dma_addr);if (!cpucp_repl_rows_info_cpu_addr) {dev_err (hdev->dev,"Failed to allocate DMA memory for CPU-CP replaced rows info packet\n" );
@@ -1092,9 +1084,8 @@ int hl_fw_dram_replaced_row_get(struct hl_device *hdev,memcpy (info, cpucp_repl_rows_info_cpu_addr,sizeof (*info)); out: - hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, -sizeof (struct cpucp_hbm_row_info ), - cpucp_repl_rows_info_cpu_addr); +hl_cpu_accessible_dma_pool_free (hdev,sizeof (struct cpucp_hbm_row_info ), + cpucp_repl_rows_info_cpu_addr);return rc; }
drivers/misc/habanalabs/common/habanalabs.h +9 -0
@@ -3121,6 +3121,15 @@ static inline bool hl_mem_area_crosses_range(u64 address, u32 size, }uint64_t hl_set_dram_bar_default (struct hl_device *hdev,u64 addr); +void *hl_asic_dma_alloc_coherent (struct hl_device *hdev,size_t size,dma_addr_t *dma_handle, +gfp_t flag); +void hl_asic_dma_free_coherent (struct hl_device *hdev,size_t size,void *cpu_addr, +dma_addr_t dma_handle); +void *hl_cpu_accessible_dma_pool_alloc (struct hl_device *hdev,size_t size,dma_addr_t *dma_handle); +void hl_cpu_accessible_dma_pool_free (struct hl_device *hdev,size_t size,void *vaddr); +void *hl_asic_dma_pool_zalloc (struct hl_device *hdev,size_t size,gfp_t mem_flags, +dma_addr_t *dma_handle); +void hl_asic_dma_pool_free (struct hl_device *hdev,void *vaddr,dma_addr_t dma_addr);int hl_dma_map_sgtable (struct hl_device *hdev,struct sg_table *sgt,enum dma_data_direction dir);void hl_dma_unmap_sgtable (struct hl_device *hdev,struct sg_table *sgt,enum dma_data_direction dir);
drivers/misc/habanalabs/common/hw_queue.c +10 -24
@@ -807,13 +807,9 @@ static int ext_and_cpu_queue_init(struct hl_device *hdev, struct hl_hw_queue *q,int rc;if (is_cpu_queue) - p = hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, -HL_QUEUE_SIZE_IN_BYTES , - &q->bus_address); + p =hl_cpu_accessible_dma_pool_alloc (hdev,HL_QUEUE_SIZE_IN_BYTES , &q->bus_address);else - p = hdev->asic_funcs->asic_dma_alloc_coherent (hdev, -HL_QUEUE_SIZE_IN_BYTES , - &q->bus_address, + p =hl_asic_dma_alloc_coherent (hdev,HL_QUEUE_SIZE_IN_BYTES , &q->bus_address,GFP_KERNEL | __GFP_ZERO);if (!p)return -ENOMEM ;
@@ -839,14 +835,10 @@ static int ext_and_cpu_queue_init(struct hl_device *hdev, struct hl_hw_queue *q, free_queue:if (is_cpu_queue) - hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, -HL_QUEUE_SIZE_IN_BYTES , - q->kernel_address); +hl_cpu_accessible_dma_pool_free (hdev,HL_QUEUE_SIZE_IN_BYTES , q->kernel_address);else - hdev->asic_funcs->asic_dma_free_coherent (hdev, -HL_QUEUE_SIZE_IN_BYTES , - q->kernel_address, - q->bus_address); +hl_asic_dma_free_coherent (hdev,HL_QUEUE_SIZE_IN_BYTES , q->kernel_address, + q->bus_address);return rc; }
@@ -885,10 +877,8 @@ static int hw_queue_init(struct hl_device *hdev, struct hl_hw_queue *q) {void *p; - p = hdev->asic_funcs->asic_dma_alloc_coherent (hdev, -HL_QUEUE_SIZE_IN_BYTES , - &q->bus_address, -GFP_KERNEL | __GFP_ZERO); + p =hl_asic_dma_alloc_coherent (hdev,HL_QUEUE_SIZE_IN_BYTES , &q->bus_address, +GFP_KERNEL | __GFP_ZERO);if (!p)return -ENOMEM ;
@@ -1061,14 +1051,10 @@ static void queue_fini(struct hl_device *hdev, struct hl_hw_queue *q)kfree (q->shadow_queue);if (q->queue_type ==QUEUE_TYPE_CPU ) - hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, -HL_QUEUE_SIZE_IN_BYTES , - q->kernel_address); +hl_cpu_accessible_dma_pool_free (hdev,HL_QUEUE_SIZE_IN_BYTES , q->kernel_address);else - hdev->asic_funcs->asic_dma_free_coherent (hdev, -HL_QUEUE_SIZE_IN_BYTES , - q->kernel_address, - q->bus_address); +hl_asic_dma_free_coherent (hdev,HL_QUEUE_SIZE_IN_BYTES , q->kernel_address, + q->bus_address); }int hl_hw_queues_create (struct hl_device *hdev)
drivers/misc/habanalabs/common/irq.c +5 -11
@@ -403,8 +403,8 @@ int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id) {void *p; - p = hdev->asic_funcs->asic_dma_alloc_coherent (hdev,HL_CQ_SIZE_IN_BYTES , - &q->bus_address,GFP_KERNEL | __GFP_ZERO); + p =hl_asic_dma_alloc_coherent (hdev,HL_CQ_SIZE_IN_BYTES , &q->bus_address, +GFP_KERNEL | __GFP_ZERO);if (!p)return -ENOMEM ;
@@ -429,9 +429,7 @@ int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id)*/ void hl_cq_fini (struct hl_device *hdev,struct hl_cq *q) { - hdev->asic_funcs->asic_dma_free_coherent (hdev,HL_CQ_SIZE_IN_BYTES , - q->kernel_address, - q->bus_address); +hl_asic_dma_free_coherent (hdev,HL_CQ_SIZE_IN_BYTES , q->kernel_address, q->bus_address); }void hl_cq_reset (struct hl_device *hdev,struct hl_cq *q)
@@ -464,9 +462,7 @@ int hl_eq_init(struct hl_device *hdev, struct hl_eq *q) {void *p; - p = hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, -HL_EQ_SIZE_IN_BYTES , - &q->bus_address); + p =hl_cpu_accessible_dma_pool_alloc (hdev,HL_EQ_SIZE_IN_BYTES , &q->bus_address);if (!p)return -ENOMEM ;
@@ -490,9 +486,7 @@ void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q) {flush_workqueue (hdev->eq_wq); - hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, -HL_EQ_SIZE_IN_BYTES , - q->kernel_address); +hl_cpu_accessible_dma_pool_free (hdev,HL_EQ_SIZE_IN_BYTES , q->kernel_address); }void hl_eq_reset (struct hl_device *hdev,struct hl_eq *q)
drivers/misc/habanalabs/gaudi/gaudi.c +30 -59
198 lines not on this page · raw message
drivers/misc/habanalabs/goya/goya.c +14 -26
@@ -1012,11 +1012,9 @@ static int goya_sw_init(struct hl_device *hdev) goto free_goya_device; } - hdev->cpu_accessible_dma_mem = - hdev->asic_funcs->asic_dma_alloc_coherent (hdev, -HL_CPU_ACCESSIBLE_MEM_SIZE , - &hdev->cpu_accessible_dma_address, -GFP_KERNEL | __GFP_ZERO); + hdev->cpu_accessible_dma_mem =hl_asic_dma_alloc_coherent (hdev,HL_CPU_ACCESSIBLE_MEM_SIZE , + &hdev->cpu_accessible_dma_address, +GFP_KERNEL | __GFP_ZERO);if (!hdev->cpu_accessible_dma_mem) { rc = -ENOMEM ;
@@ -1066,10 +1064,8 @@ static int goya_sw_init(struct hl_device *hdev) free_cpu_accessible_dma_pool:gen_pool_destroy (hdev->cpu_accessible_dma_pool); free_cpu_dma_mem: - hdev->asic_funcs->asic_dma_free_coherent (hdev, -HL_CPU_ACCESSIBLE_MEM_SIZE , - hdev->cpu_accessible_dma_mem, - hdev->cpu_accessible_dma_address); +hl_asic_dma_free_coherent (hdev,HL_CPU_ACCESSIBLE_MEM_SIZE , hdev->cpu_accessible_dma_mem, + hdev->cpu_accessible_dma_address); free_dma_pool:dma_pool_destroy (hdev->dma_pool); free_goya_device:
@@ -1090,10 +1086,8 @@ static int goya_sw_fini(struct hl_device *hdev)gen_pool_destroy (hdev->cpu_accessible_dma_pool); - hdev->asic_funcs->asic_dma_free_coherent (hdev, -HL_CPU_ACCESSIBLE_MEM_SIZE , - hdev->cpu_accessible_dma_mem, - hdev->cpu_accessible_dma_address); +hl_asic_dma_free_coherent (hdev,HL_CPU_ACCESSIBLE_MEM_SIZE , hdev->cpu_accessible_dma_mem, + hdev->cpu_accessible_dma_address);dma_pool_destroy (hdev->dma_pool);
@@ -3102,8 +3096,7 @@ static int goya_send_job_on_qman0(struct hl_device *hdev, struct hl_cs_job *job)return -EBUSY ; } - fence_ptr = hdev->asic_funcs->asic_dma_pool_zalloc (hdev,4 ,GFP_KERNEL , - &fence_dma_addr); + fence_ptr =hl_asic_dma_pool_zalloc (hdev,4 ,GFP_KERNEL , &fence_dma_addr);if (!fence_ptr) {dev_err (hdev->dev,"Failed to allocate fence memory for QMAN0\n" );
@@ -3143,8 +3136,7 @@ static int goya_send_job_on_qman0(struct hl_device *hdev, struct hl_cs_job *job) } free_fence_ptr: - hdev->asic_funcs->asic_dma_pool_free (hdev, (void *) fence_ptr, - fence_dma_addr); +hl_asic_dma_pool_free (hdev, (void *) fence_ptr, fence_dma_addr);goya_qman0_set_security (hdev, false);
@@ -3180,8 +3172,7 @@ int goya_test_queue(struct hl_device *hdev, u32 hw_queue_id) fence_val =GOYA_QMAN0_FENCE_VAL ; - fence_ptr = hdev->asic_funcs->asic_dma_pool_zalloc (hdev,4 ,GFP_KERNEL , - &fence_dma_addr); + fence_ptr =hl_asic_dma_pool_zalloc (hdev,4 ,GFP_KERNEL , &fence_dma_addr);if (!fence_ptr) { dev_err(hdev->dev, "Failed to allocate memory for H/W queue %d testing\n",
@@ -3191,9 +3182,8 @@ int goya_test_queue(struct hl_device *hdev, u32 hw_queue_id) *fence_ptr =0 ; - fence_pkt = hdev->asic_funcs->asic_dma_pool_zalloc (hdev, -sizeof (struct packet_msg_prot ), -GFP_KERNEL , &pkt_dma_addr); + fence_pkt =hl_asic_dma_pool_zalloc (hdev,sizeof (struct packet_msg_prot ),GFP_KERNEL , + &pkt_dma_addr);if (!fence_pkt) { dev_err(hdev->dev, "Failed to allocate packet for H/W queue %d testing\n",
@@ -3232,11 +3222,9 @@ int goya_test_queue(struct hl_device *hdev, u32 hw_queue_id) } free_pkt: - hdev->asic_funcs->asic_dma_pool_free (hdev, (void *) fence_pkt, - pkt_dma_addr); +hl_asic_dma_pool_free (hdev, (void *) fence_pkt, pkt_dma_addr); free_fence_ptr: - hdev->asic_funcs->asic_dma_pool_free (hdev, (void *) fence_ptr, - fence_dma_addr); +hl_asic_dma_pool_free (hdev, (void *) fence_ptr, fence_dma_addr);return rc; }
Signature · 1 line
2.25.1
PATCH 2/2 habanalabs: page size can only be a power of 2
From: Ohad Sharabi <[email protected]>
We dropped support for page sizes that are not power of 2.
Signed-off-by Ohad Sharabi <[email protected]>
Reviewed-by Oded Gabbay <[email protected]>
Signed-off-by Oded Gabbay <[email protected]>
--- drivers/misc/habanalabs/common/habanalabs.h | 3 --- drivers/misc/habanalabs/common/memory.c | 4 ++-- drivers/misc/habanalabs/gaudi/gaudi.c | 1 - drivers/misc/habanalabs/goya/goya.c | 1 - 4 files changed, 2 insertions(+), 7 deletions(-)
drivers/misc/habanalabs/common/habanalabs.h +0 -3
@@ -1332,8 +1332,6 @@ struct fw_load_mgr {* @get_sob_addr: get SOB base address offset. * @set_pci_memory_regions: setting properties of PCI memory regions * @get_stream_master_qid_arr: get pointer to stream masters QID array -* @is_valid_dram_page_size: return true if page size is supported in device -* memory allocation, otherwise false. * @get_valid_dram_page_orders: get valid device memory allocation page orders * @access_dev_mem: access device memory * @set_dram_bar_base: set the base of the DRAM BAR
@@ -1453,7 +1451,6 @@ struct hl_asic_funcs {u32 (*get_sob_addr)(struct hl_device *hdev,u32 sob_id);void (*set_pci_memory_regions)(struct hl_device *hdev); u32* (*get_stream_master_qid_arr)(void); -bool (*is_valid_dram_page_size)(u32 page_size);int (*mmu_get_real_page_size)(struct hl_device *hdev,struct hl_mmu_properties *mmu_prop,u32 page_size,u32 *real_page_size,bool is_dram_addr);void (*get_valid_dram_page_orders)(struct hl_info_dev_memalloc_page_sizes *info);
drivers/misc/habanalabs/common/memory.c +2 -2
@@ -36,8 +36,8 @@ static int set_alloc_page_size(struct hl_device *hdev, struct hl_mem_in *args, uif (prop->supports_user_set_page_size && args->alloc.page_size) { psize = args->alloc.page_size; -if (!hdev->asic_funcs->is_valid_dram_page_size (psize)) { -dev_err (hdev->dev,"user page size (%#x) is not valid\n" , psize); +if (!is_power_of_2 (psize)) { +dev_err (hdev->dev,"user page size (%#x) is not power of 2\n" , psize);return -EINVAL ; } }else {
drivers/misc/habanalabs/gaudi/gaudi.c +0 -1
@@ -9350,7 +9350,6 @@ static const struct hl_asic_funcs gaudi_funcs = { .get_sob_addr = gaudi_get_sob_addr, .set_pci_memory_regions = gaudi_set_pci_memory_regions, .get_stream_master_qid_arr = gaudi_get_stream_master_qid_arr, - .is_valid_dram_page_size = NULL, .mmu_get_real_page_size = hl_mmu_get_real_page_size, .get_valid_dram_page_orders = gaudi_get_valid_dram_page_orders, .access_dev_mem = hl_access_dev_mem,
drivers/misc/habanalabs/goya/goya.c +0 -1
@@ -5504,7 +5504,6 @@ static const struct hl_asic_funcs goya_funcs = { .get_sob_addr = &goya_get_sob_addr, .set_pci_memory_regions = goya_set_pci_memory_regions, .get_stream_master_qid_arr = goya_get_stream_master_qid_arr, - .is_valid_dram_page_size = NULL, .mmu_get_real_page_size = hl_mmu_get_real_page_size, .get_valid_dram_page_orders = goya_get_valid_dram_page_orders, .access_dev_mem = hl_access_dev_mem,
Signature · 1 line
2.25.1