habanalabs/gaudi2: add uapi to stall/resume engine
Subsystems (4)
Oded Gabbay
maintainerPATCH 1/3 habanalabs/gaudi2: add uapi to stall/resume engine
From: Koby Elbaz <[email protected]>
The user might want to stall/resume engines to perform power testing for various scenarios. Because our current HL_CS_FLAGS_ENGINE_CORE_COMMAND command only handles the engines' cores, we need to add another opcode for handling entire engine and not just its core.
The user supplies an array, where each entry holds the engine's ID and the command to send to the engine. The size of the array is limited by the number of engines in the ASIC (only Gaudi2 is currently supported).
--- .../habanalabs/common/command_submission.c | 55 +++- drivers/accel/habanalabs/common/habanalabs.h | 12 +- drivers/accel/habanalabs/gaudi/gaudi.c | 1 + drivers/accel/habanalabs/gaudi2/gaudi2.c | 255 +++++++++++++++++- .../accel/habanalabs/gaudi2/gaudi2_masks.h | 2 + .../include/gaudi2/asic_reg/gaudi2_regs.h | 5 + include/uapi/drm/habanalabs_accel.h | 36 ++- 7 files changed, 356 insertions(+), 10 deletions(-)
drivers/accel/habanalabs/common/command_submission.c +53 -2
@@ -14,7 +14,7 @@#define HL_CS_FLAGS_TYPE_MASK (HL_CS_FLAGS_SIGNAL | HL_CS_FLAGS_WAIT | \ HL_CS_FLAGS_COLLECTIVE_WAIT | HL_CS_FLAGS_RESERVE_SIGNALS_ONLY | \ HL_CS_FLAGS_UNRESERVE_SIGNALS_ONLY | HL_CS_FLAGS_ENGINE_CORE_COMMAND | \ - HL_CS_FLAGS_FLUSH_PCI_HBW_WRITES) + HL_CS_FLAGS_ENGINES_COMMAND | HL_CS_FLAGS_FLUSH_PCI_HBW_WRITES)#define MAX_TS_ITER_NUM 100
@@ -1319,6 +1319,8 @@ static enum hl_cs_type hl_cs_get_cs_type(u32 cs_type_flags)return CS_UNRESERVE_SIGNALS ; else if (cs_type_flags & HL_CS_FLAGS_ENGINE_CORE_COMMAND) return CS_TYPE_ENGINE_CORE; + else if (cs_type_flags & HL_CS_FLAGS_ENGINES_COMMAND) + return CS_TYPE_ENGINES; else if (cs_type_flags & HL_CS_FLAGS_FLUSH_PCI_HBW_WRITES) return CS_TYPE_FLUSH_PCI_HBW_WRITES; else
@@ -2444,10 +2446,13 @@ static int cs_ioctl_signal_wait(struct hl_fpriv *hpriv, enum hl_cs_type cs_type,static int cs_ioctl_engine_cores (struct hl_fpriv *hpriv,u64 engine_cores,u32 num_engine_cores,u32 core_command) { -int rc;struct hl_device *hdev = hpriv->hdev;void __user *engine_cores_arr;u32 *cores; +int rc; + +if (!hdev->asic_prop.supports_engine_modes) +return -EPERM ;if (!num_engine_cores || num_engine_cores > hdev->asic_prop.num_engine_cores) {dev_err (hdev->dev,"Number of engine cores %d is invalid\n" , num_engine_cores);
@@ -2476,6 +2481,48 @@ static int cs_ioctl_engine_cores(struct hl_fpriv *hpriv, u64 engine_cores,return rc; } +static int cs_ioctl_engines (struct hl_fpriv *hpriv,u64 engines_arr_user_addr, +u32 num_engines,enum hl_engine_command command) +{ +struct hl_device *hdev = hpriv->hdev; +u32 *engines, max_num_of_engines; +void __user *engines_arr; +int rc; + +if (!hdev->asic_prop.supports_engine_modes) +return -EPERM ; + +if (command >=HL_ENGINE_COMMAND_MAX ) { +dev_err (hdev->dev,"Engine command is invalid\n" ); +return -EINVAL ; + } + + max_num_of_engines = hdev->asic_prop.max_num_of_engines; +if (command ==HL_ENGINE_CORE_RUN || command ==HL_ENGINE_CORE_HALT ) + max_num_of_engines = hdev->asic_prop.num_engine_cores; + +if (!num_engines || num_engines > max_num_of_engines) { +dev_err (hdev->dev,"Number of engines %d is invalid\n" , num_engines); +return -EINVAL ; + } + + engines_arr = (void __user *) (uintptr_t ) engines_arr_user_addr; + engines =kmalloc_array (num_engines,sizeof (u32),GFP_KERNEL ); +if (!engines) +return -ENOMEM ; + +if (copy_from_user (engines, engines_arr, num_engines *sizeof (u32))) { +dev_err (hdev->dev,"Failed to copy engine-ids array from user\n" ); +kfree (engines); +return -EFAULT ; + } + + rc = hdev->asic_funcs->set_engines (hdev, engines, num_engines, command); +kfree (engines); + +return rc; +} +static int cs_ioctl_flush_pci_hbw_writes (struct hl_fpriv *hpriv) {struct hl_device *hdev = hpriv->hdev;
@@ -2547,6 +2594,10 @@ int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data) rc =cs_ioctl_engine_cores (hpriv, args->in.engine_cores, args->in.num_engine_cores, args->in.core_command);break ; +case CS_TYPE_ENGINES : + rc =cs_ioctl_engines (hpriv, args->in.engines, + args->in.num_engines, args->in.engine_command); +break ;case CS_TYPE_FLUSH_PCI_HBW_WRITES : rc =cs_ioctl_flush_pci_hbw_writes (hpriv);break ;
drivers/accel/habanalabs/common/habanalabs.h +10 -2
@@ -372,6 +372,7 @@ enum hl_cs_type {CS_RESERVE_SIGNALS ,CS_UNRESERVE_SIGNALS ,CS_TYPE_ENGINE_CORE , +CS_TYPE_ENGINES , CS_TYPE_FLUSH_PCI_HBW_WRITES, };
@@ -644,7 +645,8 @@ struct hl_hints_range {* which the property supports_user_set_page_size is true * (i.e. the DRAM supports multiple page sizes), otherwise * it will shall be equal to dram_page_size. -* @num_engine_cores: number of engine cpu cores +* @num_engine_cores: number of engine cpu cores. +* @max_num_of_engines: maximum number of all engines in the ASIC. * @num_of_special_blocks: special_blocks array size. * @glbl_err_cause_num: global err cause number. * @hbw_flush_reg: register to read to generate HBW flush. value of 0 means HBW flush is
@@ -695,6 +697,7 @@ struct hl_hints_range {* @supports_user_set_page_size: true if user can set the allocation page size. * @dma_mask: the dma mask to be set for this device * @supports_advanced_cpucp_rc: true if new cpucp opcodes are supported. +* @supports_engine_modes: true if changing engines/engine_cores modes is supported. */ struct asic_fixed_properties {struct hw_queue_properties *hw_queues_props;
@@ -773,6 +776,7 @@ struct asic_fixed_properties {u32 xbar_edge_enabled_mask;u32 device_mem_alloc_default_page_size;u32 num_engine_cores; +u32 max_num_of_engines;u32 num_of_special_blocks;u32 glbl_err_cause_num;u32 hbw_flush_reg;
@@ -810,6 +814,7 @@ struct asic_fixed_properties {u8 supports_user_set_page_size;u8 dma_mask;u8 supports_advanced_cpucp_rc; +u8 supports_engine_modes; };/**
@@ -1564,6 +1569,7 @@ struct engines_data {* @access_dev_mem: access device memory * @set_dram_bar_base: set the base of the DRAM BAR * @set_engine_cores: set a config command to engine cores +* @set_engines: set a config command to user engines * @send_device_activity: indication to FW about device availability * @set_dram_properties: set DRAM related properties. * @set_binning_masks: set binning/enable masks for all relevant components.
@@ -1703,6 +1709,8 @@ struct hl_asic_funcs {u64 (*set_dram_bar_base)(struct hl_device *hdev,u64 addr);int (*set_engine_cores)(struct hl_device *hdev,u32 *core_ids,u32 num_cores,u32 core_command); +int (*set_engines)(struct hl_device *hdev,u32 *engine_ids, +u32 num_engines,u32 engine_command);int (*send_device_activity)(struct hl_device *hdev,bool open);int (*set_dram_properties)(struct hl_device *hdev);int (*set_binning_masks)(struct hl_device *hdev);
@@ -1826,7 +1834,7 @@ struct hl_cs_outcome_store {* @hpriv: pointer to the private (Kernel Driver) data of the process (fd). * @hdev: pointer to the device structure. * @refcount: reference counter for the context. Context is released only when -* this hits 0l. It is incremented on CS and CS_WAIT. +* this hits 0. It is incremented on CS and CS_WAIT. * @cs_pending: array of hl fence objects representing pending CS. * @outcome_store: storage data structure used to remember outcomes of completed * command submissions for a long time after CS id wraparound.
drivers/accel/habanalabs/gaudi/gaudi.c +1 -0
@@ -656,6 +656,7 @@ static int gaudi_set_fixed_properties(struct hl_device *hdev) prop->cfg_size =CFG_SIZE ; prop->max_asid =MAX_ASID ; prop->num_of_events =GAUDI_EVENT_SIZE ; + prop->max_num_of_engines =GAUDI_ENGINE_ID_SIZE ; prop->tpc_enabled_mask =TPC_ENABLED_MASK ;set_default_power_values (hdev);
drivers/accel/habanalabs/gaudi2/gaudi2.c +254 -1
302 lines not on this page · raw message
drivers/accel/habanalabs/gaudi2/gaudi2_masks.h +2 -0
@@ -86,6 +86,8 @@#define DCORE0_TPC0_QM_CGM_STS_AGENT_IDLE_MASK 0x100 +#define DCORE0_TPC0_EML_CFG_DBG_CNT_DBG_EXIT_MASK 0x40 +/* CGM_IDLE_MASK is valid for all engines CGM idle check */ #define CGM_IDLE_MASK DCORE0_TPC0_QM_CGM_STS_AGENT_IDLE_MASK
drivers/accel/habanalabs/include/gaudi2/asic_reg/gaudi2_regs.h +5 -0
@@ -164,6 +164,8 @@#define mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR 0x4800040 +#define mmDCORE0_TPC0_EML_CFG_DBG_CNT 0x40000 +#define SM_OBJS_PROT_BITS_OFFS 0x14000#define DCORE_OFFSET (mmDCORE1_TPC0_QM_BASE - mmDCORE0_TPC0_QM_BASE)
@@ -185,7 +187,10 @@#define TPC_CFG_STALL_ON_ERR_OFFSET (mmDCORE0_TPC0_CFG_STALL_ON_ERR - mmDCORE0_TPC0_CFG_BASE)#define TPC_CFG_TPC_INTR_MASK_OFFSET (mmDCORE0_TPC0_CFG_TPC_INTR_MASK - mmDCORE0_TPC0_CFG_BASE)#define TPC_CFG_MSS_CONFIG_OFFSET (mmDCORE0_TPC0_CFG_MSS_CONFIG - mmDCORE0_TPC0_CFG_BASE) +#define TPC_EML_CFG_DBG_CNT_OFFSET (mmDCORE0_TPC0_EML_CFG_DBG_CNT - mmDCORE0_TPC0_EML_CFG_BASE) +#define EDMA_CORE_CFG_STALL_OFFSET (mmDCORE0_EDMA0_CORE_CFG_1 - mmDCORE0_EDMA0_CORE_BASE) +#define MME_CTRL_LO_QM_STALL_OFFSET (mmDCORE0_MME_CTRL_LO_QM_STALL - mmDCORE0_MME_CTRL_LO_BASE)#define MME_ACC_INTR_MASK_OFFSET (mmDCORE0_MME_ACC_INTR_MASK - mmDCORE0_MME_ACC_BASE)#define MME_ACC_WR_AXI_AGG_COUT0_OFFSET (mmDCORE0_MME_ACC_WR_AXI_AGG_COUT0 - mmDCORE0_MME_ACC_BASE)#define MME_ACC_WR_AXI_AGG_COUT1_OFFSET (mmDCORE0_MME_ACC_WR_AXI_AGG_COUT1 - mmDCORE0_MME_ACC_BASE)
include/uapi/drm/habanalabs_accel.h +31 -5
@@ -1535,17 +1535,31 @@ struct hl_cs_chunk {*/ #define HL_CS_FLAGS_FLUSH_PCI_HBW_WRITES 0x8000 +/* +* The engines CS is merged into the existing CS ioctls. +* Use it to control engines modes. +*/ +#define HL_CS_FLAGS_ENGINES_COMMAND 0x10000 +#define HL_CS_STATUS_SUCCESS 0#define HL_MAX_JOBS_PER_CS 512 -/* HL_ENGINE_CORE_ values +/* +* enum hl_engine_command - engine command * -* HL_ENGINE_CORE_HALT: engine core halt -* HL_ENGINE_CORE_RUN: engine core run +* @HL_ENGINE_CORE_HALT: engine core halt +* @HL_ENGINE_CORE_RUN: engine core run +* @HL_ENGINE_STALL: user engine/s stall +* @HL_ENGINE_RESUME: user engine/s resume */ -#define HL_ENGINE_CORE_HALT (1 << 0) -#define HL_ENGINE_CORE_RUN (1 << 1) +enum hl_engine_command { +HL_ENGINE_CORE_HALT =1 , +HL_ENGINE_CORE_RUN =2 , +HL_ENGINE_STALL =3 , +HL_ENGINE_RESUME =4 , +HL_ENGINE_COMMAND_MAX +};struct hl_cs_in {
@@ -1569,6 +1583,18 @@ struct hl_cs_in {/* the core command to be sent towards engine cores */ __u32 core_command; }; + +/* Valid only when HL_CS_FLAGS_ENGINES_COMMAND is set */ +struct { +/* this holds address of array of uint32 for engines */ +__u64 engines; + +/* number of engines in engines array */ +__u32 num_engines; + +/* the engine command to be sent towards engines */ +__u32 engine_command; + }; };union {
Signature · 1 line
2.39.2
Oded Gabbay
maintainerPATCH 2/3 habanalabs/gaudi2: move soft-reset wait to soft-reset execute
From: Dafna Hirschfeld <[email protected]>
We plan to do soft-reset either by mmio or by using cpucp packet depending on the FW version. We don't want to check FW version in two different places for that (execute soft-reset and wait to soft-reset) so move the waiting to gaudi2_execute_soft_reset. This also makes sense because the cpucp also does the waiting.
--- drivers/accel/habanalabs/gaudi2/gaudi2.c | 50 ++++++++++++------------ 1 file changed, 26 insertions(+), 24 deletions(-)
drivers/accel/habanalabs/gaudi2/gaudi2.c +26 -24
@@ -6068,17 +6068,37 @@ static void gaudi2_execute_hard_reset(struct hl_device *hdev, u32 reset_sleep_msWREG32 (mmPSOC_RESET_CONF_SW_ALL_RST,1 ); } +static void gaudi2_get_soft_rst_done_indication (struct hl_device *hdev,u32 poll_timeout_us) +{ +int i, rc =0 ; +u32 reg_val; + +for (i =0 ; i <GAUDI2_RESET_POLL_CNT ; i++) + rc =hl_poll_timeout ( + hdev, + mmCPU_RST_STATUS_TO_HOST, + reg_val, + reg_val ==CPU_RST_STATUS_SOFT_RST_DONE , +1000 , + poll_timeout_us); + +if (rc) +dev_err (hdev->dev,"Timeout while waiting for FW to complete soft reset (0x%x)\n" , + reg_val); +} +/** * gaudi2_execute_soft_reset - execute soft reset by driver/FW * * @hdev: pointer to the habanalabs device structure * @reset_sleep_ms: sleep time in msec after reset * @driver_performs_reset: true if driver should perform reset instead of f/w. +* @poll_timeout_us: time to wait for response from f/w. * * This function executes soft reset based on if driver/FW should do the reset */ static void gaudi2_execute_soft_reset (struct hl_device *hdev,u32 reset_sleep_ms, -bool driver_performs_reset) +bool driver_performs_reset,u32 poll_timeout_us) {struct cpu_dyn_regs *dyn_regs = &hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
@@ -6091,6 +6111,7 @@ static void gaudi2_execute_soft_reset(struct hl_device *hdev, u32 reset_sleep_msWREG32 (le32_to_cpu (dyn_regs->gic_host_soft_rst_irq), gaudi2_irq_map_table[GAUDI2_EVENT_CPU_SOFT_RESET ].cpu_id); +gaudi2_get_soft_rst_done_indication (hdev, poll_timeout_us);return ; }
@@ -6133,25 +6154,6 @@ static void gaudi2_poll_btm_indication(struct hl_device *hdev, u32 reset_sleep_mdev_err (hdev->dev,"Timeout while waiting for device to reset 0x%x\n" , reg_val); } -static void gaudi2_get_soft_rst_done_indication (struct hl_device *hdev,u32 poll_timeout_us) -{ -int i, rc =0 ; -u32 reg_val; - -for (i =0 ; i <GAUDI2_RESET_POLL_CNT ; i++) - rc =hl_poll_timeout ( - hdev, - mmCPU_RST_STATUS_TO_HOST, - reg_val, - reg_val ==CPU_RST_STATUS_SOFT_RST_DONE , -1000 , - poll_timeout_us); - -if (rc) -dev_err (hdev->dev,"Timeout while waiting for FW to complete soft reset (0x%x)\n" , - reg_val); -} -static int gaudi2_hw_fini (struct hl_device *hdev,bool hard_reset,bool fw_reset) {struct gaudi2_device *gaudi2 = hdev->asic_specific;
@@ -6184,11 +6186,12 @@ static int gaudi2_hw_fini(struct hl_device *hdev, bool hard_reset, bool fw_reset*/ driver_performs_reset = (hdev->fw_components ==FW_TYPE_PREBOOT_CPU && !hdev->asic_prop.fw_security_enabled); -gaudi2_execute_soft_reset (hdev, reset_sleep_ms, driver_performs_reset); +gaudi2_execute_soft_reset (hdev, reset_sleep_ms, driver_performs_reset, + poll_timeout_us); } skip_reset: -if (driver_performs_reset || hard_reset) +if (driver_performs_reset || hard_reset) {/* * Instead of waiting for BTM indication we should wait for preboot ready: * Consider the below scenario:
@@ -6214,8 +6217,7 @@ static int gaudi2_hw_fini(struct hl_device *hdev, bool hard_reset, bool fw_reset }else {gaudi2_poll_btm_indication (hdev, reset_sleep_ms, poll_timeout_us); } - else -gaudi2_get_soft_rst_done_indication (hdev ,poll_timeout_us ); + }if (!gaudi2)return 0 ;
Signature · 1 line
2.39.2
Oded Gabbay
maintainerPATCH 3/3 habanalabs: postpone mem_mgr IDR destruction to hpriv_release()
In reply to Oded GabbayFrom: Tomer Tayar <[email protected]>
The memory manager IDR is currently destroyed when user releases the file descriptor.
However, at this point the user context might be still held, and memory buffers might be still in use.
Later on, calls to release those buffers will fail due to not finding their handles in the IDR, leading to a memory leak.
To avoid this leak, split the IDR destruction from the memory manager fini, and postpone it to hpriv_release() when there is no user context and no buffers are used.
--- drivers/accel/habanalabs/common/device.c | 9 +++++++++ drivers/accel/habanalabs/common/habanalabs.h | 1 + drivers/accel/habanalabs/common/habanalabs_drv.c | 1 + drivers/accel/habanalabs/common/memory_mgr.c | 13 ++++++++++++- 4 files changed, 23 insertions(+), 1 deletion(-)
drivers/accel/habanalabs/common/device.c +9 -0
@@ -413,6 +413,9 @@ static void hpriv_release(struct kref *ref)mutex_destroy (&hpriv->ctx_lock);mutex_destroy (&hpriv->restore_phase_mutex); +/* There should be no memory buffers at this point and handles IDR can be destroyed */ +hl_mem_mgr_idr_destroy (&hpriv->mem_mgr); +/* Device should be reset if reset-upon-device-release is enabled, or if there is a pending * reset that waits for device release. */
@@ -534,6 +537,10 @@ static int hl_device_release(struct inode *inode, struct file *filp) }hl_ctx_mgr_fini (hdev, &hpriv->ctx_mgr); + +/* Memory buffers might be still in use at this point and thus the handles IDR destruction +* is postponed to hpriv_release(). +*/ hl_mem_mgr_fini (&hpriv->mem_mgr); hdev->compute_ctx_in_release =1 ;
@@ -910,6 +917,7 @@ static int device_early_init(struct hl_device *hdev) free_cb_mgr:hl_mem_mgr_fini (&hdev->kernel_mem_mgr); +hl_mem_mgr_idr_destroy (&hdev->kernel_mem_mgr); free_chip_info:kfree (hdev->hl_chip_info); free_prefetch_wq:
@@ -953,6 +961,7 @@ static void device_early_fini(struct hl_device *hdev)mutex_destroy (&hdev->clk_throttling.lock);hl_mem_mgr_fini (&hdev->kernel_mem_mgr); +hl_mem_mgr_idr_destroy (&hdev->kernel_mem_mgr);kfree (hdev->hl_chip_info);
drivers/accel/habanalabs/common/habanalabs.h +1 -0
@@ -3927,6 +3927,7 @@ const char *hl_sync_engine_to_string(enum hl_sync_engine_type engine_type);void hl_mem_mgr_init (struct device *dev,struct hl_mem_mgr *mmg);void hl_mem_mgr_fini (struct hl_mem_mgr *mmg); +void hl_mem_mgr_idr_destroy (struct hl_mem_mgr *mmg);int hl_mem_mgr_mmap (struct hl_mem_mgr *mmg,struct vm_area_struct *vma,void *args); struct hl_mmap_mem_buf *hl_mmap_mem_buf_get(struct hl_mem_mgr *mmg,
drivers/accel/habanalabs/common/habanalabs_drv.c +1 -0
@@ -234,6 +234,7 @@ int hl_device_open(struct inode *inode, struct file *filp) out_err:mutex_unlock (&hdev->fpriv_list_lock);hl_mem_mgr_fini (&hpriv->mem_mgr); +hl_mem_mgr_idr_destroy (&hpriv->mem_mgr);hl_ctx_mgr_fini (hpriv->hdev, &hpriv->ctx_mgr); filp->private_data =NULL ;mutex_destroy (&hpriv->ctx_lock);
drivers/accel/habanalabs/common/memory_mgr.c +12 -1
@@ -341,8 +341,19 @@ void hl_mem_mgr_fini(struct hl_mem_mgr *mmg)"%s: Buff handle %u for CTX is still alive\n" , topic, id); } +} -/* TODO: can it happen that some buffer is still in use at this point? */ +/** +* hl_mem_mgr_idr_destroy() - destroy memory manager IDR. +* @mmg: parent unified memory manager +* +* Destroy the memory manager IDR. +* Shall be called when IDR is empty and no memory buffers are in use. +*/ +void hl_mem_mgr_idr_destroy (struct hl_mem_mgr *mmg) +{ +if (!idr_is_empty (&mmg->handles)) +dev_crit (mmg->dev,"memory manager IDR is destroyed while it is not empty!\n" );idr_destroy (&mmg->handles); }
Signature · 1 line
2.39.2