Habana Labs kernel driver
Subsystems (7)
Re: [PATCH 07/15] habanalabs: add h/w queues module
In reply to Oded GabbayQuoted 151 lines · Oded Gabbay
This patch adds the H/W queues module and the code to initialize Goya's various compute and DMA engines and their queues.
Goya has 5 DMA channels, 8 TPC engines and a single MME engine. For each channel/engine, there is a H/W queue logic which is used to pass commands from the user to the H/W. That logic is called QMAN.
There are two types of QMANs: external and internal. The DMA QMANs are considered external while the TPC and MME QMANs are considered internal. For each external queue there is a completion queue, which is located on the Host memory.
The differences between external and internal QMANs are:
1. The location of the queue's memory. External QMANs are located on the Host memory while internal QMANs are located on the on-chip memory.
2. The external QMAN write an entry to a completion queue and sends an MSI-X interrupt upon completion of a command buffer that was given to it. The internal QMAN doesn't do that.
Signed-off-by Oded Gabbay <[email protected]>--- drivers/misc/habanalabs/Makefile | 2 +- drivers/misc/habanalabs/device.c | 74 +- drivers/misc/habanalabs/goya/goya.c | 1518 +++++++++++++++-- drivers/misc/habanalabs/goya/goyaP.h | 6 + drivers/misc/habanalabs/habanalabs.h | 176 +- drivers/misc/habanalabs/habanalabs_drv.c | 6 + drivers/misc/habanalabs/hw_queue.c | 404 +++++ .../habanalabs/include/goya/goya_packets.h | 234 +++ .../habanalabs/include/habanalabs_device_if.h | 272 +++ drivers/misc/habanalabs/irq.c | 150 ++ 10 files changed, 2721 insertions(+), 121 deletions(-) create mode 100644 drivers/misc/habanalabs/hw_queue.c create mode 100644 drivers/misc/habanalabs/include/goya/goya_packets.h create mode 100644 drivers/misc/habanalabs/irq.cdrivers/misc/habanalabs/Makefile +1
-1@@ -5,7 +5,7 @@ obj-m := habanalabs.o habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \ - command_buffer.o + command_buffer.o hw_queue.o irq.o include $(src)/goya/Makefile habanalabs-y += $(HL_GOYA_FILES)drivers/misc/habanalabs/device.c +51
-2@@ -170,13 +170,22 @@ static int device_early_init(struct hl_device *hdev)if (rc) goto early_fini; + hdev->cq_wq =alloc_workqueue ("hl-free-jobs" ,WQ_UNBOUND ,0 ); +if (hdev->cq_wq ==NULL ) { +dev_err (hdev->dev,"Failed to allocate CQ workqueue\n" ); + goto asid_fini; + } +hl_cb_mgr_init (&hdev->kernel_cb_mgr);mutex_init (&hdev->device_open); +mutex_init (&hdev->send_cpu_message_lock);atomic_set (&hdev->fd_open_cnt,0 );return 0 ; +asid_fini: +hl_asid_fini (hdev); early_fini:if (hdev->asic_funcs->early_fini) hdev->asic_funcs->early_fini (hdev);@@ -192,9 +201,12 @@ static int device_early_init(struct hl_device *hdev)*/ static void device_early_fini (struct hl_device *hdev) { +mutex_destroy (&hdev->send_cpu_message_lock);hl_cb_mgr_fini (hdev, &hdev->kernel_cb_mgr); +destroy_workqueue (hdev->cq_wq); +hl_asid_fini (hdev);if (hdev->asic_funcs->early_fini)@@ -273,7 +285,7 @@ int hl_device_resume(struct hl_device *hdev)*/ int hl_device_init (struct hl_device *hdev,struct class *hclass) { -int rc; +int i, rc, cq_ready_cnt;/* Create device */ rc =device_setup_cdev (hdev, hclass, hdev->id, &hl_ops);@@ -294,11 +306,48 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)if (rc) goto early_fini; +/* +* 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; + } + +/* +* 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 +*/ + hdev->completion_queue = +kcalloc (hdev->asic_prop.completion_queues_count, +sizeof (*hdev->completion_queue),GFP_KERNEL ); + +if (!hdev->completion_queue) { +dev_err (hdev->dev,"failed to allocate completion queues\n" ); + rc = -ENOMEM ; + goto hw_queues_destroy; + } + +for (i =0 , cq_ready_cnt =0 ; + i < hdev->asic_prop.completion_queues_count; + i++, cq_ready_cnt++) { + rc =hl_cq_init (hdev, &hdev->completion_queue[i], i); +if (rc) { +dev_err (hdev->dev, +"failed to initialize completion queue\n" ); + goto cq_fini; + } + } +/* Allocate the kernel context */ hdev->kernel_ctx =kzalloc (sizeof (*hdev->kernel_ctx),GFP_KERNEL );if (!hdev->kernel_ctx) { rc = -ENOMEM ; - goto sw_fini; + goto cq_fini; } hdev->user_ctx =NULL ;
drivers/misc/habanalabs/device.c int hl_device_init(struct hl_device *hdev, struct class *hclass) · line 380
Quoted 8 lines
drivers/misc/habanalabs/device.c +5
-0@@ -324,6 +373,14 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) hdev->disabled = false; +/* 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 ;
Why rc is 0 here?
Quoted 242 lines
drivers/misc/habanalabs/device.c +16
-0+ goto out_disabled; + } +dev_notice (hdev->dev,"Successfully added device to habanalabs driver\n" );@@ -335,6 +392,12 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) "kernel ctx is still alive on initialization failure\n"); free_ctx:kfree (hdev->kernel_ctx); +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); sw_fini: hdev->asic_funcs->sw_fini (hdev); early_fini:@@ -364,6 +427,7 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)*/ void hl_device_fini (struct hl_device *hdev) { +int i;dev_info (hdev->dev,"Removing device\n" );/* Mark device as disabled */ @@ -378,6 +442,12 @@ void hl_device_fini(struct hl_device *hdev)/* Reset the H/W. It will be in idle state after this returns */ hdev->asic_funcs->hw_fini (hdev, true); +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++) +hl_cq_fini (hdev, &hdev->completion_queue[i]); +kfree (hdev->completion_queue); + +hl_hw_queues_destroy (hdev); +/* Call ASIC S/W finalize function */ hdev->asic_funcs->sw_fini (hdev);drivers/misc/habanalabs/goya/goya.c +173
-0@@ -98,6 +98,26 @@static void goya_get_fixed_properties (struct hl_device *hdev) {struct asic_fixed_properties *prop = &hdev->asic_prop; +int i; + +for (i =0 ; i <NUMBER_OF_EXT_HW_QUEUES ; i++) { + prop->hw_queues_props[i].type =QUEUE_TYPE_EXT ; + prop->hw_queues_props[i].kmd_only =0 ; + } + +for (; i <NUMBER_OF_EXT_HW_QUEUES +NUMBER_OF_CPU_HW_QUEUES ; i++) { + prop->hw_queues_props[i].type =QUEUE_TYPE_CPU ; + prop->hw_queues_props[i].kmd_only =1 ; + } + +for (; i <NUMBER_OF_EXT_HW_QUEUES +NUMBER_OF_CPU_HW_QUEUES + +NUMBER_OF_INT_HW_QUEUES ; i++) { + prop->hw_queues_props[i].type =QUEUE_TYPE_INT ; + prop->hw_queues_props[i].kmd_only =0 ; + } + +for (; i <HL_MAX_QUEUES ; i++) + prop->hw_queues_props[i].type =QUEUE_TYPE_NA ; prop->completion_queues_count =NUMBER_OF_CMPLT_QUEUES ;@@ -126,6 +146,18 @@ static void goya_get_fixed_properties(struct hl_device *hdev) prop->high_pll =PLL_HIGH_DEFAULT ; } +int goya_send_pci_access_msg (struct hl_device *hdev,u32 opcode) +{ +struct armcp_packet pkt; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode = opcode; + +return hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt, +sizeof (pkt),HL_DEVICE_TIMEOUT_USEC ,NULL ); +} +/** * goya_pci_bars_map - Map PCI BARS of Goya device * @@ -509,6 +541,8 @@ static int goya_sw_init(struct hl_device *hdev)if (!goya)return -ENOMEM ; + goya->test_cpu_queue = goya_test_cpu_queue; +/* according to goya_init_iatu */ goya->ddr_bar_cur_addr =DRAM_PHYS_BASE ; hdev->asic_specific = goya;@@ -595,6 +629,299 @@ int goya_sw_fini(struct hl_device *hdev)return 0 ; } +static void goya_init_dma_qman (struct hl_device *hdev,int dma_id, +dma_addr_t bus_address) +{ +struct goya_device *goya = hdev->asic_specific; +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u32 reg_off = dma_id * (mmDMA_QM_1_PQ_PI - mmDMA_QM_0_PQ_PI); + + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + +WREG32 (mmDMA_QM_0_PQ_BASE_LO + reg_off,lower_32_bits (bus_address)); +WREG32 (mmDMA_QM_0_PQ_BASE_HI + reg_off,upper_32_bits (bus_address)); + +WREG32 (mmDMA_QM_0_PQ_SIZE + reg_off,ilog2 (HL_QUEUE_LENGTH )); +WREG32 (mmDMA_QM_0_PQ_PI + reg_off,0 ); +WREG32 (mmDMA_QM_0_PQ_CI + reg_off,0 ); + +WREG32 (mmDMA_QM_0_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo); +WREG32 (mmDMA_QM_0_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi); +WREG32 (mmDMA_QM_0_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo); +WREG32 (mmDMA_QM_0_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi); +WREG32 (mmDMA_QM_0_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo); +WREG32 (mmDMA_QM_0_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi); +WREG32 (mmDMA_QM_0_GLBL_ERR_WDATA + reg_off, +GOYA_ASYNC_EVENT_ID_DMA0_QM + dma_id); + +/* PQ has buffer of 2 cache lines, while CQ has 8 lines */ +WREG32 (mmDMA_QM_0_PQ_CFG1 + reg_off,0x00020002 ); +WREG32 (mmDMA_QM_0_CQ_CFG1 + reg_off,0x00080008 ); + +if (dma_id ==0 ) +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off,QMAN_DMA_FULLY_TRUSTED ); +else +if (goya->hw_cap_initialized &HW_CAP_MMU ) +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off, +QMAN_DMA_PARTLY_TRUSTED ); +else +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off, +QMAN_DMA_FULLY_TRUSTED ); + +WREG32 (mmDMA_QM_0_GLBL_ERR_CFG + reg_off,QMAN_DMA_ERR_MSG_EN ); +WREG32 (mmDMA_QM_0_GLBL_CFG0 + reg_off,QMAN_DMA_ENABLE ); +} + +static void goya_init_dma_ch (struct hl_device *hdev,int dma_id) +{ +u32 gic_base_lo, gic_base_hi; +u64 sob_addr; +u32 reg_off = dma_id * (mmDMA_CH_1_CFG1 - mmDMA_CH_0_CFG1); + + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + +WREG32 (mmDMA_CH_0_ERRMSG_ADDR_LO + reg_off, gic_base_lo); +WREG32 (mmDMA_CH_0_ERRMSG_ADDR_HI + reg_off, gic_base_hi); +WREG32 (mmDMA_CH_0_ERRMSG_WDATA + reg_off, +GOYA_ASYNC_EVENT_ID_DMA0_CH + dma_id); + +if (dma_id) { + sob_addr =CFG_BASE + mmSYNC_MNGR_SOB_OBJ_1000 + + (dma_id -1 ) *4 ; +WREG32 (mmDMA_CH_0_WR_COMP_ADDR_LO + reg_off, +lower_32_bits (sob_addr)); +WREG32 (mmDMA_CH_0_WR_COMP_ADDR_HI + reg_off, +upper_32_bits (sob_addr)); +WREG32 (mmDMA_CH_0_WR_COMP_WDATA + reg_off,0x80000001 ); + } +} + +/** +* goya_init_dma_qmans - Initialize QMAN DMA registers +* +* @hdev: pointer to hl_device structure +* +* Initialize the H/W registers of the QMAN DMA channels +* +*/ +static void goya_init_dma_qmans (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +struct hl_hw_queue *q; +dma_addr_t bus_address; +int i; + +if (goya->hw_cap_initialized &HW_CAP_DMA ) +return ; + + q = &hdev->kernel_queues[0 ]; + +for (i =0 ; i <NUMBER_OF_EXT_HW_QUEUES ; i++, q++) { + bus_address = q->bus_address + + hdev->asic_prop.host_phys_base_address; + +goya_init_dma_qman (hdev, i, bus_address); +goya_init_dma_ch (hdev, i); + } + + goya->hw_cap_initialized |=HW_CAP_DMA ; +} + +/** +* goya_disable_external_queues - Disable external queues +* +* @hdev: pointer to hl_device structure +* +*/ +static void goya_disable_external_queues (struct hl_device *hdev) +{ +WREG32 (mmDMA_QM_0_GLBL_CFG0,0 ); +WREG32 (mmDMA_QM_1_GLBL_CFG0,0 ); +WREG32 (mmDMA_QM_2_GLBL_CFG0,0 ); +WREG32 (mmDMA_QM_3_GLBL_CFG0,0 ); +WREG32 (mmDMA_QM_4_GLBL_CFG0,0 ); +} + +static int goya_stop_queue (struct hl_device *hdev,u32 cfg_reg, +u32 cp_sts_reg,u32 glbl_sts0_reg) +{ +int rc; +u32 status; + +/* use the values of TPC0 as they are all the same*/ + +WREG32 (cfg_reg,1 <<TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT ); + + status =RREG32 (cp_sts_reg); +if (status &TPC0_QM_CP_STS_FENCE_IN_PROGRESS_MASK ) { + rc = hl_poll_timeout(
drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 780
Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10
-0+ hdev, + cp_sts_reg, + status, + !(status &TPC0_QM_CP_STS_FENCE_IN_PROGRESS_MASK ), +1000 , + QMAN_FENCE_TIMEOUT_USEC); + +/* if QMAN is stuck in fence no need to check for stop */ +if (rc) +return 0 ;
Isn't it an error?
Quoted 554 lines
drivers/misc/habanalabs/goya/goya.c +401
-113+ } + + rc =hl_poll_timeout ( + hdev, + glbl_sts0_reg, + status, + (status &TPC0_QM_GLBL_STS0_CP_IS_STOP_MASK ), +1000 , +QMAN_STOP_TIMEOUT_USEC ); + +if (rc) { +dev_err (hdev->dev, +"Timeout while waiting for QMAN to stop\n" ); +return -EINVAL ; + } + +return 0 ; +} + +/** +* goya_stop_external_queues - Stop external queues +* +* @hdev: pointer to hl_device structure +* +* Returns 0 on success +* +*/ +static int goya_stop_external_queues (struct hl_device *hdev) +{ +int rc =goya_stop_queue (hdev, + mmDMA_QM_0_GLBL_CFG1, + mmDMA_QM_0_CP_STS, + mmDMA_QM_0_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 0\n" ); + + rc =goya_stop_queue (hdev, + mmDMA_QM_1_GLBL_CFG1, + mmDMA_QM_1_CP_STS, + mmDMA_QM_1_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 1\n" ); + + rc =goya_stop_queue (hdev, + mmDMA_QM_2_GLBL_CFG1, + mmDMA_QM_2_CP_STS, + mmDMA_QM_2_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 2\n" ); + + rc =goya_stop_queue (hdev, + mmDMA_QM_3_GLBL_CFG1, + mmDMA_QM_3_CP_STS, + mmDMA_QM_3_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 3\n" ); + + rc =goya_stop_queue (hdev, + mmDMA_QM_4_GLBL_CFG1, + mmDMA_QM_4_CP_STS, + mmDMA_QM_4_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 4\n" ); + +return rc; +} + +static void goya_resume_external_queues (struct hl_device *hdev) +{ +WREG32 (mmDMA_QM_0_GLBL_CFG1,0 ); +WREG32 (mmDMA_QM_1_GLBL_CFG1,0 ); +WREG32 (mmDMA_QM_2_GLBL_CFG1,0 ); +WREG32 (mmDMA_QM_3_GLBL_CFG1,0 ); +WREG32 (mmDMA_QM_4_GLBL_CFG1,0 ); +} + +/** +* goya_init_cpu_queues - Initialize PQ/CQ/EQ of CPU +* +* @hdev: pointer to hl_device structure +* +* Returns 0 on success +* +*/ +int goya_init_cpu_queues (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +dma_addr_t bus_address; +u32 status; +struct hl_hw_queue *cpu_pq = &hdev->kernel_queues[GOYA_QUEUE_ID_CPU_PQ ]; +int err; + +if (!hdev->cpu_queues_enable) +return 0 ; + +if (goya->hw_cap_initialized &HW_CAP_CPU_Q ) +return 0 ; + + bus_address = cpu_pq->bus_address + + hdev->asic_prop.host_phys_base_address; +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_0,lower_32_bits (bus_address)); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_1,upper_32_bits (bus_address)); + + bus_address = hdev->cpu_accessible_dma_address + + hdev->asic_prop.host_phys_base_address; +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_8,lower_32_bits (bus_address)); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_9,upper_32_bits (bus_address)); + +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_5,HL_QUEUE_SIZE_IN_BYTES ); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_10,CPU_ACCESSIBLE_MEM_SIZE ); + +/* Used for EQ CI */ +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_6,0 ); + +WREG32 (mmCPU_IF_PF_PQ_PI,0 ); + +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_7,PQ_INIT_STATUS_READY_FOR_CP ); + +WREG32 (mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, +GOYA_ASYNC_EVENT_ID_PI_UPDATE ); + + err =hl_poll_timeout ( + hdev, + mmPSOC_GLOBAL_CONF_SCRATCHPAD_7, + status, + (status ==PQ_INIT_STATUS_READY_FOR_HOST ), +1000 , +GOYA_CPU_TIMEOUT_USEC ); + +if (err) { +dev_err (hdev->dev, +"Failed to communicate with ARM CPU (ArmCP timeout)\n" ); +return -EIO ; + } + + goya->hw_cap_initialized |=HW_CAP_CPU_Q ; +return 0 ; +} +/** * goya_init_pll - Initialize pll registers * @@ -1960,152 +2287,646 @@ static void goya_init_golden_registers(struct hl_device *hdev) goya->hw_cap_initialized |=HW_CAP_GOLDEN ; } - -/** -* goya_push_uboot_to_device - Push u-boot FW code to device -* -* @hdev: pointer to hl_device structure -* -* Copy u-boot fw code from firmware file to SRAM BAR. -* Returns 0 on success -* -*/ -static int goya_push_uboot_to_device (struct hl_device *hdev) +static void goya_init_mme_qman (struct hl_device *hdev) { -char fw_name[200 ]; -const u64 *fw_data; -void __iomem *dst; -size_t fw_size, i; -int rc; +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u64 qman_base_addr; -snprintf (fw_name,sizeof (fw_name),"habanalabs/goya/goya-u-boot.bin" ); + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); - rc =request_firmware (&hdev->spl_fw, fw_name, hdev->dev); + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); -if (rc) { -dev_err (hdev->dev,"Failed to request u-boot fw image\n" ); - goto out; - } + qman_base_addr = hdev->asic_prop.sram_base_address + +MME_QMAN_BASE_OFFSET ; - fw_size = hdev->spl_fw->size; -if ((fw_size %4 ) !=0 ) { -dev_err (hdev->dev,"illegal u-boot firmware size %lu\n" , - fw_size); - rc = -EINVAL ; - goto out; - } +WREG32 (mmMME_QM_PQ_BASE_LO,lower_32_bits (qman_base_addr)); +WREG32 (mmMME_QM_PQ_BASE_HI,upper_32_bits (qman_base_addr)); +WREG32 (mmMME_QM_PQ_SIZE,ilog2 (MME_QMAN_LENGTH )); +WREG32 (mmMME_QM_PQ_PI,0 ); +WREG32 (mmMME_QM_PQ_CI,0 ); +WREG32 (mmMME_QM_CP_LDMA_SRC_BASE_LO_OFFSET,0x10C0 ); +WREG32 (mmMME_QM_CP_LDMA_SRC_BASE_HI_OFFSET,0x10C4 ); +WREG32 (mmMME_QM_CP_LDMA_TSIZE_OFFSET,0x10C8 ); +WREG32 (mmMME_QM_CP_LDMA_COMMIT_OFFSET,0x10CC ); -dev_dbg (hdev->dev,"u-boot firmware size == %lu\n" , fw_size); +WREG32 (mmMME_QM_CP_MSG_BASE0_ADDR_LO, mtr_base_lo); +WREG32 (mmMME_QM_CP_MSG_BASE0_ADDR_HI, mtr_base_hi); +WREG32 (mmMME_QM_CP_MSG_BASE1_ADDR_LO, so_base_lo); +WREG32 (mmMME_QM_CP_MSG_BASE1_ADDR_HI, so_base_hi); - fw_data = (const u64 *) hdev->spl_fw->data; - dst = hdev->pcie_bar[SRAM_CFG_BAR_ID ] +UBOOT_FW_OFFSET ; +/* QMAN CQ has 8 cache lines */ +WREG32 (mmMME_QM_CQ_CFG1,0x00080008 ); -if ((hdev->spl_fw->size %8 ) !=0 ) - fw_size -=8 ; +WREG32 (mmMME_QM_GLBL_ERR_ADDR_LO, gic_base_lo); +WREG32 (mmMME_QM_GLBL_ERR_ADDR_HI, gic_base_hi); -for (i =0 ; i < fw_size ; i +=8 , fw_data++, dst +=8 ) { -if (!(i & (0x80000 -1 ))) -dev_dbg (hdev->dev, -"u-boot copied so far %lu out of %lu" , - i, fw_size); +WREG32 (mmMME_QM_GLBL_ERR_WDATA,GOYA_ASYNC_EVENT_ID_MME_QM ); -writeq (*fw_data, dst); - } +WREG32 (mmMME_QM_GLBL_ERR_CFG,QMAN_MME_ERR_MSG_EN ); -if ((hdev->spl_fw->size %8 ) !=0 ) -writel (*(const u32 *) fw_data, dst); +WREG32 (mmMME_QM_GLBL_PROT,QMAN_MME_ERR_PROT ); -out: -release_firmware (hdev->spl_fw); -return rc; +WREG32 (mmMME_QM_GLBL_CFG0,QMAN_MME_ENABLE ); } -/** -* goya_push_linux_to_device - Push LINUX FW code to device -* -* @hdev: pointer to hl_device structure -* -* Copy LINXU fw code from firmware file to DDR BAR. -* Returns 0 on success -* -*/ -static int goya_push_linux_to_device (struct hl_device *hdev) +static void goya_init_mme_cmdq (struct hl_device *hdev) { -char fw_name[200 ]; -const u64 *fw_data; -void __iomem *dst; -size_t fw_size, i; -int rc; +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u64 qman_base_addr; -snprintf (fw_name,sizeof (fw_name),"habanalabs/goya/goya-fit.itb" ); + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); - rc =request_firmware (&hdev->spl_fw, fw_name, hdev->dev); + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); -if (rc) { -dev_err (hdev->dev,"Failed to request Linux fw image\n" ); - goto out; - } + qman_base_addr = hdev->asic_prop.sram_base_address + +MME_QMAN_BASE_OFFSET ; - fw_size = hdev->spl_fw->size; -if ((fw_size %4 ) !=0 ) { -dev_err (hdev->dev,"illegal Linux firmware size %lu\n" , - fw_size); - rc = -EINVAL ; - goto out; - } +WREG32 (mmMME_CMDQ_CP_MSG_BASE0_ADDR_LO, mtr_base_lo); +WREG32 (mmMME_CMDQ_CP_MSG_BASE0_ADDR_HI, mtr_base_hi); +WREG32 (mmMME_CMDQ_CP_MSG_BASE1_ADDR_LO, so_base_lo); +WREG32 (mmMME_CMDQ_CP_MSG_BASE1_ADDR_HI, so_base_hi); -dev_dbg (hdev->dev,"Linux firmware size == %lu\n" , fw_size); +/* CMDQ CQ has 20 cache lines */ +WREG32 (mmMME_CMDQ_CQ_CFG1,0x00140014 ); - fw_data = (const u64 *) hdev->spl_fw->data; - dst = hdev->pcie_bar[DDR_BAR_ID ] +LINUX_FW_OFFSET ; +WREG32 (mmMME_CMDQ_GLBL_ERR_ADDR_LO, gic_base_lo); +WREG32 (mmMME_CMDQ_GLBL_ERR_ADDR_HI, gic_base_hi); -if ((hdev->spl_fw->size %8 ) !=0 ) - fw_size -=8 ; +WREG32 (mmMME_CMDQ_GLBL_ERR_WDATA,GOYA_ASYNC_EVENT_ID_MME_CMDQ ); -for (i =0 ; i < fw_size ; i +=8 , fw_data++, dst +=8 ) { -if (!(i & (0x80000 -1 ))) { -dev_dbg (hdev->dev, -"Linux copied so far %lu out of %lu" , - i, fw_size); -usleep_range (20 ,100 ); - } -writeq (*fw_data, dst); - } +WREG32 (mmMME_CMDQ_GLBL_ERR_CFG,CMDQ_MME_ERR_MSG_EN ); -if ((hdev->spl_fw->size %8 ) !=0 ) -writel (*(const u32 *) fw_data, dst); +WREG32 (mmMME_CMDQ_GLBL_PROT,CMDQ_MME_ERR_PROT ); -out: -release_firmware (hdev->spl_fw); -return rc; +WREG32 (mmMME_CMDQ_GLBL_CFG0,CMDQ_MME_ENABLE ); } -static int goya_pldm_init_cpu (struct hl_device *hdev) +static void goya_init_mme_qmans (struct hl_device *hdev) { -u32 val, unit_rst_val; -int rc; +struct goya_device *goya = hdev->asic_specific; +u32 so_base_lo, so_base_hi; -/* Must initialize SRAM scrambler before pushing u-boot to SRAM */ -goya_init_golden_registers (hdev); +if (goya->hw_cap_initialized &HW_CAP_MME ) +return ; -/* Put ARM cores into reset */ -WREG32 (mmCPU_CA53_CFG_ARM_RST_CONTROL,CPU_RESET_ASSERT ); - val =RREG32 (mmCPU_CA53_CFG_ARM_RST_CONTROL); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); -/* Reset the CA53 MACRO */ - unit_rst_val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); -WREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N,CA53_RESET ); - val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); -WREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N, unit_rst_val); - val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); +WREG32 (mmMME_SM_BASE_ADDRESS_LOW, so_base_lo); +WREG32 (mmMME_SM_BASE_ADDRESS_HIGH, so_base_hi); - rc =goya_push_uboot_to_device (hdev); -if (rc) -return rc; +goya_init_mme_qman (hdev); +goya_init_mme_cmdq (hdev); - rc =goya_push_linux_to_device (hdev); -if (rc) -return rc; + goya->hw_cap_initialized |=HW_CAP_MME ; +} + +static void goya_init_tpc_qman (struct hl_device *hdev,u32 base_off,int tpc_id) +{ +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u64 qman_base_addr; +u32 reg_off = tpc_id * (mmTPC1_QM_PQ_PI - mmTPC0_QM_PQ_PI); + + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + + qman_base_addr = hdev->asic_prop.sram_base_address + base_off; + +WREG32 (mmTPC0_QM_PQ_BASE_LO + reg_off,lower_32_bits (qman_base_addr)); +WREG32 (mmTPC0_QM_PQ_BASE_HI + reg_off,upper_32_bits (qman_base_addr)); +WREG32 (mmTPC0_QM_PQ_SIZE + reg_off,ilog2 (TPC_QMAN_LENGTH )); +WREG32 (mmTPC0_QM_PQ_PI + reg_off,0 ); +WREG32 (mmTPC0_QM_PQ_CI + reg_off,0 ); +WREG32 (mmTPC0_QM_CP_LDMA_SRC_BASE_LO_OFFSET + reg_off,0x10C0 ); +WREG32 (mmTPC0_QM_CP_LDMA_SRC_BASE_HI_OFFSET + reg_off,0x10C4 ); +WREG32 (mmTPC0_QM_CP_LDMA_TSIZE_OFFSET + reg_off,0x10C8 ); +WREG32 (mmTPC0_QM_CP_LDMA_COMMIT_OFFSET + reg_off,0x10CC ); + +WREG32 (mmTPC0_QM_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo); +WREG32 (mmTPC0_QM_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi); +WREG32 (mmTPC0_QM_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo); +WREG32 (mmTPC0_QM_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi); + +WREG32 (mmTPC0_QM_CQ_CFG1 + reg_off,0x00080008 ); + +WREG32 (mmTPC0_QM_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo); +WREG32 (mmTPC0_QM_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi); + +WREG32 (mmTPC0_QM_GLBL_ERR_WDATA + reg_off, +GOYA_ASYNC_EVENT_ID_TPC0_QM + tpc_id); + +WREG32 (mmTPC0_QM_GLBL_ERR_CFG + reg_off,QMAN_TPC_ERR_MSG_EN ); + +WREG32 (mmTPC0_QM_GLBL_PROT + reg_off,QMAN_TPC_ERR_PROT ); + +WREG32 (mmTPC0_QM_GLBL_CFG0 + reg_off,QMAN_TPC_ENABLE ); +} + +static void goya_init_tpc_cmdq (struct hl_device *hdev,int tpc_id) +{ +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u32 reg_off = tpc_id * (mmTPC1_CMDQ_CQ_CFG1 - mmTPC0_CMDQ_CQ_CFG1); + + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + +WREG32 (mmTPC0_CMDQ_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo); +WREG32 (mmTPC0_CMDQ_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi); +WREG32 (mmTPC0_CMDQ_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo); +WREG32 (mmTPC0_CMDQ_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi); + +WREG32 (mmTPC0_CMDQ_CQ_CFG1 + reg_off,0x00140014 ); + +WREG32 (mmTPC0_CMDQ_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo); +WREG32 (mmTPC0_CMDQ_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi); + +WREG32 (mmTPC0_CMDQ_GLBL_ERR_WDATA + reg_off, +GOYA_ASYNC_EVENT_ID_TPC0_CMDQ + tpc_id); + +WREG32 (mmTPC0_CMDQ_GLBL_ERR_CFG + reg_off,CMDQ_TPC_ERR_MSG_EN ); + +WREG32 (mmTPC0_CMDQ_GLBL_PROT + reg_off,CMDQ_TPC_ERR_PROT ); + +WREG32 (mmTPC0_CMDQ_GLBL_CFG0 + reg_off,CMDQ_TPC_ENABLE ); +} + +static void goya_init_tpc_qmans (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +u32 so_base_lo, so_base_hi; +u32 cfg_off = mmTPC1_CFG_SM_BASE_ADDRESS_LOW - + mmTPC0_CFG_SM_BASE_ADDRESS_LOW; +int i; + +if (goya->hw_cap_initialized &HW_CAP_TPC ) +return ; + + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + +for (i =0 ; i <TPC_MAX_NUM ; i++) { +WREG32 (mmTPC0_CFG_SM_BASE_ADDRESS_LOW + i * cfg_off, + so_base_lo); +WREG32 (mmTPC0_CFG_SM_BASE_ADDRESS_HIGH + i * cfg_off, + so_base_hi); + } + +goya_init_tpc_qman (hdev,TPC0_QMAN_BASE_OFFSET ,0 ); +goya_init_tpc_qman (hdev,TPC1_QMAN_BASE_OFFSET ,1 ); +goya_init_tpc_qman (hdev,TPC2_QMAN_BASE_OFFSET ,2 ); +goya_init_tpc_qman (hdev,TPC3_QMAN_BASE_OFFSET ,3 ); +goya_init_tpc_qman (hdev,TPC4_QMAN_BASE_OFFSET ,4 ); +goya_init_tpc_qman (hdev,TPC5_QMAN_BASE_OFFSET ,5 ); +goya_init_tpc_qman (hdev,TPC6_QMAN_BASE_OFFSET ,6 ); +goya_init_tpc_qman (hdev,TPC7_QMAN_BASE_OFFSET ,7 ); + +for (i =0 ; i <TPC_MAX_NUM ; i++) +goya_init_tpc_cmdq (hdev, i); + + goya->hw_cap_initialized |=HW_CAP_TPC ; +} + +/** +* goya_disable_internal_queues - Disable internal queues +* +* @hdev: pointer to hl_device structure +* +*/ +static void goya_disable_internal_queues (struct hl_device *hdev) +{ +WREG32 (mmMME_QM_GLBL_CFG0,0 ); +WREG32 (mmMME_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC0_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC0_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC1_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC1_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC2_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC2_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC3_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC3_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC4_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC4_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC5_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC5_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC6_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC6_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC7_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC7_CMDQ_GLBL_CFG0,0 ); +} + +/** +* goya_stop_internal_queues - Stop internal queues +* +* @hdev: pointer to hl_device structure +* +* Returns 0 on success +* +*/ +static int goya_stop_internal_queues (struct hl_device *hdev) +{ +int rc, retval =0 ; + + rc =goya_stop_queue (hdev, + mmMME_QM_GLBL_CFG1, + mmMME_QM_CP_STS, + mmMME_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop MME QMAN\n" ); + retval = -EIO ; + }
drivers/misc/habanalabs/goya/goya.c static void goya_init_golden_registers(struct hl_device *hdev) · line 2589
Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10
-0+ + rc =goya_stop_queue (hdev, + mmMME_CMDQ_GLBL_CFG1, + mmMME_CMDQ_CP_STS, + mmMME_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop MME CMDQ\n" ); + retval = -EIO ; + }
If I understand correctly, the queues can be and should be stopped independently and failure to stop one of them wouldn't prevent stopping the others.
If that's the case a comment explaining that would be nice.
Quoted 656 lines
drivers/misc/habanalabs/goya/goya.c +599
-4+ rc =goya_stop_queue (hdev, + mmTPC0_QM_GLBL_CFG1, + mmTPC0_QM_CP_STS, + mmTPC0_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 0 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC0_CMDQ_GLBL_CFG1, + mmTPC0_CMDQ_CP_STS, + mmTPC0_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 0 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC1_QM_GLBL_CFG1, + mmTPC1_QM_CP_STS, + mmTPC1_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 1 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC1_CMDQ_GLBL_CFG1, + mmTPC1_CMDQ_CP_STS, + mmTPC1_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 1 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC2_QM_GLBL_CFG1, + mmTPC2_QM_CP_STS, + mmTPC2_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 2 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC2_CMDQ_GLBL_CFG1, + mmTPC2_CMDQ_CP_STS, + mmTPC2_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 2 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC3_QM_GLBL_CFG1, + mmTPC3_QM_CP_STS, + mmTPC3_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 3 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC3_CMDQ_GLBL_CFG1, + mmTPC3_CMDQ_CP_STS, + mmTPC3_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 3 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC4_QM_GLBL_CFG1, + mmTPC4_QM_CP_STS, + mmTPC4_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 4 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC4_CMDQ_GLBL_CFG1, + mmTPC4_CMDQ_CP_STS, + mmTPC4_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 4 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC5_QM_GLBL_CFG1, + mmTPC5_QM_CP_STS, + mmTPC5_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 5 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC5_CMDQ_GLBL_CFG1, + mmTPC5_CMDQ_CP_STS, + mmTPC5_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 5 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC6_QM_GLBL_CFG1, + mmTPC6_QM_CP_STS, + mmTPC6_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 6 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC6_CMDQ_GLBL_CFG1, + mmTPC6_CMDQ_CP_STS, + mmTPC6_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 6 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC7_QM_GLBL_CFG1, + mmTPC7_QM_CP_STS, + mmTPC7_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 7 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC7_CMDQ_GLBL_CFG1, + mmTPC7_CMDQ_CP_STS, + mmTPC7_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 7 CMDQ\n" ); + retval = -EIO ; + } + +return rc; +} + +static void goya_resume_internal_queues (struct hl_device *hdev) +{ +WREG32 (mmMME_QM_GLBL_CFG1,0 ); +WREG32 (mmMME_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC0_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC0_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC1_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC1_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC2_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC2_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC3_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC3_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC4_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC4_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC5_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC5_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC6_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC6_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC7_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC7_CMDQ_GLBL_CFG1,0 ); +} + + +/** +* goya_push_uboot_to_device - Push u-boot FW code to device +* +* @hdev: pointer to hl_device structure +* +* Copy u-boot fw code from firmware file to SRAM BAR. +* Returns 0 on success +* +*/ +static int goya_push_uboot_to_device (struct hl_device *hdev) +{ +char fw_name[200 ]; +const u64 *fw_data; +void __iomem *dst; +size_t fw_size, i; +int rc; + +snprintf (fw_name,sizeof (fw_name),"habanalabs/goya/goya-u-boot.bin" ); + + rc =request_firmware (&hdev->spl_fw, fw_name, hdev->dev); + +if (rc) { +dev_err (hdev->dev,"Failed to request u-boot fw image\n" ); + goto out; + } + + fw_size = hdev->spl_fw->size; +if ((fw_size %4 ) !=0 ) { +dev_err (hdev->dev,"illegal u-boot firmware size %lu\n" , + fw_size); + rc = -EINVAL ; + goto out; + } + +dev_dbg (hdev->dev,"u-boot firmware size == %lu\n" , fw_size); + + fw_data = (const u64 *) hdev->spl_fw->data; + dst = hdev->pcie_bar[SRAM_CFG_BAR_ID ] +UBOOT_FW_OFFSET ; + +if ((hdev->spl_fw->size %8 ) !=0 ) + fw_size -=8 ; + +for (i =0 ; i < fw_size ; i +=8 , fw_data++, dst +=8 ) { +if (!(i & (0x80000 -1 ))) +dev_dbg (hdev->dev, +"u-boot copied so far %lu out of %lu" , + i, fw_size); + +writeq (*fw_data, dst); + } + +if ((hdev->spl_fw->size %8 ) !=0 ) +writel (*(const u32 *) fw_data, dst); + +out: +release_firmware (hdev->spl_fw); +return rc; +} + +/** +* goya_push_linux_to_device - Push LINUX FW code to device +* +* @hdev: pointer to hl_device structure +* +* Copy LINXU fw code from firmware file to DDR BAR. +* Returns 0 on success +* +*/ +static int goya_push_linux_to_device (struct hl_device *hdev) +{ +char fw_name[200 ]; +const u64 *fw_data; +void __iomem *dst; +size_t fw_size, i; +int rc; + +snprintf (fw_name,sizeof (fw_name),"habanalabs/goya/goya-fit.itb" ); + + rc =request_firmware (&hdev->spl_fw, fw_name, hdev->dev); + +if (rc) { +dev_err (hdev->dev,"Failed to request Linux fw image\n" ); + goto out; + } + + fw_size = hdev->spl_fw->size; +if ((fw_size %4 ) !=0 ) { +dev_err (hdev->dev,"illegal Linux firmware size %lu\n" , + fw_size); + rc = -EINVAL ; + goto out; + } + +dev_dbg (hdev->dev,"Linux firmware size == %lu\n" , fw_size); + + fw_data = (const u64 *) hdev->spl_fw->data; + dst = hdev->pcie_bar[DDR_BAR_ID ] +LINUX_FW_OFFSET ; + +if ((hdev->spl_fw->size %8 ) !=0 ) + fw_size -=8 ; + +for (i =0 ; i < fw_size ; i +=8 , fw_data++, dst +=8 ) { +if (!(i & (0x80000 -1 ))) { +dev_dbg (hdev->dev, +"Linux copied so far %lu out of %lu" , + i, fw_size); +usleep_range (20 ,100 ); + } +writeq (*fw_data, dst); + } + +if ((hdev->spl_fw->size %8 ) !=0 ) +writel (*(const u32 *) fw_data, dst); + +out: +release_firmware (hdev->spl_fw); +return rc; +} + +static int goya_pldm_init_cpu (struct hl_device *hdev) +{ +u32 val, unit_rst_val; +int rc; + +/* Must initialize SRAM scrambler before pushing u-boot to SRAM */ +goya_init_golden_registers (hdev); + +/* Put ARM cores into reset */ +WREG32 (mmCPU_CA53_CFG_ARM_RST_CONTROL,CPU_RESET_ASSERT ); + val =RREG32 (mmCPU_CA53_CFG_ARM_RST_CONTROL); + +/* Reset the CA53 MACRO */ + unit_rst_val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); +WREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N,CA53_RESET ); + val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); +WREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N, unit_rst_val); + val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); + + rc =goya_push_uboot_to_device (hdev); +if (rc) +return rc; + + rc =goya_push_linux_to_device (hdev); +if (rc) +return rc;WREG32 (mmPSOC_GLOBAL_CONF_UBOOT_MAGIC,KMD_MSG_FIT_RDY );WREG32 (mmPSOC_GLOBAL_CONF_WARM_REBOOT,CPU_BOOT_STATUS_NA );@@ -2339,6 +3160,19 @@ static int goya_hw_init(struct hl_device *hdev)goya_init_security (hdev); +goya_init_dma_qmans (hdev); + +goya_init_mme_qmans (hdev); + +goya_init_tpc_qmans (hdev); + + rc =goya_init_cpu_queues (hdev); +if (rc) { +dev_err (hdev->dev,"failed to initialize CPU H/W queues %d\n" , + rc); + goto disable_queues; + } +/* CPU initialization is finished, we can now move to 48 bit DMA mask */ rc =pci_set_dma_mask (hdev->pdev,DMA_BIT_MASK (48 ));if (rc) {@@ -2347,7 +3181,7 @@ static int goya_hw_init(struct hl_device *hdev)if (rc) {dev_err (hdev->dev,"Unable to set pci dma mask to 32 bits\n" ); -return rc; + goto disable_pci_access; } }@@ -2359,7 +3193,7 @@ static int goya_hw_init(struct hl_device *hdev)if (rc) {dev_err (hdev->dev,"Unable to set pci consistent dma mask to 32 bits\n" ); -return rc; + goto disable_pci_access; } }@@ -2367,6 +3201,14 @@ static int goya_hw_init(struct hl_device *hdev) val =RREG32 (mmPCIE_DBI_DEVICE_ID_VENDOR_ID_REG);return 0 ; + +disable_pci_access: +goya_send_pci_access_msg (hdev,ARMCP_PACKET_DISABLE_PCI_ACCESS ); +disable_queues: +goya_disable_internal_queues (hdev); +goya_disable_external_queues (hdev); + +return rc; }/** @@ -2473,12 +3315,40 @@ static void goya_hw_fini(struct hl_device *hdev, bool hard_reset)int goya_suspend (struct hl_device *hdev) { -return 0 ; +int rc; + + rc =goya_stop_internal_queues (hdev); + +if (rc) { +dev_err (hdev->dev,"failed to stop internal queues\n" ); +return rc; + } + + rc =goya_stop_external_queues (hdev); + +if (rc) { +dev_err (hdev->dev,"failed to stop external queues\n" ); +return rc; + } + + rc =goya_send_pci_access_msg (hdev,ARMCP_PACKET_DISABLE_PCI_ACCESS ); +if (rc) +dev_err (hdev->dev,"Failed to disable PCI access from CPU\n" ); + +return rc; }int goya_resume (struct hl_device *hdev) { -return 0 ; +int rc; + +goya_resume_external_queues (hdev); +goya_resume_internal_queues (hdev); + + rc =goya_send_pci_access_msg (hdev,ARMCP_PACKET_ENABLE_PCI_ACCESS ); +if (rc) +dev_err (hdev->dev,"Failed to enable PCI access from CPU\n" ); +return rc; }int goya_mmap (struct hl_fpriv *hpriv,struct vm_area_struct *vma)@@ -2502,6 +3372,104 @@ int goya_cb_mmap(struct hl_device *hdev, struct vm_area_struct *vma,return rc; } +void goya_ring_doorbell (struct hl_device *hdev,u32 hw_queue_id,u32 pi) +{ +u32 db_reg_offset, db_value; +bool invalid_queue = false; + +switch (hw_queue_id) { +case GOYA_QUEUE_ID_DMA_0 : + db_reg_offset = mmDMA_QM_0_PQ_PI; +break ; + +case GOYA_QUEUE_ID_DMA_1 : + db_reg_offset = mmDMA_QM_1_PQ_PI; +break ; + +case GOYA_QUEUE_ID_DMA_2 : + db_reg_offset = mmDMA_QM_2_PQ_PI; +break ; + +case GOYA_QUEUE_ID_DMA_3 : + db_reg_offset = mmDMA_QM_3_PQ_PI; +break ; + +case GOYA_QUEUE_ID_DMA_4 : + db_reg_offset = mmDMA_QM_4_PQ_PI; +break ; + +case GOYA_QUEUE_ID_CPU_PQ : +if (hdev->cpu_queues_enable) + db_reg_offset = mmCPU_IF_PF_PQ_PI; +else + invalid_queue = true; +break ; + +case GOYA_QUEUE_ID_MME : + db_reg_offset = mmMME_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC0 : + db_reg_offset = mmTPC0_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC1 : + db_reg_offset = mmTPC1_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC2 : + db_reg_offset = mmTPC2_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC3 : + db_reg_offset = mmTPC3_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC4 : + db_reg_offset = mmTPC4_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC5 : + db_reg_offset = mmTPC5_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC6 : + db_reg_offset = mmTPC6_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC7 : + db_reg_offset = mmTPC7_QM_PQ_PI; +break ; + +default : + invalid_queue = true; + } + +if (invalid_queue) { +/* Should never get here */ +dev_err (hdev->dev,"h/w queue %d is invalid. Can't set pi\n" , + hw_queue_id); +return ; + } + + db_value = pi; + +if (hdev->ifh) +return ; + +/* ring the doorbell */ +WREG32 (db_reg_offset, db_value); + +if (hw_queue_id ==GOYA_QUEUE_ID_CPU_PQ ) +WREG32 (mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, +GOYA_ASYNC_EVENT_ID_PI_UPDATE ); +} + +void goya_flush_pq_write (struct hl_device *hdev,u64 *pq,u64 exp_val) +{ +/* Not needed in Goya */ +} +void *goya_dma_alloc_coherent (struct hl_device *hdev,size_t size,dma_addr_t *dma_handle,gfp_t flags) {@@ -2514,6 +3482,311 @@ void goya_dma_free_coherent(struct hl_device *hdev, size_t size, void *cpu_addr,dma_free_coherent (&hdev->pdev->dev, size, cpu_addr, dma_handle); } +void *goya_get_int_queue_base (struct hl_device *hdev,u32 queue_id, +dma_addr_t *dma_handle,u16 *queue_len) +{ +void *base; +u32 offset; + + *dma_handle = hdev->asic_prop.sram_base_address; + + base = hdev->pcie_bar[SRAM_CFG_BAR_ID ]; + +switch (queue_id) { +case GOYA_QUEUE_ID_MME : + offset =MME_QMAN_BASE_OFFSET ; + *queue_len =MME_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC0 : + offset =TPC0_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC1 : + offset =TPC1_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC2 : + offset =TPC2_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC3 : + offset =TPC3_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC4 : + offset =TPC4_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC5 : + offset =TPC5_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC6 : + offset =TPC6_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC7 : + offset =TPC7_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +default : +dev_err (hdev->dev,"Got invalid queue id %d\n" , queue_id); +return NULL ; + } + + base += offset; + *dma_handle += offset; + +return base; +} + +int goya_send_cpu_message (struct hl_device *hdev,u32 *msg,u16 len, +u32 timeout,long *result) +{ +struct goya_device *goya = hdev->asic_specific; +struct armcp_packet *pkt; +dma_addr_t pkt_dma_addr; +u32 tmp; +int rc =0 ; + +if (!(goya->hw_cap_initialized &HW_CAP_CPU_Q )) { +if (result) + *result =0 ; +return 0 ; + } + +if (len >CPU_CB_SIZE ) { +dev_err (hdev->dev,"Invalid CPU message size of %d bytes\n" , + len); +return -ENOMEM ; + } + + pkt = hdev->asic_funcs->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" ); +return -ENOMEM ; + } + +memcpy (pkt, msg, len); + +mutex_lock (&hdev->send_cpu_message_lock); + +if (hdev->disabled) + goto out; + + rc =hl_hw_queue_send_cb_no_cmpl (hdev,GOYA_QUEUE_ID_CPU_PQ , len, + pkt_dma_addr); +if (rc) { +dev_err (hdev->dev,"Failed to send CB on CPU PQ (%d)\n" , rc); + goto out; + } + + rc =hl_poll_timeout_memory (hdev, (u64) &pkt->fence, timeout, &tmp); + +hl_hw_queue_inc_ci_kernel (hdev,GOYA_QUEUE_ID_CPU_PQ ); + +if (rc == -ETIMEDOUT ) { +dev_err (hdev->dev, +"Timeout while waiting for CPU packet fence\n" ); + goto out;
drivers/misc/habanalabs/goya/goya.c void goya_dma_free_coherent(struct hl_device *hdev, size_t size, void *cpu_addr, · line 3603
Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10
-0+ } + +if (tmp ==ARMCP_PACKET_FENCE_VAL ) { +if (pkt->rc) { +dev_err (hdev->dev, +"failed to execute CPU packet, rc: %d\n" , + pkt->rc); + rc = -EINVAL ; + }else if (result) { + *result = pkt->result;
For some error cases above the *result is not initialized.
Quoted 1,633 lines
drivers/misc/habanalabs/goya/goya.c +197
-0+ } + }else { +dev_err (hdev->dev,"CPU packet wrong fence value\n" ); + rc = -EINVAL ; + } + +out: +mutex_unlock (&hdev->send_cpu_message_lock); + + hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, len, pkt); + +return rc; +} + +int goya_test_queue (struct hl_device *hdev,u32 hw_queue_id) +{ +struct packet_msg_prot *fence_pkt; +dma_addr_t pkt_dma_addr; +u32 fence_val, tmp; +dma_addr_t fence_dma_addr; +u32 *fence_ptr; +int rc; + + fence_val =GOYA_QMAN0_FENCE_VAL ; + + fence_ptr = hdev->asic_funcs->dma_pool_zalloc (hdev,4 ,GFP_KERNEL , + &fence_dma_addr); +if (!fence_ptr) { +dev_err (hdev->dev, +"Failed to allocate memory for queue testing\n" ); +return -ENOMEM ; + } + + *fence_ptr =0 ; + + fence_pkt = hdev->asic_funcs->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 queue testing\n" ); + rc = -ENOMEM ; + goto free_fence_ptr; + } + + fence_pkt->opcode =PACKET_MSG_PROT ; + fence_pkt->value = fence_val; + fence_pkt->addr = fence_dma_addr + + hdev->asic_prop.host_phys_base_address; + + rc =hl_hw_queue_send_cb_no_cmpl (hdev, hw_queue_id, +sizeof (struct packet_msg_prot ), + pkt_dma_addr); +if (rc) { +dev_err (hdev->dev, +"Failed to send fence packet\n" ); + goto free_pkt; + } + + rc =hl_poll_timeout_memory (hdev, (u64 ) fence_ptr, +GOYA_TEST_QUEUE_WAIT_USEC , &tmp); + +hl_hw_queue_inc_ci_kernel (hdev, hw_queue_id); + +if ((!rc) && (tmp == fence_val)) { +dev_info (hdev->dev, +"queue test on H/W queue %d succeeded\n" , + hw_queue_id); + }else { +dev_err (hdev->dev, +"H/W queue %d test failed (scratch(0x%08llX) == 0x%08X)\n" , + hw_queue_id, fence_dma_addr, tmp); + rc = -EINVAL ; + } + +free_pkt: + hdev->asic_funcs->dma_pool_free (hdev, (void *) fence_pkt, + pkt_dma_addr); +free_fence_ptr: + hdev->asic_funcs->dma_pool_free (hdev, (void *) fence_ptr, + fence_dma_addr); +return rc; +} + +int goya_test_cpu_queue (struct hl_device *hdev) +{ +struct armcp_packet test_pkt; +long result; +int rc; + +/* cpu_queues_enable flag is always checked in send cpu message */ + +memset (&test_pkt,0 ,sizeof (test_pkt)); + + test_pkt.opcode =ARMCP_PACKET_TEST ; + test_pkt.value =ARMCP_PACKET_FENCE_VAL ; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &test_pkt, +sizeof (test_pkt),HL_DEVICE_TIMEOUT_USEC , &result); + +if (!rc) +dev_info (hdev->dev,"queue test on CPU queue succeeded\n" ); +else +dev_err (hdev->dev,"CPU queue test failed (0x%08lX)\n" , result); + +return rc; +} + +static int goya_test_queues (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +int i, rc, ret_val =0 ; + +if (hdev->ifh) +return 0 ; + +for (i =0 ; i <NUMBER_OF_EXT_HW_QUEUES ; i++) { + rc =goya_test_queue (hdev, i); +if (rc) + ret_val = -EINVAL ; + } + +if (hdev->cpu_queues_enable) { + rc = goya->test_cpu_queue (hdev); +if (rc) + ret_val = -EINVAL ; + } + +return ret_val; +} + +void *goya_dma_pool_zalloc (struct hl_device *hdev,size_t size,gfp_t mem_flags, +dma_addr_t *dma_handle) +{ +if (size >GOYA_DMA_POOL_BLK_SIZE ) +return NULL ; + +return dma_pool_zalloc (hdev->dma_pool, mem_flags, dma_handle); +} + +void goya_dma_pool_free (struct hl_device *hdev,void *vaddr, +dma_addr_t dma_addr) +{ +dma_pool_free (hdev->dma_pool, vaddr, dma_addr); +} + +void *goya_cpu_accessible_dma_pool_alloc (struct hl_device *hdev,size_t size, +dma_addr_t *dma_handle) +{ +u64 kernel_addr; + +/* roundup to CPU_PKT_SIZE */ + size = (size + (CPU_PKT_SIZE -1 )) &CPU_PKT_MASK ; + + kernel_addr =gen_pool_alloc (hdev->cpu_accessible_dma_pool, size); + + *dma_handle = hdev->cpu_accessible_dma_address + + (kernel_addr - (u64 ) hdev->cpu_accessible_dma_mem); + +return (void *) kernel_addr; +} + +void goya_cpu_accessible_dma_pool_free (struct hl_device *hdev,size_t size, +void *vaddr) +{ +/* roundup to CPU_PKT_SIZE */ + size = (size + (CPU_PKT_SIZE -1 )) &CPU_PKT_MASK ; + +gen_pool_free (hdev->cpu_accessible_dma_pool, (u64 ) vaddr, size); +} + + +static void goya_hw_queues_lock (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; + +spin_lock (&goya->hw_queues_lock); +} + +static void goya_hw_queues_unlock (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; + +spin_unlock (&goya->hw_queues_lock); +} +static const struct hl_asic_funcs goya_funcs = { .early_init = goya_early_init, .early_fini = goya_early_fini,@@ -2525,8 +3798,19 @@ static const struct hl_asic_funcs goya_funcs = { .resume = goya_resume, .mmap = goya_mmap, .cb_mmap = goya_cb_mmap, + .ring_doorbell = goya_ring_doorbell, + .flush_pq_write = goya_flush_pq_write, .dma_alloc_coherent = goya_dma_alloc_coherent, .dma_free_coherent = goya_dma_free_coherent, + .get_int_queue_base = goya_get_int_queue_base, + .test_queues = goya_test_queues, + .dma_pool_zalloc = goya_dma_pool_zalloc, + .dma_pool_free = goya_dma_pool_free, + .cpu_accessible_dma_pool_alloc = goya_cpu_accessible_dma_pool_alloc, + .cpu_accessible_dma_pool_free = goya_cpu_accessible_dma_pool_free, + .hw_queues_lock = goya_hw_queues_lock, + .hw_queues_unlock = goya_hw_queues_unlock, + .send_cpu_message = goya_send_cpu_message };/** drivers/misc/habanalabs/goya/goyaP.h +6
-0@@ -9,6 +9,7 @@#define GOYAP_H_ #include "habanalabs.h" +#include "include/goya/goya_packets.h" #include "include/goya/goya_boot_if.h" #include "include/goya/goya.h" @@ -117,12 +118,17 @@ enum goya_fw_component { };struct goya_device { +int (*test_cpu_queue)(struct hl_device *hdev); +/* TODO: remove hw_queues_lock after moving to scheduler code */ spinlock_t hw_queues_lock;u64 ddr_bar_cur_addr;u32 hw_cap_initialized; }; +int goya_test_cpu_queue (struct hl_device *hdev); +int goya_send_cpu_message (struct hl_device *hdev,u32 *msg,u16 len, +u32 timeout,long *result);void goya_init_security (struct hl_device *hdev);#endif /* GOYAP_H_ */ drivers/misc/habanalabs/habanalabs.h +175
-1@@ -30,10 +30,36 @@struct hl_device ;struct hl_fpriv ; +/** +* enum hl_queue_type - Supported QUEUE types. +* @QUEUE_TYPE_NA: queue is not available. +* @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the +* host. +* @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's +* memories and/or operates the compute engines. +* @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU. +*/ +enum hl_queue_type { +QUEUE_TYPE_NA , +QUEUE_TYPE_EXT , +QUEUE_TYPE_INT , +QUEUE_TYPE_CPU +}; +/** +* struct hw_queue_properties - queue information. +* @type: queue type. +* @kmd_only: true if only KMD is allowed to send a job to this queue, false +* otherwise. +*/ +struct hw_queue_properties { +enum hl_queue_type type; +u8 kmd_only; +};/** * struct asic_fixed_properties - ASIC specific immutable properties. +* @hw_queues_props: H/W queues properties. * @uboot_ver: F/W U-boot version. * @preboot_ver: F/W Preboot version. * @sram_base_address: SRAM physical start address. @@ -64,6 +90,7 @@ struct hl_fpriv;* @tpc_enabled_mask: which TPCs are enabled. */ struct asic_fixed_properties { +struct hw_queue_properties hw_queues_props[HL_MAX_QUEUES ];char uboot_ver[VERSION_MAX_LEN ];char preboot_ver[VERSION_MAX_LEN ];u64 sram_base_address;@@ -145,7 +172,92 @@ struct hl_cb { +/* +* QUEUES +*/ + +struct hl_cs_job ; + +/* +* Currently, there are two limitations on the maximum length of a queue: +* +* 1. The memory footprint of the queue. The current allocated space for the +* queue is PAGE_SIZE. Because each entry in the queue is HL_BD_SIZE, +* the maximum length of the queue can be PAGE_SIZE / HL_BD_SIZE, +* which currently is 4096/16 = 256 entries. +* +* To increase that, we need either to decrease the size of the +* BD (difficult), or allocate more than a single page (easier). +* +* 2. Because the size of the JOB handle field in the BD CTL / completion queue +* is 10-bit, we can have up to 1024 open jobs per hardware queue. +* Therefore, each queue can hold up to 1024 entries. +* +* HL_QUEUE_LENGTH is in units of struct hl_bd. +* HL_QUEUE_LENGTH * sizeof(struct hl_bd) should be <= HL_PAGE_SIZE +*/ + +#define HL_PAGE_SIZE 4096/* minimum page size */ +/* Must be power of 2 (HL_PAGE_SIZE / HL_BD_SIZE) */ #define HL_QUEUE_LENGTH 256 +#define HL_QUEUE_SIZE_IN_BYTES (HL_QUEUE_LENGTH * HL_BD_SIZE) + +/* +* HL_CQ_LENGTH is in units of struct hl_cq_entry. +* HL_CQ_LENGTH should be <= HL_PAGE_SIZE +*/ +#define HL_CQ_LENGTH HL_QUEUE_LENGTH +#define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE) + + + +/** +* struct hl_hw_queue - describes a H/W transport queue. +* @shadow_queue: pointer to a shadow queue that holds pointers to jobs. +* @queue_type: type of queue. +* @kernel_address: holds the queue's kernel virtual address. +* @bus_address: holds the queue's DMA address. +* @pi: holds the queue's pi value. +* @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci). +* @hw_queue_id: the id of the H/W queue. +* @int_queue_len: length of internal queue (number of entries). +* @valid: is the queue valid (we have array of 32 queues, not all of them +* exists). +*/ +struct hl_hw_queue { +struct hl_cs_job **shadow_queue; +enum hl_queue_type queue_type; +u64 kernel_address; +dma_addr_t bus_address; +u32 pi; +u32 ci; +u32 hw_queue_id; +u16 int_queue_len; +u8 valid; +}; + +/** +* struct hl_cq - describes a completion queue +* @hdev: pointer to the device structure +* @kernel_address: holds the queue's kernel virtual address +* @bus_address: holds the queue's DMA address +* @hw_queue_id: the id of the matching H/W queue +* @ci: ci inside the queue +* @pi: pi inside the queue +* @free_slots_cnt: counter of free slots in queue +*/ +struct hl_cq { +struct hl_device *hdev; +u64 kernel_address; +dma_addr_t bus_address; +u32 hw_queue_id; +u32 ci; +u32 pi; +atomic_t free_slots_cnt; +}; + + +/* @@ -180,8 +292,20 @@ enum hl_asic_type {* @resume: handles IP specific H/W or SW changes for resume. * @mmap: mmap function, does nothing. * @cb_mmap: maps a CB. +* @ring_doorbell: increment PI on a given QMAN. +* @flush_pq_write: flush PQ entry write if necessary, WARN if flushing failed. * @dma_alloc_coherent: DMA allocate coherent memory. * @dma_free_coherent: free DMA allocation. +* @get_int_queue_base: get the internal queue base address. +* @test_queues: run simple test on all queues for sanity check. +* @dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool. +* size of allocation is HL_DMA_POOL_BLK_SIZE. +* @dma_pool_free: free small DMA allocation from pool. +* @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool. +* @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool. +* @hw_queues_lock: acquire H/W queues lock. +* @hw_queues_unlock: release H/W queues lock. +* @send_cpu_message: send buffer to ArmCP. */ struct hl_asic_funcs {int (*early_init)(struct hl_device *hdev);@@ -195,10 +319,27 @@ struct hl_asic_funcs {int (*mmap)(struct hl_fpriv *hpriv,struct vm_area_struct *vma);int (*cb_mmap)(struct hl_device *hdev,struct vm_area_struct *vma,u64 kaddress,phys_addr_t paddress,u32 size); +void (*ring_doorbell)(struct hl_device *hdev,u32 hw_queue_id,u32 pi); +void (*flush_pq_write)(struct hl_device *hdev,u64 *pq,u64 exp_val);void * (*dma_alloc_coherent)(struct hl_device *hdev,size_t size,dma_addr_t *dma_handle,gfp_t flag);void (*dma_free_coherent)(struct hl_device *hdev,size_t size,void *cpu_addr,dma_addr_t dma_handle); +void * (*get_int_queue_base)(struct hl_device *hdev,u32 queue_id, +dma_addr_t *dma_handle,u16 *queue_len); +int (*test_queues)(struct hl_device *hdev); +void * (*dma_pool_zalloc)(struct hl_device *hdev,size_t size, +gfp_t mem_flags,dma_addr_t *dma_handle); +void (*dma_pool_free)(struct hl_device *hdev,void *vaddr, +dma_addr_t dma_addr); +void * (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev, +size_t size,dma_addr_t *dma_handle); +void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev, +size_t size,void *vaddr); +void (*hw_queues_lock)(struct hl_device *hdev); +void (*hw_queues_unlock)(struct hl_device *hdev); +int (*send_cpu_message)(struct hl_device *hdev,u32 *msg, +u16 len,u32 timeout,long *result); };@@ -240,6 +381,17 @@ struct hl_ctx_mgr { +/** +* struct hl_cs_job - command submission job. +* @finish_work: workqueue object to run when job is completed. +* @id: the id of this job inside a CS. +*/ +struct hl_cs_job { +struct work_struct finish_work; +u32 id; +}; + +/* * FILE PRIVATE STRUCTURE */ @@ -316,7 +468,11 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @dev: realted kernel basic device structure. * @asic_name: ASIC specific nmae. * @asic_type: ASIC specific type. +* @completion_queue: array of hl_cq. +* @cq_wq: work queue of completion queues for executing work in process context +* @eq_wq: work queue of event queue for executing work in process context. * @kernel_ctx: KMD context structure. +* @kernel_queues: array of hl_hw_queue. * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs. * @dma_pool: DMA pool for small allocations. * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address. @@ -326,6 +482,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @asid_bitmap: holds used/available ASIDs. * @asid_mutex: protects asid_bitmap. * @device_open: lock for sanity checks upon FD open. +* @send_cpu_message_lock: enforces only one message in KMD <-> ArmCP queue. * @asic_prop: ASIC specific immutable properties. * @asic_funcs: ASIC specific functions. * @asic_specific: ASIC specific information to use only from ASIC files. @@ -345,7 +502,10 @@ struct hl_device {struct device *dev;char asic_name[16 ];enum hl_asic_type asic_type; +struct hl_cq *completion_queue; +struct workqueue_struct *cq_wq;struct hl_ctx *kernel_ctx; +struct hl_hw_queue *kernel_queues;struct hl_cb_mgr kernel_cb_mgr;struct dma_pool *dma_pool;void *cpu_accessible_dma_mem;@@ -356,6 +516,7 @@ struct hl_device {struct mutex asid_mutex;/* TODO: change to rw_sem for multiple contexts (same as other IOCTL) */ struct mutex device_open; +struct mutex send_cpu_message_lock;struct asic_fixed_properties asic_prop;const struct hl_asic_funcs *asic_funcs;void *asic_specific;@@ -374,7 +535,9 @@ struct hl_device {u8 cpu_enable;u8 reset_pcilink;u8 config_pll; +u8 cpu_queues_enable;u8 fw_loading; +u8 ifh;u8 pldm; };@@ -418,7 +581,18 @@ int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr, u32 timeout_us, u32 *val);int hl_poll_timeout_device_memory (struct hl_device *hdev,void __iomem *addr,u32 timeout_us,u32 *val); - +int hl_hw_queues_create (struct hl_device *hdev); +void hl_hw_queues_destroy (struct hl_device *hdev); +int hl_hw_queue_send_cb_no_cmpl (struct hl_device *hdev,u32 hw_queue_id, +u32 cb_size,u64 cb_ptr); +u32 hl_hw_queue_add_ptr (u32 ptr,u16 val); +void hl_hw_queue_inc_ci_kernel (struct hl_device *hdev,u32 hw_queue_id); + +#define hl_queue_inc_ptr (p) hl_hw_queue_add_ptr(p, 1) +#define hl_pi_2_offset (pi) ((pi) & (HL_QUEUE_LENGTH - 1)) + +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);int hl_asid_init (struct hl_device *hdev);void hl_asid_fini (struct hl_device *hdev);unsigned long hl_asid_alloc (struct hl_device *hdev);drivers/misc/habanalabs/habanalabs_drv.c +6
-0@@ -184,13 +184,19 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev, hdev->cpu_enable =1 ; hdev->reset_pcilink =0 ; hdev->config_pll =0 ; + hdev->cpu_queues_enable =1 ; hdev->fw_loading =1 ; + hdev->ifh =0 ; hdev->pldm =0 ;/* If CPU is disabled, no point in loading FW */ if (!hdev->cpu_enable) hdev->fw_loading =0 ; +/* If we don't load FW, no need to initialize CPU queues */ +if (!hdev->fw_loading) + hdev->cpu_queues_enable =0 ; + hdev->disabled = true; hdev->pdev = pdev;/* can be NULL in case of simulator device */ drivers/misc/habanalabs/hw_queue.c new file +404
-0@@ -0,0 +1,404 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include "habanalabs.h" + +#include <linux/dma-mapping.h> +#include <linux/sched.h> +#include <linux/wait.h> +#include <linux/delay.h> + +/** +* hl_queue_add_ptr - add to pi or ci and checks if it wraps around +* +* @ptr: the current pi/ci value +* @val: the amount to add +* +* Add val to ptr. It can go until twice the queue length. +*/ +inline u32 hl_hw_queue_add_ptr (u32 ptr,u16 val) +{ + ptr += val; + ptr &= ((HL_QUEUE_LENGTH <<1 ) -1 ); +return ptr; +} + +static inline int queue_free_slots (struct hl_hw_queue *q,u32 queue_len) +{ +int delta = (q->pi - q->ci); + +if (delta >=0 ) +return (queue_len - delta); +else +return (abs (delta) - queue_len); +} + +/** +* ext_queue_submit_bd - Submit a buffer descriptor to an external queue +* +* @hdev: pointer to habanalabs device structure +* @q: pointer to habanalabs queue structure +* @ctl: BD's control word +* @len: BD's length +* @ptr: BD's pointer +* +* This function assumes there is enough space on the queue to submit a new +* BD to it. It initializes the next BD and calls the device specific +* function to set the pi (and doorbell) +* +* This function must be called when the scheduler mutex is taken +* +*/ +static void ext_queue_submit_bd (struct hl_device *hdev,struct hl_hw_queue *q, +u32 ctl,u32 len,u64 ptr) +{ +struct hl_bd *bd; + + bd = (struct hl_bd *) q->kernel_address; + bd +=hl_pi_2_offset (q->pi); + bd->ctl = ctl; + bd->len = len; + bd->ptr = ptr + hdev->asic_prop.host_phys_base_address; + + q->pi =hl_queue_inc_ptr (q->pi); + hdev->asic_funcs->ring_doorbell (hdev, q->hw_queue_id, q->pi); +} + +/** +* ext_queue_sanity_checks - perform some sanity checks on external queue +* +* @hdev : pointer to hl_device structure +* @q : pointer to hl_hw_queue structure +* @num_of_entries : how many entries to check for space +* @reserve_cq_entry : whether to reserve an entry in the cq +* +* H/W queues spinlock should be taken before calling this function +* +* Perform the following: +* - Make sure we have enough space in the h/w queue +* - Make sure we have enough space in the completion queue +* - Reserve space in the completion queue (needs to be reversed if there +* is a failure down the road before the actual submission of work). Only +* do this action if reserve_cq_entry is true +* +*/ +static int ext_queue_sanity_checks (struct hl_device *hdev, +struct hl_hw_queue *q,int num_of_entries, +bool reserve_cq_entry) +{ +atomic_t *free_slots = + &hdev->completion_queue[q->hw_queue_id].free_slots_cnt; +int free_slots_cnt; + +/* Check we have enough space in the queue */ + free_slots_cnt =queue_free_slots (q,HL_QUEUE_LENGTH ); + +if (free_slots_cnt < num_of_entries) { +dev_dbg (hdev->dev,"Queue %d doesn't have room for %d CBs\n" , + q->hw_queue_id, num_of_entries); +return -EAGAIN ; + } + +if (reserve_cq_entry) { +/* +* Check we have enough space in the completion queue +* Add -1 to counter (decrement) unless counter was already 0 +* In that case, CQ is full so we can't submit a new CB because +* we won't get ack on its completion +* atomic_add_unless will return 0 if counter was already 0 +*/ +if (atomic_add_negative (num_of_entries *-1 , free_slots)) { +dev_dbg (hdev->dev,"No space for %d on CQ %d\n" , + num_of_entries, q->hw_queue_id); +atomic_add (num_of_entries, free_slots); +return -EAGAIN ; + } + } + +return 0 ; +} + +/** +* hl_hw_queue_send_cb_no_cmpl - send a single CB (not a JOB) without completion +* +* @hdev: pointer to hl_device structure +* @hw_queue_id: Queue's type +* @cb_size: size of CB +* @cb_ptr: pointer to CB location +* +* This function sends a single CB, that must NOT generate a completion entry +* +*/ +int hl_hw_queue_send_cb_no_cmpl (struct hl_device *hdev,u32 hw_queue_id, +u32 cb_size,u64 cb_ptr) +{ +struct hl_hw_queue *q = &hdev->kernel_queues[hw_queue_id]; +int rc; + +/* +* The CPU queue is a synchronous queue with an effective depth of +* a single entry (although it is allocated with room for multiple +* entries). Therefore, there is a different lock, called +* send_cpu_message_lock, that serializes accesses to the CPU queue. +* As a result, we don't need to lock the access to the entire H/W +* queues module when submitting a JOB to the CPU queue +*/ +if (q->queue_type !=QUEUE_TYPE_CPU ) + hdev->asic_funcs->hw_queues_lock (hdev); + +if (hdev->disabled) { + rc = -EPERM ; + goto out; + } + + rc =ext_queue_sanity_checks (hdev, q,1 , false); +if (rc) + goto out; + +ext_queue_submit_bd (hdev, q,0 , cb_size, cb_ptr); + +out: +if (q->queue_type !=QUEUE_TYPE_CPU ) + hdev->asic_funcs->hw_queues_unlock (hdev); + +return rc; +} + +/** +* hl_hw_queue_inc_ci_kernel - increment ci for kernel's queue +* +* @hdev: pointer to hl_device structure +* @hw_queue_id: which queue to increment its ci +*/ +void hl_hw_queue_inc_ci_kernel (struct hl_device *hdev,u32 hw_queue_id) +{ +struct hl_hw_queue *q = &hdev->kernel_queues[hw_queue_id]; + + q->ci =hl_queue_inc_ptr (q->ci); +} + +static int ext_and_cpu_hw_queue_init (struct hl_device *hdev, +struct hl_hw_queue *q) +{ +void *p; +int rc; + + p = hdev->asic_funcs->dma_alloc_coherent (hdev, +HL_QUEUE_SIZE_IN_BYTES , + &q->bus_address,GFP_KERNEL | __GFP_ZERO); +if (!p) +return -ENOMEM ; + + q->kernel_address = (u64 ) p; + + q->shadow_queue =kmalloc_array (HL_QUEUE_LENGTH , +sizeof (*q->shadow_queue), +GFP_KERNEL ); +if (!q->shadow_queue) { +dev_err (hdev->dev, +"Failed to allocate shadow queue for H/W queue %d\n" , + q->hw_queue_id); + rc = -ENOMEM ; + goto free_queue; + } + +/* Make sure read/write pointers are initialized to start of queue */ + q->ci =0 ; + q->pi =0 ; + +return 0 ; + +free_queue: + hdev->asic_funcs->dma_free_coherent (hdev,HL_QUEUE_SIZE_IN_BYTES , + (void *) q->kernel_address, q->bus_address); + +return rc; +} + +static int int_hw_queue_init (struct hl_device *hdev,struct hl_hw_queue *q) +{ +void *p; + + p = hdev->asic_funcs->get_int_queue_base (hdev, q->hw_queue_id, + &q->bus_address, &q->int_queue_len); +if (!p) { +dev_err (hdev->dev, +"Failed to get base address for internal queue %d\n" , + q->hw_queue_id); +return -EFAULT ; + } + + q->kernel_address = (u64 ) p; + q->pi =0 ; + q->ci =0 ; + +return 0 ; +} + +static int cpu_hw_queue_init (struct hl_device *hdev,struct hl_hw_queue *q) +{ +return ext_and_cpu_hw_queue_init (hdev, q); +} + +static int ext_hw_queue_init (struct hl_device *hdev,struct hl_hw_queue *q) +{ +return ext_and_cpu_hw_queue_init (hdev, q); +} + +/** +* hw_queue_init - main initialization function for H/W queue object +* +* @hdev: pointer to hl_device device structure +* @q: pointer to hl_hw_queue queue structure +* @hw_queue_id: The id of the H/W queue +* +* Allocate dma-able memory for the queue and initialize fields +* Returns 0 on success +*/ +static int hw_queue_init (struct hl_device *hdev,struct hl_hw_queue *q, +u32 hw_queue_id) +{ +int rc; + +BUILD_BUG_ON (HL_QUEUE_SIZE_IN_BYTES >HL_PAGE_SIZE ); + + q->hw_queue_id = hw_queue_id; + +switch (q->queue_type) { +case QUEUE_TYPE_EXT : + rc =ext_hw_queue_init (hdev, q); +break ; + +case QUEUE_TYPE_INT : + rc =int_hw_queue_init (hdev, q); +break ; + +case QUEUE_TYPE_CPU : + rc =cpu_hw_queue_init (hdev, q); +break ; + +case QUEUE_TYPE_NA : + q->valid =0 ; +return 0 ; + +default : +dev_crit (hdev->dev,"wrong queue type %d during init\n" , + q->queue_type); + rc = -EINVAL ; +break ; + } + +if (rc) +return rc; + + q->valid =1 ; + +return 0 ; +} + +/** +* hw_queue_fini - destroy queue +* +* @hdev: pointer to hl_device device structure +* @q: pointer to hl_hw_queue queue structure +* +* Free the queue memory +*/ +static void hw_queue_fini (struct hl_device *hdev,struct hl_hw_queue *q) +{ +if (!q->valid) +return ; + +/* +* If we arrived here, there are no jobs waiting on this queue +* so we can safely remove it. +* This is because this function can only called when: +* 1. Either a context is deleted, which only can occur if all its +* jobs were finished +* 2. A context wasn't able to be created due to failure or timeout, +* which means there are no jobs on the queue yet +* +* The only exception are the queues of the kernel context, but +* if they are being destroyed, it means that the entire module is +* being removed. If the module is removed, it means there is no open +* user context. It also means that if a job was submitted by +* the kernel driver (e.g. context creation), the job itself was +* released by the kernel driver when a timeout occurred on its +* Completion. Thus, we don't need to release it again. +*/ + +if (q->queue_type ==QUEUE_TYPE_INT ) +return ; + +kfree (q->shadow_queue); + + hdev->asic_funcs->dma_free_coherent (hdev, +HL_QUEUE_SIZE_IN_BYTES , + (void *) q->kernel_address, q->bus_address); +} + +int hl_hw_queues_create (struct hl_device *hdev) +{ +struct asic_fixed_properties *asic = &hdev->asic_prop; +struct hl_hw_queue *q; +int i, rc, q_ready_cnt; + + hdev->kernel_queues =kcalloc (HL_MAX_QUEUES , +sizeof (*hdev->kernel_queues),GFP_KERNEL ); + +if (!hdev->kernel_queues) { +dev_err (hdev->dev,"Not enough memory for H/W queues\n" ); +return -ENOMEM ; + } + +/* Initialize the H/W queues */ +for (i =0 , q_ready_cnt =0 , q = hdev->kernel_queues; + i <HL_MAX_QUEUES ; i++, q_ready_cnt++, q++) { + + q->queue_type = asic->hw_queues_props[i].type; + rc =hw_queue_init (hdev, q, i); +if (rc) { +dev_err (hdev->dev, +"failed to initialize queue %d\n" , i); + goto release_queues; + } + } + +return 0 ; + +release_queues: +for (i =0 , q = hdev->kernel_queues ; i < q_ready_cnt ; i++, q++) +hw_queue_fini (hdev, q); + +kfree (hdev->kernel_queues); + +return rc; +} + +void hl_hw_queues_destroy (struct hl_device *hdev) +{ +struct hl_hw_queue *q; +int i; + +for (i =0 , q = hdev->kernel_queues ; i <HL_MAX_QUEUES ; i++, q++) +hw_queue_fini (hdev, q); + +kfree (hdev->kernel_queues); +} + +void hl_hw_queue_reset (struct hl_device *hdev,bool hard_reset) +{ +struct hl_hw_queue *q; +int i; + +for (i =0 , q = hdev->kernel_queues ; i <HL_MAX_QUEUES ; i++, q++) { +if ((!q->valid) || + ((!hard_reset) && (q->queue_type ==QUEUE_TYPE_CPU ))) +continue ; + q->pi = q->ci =0 ; + } +}drivers/misc/habanalabs/include/goya/goya_packets.h new file +234
-0@@ -0,0 +1,234 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* Copyright 2017-2018 HabanaLabs, Ltd. +* All Rights Reserved. +* +* Authors: +* +* Oded Gabbay <[email protected]> +* Guy Eilat <[email protected]> +* +*/ + +#ifndef GOYA_PACKETS_H +#define GOYA_PACKETS_H + +#include <linux/types.h> + +#define PACKET_HEADER_PACKET_ID_SHIFT 56 +#define PACKET_HEADER_PACKET_ID_MASK 0x1F00000000000000ull + +enum packet_id { +PACKET_WREG_32 =0x1 , +PACKET_WREG_BULK =0x2 , +PACKET_MSG_LONG =0x3 , +PACKET_MSG_SHORT =0x4 , +PACKET_CP_DMA =0x5 , +PACKET_MSG_PROT =0x7 , +PACKET_FENCE =0x8 , +PACKET_LIN_DMA =0x9 , +PACKET_NOP =0xA , +PACKET_STOP =0xB , +MAX_PACKET_ID = (PACKET_HEADER_PACKET_ID_MASK >> +PACKET_HEADER_PACKET_ID_SHIFT ) +1 +}; + +enum goya_dma_direction { +DMA_HOST_TO_DRAM , +DMA_HOST_TO_SRAM , +DMA_DRAM_TO_SRAM , +DMA_SRAM_TO_DRAM , +DMA_SRAM_TO_HOST , +DMA_DRAM_TO_HOST , +DMA_DRAM_TO_DRAM , +DMA_SRAM_TO_SRAM , +DMA_ENUM_MAX +}; + +struct packet_nop { +__u32 reserved; +union { +struct { + __u32:24; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +}; + +struct packet_stop { +__u32 reserved; +union { +struct { + __u32:24; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 0 */ +__u32 msg_barrier :1 ;/* must be 0 */ + }; +__u32 ctl; + }; +}; + +struct packet_wreg32 { +__u32 value; +union { +struct { +__u32 reg_offset :16 ; + __u32:7; +__u32 local :1 ;/* 0: write to TCL regs, +* 1: write to CMDQ regs +*/ +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 1 */ +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +}; + +struct packet_wreg_bulk { +__u32 size64 :16 ; + __u32:16; +__u32 reg_offset :16 ; + __u32:8; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 1 */ +__u32 msg_barrier :1 ; +__u64 values[0 ];/* data starts here */ +}; + +struct packet_msg_long { +__u32 value; +union { +struct { + __u32:16; +__u32 weakly_ordered :1 ; +__u32 no_snoop :1 ; + __u32:2; +__u32 op :2 ;/* 0: write <value>. 1: write timestamp. */ + __u32:2; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +__u64 addr; +}; + +struct packet_msg_short { +union { +struct { +__u32 sync_id :10 ; + __u32:5; +__u32 mode :1 ; +__u32 sync_value :16 ; + } mon_arm_register; +struct { +__u32 sync_value :16 ; + __u32:15; +__u32 mode :1 ; + } so_upd; +__u32 value; + }; +union { +struct { +__u32 msg_addr_offset :16 ; +__u32 weakly_ordered :1 ; +__u32 no_snoop :1 ; + __u32:2; +__u32 op :2 ; +__u32 base :2 ; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +}; + +struct packet_msg_prot { +__u32 value; +union { +struct { + __u32:16; +__u32 weakly_ordered :1 ; +__u32 no_snoop :1 ; + __u32:2; +__u32 op :2 ;/* 0: write <value>. 1: write timestamp. */ + __u32:2; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +__u64 addr; +}; + +struct packet_fence { +__u32 dec_val :4 ; + __u32:12; +__u32 gate_val :8 ; + __u32:6; +__u32 id :2 ; + __u32:24; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; +}; + +struct packet_lin_dma { +__u32 tsize; +union { +struct { +__u32 weakly_ordered :1 ;/* H/W bug, must be 1 */ +__u32 rdcomp :1 ; +__u32 wrcomp :1 ; +__u32 no_snoop :1 ; +__u32 src_disable :1 ; +__u32 dst_disable :1 ; +__u32 memset_mode :1 ; +__u32 tensor_dma :1 ;/* N/A, must be 0 */ +__u32 cntrl :12 ; +__u32 dma_dir :3 ;/* S/W only, no effect on HW */ + __u32:1; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 1 */ +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +__u64 src_addr; +__u64 dst_addr; +}; + +struct packet_cp_dma { +__u32 tsize; +union { +struct { +__u32 weakly_ordered :1 ; +__u32 no_snoop :1 ; + __u32:22; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 1 */ +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +__u64 src_addr; +}; + +#endif /* GOYA_PACKETS_H */ drivers/misc/habanalabs/include/habanalabs_device_if.h +272
-0@@ -97,6 +97,278 @@ enum pq_init_status {PQ_INIT_STATUS_READY_FOR_HOST }; +/* +* ArmCP Primary Queue Packets +* +* During normal operation, KMD needs to send various messages to ArmCP, +* usually either to SET some value into a H/W periphery or to GET the current +* value of some H/W periphery. For example, SET the frequency of MME/TPC and +* GET the value of the thermal sensor. +* +* These messages can be initiated either by the User application or by KMD +* itself, e.g. power management code. In either case, the communication from +* KMD to ArmCP will *always* be in synchronous mode, meaning that KMD will +* send a single message and poll until the message was acknowledged and the +* results are ready (if results are needed). +* +* This means that only a single message can be sent at a time and KMD must +* wait for its result before sending the next message. Having said that, +* because these are control messages which are sent in a relatively low +* frequency, this limitation seems acceptable. It's important to note that +* in case of multiple devices, messages to different devices *can* be sent +* at the same time. +* +* The message, inputs/outputs (if relevant) and fence object will be located +* on the device DDR at an address that will be determined by KMD. During +* device initialization phase, KMD will pass to ArmCP that address. Most of +* the message types will contain inputs/outputs inside the message itself. +* The common part of each message will contain the opcode of the message (its +* type) and a field representing a fence object. +* +* When KMD wishes to send a message to ArmCP, it will write the message +* contents to the device DDR, clear the fence object and then write the +* value 484 to the mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR register to issue +* the 484 interrupt-id to the ARM core. +* +* Upon receiving the 484 interrupt-id, ArmCP will read the message from the +* DDR. In case the message is a SET operation, ArmCP will first perform the +* operation and then write to the fence object on the device DDR. In case the +* message is a GET operation, ArmCP will first fill the results section on the +* device DDR and then write to the fence object. If an error occurred, ArmCP +* will fill the rc field with the right error code. +* +* In the meantime, KMD will poll on the fence object. Once KMD sees that the +* fence object is signaled, it will read the results from the device DDR +* (if relevant) and resume the code execution in KMD. +* +* To use QMAN packets, the opcode must be the QMAN opcode, shifted by 8 +* so the value being put by the KMD matches the value read by ArmCP +* +* Non-QMAN packets should be limited to values 1 through (2^8 - 1) +* +* Detailed description: +* +* ARMCP_PACKET_DISABLE_PCI_ACCESS - +* After receiving this packet the embedded CPU must NOT issue PCI +* transactions (read/write) towards the Host CPU. This also include +* sending MSI-X interrupts. +* This packet is usually sent before the device is moved to D3Hot state. +* +* ARMCP_PACKET_ENABLE_PCI_ACCESS - +* After receiving this packet the embedded CPU is allowed to issue PCI +* transactions towards the Host CPU, including sending MSI-X interrupts. +* This packet is usually send after the device is moved to D0 state. +* +* ARMCP_PACKET_TEMPERATURE_GET - +* Fetch the current temperature / Max / Max Hyst / Critical / +* Critical Hyst of a specified thermal sensor. The packet's +* arguments specify the desired sensor and the field to get. +* +* ARMCP_PACKET_VOLTAGE_GET - +* Fetch the voltage / Max / Min of a specified sensor. The packet's +* arguments specify the sensor and type. +* +* ARMCP_PACKET_CURRENT_GET - +* Fetch the current / Max / Min of a specified sensor. The packet's +* arguments specify the sensor and type. +* +* ARMCP_PACKET_FAN_SPEED_GET - +* Fetch the speed / Max / Min of a specified fan. The packet's +* arguments specify the sensor and type. +* +* ARMCP_PACKET_PWM_GET - +* Fetch the pwm value / mode of a specified pwm. The packet's +* arguments specify the sensor and type. +* +* ARMCP_PACKET_PWM_SET - +* Set the pwm value / mode of a specified pwm. The packet's +* arguments specify the sensor, type and value. +* +* ARMCP_PACKET_FREQUENCY_SET - +* Set the frequency of a specified PLL. The packet's arguments specify +* the PLL and the desired frequency. The actual frequency in the device +* might differ from the requested frequency. +* +* ARMCP_PACKET_FREQUENCY_GET - +* Fetch the frequency of a specified PLL. The packet's arguments specify +* the PLL. +* +* ARMCP_PACKET_LED_SET - +* Set the state of a specified led. The packet's arguments +* specify the led and the desired state. +* +* ARMCP_PACKET_I2C_WR - +* Write 32-bit value to I2C device. The packet's arguments specify the +* I2C bus, address and value. +* +* ARMCP_PACKET_I2C_RD - +* Read 32-bit value from I2C device. The packet's arguments specify the +* I2C bus and address. +* +* ARMCP_PACKET_INFO_GET - +* Fetch information from the device as specified in the packet's +* structure. KMD passes the max size it allows the ArmCP to write to +* the structure, to prevent data corruption in case of mismatched +* KMD/FW versions. +* +* ARMCP_PACKET_FLASH_PROGRAM_REMOVED - this packet was removed +* +* ARMCP_PACKET_UNMASK_RAZWI_IRQ - +* Unmask the given IRQ. The IRQ number is specified in the value field. +* The packet is sent after receiving an interrupt and printing its +* relevant information. +* +* ARMCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY - +* Unmask the given IRQs. The IRQs numbers are specified in an array right +* after the armcp_packet structure, where its first element is the array +* length. The packet is sent after a soft reset was done in order to +* handle any interrupts that were sent during the reset process. +* +* ARMCP_PACKET_TEST - +* Test packet for ArmCP connectivity. The CPU will put the fence value +* in the result field. +* +* ARMCP_PACKET_FREQUENCY_CURR_GET - +* Fetch the current frequency of a specified PLL. The packet's arguments +* specify the PLL. +* +* ARMCP_PACKET_MAX_POWER_GET - +* Fetch the maximal power of the device. +* +* ARMCP_PACKET_MAX_POWER_SET - +* Set the maximal power of the device. The packet's arguments specify +* the power. +* +* ARMCP_PACKET_EEPROM_DATA_GET - +* Get EEPROM data from the ArmCP kernel. The buffer is specified in the +* addr field. The CPU will put the returned data size in the result +* field. In addition, KMD passes the max size it allows the ArmCP to +* write to the structure, to prevent data corruption in case of +* mismatched KMD/FW versions. +* +*/ + +enum armcp_packet_id { +ARMCP_PACKET_DISABLE_PCI_ACCESS =1 ,/* internal */ +ARMCP_PACKET_ENABLE_PCI_ACCESS ,/* internal */ +ARMCP_PACKET_TEMPERATURE_GET ,/* sysfs */ +ARMCP_PACKET_VOLTAGE_GET ,/* sysfs */ +ARMCP_PACKET_CURRENT_GET ,/* sysfs */ +ARMCP_PACKET_FAN_SPEED_GET ,/* sysfs */ +ARMCP_PACKET_PWM_GET ,/* sysfs */ +ARMCP_PACKET_PWM_SET ,/* sysfs */ +ARMCP_PACKET_FREQUENCY_SET ,/* sysfs */ +ARMCP_PACKET_FREQUENCY_GET ,/* sysfs */ +ARMCP_PACKET_LED_SET ,/* debugfs */ +ARMCP_PACKET_I2C_WR ,/* debugfs */ +ARMCP_PACKET_I2C_RD ,/* debugfs */ +ARMCP_PACKET_INFO_GET ,/* IOCTL */ +ARMCP_PACKET_FLASH_PROGRAM_REMOVED , +ARMCP_PACKET_UNMASK_RAZWI_IRQ ,/* internal */ +ARMCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY ,/* internal */ +ARMCP_PACKET_TEST ,/* internal */ +ARMCP_PACKET_FREQUENCY_CURR_GET ,/* sysfs */ +ARMCP_PACKET_MAX_POWER_GET ,/* sysfs */ +ARMCP_PACKET_MAX_POWER_SET ,/* sysfs */ +ARMCP_PACKET_EEPROM_DATA_GET ,/* sysfs */ +}; + +#define ARMCP_PACKET_FENCE_VAL 0xFE8CE7A5 + +struct armcp_packet { +union { +__u64 value;/* For SET packets */ +__u64 result;/* For GET packets */ +__u64 addr;/* For PQ */ + }; + +union { +struct { + __u32:12; +__u32 rc :4 ; +__u32 opcode :13 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; + +__u32 fence;/* Signal to KMD that message is completed */ + +union { +struct {/* For temperature/current/voltage/fan/pwm get/set */ +__u16 sensor_index; +__u16 type; + }; + +struct {/* For I2C read/write */ +__u8 i2c_bus; +__u8 i2c_addr; +__u8 i2c_reg; +__u8 pad;/* unused */ + }; + +/* For frequency get/set */ +__u32 pll_index; + +/* For led set */ +__u32 led_index; + +/* For get Armcp info/EEPROM data */ +__u32 data_max_size; + }; +}; + +struct armcp_unmask_irq_arr_packet { +struct armcp_packet armcp_pkt; +__u32 length; +__u32 irqs[0 ]; +}; + +enum armcp_packet_rc { + armcp_packet_success, + armcp_packet_invalid, + armcp_packet_fault +}; + +enum armcp_temp_type { + armcp_temp_input, + armcp_temp_max =6 , + armcp_temp_max_hyst, + armcp_temp_crit, + armcp_temp_crit_hyst +}; + +enum armcp_in_attributes { + armcp_in_input, + armcp_in_min, + armcp_in_max +}; + +enum armcp_curr_attributes { + armcp_curr_input, + armcp_curr_min, + armcp_curr_max +}; + +enum armcp_fan_attributes { + armcp_fan_input, + armcp_fan_min =2 , + armcp_fan_max +}; + +enum armcp_pwm_attributes { + armcp_pwm_input, + armcp_pwm_enable +}; + +/* Event Queue Packets */ + +struct eq_generic_event { +__u64 data[7 ]; +}; +/* * ArmCP info */ drivers/misc/habanalabs/irq.c new file +150
-0@@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours, Mike.
Part of the diff is not shown here. View the raw message.
Re: [PATCH 07/15] habanalabs: add h/w queues module
Quoted 156 lines · Mike Rapoport
Quoted 154 lines · Oded Gabbay
This patch adds the H/W queues module and the code to initialize Goya's various compute and DMA engines and their queues.
Goya has 5 DMA channels, 8 TPC engines and a single MME engine. For each channel/engine, there is a H/W queue logic which is used to pass commands from the user to the H/W. That logic is called QMAN.
There are two types of QMANs: external and internal. The DMA QMANs are considered external while the TPC and MME QMANs are considered internal. For each external queue there is a completion queue, which is located on the Host memory.
The differences between external and internal QMANs are:
1. The location of the queue's memory. External QMANs are located on the Host memory while internal QMANs are located on the on-chip memory.
2. The external QMAN write an entry to a completion queue and sends an MSI-X interrupt upon completion of a command buffer that was given to it. The internal QMAN doesn't do that.
Signed-off-by Oded Gabbay <[email protected]>--- drivers/misc/habanalabs/Makefile | 2 +- drivers/misc/habanalabs/device.c | 74 +- drivers/misc/habanalabs/goya/goya.c | 1518 +++++++++++++++-- drivers/misc/habanalabs/goya/goyaP.h | 6 + drivers/misc/habanalabs/habanalabs.h | 176 +- drivers/misc/habanalabs/habanalabs_drv.c | 6 + drivers/misc/habanalabs/hw_queue.c | 404 +++++ .../habanalabs/include/goya/goya_packets.h | 234 +++ .../habanalabs/include/habanalabs_device_if.h | 272 +++ drivers/misc/habanalabs/irq.c | 150 ++ 10 files changed, 2721 insertions(+), 121 deletions(-) create mode 100644 drivers/misc/habanalabs/hw_queue.c create mode 100644 drivers/misc/habanalabs/include/goya/goya_packets.h create mode 100644 drivers/misc/habanalabs/irq.cdrivers/misc/habanalabs/Makefile +1
-1@@ -5,7 +5,7 @@ obj-m := habanalabs.o habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \ - command_buffer.o + command_buffer.o hw_queue.o irq.o include $(src)/goya/Makefile habanalabs-y += $(HL_GOYA_FILES)drivers/misc/habanalabs/device.c +51
-2@@ -170,13 +170,22 @@ static int device_early_init(struct hl_device *hdev)if (rc) goto early_fini; + hdev->cq_wq =alloc_workqueue ("hl-free-jobs" ,WQ_UNBOUND ,0 ); +if (hdev->cq_wq ==NULL ) { +dev_err (hdev->dev,"Failed to allocate CQ workqueue\n" ); + goto asid_fini; + } +hl_cb_mgr_init (&hdev->kernel_cb_mgr);mutex_init (&hdev->device_open); +mutex_init (&hdev->send_cpu_message_lock);atomic_set (&hdev->fd_open_cnt,0 );return 0 ; +asid_fini: +hl_asid_fini (hdev); early_fini:if (hdev->asic_funcs->early_fini) hdev->asic_funcs->early_fini (hdev);@@ -192,9 +201,12 @@ static int device_early_init(struct hl_device *hdev)*/ static void device_early_fini (struct hl_device *hdev) { +mutex_destroy (&hdev->send_cpu_message_lock);hl_cb_mgr_fini (hdev, &hdev->kernel_cb_mgr); +destroy_workqueue (hdev->cq_wq); +hl_asid_fini (hdev);if (hdev->asic_funcs->early_fini)@@ -273,7 +285,7 @@ int hl_device_resume(struct hl_device *hdev)*/ int hl_device_init (struct hl_device *hdev,struct class *hclass) { -int rc; +int i, rc, cq_ready_cnt;/* Create device */ rc =device_setup_cdev (hdev, hclass, hdev->id, &hl_ops);@@ -294,11 +306,48 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)if (rc) goto early_fini; +/* +* 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; + } + +/* +* 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 +*/ + hdev->completion_queue = +kcalloc (hdev->asic_prop.completion_queues_count, +sizeof (*hdev->completion_queue),GFP_KERNEL ); + +if (!hdev->completion_queue) { +dev_err (hdev->dev,"failed to allocate completion queues\n" ); + rc = -ENOMEM ; + goto hw_queues_destroy; + } + +for (i =0 , cq_ready_cnt =0 ; + i < hdev->asic_prop.completion_queues_count; + i++, cq_ready_cnt++) { + rc =hl_cq_init (hdev, &hdev->completion_queue[i], i); +if (rc) { +dev_err (hdev->dev, +"failed to initialize completion queue\n" ); + goto cq_fini; + } + } +/* Allocate the kernel context */ hdev->kernel_ctx =kzalloc (sizeof (*hdev->kernel_ctx),GFP_KERNEL );if (!hdev->kernel_ctx) { rc = -ENOMEM ; - goto sw_fini; + goto cq_fini; } hdev->user_ctx =NULL ;@@ -324,6 +373,14 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) hdev->disabled = false;
Quoted 8 lines
drivers/misc/habanalabs/device.c int hl_device_init(struct hl_device *hdev, struct class *hclass) · line 380
Quoted 5 lines
drivers/misc/habanalabs/device.c +5
-0+/* 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 ;Why rc is 0 here?
See my explanation in the previous patch. It is to make the device stay in Linux in "disabled/malfunction" state and give user ability to reset it / debug it
Quoted 246 lines
Quoted 246 lines
drivers/misc/habanalabs/device.c +16
-0+ goto out_disabled; + } +dev_notice (hdev->dev,"Successfully added device to habanalabs driver\n" );@@ -335,6 +392,12 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) "kernel ctx is still alive on initialization failure\n"); free_ctx:kfree (hdev->kernel_ctx); +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); sw_fini: hdev->asic_funcs->sw_fini (hdev); early_fini:@@ -364,6 +427,7 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)*/ void hl_device_fini (struct hl_device *hdev) { +int i;dev_info (hdev->dev,"Removing device\n" );/* Mark device as disabled */ @@ -378,6 +442,12 @@ void hl_device_fini(struct hl_device *hdev)/* Reset the H/W. It will be in idle state after this returns */ hdev->asic_funcs->hw_fini (hdev, true); +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++) +hl_cq_fini (hdev, &hdev->completion_queue[i]); +kfree (hdev->completion_queue); + +hl_hw_queues_destroy (hdev); +/* Call ASIC S/W finalize function */ hdev->asic_funcs->sw_fini (hdev);drivers/misc/habanalabs/goya/goya.c +177
-0@@ -98,6 +98,26 @@static void goya_get_fixed_properties (struct hl_device *hdev) {struct asic_fixed_properties *prop = &hdev->asic_prop; +int i; + +for (i =0 ; i <NUMBER_OF_EXT_HW_QUEUES ; i++) { + prop->hw_queues_props[i].type =QUEUE_TYPE_EXT ; + prop->hw_queues_props[i].kmd_only =0 ; + } + +for (; i <NUMBER_OF_EXT_HW_QUEUES +NUMBER_OF_CPU_HW_QUEUES ; i++) { + prop->hw_queues_props[i].type =QUEUE_TYPE_CPU ; + prop->hw_queues_props[i].kmd_only =1 ; + } + +for (; i <NUMBER_OF_EXT_HW_QUEUES +NUMBER_OF_CPU_HW_QUEUES + +NUMBER_OF_INT_HW_QUEUES ; i++) { + prop->hw_queues_props[i].type =QUEUE_TYPE_INT ; + prop->hw_queues_props[i].kmd_only =0 ; + } + +for (; i <HL_MAX_QUEUES ; i++) + prop->hw_queues_props[i].type =QUEUE_TYPE_NA ; prop->completion_queues_count =NUMBER_OF_CMPLT_QUEUES ;@@ -126,6 +146,18 @@ static void goya_get_fixed_properties(struct hl_device *hdev) prop->high_pll =PLL_HIGH_DEFAULT ; } +int goya_send_pci_access_msg (struct hl_device *hdev,u32 opcode) +{ +struct armcp_packet pkt; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode = opcode; + +return hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt, +sizeof (pkt),HL_DEVICE_TIMEOUT_USEC ,NULL ); +} +/** * goya_pci_bars_map - Map PCI BARS of Goya device * @@ -509,6 +541,8 @@ static int goya_sw_init(struct hl_device *hdev)if (!goya)return -ENOMEM ; + goya->test_cpu_queue = goya_test_cpu_queue; +/* according to goya_init_iatu */ goya->ddr_bar_cur_addr =DRAM_PHYS_BASE ; hdev->asic_specific = goya;@@ -595,6 +629,299 @@ int goya_sw_fini(struct hl_device *hdev)return 0 ; } +static void goya_init_dma_qman (struct hl_device *hdev,int dma_id, +dma_addr_t bus_address) +{ +struct goya_device *goya = hdev->asic_specific; +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u32 reg_off = dma_id * (mmDMA_QM_1_PQ_PI - mmDMA_QM_0_PQ_PI); + + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + +WREG32 (mmDMA_QM_0_PQ_BASE_LO + reg_off,lower_32_bits (bus_address)); +WREG32 (mmDMA_QM_0_PQ_BASE_HI + reg_off,upper_32_bits (bus_address)); + +WREG32 (mmDMA_QM_0_PQ_SIZE + reg_off,ilog2 (HL_QUEUE_LENGTH )); +WREG32 (mmDMA_QM_0_PQ_PI + reg_off,0 ); +WREG32 (mmDMA_QM_0_PQ_CI + reg_off,0 ); + +WREG32 (mmDMA_QM_0_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo); +WREG32 (mmDMA_QM_0_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi); +WREG32 (mmDMA_QM_0_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo); +WREG32 (mmDMA_QM_0_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi); +WREG32 (mmDMA_QM_0_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo); +WREG32 (mmDMA_QM_0_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi); +WREG32 (mmDMA_QM_0_GLBL_ERR_WDATA + reg_off, +GOYA_ASYNC_EVENT_ID_DMA0_QM + dma_id); + +/* PQ has buffer of 2 cache lines, while CQ has 8 lines */ +WREG32 (mmDMA_QM_0_PQ_CFG1 + reg_off,0x00020002 ); +WREG32 (mmDMA_QM_0_CQ_CFG1 + reg_off,0x00080008 ); + +if (dma_id ==0 ) +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off,QMAN_DMA_FULLY_TRUSTED ); +else +if (goya->hw_cap_initialized &HW_CAP_MMU ) +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off, +QMAN_DMA_PARTLY_TRUSTED ); +else +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off, +QMAN_DMA_FULLY_TRUSTED ); + +WREG32 (mmDMA_QM_0_GLBL_ERR_CFG + reg_off,QMAN_DMA_ERR_MSG_EN ); +WREG32 (mmDMA_QM_0_GLBL_CFG0 + reg_off,QMAN_DMA_ENABLE ); +} + +static void goya_init_dma_ch (struct hl_device *hdev,int dma_id) +{ +u32 gic_base_lo, gic_base_hi; +u64 sob_addr; +u32 reg_off = dma_id * (mmDMA_CH_1_CFG1 - mmDMA_CH_0_CFG1); + + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + +WREG32 (mmDMA_CH_0_ERRMSG_ADDR_LO + reg_off, gic_base_lo); +WREG32 (mmDMA_CH_0_ERRMSG_ADDR_HI + reg_off, gic_base_hi); +WREG32 (mmDMA_CH_0_ERRMSG_WDATA + reg_off, +GOYA_ASYNC_EVENT_ID_DMA0_CH + dma_id); + +if (dma_id) { + sob_addr =CFG_BASE + mmSYNC_MNGR_SOB_OBJ_1000 + + (dma_id -1 ) *4 ; +WREG32 (mmDMA_CH_0_WR_COMP_ADDR_LO + reg_off, +lower_32_bits (sob_addr)); +WREG32 (mmDMA_CH_0_WR_COMP_ADDR_HI + reg_off, +upper_32_bits (sob_addr)); +WREG32 (mmDMA_CH_0_WR_COMP_WDATA + reg_off,0x80000001 ); + } +} + +/** +* goya_init_dma_qmans - Initialize QMAN DMA registers +* +* @hdev: pointer to hl_device structure +* +* Initialize the H/W registers of the QMAN DMA channels +* +*/ +static void goya_init_dma_qmans (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +struct hl_hw_queue *q; +dma_addr_t bus_address; +int i; + +if (goya->hw_cap_initialized &HW_CAP_DMA ) +return ; + + q = &hdev->kernel_queues[0 ]; + +for (i =0 ; i <NUMBER_OF_EXT_HW_QUEUES ; i++, q++) { + bus_address = q->bus_address + + hdev->asic_prop.host_phys_base_address; + +goya_init_dma_qman (hdev, i, bus_address); +goya_init_dma_ch (hdev, i); + } + + goya->hw_cap_initialized |=HW_CAP_DMA ; +} + +/** +* goya_disable_external_queues - Disable external queues +* +* @hdev: pointer to hl_device structure +* +*/ +static void goya_disable_external_queues (struct hl_device *hdev) +{ +WREG32 (mmDMA_QM_0_GLBL_CFG0,0 ); +WREG32 (mmDMA_QM_1_GLBL_CFG0,0 ); +WREG32 (mmDMA_QM_2_GLBL_CFG0,0 ); +WREG32 (mmDMA_QM_3_GLBL_CFG0,0 ); +WREG32 (mmDMA_QM_4_GLBL_CFG0,0 ); +} + +static int goya_stop_queue (struct hl_device *hdev,u32 cfg_reg, +u32 cp_sts_reg,u32 glbl_sts0_reg) +{ +int rc; +u32 status; + +/* use the values of TPC0 as they are all the same*/ + +WREG32 (cfg_reg,1 <<TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT ); + + status =RREG32 (cp_sts_reg); +if (status &TPC0_QM_CP_STS_FENCE_IN_PROGRESS_MASK ) { + rc = hl_poll_timeout( + hdev, + cp_sts_reg, + status, + !(status & TPC0_QM_CP_STS_FENCE_IN_PROGRESS_MASK),
Quoted 8 lines
drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 780
Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c +6
-0+1000 , + QMAN_FENCE_TIMEOUT_USEC); + +/* if QMAN is stuck in fence no need to check for stop */ +if (rc) +return 0 ;Isn't it an error?
Nope, that's how our H/W works :( if the QMAN is stuck in fence, the stop indication will never be set, so no point in checking it. But when the QMAN is stuck in fence, it is almost equal for stop and it is good enough for reset.
Quoted 561 lines
Quoted 561 lines
drivers/misc/habanalabs/goya/goya.c +407
-113+ } + + rc =hl_poll_timeout ( + hdev, + glbl_sts0_reg, + status, + (status &TPC0_QM_GLBL_STS0_CP_IS_STOP_MASK ), +1000 , +QMAN_STOP_TIMEOUT_USEC ); + +if (rc) { +dev_err (hdev->dev, +"Timeout while waiting for QMAN to stop\n" ); +return -EINVAL ; + } + +return 0 ; +} + +/** +* goya_stop_external_queues - Stop external queues +* +* @hdev: pointer to hl_device structure +* +* Returns 0 on success +* +*/ +static int goya_stop_external_queues (struct hl_device *hdev) +{ +int rc =goya_stop_queue (hdev, + mmDMA_QM_0_GLBL_CFG1, + mmDMA_QM_0_CP_STS, + mmDMA_QM_0_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 0\n" ); + + rc =goya_stop_queue (hdev, + mmDMA_QM_1_GLBL_CFG1, + mmDMA_QM_1_CP_STS, + mmDMA_QM_1_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 1\n" ); + + rc =goya_stop_queue (hdev, + mmDMA_QM_2_GLBL_CFG1, + mmDMA_QM_2_CP_STS, + mmDMA_QM_2_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 2\n" ); + + rc =goya_stop_queue (hdev, + mmDMA_QM_3_GLBL_CFG1, + mmDMA_QM_3_CP_STS, + mmDMA_QM_3_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 3\n" ); + + rc =goya_stop_queue (hdev, + mmDMA_QM_4_GLBL_CFG1, + mmDMA_QM_4_CP_STS, + mmDMA_QM_4_GLBL_STS0); + +if (rc) +dev_err (hdev->dev,"failed to stop DMA QMAN 4\n" ); + +return rc; +} + +static void goya_resume_external_queues (struct hl_device *hdev) +{ +WREG32 (mmDMA_QM_0_GLBL_CFG1,0 ); +WREG32 (mmDMA_QM_1_GLBL_CFG1,0 ); +WREG32 (mmDMA_QM_2_GLBL_CFG1,0 ); +WREG32 (mmDMA_QM_3_GLBL_CFG1,0 ); +WREG32 (mmDMA_QM_4_GLBL_CFG1,0 ); +} + +/** +* goya_init_cpu_queues - Initialize PQ/CQ/EQ of CPU +* +* @hdev: pointer to hl_device structure +* +* Returns 0 on success +* +*/ +int goya_init_cpu_queues (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +dma_addr_t bus_address; +u32 status; +struct hl_hw_queue *cpu_pq = &hdev->kernel_queues[GOYA_QUEUE_ID_CPU_PQ ]; +int err; + +if (!hdev->cpu_queues_enable) +return 0 ; + +if (goya->hw_cap_initialized &HW_CAP_CPU_Q ) +return 0 ; + + bus_address = cpu_pq->bus_address + + hdev->asic_prop.host_phys_base_address; +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_0,lower_32_bits (bus_address)); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_1,upper_32_bits (bus_address)); + + bus_address = hdev->cpu_accessible_dma_address + + hdev->asic_prop.host_phys_base_address; +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_8,lower_32_bits (bus_address)); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_9,upper_32_bits (bus_address)); + +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_5,HL_QUEUE_SIZE_IN_BYTES ); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_10,CPU_ACCESSIBLE_MEM_SIZE ); + +/* Used for EQ CI */ +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_6,0 ); + +WREG32 (mmCPU_IF_PF_PQ_PI,0 ); + +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_7,PQ_INIT_STATUS_READY_FOR_CP ); + +WREG32 (mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, +GOYA_ASYNC_EVENT_ID_PI_UPDATE ); + + err =hl_poll_timeout ( + hdev, + mmPSOC_GLOBAL_CONF_SCRATCHPAD_7, + status, + (status ==PQ_INIT_STATUS_READY_FOR_HOST ), +1000 , +GOYA_CPU_TIMEOUT_USEC ); + +if (err) { +dev_err (hdev->dev, +"Failed to communicate with ARM CPU (ArmCP timeout)\n" ); +return -EIO ; + } + + goya->hw_cap_initialized |=HW_CAP_CPU_Q ; +return 0 ; +} +/** * goya_init_pll - Initialize pll registers * @@ -1960,152 +2287,646 @@ static void goya_init_golden_registers(struct hl_device *hdev) goya->hw_cap_initialized |=HW_CAP_GOLDEN ; } - -/** -* goya_push_uboot_to_device - Push u-boot FW code to device -* -* @hdev: pointer to hl_device structure -* -* Copy u-boot fw code from firmware file to SRAM BAR. -* Returns 0 on success -* -*/ -static int goya_push_uboot_to_device (struct hl_device *hdev) +static void goya_init_mme_qman (struct hl_device *hdev) { -char fw_name[200 ]; -const u64 *fw_data; -void __iomem *dst; -size_t fw_size, i; -int rc; +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u64 qman_base_addr; -snprintf (fw_name,sizeof (fw_name),"habanalabs/goya/goya-u-boot.bin" ); + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); - rc =request_firmware (&hdev->spl_fw, fw_name, hdev->dev); + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); -if (rc) { -dev_err (hdev->dev,"Failed to request u-boot fw image\n" ); - goto out; - } + qman_base_addr = hdev->asic_prop.sram_base_address + +MME_QMAN_BASE_OFFSET ; - fw_size = hdev->spl_fw->size; -if ((fw_size %4 ) !=0 ) { -dev_err (hdev->dev,"illegal u-boot firmware size %lu\n" , - fw_size); - rc = -EINVAL ; - goto out; - } +WREG32 (mmMME_QM_PQ_BASE_LO,lower_32_bits (qman_base_addr)); +WREG32 (mmMME_QM_PQ_BASE_HI,upper_32_bits (qman_base_addr)); +WREG32 (mmMME_QM_PQ_SIZE,ilog2 (MME_QMAN_LENGTH )); +WREG32 (mmMME_QM_PQ_PI,0 ); +WREG32 (mmMME_QM_PQ_CI,0 ); +WREG32 (mmMME_QM_CP_LDMA_SRC_BASE_LO_OFFSET,0x10C0 ); +WREG32 (mmMME_QM_CP_LDMA_SRC_BASE_HI_OFFSET,0x10C4 ); +WREG32 (mmMME_QM_CP_LDMA_TSIZE_OFFSET,0x10C8 ); +WREG32 (mmMME_QM_CP_LDMA_COMMIT_OFFSET,0x10CC ); -dev_dbg (hdev->dev,"u-boot firmware size == %lu\n" , fw_size); +WREG32 (mmMME_QM_CP_MSG_BASE0_ADDR_LO, mtr_base_lo); +WREG32 (mmMME_QM_CP_MSG_BASE0_ADDR_HI, mtr_base_hi); +WREG32 (mmMME_QM_CP_MSG_BASE1_ADDR_LO, so_base_lo); +WREG32 (mmMME_QM_CP_MSG_BASE1_ADDR_HI, so_base_hi); - fw_data = (const u64 *) hdev->spl_fw->data; - dst = hdev->pcie_bar[SRAM_CFG_BAR_ID ] +UBOOT_FW_OFFSET ; +/* QMAN CQ has 8 cache lines */ +WREG32 (mmMME_QM_CQ_CFG1,0x00080008 ); -if ((hdev->spl_fw->size %8 ) !=0 ) - fw_size -=8 ; +WREG32 (mmMME_QM_GLBL_ERR_ADDR_LO, gic_base_lo); +WREG32 (mmMME_QM_GLBL_ERR_ADDR_HI, gic_base_hi); -for (i =0 ; i < fw_size ; i +=8 , fw_data++, dst +=8 ) { -if (!(i & (0x80000 -1 ))) -dev_dbg (hdev->dev, -"u-boot copied so far %lu out of %lu" , - i, fw_size); +WREG32 (mmMME_QM_GLBL_ERR_WDATA,GOYA_ASYNC_EVENT_ID_MME_QM ); -writeq (*fw_data, dst); - } +WREG32 (mmMME_QM_GLBL_ERR_CFG,QMAN_MME_ERR_MSG_EN ); -if ((hdev->spl_fw->size %8 ) !=0 ) -writel (*(const u32 *) fw_data, dst); +WREG32 (mmMME_QM_GLBL_PROT,QMAN_MME_ERR_PROT ); -out: -release_firmware (hdev->spl_fw); -return rc; +WREG32 (mmMME_QM_GLBL_CFG0,QMAN_MME_ENABLE ); } -/** -* goya_push_linux_to_device - Push LINUX FW code to device -* -* @hdev: pointer to hl_device structure -* -* Copy LINXU fw code from firmware file to DDR BAR. -* Returns 0 on success -* -*/ -static int goya_push_linux_to_device (struct hl_device *hdev) +static void goya_init_mme_cmdq (struct hl_device *hdev) { -char fw_name[200 ]; -const u64 *fw_data; -void __iomem *dst; -size_t fw_size, i; -int rc; +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u64 qman_base_addr; -snprintf (fw_name,sizeof (fw_name),"habanalabs/goya/goya-fit.itb" ); + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); - rc =request_firmware (&hdev->spl_fw, fw_name, hdev->dev); + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); -if (rc) { -dev_err (hdev->dev,"Failed to request Linux fw image\n" ); - goto out; - } + qman_base_addr = hdev->asic_prop.sram_base_address + +MME_QMAN_BASE_OFFSET ; - fw_size = hdev->spl_fw->size; -if ((fw_size %4 ) !=0 ) { -dev_err (hdev->dev,"illegal Linux firmware size %lu\n" , - fw_size); - rc = -EINVAL ; - goto out; - } +WREG32 (mmMME_CMDQ_CP_MSG_BASE0_ADDR_LO, mtr_base_lo); +WREG32 (mmMME_CMDQ_CP_MSG_BASE0_ADDR_HI, mtr_base_hi); +WREG32 (mmMME_CMDQ_CP_MSG_BASE1_ADDR_LO, so_base_lo); +WREG32 (mmMME_CMDQ_CP_MSG_BASE1_ADDR_HI, so_base_hi); -dev_dbg (hdev->dev,"Linux firmware size == %lu\n" , fw_size); +/* CMDQ CQ has 20 cache lines */ +WREG32 (mmMME_CMDQ_CQ_CFG1,0x00140014 ); - fw_data = (const u64 *) hdev->spl_fw->data; - dst = hdev->pcie_bar[DDR_BAR_ID ] +LINUX_FW_OFFSET ; +WREG32 (mmMME_CMDQ_GLBL_ERR_ADDR_LO, gic_base_lo); +WREG32 (mmMME_CMDQ_GLBL_ERR_ADDR_HI, gic_base_hi); -if ((hdev->spl_fw->size %8 ) !=0 ) - fw_size -=8 ; +WREG32 (mmMME_CMDQ_GLBL_ERR_WDATA,GOYA_ASYNC_EVENT_ID_MME_CMDQ ); -for (i =0 ; i < fw_size ; i +=8 , fw_data++, dst +=8 ) { -if (!(i & (0x80000 -1 ))) { -dev_dbg (hdev->dev, -"Linux copied so far %lu out of %lu" , - i, fw_size); -usleep_range (20 ,100 ); - } -writeq (*fw_data, dst); - } +WREG32 (mmMME_CMDQ_GLBL_ERR_CFG,CMDQ_MME_ERR_MSG_EN ); -if ((hdev->spl_fw->size %8 ) !=0 ) -writel (*(const u32 *) fw_data, dst); +WREG32 (mmMME_CMDQ_GLBL_PROT,CMDQ_MME_ERR_PROT ); -out: -release_firmware (hdev->spl_fw); -return rc; +WREG32 (mmMME_CMDQ_GLBL_CFG0,CMDQ_MME_ENABLE ); } -static int goya_pldm_init_cpu (struct hl_device *hdev) +static void goya_init_mme_qmans (struct hl_device *hdev) { -u32 val, unit_rst_val; -int rc; +struct goya_device *goya = hdev->asic_specific; +u32 so_base_lo, so_base_hi; -/* Must initialize SRAM scrambler before pushing u-boot to SRAM */ -goya_init_golden_registers (hdev); +if (goya->hw_cap_initialized &HW_CAP_MME ) +return ; -/* Put ARM cores into reset */ -WREG32 (mmCPU_CA53_CFG_ARM_RST_CONTROL,CPU_RESET_ASSERT ); - val =RREG32 (mmCPU_CA53_CFG_ARM_RST_CONTROL); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); -/* Reset the CA53 MACRO */ - unit_rst_val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); -WREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N,CA53_RESET ); - val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); -WREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N, unit_rst_val); - val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); +WREG32 (mmMME_SM_BASE_ADDRESS_LOW, so_base_lo); +WREG32 (mmMME_SM_BASE_ADDRESS_HIGH, so_base_hi); - rc =goya_push_uboot_to_device (hdev); -if (rc) -return rc; +goya_init_mme_qman (hdev); +goya_init_mme_cmdq (hdev); - rc =goya_push_linux_to_device (hdev); -if (rc) -return rc; + goya->hw_cap_initialized |=HW_CAP_MME ; +} + +static void goya_init_tpc_qman (struct hl_device *hdev,u32 base_off,int tpc_id) +{ +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u64 qman_base_addr; +u32 reg_off = tpc_id * (mmTPC1_QM_PQ_PI - mmTPC0_QM_PQ_PI); + + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + + qman_base_addr = hdev->asic_prop.sram_base_address + base_off; + +WREG32 (mmTPC0_QM_PQ_BASE_LO + reg_off,lower_32_bits (qman_base_addr)); +WREG32 (mmTPC0_QM_PQ_BASE_HI + reg_off,upper_32_bits (qman_base_addr)); +WREG32 (mmTPC0_QM_PQ_SIZE + reg_off,ilog2 (TPC_QMAN_LENGTH )); +WREG32 (mmTPC0_QM_PQ_PI + reg_off,0 ); +WREG32 (mmTPC0_QM_PQ_CI + reg_off,0 ); +WREG32 (mmTPC0_QM_CP_LDMA_SRC_BASE_LO_OFFSET + reg_off,0x10C0 ); +WREG32 (mmTPC0_QM_CP_LDMA_SRC_BASE_HI_OFFSET + reg_off,0x10C4 ); +WREG32 (mmTPC0_QM_CP_LDMA_TSIZE_OFFSET + reg_off,0x10C8 ); +WREG32 (mmTPC0_QM_CP_LDMA_COMMIT_OFFSET + reg_off,0x10CC ); + +WREG32 (mmTPC0_QM_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo); +WREG32 (mmTPC0_QM_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi); +WREG32 (mmTPC0_QM_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo); +WREG32 (mmTPC0_QM_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi); + +WREG32 (mmTPC0_QM_CQ_CFG1 + reg_off,0x00080008 ); + +WREG32 (mmTPC0_QM_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo); +WREG32 (mmTPC0_QM_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi); + +WREG32 (mmTPC0_QM_GLBL_ERR_WDATA + reg_off, +GOYA_ASYNC_EVENT_ID_TPC0_QM + tpc_id); + +WREG32 (mmTPC0_QM_GLBL_ERR_CFG + reg_off,QMAN_TPC_ERR_MSG_EN ); + +WREG32 (mmTPC0_QM_GLBL_PROT + reg_off,QMAN_TPC_ERR_PROT ); + +WREG32 (mmTPC0_QM_GLBL_CFG0 + reg_off,QMAN_TPC_ENABLE ); +} + +static void goya_init_tpc_cmdq (struct hl_device *hdev,int tpc_id) +{ +u32 mtr_base_lo, mtr_base_hi; +u32 so_base_lo, so_base_hi; +u32 gic_base_lo, gic_base_hi; +u32 reg_off = tpc_id * (mmTPC1_CMDQ_CQ_CFG1 - mmTPC0_CMDQ_CQ_CFG1); + + mtr_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + mtr_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0); + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + + gic_base_lo = +lower_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + gic_base_hi = +upper_32_bits (CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR); + +WREG32 (mmTPC0_CMDQ_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo); +WREG32 (mmTPC0_CMDQ_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi); +WREG32 (mmTPC0_CMDQ_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo); +WREG32 (mmTPC0_CMDQ_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi); + +WREG32 (mmTPC0_CMDQ_CQ_CFG1 + reg_off,0x00140014 ); + +WREG32 (mmTPC0_CMDQ_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo); +WREG32 (mmTPC0_CMDQ_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi); + +WREG32 (mmTPC0_CMDQ_GLBL_ERR_WDATA + reg_off, +GOYA_ASYNC_EVENT_ID_TPC0_CMDQ + tpc_id); + +WREG32 (mmTPC0_CMDQ_GLBL_ERR_CFG + reg_off,CMDQ_TPC_ERR_MSG_EN ); + +WREG32 (mmTPC0_CMDQ_GLBL_PROT + reg_off,CMDQ_TPC_ERR_PROT ); + +WREG32 (mmTPC0_CMDQ_GLBL_CFG0 + reg_off,CMDQ_TPC_ENABLE ); +} + +static void goya_init_tpc_qmans (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +u32 so_base_lo, so_base_hi; +u32 cfg_off = mmTPC1_CFG_SM_BASE_ADDRESS_LOW - + mmTPC0_CFG_SM_BASE_ADDRESS_LOW; +int i; + +if (goya->hw_cap_initialized &HW_CAP_TPC ) +return ; + + so_base_lo =lower_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + so_base_hi =upper_32_bits (CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0); + +for (i =0 ; i <TPC_MAX_NUM ; i++) { +WREG32 (mmTPC0_CFG_SM_BASE_ADDRESS_LOW + i * cfg_off, + so_base_lo); +WREG32 (mmTPC0_CFG_SM_BASE_ADDRESS_HIGH + i * cfg_off, + so_base_hi); + } + +goya_init_tpc_qman (hdev,TPC0_QMAN_BASE_OFFSET ,0 ); +goya_init_tpc_qman (hdev,TPC1_QMAN_BASE_OFFSET ,1 ); +goya_init_tpc_qman (hdev,TPC2_QMAN_BASE_OFFSET ,2 ); +goya_init_tpc_qman (hdev,TPC3_QMAN_BASE_OFFSET ,3 ); +goya_init_tpc_qman (hdev,TPC4_QMAN_BASE_OFFSET ,4 ); +goya_init_tpc_qman (hdev,TPC5_QMAN_BASE_OFFSET ,5 ); +goya_init_tpc_qman (hdev,TPC6_QMAN_BASE_OFFSET ,6 ); +goya_init_tpc_qman (hdev,TPC7_QMAN_BASE_OFFSET ,7 ); + +for (i =0 ; i <TPC_MAX_NUM ; i++) +goya_init_tpc_cmdq (hdev, i); + + goya->hw_cap_initialized |=HW_CAP_TPC ; +} + +/** +* goya_disable_internal_queues - Disable internal queues +* +* @hdev: pointer to hl_device structure +* +*/ +static void goya_disable_internal_queues (struct hl_device *hdev) +{ +WREG32 (mmMME_QM_GLBL_CFG0,0 ); +WREG32 (mmMME_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC0_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC0_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC1_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC1_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC2_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC2_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC3_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC3_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC4_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC4_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC5_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC5_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC6_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC6_CMDQ_GLBL_CFG0,0 ); + +WREG32 (mmTPC7_QM_GLBL_CFG0,0 ); +WREG32 (mmTPC7_CMDQ_GLBL_CFG0,0 ); +} + +/** +* goya_stop_internal_queues - Stop internal queues +* +* @hdev: pointer to hl_device structure +* +* Returns 0 on success +* +*/ +static int goya_stop_internal_queues (struct hl_device *hdev) +{ +int rc, retval =0 ; + + rc =goya_stop_queue (hdev, + mmMME_QM_GLBL_CFG1, + mmMME_QM_CP_STS, + mmMME_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop MME QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmMME_CMDQ_GLBL_CFG1, + mmMME_CMDQ_CP_STS, + mmMME_CMDQ_GLBL_STS0); +
Quoted 8 lines
drivers/misc/habanalabs/goya/goya.c static void goya_init_golden_registers(struct hl_device *hdev) · line 2589
Quoted 4 lines
drivers/misc/habanalabs/goya/goya.c +4
-0+if (rc) { +dev_err (hdev->dev,"failed to stop MME CMDQ\n" ); + retval = -EIO ; + }If I understand correctly, the queues can be and should be stopped independently and failure to stop one of them wouldn't prevent stopping the others.
If that's the case a comment explaining that would be nice.
Correct, added comment
Quoted 2,308 lines
Quoted 657 lines
drivers/misc/habanalabs/goya/goya.c +599
-4+ rc =goya_stop_queue (hdev, + mmTPC0_QM_GLBL_CFG1, + mmTPC0_QM_CP_STS, + mmTPC0_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 0 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC0_CMDQ_GLBL_CFG1, + mmTPC0_CMDQ_CP_STS, + mmTPC0_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 0 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC1_QM_GLBL_CFG1, + mmTPC1_QM_CP_STS, + mmTPC1_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 1 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC1_CMDQ_GLBL_CFG1, + mmTPC1_CMDQ_CP_STS, + mmTPC1_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 1 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC2_QM_GLBL_CFG1, + mmTPC2_QM_CP_STS, + mmTPC2_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 2 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC2_CMDQ_GLBL_CFG1, + mmTPC2_CMDQ_CP_STS, + mmTPC2_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 2 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC3_QM_GLBL_CFG1, + mmTPC3_QM_CP_STS, + mmTPC3_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 3 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC3_CMDQ_GLBL_CFG1, + mmTPC3_CMDQ_CP_STS, + mmTPC3_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 3 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC4_QM_GLBL_CFG1, + mmTPC4_QM_CP_STS, + mmTPC4_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 4 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC4_CMDQ_GLBL_CFG1, + mmTPC4_CMDQ_CP_STS, + mmTPC4_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 4 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC5_QM_GLBL_CFG1, + mmTPC5_QM_CP_STS, + mmTPC5_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 5 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC5_CMDQ_GLBL_CFG1, + mmTPC5_CMDQ_CP_STS, + mmTPC5_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 5 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC6_QM_GLBL_CFG1, + mmTPC6_QM_CP_STS, + mmTPC6_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 6 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC6_CMDQ_GLBL_CFG1, + mmTPC6_CMDQ_CP_STS, + mmTPC6_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 6 CMDQ\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC7_QM_GLBL_CFG1, + mmTPC7_QM_CP_STS, + mmTPC7_QM_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 7 QMAN\n" ); + retval = -EIO ; + } + + rc =goya_stop_queue (hdev, + mmTPC7_CMDQ_GLBL_CFG1, + mmTPC7_CMDQ_CP_STS, + mmTPC7_CMDQ_GLBL_STS0); + +if (rc) { +dev_err (hdev->dev,"failed to stop TPC 7 CMDQ\n" ); + retval = -EIO ; + } + +return rc; +} + +static void goya_resume_internal_queues (struct hl_device *hdev) +{ +WREG32 (mmMME_QM_GLBL_CFG1,0 ); +WREG32 (mmMME_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC0_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC0_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC1_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC1_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC2_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC2_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC3_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC3_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC4_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC4_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC5_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC5_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC6_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC6_CMDQ_GLBL_CFG1,0 ); + +WREG32 (mmTPC7_QM_GLBL_CFG1,0 ); +WREG32 (mmTPC7_CMDQ_GLBL_CFG1,0 ); +} + + +/** +* goya_push_uboot_to_device - Push u-boot FW code to device +* +* @hdev: pointer to hl_device structure +* +* Copy u-boot fw code from firmware file to SRAM BAR. +* Returns 0 on success +* +*/ +static int goya_push_uboot_to_device (struct hl_device *hdev) +{ +char fw_name[200 ]; +const u64 *fw_data; +void __iomem *dst; +size_t fw_size, i; +int rc; + +snprintf (fw_name,sizeof (fw_name),"habanalabs/goya/goya-u-boot.bin" ); + + rc =request_firmware (&hdev->spl_fw, fw_name, hdev->dev); + +if (rc) { +dev_err (hdev->dev,"Failed to request u-boot fw image\n" ); + goto out; + } + + fw_size = hdev->spl_fw->size; +if ((fw_size %4 ) !=0 ) { +dev_err (hdev->dev,"illegal u-boot firmware size %lu\n" , + fw_size); + rc = -EINVAL ; + goto out; + } + +dev_dbg (hdev->dev,"u-boot firmware size == %lu\n" , fw_size); + + fw_data = (const u64 *) hdev->spl_fw->data; + dst = hdev->pcie_bar[SRAM_CFG_BAR_ID ] +UBOOT_FW_OFFSET ; + +if ((hdev->spl_fw->size %8 ) !=0 ) + fw_size -=8 ; + +for (i =0 ; i < fw_size ; i +=8 , fw_data++, dst +=8 ) { +if (!(i & (0x80000 -1 ))) +dev_dbg (hdev->dev, +"u-boot copied so far %lu out of %lu" , + i, fw_size); + +writeq (*fw_data, dst); + } + +if ((hdev->spl_fw->size %8 ) !=0 ) +writel (*(const u32 *) fw_data, dst); + +out: +release_firmware (hdev->spl_fw); +return rc; +} + +/** +* goya_push_linux_to_device - Push LINUX FW code to device +* +* @hdev: pointer to hl_device structure +* +* Copy LINXU fw code from firmware file to DDR BAR. +* Returns 0 on success +* +*/ +static int goya_push_linux_to_device (struct hl_device *hdev) +{ +char fw_name[200 ]; +const u64 *fw_data; +void __iomem *dst; +size_t fw_size, i; +int rc; + +snprintf (fw_name,sizeof (fw_name),"habanalabs/goya/goya-fit.itb" ); + + rc =request_firmware (&hdev->spl_fw, fw_name, hdev->dev); + +if (rc) { +dev_err (hdev->dev,"Failed to request Linux fw image\n" ); + goto out; + } + + fw_size = hdev->spl_fw->size; +if ((fw_size %4 ) !=0 ) { +dev_err (hdev->dev,"illegal Linux firmware size %lu\n" , + fw_size); + rc = -EINVAL ; + goto out; + } + +dev_dbg (hdev->dev,"Linux firmware size == %lu\n" , fw_size); + + fw_data = (const u64 *) hdev->spl_fw->data; + dst = hdev->pcie_bar[DDR_BAR_ID ] +LINUX_FW_OFFSET ; + +if ((hdev->spl_fw->size %8 ) !=0 ) + fw_size -=8 ; + +for (i =0 ; i < fw_size ; i +=8 , fw_data++, dst +=8 ) { +if (!(i & (0x80000 -1 ))) { +dev_dbg (hdev->dev, +"Linux copied so far %lu out of %lu" , + i, fw_size); +usleep_range (20 ,100 ); + } +writeq (*fw_data, dst); + } + +if ((hdev->spl_fw->size %8 ) !=0 ) +writel (*(const u32 *) fw_data, dst); + +out: +release_firmware (hdev->spl_fw); +return rc; +} + +static int goya_pldm_init_cpu (struct hl_device *hdev) +{ +u32 val, unit_rst_val; +int rc; + +/* Must initialize SRAM scrambler before pushing u-boot to SRAM */ +goya_init_golden_registers (hdev); + +/* Put ARM cores into reset */ +WREG32 (mmCPU_CA53_CFG_ARM_RST_CONTROL,CPU_RESET_ASSERT ); + val =RREG32 (mmCPU_CA53_CFG_ARM_RST_CONTROL); + +/* Reset the CA53 MACRO */ + unit_rst_val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); +WREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N,CA53_RESET ); + val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); +WREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N, unit_rst_val); + val =RREG32 (mmPSOC_GLOBAL_CONF_UNIT_RST_N); + + rc =goya_push_uboot_to_device (hdev); +if (rc) +return rc; + + rc =goya_push_linux_to_device (hdev); +if (rc) +return rc;WREG32 (mmPSOC_GLOBAL_CONF_UBOOT_MAGIC,KMD_MSG_FIT_RDY );WREG32 (mmPSOC_GLOBAL_CONF_WARM_REBOOT,CPU_BOOT_STATUS_NA );@@ -2339,6 +3160,19 @@ static int goya_hw_init(struct hl_device *hdev)goya_init_security (hdev); +goya_init_dma_qmans (hdev); + +goya_init_mme_qmans (hdev); + +goya_init_tpc_qmans (hdev); + + rc =goya_init_cpu_queues (hdev); +if (rc) { +dev_err (hdev->dev,"failed to initialize CPU H/W queues %d\n" , + rc); + goto disable_queues; + } +/* CPU initialization is finished, we can now move to 48 bit DMA mask */ rc =pci_set_dma_mask (hdev->pdev,DMA_BIT_MASK (48 ));if (rc) {@@ -2347,7 +3181,7 @@ static int goya_hw_init(struct hl_device *hdev)if (rc) {dev_err (hdev->dev,"Unable to set pci dma mask to 32 bits\n" ); -return rc; + goto disable_pci_access; } }@@ -2359,7 +3193,7 @@ static int goya_hw_init(struct hl_device *hdev)if (rc) {dev_err (hdev->dev,"Unable to set pci consistent dma mask to 32 bits\n" ); -return rc; + goto disable_pci_access; } }@@ -2367,6 +3201,14 @@ static int goya_hw_init(struct hl_device *hdev) val =RREG32 (mmPCIE_DBI_DEVICE_ID_VENDOR_ID_REG);return 0 ; + +disable_pci_access: +goya_send_pci_access_msg (hdev,ARMCP_PACKET_DISABLE_PCI_ACCESS ); +disable_queues: +goya_disable_internal_queues (hdev); +goya_disable_external_queues (hdev); + +return rc; }/** @@ -2473,12 +3315,40 @@ static void goya_hw_fini(struct hl_device *hdev, bool hard_reset)int goya_suspend (struct hl_device *hdev) { -return 0 ; +int rc; + + rc =goya_stop_internal_queues (hdev); + +if (rc) { +dev_err (hdev->dev,"failed to stop internal queues\n" ); +return rc; + } + + rc =goya_stop_external_queues (hdev); + +if (rc) { +dev_err (hdev->dev,"failed to stop external queues\n" ); +return rc; + } + + rc =goya_send_pci_access_msg (hdev,ARMCP_PACKET_DISABLE_PCI_ACCESS ); +if (rc) +dev_err (hdev->dev,"Failed to disable PCI access from CPU\n" ); + +return rc; }int goya_resume (struct hl_device *hdev) { -return 0 ; +int rc; + +goya_resume_external_queues (hdev); +goya_resume_internal_queues (hdev); + + rc =goya_send_pci_access_msg (hdev,ARMCP_PACKET_ENABLE_PCI_ACCESS ); +if (rc) +dev_err (hdev->dev,"Failed to enable PCI access from CPU\n" ); +return rc; }int goya_mmap (struct hl_fpriv *hpriv,struct vm_area_struct *vma)@@ -2502,6 +3372,104 @@ int goya_cb_mmap(struct hl_device *hdev, struct vm_area_struct *vma,return rc; } +void goya_ring_doorbell (struct hl_device *hdev,u32 hw_queue_id,u32 pi) +{ +u32 db_reg_offset, db_value; +bool invalid_queue = false; + +switch (hw_queue_id) { +case GOYA_QUEUE_ID_DMA_0 : + db_reg_offset = mmDMA_QM_0_PQ_PI; +break ; + +case GOYA_QUEUE_ID_DMA_1 : + db_reg_offset = mmDMA_QM_1_PQ_PI; +break ; + +case GOYA_QUEUE_ID_DMA_2 : + db_reg_offset = mmDMA_QM_2_PQ_PI; +break ; + +case GOYA_QUEUE_ID_DMA_3 : + db_reg_offset = mmDMA_QM_3_PQ_PI; +break ; + +case GOYA_QUEUE_ID_DMA_4 : + db_reg_offset = mmDMA_QM_4_PQ_PI; +break ; + +case GOYA_QUEUE_ID_CPU_PQ : +if (hdev->cpu_queues_enable) + db_reg_offset = mmCPU_IF_PF_PQ_PI; +else + invalid_queue = true; +break ; + +case GOYA_QUEUE_ID_MME : + db_reg_offset = mmMME_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC0 : + db_reg_offset = mmTPC0_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC1 : + db_reg_offset = mmTPC1_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC2 : + db_reg_offset = mmTPC2_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC3 : + db_reg_offset = mmTPC3_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC4 : + db_reg_offset = mmTPC4_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC5 : + db_reg_offset = mmTPC5_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC6 : + db_reg_offset = mmTPC6_QM_PQ_PI; +break ; + +case GOYA_QUEUE_ID_TPC7 : + db_reg_offset = mmTPC7_QM_PQ_PI; +break ; + +default : + invalid_queue = true; + } + +if (invalid_queue) { +/* Should never get here */ +dev_err (hdev->dev,"h/w queue %d is invalid. Can't set pi\n" , + hw_queue_id); +return ; + } + + db_value = pi; + +if (hdev->ifh) +return ; + +/* ring the doorbell */ +WREG32 (db_reg_offset, db_value); + +if (hw_queue_id ==GOYA_QUEUE_ID_CPU_PQ ) +WREG32 (mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, +GOYA_ASYNC_EVENT_ID_PI_UPDATE ); +} + +void goya_flush_pq_write (struct hl_device *hdev,u64 *pq,u64 exp_val) +{ +/* Not needed in Goya */ +} +void *goya_dma_alloc_coherent (struct hl_device *hdev,size_t size,dma_addr_t *dma_handle,gfp_t flags) {@@ -2514,6 +3482,311 @@ void goya_dma_free_coherent(struct hl_device *hdev, size_t size, void *cpu_addr,dma_free_coherent (&hdev->pdev->dev, size, cpu_addr, dma_handle); } +void *goya_get_int_queue_base (struct hl_device *hdev,u32 queue_id, +dma_addr_t *dma_handle,u16 *queue_len) +{ +void *base; +u32 offset; + + *dma_handle = hdev->asic_prop.sram_base_address; + + base = hdev->pcie_bar[SRAM_CFG_BAR_ID ]; + +switch (queue_id) { +case GOYA_QUEUE_ID_MME : + offset =MME_QMAN_BASE_OFFSET ; + *queue_len =MME_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC0 : + offset =TPC0_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC1 : + offset =TPC1_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC2 : + offset =TPC2_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC3 : + offset =TPC3_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC4 : + offset =TPC4_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC5 : + offset =TPC5_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC6 : + offset =TPC6_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +case GOYA_QUEUE_ID_TPC7 : + offset =TPC7_QMAN_BASE_OFFSET ; + *queue_len =TPC_QMAN_LENGTH ; +break ; +default : +dev_err (hdev->dev,"Got invalid queue id %d\n" , queue_id); +return NULL ; + } + + base += offset; + *dma_handle += offset; + +return base; +} + +int goya_send_cpu_message (struct hl_device *hdev,u32 *msg,u16 len, +u32 timeout,long *result) +{ +struct goya_device *goya = hdev->asic_specific; +struct armcp_packet *pkt; +dma_addr_t pkt_dma_addr; +u32 tmp; +int rc =0 ; + +if (!(goya->hw_cap_initialized &HW_CAP_CPU_Q )) { +if (result) + *result =0 ; +return 0 ; + } + +if (len >CPU_CB_SIZE ) { +dev_err (hdev->dev,"Invalid CPU message size of %d bytes\n" , + len); +return -ENOMEM ; + } + + pkt = hdev->asic_funcs->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" ); +return -ENOMEM ; + } + +memcpy (pkt, msg, len); + +mutex_lock (&hdev->send_cpu_message_lock); + +if (hdev->disabled) + goto out; + + rc =hl_hw_queue_send_cb_no_cmpl (hdev,GOYA_QUEUE_ID_CPU_PQ , len, + pkt_dma_addr); +if (rc) { +dev_err (hdev->dev,"Failed to send CB on CPU PQ (%d)\n" , rc); + goto out; + } + + rc =hl_poll_timeout_memory (hdev, (u64) &pkt->fence, timeout, &tmp); + +hl_hw_queue_inc_ci_kernel (hdev,GOYA_QUEUE_ID_CPU_PQ ); + +if (rc == -ETIMEDOUT ) { +dev_err (hdev->dev, +"Timeout while waiting for CPU packet fence\n" ); + goto out;drivers/misc/habanalabs/goya/goya.c void goya_dma_free_coherent(struct hl_device *hdev, size_t size, void *cpu_addr, · line 3603
Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10
-0+ } + +if (tmp ==ARMCP_PACKET_FENCE_VAL ) { +if (pkt->rc) { +dev_err (hdev->dev, +"failed to execute CPU packet, rc: %d\n" , + pkt->rc); + rc = -EINVAL ; + }else if (result) { + *result = pkt->result;For some error cases above the *result is not initialized.
Quoted 1,631 lines
drivers/misc/habanalabs/goya/goya.c +197
-0+ } + }else { +dev_err (hdev->dev,"CPU packet wrong fence value\n" ); + rc = -EINVAL ; + } + +out: +mutex_unlock (&hdev->send_cpu_message_lock); + + hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, len, pkt); + +return rc; +} + +int goya_test_queue (struct hl_device *hdev,u32 hw_queue_id) +{ +struct packet_msg_prot *fence_pkt; +dma_addr_t pkt_dma_addr; +u32 fence_val, tmp; +dma_addr_t fence_dma_addr; +u32 *fence_ptr; +int rc; + + fence_val =GOYA_QMAN0_FENCE_VAL ; + + fence_ptr = hdev->asic_funcs->dma_pool_zalloc (hdev,4 ,GFP_KERNEL , + &fence_dma_addr); +if (!fence_ptr) { +dev_err (hdev->dev, +"Failed to allocate memory for queue testing\n" ); +return -ENOMEM ; + } + + *fence_ptr =0 ; + + fence_pkt = hdev->asic_funcs->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 queue testing\n" ); + rc = -ENOMEM ; + goto free_fence_ptr; + } + + fence_pkt->opcode =PACKET_MSG_PROT ; + fence_pkt->value = fence_val; + fence_pkt->addr = fence_dma_addr + + hdev->asic_prop.host_phys_base_address; + + rc =hl_hw_queue_send_cb_no_cmpl (hdev, hw_queue_id, +sizeof (struct packet_msg_prot ), + pkt_dma_addr); +if (rc) { +dev_err (hdev->dev, +"Failed to send fence packet\n" ); + goto free_pkt; + } + + rc =hl_poll_timeout_memory (hdev, (u64 ) fence_ptr, +GOYA_TEST_QUEUE_WAIT_USEC , &tmp); + +hl_hw_queue_inc_ci_kernel (hdev, hw_queue_id); + +if ((!rc) && (tmp == fence_val)) { +dev_info (hdev->dev, +"queue test on H/W queue %d succeeded\n" , + hw_queue_id); + }else { +dev_err (hdev->dev, +"H/W queue %d test failed (scratch(0x%08llX) == 0x%08X)\n" , + hw_queue_id, fence_dma_addr, tmp); + rc = -EINVAL ; + } + +free_pkt: + hdev->asic_funcs->dma_pool_free (hdev, (void *) fence_pkt, + pkt_dma_addr); +free_fence_ptr: + hdev->asic_funcs->dma_pool_free (hdev, (void *) fence_ptr, + fence_dma_addr); +return rc; +} + +int goya_test_cpu_queue (struct hl_device *hdev) +{ +struct armcp_packet test_pkt; +long result; +int rc; + +/* cpu_queues_enable flag is always checked in send cpu message */ + +memset (&test_pkt,0 ,sizeof (test_pkt)); + + test_pkt.opcode =ARMCP_PACKET_TEST ; + test_pkt.value =ARMCP_PACKET_FENCE_VAL ; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &test_pkt, +sizeof (test_pkt),HL_DEVICE_TIMEOUT_USEC , &result); + +if (!rc) +dev_info (hdev->dev,"queue test on CPU queue succeeded\n" ); +else +dev_err (hdev->dev,"CPU queue test failed (0x%08lX)\n" , result); + +return rc; +} + +static int goya_test_queues (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +int i, rc, ret_val =0 ; + +if (hdev->ifh) +return 0 ; + +for (i =0 ; i <NUMBER_OF_EXT_HW_QUEUES ; i++) { + rc =goya_test_queue (hdev, i); +if (rc) + ret_val = -EINVAL ; + } + +if (hdev->cpu_queues_enable) { + rc = goya->test_cpu_queue (hdev); +if (rc) + ret_val = -EINVAL ; + } + +return ret_val; +} + +void *goya_dma_pool_zalloc (struct hl_device *hdev,size_t size,gfp_t mem_flags, +dma_addr_t *dma_handle) +{ +if (size >GOYA_DMA_POOL_BLK_SIZE ) +return NULL ; + +return dma_pool_zalloc (hdev->dma_pool, mem_flags, dma_handle); +} + +void goya_dma_pool_free (struct hl_device *hdev,void *vaddr, +dma_addr_t dma_addr) +{ +dma_pool_free (hdev->dma_pool, vaddr, dma_addr); +} + +void *goya_cpu_accessible_dma_pool_alloc (struct hl_device *hdev,size_t size, +dma_addr_t *dma_handle) +{ +u64 kernel_addr; + +/* roundup to CPU_PKT_SIZE */ + size = (size + (CPU_PKT_SIZE -1 )) &CPU_PKT_MASK ; + + kernel_addr =gen_pool_alloc (hdev->cpu_accessible_dma_pool, size); + + *dma_handle = hdev->cpu_accessible_dma_address + + (kernel_addr - (u64 ) hdev->cpu_accessible_dma_mem); + +return (void *) kernel_addr; +} + +void goya_cpu_accessible_dma_pool_free (struct hl_device *hdev,size_t size, +void *vaddr) +{ +/* roundup to CPU_PKT_SIZE */ + size = (size + (CPU_PKT_SIZE -1 )) &CPU_PKT_MASK ; + +gen_pool_free (hdev->cpu_accessible_dma_pool, (u64 ) vaddr, size); +} + + +static void goya_hw_queues_lock (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; + +spin_lock (&goya->hw_queues_lock); +} + +static void goya_hw_queues_unlock (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; + +spin_unlock (&goya->hw_queues_lock); +} +static const struct hl_asic_funcs goya_funcs = { .early_init = goya_early_init, .early_fini = goya_early_fini,@@ -2525,8 +3798,19 @@ static const struct hl_asic_funcs goya_funcs = { .resume = goya_resume, .mmap = goya_mmap, .cb_mmap = goya_cb_mmap, + .ring_doorbell = goya_ring_doorbell, + .flush_pq_write = goya_flush_pq_write, .dma_alloc_coherent = goya_dma_alloc_coherent, .dma_free_coherent = goya_dma_free_coherent, + .get_int_queue_base = goya_get_int_queue_base, + .test_queues = goya_test_queues, + .dma_pool_zalloc = goya_dma_pool_zalloc, + .dma_pool_free = goya_dma_pool_free, + .cpu_accessible_dma_pool_alloc = goya_cpu_accessible_dma_pool_alloc, + .cpu_accessible_dma_pool_free = goya_cpu_accessible_dma_pool_free, + .hw_queues_lock = goya_hw_queues_lock, + .hw_queues_unlock = goya_hw_queues_unlock, + .send_cpu_message = goya_send_cpu_message };/** drivers/misc/habanalabs/goya/goyaP.h +6
-0@@ -9,6 +9,7 @@#define GOYAP_H_ #include "habanalabs.h" +#include "include/goya/goya_packets.h" #include "include/goya/goya_boot_if.h" #include "include/goya/goya.h" @@ -117,12 +118,17 @@ enum goya_fw_component { };struct goya_device { +int (*test_cpu_queue)(struct hl_device *hdev); +/* TODO: remove hw_queues_lock after moving to scheduler code */ spinlock_t hw_queues_lock;u64 ddr_bar_cur_addr;u32 hw_cap_initialized; }; +int goya_test_cpu_queue (struct hl_device *hdev); +int goya_send_cpu_message (struct hl_device *hdev,u32 *msg,u16 len, +u32 timeout,long *result);void goya_init_security (struct hl_device *hdev);#endif /* GOYAP_H_ */ drivers/misc/habanalabs/habanalabs.h +175
-1@@ -30,10 +30,36 @@struct hl_device ;struct hl_fpriv ; +/** +* enum hl_queue_type - Supported QUEUE types. +* @QUEUE_TYPE_NA: queue is not available. +* @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the +* host. +* @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's +* memories and/or operates the compute engines. +* @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU. +*/ +enum hl_queue_type { +QUEUE_TYPE_NA , +QUEUE_TYPE_EXT , +QUEUE_TYPE_INT , +QUEUE_TYPE_CPU +}; +/** +* struct hw_queue_properties - queue information. +* @type: queue type. +* @kmd_only: true if only KMD is allowed to send a job to this queue, false +* otherwise. +*/ +struct hw_queue_properties { +enum hl_queue_type type; +u8 kmd_only; +};/** * struct asic_fixed_properties - ASIC specific immutable properties. +* @hw_queues_props: H/W queues properties. * @uboot_ver: F/W U-boot version. * @preboot_ver: F/W Preboot version. * @sram_base_address: SRAM physical start address. @@ -64,6 +90,7 @@ struct hl_fpriv;* @tpc_enabled_mask: which TPCs are enabled. */ struct asic_fixed_properties { +struct hw_queue_properties hw_queues_props[HL_MAX_QUEUES ];char uboot_ver[VERSION_MAX_LEN ];char preboot_ver[VERSION_MAX_LEN ];u64 sram_base_address;@@ -145,7 +172,92 @@ struct hl_cb { +/* +* QUEUES +*/ + +struct hl_cs_job ; + +/* +* Currently, there are two limitations on the maximum length of a queue: +* +* 1. The memory footprint of the queue. The current allocated space for the +* queue is PAGE_SIZE. Because each entry in the queue is HL_BD_SIZE, +* the maximum length of the queue can be PAGE_SIZE / HL_BD_SIZE, +* which currently is 4096/16 = 256 entries. +* +* To increase that, we need either to decrease the size of the +* BD (difficult), or allocate more than a single page (easier). +* +* 2. Because the size of the JOB handle field in the BD CTL / completion queue +* is 10-bit, we can have up to 1024 open jobs per hardware queue. +* Therefore, each queue can hold up to 1024 entries. +* +* HL_QUEUE_LENGTH is in units of struct hl_bd. +* HL_QUEUE_LENGTH * sizeof(struct hl_bd) should be <= HL_PAGE_SIZE +*/ + +#define HL_PAGE_SIZE 4096/* minimum page size */ +/* Must be power of 2 (HL_PAGE_SIZE / HL_BD_SIZE) */ #define HL_QUEUE_LENGTH 256 +#define HL_QUEUE_SIZE_IN_BYTES (HL_QUEUE_LENGTH * HL_BD_SIZE) + +/* +* HL_CQ_LENGTH is in units of struct hl_cq_entry. +* HL_CQ_LENGTH should be <= HL_PAGE_SIZE +*/ +#define HL_CQ_LENGTH HL_QUEUE_LENGTH +#define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE) + + + +/** +* struct hl_hw_queue - describes a H/W transport queue. +* @shadow_queue: pointer to a shadow queue that holds pointers to jobs. +* @queue_type: type of queue. +* @kernel_address: holds the queue's kernel virtual address. +* @bus_address: holds the queue's DMA address. +* @pi: holds the queue's pi value. +* @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci). +* @hw_queue_id: the id of the H/W queue. +* @int_queue_len: length of internal queue (number of entries). +* @valid: is the queue valid (we have array of 32 queues, not all of them +* exists). +*/ +struct hl_hw_queue { +struct hl_cs_job **shadow_queue; +enum hl_queue_type queue_type; +u64 kernel_address; +dma_addr_t bus_address; +u32 pi; +u32 ci; +u32 hw_queue_id; +u16 int_queue_len; +u8 valid; +}; + +/** +* struct hl_cq - describes a completion queue +* @hdev: pointer to the device structure +* @kernel_address: holds the queue's kernel virtual address +* @bus_address: holds the queue's DMA address +* @hw_queue_id: the id of the matching H/W queue +* @ci: ci inside the queue +* @pi: pi inside the queue +* @free_slots_cnt: counter of free slots in queue +*/ +struct hl_cq { +struct hl_device *hdev; +u64 kernel_address; +dma_addr_t bus_address; +u32 hw_queue_id; +u32 ci; +u32 pi; +atomic_t free_slots_cnt; +}; + + +/* @@ -180,8 +292,20 @@ enum hl_asic_type {* @resume: handles IP specific H/W or SW changes for resume. * @mmap: mmap function, does nothing. * @cb_mmap: maps a CB. +* @ring_doorbell: increment PI on a given QMAN. +* @flush_pq_write: flush PQ entry write if necessary, WARN if flushing failed. * @dma_alloc_coherent: DMA allocate coherent memory. * @dma_free_coherent: free DMA allocation. +* @get_int_queue_base: get the internal queue base address. +* @test_queues: run simple test on all queues for sanity check. +* @dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool. +* size of allocation is HL_DMA_POOL_BLK_SIZE. +* @dma_pool_free: free small DMA allocation from pool. +* @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool. +* @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool. +* @hw_queues_lock: acquire H/W queues lock. +* @hw_queues_unlock: release H/W queues lock. +* @send_cpu_message: send buffer to ArmCP. */ struct hl_asic_funcs {int (*early_init)(struct hl_device *hdev);@@ -195,10 +319,27 @@ struct hl_asic_funcs {int (*mmap)(struct hl_fpriv *hpriv,struct vm_area_struct *vma);int (*cb_mmap)(struct hl_device *hdev,struct vm_area_struct *vma,u64 kaddress,phys_addr_t paddress,u32 size); +void (*ring_doorbell)(struct hl_device *hdev,u32 hw_queue_id,u32 pi); +void (*flush_pq_write)(struct hl_device *hdev,u64 *pq,u64 exp_val);void * (*dma_alloc_coherent)(struct hl_device *hdev,size_t size,dma_addr_t *dma_handle,gfp_t flag);void (*dma_free_coherent)(struct hl_device *hdev,size_t size,void *cpu_addr,dma_addr_t dma_handle); +void * (*get_int_queue_base)(struct hl_device *hdev,u32 queue_id, +dma_addr_t *dma_handle,u16 *queue_len); +int (*test_queues)(struct hl_device *hdev); +void * (*dma_pool_zalloc)(struct hl_device *hdev,size_t size, +gfp_t mem_flags,dma_addr_t *dma_handle); +void (*dma_pool_free)(struct hl_device *hdev,void *vaddr, +dma_addr_t dma_addr); +void * (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev, +size_t size,dma_addr_t *dma_handle); +void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev, +size_t size,void *vaddr); +void (*hw_queues_lock)(struct hl_device *hdev); +void (*hw_queues_unlock)(struct hl_device *hdev); +int (*send_cpu_message)(struct hl_device *hdev,u32 *msg, +u16 len,u32 timeout,long *result); };@@ -240,6 +381,17 @@ struct hl_ctx_mgr { +/** +* struct hl_cs_job - command submission job. +* @finish_work: workqueue object to run when job is completed. +* @id: the id of this job inside a CS. +*/ +struct hl_cs_job { +struct work_struct finish_work; +u32 id; +}; + +/* * FILE PRIVATE STRUCTURE */ @@ -316,7 +468,11 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @dev: realted kernel basic device structure. * @asic_name: ASIC specific nmae. * @asic_type: ASIC specific type. +* @completion_queue: array of hl_cq. +* @cq_wq: work queue of completion queues for executing work in process context +* @eq_wq: work queue of event queue for executing work in process context. * @kernel_ctx: KMD context structure. +* @kernel_queues: array of hl_hw_queue. * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs. * @dma_pool: DMA pool for small allocations. * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address. @@ -326,6 +482,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @asid_bitmap: holds used/available ASIDs. * @asid_mutex: protects asid_bitmap. * @device_open: lock for sanity checks upon FD open. +* @send_cpu_message_lock: enforces only one message in KMD <-> ArmCP queue. * @asic_prop: ASIC specific immutable properties. * @asic_funcs: ASIC specific functions. * @asic_specific: ASIC specific information to use only from ASIC files. @@ -345,7 +502,10 @@ struct hl_device {struct device *dev;char asic_name[16 ];enum hl_asic_type asic_type; +struct hl_cq *completion_queue; +struct workqueue_struct *cq_wq;struct hl_ctx *kernel_ctx; +struct hl_hw_queue *kernel_queues;struct hl_cb_mgr kernel_cb_mgr;struct dma_pool *dma_pool;void *cpu_accessible_dma_mem;@@ -356,6 +516,7 @@ struct hl_device {struct mutex asid_mutex;/* TODO: change to rw_sem for multiple contexts (same as other IOCTL) */ struct mutex device_open; +struct mutex send_cpu_message_lock;struct asic_fixed_properties asic_prop;const struct hl_asic_funcs *asic_funcs;void *asic_specific;@@ -374,7 +535,9 @@ struct hl_device {u8 cpu_enable;u8 reset_pcilink;u8 config_pll; +u8 cpu_queues_enable;u8 fw_loading; +u8 ifh;u8 pldm; };@@ -418,7 +581,18 @@ int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr, u32 timeout_us, u32 *val);int hl_poll_timeout_device_memory (struct hl_device *hdev,void __iomem *addr,u32 timeout_us,u32 *val); - +int hl_hw_queues_create (struct hl_device *hdev); +void hl_hw_queues_destroy (struct hl_device *hdev); +int hl_hw_queue_send_cb_no_cmpl (struct hl_device *hdev,u32 hw_queue_id, +u32 cb_size,u64 cb_ptr); +u32 hl_hw_queue_add_ptr (u32 ptr,u16 val); +void hl_hw_queue_inc_ci_kernel (struct hl_device *hdev,u32 hw_queue_id); + +#define hl_queue_inc_ptr (p) hl_hw_queue_add_ptr(p, 1) +#define hl_pi_2_offset (pi) ((pi) & (HL_QUEUE_LENGTH - 1)) + +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);int hl_asid_init (struct hl_device *hdev);void hl_asid_fini (struct hl_device *hdev);unsigned long hl_asid_alloc (struct hl_device *hdev);drivers/misc/habanalabs/habanalabs_drv.c +6
-0@@ -184,13 +184,19 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev, hdev->cpu_enable =1 ; hdev->reset_pcilink =0 ; hdev->config_pll =0 ; + hdev->cpu_queues_enable =1 ; hdev->fw_loading =1 ; + hdev->ifh =0 ; hdev->pldm =0 ;/* If CPU is disabled, no point in loading FW */ if (!hdev->cpu_enable) hdev->fw_loading =0 ; +/* If we don't load FW, no need to initialize CPU queues */ +if (!hdev->fw_loading) + hdev->cpu_queues_enable =0 ; + hdev->disabled = true; hdev->pdev = pdev;/* can be NULL in case of simulator device */ drivers/misc/habanalabs/hw_queue.c new file +404
-0@@ -0,0 +1,404 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include "habanalabs.h" + +#include <linux/dma-mapping.h> +#include <linux/sched.h> +#include <linux/wait.h> +#include <linux/delay.h> + +/** +* hl_queue_add_ptr - add to pi or ci and checks if it wraps around +* +* @ptr: the current pi/ci value +* @val: the amount to add +* +* Add val to ptr. It can go until twice the queue length. +*/ +inline u32 hl_hw_queue_add_ptr (u32 ptr,u16 val) +{ + ptr += val; + ptr &= ((HL_QUEUE_LENGTH <<1 ) -1 ); +return ptr; +} + +static inline int queue_free_slots (struct hl_hw_queue *q,u32 queue_len) +{ +int delta = (q->pi - q->ci); + +if (delta >=0 ) +return (queue_len - delta); +else +return (abs (delta) - queue_len); +} + +/** +* ext_queue_submit_bd - Submit a buffer descriptor to an external queue +* +* @hdev: pointer to habanalabs device structure +* @q: pointer to habanalabs queue structure +* @ctl: BD's control word +* @len: BD's length +* @ptr: BD's pointer +* +* This function assumes there is enough space on the queue to submit a new +* BD to it. It initializes the next BD and calls the device specific +* function to set the pi (and doorbell) +* +* This function must be called when the scheduler mutex is taken +* +*/ +static void ext_queue_submit_bd (struct hl_device *hdev,struct hl_hw_queue *q, +u32 ctl,u32 len,u64 ptr) +{ +struct hl_bd *bd; + + bd = (struct hl_bd *) q->kernel_address; + bd +=hl_pi_2_offset (q->pi); + bd->ctl = ctl; + bd->len = len; + bd->ptr = ptr + hdev->asic_prop.host_phys_base_address; + + q->pi =hl_queue_inc_ptr (q->pi); + hdev->asic_funcs->ring_doorbell (hdev, q->hw_queue_id, q->pi); +} + +/** +* ext_queue_sanity_checks - perform some sanity checks on external queue +* +* @hdev : pointer to hl_device structure +* @q : pointer to hl_hw_queue structure +* @num_of_entries : how many entries to check for space +* @reserve_cq_entry : whether to reserve an entry in the cq +* +* H/W queues spinlock should be taken before calling this function +* +* Perform the following: +* - Make sure we have enough space in the h/w queue +* - Make sure we have enough space in the completion queue +* - Reserve space in the completion queue (needs to be reversed if there +* is a failure down the road before the actual submission of work). Only +* do this action if reserve_cq_entry is true +* +*/ +static int ext_queue_sanity_checks (struct hl_device *hdev, +struct hl_hw_queue *q,int num_of_entries, +bool reserve_cq_entry) +{ +atomic_t *free_slots = + &hdev->completion_queue[q->hw_queue_id].free_slots_cnt; +int free_slots_cnt; + +/* Check we have enough space in the queue */ + free_slots_cnt =queue_free_slots (q,HL_QUEUE_LENGTH ); + +if (free_slots_cnt < num_of_entries) { +dev_dbg (hdev->dev,"Queue %d doesn't have room for %d CBs\n" , + q->hw_queue_id, num_of_entries); +return -EAGAIN ; + } + +if (reserve_cq_entry) { +/* +* Check we have enough space in the completion queue +* Add -1 to counter (decrement) unless counter was already 0 +* In that case, CQ is full so we can't submit a new CB because +* we won't get ack on its completion +* atomic_add_unless will return 0 if counter was already 0 +*/ +if (atomic_add_negative (num_of_entries *-1 , free_slots)) { +dev_dbg (hdev->dev,"No space for %d on CQ %d\n" , + num_of_entries, q->hw_queue_id); +atomic_add (num_of_entries, free_slots); +return -EAGAIN ; + } + } + +return 0 ; +} + +/** +* hl_hw_queue_send_cb_no_cmpl - send a single CB (not a JOB) without completion +* +* @hdev: pointer to hl_device structure +* @hw_queue_id: Queue's type +* @cb_size: size of CB +* @cb_ptr: pointer to CB location +* +* This function sends a single CB, that must NOT generate a completion entry +* +*/ +int hl_hw_queue_send_cb_no_cmpl (struct hl_device *hdev,u32 hw_queue_id, +u32 cb_size,u64 cb_ptr) +{ +struct hl_hw_queue *q = &hdev->kernel_queues[hw_queue_id]; +int rc; + +/* +* The CPU queue is a synchronous queue with an effective depth of +* a single entry (although it is allocated with room for multiple +* entries). Therefore, there is a different lock, called +* send_cpu_message_lock, that serializes accesses to the CPU queue. +* As a result, we don't need to lock the access to the entire H/W +* queues module when submitting a JOB to the CPU queue +*/ +if (q->queue_type !=QUEUE_TYPE_CPU ) + hdev->asic_funcs->hw_queues_lock (hdev); + +if (hdev->disabled) { + rc = -EPERM ; + goto out; + } + + rc =ext_queue_sanity_checks (hdev, q,1 , false); +if (rc) + goto out; + +ext_queue_submit_bd (hdev, q,0 , cb_size, cb_ptr); + +out: +if (q->queue_type !=QUEUE_TYPE_CPU ) + hdev->asic_funcs->hw_queues_unlock (hdev); + +return rc; +} + +/** +* hl_hw_queue_inc_ci_kernel - increment ci for kernel's queue +* +* @hdev: pointer to hl_device structure +* @hw_queue_id: which queue to increment its ci +*/ +void hl_hw_queue_inc_ci_kernel (struct hl_device *hdev,u32 hw_queue_id) +{ +struct hl_hw_queue *q = &hdev->kernel_queues[hw_queue_id]; + + q->ci =hl_queue_inc_ptr (q->ci); +} + +static int ext_and_cpu_hw_queue_init (struct hl_device *hdev, +struct hl_hw_queue *q) +{ +void *p; +int rc; + + p = hdev->asic_funcs->dma_alloc_coherent (hdev, +HL_QUEUE_SIZE_IN_BYTES , + &q->bus_address,GFP_KERNEL | __GFP_ZERO); +if (!p) +return -ENOMEM ; + + q->kernel_address = (u64 ) p; + + q->shadow_queue =kmalloc_array (HL_QUEUE_LENGTH , +sizeof (*q->shadow_queue), +GFP_KERNEL ); +if (!q->shadow_queue) { +dev_err (hdev->dev, +"Failed to allocate shadow queue for H/W queue %d\n" , + q->hw_queue_id); + rc = -ENOMEM ; + goto free_queue; + } + +/* Make sure read/write pointers are initialized to start of queue */ + q->ci =0 ; + q->pi =0 ; + +return 0 ; + +free_queue: + hdev->asic_funcs->dma_free_coherent (hdev,HL_QUEUE_SIZE_IN_BYTES , + (void *) q->kernel_address, q->bus_address); + +return rc; +} + +static int int_hw_queue_init (struct hl_device *hdev,struct hl_hw_queue *q) +{ +void *p; + + p = hdev->asic_funcs->get_int_queue_base (hdev, q->hw_queue_id, + &q->bus_address, &q->int_queue_len); +if (!p) { +dev_err (hdev->dev, +"Failed to get base address for internal queue %d\n" , + q->hw_queue_id); +return -EFAULT ; + } + + q->kernel_address = (u64 ) p; + q->pi =0 ; + q->ci =0 ; + +return 0 ; +} + +static int cpu_hw_queue_init (struct hl_device *hdev,struct hl_hw_queue *q) +{ +return ext_and_cpu_hw_queue_init (hdev, q); +} + +static int ext_hw_queue_init (struct hl_device *hdev,struct hl_hw_queue *q) +{ +return ext_and_cpu_hw_queue_init (hdev, q); +} + +/** +* hw_queue_init - main initialization function for H/W queue object +* +* @hdev: pointer to hl_device device structure +* @q: pointer to hl_hw_queue queue structure +* @hw_queue_id: The id of the H/W queue +* +* Allocate dma-able memory for the queue and initialize fields +* Returns 0 on success +*/ +static int hw_queue_init (struct hl_device *hdev,struct hl_hw_queue *q, +u32 hw_queue_id) +{ +int rc; + +BUILD_BUG_ON (HL_QUEUE_SIZE_IN_BYTES >HL_PAGE_SIZE ); + + q->hw_queue_id = hw_queue_id; + +switch (q->queue_type) { +case QUEUE_TYPE_EXT : + rc =ext_hw_queue_init (hdev, q); +break ; + +case QUEUE_TYPE_INT : + rc =int_hw_queue_init (hdev, q); +break ; + +case QUEUE_TYPE_CPU : + rc =cpu_hw_queue_init (hdev, q); +break ; + +case QUEUE_TYPE_NA : + q->valid =0 ; +return 0 ; + +default : +dev_crit (hdev->dev,"wrong queue type %d during init\n" , + q->queue_type); + rc = -EINVAL ; +break ; + } + +if (rc) +return rc; + + q->valid =1 ; + +return 0 ; +} + +/** +* hw_queue_fini - destroy queue +* +* @hdev: pointer to hl_device device structure +* @q: pointer to hl_hw_queue queue structure +* +* Free the queue memory +*/ +static void hw_queue_fini (struct hl_device *hdev,struct hl_hw_queue *q) +{ +if (!q->valid) +return ; + +/* +* If we arrived here, there are no jobs waiting on this queue +* so we can safely remove it. +* This is because this function can only called when: +* 1. Either a context is deleted, which only can occur if all its +* jobs were finished +* 2. A context wasn't able to be created due to failure or timeout, +* which means there are no jobs on the queue yet +* +* The only exception are the queues of the kernel context, but +* if they are being destroyed, it means that the entire module is +* being removed. If the module is removed, it means there is no open +* user context. It also means that if a job was submitted by +* the kernel driver (e.g. context creation), the job itself was +* released by the kernel driver when a timeout occurred on its +* Completion. Thus, we don't need to release it again. +*/ + +if (q->queue_type ==QUEUE_TYPE_INT ) +return ; + +kfree (q->shadow_queue); + + hdev->asic_funcs->dma_free_coherent (hdev, +HL_QUEUE_SIZE_IN_BYTES , + (void *) q->kernel_address, q->bus_address); +} + +int hl_hw_queues_create (struct hl_device *hdev) +{ +struct asic_fixed_properties *asic = &hdev->asic_prop; +struct hl_hw_queue *q; +int i, rc, q_ready_cnt; + + hdev->kernel_queues =kcalloc (HL_MAX_QUEUES , +sizeof (*hdev->kernel_queues),GFP_KERNEL ); + +if (!hdev->kernel_queues) { +dev_err (hdev->dev,"Not enough memory for H/W queues\n" ); +return -ENOMEM ; + } + +/* Initialize the H/W queues */ +for (i =0 , q_ready_cnt =0 , q = hdev->kernel_queues; + i <HL_MAX_QUEUES ; i++, q_ready_cnt++, q++) { + + q->queue_type = asic->hw_queues_props[i].type; + rc =hw_queue_init (hdev, q, i); +if (rc) { +dev_err (hdev->dev, +"failed to initialize queue %d\n" , i); + goto release_queues; + } + } + +return 0 ; + +release_queues: +for (i =0 , q = hdev->kernel_queues ; i < q_ready_cnt ; i++, q++) +hw_queue_fini (hdev, q); + +kfree (hdev->kernel_queues); + +return rc; +} + +void hl_hw_queues_destroy (struct hl_device *hdev) +{ +struct hl_hw_queue *q; +int i; + +for (i =0 , q = hdev->kernel_queues ; i <HL_MAX_QUEUES ; i++, q++) +hw_queue_fini (hdev, q); + +kfree (hdev->kernel_queues); +} + +void hl_hw_queue_reset (struct hl_device *hdev,bool hard_reset) +{ +struct hl_hw_queue *q; +int i; + +for (i =0 , q = hdev->kernel_queues ; i <HL_MAX_QUEUES ; i++, q++) { +if ((!q->valid) || + ((!hard_reset) && (q->queue_type ==QUEUE_TYPE_CPU ))) +continue ; + q->pi = q->ci =0 ; + } +}drivers/misc/habanalabs/include/goya/goya_packets.h new file +234
-0@@ -0,0 +1,234 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* Copyright 2017-2018 HabanaLabs, Ltd. +* All Rights Reserved. +* +* Authors: +* +* Oded Gabbay <[email protected]> +* Guy Eilat <[email protected]> +* +*/ + +#ifndef GOYA_PACKETS_H +#define GOYA_PACKETS_H + +#include <linux/types.h> + +#define PACKET_HEADER_PACKET_ID_SHIFT 56 +#define PACKET_HEADER_PACKET_ID_MASK 0x1F00000000000000ull + +enum packet_id { +PACKET_WREG_32 =0x1 , +PACKET_WREG_BULK =0x2 , +PACKET_MSG_LONG =0x3 , +PACKET_MSG_SHORT =0x4 , +PACKET_CP_DMA =0x5 , +PACKET_MSG_PROT =0x7 , +PACKET_FENCE =0x8 , +PACKET_LIN_DMA =0x9 , +PACKET_NOP =0xA , +PACKET_STOP =0xB , +MAX_PACKET_ID = (PACKET_HEADER_PACKET_ID_MASK >> +PACKET_HEADER_PACKET_ID_SHIFT ) +1 +}; + +enum goya_dma_direction { +DMA_HOST_TO_DRAM , +DMA_HOST_TO_SRAM , +DMA_DRAM_TO_SRAM , +DMA_SRAM_TO_DRAM , +DMA_SRAM_TO_HOST , +DMA_DRAM_TO_HOST , +DMA_DRAM_TO_DRAM , +DMA_SRAM_TO_SRAM , +DMA_ENUM_MAX +}; + +struct packet_nop { +__u32 reserved; +union { +struct { + __u32:24; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +}; + +struct packet_stop { +__u32 reserved; +union { +struct { + __u32:24; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 0 */ +__u32 msg_barrier :1 ;/* must be 0 */ + }; +__u32 ctl; + }; +}; + +struct packet_wreg32 { +__u32 value; +union { +struct { +__u32 reg_offset :16 ; + __u32:7; +__u32 local :1 ;/* 0: write to TCL regs, +* 1: write to CMDQ regs +*/ +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 1 */ +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +}; + +struct packet_wreg_bulk { +__u32 size64 :16 ; + __u32:16; +__u32 reg_offset :16 ; + __u32:8; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 1 */ +__u32 msg_barrier :1 ; +__u64 values[0 ];/* data starts here */ +}; + +struct packet_msg_long { +__u32 value; +union { +struct { + __u32:16; +__u32 weakly_ordered :1 ; +__u32 no_snoop :1 ; + __u32:2; +__u32 op :2 ;/* 0: write <value>. 1: write timestamp. */ + __u32:2; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +__u64 addr; +}; + +struct packet_msg_short { +union { +struct { +__u32 sync_id :10 ; + __u32:5; +__u32 mode :1 ; +__u32 sync_value :16 ; + } mon_arm_register; +struct { +__u32 sync_value :16 ; + __u32:15; +__u32 mode :1 ; + } so_upd; +__u32 value; + }; +union { +struct { +__u32 msg_addr_offset :16 ; +__u32 weakly_ordered :1 ; +__u32 no_snoop :1 ; + __u32:2; +__u32 op :2 ; +__u32 base :2 ; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +}; + +struct packet_msg_prot { +__u32 value; +union { +struct { + __u32:16; +__u32 weakly_ordered :1 ; +__u32 no_snoop :1 ; + __u32:2; +__u32 op :2 ;/* 0: write <value>. 1: write timestamp. */ + __u32:2; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +__u64 addr; +}; + +struct packet_fence { +__u32 dec_val :4 ; + __u32:12; +__u32 gate_val :8 ; + __u32:6; +__u32 id :2 ; + __u32:24; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; +}; + +struct packet_lin_dma { +__u32 tsize; +union { +struct { +__u32 weakly_ordered :1 ;/* H/W bug, must be 1 */ +__u32 rdcomp :1 ; +__u32 wrcomp :1 ; +__u32 no_snoop :1 ; +__u32 src_disable :1 ; +__u32 dst_disable :1 ; +__u32 memset_mode :1 ; +__u32 tensor_dma :1 ;/* N/A, must be 0 */ +__u32 cntrl :12 ; +__u32 dma_dir :3 ;/* S/W only, no effect on HW */ + __u32:1; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 1 */ +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +__u64 src_addr; +__u64 dst_addr; +}; + +struct packet_cp_dma { +__u32 tsize; +union { +struct { +__u32 weakly_ordered :1 ; +__u32 no_snoop :1 ; + __u32:22; +__u32 opcode :5 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ;/* must be 1 */ +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; +__u64 src_addr; +}; + +#endif /* GOYA_PACKETS_H */ drivers/misc/habanalabs/include/habanalabs_device_if.h +272
-0@@ -97,6 +97,278 @@ enum pq_init_status {PQ_INIT_STATUS_READY_FOR_HOST }; +/* +* ArmCP Primary Queue Packets +* +* During normal operation, KMD needs to send various messages to ArmCP, +* usually either to SET some value into a H/W periphery or to GET the current +* value of some H/W periphery. For example, SET the frequency of MME/TPC and +* GET the value of the thermal sensor. +* +* These messages can be initiated either by the User application or by KMD +* itself, e.g. power management code. In either case, the communication from +* KMD to ArmCP will *always* be in synchronous mode, meaning that KMD will +* send a single message and poll until the message was acknowledged and the +* results are ready (if results are needed). +* +* This means that only a single message can be sent at a time and KMD must +* wait for its result before sending the next message. Having said that, +* because these are control messages which are sent in a relatively low +* frequency, this limitation seems acceptable. It's important to note that +* in case of multiple devices, messages to different devices *can* be sent +* at the same time. +* +* The message, inputs/outputs (if relevant) and fence object will be located +* on the device DDR at an address that will be determined by KMD. During +* device initialization phase, KMD will pass to ArmCP that address. Most of +* the message types will contain inputs/outputs inside the message itself. +* The common part of each message will contain the opcode of the message (its +* type) and a field representing a fence object. +* +* When KMD wishes to send a message to ArmCP, it will write the message +* contents to the device DDR, clear the fence object and then write the +* value 484 to the mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR register to issue +* the 484 interrupt-id to the ARM core. +* +* Upon receiving the 484 interrupt-id, ArmCP will read the message from the +* DDR. In case the message is a SET operation, ArmCP will first perform the +* operation and then write to the fence object on the device DDR. In case the +* message is a GET operation, ArmCP will first fill the results section on the +* device DDR and then write to the fence object. If an error occurred, ArmCP +* will fill the rc field with the right error code. +* +* In the meantime, KMD will poll on the fence object. Once KMD sees that the +* fence object is signaled, it will read the results from the device DDR +* (if relevant) and resume the code execution in KMD. +* +* To use QMAN packets, the opcode must be the QMAN opcode, shifted by 8 +* so the value being put by the KMD matches the value read by ArmCP +* +* Non-QMAN packets should be limited to values 1 through (2^8 - 1) +* +* Detailed description: +* +* ARMCP_PACKET_DISABLE_PCI_ACCESS - +* After receiving this packet the embedded CPU must NOT issue PCI +* transactions (read/write) towards the Host CPU. This also include +* sending MSI-X interrupts. +* This packet is usually sent before the device is moved to D3Hot state. +* +* ARMCP_PACKET_ENABLE_PCI_ACCESS - +* After receiving this packet the embedded CPU is allowed to issue PCI +* transactions towards the Host CPU, including sending MSI-X interrupts. +* This packet is usually send after the device is moved to D0 state. +* +* ARMCP_PACKET_TEMPERATURE_GET - +* Fetch the current temperature / Max / Max Hyst / Critical / +* Critical Hyst of a specified thermal sensor. The packet's +* arguments specify the desired sensor and the field to get. +* +* ARMCP_PACKET_VOLTAGE_GET - +* Fetch the voltage / Max / Min of a specified sensor. The packet's +* arguments specify the sensor and type. +* +* ARMCP_PACKET_CURRENT_GET - +* Fetch the current / Max / Min of a specified sensor. The packet's +* arguments specify the sensor and type. +* +* ARMCP_PACKET_FAN_SPEED_GET - +* Fetch the speed / Max / Min of a specified fan. The packet's +* arguments specify the sensor and type. +* +* ARMCP_PACKET_PWM_GET - +* Fetch the pwm value / mode of a specified pwm. The packet's +* arguments specify the sensor and type. +* +* ARMCP_PACKET_PWM_SET - +* Set the pwm value / mode of a specified pwm. The packet's +* arguments specify the sensor, type and value. +* +* ARMCP_PACKET_FREQUENCY_SET - +* Set the frequency of a specified PLL. The packet's arguments specify +* the PLL and the desired frequency. The actual frequency in the device +* might differ from the requested frequency. +* +* ARMCP_PACKET_FREQUENCY_GET - +* Fetch the frequency of a specified PLL. The packet's arguments specify +* the PLL. +* +* ARMCP_PACKET_LED_SET - +* Set the state of a specified led. The packet's arguments +* specify the led and the desired state. +* +* ARMCP_PACKET_I2C_WR - +* Write 32-bit value to I2C device. The packet's arguments specify the +* I2C bus, address and value. +* +* ARMCP_PACKET_I2C_RD - +* Read 32-bit value from I2C device. The packet's arguments specify the +* I2C bus and address. +* +* ARMCP_PACKET_INFO_GET - +* Fetch information from the device as specified in the packet's +* structure. KMD passes the max size it allows the ArmCP to write to +* the structure, to prevent data corruption in case of mismatched +* KMD/FW versions. +* +* ARMCP_PACKET_FLASH_PROGRAM_REMOVED - this packet was removed +* +* ARMCP_PACKET_UNMASK_RAZWI_IRQ - +* Unmask the given IRQ. The IRQ number is specified in the value field. +* The packet is sent after receiving an interrupt and printing its +* relevant information. +* +* ARMCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY - +* Unmask the given IRQs. The IRQs numbers are specified in an array right +* after the armcp_packet structure, where its first element is the array +* length. The packet is sent after a soft reset was done in order to +* handle any interrupts that were sent during the reset process. +* +* ARMCP_PACKET_TEST - +* Test packet for ArmCP connectivity. The CPU will put the fence value +* in the result field. +* +* ARMCP_PACKET_FREQUENCY_CURR_GET - +* Fetch the current frequency of a specified PLL. The packet's arguments +* specify the PLL. +* +* ARMCP_PACKET_MAX_POWER_GET - +* Fetch the maximal power of the device. +* +* ARMCP_PACKET_MAX_POWER_SET - +* Set the maximal power of the device. The packet's arguments specify +* the power. +* +* ARMCP_PACKET_EEPROM_DATA_GET - +* Get EEPROM data from the ArmCP kernel. The buffer is specified in the +* addr field. The CPU will put the returned data size in the result +* field. In addition, KMD passes the max size it allows the ArmCP to +* write to the structure, to prevent data corruption in case of +* mismatched KMD/FW versions. +* +*/ + +enum armcp_packet_id { +ARMCP_PACKET_DISABLE_PCI_ACCESS =1 ,/* internal */ +ARMCP_PACKET_ENABLE_PCI_ACCESS ,/* internal */ +ARMCP_PACKET_TEMPERATURE_GET ,/* sysfs */ +ARMCP_PACKET_VOLTAGE_GET ,/* sysfs */ +ARMCP_PACKET_CURRENT_GET ,/* sysfs */ +ARMCP_PACKET_FAN_SPEED_GET ,/* sysfs */ +ARMCP_PACKET_PWM_GET ,/* sysfs */ +ARMCP_PACKET_PWM_SET ,/* sysfs */ +ARMCP_PACKET_FREQUENCY_SET ,/* sysfs */ +ARMCP_PACKET_FREQUENCY_GET ,/* sysfs */ +ARMCP_PACKET_LED_SET ,/* debugfs */ +ARMCP_PACKET_I2C_WR ,/* debugfs */ +ARMCP_PACKET_I2C_RD ,/* debugfs */ +ARMCP_PACKET_INFO_GET ,/* IOCTL */ +ARMCP_PACKET_FLASH_PROGRAM_REMOVED , +ARMCP_PACKET_UNMASK_RAZWI_IRQ ,/* internal */ +ARMCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY ,/* internal */ +ARMCP_PACKET_TEST ,/* internal */ +ARMCP_PACKET_FREQUENCY_CURR_GET ,/* sysfs */ +ARMCP_PACKET_MAX_POWER_GET ,/* sysfs */ +ARMCP_PACKET_MAX_POWER_SET ,/* sysfs */ +ARMCP_PACKET_EEPROM_DATA_GET ,/* sysfs */ +}; + +#define ARMCP_PACKET_FENCE_VAL 0xFE8CE7A5 + +struct armcp_packet { +union { +__u64 value;/* For SET packets */ +__u64 result;/* For GET packets */ +__u64 addr;/* For PQ */ + }; + +union { +struct { + __u32:12; +__u32 rc :4 ; +__u32 opcode :13 ; +__u32 eng_barrier :1 ; +__u32 reg_barrier :1 ; +__u32 msg_barrier :1 ; + }; +__u32 ctl; + }; + +__u32 fence;/* Signal to KMD that message is completed */ + +union { +struct {/* For temperature/current/voltage/fan/pwm get/set */ +__u16 sensor_index; +__u16 type; + }; + +struct {/* For I2C read/write */ +__u8 i2c_bus; +__u8 i2c_addr; +__u8 i2c_reg; +__u8 pad;/* unused */ + }; + +/* For frequency get/set */ +__u32 pll_index; + +/* For led set */ +__u32 led_index; + +/* For get Armcp info/EEPROM data */ +__u32 data_max_size; + }; +}; + +struct armcp_unmask_irq_arr_packet { +struct armcp_packet armcp_pkt; +__u32 length; +__u32 irqs[0 ]; +}; + +enum armcp_packet_rc { + armcp_packet_success, + armcp_packet_invalid, + armcp_packet_fault +}; + +enum armcp_temp_type { + armcp_temp_input, + armcp_temp_max =6 , + armcp_temp_max_hyst, + armcp_temp_crit, + armcp_temp_crit_hyst +}; + +enum armcp_in_attributes { + armcp_in_input, + armcp_in_min, + armcp_in_max +}; + +enum armcp_curr_attributes { + armcp_curr_input, + armcp_curr_min, + armcp_curr_max +}; + +enum armcp_fan_attributes { + armcp_fan_input, + armcp_fan_min =2 , + armcp_fan_max +}; + +enum armcp_pwm_attributes { + armcp_pwm_input, + armcp_pwm_enable +}; + +/* Event Queue Packets */ + +struct eq_generic_event { +__u64 data[7 ]; +}; +/* * ArmCP info */ drivers/misc/habanalabs/irq.c new file +150
-1@@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Quoted 2 lines
2.17.1
--
Sincerely yours,
Mike.
Part of the diff is not shown here. View the raw message.
PATCH 8/15 habanalabs: add event queue and interrupts
In reply to Oded GabbayThis patch adds support for receiving events from Goya's control CPU and for receiving MSI-X interrupts from Goya's DMA engines and CPU.
Goya's PCI controller supports up to 8 MSI-X interrupts, which only 6 of them are currently used. The first 5 interrupts are dedicated for Goya's DMA engine queues. The 6th interrupt is dedicated for Goya's control CPU.
The DMA queue will signal its MSI-X entry upon each completion of a command buffer that was placed on its primary queue. The driver will then mark that CB as completed and free the related resources. It will also update the command submission object which that CB belongs to.
There is a dedicated event queue (EQ) between the driver and Goya's control CPU. The EQ is located on the Host memory. The control CPU writes a new entry to the EQ for various reasons, such as ECC error, MMU page fault, Hot temperature. After writing the new entry to the EQ, the control CPU will trigger its dedicated MSI-X entry to signal the driver that there is a new entry in the EQ. The driver will then read the entry and act accordingly.
--- drivers/misc/habanalabs/device.c | 35 +- drivers/misc/habanalabs/goya/goya.c | 522 +++++++++++++++++++- drivers/misc/habanalabs/goya/goyaP.h | 1 + drivers/misc/habanalabs/habanalabs.h | 37 ++ drivers/misc/habanalabs/include/goya/goya.h | 1 - drivers/misc/habanalabs/irq.c | 144 ++++++ 6 files changed, 729 insertions(+), 11 deletions(-)
drivers/misc/habanalabs/device.c +34 -1
@@ -173,9 +173,17 @@ static int device_early_init(struct hl_device *hdev) hdev->cq_wq =alloc_workqueue ("hl-free-jobs" ,WQ_UNBOUND ,0 );if (hdev->cq_wq ==NULL ) {dev_err (hdev->dev,"Failed to allocate CQ workqueue\n" ); + rc = -ENOMEM ; goto asid_fini; } + hdev->eq_wq =alloc_workqueue ("hl-events" ,WQ_UNBOUND ,0 ); +if (hdev->eq_wq ==NULL ) { +dev_err (hdev->dev,"Failed to allocate EQ workqueue\n" ); + rc = -ENOMEM ; + goto free_cq_wq; + } +hl_cb_mgr_init (&hdev->kernel_cb_mgr);mutex_init (&hdev->device_open);
@@ -184,6 +192,8 @@ static int device_early_init(struct hl_device *hdev)return 0 ; +free_cq_wq: +destroy_workqueue (hdev->cq_wq); asid_fini:hl_asid_fini (hdev); early_fini:
@@ -205,6 +215,7 @@ static void device_early_fini(struct hl_device *hdev)hl_cb_mgr_fini (hdev, &hdev->kernel_cb_mgr); +destroy_workqueue (hdev->eq_wq);destroy_workqueue (hdev->cq_wq);hl_asid_fini (hdev);
@@ -343,11 +354,22 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) } } +/* +* 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" ); + goto cq_fini; + } +/* Allocate the kernel context */ hdev->kernel_ctx =kzalloc (sizeof (*hdev->kernel_ctx),GFP_KERNEL );if (!hdev->kernel_ctx) { rc = -ENOMEM ; - goto cq_fini; + goto eq_fini; } hdev->user_ctx =NULL ;
@@ -392,6 +414,8 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) "kernel ctx is still alive on initialization failure\n"); free_ctx:kfree (hdev->kernel_ctx); +eq_fini: +hl_eq_fini (hdev, &hdev->event_queue); cq_fini:for (i =0 ; i < cq_ready_cnt ; i++)hl_cq_fini (hdev, &hdev->completion_queue[i]);
@@ -433,6 +457,13 @@ void hl_device_fini(struct hl_device *hdev)/* Mark device as disabled */ hdev->disabled = true; +/* +* 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 +*/ + hdev->asic_funcs->halt_engines (hdev, true); +hl_cb_pool_fini (hdev);/* Release kernel context */
@@ -442,6 +473,8 @@ void hl_device_fini(struct hl_device *hdev)/* Reset the H/W. It will be in idle state after this returns */ hdev->asic_funcs->hw_fini (hdev, true); +hl_eq_fini (hdev, &hdev->event_queue); +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++)hl_cq_fini (hdev, &hdev->completion_queue[i]);kfree (hdev->completion_queue);
drivers/misc/habanalabs/goya/goya.c +513 -9
613 lines not on this page · raw message
drivers/misc/habanalabs/goya/goyaP.h +1 -0
@@ -123,6 +123,7 @@ struct goya_device {/* TODO: remove hw_queues_lock after moving to scheduler code */ spinlock_t hw_queues_lock;u64 ddr_bar_cur_addr; +u32 events_stat[GOYA_ASYNC_EVENT_ID_SIZE ];u32 hw_cap_initialized; };
drivers/misc/habanalabs/habanalabs.h +37 -0
@@ -83,6 +83,7 @@ struct hw_queue_properties {* @cfg_size: configuration space size on SRAM. * @sram_size: total size of SRAM. * @max_asid: maximum number of open contexts (ASIDs). +* @num_of_events: number of possible internal H/W IRQs. * @completion_queues_count: number of completion queues. * @high_pll: high PLL frequency used by the device. * @cb_pool_cb_cnt: number of CBs in the CB pool.
@@ -109,6 +110,7 @@ struct asic_fixed_properties {u32 cfg_size;u32 sram_size;u32 max_asid; +u32 num_of_events;u32 high_pll;u32 cb_pool_cb_cnt;u32 cb_pool_cb_size;
@@ -209,6 +211,9 @@ struct hl_cs_job;#define HL_CQ_LENGTH HL_QUEUE_LENGTH#define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE) +/* Must be power of 2 (HL_PAGE_SIZE / HL_EQ_ENTRY_SIZE) */ +#define HL_EQ_LENGTH 64 +#define HL_EQ_SIZE_IN_BYTES (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)/**
@@ -256,6 +261,20 @@ struct hl_cq {atomic_t free_slots_cnt; }; +/** +* struct hl_eq - describes the event queue (single one per device) +* @hdev: pointer to the device structure +* @kernel_address: holds the queue's kernel virtual address +* @bus_address: holds the queue's DMA address +* @ci: ci inside the queue +*/ +struct hl_eq { +struct hl_device *hdev; +u64 kernel_address; +dma_addr_t bus_address; +u32 ci; +}; +
@@ -288,6 +307,9 @@ enum hl_asic_type {* @sw_fini: tears down driver state, does not configure H/W. * @hw_init: sets up the H/W state. * @hw_fini: tears down the H/W state. +* @halt_engines: halt engines, needed for reset sequence. This also disables +* interrupts from the device. Should be called before +* hw_fini and before CS rollback. * @suspend: handles IP specific H/W or SW changes for suspend. * @resume: handles IP specific H/W or SW changes for resume. * @mmap: mmap function, does nothing.
@@ -303,6 +325,9 @@ enum hl_asic_type {* @dma_pool_free: free small DMA allocation from pool. * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool. * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool. +* @update_eq_ci: update event queue CI. +* @handle_eqe: handle event queue entry (IRQ) from ArmCP. +* @get_events_stat: retrieve event queue entries histogram. * @hw_queues_lock: acquire H/W queues lock. * @hw_queues_unlock: release H/W queues lock. * @send_cpu_message: send buffer to ArmCP.
@@ -314,6 +339,7 @@ struct hl_asic_funcs {int (*sw_fini)(struct hl_device *hdev);int (*hw_init)(struct hl_device *hdev);void (*hw_fini)(struct hl_device *hdev,bool hard_reset); +void (*halt_engines)(struct hl_device *hdev,bool hard_reset);int (*suspend)(struct hl_device *hdev);int (*resume)(struct hl_device *hdev);int (*mmap)(struct hl_fpriv *hpriv,struct vm_area_struct *vma);
@@ -336,6 +362,10 @@ struct hl_asic_funcs { size_t size, dma_addr_t *dma_handle);void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,size_t size,void *vaddr); +void (*update_eq_ci)(struct hl_device *hdev,u32 val); +void (*handle_eqe)(struct hl_device *hdev, +struct hl_eq_entry *eq_entry); +void * (*get_events_stat)(struct hl_device *hdev,u32 *size);void (*hw_queues_lock)(struct hl_device *hdev);void (*hw_queues_unlock)(struct hl_device *hdev); int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
@@ -474,6 +504,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @kernel_ctx: KMD context structure. * @kernel_queues: array of hl_hw_queue. * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs. +* @event_queue: event queue for IRQ from ArmCP. * @dma_pool: DMA pool for small allocations. * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address. * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
@@ -504,9 +535,11 @@ struct hl_device {enum hl_asic_type asic_type;struct hl_cq *completion_queue;struct workqueue_struct *cq_wq; +struct workqueue_struct *eq_wq;struct hl_ctx *kernel_ctx;struct hl_hw_queue *kernel_queues;struct hl_cb_mgr kernel_cb_mgr; +struct hl_eq event_queue;struct dma_pool *dma_pool;void *cpu_accessible_dma_mem;dma_addr_t cpu_accessible_dma_address;
@@ -593,6 +626,10 @@ void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);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); +int hl_eq_init (struct hl_device *hdev,struct hl_eq *q); +void hl_eq_fini (struct hl_device *hdev,struct hl_eq *q); +irqreturn_t hl_irq_handler_cq (int irq,void *arg); +irqreturn_t hl_irq_handler_eq (int irq,void *arg);int hl_asid_init (struct hl_device *hdev);void hl_asid_fini (struct hl_device *hdev);unsigned long hl_asid_alloc (struct hl_device *hdev);
drivers/misc/habanalabs/include/goya/goya.h +0 -1
@@ -65,7 +65,6 @@#define GOYA_MSIX_ENTRIES 8#define EVENT_QUEUE_MSIX_IDX 5 -#define ARMCP_RESET_MSIX_IDX 6#define QMAN_PQ_ENTRY_SIZE 16/* Bytes */
drivers/misc/habanalabs/irq.c +144 -0
165 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 08/15] habanalabs: add event queue and interrupts
Quoted 34 lines · Oded Gabbay
This patch adds support for receiving events from Goya's control CPU and for receiving MSI-X interrupts from Goya's DMA engines and CPU.
Goya's PCI controller supports up to 8 MSI-X interrupts, which only 6 of them are currently used. The first 5 interrupts are dedicated for Goya's DMA engine queues. The 6th interrupt is dedicated for Goya's control CPU.
The DMA queue will signal its MSI-X entry upon each completion of a command buffer that was placed on its primary queue. The driver will then mark that CB as completed and free the related resources. It will also update the command submission object which that CB belongs to.
There is a dedicated event queue (EQ) between the driver and Goya's control CPU. The EQ is located on the Host memory. The control CPU writes a new entry to the EQ for various reasons, such as ECC error, MMU page fault, Hot temperature. After writing the new entry to the EQ, the control CPU will trigger its dedicated MSI-X entry to signal the driver that there is a new entry in the EQ. The driver will then read the entry and act accordingly.
Signed-off-by Oded Gabbay <[email protected]>--- drivers/misc/habanalabs/device.c | 35 +- drivers/misc/habanalabs/goya/goya.c | 522 +++++++++++++++++++- drivers/misc/habanalabs/goya/goyaP.h | 1 + drivers/misc/habanalabs/habanalabs.h | 37 ++ drivers/misc/habanalabs/include/goya/goya.h | 1 - drivers/misc/habanalabs/irq.c | 144 ++++++ 6 files changed, 729 insertions(+), 11 deletions(-)drivers/misc/habanalabs/device.c +0
-0
drivers/misc/habanalabs/device.c static int device_early_init(struct hl_device *hdev) · line 176
Quoted 4 lines
drivers/misc/habanalabs/device.c +1
-0@@ -173,9 +173,17 @@ static int device_early_init(struct hl_device *hdev) hdev->cq_wq =alloc_workqueue ("hl-free-jobs" ,WQ_UNBOUND ,0 );if (hdev->cq_wq ==NULL ) {dev_err (hdev->dev,"Failed to allocate CQ workqueue\n" ); + rc = -ENOMEM ;
Apparently, it should have been in one of the earlier patches
Quoted 429 lines
drivers/misc/habanalabs/device.c +33
-1goto asid_fini; } + hdev->eq_wq =alloc_workqueue ("hl-events" ,WQ_UNBOUND ,0 ); +if (hdev->eq_wq ==NULL ) { +dev_err (hdev->dev,"Failed to allocate EQ workqueue\n" ); + rc = -ENOMEM ; + goto free_cq_wq; + } +hl_cb_mgr_init (&hdev->kernel_cb_mgr);mutex_init (&hdev->device_open);@@ -184,6 +192,8 @@ static int device_early_init(struct hl_device *hdev)return 0 ; +free_cq_wq: +destroy_workqueue (hdev->cq_wq); asid_fini:hl_asid_fini (hdev); early_fini:@@ -205,6 +215,7 @@ static void device_early_fini(struct hl_device *hdev)hl_cb_mgr_fini (hdev, &hdev->kernel_cb_mgr); +destroy_workqueue (hdev->eq_wq);destroy_workqueue (hdev->cq_wq);hl_asid_fini (hdev);@@ -343,11 +354,22 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) } } +/* +* 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" ); + goto cq_fini; + } +/* Allocate the kernel context */ hdev->kernel_ctx =kzalloc (sizeof (*hdev->kernel_ctx),GFP_KERNEL );if (!hdev->kernel_ctx) { rc = -ENOMEM ; - goto cq_fini; + goto eq_fini; } hdev->user_ctx =NULL ;@@ -392,6 +414,8 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) "kernel ctx is still alive on initialization failure\n"); free_ctx:kfree (hdev->kernel_ctx); +eq_fini: +hl_eq_fini (hdev, &hdev->event_queue); cq_fini:for (i =0 ; i < cq_ready_cnt ; i++)hl_cq_fini (hdev, &hdev->completion_queue[i]);@@ -433,6 +457,13 @@ void hl_device_fini(struct hl_device *hdev)/* Mark device as disabled */ hdev->disabled = true; +/* +* 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 +*/ + hdev->asic_funcs->halt_engines (hdev, true); +hl_cb_pool_fini (hdev);/* Release kernel context */ @@ -442,6 +473,8 @@ void hl_device_fini(struct hl_device *hdev)/* Reset the H/W. It will be in idle state after this returns */ hdev->asic_funcs->hw_fini (hdev, true); +hl_eq_fini (hdev, &hdev->event_queue); +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++)hl_cq_fini (hdev, &hdev->completion_queue[i]);kfree (hdev->completion_queue);drivers/misc/habanalabs/goya/goya.c +244
-9@@ -92,9 +92,41 @@#define GOYA_MAX_INITIATORS 20 +#define GOYA_MAX_STRING_LEN 20 +#define GOYA_CB_POOL_CB_CNT 512#define GOYA_CB_POOL_CB_SIZE 0x20000/* 128KB */ +static const char goya_irq_name[GOYA_MSIX_ENTRIES ][GOYA_MAX_STRING_LEN ] = { +"goya cq 0" ,"goya cq 1" ,"goya cq 2" ,"goya cq 3" , +"goya cq 4" ,"goya cpu eq" +}; + +static const char *goya_axi_name[GOYA_MAX_INITIATORS ] = { +"MME0" , +"MME1" , +"MME2" , +"MME3" , +"MME4" , +"MME5" , +"TPC0" , +"TPC1" , +"TPC2" , +"TPC3" , +"TPC4" , +"TPC5" , +"TPC6" , +"TPC7" , +"PCI" , +"DMA" ,/* HBW */ +"DMA" ,/* LBW */ +"PSOC" , +"CPU" , +"MMU" +}; + +#define GOYA_ASYC_EVENT_GROUP_NON_FATAL_SIZE 121 +static void goya_get_fixed_properties (struct hl_device *hdev) {struct asic_fixed_properties *prop = &hdev->asic_prop;@@ -139,6 +171,7 @@ static void goya_get_fixed_properties(struct hl_device *hdev) prop->va_space_dram_end_address =VA_DDR_SPACE_END ; prop->cfg_size =CFG_SIZE ; prop->max_asid =MAX_ASID ; + prop->num_of_events =GOYA_ASYNC_EVENT_ID_SIZE ; prop->cb_pool_cb_cnt =GOYA_CB_POOL_CB_CNT ; prop->cb_pool_cb_size =GOYA_CB_POOL_CB_SIZE ; prop->tpc_enabled_mask =TPC_ENABLED_MASK ;@@ -668,15 +701,10 @@ static void goya_init_dma_qman(struct hl_device *hdev, int dma_id,WREG32 (mmDMA_QM_0_PQ_CFG1 + reg_off,0x00020002 );WREG32 (mmDMA_QM_0_CQ_CFG1 + reg_off,0x00080008 ); -if (dma_id ==0 ) -WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off,QMAN_DMA_FULLY_TRUSTED ); +if (goya->hw_cap_initialized &HW_CAP_MMU ) +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off,QMAN_DMA_PARTLY_TRUSTED );else -if (goya->hw_cap_initialized &HW_CAP_MMU ) -WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off, -QMAN_DMA_PARTLY_TRUSTED ); -else -WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off, -QMAN_DMA_FULLY_TRUSTED ); +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off,QMAN_DMA_FULLY_TRUSTED );WREG32 (mmDMA_QM_0_GLBL_ERR_CFG + reg_off,QMAN_DMA_ERR_MSG_EN );WREG32 (mmDMA_QM_0_GLBL_CFG0 + reg_off,QMAN_DMA_ENABLE );@@ -870,6 +898,7 @@ static void goya_resume_external_queues(struct hl_device *hdev)int goya_init_cpu_queues (struct hl_device *hdev) {struct goya_device *goya = hdev->asic_specific; +struct hl_eq *eq;dma_addr_t bus_address;u32 status;struct hl_hw_queue *cpu_pq = &hdev->kernel_queues[GOYA_QUEUE_ID_CPU_PQ ];@@ -881,17 +910,24 @@ int goya_init_cpu_queues(struct hl_device *hdev)if (goya->hw_cap_initialized &HW_CAP_CPU_Q )return 0 ; + eq = &hdev->event_queue; + bus_address = cpu_pq->bus_address + hdev->asic_prop.host_phys_base_address;WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_0,lower_32_bits (bus_address));WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_1,upper_32_bits (bus_address)); + bus_address = eq->bus_address + hdev->asic_prop.host_phys_base_address; +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_2,lower_32_bits (bus_address)); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_3,upper_32_bits (bus_address)); + bus_address = hdev->cpu_accessible_dma_address + hdev->asic_prop.host_phys_base_address;WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_8,lower_32_bits (bus_address));WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_9,upper_32_bits (bus_address));WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_5,HL_QUEUE_SIZE_IN_BYTES ); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_4,HL_EQ_SIZE_IN_BYTES );WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_10,CPU_ACCESSIBLE_MEM_SIZE );/* Used for EQ CI */ @@ -2781,6 +2817,163 @@ static void goya_resume_internal_queues(struct hl_device *hdev)WREG32 (mmTPC7_CMDQ_GLBL_CFG1,0 ); } +static void goya_dma_stall (struct hl_device *hdev) +{ +WREG32 (mmDMA_QM_0_GLBL_CFG1,1 <<DMA_QM_0_GLBL_CFG1_DMA_STOP_SHIFT ); +WREG32 (mmDMA_QM_1_GLBL_CFG1,1 <<DMA_QM_1_GLBL_CFG1_DMA_STOP_SHIFT ); +WREG32 (mmDMA_QM_2_GLBL_CFG1,1 <<DMA_QM_2_GLBL_CFG1_DMA_STOP_SHIFT ); +WREG32 (mmDMA_QM_3_GLBL_CFG1,1 <<DMA_QM_3_GLBL_CFG1_DMA_STOP_SHIFT ); +WREG32 (mmDMA_QM_4_GLBL_CFG1,1 <<DMA_QM_4_GLBL_CFG1_DMA_STOP_SHIFT ); +} + +static void goya_tpc_stall (struct hl_device *hdev) +{ +WREG32 (mmTPC0_CFG_TPC_STALL,1 <<TPC0_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC1_CFG_TPC_STALL,1 <<TPC1_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC2_CFG_TPC_STALL,1 <<TPC2_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC3_CFG_TPC_STALL,1 <<TPC3_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC4_CFG_TPC_STALL,1 <<TPC4_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC5_CFG_TPC_STALL,1 <<TPC5_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC6_CFG_TPC_STALL,1 <<TPC6_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC7_CFG_TPC_STALL,1 <<TPC7_CFG_TPC_STALL_V_SHIFT ); +} + +static void goya_mme_stall (struct hl_device *hdev) +{ +WREG32 (mmMME_STALL,0xFFFFFFFF ); +} + +static int goya_enable_msix (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +int cq_cnt = hdev->asic_prop.completion_queues_count; +int rc, i, irq_cnt_init, irq; + +if (goya->hw_cap_initialized &HW_CAP_MSIX ) +return 0 ; + + rc =pci_alloc_irq_vectors (hdev->pdev,GOYA_MSIX_ENTRIES , +GOYA_MSIX_ENTRIES ,PCI_IRQ_MSIX ); +if (rc <0 ) { +dev_err (hdev->dev, +"MSI-X: Failed to enable support -- %d/%d\n" , +GOYA_MSIX_ENTRIES , rc); +return rc; + } + +for (i =0 , irq_cnt_init =0 ; i < cq_cnt ; i++, irq_cnt_init++) { + irq =pci_irq_vector (hdev->pdev, i); + rc =request_irq (irq, hl_irq_handler_cq,0 , goya_irq_name[i], + &hdev->completion_queue[i]); +if (rc) { +dev_err (hdev->dev,"Failed to request IRQ %d" , irq); + goto free_irqs; + } + } + + irq =pci_irq_vector (hdev->pdev,EVENT_QUEUE_MSIX_IDX ); + + rc =request_irq (irq, hl_irq_handler_eq,0 , + goya_irq_name[EVENT_QUEUE_MSIX_IDX ], + &hdev->event_queue); +if (rc) { +dev_err (hdev->dev,"Failed to request IRQ %d" , irq); + goto free_irqs; + } + + goya->hw_cap_initialized |=HW_CAP_MSIX ; +return 0 ; + +free_irqs: +for (i =0 ; i < irq_cnt_init ; i++) +free_irq (pci_irq_vector (hdev->pdev, i), + &hdev->completion_queue[i]); + +pci_free_irq_vectors (hdev->pdev); +return rc; +} + +static void goya_sync_irqs (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +int i; + +if (!(goya->hw_cap_initialized &HW_CAP_MSIX )) +return ; + +/* Wait for all pending IRQs to be finished */ +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++) +synchronize_irq (pci_irq_vector (hdev->pdev, i)); + +synchronize_irq (pci_irq_vector (hdev->pdev,EVENT_QUEUE_MSIX_IDX )); +} + +static void goya_disable_msix (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +int i, irq; + +if (!(goya->hw_cap_initialized &HW_CAP_MSIX )) +return ; + +goya_sync_irqs (hdev); + + irq =pci_irq_vector (hdev->pdev,EVENT_QUEUE_MSIX_IDX ); +free_irq (irq, &hdev->event_queue); + +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++) { + irq =pci_irq_vector (hdev->pdev, i); +free_irq (irq, &hdev->completion_queue[i]); + } + +pci_free_irq_vectors (hdev->pdev); + + goya->hw_cap_initialized &= ~HW_CAP_MSIX ; +} + +static void goya_halt_engines (struct hl_device *hdev,bool hard_reset) +{ +struct goya_device *goya = hdev->asic_specific; +u32 wait_timeout_ms, cpu_timeout_ms; + +dev_info (hdev->dev, +"Halting compute engines and disabling interrupts\n" ); + +if (hdev->pldm) { + wait_timeout_ms =GOYA_PLDM_RESET_WAIT_MSEC ; + cpu_timeout_ms =GOYA_PLDM_RESET_WAIT_MSEC ; + }else { + wait_timeout_ms =GOYA_RESET_WAIT_MSEC ; + cpu_timeout_ms =GOYA_CPU_RESET_WAIT_MSEC ; + } + +if ((hard_reset) && (goya->hw_cap_initialized &HW_CAP_CPU )) { +WREG32 (mmPSOC_GLOBAL_CONF_UBOOT_MAGIC,KMD_MSG_GOTO_WFE ); +if (hdev->fw_loading) +WREG32 (mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, +GOYA_ASYNC_EVENT_ID_HALT_MACHINE ); +msleep (cpu_timeout_ms); + } + +goya_stop_external_queues (hdev); +goya_stop_internal_queues (hdev); + +msleep (wait_timeout_ms); + +goya_dma_stall (hdev); +goya_tpc_stall (hdev); +goya_mme_stall (hdev); + +msleep (wait_timeout_ms); + +goya_disable_external_queues (hdev); +goya_disable_internal_queues (hdev); + +if (hard_reset) +goya_disable_msix (hdev); +else +goya_sync_irqs (hdev); +}/** * goya_push_uboot_to_device - Push u-boot FW code to device @@ -3166,11 +3359,16 @@ static int goya_hw_init(struct hl_device *hdev)goya_init_tpc_qmans (hdev); +/* MSI-X must be enabled before CPU queues are initialized */ + rc =goya_enable_msix (hdev); +if (rc) + goto disable_queues; + rc =goya_init_cpu_queues (hdev);if (rc) {dev_err (hdev->dev,"failed to initialize CPU H/W queues %d\n" , rc); - goto disable_queues; + goto disable_msix; }/* CPU initialization is finished, we can now move to 48 bit DMA mask */ @@ -3204,6 +3402,8 @@ static int goya_hw_init(struct hl_device *hdev) disable_pci_access:goya_send_pci_access_msg (hdev,ARMCP_PACKET_DISABLE_PCI_ACCESS ); +disable_msix: +goya_disable_msix (hdev); disable_queues:goya_disable_internal_queues (hdev);goya_disable_external_queues (hdev);@@ -3287,6 +3487,7 @@ static void goya_hw_fini(struct hl_device *hdev, bool hard_reset)HW_CAP_DMA |HW_CAP_MME |HW_CAP_MMU |HW_CAP_TPC_MBIST | HW_CAP_GOLDEN | HW_CAP_TPC); +memset (goya->events_stat,0 ,sizeof (goya->events_stat));if (!hdev->pldm) {int rc;@@ -3772,6 +3973,305 @@ void goya_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,gen_pool_free (hdev->cpu_accessible_dma_pool, (u64 ) vaddr, size); } +static void goya_update_eq_ci (struct hl_device *hdev,u32 val) +{ +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_6, val); +} + +static void goya_get_axi_name (struct hl_device *hdev,u32 agent_id, +u16 event_type,char *axi_name,int len) +{ +if (!strcmp (goya_axi_name[agent_id],"DMA" )) +if (event_type >=GOYA_ASYNC_EVENT_ID_DMA0_CH ) +snprintf (axi_name, len,"DMA %d" , + event_type -GOYA_ASYNC_EVENT_ID_DMA0_CH ); +else +snprintf (axi_name, len,"DMA %d" , + event_type -GOYA_ASYNC_EVENT_ID_DMA0_QM ); +else +snprintf (axi_name, len,"%s" , goya_axi_name[agent_id]); +} + +static void goya_print_razwi_info (struct hl_device *hdev,u64 reg, +bool is_hbw,bool is_read,u16 event_type) +{ +u32 val, id, internal_id, agent_id, y, x; +char axi_name[10 ] = {0 }; + + val =RREG32 (reg); + +if (is_hbw) { + id = (val &GOYA_IRQ_HBW_ID_MASK ) >>GOYA_IRQ_HBW_ID_SHIFT ; + internal_id = (val &GOYA_IRQ_HBW_INTERNAL_ID_MASK ) >> +GOYA_IRQ_HBW_INTERNAL_ID_SHIFT ; + agent_id = (val &GOYA_IRQ_HBW_AGENT_ID_MASK ) >> +GOYA_IRQ_HBW_AGENT_ID_SHIFT ; + y = (val &GOYA_IRQ_HBW_Y_MASK ) >>GOYA_IRQ_HBW_Y_SHIFT ;
drivers/misc/habanalabs/goya/goya.c void goya_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size, · line 4019
Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10
-0+ x = (val &GOYA_IRQ_HBW_X_MASK ) >>GOYA_IRQ_HBW_X_SHIFT ; + }else { + id = (val &GOYA_IRQ_LBW_ID_MASK ) >>GOYA_IRQ_LBW_ID_SHIFT ; + internal_id = (val &GOYA_IRQ_LBW_INTERNAL_ID_MASK ) >> +GOYA_IRQ_LBW_INTERNAL_ID_SHIFT ; + agent_id = (val &GOYA_IRQ_LBW_AGENT_ID_MASK ) >> +GOYA_IRQ_LBW_AGENT_ID_SHIFT ; + y = (val &GOYA_IRQ_LBW_Y_MASK ) >>GOYA_IRQ_LBW_Y_SHIFT ; + x = (val &GOYA_IRQ_LBW_X_MASK ) >>GOYA_IRQ_LBW_X_SHIFT ; + }
It seems that only agent_id is used
Quoted 158 lines
drivers/misc/habanalabs/goya/goya.c +158
-0+ +if (agent_id >=GOYA_MAX_INITIATORS ) { +dev_err (hdev->dev, +"Illegal %s %s with wrong initiator id %d, H/W IRQ %d\n" , + is_read ?"read from" :"write to" , + is_hbw ?"HBW" :"LBW" , + agent_id, + event_type); + }else { +goya_get_axi_name (hdev, agent_id, event_type, axi_name, +sizeof (axi_name)); +dev_err (hdev->dev,"Illegal %s by %s %s %s, H/W IRQ %d\n" , + is_read ?"read" :"write" , + axi_name, + is_read ?"from" :"to" , + is_hbw ?"HBW" :"LBW" , + event_type); + } +} + +static void goya_print_irq_info (struct hl_device *hdev,u16 event_type) +{ +struct goya_device *goya = hdev->asic_specific; +bool is_hbw = false, is_read = false, is_info = false; + +if (RREG32 (mmDMA_MACRO_RAZWI_LBW_WT_VLD)) { +goya_print_razwi_info (hdev, mmDMA_MACRO_RAZWI_LBW_WT_ID, is_hbw, + is_read, event_type); +WREG32 (mmDMA_MACRO_RAZWI_LBW_WT_VLD,0 ); + is_info = true; + } +if (RREG32 (mmDMA_MACRO_RAZWI_LBW_RD_VLD)) { + is_read = true; +goya_print_razwi_info (hdev, mmDMA_MACRO_RAZWI_LBW_RD_ID, is_hbw, + is_read, event_type); +WREG32 (mmDMA_MACRO_RAZWI_LBW_RD_VLD,0 ); + is_info = true; + } +if (RREG32 (mmDMA_MACRO_RAZWI_HBW_WT_VLD)) { + is_hbw = true; +goya_print_razwi_info (hdev, mmDMA_MACRO_RAZWI_HBW_WT_ID, is_hbw, + is_read, event_type); +WREG32 (mmDMA_MACRO_RAZWI_HBW_WT_VLD,0 ); + is_info = true; + } +if (RREG32 (mmDMA_MACRO_RAZWI_HBW_RD_VLD)) { + is_hbw = true; + is_read = true; +goya_print_razwi_info (hdev, mmDMA_MACRO_RAZWI_HBW_RD_ID, is_hbw, + is_read, event_type); +WREG32 (mmDMA_MACRO_RAZWI_HBW_RD_VLD,0 ); + is_info = true; + } +if (!is_info) { +dev_err (hdev->dev, +"Received H/W interrupt %d, no additional info\n" , + event_type); +return ; + } + +if (goya->hw_cap_initialized &HW_CAP_MMU ) { +u32 val =RREG32 (mmMMU_PAGE_ERROR_CAPTURE); +u64 addr; + +if (val &MMU_PAGE_ERROR_CAPTURE_ENTRY_VALID_MASK ) { + addr = val &MMU_PAGE_ERROR_CAPTURE_VA_49_32_MASK ; + addr <<=32 ; + addr |=RREG32 (mmMMU_PAGE_ERROR_CAPTURE_VA); + +dev_err (hdev->dev,"MMU page fault on va 0x%llx\n" , + addr); + +WREG32 (mmMMU_PAGE_ERROR_CAPTURE,0 ); + } + } +} + +static int goya_unmask_irq (struct hl_device *hdev,u16 event_type) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_UNMASK_RAZWI_IRQ ; + pkt.value = event_type; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +HL_DEVICE_TIMEOUT_USEC , &result); + +if (rc) +dev_err (hdev->dev,"failed to unmask RAZWI IRQ %d" , event_type); + +return rc; +} + +void goya_handle_eqe (struct hl_device *hdev,struct hl_eq_entry *eq_entry) +{ +u16 event_type = ((eq_entry->hdr.ctl &EQ_CTL_EVENT_TYPE_MASK ) + >>EQ_CTL_EVENT_TYPE_SHIFT ); +struct goya_device *goya = hdev->asic_specific; + + goya->events_stat[event_type]++; + +switch (event_type) { +case GOYA_ASYNC_EVENT_ID_PCIE_IF : +case GOYA_ASYNC_EVENT_ID_TPC0_ECC : +case GOYA_ASYNC_EVENT_ID_TPC1_ECC : +case GOYA_ASYNC_EVENT_ID_TPC2_ECC : +case GOYA_ASYNC_EVENT_ID_TPC3_ECC : +case GOYA_ASYNC_EVENT_ID_TPC4_ECC : +case GOYA_ASYNC_EVENT_ID_TPC5_ECC : +case GOYA_ASYNC_EVENT_ID_TPC6_ECC : +case GOYA_ASYNC_EVENT_ID_TPC7_ECC : +case GOYA_ASYNC_EVENT_ID_MME_ECC : +case GOYA_ASYNC_EVENT_ID_MME_ECC_EXT : +case GOYA_ASYNC_EVENT_ID_MMU_ECC : +case GOYA_ASYNC_EVENT_ID_DMA_MACRO : +case GOYA_ASYNC_EVENT_ID_DMA_ECC : +case GOYA_ASYNC_EVENT_ID_CPU_IF_ECC : +case GOYA_ASYNC_EVENT_ID_PSOC_MEM : +case GOYA_ASYNC_EVENT_ID_PSOC_CORESIGHT : +case GOYA_ASYNC_EVENT_ID_SRAM0 : +case GOYA_ASYNC_EVENT_ID_SRAM1 : +case GOYA_ASYNC_EVENT_ID_SRAM2 : +case GOYA_ASYNC_EVENT_ID_SRAM3 : +case GOYA_ASYNC_EVENT_ID_SRAM4 : +case GOYA_ASYNC_EVENT_ID_SRAM5 : +case GOYA_ASYNC_EVENT_ID_SRAM6 : +case GOYA_ASYNC_EVENT_ID_SRAM7 : +case GOYA_ASYNC_EVENT_ID_SRAM8 : +case GOYA_ASYNC_EVENT_ID_SRAM9 : +case GOYA_ASYNC_EVENT_ID_SRAM10 : +case GOYA_ASYNC_EVENT_ID_SRAM11 : +case GOYA_ASYNC_EVENT_ID_SRAM12 : +case GOYA_ASYNC_EVENT_ID_SRAM13 : +case GOYA_ASYNC_EVENT_ID_SRAM14 : +case GOYA_ASYNC_EVENT_ID_SRAM15 : +case GOYA_ASYNC_EVENT_ID_SRAM16 : +case GOYA_ASYNC_EVENT_ID_SRAM17 : +case GOYA_ASYNC_EVENT_ID_SRAM18 : +case GOYA_ASYNC_EVENT_ID_SRAM19 : +case GOYA_ASYNC_EVENT_ID_SRAM20 : +case GOYA_ASYNC_EVENT_ID_SRAM21 : +case GOYA_ASYNC_EVENT_ID_SRAM22 : +case GOYA_ASYNC_EVENT_ID_SRAM23 : +case GOYA_ASYNC_EVENT_ID_SRAM24 : +case GOYA_ASYNC_EVENT_ID_SRAM25 : +case GOYA_ASYNC_EVENT_ID_SRAM26 : +case GOYA_ASYNC_EVENT_ID_SRAM27 : +case GOYA_ASYNC_EVENT_ID_SRAM28 : +case GOYA_ASYNC_EVENT_ID_SRAM29 : +case GOYA_ASYNC_EVENT_ID_GIC500 : +case GOYA_ASYNC_EVENT_ID_PLL0 : +case GOYA_ASYNC_EVENT_ID_PLL1 : +case GOYA_ASYNC_EVENT_ID_PLL3 : +case GOYA_ASYNC_EVENT_ID_PLL4 :
drivers/misc/habanalabs/goya/goya.c void goya_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size, · line 4187
Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10
-0+case GOYA_ASYNC_EVENT_ID_PLL5 : +case GOYA_ASYNC_EVENT_ID_PLL6 : +case GOYA_ASYNC_EVENT_ID_AXI_ECC : +case GOYA_ASYNC_EVENT_ID_L2_RAM_ECC : +case GOYA_ASYNC_EVENT_ID_PSOC_GPIO_05_SW_RESET : +case GOYA_ASYNC_EVENT_ID_PSOC_GPIO_10_VRHOT_ICRIT : +dev_err (hdev->dev, +"Received H/W interrupt %d, reset the chip\n" , + event_type); +break ;
Looks tough. Any chance some of these values are consecutive and can be grouped, e.g
case GOYA_ASYNC_EVENT_ID_SRAM0 ... GOYA_ASYNC_EVENT_ID_SRAM29:
?
Quoted 346 lines
drivers/misc/habanalabs/goya/goya.c +91
-0+ +case GOYA_ASYNC_EVENT_ID_PCIE_DEC : +case GOYA_ASYNC_EVENT_ID_TPC0_DEC : +case GOYA_ASYNC_EVENT_ID_TPC1_DEC : +case GOYA_ASYNC_EVENT_ID_TPC2_DEC : +case GOYA_ASYNC_EVENT_ID_TPC3_DEC : +case GOYA_ASYNC_EVENT_ID_TPC4_DEC : +case GOYA_ASYNC_EVENT_ID_TPC5_DEC : +case GOYA_ASYNC_EVENT_ID_TPC6_DEC : +case GOYA_ASYNC_EVENT_ID_TPC7_DEC : +case GOYA_ASYNC_EVENT_ID_MME_WACS : +case GOYA_ASYNC_EVENT_ID_MME_WACSD : +case GOYA_ASYNC_EVENT_ID_CPU_AXI_SPLITTER : +case GOYA_ASYNC_EVENT_ID_PSOC_AXI_DEC : +case GOYA_ASYNC_EVENT_ID_PSOC : +case GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC1_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC2_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC3_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC4_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC5_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC6_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC7_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC0_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC1_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC2_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC3_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC4_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC5_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC6_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC7_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC0_QM : +case GOYA_ASYNC_EVENT_ID_TPC1_QM : +case GOYA_ASYNC_EVENT_ID_TPC2_QM : +case GOYA_ASYNC_EVENT_ID_TPC3_QM : +case GOYA_ASYNC_EVENT_ID_TPC4_QM : +case GOYA_ASYNC_EVENT_ID_TPC5_QM : +case GOYA_ASYNC_EVENT_ID_TPC6_QM : +case GOYA_ASYNC_EVENT_ID_TPC7_QM : +case GOYA_ASYNC_EVENT_ID_MME_QM : +case GOYA_ASYNC_EVENT_ID_MME_CMDQ : +case GOYA_ASYNC_EVENT_ID_DMA0_QM : +case GOYA_ASYNC_EVENT_ID_DMA1_QM : +case GOYA_ASYNC_EVENT_ID_DMA2_QM : +case GOYA_ASYNC_EVENT_ID_DMA3_QM : +case GOYA_ASYNC_EVENT_ID_DMA4_QM : +case GOYA_ASYNC_EVENT_ID_DMA0_CH : +case GOYA_ASYNC_EVENT_ID_DMA1_CH : +case GOYA_ASYNC_EVENT_ID_DMA2_CH : +case GOYA_ASYNC_EVENT_ID_DMA3_CH : +case GOYA_ASYNC_EVENT_ID_DMA4_CH : +goya_print_irq_info (hdev, event_type); +goya_unmask_irq (hdev, event_type); +break ; + +case GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC1_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC2_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC3_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC4_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC5_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC6_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC7_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH0 : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH1 : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH2 : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH3 : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH4 : +dev_info (hdev->dev,"Received H/W interrupt %d\n" , event_type); +break ; + +default : +dev_err (hdev->dev,"Received invalid H/W interrupt %d\n" , + event_type); +break ; + } +} + +void *goya_get_events_stat (struct hl_device *hdev,u32 *size) +{ +struct goya_device *goya = hdev->asic_specific; + + *size = (u32 )sizeof (goya->events_stat); + +return goya->events_stat; +} +static void goya_hw_queues_lock (struct hl_device *hdev) {@@ -3794,6 +4294,7 @@ static const struct hl_asic_funcs goya_funcs = { .sw_fini = goya_sw_fini, .hw_init = goya_hw_init, .hw_fini = goya_hw_fini, + .halt_engines = goya_halt_engines, .suspend = goya_suspend, .resume = goya_resume, .mmap = goya_mmap,@@ -3808,6 +4309,9 @@ static const struct hl_asic_funcs goya_funcs = { .dma_pool_free = goya_dma_pool_free, .cpu_accessible_dma_pool_alloc = goya_cpu_accessible_dma_pool_alloc, .cpu_accessible_dma_pool_free = goya_cpu_accessible_dma_pool_free, + .update_eq_ci = goya_update_eq_ci, + .handle_eqe = goya_handle_eqe, + .get_events_stat = goya_get_events_stat, .hw_queues_lock = goya_hw_queues_lock, .hw_queues_unlock = goya_hw_queues_unlock, .send_cpu_message = goya_send_cpu_messagedrivers/misc/habanalabs/goya/goyaP.h +1
-0@@ -123,6 +123,7 @@ struct goya_device {/* TODO: remove hw_queues_lock after moving to scheduler code */ spinlock_t hw_queues_lock;u64 ddr_bar_cur_addr; +u32 events_stat[GOYA_ASYNC_EVENT_ID_SIZE ];u32 hw_cap_initialized; };drivers/misc/habanalabs/habanalabs.h +37
-0@@ -83,6 +83,7 @@ struct hw_queue_properties {* @cfg_size: configuration space size on SRAM. * @sram_size: total size of SRAM. * @max_asid: maximum number of open contexts (ASIDs). +* @num_of_events: number of possible internal H/W IRQs. * @completion_queues_count: number of completion queues. * @high_pll: high PLL frequency used by the device. * @cb_pool_cb_cnt: number of CBs in the CB pool. @@ -109,6 +110,7 @@ struct asic_fixed_properties {u32 cfg_size;u32 sram_size;u32 max_asid; +u32 num_of_events;u32 high_pll;u32 cb_pool_cb_cnt;u32 cb_pool_cb_size;@@ -209,6 +211,9 @@ struct hl_cs_job;#define HL_CQ_LENGTH HL_QUEUE_LENGTH#define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE) +/* Must be power of 2 (HL_PAGE_SIZE / HL_EQ_ENTRY_SIZE) */ +#define HL_EQ_LENGTH 64 +#define HL_EQ_SIZE_IN_BYTES (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)/** @@ -256,6 +261,20 @@ struct hl_cq {atomic_t free_slots_cnt; }; +/** +* struct hl_eq - describes the event queue (single one per device) +* @hdev: pointer to the device structure +* @kernel_address: holds the queue's kernel virtual address +* @bus_address: holds the queue's DMA address +* @ci: ci inside the queue +*/ +struct hl_eq { +struct hl_device *hdev; +u64 kernel_address; +dma_addr_t bus_address; +u32 ci; +}; +@@ -288,6 +307,9 @@ enum hl_asic_type {* @sw_fini: tears down driver state, does not configure H/W. * @hw_init: sets up the H/W state. * @hw_fini: tears down the H/W state. +* @halt_engines: halt engines, needed for reset sequence. This also disables +* interrupts from the device. Should be called before +* hw_fini and before CS rollback. * @suspend: handles IP specific H/W or SW changes for suspend. * @resume: handles IP specific H/W or SW changes for resume. * @mmap: mmap function, does nothing. @@ -303,6 +325,9 @@ enum hl_asic_type {* @dma_pool_free: free small DMA allocation from pool. * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool. * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool. +* @update_eq_ci: update event queue CI. +* @handle_eqe: handle event queue entry (IRQ) from ArmCP. +* @get_events_stat: retrieve event queue entries histogram. * @hw_queues_lock: acquire H/W queues lock. * @hw_queues_unlock: release H/W queues lock. * @send_cpu_message: send buffer to ArmCP. @@ -314,6 +339,7 @@ struct hl_asic_funcs {int (*sw_fini)(struct hl_device *hdev);int (*hw_init)(struct hl_device *hdev);void (*hw_fini)(struct hl_device *hdev,bool hard_reset); +void (*halt_engines)(struct hl_device *hdev,bool hard_reset);int (*suspend)(struct hl_device *hdev);int (*resume)(struct hl_device *hdev);int (*mmap)(struct hl_fpriv *hpriv,struct vm_area_struct *vma);@@ -336,6 +362,10 @@ struct hl_asic_funcs { size_t size, dma_addr_t *dma_handle);void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,size_t size,void *vaddr); +void (*update_eq_ci)(struct hl_device *hdev,u32 val); +void (*handle_eqe)(struct hl_device *hdev, +struct hl_eq_entry *eq_entry); +void * (*get_events_stat)(struct hl_device *hdev,u32 *size);void (*hw_queues_lock)(struct hl_device *hdev);void (*hw_queues_unlock)(struct hl_device *hdev); int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,@@ -474,6 +504,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @kernel_ctx: KMD context structure. * @kernel_queues: array of hl_hw_queue. * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs. +* @event_queue: event queue for IRQ from ArmCP. * @dma_pool: DMA pool for small allocations. * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address. * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address. @@ -504,9 +535,11 @@ struct hl_device {enum hl_asic_type asic_type;struct hl_cq *completion_queue;struct workqueue_struct *cq_wq; +struct workqueue_struct *eq_wq;struct hl_ctx *kernel_ctx;struct hl_hw_queue *kernel_queues;struct hl_cb_mgr kernel_cb_mgr; +struct hl_eq event_queue;struct dma_pool *dma_pool;void *cpu_accessible_dma_mem;dma_addr_t cpu_accessible_dma_address;@@ -593,6 +626,10 @@ void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);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); +int hl_eq_init (struct hl_device *hdev,struct hl_eq *q); +void hl_eq_fini (struct hl_device *hdev,struct hl_eq *q); +irqreturn_t hl_irq_handler_cq (int irq,void *arg); +irqreturn_t hl_irq_handler_eq (int irq,void *arg);int hl_asid_init (struct hl_device *hdev);void hl_asid_fini (struct hl_device *hdev);unsigned long hl_asid_alloc (struct hl_device *hdev);drivers/misc/habanalabs/include/goya/goya.h +0
-1@@ -65,7 +65,6 @@#define GOYA_MSIX_ENTRIES 8#define EVENT_QUEUE_MSIX_IDX 5 -#define ARMCP_RESET_MSIX_IDX 6#define QMAN_PQ_ENTRY_SIZE 16/* Bytes */ drivers/misc/habanalabs/irq.c +71
-0@@ -9,6 +9,18 @@#include <linux/dma-mapping.h> +/** +* This structure is used to schedule work of EQ entry and armcp_reset event +* +* @eq_work - workqueue object to run when EQ entry is received +* @hdev - pointer to device structure +* @eq_entry - copy of the EQ entry +*/ +struct hl_eqe_work { +struct work_struct eq_work; +struct hl_device *hdev; +struct hl_eq_entry eq_entry; +};/** * hl_cq_inc_ptr - increment ci or pi of cq @@ -26,6 +38,33 @@ inline u32 hl_cq_inc_ptr(u32 ptr)return ptr; } +/** +* hl_eq_inc_ptr - increment ci of eq +* +* @ptr: the current ci value of the event queue +* +* Increment ptr by 1. If it reaches the number of event queue +* entries, set it to 0 +*/ +inline u32 hl_eq_inc_ptr (u32 ptr) +{ + ptr++; +if (unlikely (ptr ==HL_EQ_LENGTH )) + ptr =0 ; +return ptr; +} + +static void irq_handle_eqe (struct work_struct *work) +{ + struct hl_eqe_work *eqe_work = container_of(work, struct hl_eqe_work, + eq_work); +struct hl_device *hdev = eqe_work->hdev; + + hdev->asic_funcs->handle_eqe (hdev, &eqe_work->eq_entry); + +kfree (eqe_work); +} +/** * hl_irq_handler_cq - irq handler for completion queue * @@ -103,6 +142,68 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg)return IRQ_HANDLED ; } +/** +* hl_irq_handler_eq - irq handler for event queue +* +* @irq: irq number +* @arg: pointer to event queue structure +* +*/ +irqreturn_t hl_irq_handler_eq (int irq,void *arg) +{ +struct hl_eq *eq = arg; +struct hl_device *hdev = eq->hdev; +struct hl_eq_entry *eq_entry; +struct hl_eq_entry *eq_base; +struct hl_eqe_work *handle_eqe_work; + + eq_base = (struct hl_eq_entry *) eq->kernel_address; + +while (1 ) { +bool entry_ready = + ((eq_base[eq->ci].hdr.ctl &EQ_CTL_READY_MASK ) + >>EQ_CTL_READY_SHIFT ); + +if (!entry_ready) +break ; + + eq_entry = &eq_base[eq->ci]; + +/* +* Make sure we read EQ entry contents after we've +* checked the ownership bit. +*/ +dma_rmb ();
drivers/misc/habanalabs/irq.c irqreturn_t hl_irq_handler_cq(int irq, void *arg) · line 186
Quoted 10 lines
drivers/misc/habanalabs/irq.c +10
-0+ +if (hdev->disabled) { +dev_warn (hdev->dev, +"Device disabled but received IRQ %d for EQ\n" , + irq); + goto skip_irq; + } + + handle_eqe_work =kmalloc (sizeof (*handle_eqe_work),GFP_ATOMIC ); +if (handle_eqe_work) {
I couldn't find where is it freed
Quoted 73 lines
drivers/misc/habanalabs/irq.c +63
-0+INIT_WORK (&handle_eqe_work->eq_work, irq_handle_eqe); + handle_eqe_work->hdev = hdev; + +memcpy (&handle_eqe_work->eq_entry, eq_entry, +sizeof (*eq_entry)); + +queue_work (hdev->eq_wq, &handle_eqe_work->eq_work); + } +skip_irq: +/* Clear EQ entry ready bit */ + eq_entry->hdr.ctl &= ~EQ_CTL_READY_MASK ; + + eq->ci =hl_eq_inc_ptr (eq->ci); + + hdev->asic_funcs->update_eq_ci (hdev, eq->ci); + } + +return IRQ_HANDLED ; +} +/** * hl_cq_init - main initialization function for an cq object * @@ -148,3 +249,46 @@ void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q) hdev->asic_funcs->dma_free_coherent (hdev,HL_CQ_SIZE_IN_BYTES , (void *) q->kernel_address, q->bus_address); } + +/** +* hl_eq_init - main initialization function for an event queue object +* +* @hdev: pointer to device structure +* @q: pointer to eq structure +* +* Allocate dma-able memory for the event queue and initialize fields +* Returns 0 on success +*/ +int hl_eq_init (struct hl_device *hdev,struct hl_eq *q) +{ +void *p; + +BUILD_BUG_ON (HL_EQ_SIZE_IN_BYTES >HL_PAGE_SIZE ); + + p = hdev->asic_funcs->dma_alloc_coherent (hdev,HL_EQ_SIZE_IN_BYTES , + &q->bus_address,GFP_KERNEL | __GFP_ZERO); +if (!p) +return -ENOMEM ; + + q->hdev = hdev; + q->kernel_address = (u64 ) p; + q->ci =0 ; + +return 0 ; +} + +/** +* hl_eq_fini - destroy event queue +* +* @hdev: pointer to device structure +* @q: pointer to eq structure +* +* Free the event queue memory +*/ +void hl_eq_fini (struct hl_device *hdev,struct hl_eq *q) +{ +flush_workqueue (hdev->eq_wq); + + hdev->asic_funcs->dma_free_coherent (hdev,HL_EQ_SIZE_IN_BYTES , + (void *) q->kernel_address, q->bus_address); +}Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 08/15] habanalabs: add event queue and interrupts
Quoted 31 lines · Mike Rapoport
Quoted 29 lines · Oded Gabbay
This patch adds support for receiving events from Goya's control CPU and for receiving MSI-X interrupts from Goya's DMA engines and CPU.
Goya's PCI controller supports up to 8 MSI-X interrupts, which only 6 of them are currently used. The first 5 interrupts are dedicated for Goya's DMA engine queues. The 6th interrupt is dedicated for Goya's control CPU.
The DMA queue will signal its MSI-X entry upon each completion of a command buffer that was placed on its primary queue. The driver will then mark that CB as completed and free the related resources. It will also update the command submission object which that CB belongs to.
There is a dedicated event queue (EQ) between the driver and Goya's control CPU. The EQ is located on the Host memory. The control CPU writes a new entry to the EQ for various reasons, such as ECC error, MMU page fault, Hot temperature. After writing the new entry to the EQ, the control CPU will trigger its dedicated MSI-X entry to signal the driver that there is a new entry in the EQ. The driver will then read the entry and act accordingly.
Signed-off-by Oded Gabbay <[email protected]>--- drivers/misc/habanalabs/device.c | 35 +- drivers/misc/habanalabs/goya/goya.c | 522 +++++++++++++++++++- drivers/misc/habanalabs/goya/goyaP.h | 1 + drivers/misc/habanalabs/habanalabs.h | 37 ++ drivers/misc/habanalabs/include/goya/goya.h | 1 - drivers/misc/habanalabs/irq.c | 144 ++++++ 6 files changed, 729 insertions(+), 11 deletions(-)
Quoted 12 lines
drivers/misc/habanalabs/device.c static int device_early_init(struct hl_device *hdev) · line 176
Quoted 9 lines
drivers/misc/habanalabs/device.c +1
-0@@ -173,9 +173,17 @@ static int device_early_init(struct hl_device *hdev) hdev->cq_wq =alloc_workqueue ("hl-free-jobs" ,WQ_UNBOUND ,0 );if (hdev->cq_wq ==NULL ) {dev_err (hdev->dev,"Failed to allocate CQ workqueue\n" ); + rc = -ENOMEM ;Apparently, it should have been in one of the earlier patches
Correct, fixed
Quoted 433 lines
Quoted 433 lines
drivers/misc/habanalabs/device.c +33
-1goto asid_fini; } + hdev->eq_wq =alloc_workqueue ("hl-events" ,WQ_UNBOUND ,0 ); +if (hdev->eq_wq ==NULL ) { +dev_err (hdev->dev,"Failed to allocate EQ workqueue\n" ); + rc = -ENOMEM ; + goto free_cq_wq; + } +hl_cb_mgr_init (&hdev->kernel_cb_mgr);mutex_init (&hdev->device_open);@@ -184,6 +192,8 @@ static int device_early_init(struct hl_device *hdev)return 0 ; +free_cq_wq: +destroy_workqueue (hdev->cq_wq); asid_fini:hl_asid_fini (hdev); early_fini:@@ -205,6 +215,7 @@ static void device_early_fini(struct hl_device *hdev)hl_cb_mgr_fini (hdev, &hdev->kernel_cb_mgr); +destroy_workqueue (hdev->eq_wq);destroy_workqueue (hdev->cq_wq);hl_asid_fini (hdev);@@ -343,11 +354,22 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) } } +/* +* 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" ); + goto cq_fini; + } +/* Allocate the kernel context */ hdev->kernel_ctx =kzalloc (sizeof (*hdev->kernel_ctx),GFP_KERNEL );if (!hdev->kernel_ctx) { rc = -ENOMEM ; - goto cq_fini; + goto eq_fini; } hdev->user_ctx =NULL ;@@ -392,6 +414,8 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) "kernel ctx is still alive on initialization failure\n"); free_ctx:kfree (hdev->kernel_ctx); +eq_fini: +hl_eq_fini (hdev, &hdev->event_queue); cq_fini:for (i =0 ; i < cq_ready_cnt ; i++)hl_cq_fini (hdev, &hdev->completion_queue[i]);@@ -433,6 +457,13 @@ void hl_device_fini(struct hl_device *hdev)/* Mark device as disabled */ hdev->disabled = true; +/* +* 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 +*/ + hdev->asic_funcs->halt_engines (hdev, true); +hl_cb_pool_fini (hdev);/* Release kernel context */ @@ -442,6 +473,8 @@ void hl_device_fini(struct hl_device *hdev)/* Reset the H/W. It will be in idle state after this returns */ hdev->asic_funcs->hw_fini (hdev, true); +hl_eq_fini (hdev, &hdev->event_queue); +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++)hl_cq_fini (hdev, &hdev->completion_queue[i]);kfree (hdev->completion_queue);drivers/misc/habanalabs/goya/goya.c +248
-9@@ -92,9 +92,41 @@#define GOYA_MAX_INITIATORS 20 +#define GOYA_MAX_STRING_LEN 20 +#define GOYA_CB_POOL_CB_CNT 512#define GOYA_CB_POOL_CB_SIZE 0x20000/* 128KB */ +static const char goya_irq_name[GOYA_MSIX_ENTRIES ][GOYA_MAX_STRING_LEN ] = { +"goya cq 0" ,"goya cq 1" ,"goya cq 2" ,"goya cq 3" , +"goya cq 4" ,"goya cpu eq" +}; + +static const char *goya_axi_name[GOYA_MAX_INITIATORS ] = { +"MME0" , +"MME1" , +"MME2" , +"MME3" , +"MME4" , +"MME5" , +"TPC0" , +"TPC1" , +"TPC2" , +"TPC3" , +"TPC4" , +"TPC5" , +"TPC6" , +"TPC7" , +"PCI" , +"DMA" ,/* HBW */ +"DMA" ,/* LBW */ +"PSOC" , +"CPU" , +"MMU" +}; + +#define GOYA_ASYC_EVENT_GROUP_NON_FATAL_SIZE 121 +static void goya_get_fixed_properties (struct hl_device *hdev) {struct asic_fixed_properties *prop = &hdev->asic_prop;@@ -139,6 +171,7 @@ static void goya_get_fixed_properties(struct hl_device *hdev) prop->va_space_dram_end_address =VA_DDR_SPACE_END ; prop->cfg_size =CFG_SIZE ; prop->max_asid =MAX_ASID ; + prop->num_of_events =GOYA_ASYNC_EVENT_ID_SIZE ; prop->cb_pool_cb_cnt =GOYA_CB_POOL_CB_CNT ; prop->cb_pool_cb_size =GOYA_CB_POOL_CB_SIZE ; prop->tpc_enabled_mask =TPC_ENABLED_MASK ;@@ -668,15 +701,10 @@ static void goya_init_dma_qman(struct hl_device *hdev, int dma_id,WREG32 (mmDMA_QM_0_PQ_CFG1 + reg_off,0x00020002 );WREG32 (mmDMA_QM_0_CQ_CFG1 + reg_off,0x00080008 ); -if (dma_id ==0 ) -WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off,QMAN_DMA_FULLY_TRUSTED ); +if (goya->hw_cap_initialized &HW_CAP_MMU ) +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off,QMAN_DMA_PARTLY_TRUSTED );else -if (goya->hw_cap_initialized &HW_CAP_MMU ) -WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off, -QMAN_DMA_PARTLY_TRUSTED ); -else -WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off, -QMAN_DMA_FULLY_TRUSTED ); +WREG32 (mmDMA_QM_0_GLBL_PROT + reg_off,QMAN_DMA_FULLY_TRUSTED );WREG32 (mmDMA_QM_0_GLBL_ERR_CFG + reg_off,QMAN_DMA_ERR_MSG_EN );WREG32 (mmDMA_QM_0_GLBL_CFG0 + reg_off,QMAN_DMA_ENABLE );@@ -870,6 +898,7 @@ static void goya_resume_external_queues(struct hl_device *hdev)int goya_init_cpu_queues (struct hl_device *hdev) {struct goya_device *goya = hdev->asic_specific; +struct hl_eq *eq;dma_addr_t bus_address;u32 status;struct hl_hw_queue *cpu_pq = &hdev->kernel_queues[GOYA_QUEUE_ID_CPU_PQ ];@@ -881,17 +910,24 @@ int goya_init_cpu_queues(struct hl_device *hdev)if (goya->hw_cap_initialized &HW_CAP_CPU_Q )return 0 ; + eq = &hdev->event_queue; + bus_address = cpu_pq->bus_address + hdev->asic_prop.host_phys_base_address;WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_0,lower_32_bits (bus_address));WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_1,upper_32_bits (bus_address)); + bus_address = eq->bus_address + hdev->asic_prop.host_phys_base_address; +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_2,lower_32_bits (bus_address)); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_3,upper_32_bits (bus_address)); + bus_address = hdev->cpu_accessible_dma_address + hdev->asic_prop.host_phys_base_address;WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_8,lower_32_bits (bus_address));WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_9,upper_32_bits (bus_address));WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_5,HL_QUEUE_SIZE_IN_BYTES ); +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_4,HL_EQ_SIZE_IN_BYTES );WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_10,CPU_ACCESSIBLE_MEM_SIZE );/* Used for EQ CI */ @@ -2781,6 +2817,163 @@ static void goya_resume_internal_queues(struct hl_device *hdev)WREG32 (mmTPC7_CMDQ_GLBL_CFG1,0 ); } +static void goya_dma_stall (struct hl_device *hdev) +{ +WREG32 (mmDMA_QM_0_GLBL_CFG1,1 <<DMA_QM_0_GLBL_CFG1_DMA_STOP_SHIFT ); +WREG32 (mmDMA_QM_1_GLBL_CFG1,1 <<DMA_QM_1_GLBL_CFG1_DMA_STOP_SHIFT ); +WREG32 (mmDMA_QM_2_GLBL_CFG1,1 <<DMA_QM_2_GLBL_CFG1_DMA_STOP_SHIFT ); +WREG32 (mmDMA_QM_3_GLBL_CFG1,1 <<DMA_QM_3_GLBL_CFG1_DMA_STOP_SHIFT ); +WREG32 (mmDMA_QM_4_GLBL_CFG1,1 <<DMA_QM_4_GLBL_CFG1_DMA_STOP_SHIFT ); +} + +static void goya_tpc_stall (struct hl_device *hdev) +{ +WREG32 (mmTPC0_CFG_TPC_STALL,1 <<TPC0_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC1_CFG_TPC_STALL,1 <<TPC1_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC2_CFG_TPC_STALL,1 <<TPC2_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC3_CFG_TPC_STALL,1 <<TPC3_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC4_CFG_TPC_STALL,1 <<TPC4_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC5_CFG_TPC_STALL,1 <<TPC5_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC6_CFG_TPC_STALL,1 <<TPC6_CFG_TPC_STALL_V_SHIFT ); +WREG32 (mmTPC7_CFG_TPC_STALL,1 <<TPC7_CFG_TPC_STALL_V_SHIFT ); +} + +static void goya_mme_stall (struct hl_device *hdev) +{ +WREG32 (mmMME_STALL,0xFFFFFFFF ); +} + +static int goya_enable_msix (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +int cq_cnt = hdev->asic_prop.completion_queues_count; +int rc, i, irq_cnt_init, irq; + +if (goya->hw_cap_initialized &HW_CAP_MSIX ) +return 0 ; + + rc =pci_alloc_irq_vectors (hdev->pdev,GOYA_MSIX_ENTRIES , +GOYA_MSIX_ENTRIES ,PCI_IRQ_MSIX ); +if (rc <0 ) { +dev_err (hdev->dev, +"MSI-X: Failed to enable support -- %d/%d\n" , +GOYA_MSIX_ENTRIES , rc); +return rc; + } + +for (i =0 , irq_cnt_init =0 ; i < cq_cnt ; i++, irq_cnt_init++) { + irq =pci_irq_vector (hdev->pdev, i); + rc =request_irq (irq, hl_irq_handler_cq,0 , goya_irq_name[i], + &hdev->completion_queue[i]); +if (rc) { +dev_err (hdev->dev,"Failed to request IRQ %d" , irq); + goto free_irqs; + } + } + + irq =pci_irq_vector (hdev->pdev,EVENT_QUEUE_MSIX_IDX ); + + rc =request_irq (irq, hl_irq_handler_eq,0 , + goya_irq_name[EVENT_QUEUE_MSIX_IDX ], + &hdev->event_queue); +if (rc) { +dev_err (hdev->dev,"Failed to request IRQ %d" , irq); + goto free_irqs; + } + + goya->hw_cap_initialized |=HW_CAP_MSIX ; +return 0 ; + +free_irqs: +for (i =0 ; i < irq_cnt_init ; i++) +free_irq (pci_irq_vector (hdev->pdev, i), + &hdev->completion_queue[i]); + +pci_free_irq_vectors (hdev->pdev); +return rc; +} + +static void goya_sync_irqs (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +int i; + +if (!(goya->hw_cap_initialized &HW_CAP_MSIX )) +return ; + +/* Wait for all pending IRQs to be finished */ +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++) +synchronize_irq (pci_irq_vector (hdev->pdev, i)); + +synchronize_irq (pci_irq_vector (hdev->pdev,EVENT_QUEUE_MSIX_IDX )); +} + +static void goya_disable_msix (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +int i, irq; + +if (!(goya->hw_cap_initialized &HW_CAP_MSIX )) +return ; + +goya_sync_irqs (hdev); + + irq =pci_irq_vector (hdev->pdev,EVENT_QUEUE_MSIX_IDX ); +free_irq (irq, &hdev->event_queue); + +for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++) { + irq =pci_irq_vector (hdev->pdev, i); +free_irq (irq, &hdev->completion_queue[i]); + } + +pci_free_irq_vectors (hdev->pdev); + + goya->hw_cap_initialized &= ~HW_CAP_MSIX ; +} + +static void goya_halt_engines (struct hl_device *hdev,bool hard_reset) +{ +struct goya_device *goya = hdev->asic_specific; +u32 wait_timeout_ms, cpu_timeout_ms; + +dev_info (hdev->dev, +"Halting compute engines and disabling interrupts\n" ); + +if (hdev->pldm) { + wait_timeout_ms =GOYA_PLDM_RESET_WAIT_MSEC ; + cpu_timeout_ms =GOYA_PLDM_RESET_WAIT_MSEC ; + }else { + wait_timeout_ms =GOYA_RESET_WAIT_MSEC ; + cpu_timeout_ms =GOYA_CPU_RESET_WAIT_MSEC ; + } + +if ((hard_reset) && (goya->hw_cap_initialized &HW_CAP_CPU )) { +WREG32 (mmPSOC_GLOBAL_CONF_UBOOT_MAGIC,KMD_MSG_GOTO_WFE ); +if (hdev->fw_loading) +WREG32 (mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, +GOYA_ASYNC_EVENT_ID_HALT_MACHINE ); +msleep (cpu_timeout_ms); + } + +goya_stop_external_queues (hdev); +goya_stop_internal_queues (hdev); + +msleep (wait_timeout_ms); + +goya_dma_stall (hdev); +goya_tpc_stall (hdev); +goya_mme_stall (hdev); + +msleep (wait_timeout_ms); + +goya_disable_external_queues (hdev); +goya_disable_internal_queues (hdev); + +if (hard_reset) +goya_disable_msix (hdev); +else +goya_sync_irqs (hdev); +}/** * goya_push_uboot_to_device - Push u-boot FW code to device @@ -3166,11 +3359,16 @@ static int goya_hw_init(struct hl_device *hdev)goya_init_tpc_qmans (hdev); +/* MSI-X must be enabled before CPU queues are initialized */ + rc =goya_enable_msix (hdev); +if (rc) + goto disable_queues; + rc =goya_init_cpu_queues (hdev);if (rc) {dev_err (hdev->dev,"failed to initialize CPU H/W queues %d\n" , rc); - goto disable_queues; + goto disable_msix; }/* CPU initialization is finished, we can now move to 48 bit DMA mask */ @@ -3204,6 +3402,8 @@ static int goya_hw_init(struct hl_device *hdev) disable_pci_access:goya_send_pci_access_msg (hdev,ARMCP_PACKET_DISABLE_PCI_ACCESS ); +disable_msix: +goya_disable_msix (hdev); disable_queues:goya_disable_internal_queues (hdev);goya_disable_external_queues (hdev);@@ -3287,6 +3487,7 @@ static void goya_hw_fini(struct hl_device *hdev, bool hard_reset)HW_CAP_DMA |HW_CAP_MME |HW_CAP_MMU |HW_CAP_TPC_MBIST | HW_CAP_GOLDEN | HW_CAP_TPC); +memset (goya->events_stat,0 ,sizeof (goya->events_stat));if (!hdev->pldm) {int rc;@@ -3772,6 +3973,305 @@ void goya_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,gen_pool_free (hdev->cpu_accessible_dma_pool, (u64 ) vaddr, size); } +static void goya_update_eq_ci (struct hl_device *hdev,u32 val) +{ +WREG32 (mmPSOC_GLOBAL_CONF_SCRATCHPAD_6, val); +} + +static void goya_get_axi_name (struct hl_device *hdev,u32 agent_id, +u16 event_type,char *axi_name,int len) +{ +if (!strcmp (goya_axi_name[agent_id],"DMA" )) +if (event_type >=GOYA_ASYNC_EVENT_ID_DMA0_CH ) +snprintf (axi_name, len,"DMA %d" , + event_type -GOYA_ASYNC_EVENT_ID_DMA0_CH ); +else +snprintf (axi_name, len,"DMA %d" , + event_type -GOYA_ASYNC_EVENT_ID_DMA0_QM ); +else +snprintf (axi_name, len,"%s" , goya_axi_name[agent_id]); +} + +static void goya_print_razwi_info (struct hl_device *hdev,u64 reg, +bool is_hbw,bool is_read,u16 event_type) +{ +u32 val, id, internal_id, agent_id, y, x; +char axi_name[10 ] = {0 }; + + val =RREG32 (reg); + +if (is_hbw) { + id = (val &GOYA_IRQ_HBW_ID_MASK ) >>GOYA_IRQ_HBW_ID_SHIFT ; + internal_id = (val &GOYA_IRQ_HBW_INTERNAL_ID_MASK ) >> +GOYA_IRQ_HBW_INTERNAL_ID_SHIFT ; + agent_id = (val &GOYA_IRQ_HBW_AGENT_ID_MASK ) >> +GOYA_IRQ_HBW_AGENT_ID_SHIFT ; + y = (val &GOYA_IRQ_HBW_Y_MASK ) >>GOYA_IRQ_HBW_Y_SHIFT ; + x = (val &GOYA_IRQ_HBW_X_MASK ) >>GOYA_IRQ_HBW_X_SHIFT ; + }else { + id = (val &GOYA_IRQ_LBW_ID_MASK ) >>GOYA_IRQ_LBW_ID_SHIFT ; + internal_id = (val & GOYA_IRQ_LBW_INTERNAL_ID_MASK) >>
Quoted 9 lines
drivers/misc/habanalabs/goya/goya.c void goya_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size, · line 4019
Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c +6
-0+GOYA_IRQ_LBW_INTERNAL_ID_SHIFT ; + agent_id = (val &GOYA_IRQ_LBW_AGENT_ID_MASK ) >> +GOYA_IRQ_LBW_AGENT_ID_SHIFT ; + y = (val &GOYA_IRQ_LBW_Y_MASK ) >>GOYA_IRQ_LBW_Y_SHIFT ; + x = (val &GOYA_IRQ_LBW_X_MASK ) >>GOYA_IRQ_LBW_X_SHIFT ; + }It seems that only
agent_idis used
Fixed
Quoted 172 lines
Quoted 158 lines
drivers/misc/habanalabs/goya/goya.c +158
-0+ +if (agent_id >=GOYA_MAX_INITIATORS ) { +dev_err (hdev->dev, +"Illegal %s %s with wrong initiator id %d, H/W IRQ %d\n" , + is_read ?"read from" :"write to" , + is_hbw ?"HBW" :"LBW" , + agent_id, + event_type); + }else { +goya_get_axi_name (hdev, agent_id, event_type, axi_name, +sizeof (axi_name)); +dev_err (hdev->dev,"Illegal %s by %s %s %s, H/W IRQ %d\n" , + is_read ?"read" :"write" , + axi_name, + is_read ?"from" :"to" , + is_hbw ?"HBW" :"LBW" , + event_type); + } +} + +static void goya_print_irq_info (struct hl_device *hdev,u16 event_type) +{ +struct goya_device *goya = hdev->asic_specific; +bool is_hbw = false, is_read = false, is_info = false; + +if (RREG32 (mmDMA_MACRO_RAZWI_LBW_WT_VLD)) { +goya_print_razwi_info (hdev, mmDMA_MACRO_RAZWI_LBW_WT_ID, is_hbw, + is_read, event_type); +WREG32 (mmDMA_MACRO_RAZWI_LBW_WT_VLD,0 ); + is_info = true; + } +if (RREG32 (mmDMA_MACRO_RAZWI_LBW_RD_VLD)) { + is_read = true; +goya_print_razwi_info (hdev, mmDMA_MACRO_RAZWI_LBW_RD_ID, is_hbw, + is_read, event_type); +WREG32 (mmDMA_MACRO_RAZWI_LBW_RD_VLD,0 ); + is_info = true; + } +if (RREG32 (mmDMA_MACRO_RAZWI_HBW_WT_VLD)) { + is_hbw = true; +goya_print_razwi_info (hdev, mmDMA_MACRO_RAZWI_HBW_WT_ID, is_hbw, + is_read, event_type); +WREG32 (mmDMA_MACRO_RAZWI_HBW_WT_VLD,0 ); + is_info = true; + } +if (RREG32 (mmDMA_MACRO_RAZWI_HBW_RD_VLD)) { + is_hbw = true; + is_read = true; +goya_print_razwi_info (hdev, mmDMA_MACRO_RAZWI_HBW_RD_ID, is_hbw, + is_read, event_type); +WREG32 (mmDMA_MACRO_RAZWI_HBW_RD_VLD,0 ); + is_info = true; + } +if (!is_info) { +dev_err (hdev->dev, +"Received H/W interrupt %d, no additional info\n" , + event_type); +return ; + } + +if (goya->hw_cap_initialized &HW_CAP_MMU ) { +u32 val =RREG32 (mmMMU_PAGE_ERROR_CAPTURE); +u64 addr; + +if (val &MMU_PAGE_ERROR_CAPTURE_ENTRY_VALID_MASK ) { + addr = val &MMU_PAGE_ERROR_CAPTURE_VA_49_32_MASK ; + addr <<=32 ; + addr |=RREG32 (mmMMU_PAGE_ERROR_CAPTURE_VA); + +dev_err (hdev->dev,"MMU page fault on va 0x%llx\n" , + addr); + +WREG32 (mmMMU_PAGE_ERROR_CAPTURE,0 ); + } + } +} + +static int goya_unmask_irq (struct hl_device *hdev,u16 event_type) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_UNMASK_RAZWI_IRQ ; + pkt.value = event_type; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +HL_DEVICE_TIMEOUT_USEC , &result); + +if (rc) +dev_err (hdev->dev,"failed to unmask RAZWI IRQ %d" , event_type); + +return rc; +} + +void goya_handle_eqe (struct hl_device *hdev,struct hl_eq_entry *eq_entry) +{ +u16 event_type = ((eq_entry->hdr.ctl &EQ_CTL_EVENT_TYPE_MASK ) + >>EQ_CTL_EVENT_TYPE_SHIFT ); +struct goya_device *goya = hdev->asic_specific; + + goya->events_stat[event_type]++; + +switch (event_type) { +case GOYA_ASYNC_EVENT_ID_PCIE_IF : +case GOYA_ASYNC_EVENT_ID_TPC0_ECC : +case GOYA_ASYNC_EVENT_ID_TPC1_ECC : +case GOYA_ASYNC_EVENT_ID_TPC2_ECC : +case GOYA_ASYNC_EVENT_ID_TPC3_ECC : +case GOYA_ASYNC_EVENT_ID_TPC4_ECC : +case GOYA_ASYNC_EVENT_ID_TPC5_ECC : +case GOYA_ASYNC_EVENT_ID_TPC6_ECC : +case GOYA_ASYNC_EVENT_ID_TPC7_ECC : +case GOYA_ASYNC_EVENT_ID_MME_ECC : +case GOYA_ASYNC_EVENT_ID_MME_ECC_EXT : +case GOYA_ASYNC_EVENT_ID_MMU_ECC : +case GOYA_ASYNC_EVENT_ID_DMA_MACRO : +case GOYA_ASYNC_EVENT_ID_DMA_ECC : +case GOYA_ASYNC_EVENT_ID_CPU_IF_ECC : +case GOYA_ASYNC_EVENT_ID_PSOC_MEM : +case GOYA_ASYNC_EVENT_ID_PSOC_CORESIGHT : +case GOYA_ASYNC_EVENT_ID_SRAM0 : +case GOYA_ASYNC_EVENT_ID_SRAM1 : +case GOYA_ASYNC_EVENT_ID_SRAM2 : +case GOYA_ASYNC_EVENT_ID_SRAM3 : +case GOYA_ASYNC_EVENT_ID_SRAM4 : +case GOYA_ASYNC_EVENT_ID_SRAM5 : +case GOYA_ASYNC_EVENT_ID_SRAM6 : +case GOYA_ASYNC_EVENT_ID_SRAM7 : +case GOYA_ASYNC_EVENT_ID_SRAM8 : +case GOYA_ASYNC_EVENT_ID_SRAM9 : +case GOYA_ASYNC_EVENT_ID_SRAM10 : +case GOYA_ASYNC_EVENT_ID_SRAM11 : +case GOYA_ASYNC_EVENT_ID_SRAM12 : +case GOYA_ASYNC_EVENT_ID_SRAM13 : +case GOYA_ASYNC_EVENT_ID_SRAM14 : +case GOYA_ASYNC_EVENT_ID_SRAM15 : +case GOYA_ASYNC_EVENT_ID_SRAM16 : +case GOYA_ASYNC_EVENT_ID_SRAM17 : +case GOYA_ASYNC_EVENT_ID_SRAM18 : +case GOYA_ASYNC_EVENT_ID_SRAM19 : +case GOYA_ASYNC_EVENT_ID_SRAM20 : +case GOYA_ASYNC_EVENT_ID_SRAM21 : +case GOYA_ASYNC_EVENT_ID_SRAM22 : +case GOYA_ASYNC_EVENT_ID_SRAM23 : +case GOYA_ASYNC_EVENT_ID_SRAM24 : +case GOYA_ASYNC_EVENT_ID_SRAM25 : +case GOYA_ASYNC_EVENT_ID_SRAM26 : +case GOYA_ASYNC_EVENT_ID_SRAM27 : +case GOYA_ASYNC_EVENT_ID_SRAM28 : +case GOYA_ASYNC_EVENT_ID_SRAM29 : +case GOYA_ASYNC_EVENT_ID_GIC500 : +case GOYA_ASYNC_EVENT_ID_PLL0 : +case GOYA_ASYNC_EVENT_ID_PLL1 : +case GOYA_ASYNC_EVENT_ID_PLL3 : +case GOYA_ASYNC_EVENT_ID_PLL4 :drivers/misc/habanalabs/goya/goya.c void goya_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size, · line 4187
Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10
-0+case GOYA_ASYNC_EVENT_ID_PLL5 : +case GOYA_ASYNC_EVENT_ID_PLL6 : +case GOYA_ASYNC_EVENT_ID_AXI_ECC : +case GOYA_ASYNC_EVENT_ID_L2_RAM_ECC : +case GOYA_ASYNC_EVENT_ID_PSOC_GPIO_05_SW_RESET : +case GOYA_ASYNC_EVENT_ID_PSOC_GPIO_10_VRHOT_ICRIT : +dev_err (hdev->dev, +"Received H/W interrupt %d, reset the chip\n" , + event_type); +break ;Looks tough. Any chance some of these values are consecutive and can be grouped, e.g
Quoted 2 lines
case
GOYA_ASYNC_EVENT_ID_SRAM0...GOYA_ASYNC_EVENT_ID_SRAM29:
?
Fixed (did what I could)
Quoted 351 lines
Quoted 351 lines
drivers/misc/habanalabs/goya/goya.c +91
-0+ +case GOYA_ASYNC_EVENT_ID_PCIE_DEC : +case GOYA_ASYNC_EVENT_ID_TPC0_DEC : +case GOYA_ASYNC_EVENT_ID_TPC1_DEC : +case GOYA_ASYNC_EVENT_ID_TPC2_DEC : +case GOYA_ASYNC_EVENT_ID_TPC3_DEC : +case GOYA_ASYNC_EVENT_ID_TPC4_DEC : +case GOYA_ASYNC_EVENT_ID_TPC5_DEC : +case GOYA_ASYNC_EVENT_ID_TPC6_DEC : +case GOYA_ASYNC_EVENT_ID_TPC7_DEC : +case GOYA_ASYNC_EVENT_ID_MME_WACS : +case GOYA_ASYNC_EVENT_ID_MME_WACSD : +case GOYA_ASYNC_EVENT_ID_CPU_AXI_SPLITTER : +case GOYA_ASYNC_EVENT_ID_PSOC_AXI_DEC : +case GOYA_ASYNC_EVENT_ID_PSOC : +case GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC1_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC2_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC3_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC4_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC5_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC6_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC7_KRN_ERR : +case GOYA_ASYNC_EVENT_ID_TPC0_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC1_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC2_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC3_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC4_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC5_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC6_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC7_CMDQ : +case GOYA_ASYNC_EVENT_ID_TPC0_QM : +case GOYA_ASYNC_EVENT_ID_TPC1_QM : +case GOYA_ASYNC_EVENT_ID_TPC2_QM : +case GOYA_ASYNC_EVENT_ID_TPC3_QM : +case GOYA_ASYNC_EVENT_ID_TPC4_QM : +case GOYA_ASYNC_EVENT_ID_TPC5_QM : +case GOYA_ASYNC_EVENT_ID_TPC6_QM : +case GOYA_ASYNC_EVENT_ID_TPC7_QM : +case GOYA_ASYNC_EVENT_ID_MME_QM : +case GOYA_ASYNC_EVENT_ID_MME_CMDQ : +case GOYA_ASYNC_EVENT_ID_DMA0_QM : +case GOYA_ASYNC_EVENT_ID_DMA1_QM : +case GOYA_ASYNC_EVENT_ID_DMA2_QM : +case GOYA_ASYNC_EVENT_ID_DMA3_QM : +case GOYA_ASYNC_EVENT_ID_DMA4_QM : +case GOYA_ASYNC_EVENT_ID_DMA0_CH : +case GOYA_ASYNC_EVENT_ID_DMA1_CH : +case GOYA_ASYNC_EVENT_ID_DMA2_CH : +case GOYA_ASYNC_EVENT_ID_DMA3_CH : +case GOYA_ASYNC_EVENT_ID_DMA4_CH : +goya_print_irq_info (hdev, event_type); +goya_unmask_irq (hdev, event_type); +break ; + +case GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC1_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC2_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC3_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC4_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC5_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC6_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_TPC7_BMON_SPMU : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH0 : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH1 : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH2 : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH3 : +case GOYA_ASYNC_EVENT_ID_DMA_BM_CH4 : +dev_info (hdev->dev,"Received H/W interrupt %d\n" , event_type); +break ; + +default : +dev_err (hdev->dev,"Received invalid H/W interrupt %d\n" , + event_type); +break ; + } +} + +void *goya_get_events_stat (struct hl_device *hdev,u32 *size) +{ +struct goya_device *goya = hdev->asic_specific; + + *size = (u32 )sizeof (goya->events_stat); + +return goya->events_stat; +} +static void goya_hw_queues_lock (struct hl_device *hdev) {@@ -3794,6 +4294,7 @@ static const struct hl_asic_funcs goya_funcs = { .sw_fini = goya_sw_fini, .hw_init = goya_hw_init, .hw_fini = goya_hw_fini, + .halt_engines = goya_halt_engines, .suspend = goya_suspend, .resume = goya_resume, .mmap = goya_mmap,@@ -3808,6 +4309,9 @@ static const struct hl_asic_funcs goya_funcs = { .dma_pool_free = goya_dma_pool_free, .cpu_accessible_dma_pool_alloc = goya_cpu_accessible_dma_pool_alloc, .cpu_accessible_dma_pool_free = goya_cpu_accessible_dma_pool_free, + .update_eq_ci = goya_update_eq_ci, + .handle_eqe = goya_handle_eqe, + .get_events_stat = goya_get_events_stat, .hw_queues_lock = goya_hw_queues_lock, .hw_queues_unlock = goya_hw_queues_unlock, .send_cpu_message = goya_send_cpu_messagedrivers/misc/habanalabs/goya/goyaP.h +1
-0@@ -123,6 +123,7 @@ struct goya_device {/* TODO: remove hw_queues_lock after moving to scheduler code */ spinlock_t hw_queues_lock;u64 ddr_bar_cur_addr; +u32 events_stat[GOYA_ASYNC_EVENT_ID_SIZE ];u32 hw_cap_initialized; };drivers/misc/habanalabs/habanalabs.h +37
-0@@ -83,6 +83,7 @@ struct hw_queue_properties {* @cfg_size: configuration space size on SRAM. * @sram_size: total size of SRAM. * @max_asid: maximum number of open contexts (ASIDs). +* @num_of_events: number of possible internal H/W IRQs. * @completion_queues_count: number of completion queues. * @high_pll: high PLL frequency used by the device. * @cb_pool_cb_cnt: number of CBs in the CB pool. @@ -109,6 +110,7 @@ struct asic_fixed_properties {u32 cfg_size;u32 sram_size;u32 max_asid; +u32 num_of_events;u32 high_pll;u32 cb_pool_cb_cnt;u32 cb_pool_cb_size;@@ -209,6 +211,9 @@ struct hl_cs_job;#define HL_CQ_LENGTH HL_QUEUE_LENGTH#define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE) +/* Must be power of 2 (HL_PAGE_SIZE / HL_EQ_ENTRY_SIZE) */ +#define HL_EQ_LENGTH 64 +#define HL_EQ_SIZE_IN_BYTES (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)/** @@ -256,6 +261,20 @@ struct hl_cq {atomic_t free_slots_cnt; }; +/** +* struct hl_eq - describes the event queue (single one per device) +* @hdev: pointer to the device structure +* @kernel_address: holds the queue's kernel virtual address +* @bus_address: holds the queue's DMA address +* @ci: ci inside the queue +*/ +struct hl_eq { +struct hl_device *hdev; +u64 kernel_address; +dma_addr_t bus_address; +u32 ci; +}; +@@ -288,6 +307,9 @@ enum hl_asic_type {* @sw_fini: tears down driver state, does not configure H/W. * @hw_init: sets up the H/W state. * @hw_fini: tears down the H/W state. +* @halt_engines: halt engines, needed for reset sequence. This also disables +* interrupts from the device. Should be called before +* hw_fini and before CS rollback. * @suspend: handles IP specific H/W or SW changes for suspend. * @resume: handles IP specific H/W or SW changes for resume. * @mmap: mmap function, does nothing. @@ -303,6 +325,9 @@ enum hl_asic_type {* @dma_pool_free: free small DMA allocation from pool. * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool. * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool. +* @update_eq_ci: update event queue CI. +* @handle_eqe: handle event queue entry (IRQ) from ArmCP. +* @get_events_stat: retrieve event queue entries histogram. * @hw_queues_lock: acquire H/W queues lock. * @hw_queues_unlock: release H/W queues lock. * @send_cpu_message: send buffer to ArmCP. @@ -314,6 +339,7 @@ struct hl_asic_funcs {int (*sw_fini)(struct hl_device *hdev);int (*hw_init)(struct hl_device *hdev);void (*hw_fini)(struct hl_device *hdev,bool hard_reset); +void (*halt_engines)(struct hl_device *hdev,bool hard_reset);int (*suspend)(struct hl_device *hdev);int (*resume)(struct hl_device *hdev);int (*mmap)(struct hl_fpriv *hpriv,struct vm_area_struct *vma);@@ -336,6 +362,10 @@ struct hl_asic_funcs { size_t size, dma_addr_t *dma_handle);void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,size_t size,void *vaddr); +void (*update_eq_ci)(struct hl_device *hdev,u32 val); +void (*handle_eqe)(struct hl_device *hdev, +struct hl_eq_entry *eq_entry); +void * (*get_events_stat)(struct hl_device *hdev,u32 *size);void (*hw_queues_lock)(struct hl_device *hdev);void (*hw_queues_unlock)(struct hl_device *hdev); int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,@@ -474,6 +504,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @kernel_ctx: KMD context structure. * @kernel_queues: array of hl_hw_queue. * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs. +* @event_queue: event queue for IRQ from ArmCP. * @dma_pool: DMA pool for small allocations. * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address. * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address. @@ -504,9 +535,11 @@ struct hl_device {enum hl_asic_type asic_type;struct hl_cq *completion_queue;struct workqueue_struct *cq_wq; +struct workqueue_struct *eq_wq;struct hl_ctx *kernel_ctx;struct hl_hw_queue *kernel_queues;struct hl_cb_mgr kernel_cb_mgr; +struct hl_eq event_queue;struct dma_pool *dma_pool;void *cpu_accessible_dma_mem;dma_addr_t cpu_accessible_dma_address;@@ -593,6 +626,10 @@ void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);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); +int hl_eq_init (struct hl_device *hdev,struct hl_eq *q); +void hl_eq_fini (struct hl_device *hdev,struct hl_eq *q); +irqreturn_t hl_irq_handler_cq (int irq,void *arg); +irqreturn_t hl_irq_handler_eq (int irq,void *arg);int hl_asid_init (struct hl_device *hdev);void hl_asid_fini (struct hl_device *hdev);unsigned long hl_asid_alloc (struct hl_device *hdev);drivers/misc/habanalabs/include/goya/goya.h +0
-1@@ -65,7 +65,6 @@#define GOYA_MSIX_ENTRIES 8#define EVENT_QUEUE_MSIX_IDX 5 -#define ARMCP_RESET_MSIX_IDX 6#define QMAN_PQ_ENTRY_SIZE 16/* Bytes */ drivers/misc/habanalabs/irq.c +75
-0@@ -9,6 +9,18 @@#include <linux/dma-mapping.h> +/** +* This structure is used to schedule work of EQ entry and armcp_reset event +* +* @eq_work - workqueue object to run when EQ entry is received +* @hdev - pointer to device structure +* @eq_entry - copy of the EQ entry +*/ +struct hl_eqe_work { +struct work_struct eq_work; +struct hl_device *hdev; +struct hl_eq_entry eq_entry; +};/** * hl_cq_inc_ptr - increment ci or pi of cq @@ -26,6 +38,33 @@ inline u32 hl_cq_inc_ptr(u32 ptr)return ptr; } +/** +* hl_eq_inc_ptr - increment ci of eq +* +* @ptr: the current ci value of the event queue +* +* Increment ptr by 1. If it reaches the number of event queue +* entries, set it to 0 +*/ +inline u32 hl_eq_inc_ptr (u32 ptr) +{ + ptr++; +if (unlikely (ptr ==HL_EQ_LENGTH )) + ptr =0 ; +return ptr; +} + +static void irq_handle_eqe (struct work_struct *work) +{ + struct hl_eqe_work *eqe_work = container_of(work, struct hl_eqe_work, + eq_work); +struct hl_device *hdev = eqe_work->hdev; + + hdev->asic_funcs->handle_eqe (hdev, &eqe_work->eq_entry); + +kfree (eqe_work); +} +/** * hl_irq_handler_cq - irq handler for completion queue * @@ -103,6 +142,68 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg)return IRQ_HANDLED ; } +/** +* hl_irq_handler_eq - irq handler for event queue +* +* @irq: irq number +* @arg: pointer to event queue structure +* +*/ +irqreturn_t hl_irq_handler_eq (int irq,void *arg) +{ +struct hl_eq *eq = arg; +struct hl_device *hdev = eq->hdev; +struct hl_eq_entry *eq_entry; +struct hl_eq_entry *eq_base; +struct hl_eqe_work *handle_eqe_work; + + eq_base = (struct hl_eq_entry *) eq->kernel_address; + +while (1 ) { +bool entry_ready = + ((eq_base[eq->ci].hdr.ctl &EQ_CTL_READY_MASK ) + >>EQ_CTL_READY_SHIFT ); + +if (!entry_ready) +break ; + + eq_entry = &eq_base[eq->ci]; + +/* +* Make sure we read EQ entry contents after we've +* checked the ownership bit. +*/ +dma_rmb (); + +if (hdev->disabled) { + dev_warn(hdev->dev, + "Device disabled but received IRQ %d for EQ\n",
Quoted 8 lines
drivers/misc/habanalabs/irq.c irqreturn_t hl_irq_handler_cq(int irq, void *arg) · line 186
Quoted 6 lines
drivers/misc/habanalabs/irq.c +6
-0+ irq); + goto skip_irq; + } + + handle_eqe_work =kmalloc (sizeof (*handle_eqe_work),GFP_ATOMIC ); +if (handle_eqe_work) {I couldn't find where is it freed
In irq_handle_eqe()
Quoted 79 lines
Quoted 72 lines
drivers/misc/habanalabs/irq.c +63
-1+INIT_WORK (&handle_eqe_work->eq_work, irq_handle_eqe); + handle_eqe_work->hdev = hdev; + +memcpy (&handle_eqe_work->eq_entry, eq_entry, +sizeof (*eq_entry)); + +queue_work (hdev->eq_wq, &handle_eqe_work->eq_work); + } +skip_irq: +/* Clear EQ entry ready bit */ + eq_entry->hdr.ctl &= ~EQ_CTL_READY_MASK ; + + eq->ci =hl_eq_inc_ptr (eq->ci); + + hdev->asic_funcs->update_eq_ci (hdev, eq->ci); + } + +return IRQ_HANDLED ; +} +/** * hl_cq_init - main initialization function for an cq object * @@ -148,3 +249,46 @@ void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q) hdev->asic_funcs->dma_free_coherent (hdev,HL_CQ_SIZE_IN_BYTES , (void *) q->kernel_address, q->bus_address); } + +/** +* hl_eq_init - main initialization function for an event queue object +* +* @hdev: pointer to device structure +* @q: pointer to eq structure +* +* Allocate dma-able memory for the event queue and initialize fields +* Returns 0 on success +*/ +int hl_eq_init (struct hl_device *hdev,struct hl_eq *q) +{ +void *p; + +BUILD_BUG_ON (HL_EQ_SIZE_IN_BYTES >HL_PAGE_SIZE ); + + p = hdev->asic_funcs->dma_alloc_coherent (hdev,HL_EQ_SIZE_IN_BYTES , + &q->bus_address,GFP_KERNEL | __GFP_ZERO); +if (!p) +return -ENOMEM ; + + q->hdev = hdev; + q->kernel_address = (u64 ) p; + q->ci =0 ; + +return 0 ; +} + +/** +* hl_eq_fini - destroy event queue +* +* @hdev: pointer to device structure +* @q: pointer to eq structure +* +* Free the event queue memory +*/ +void hl_eq_fini (struct hl_device *hdev,struct hl_eq *q) +{ +flush_workqueue (hdev->eq_wq); + + hdev->asic_funcs->dma_free_coherent (hdev,HL_EQ_SIZE_IN_BYTES , + (void *) q->kernel_address, q->bus_address); +} --Quoted 2 lines
2.17.1
--
Sincerely yours,
Mike.
PATCH 9/15 habanalabs: add sysfs and hwmon support
In reply to Oded GabbayThis patch add the sysfs and hwmon entries that are exposed by the driver.
Goya has several sensors, from various categories such as temperature, voltage, current, etc. The driver exposes those sensors in the standard hwmon mechanism.
In addition, the driver exposes a couple of interfaces in sysfs, both for configuration and for providing status of the device or driver.
The configuration attributes is for Power Management:
- Automatic or manual
- Frequency value when moving to high frequency mode
- Maximum power the device is allowed to consume
The rest of the attributes are read-only and provide the following information:
- Versions of the various firmwares running on the device
- Contents of the device's EEPROM
- The device type (currently only Goya is supported)
- PCI address of the device (to allow user-space to connect between /dev/hlX to PCI address)
- Status of the device (operational, malfunction, in_reset)
- How many processes are open on the device's file
--- .../ABI/testing/sysfs-driver-habanalabs | 190 ++++++ drivers/misc/habanalabs/Makefile | 2 +- drivers/misc/habanalabs/device.c | 146 +++++ drivers/misc/habanalabs/goya/Makefile | 2 +- drivers/misc/habanalabs/goya/goya.c | 230 +++++++ drivers/misc/habanalabs/goya/goyaP.h | 21 + drivers/misc/habanalabs/goya/goya_hwmgr.c | 306 +++++++++ drivers/misc/habanalabs/habanalabs.h | 97 +++ drivers/misc/habanalabs/habanalabs_drv.c | 7 + drivers/misc/habanalabs/hwmon.c | 449 +++++++++++++ drivers/misc/habanalabs/sysfs.c | 588 ++++++++++++++++++ 11 files changed, 2036 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-habanalabs create mode 100644 drivers/misc/habanalabs/goya/goya_hwmgr.c create mode 100644 drivers/misc/habanalabs/hwmon.c create mode 100644 drivers/misc/habanalabs/sysfs.c
Documentation/ABI/testing/sysfs-driver-habanalabs new file +190 -0
@@ -0,0 +1,190 @@ +What: /sys/class/habanalabs/hl<n>/armcp_kernel_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Linux kernel running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/armcp_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the application running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/cpld_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's CPLD F/W + +What: /sys/class/habanalabs/hl<n>/device_type +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the code name of the device according to its type. + The supported values are: "GOYA" + +What: /sys/class/habanalabs/hl<n>/eeprom +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: A binary file attribute that contains the contents of the + on-board EEPROM + +What: /sys/class/habanalabs/hl<n>/fuse_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the device's version from the eFuse + +What: /sys/class/habanalabs/hl<n>/hard_reset +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Interface to trigger a hard-reset operation for the device. + Hard-reset will reset ALL internal components of the device + except for the PCI interface and the internal PLLs + +What: /sys/class/habanalabs/hl<n>/hard_reset_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays how many times the device have undergone a hard-reset + operation + +What: /sys/class/habanalabs/hl<n>/high_pll +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency for MME, TPC + and IC when the power management profile is set to "automatic". + +What: /sys/class/habanalabs/hl<n>/ic_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + Interconnect fabric. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device IC clock might be set to lower value then the + maximum. The user should read the ic_clk_curr to see the actual + frequency value of the IC + +What: /sys/class/habanalabs/hl<n>/ic_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the Interconnect fabric + +What: /sys/class/habanalabs/hl<n>/infineon_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's power supply F/W code + +What: /sys/class/habanalabs/hl<n>/max_power +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum power consumption of the + device in milliwatts. + +What: /sys/class/habanalabs/hl<n>/mme_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + MME compute engine. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device MME clock might be set to lower value then the + maximum. The user should read the mme_clk_curr to see the actual + frequency value of the MME + +What: /sys/class/habanalabs/hl<n>/mme_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the MME compute engine + +What: /sys/class/habanalabs/hl<n>/pci_addr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the PCI address of the device. This is needed so the + user would be able to open a device based on its PCI address + +What: /sys/class/habanalabs/hl<n>/pm_mng_profile +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Power management profile. Values are "auto", "manual". In "auto" + mode, the driver will set the maximum clock frequency to a high + value when a user-space process opens the device's file (unless + it was already opened by another process). The driver will set + the max clock frequency to a low value when there are no user + processes that are opened on the device's file. In "manual" + mode, the user sets the maximum clock frequency by writing to + ic_clk, mme_clk and tpc_clk + + +What: /sys/class/habanalabs/hl<n>/preboot_btl_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the device's preboot F/W code + +What: /sys/class/habanalabs/hl<n>/soft_reset +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Interface to trigger a soft-reset operation for the device. + Soft-reset will reset only the compute and DMA engines of the + device + +What: /sys/class/habanalabs/hl<n>/soft_reset_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays how many times the device have undergone a soft-reset + operation + +What: /sys/class/habanalabs/hl<n>/status +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Status of the card: "Operational", "Malfunction", "In reset". + +What: /sys/class/habanalabs/hl<n>/thermal_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's thermal daemon + +What: /sys/class/habanalabs/hl<n>/tpc_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + TPC compute engines. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device TPC clock might be set to lower value then the + maximum. The user should read the tpc_clk_curr to see the actual + frequency value of the TPC + +What: /sys/class/habanalabs/hl<n>/tpc_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the TPC compute engines + +What: /sys/class/habanalabs/hl<n>/uboot_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the u-boot running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/write_open_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the total number of user processes that are currently + opened on the device's file
drivers/misc/habanalabs/Makefile +1 -1
@@ -5,7 +5,7 @@ obj-m := habanalabs.o habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \ - command_buffer.o hw_queue.o irq.o + command_buffer.o hw_queue.o irq.o sysfs.o hwmon.o include $(src)/goya/Makefile habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/device.c +146 -0
175 lines not on this page · raw message
drivers/misc/habanalabs/goya/Makefile +1 -1
@@ -1,3 +1,3 @@ subdir-ccflags-y += -I$(src) -HL_GOYA_FILES := goya/goya.o goya/goya_security.o +HL_GOYA_FILES := goya/goya.o goya/goya_security.o goya/goya_hwmgr.o
drivers/misc/habanalabs/goya/goya.c +230 -0
282 lines not on this page · raw message
drivers/misc/habanalabs/goya/goyaP.h +21 -0
@@ -48,7 +48,10 @@#define PLL_HIGH_DEFAULT 1575000000/* 1.575 GHz */ +#define MAX_POWER_DEFAULT 200000/* 200W */ +#define GOYA_ARMCP_INFO_TIMEOUT 10000000/* 10s */ +#define GOYA_ARMCP_EEPROM_TIMEOUT 10000000/* 10s */ #define DRAM_PHYS_DEFAULT_SIZE 0x100000000ull/* 4GB */
@@ -119,9 +122,15 @@ enum goya_fw_component {struct goya_device {int (*test_cpu_queue)(struct hl_device *hdev); +int (*armcp_info_get)(struct hl_device *hdev);/* TODO: remove hw_queues_lock after moving to scheduler code */ spinlock_t hw_queues_lock; + +u64 mme_clk; +u64 tpc_clk; +u64 ic_clk; +u64 ddr_bar_cur_addr;u32 events_stat[GOYA_ASYNC_EVENT_ID_SIZE ];u32 hw_cap_initialized;
@@ -130,6 +139,18 @@ struct goya_device {int goya_test_cpu_queue (struct hl_device *hdev);int goya_send_cpu_message (struct hl_device *hdev,u32 *msg,u16 len,u32 timeout,long *result); +long goya_get_temperature (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_voltage (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_current (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_fan_speed (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr); +void goya_set_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr, +long value); +void goya_set_pll_profile (struct hl_device *hdev,enum hl_pll_frequency freq); +int goya_add_device_attr (struct hl_device *hdev); +void goya_remove_device_attr (struct hl_device *hdev);void goya_init_security (struct hl_device *hdev); +u64 goya_get_max_power (struct hl_device *hdev); +void goya_set_max_power (struct hl_device *hdev,u64 value);#endif /* GOYAP_H_ */
drivers/misc/habanalabs/goya/goya_hwmgr.c new file +306 -0
306 lines not on this page · raw message
drivers/misc/habanalabs/habanalabs.h +97 -0
231 lines not on this page · raw message
drivers/misc/habanalabs/habanalabs_drv.c +7 -0
@@ -134,6 +134,13 @@ int hl_device_open(struct inode *inode, struct file *filp) hpriv->taskpid =find_get_pid (current->pid); +/* +* Device is IDLE at this point so it is legal to change PLLs. There +* is no need to check anything because if the PLL is already HIGH, the +* set function will return without doing anything +*/ +hl_device_set_frequency (hdev,PLL_HIGH ); +return 0 ; out_err:
drivers/misc/habanalabs/hwmon.c new file +449 -0
449 lines not on this page · raw message
drivers/misc/habanalabs/sysfs.c new file +588 -0
588 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 09/15] habanalabs: add sysfs and hwmon support
Quoted 389 lines · Oded Gabbay
This patch add the sysfs and hwmon entries that are exposed by the driver.
Goya has several sensors, from various categories such as temperature, voltage, current, etc. The driver exposes those sensors in the standard hwmon mechanism.
In addition, the driver exposes a couple of interfaces in sysfs, both for configuration and for providing status of the device or driver.
The configuration attributes is for Power Management:
- Automatic or manual
- Frequency value when moving to high frequency mode
- Maximum power the device is allowed to consumeThe rest of the attributes are read-only and provide the following information:
- Versions of the various firmwares running on the device
- Contents of the device's EEPROM
- The device type (currently only Goya is supported)
- PCI address of the device (to allow user-space to connect between/dev/hlXto PCI address)
- Status of the device (operational, malfunction,in_reset)
- How many processes are open on the device's fileSigned-off-by Oded Gabbay <[email protected]>--- .../ABI/testing/sysfs-driver-habanalabs | 190 ++++++ drivers/misc/habanalabs/Makefile | 2 +- drivers/misc/habanalabs/device.c | 146 +++++ drivers/misc/habanalabs/goya/Makefile | 2 +- drivers/misc/habanalabs/goya/goya.c | 230 +++++++ drivers/misc/habanalabs/goya/goyaP.h | 21 + drivers/misc/habanalabs/goya/goya_hwmgr.c | 306 +++++++++ drivers/misc/habanalabs/habanalabs.h | 97 +++ drivers/misc/habanalabs/habanalabs_drv.c | 7 + drivers/misc/habanalabs/hwmon.c | 449 +++++++++++++ drivers/misc/habanalabs/sysfs.c | 588 ++++++++++++++++++ 11 files changed, 2036 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-habanalabs create mode 100644 drivers/misc/habanalabs/goya/goya_hwmgr.c create mode 100644 drivers/misc/habanalabs/hwmon.c create mode 100644 drivers/misc/habanalabs/sysfs.cDocumentation/ABI/testing/sysfs-driver-habanalabs new file +190
-0@@ -0,0 +1,190 @@ +What: /sys/class/habanalabs/hl<n>/armcp_kernel_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Linux kernel running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/armcp_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the application running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/cpld_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's CPLD F/W + +What: /sys/class/habanalabs/hl<n>/device_type +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the code name of the device according to its type. + The supported values are: "GOYA" + +What: /sys/class/habanalabs/hl<n>/eeprom +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: A binary file attribute that contains the contents of the + on-board EEPROM + +What: /sys/class/habanalabs/hl<n>/fuse_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the device's version from the eFuse + +What: /sys/class/habanalabs/hl<n>/hard_reset +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Interface to trigger a hard-reset operation for the device. + Hard-reset will reset ALL internal components of the device + except for the PCI interface and the internal PLLs + +What: /sys/class/habanalabs/hl<n>/hard_reset_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays how many times the device have undergone a hard-reset + operation + +What: /sys/class/habanalabs/hl<n>/high_pll +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency for MME, TPC + and IC when the power management profile is set to "automatic". + +What: /sys/class/habanalabs/hl<n>/ic_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + Interconnect fabric. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device IC clock might be set to lower value then the + maximum. The user should read the ic_clk_curr to see the actual + frequency value of the IC + +What: /sys/class/habanalabs/hl<n>/ic_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the Interconnect fabric + +What: /sys/class/habanalabs/hl<n>/infineon_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's power supply F/W code + +What: /sys/class/habanalabs/hl<n>/max_power +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum power consumption of the + device in milliwatts. + +What: /sys/class/habanalabs/hl<n>/mme_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + MME compute engine. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device MME clock might be set to lower value then the + maximum. The user should read the mme_clk_curr to see the actual + frequency value of the MME + +What: /sys/class/habanalabs/hl<n>/mme_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the MME compute engine + +What: /sys/class/habanalabs/hl<n>/pci_addr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the PCI address of the device. This is needed so the + user would be able to open a device based on its PCI address + +What: /sys/class/habanalabs/hl<n>/pm_mng_profile +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Power management profile. Values are "auto", "manual". In "auto" + mode, the driver will set the maximum clock frequency to a high + value when a user-space process opens the device's file (unless + it was already opened by another process). The driver will set + the max clock frequency to a low value when there are no user + processes that are opened on the device's file. In "manual" + mode, the user sets the maximum clock frequency by writing to + ic_clk, mme_clk and tpc_clk + + +What: /sys/class/habanalabs/hl<n>/preboot_btl_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the device's preboot F/W code + +What: /sys/class/habanalabs/hl<n>/soft_reset +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Interface to trigger a soft-reset operation for the device. + Soft-reset will reset only the compute and DMA engines of the + device + +What: /sys/class/habanalabs/hl<n>/soft_reset_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays how many times the device have undergone a soft-reset + operation + +What: /sys/class/habanalabs/hl<n>/status +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Status of the card: "Operational", "Malfunction", "In reset". + +What: /sys/class/habanalabs/hl<n>/thermal_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's thermal daemon + +What: /sys/class/habanalabs/hl<n>/tpc_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + TPC compute engines. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device TPC clock might be set to lower value then the + maximum. The user should read the tpc_clk_curr to see the actual + frequency value of the TPC + +What: /sys/class/habanalabs/hl<n>/tpc_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the TPC compute engines + +What: /sys/class/habanalabs/hl<n>/uboot_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the u-boot running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/write_open_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the total number of user processes that are currently + opened on the device's filedrivers/misc/habanalabs/Makefile +1
-1@@ -5,7 +5,7 @@ obj-m := habanalabs.o habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \ - command_buffer.o hw_queue.o irq.o + command_buffer.o hw_queue.o irq.o sysfs.o hwmon.o include $(src)/goya/Makefile habanalabs-y += $(HL_GOYA_FILES)drivers/misc/habanalabs/device.c +118
-0@@ -226,6 +226,118 @@ static void device_early_fini(struct hl_device *hdev)mutex_destroy (&hdev->device_open); } +static void set_freq_to_low_job (struct work_struct *work) +{ + struct hl_device *hdev = container_of(work, struct hl_device, + work_freq.work); + +if (atomic_read (&hdev->fd_open_cnt) ==0 ) +hl_device_set_frequency (hdev,PLL_LOW ); + +schedule_delayed_work (&hdev->work_freq, +usecs_to_jiffies (HL_PLL_LOW_JOB_FREQ_USEC )); +} + +/** +* 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; + +INIT_DELAYED_WORK (&hdev->work_freq, set_freq_to_low_job); + hdev->high_pll = hdev->asic_prop.high_pll; + +/* force setting to low frequency */ +atomic_set (&hdev->curr_pll_profile,PLL_LOW ); + +if (hdev->pm_mng_profile ==PM_AUTO ) + hdev->asic_funcs->set_pll_profile (hdev,PLL_LOW ); +else + hdev->asic_funcs->set_pll_profile (hdev,PLL_LAST ); + +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; + } + } + +schedule_delayed_work (&hdev->work_freq, +usecs_to_jiffies (HL_PLL_LOW_JOB_FREQ_USEC )); + + 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 ; + +cancel_delayed_work_sync (&hdev->work_freq); + +if (hdev->asic_funcs->late_fini) + hdev->asic_funcs->late_fini (hdev); + + hdev->late_init_done = false; +} + +/** +* hl_device_set_frequency - set the frequency of the device +* +* @hdev: pointer to habanalabs device structure +* @freq: the new frequency value +* +* Change the frequency if needed. +* We allose to set PLL to low only if there is no user process +* Returns 0 if no change was done, otherwise returns 1; +*/ +int hl_device_set_frequency (struct hl_device *hdev,enum hl_pll_frequency freq) +{ +enum hl_pll_frequency old_freq = + (freq ==PLL_HIGH ) ?PLL_LOW :PLL_HIGH ; +int ret; + +if (hdev->pm_mng_profile ==PM_MANUAL ) +return 0 ; + + ret =atomic_cmpxchg (&hdev->curr_pll_profile, old_freq, freq); +if (ret == freq) +return 0 ; + +/* +* in case we want to lower frequency, check if device is not +* opened. We must have a check here to workaround race condition with +* hl_device_open +*/ +if ((freq ==PLL_LOW ) && (atomic_read (&hdev->fd_open_cnt) >0 )) { +atomic_set (&hdev->curr_pll_profile,PLL_HIGH ); +return 0 ; + } + +dev_dbg (hdev->dev,"Changing device frequency to %s\n" , + freq ==PLL_HIGH ?"high" :"low" ); + + hdev->asic_funcs->set_pll_profile (hdev, freq); + +return 1 ; +} +/** * hl_device_suspend - initiate device suspend * @@ -386,6 +498,12 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) goto release_ctx; } + rc =hl_sysfs_init (hdev); +if (rc) { +dev_err (hdev->dev,"failed to initialize sysfs\n" ); + goto free_cb_pool; + } + rc = hdev->asic_funcs->hw_init (hdev);if (rc) {dev_err (hdev->dev,"failed to initialize the H/W\n" );
drivers/misc/habanalabs/device.c int hl_device_init(struct hl_device *hdev, struct class *hclass) · line 529
Quoted 9 lines
drivers/misc/habanalabs/device.c +6
-0@@ -403,11 +521,33 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) goto out_disabled; } +/* After test_queues, KMD can start sending messages to device CPU */ + + rc =device_late_init (hdev); +if (rc) { +dev_err (hdev->dev,"Failed late initialization\n" ); + rc =0 ;
Isn't this an error?
drivers/misc/habanalabs/device.c int hl_device_init(struct hl_device *hdev, struct class *hclass) · line 540
Quoted 11 lines
drivers/misc/habanalabs/device.c +11
-0+ goto out_disabled; + } + +dev_info (hdev->dev,"Found %s device with %lluGB DRAM\n" , + hdev->asic_name, + hdev->asic_prop.dram_size /1024 /1024 /1024 ); + + rc =hl_hwmon_init (hdev); +if (rc) { +dev_err (hdev->dev,"Failed to initialize hwmon\n" ); + rc =0 ;
Ditto
Quoted 182 lines
drivers/misc/habanalabs/device.c +11
-0+ goto out_disabled; + } +dev_notice (hdev->dev,"Successfully added device to habanalabs driver\n" );return 0 ; +free_cb_pool: +hl_cb_pool_fini (hdev); release_ctx:if (hl_ctx_put (hdev->kernel_ctx) !=1 ) dev_err(hdev->dev,@@ -457,6 +597,12 @@ void hl_device_fini(struct hl_device *hdev)/* Mark device as disabled */ hdev->disabled = true; +hl_hwmon_fini (hdev); + +device_late_fini (hdev); + +hl_sysfs_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 drivers/misc/habanalabs/goya/Makefile +1
-1@@ -1,3 +1,3 @@ subdir-ccflags-y += -I$(src) -HL_GOYA_FILES := goya/goya.o goya/goya_security.o +HL_GOYA_FILES := goya/goya.o goya/goya_security.o goya/goya_hwmgr.odrivers/misc/habanalabs/goya/goya.c +106
-0@@ -127,6 +127,8 @@ static const char *goya_axi_name[GOYA_MAX_INITIATORS] = {#define GOYA_ASYC_EVENT_GROUP_NON_FATAL_SIZE 121 +static int goya_armcp_info_get (struct hl_device *hdev); +static void goya_get_fixed_properties (struct hl_device *hdev) {struct asic_fixed_properties *prop = &hdev->asic_prop;@@ -174,6 +176,7 @@ static void goya_get_fixed_properties(struct hl_device *hdev) prop->num_of_events =GOYA_ASYNC_EVENT_ID_SIZE ; prop->cb_pool_cb_cnt =GOYA_CB_POOL_CB_CNT ; prop->cb_pool_cb_size =GOYA_CB_POOL_CB_SIZE ; + prop->max_power_default =MAX_POWER_DEFAULT ; prop->tpc_enabled_mask =TPC_ENABLED_MASK ; prop->high_pll =PLL_HIGH_DEFAULT ;@@ -558,6 +561,89 @@ int goya_early_fini(struct hl_device *hdev)return 0 ; } +/** +* goya_fetch_psoc_frequency - Fetch PSOC frequency values +* +* @hdev: pointer to hl_device structure +* +*/ +static void goya_fetch_psoc_frequency (struct hl_device *hdev) +{ +struct asic_fixed_properties *prop = &hdev->asic_prop; + + prop->psoc_pci_pll_nr =RREG32 (mmPSOC_PCI_PLL_NR); + prop->psoc_pci_pll_nf =RREG32 (mmPSOC_PCI_PLL_NF); + prop->psoc_pci_pll_od =RREG32 (mmPSOC_PCI_PLL_OD); + prop->psoc_pci_pll_div_factor =RREG32 (mmPSOC_PCI_PLL_DIV_FACTOR_1); +} + +/** +* goya_late_init - GOYA late initialization code +* +* @hdev: pointer to hl_device structure +* +* Get ArmCP info and send message to CPU to enable PCI access +*/ +static int goya_late_init (struct hl_device *hdev) +{ +struct asic_fixed_properties *prop = &hdev->asic_prop; +struct goya_device *goya = hdev->asic_specific; +int rc; + + rc = goya->armcp_info_get (hdev); +if (rc) { +dev_err (hdev->dev,"Failed to get armcp info\n" ); +return rc; + } + +/* Now that we have the DRAM size in ASIC prop, we need to check +* its size and configure the DMA_IF DDR wrap protection (which is in +* the MMU block) accordingly. The value is the log2 of the DRAM size +*/ +WREG32 (mmMMU_LOG2_DDR_SIZE,ilog2 (prop->dram_size)); + + rc =goya_send_pci_access_msg (hdev,ARMCP_PACKET_ENABLE_PCI_ACCESS ); +if (rc) { +dev_err (hdev->dev,"Failed to enable PCI access from CPU\n" ); +return rc; + } + +WREG32 (mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, +GOYA_ASYNC_EVENT_ID_INTS_REGISTER ); + +goya_fetch_psoc_frequency (hdev); + +return 0 ; +} + +/** +* goya_late_fini - GOYA late tear-down code +* +* @hdev: pointer to hl_device structure +* +* Free sensors allocated structures +*/ +void goya_late_fini (struct hl_device *hdev) +{ +const struct hwmon_channel_info **channel_info_arr; +int i =0 ; + +if (!hdev->hl_chip_info.info) +return ; + + channel_info_arr = hdev->hl_chip_info.info; + +while (channel_info_arr[i]) { +kfree (channel_info_arr[i]->config); +kfree (channel_info_arr[i]); + i++; + } + +kfree (channel_info_arr); + + hdev->hl_chip_info.info =NULL ; +} +/** * goya_sw_init - Goya software initialization code * @@ -575,9 +661,15 @@ static int goya_sw_init(struct hl_device *hdev)return -ENOMEM ; goya->test_cpu_queue = goya_test_cpu_queue; + goya->armcp_info_get = goya_armcp_info_get;/* according to goya_init_iatu */ goya->ddr_bar_cur_addr =DRAM_PHYS_BASE ; + + goya->mme_clk =GOYA_PLL_FREQ_LOW ; + goya->tpc_clk =GOYA_PLL_FREQ_LOW ; + goya->ic_clk =GOYA_PLL_FREQ_LOW ; + hdev->asic_specific = goya;/* Create DMA pool for small allocations */ @@ -4272,6 +4364,87 @@ void *goya_get_events_stat(struct hl_device *hdev, u32 *size)return goya->events_stat; } +static int goya_armcp_info_get (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +struct asic_fixed_properties *prop = &hdev->asic_prop; +struct armcp_packet pkt; +void *armcp_info_cpu_addr; +dma_addr_t armcp_info_dma_addr; +u64 dram_size; +long result; +int rc; + +if (!(goya->hw_cap_initialized &HW_CAP_CPU_Q )) +return 0 ; +
drivers/misc/habanalabs/goya/goya.c void *goya_get_events_stat(struct hl_device *hdev, u32 *size) · line 4390
Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10
-0+ armcp_info_cpu_addr = + hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, +sizeof (struct armcp_info ), &armcp_info_dma_addr); +if (!armcp_info_cpu_addr) { +dev_err (hdev->dev, +"Failed to allocate DMA memory for ArmCP info packet\n" ); +return -ENOMEM ; + } + +memset (armcp_info_cpu_addr,0 ,sizeof (struct armcp_info ));
Do you expect usage of cpu_accessible_dma_pool_alloc() without the need to clear the memory?
If not memset(0) can be moved inside that function.
Quoted 1,821 lines
drivers/misc/habanalabs/goya/goya.c +114
-0+ +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_INFO_GET ; + pkt.addr = armcp_info_dma_addr + prop->host_phys_base_address; + pkt.data_max_size =sizeof (struct armcp_info ); + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +GOYA_ARMCP_INFO_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to send armcp info pkt, error %d\n" , rc); + goto out; + } + +memcpy (&prop->armcp_info, armcp_info_cpu_addr, +sizeof (prop->armcp_info)); + + dram_size = prop->armcp_info.dram_size; +if (dram_size) { +if ((!is_power_of_2 (dram_size)) || + (dram_size <DRAM_PHYS_DEFAULT_SIZE )) { +dev_err (hdev->dev, +"F/W reported invalid DRAM size %llu. Trying to use default size\n" , + dram_size); + dram_size =DRAM_PHYS_DEFAULT_SIZE ; + } + + prop->dram_size = dram_size; + prop->dram_end_address = prop->dram_base_address + dram_size; + } + + rc =hl_build_hwmon_channel_info (hdev, prop->armcp_info.sensors); +if (rc) { +dev_err (hdev->dev, +"Failed to build hwmon channel info, error %d\n" , rc); + rc = -EFAULT ; + goto out; + } + +out: + hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, +sizeof (struct armcp_info ), armcp_info_cpu_addr); + +return rc; +} + +static void goya_init_clock_gating (struct hl_device *hdev) +{ + +} + +static void goya_disable_clock_gating (struct hl_device *hdev) +{ + +}static void goya_hw_queues_lock (struct hl_device *hdev) {@@ -4287,9 +4460,60 @@ static void goya_hw_queues_unlock(struct hl_device *hdev)spin_unlock (&goya->hw_queues_lock); } +int goya_get_eeprom_data (struct hl_device *hdev,void *data,size_t max_size) +{ +struct goya_device *goya = hdev->asic_specific; +struct asic_fixed_properties *prop = &hdev->asic_prop; +struct armcp_packet pkt; +void *eeprom_info_cpu_addr; +dma_addr_t eeprom_info_dma_addr; +long result; +int rc; + +if (!(goya->hw_cap_initialized &HW_CAP_CPU_Q )) +return 0 ; + + eeprom_info_cpu_addr = + hdev->asic_funcs->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 EEPROM info packet\n" ); +return -ENOMEM ; + } + +memset (eeprom_info_cpu_addr,0 , max_size); + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_EEPROM_DATA_GET ; + pkt.addr = eeprom_info_dma_addr + prop->host_phys_base_address; + pkt.data_max_size = max_size; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +GOYA_ARMCP_EEPROM_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to send armcp EEPROM pkt, error %d\n" , rc); + goto out; + } + +/* result contains the actual 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); + +return rc; +} +static const struct hl_asic_funcs goya_funcs = { .early_init = goya_early_init, .early_fini = goya_early_fini, + .late_init = goya_late_init, + .late_fini = goya_late_fini, .sw_init = goya_sw_init, .sw_fini = goya_sw_fini, .hw_init = goya_hw_init,@@ -4310,10 +4534,16 @@ static const struct hl_asic_funcs goya_funcs = { .cpu_accessible_dma_pool_alloc = goya_cpu_accessible_dma_pool_alloc, .cpu_accessible_dma_pool_free = goya_cpu_accessible_dma_pool_free, .update_eq_ci = goya_update_eq_ci, + .add_device_attr = goya_add_device_attr, + .remove_device_attr = goya_remove_device_attr, .handle_eqe = goya_handle_eqe, + .set_pll_profile = goya_set_pll_profile, .get_events_stat = goya_get_events_stat, + .enable_clock_gating = goya_init_clock_gating, + .disable_clock_gating = goya_disable_clock_gating, .hw_queues_lock = goya_hw_queues_lock, .hw_queues_unlock = goya_hw_queues_unlock, + .get_eeprom_data = goya_get_eeprom_data, .send_cpu_message = goya_send_cpu_message };drivers/misc/habanalabs/goya/goyaP.h +21
-0@@ -48,7 +48,10 @@#define PLL_HIGH_DEFAULT 1575000000/* 1.575 GHz */ +#define MAX_POWER_DEFAULT 200000/* 200W */ +#define GOYA_ARMCP_INFO_TIMEOUT 10000000/* 10s */ +#define GOYA_ARMCP_EEPROM_TIMEOUT 10000000/* 10s */ #define DRAM_PHYS_DEFAULT_SIZE 0x100000000ull/* 4GB */ @@ -119,9 +122,15 @@ enum goya_fw_component {struct goya_device {int (*test_cpu_queue)(struct hl_device *hdev); +int (*armcp_info_get)(struct hl_device *hdev);/* TODO: remove hw_queues_lock after moving to scheduler code */ spinlock_t hw_queues_lock; + +u64 mme_clk; +u64 tpc_clk; +u64 ic_clk; +u64 ddr_bar_cur_addr;u32 events_stat[GOYA_ASYNC_EVENT_ID_SIZE ];u32 hw_cap_initialized;@@ -130,6 +139,18 @@ struct goya_device {int goya_test_cpu_queue (struct hl_device *hdev);int goya_send_cpu_message (struct hl_device *hdev,u32 *msg,u16 len,u32 timeout,long *result); +long goya_get_temperature (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_voltage (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_current (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_fan_speed (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr); +void goya_set_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr, +long value); +void goya_set_pll_profile (struct hl_device *hdev,enum hl_pll_frequency freq); +int goya_add_device_attr (struct hl_device *hdev); +void goya_remove_device_attr (struct hl_device *hdev);void goya_init_security (struct hl_device *hdev); +u64 goya_get_max_power (struct hl_device *hdev); +void goya_set_max_power (struct hl_device *hdev,u64 value);#endif /* GOYAP_H_ */ drivers/misc/habanalabs/goya/goya_hwmgr.c new file +306
-0@@ -0,0 +1,306 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include "goyaP.h" + +void goya_set_pll_profile (struct hl_device *hdev,enum hl_pll_frequency freq) +{ +struct goya_device *goya = hdev->asic_specific; + +switch (freq) { +case PLL_HIGH : +hl_set_frequency (hdev,MME_PLL , hdev->high_pll); +hl_set_frequency (hdev,TPC_PLL , hdev->high_pll); +hl_set_frequency (hdev,IC_PLL , hdev->high_pll); +break ; +case PLL_LOW : +hl_set_frequency (hdev,MME_PLL ,GOYA_PLL_FREQ_LOW ); +hl_set_frequency (hdev,TPC_PLL ,GOYA_PLL_FREQ_LOW ); +hl_set_frequency (hdev,IC_PLL ,GOYA_PLL_FREQ_LOW ); +break ; +case PLL_LAST : +hl_set_frequency (hdev,MME_PLL , goya->mme_clk); +hl_set_frequency (hdev,TPC_PLL , goya->tpc_clk); +hl_set_frequency (hdev,IC_PLL , goya->ic_clk); +break ; +default : +dev_err (hdev->dev,"unknown frequency setting\n" ); + } +} + +static ssize_t mme_clk_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,MME_PLL , false); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t mme_clk_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +struct goya_device *goya = hdev->asic_specific; +int rc; +long value; + +if (hdev->disabled) { + count = -ENODEV ; + goto fail; + } + +if (hdev->pm_mng_profile ==PM_AUTO ) { + count = -EPERM ; + goto fail; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto fail; + } + +hl_set_frequency (hdev,MME_PLL , value); + goya->mme_clk = value; + +fail: +return count; +} + +static ssize_t tpc_clk_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,TPC_PLL , false); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t tpc_clk_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +struct goya_device *goya = hdev->asic_specific; +int rc; +long value; + +if (hdev->disabled) { + count = -ENODEV ; + goto fail; + } + +if (hdev->pm_mng_profile ==PM_AUTO ) { + count = -EPERM ; + goto fail; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto fail; + } + +hl_set_frequency (hdev,TPC_PLL , value); + goya->tpc_clk = value; + +fail: +return count; +} + +static ssize_t ic_clk_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,IC_PLL , false); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t ic_clk_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +struct goya_device *goya = hdev->asic_specific; +int rc; +long value; + +if (hdev->disabled) { + count = -ENODEV ; + goto fail; + } + +if (hdev->pm_mng_profile ==PM_AUTO ) { + count = -EPERM ; + goto fail; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto fail; + } + +hl_set_frequency (hdev,IC_PLL , value); + goya->ic_clk = value; + +fail: +return count; +} + +static ssize_t mme_clk_curr_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,MME_PLL , true); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t tpc_clk_curr_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,TPC_PLL , true); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t ic_clk_curr_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,IC_PLL , true); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static DEVICE_ATTR_RW (mme_clk); +static DEVICE_ATTR_RW (tpc_clk); +static DEVICE_ATTR_RW (ic_clk); +static DEVICE_ATTR_RO (mme_clk_curr); +static DEVICE_ATTR_RO (tpc_clk_curr); +static DEVICE_ATTR_RO (ic_clk_curr); + +int goya_add_device_attr (struct hl_device *hdev) +{ +int rc; + + rc =device_create_file (hdev->dev, &dev_attr_mme_clk); +if (rc) { +dev_err (hdev->dev,"failed to create device file mme_clk\n" ); +return rc; + } + + rc =device_create_file (hdev->dev, &dev_attr_tpc_clk); +if (rc) { +dev_err (hdev->dev,"failed to create device file tpc_clk\n" ); + goto remove_mme_clk; + } + + rc =device_create_file (hdev->dev, &dev_attr_ic_clk); +if (rc) { +dev_err (hdev->dev,"failed to create device file ic_clk\n" ); + goto remove_tpc_clk; + } + + rc =device_create_file (hdev->dev, &dev_attr_mme_clk_curr); +if (rc) { +dev_err (hdev->dev, +"failed to create device file mme_clk_curr\n" ); + goto remove_ic_clk; + } + + rc =device_create_file (hdev->dev, &dev_attr_tpc_clk_curr); +if (rc) { +dev_err (hdev->dev, +"failed to create device file tpc_clk_curr\n" ); + goto remove_mme_clk_curr; + } + + rc =device_create_file (hdev->dev, &dev_attr_ic_clk_curr); +if (rc) { +dev_err (hdev->dev, +"failed to create device file ic_clk_curr\n" ); + goto remove_tpc_clk_curr; + } + +return 0 ; + +remove_tpc_clk_curr: +device_remove_file (hdev->dev, &dev_attr_tpc_clk_curr); +remove_mme_clk_curr: +device_remove_file (hdev->dev, &dev_attr_mme_clk_curr); +remove_ic_clk: +device_remove_file (hdev->dev, &dev_attr_ic_clk); +remove_tpc_clk: +device_remove_file (hdev->dev, &dev_attr_tpc_clk); +remove_mme_clk: +device_remove_file (hdev->dev, &dev_attr_mme_clk); +return rc; +} + +void goya_remove_device_attr (struct hl_device *hdev) +{ +device_remove_file (hdev->dev, &dev_attr_ic_clk_curr); +device_remove_file (hdev->dev, &dev_attr_tpc_clk_curr); +device_remove_file (hdev->dev, &dev_attr_mme_clk_curr); +device_remove_file (hdev->dev, &dev_attr_ic_clk); +device_remove_file (hdev->dev, &dev_attr_tpc_clk); +device_remove_file (hdev->dev, &dev_attr_mme_clk); +}drivers/misc/habanalabs/habanalabs.h +97
-0@@ -25,6 +25,8 @@#define HL_DEVICE_TIMEOUT_USEC 1000000/* 1 s */ +#define HL_PLL_LOW_JOB_FREQ_USEC 5000000/* 5 s */ +#define HL_MAX_QUEUES 128struct hl_device ;@@ -60,6 +62,8 @@ struct hw_queue_properties {/** * struct asic_fixed_properties - ASIC specific immutable properties. * @hw_queues_props: H/W queues properties. +* @armcp_info: received various information from ArmCP regarding the H/W. e.g. +* available sensors. * @uboot_ver: F/W U-boot version. * @preboot_ver: F/W Preboot version. * @sram_base_address: SRAM physical start address. @@ -72,6 +76,7 @@ struct hw_queue_properties {* @dram_pci_bar_size: size of PCI bar towards DRAM. * @host_phys_base_address: base physical address of host memory for * transactions that the device generates. +* @max_power_default: max power of the device after reset * @va_space_host_start_address: base address of virtual memory range for * mapping host memory. * @va_space_host_end_address: end address of virtual memory range for @@ -84,6 +89,10 @@ struct hw_queue_properties {* @sram_size: total size of SRAM. * @max_asid: maximum number of open contexts (ASIDs). * @num_of_events: number of possible internal H/W IRQs. +* @psoc_pci_pll_nr: PCI PLL NR value. +* @psoc_pci_pll_nf: PCI PLL NF value. +* @psoc_pci_pll_od: PCI PLL OD value. +* @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value. * @completion_queues_count: number of completion queues. * @high_pll: high PLL frequency used by the device. * @cb_pool_cb_cnt: number of CBs in the CB pool. @@ -92,6 +101,7 @@ struct hw_queue_properties {*/ struct asic_fixed_properties {struct hw_queue_properties hw_queues_props[HL_MAX_QUEUES ]; +struct armcp_info armcp_info;char uboot_ver[VERSION_MAX_LEN ];char preboot_ver[VERSION_MAX_LEN ];u64 sram_base_address;@@ -103,6 +113,7 @@ struct asic_fixed_properties {u64 dram_size;u64 dram_pci_bar_size;u64 host_phys_base_address; +u64 max_power_default;u64 va_space_host_start_address;u64 va_space_host_end_address;u64 va_space_dram_start_address;@@ -111,6 +122,10 @@ struct asic_fixed_properties {u32 sram_size;u32 max_asid;u32 num_of_events; +u32 psoc_pci_pll_nr; +u32 psoc_pci_pll_nf; +u32 psoc_pci_pll_od; +u32 psoc_pci_pll_div_factor;u32 high_pll;u32 cb_pool_cb_cnt;u32 cb_pool_cb_size;@@ -296,13 +311,37 @@ enum hl_asic_type { }; +/** +* enum hl_pm_mng_profile - power management profile. +* @PM_AUTO: internal clock is set by KMD. +* @PM_MANUAL: internal clock is set by the user. +* @PM_LAST: last power management type. +*/ +enum hl_pm_mng_profile { +PM_AUTO =1 , +PM_MANUAL , +PM_LAST +}; +/** +* enum hl_pll_frequency - PLL frequency. +* @PLL_HIGH: high frequency. +* @PLL_LOW: low frequency. +* @PLL_LAST: last frequency values that were configured by the user. +*/ +enum hl_pll_frequency { +PLL_HIGH =1 , +PLL_LOW , +PLL_LAST +};/** * struct hl_asic_funcs - ASIC specific functions that are can be called from * common code. * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W. * @early_fini: tears down what was done in early_init. +* @late_init: sets up late driver/hw state (post hw_init) - Optional. +* @late_fini: tears down what was done in late_init (pre hw_fini) - Optional. * @sw_init: sets up driver state, does not configure H/W. * @sw_fini: tears down driver state, does not configure H/W. * @hw_init: sets up the H/W state. @@ -326,15 +365,23 @@ enum hl_asic_type {* @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool. * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool. * @update_eq_ci: update event queue CI. +* @add_device_attr: add ASIC specific device attributes. +* @remove_device_attr: remove ASIC specific device attributes. * @handle_eqe: handle event queue entry (IRQ) from ArmCP. +* @set_pll_profile: change PLL profile (manual/automatic). * @get_events_stat: retrieve event queue entries histogram. +* @enable_clock_gating: enable clock gating for reducing power consumption. +* @disable_clock_gating: disable clock for accessing registers on HBW. * @hw_queues_lock: acquire H/W queues lock. * @hw_queues_unlock: release H/W queues lock. +* @get_eeprom_data: retrieve EEPROM data from F/W. * @send_cpu_message: send buffer to ArmCP. */ struct hl_asic_funcs {int (*early_init)(struct hl_device *hdev);int (*early_fini)(struct hl_device *hdev); +int (*late_init)(struct hl_device *hdev); +void (*late_fini)(struct hl_device *hdev);int (*sw_init)(struct hl_device *hdev);int (*sw_fini)(struct hl_device *hdev);int (*hw_init)(struct hl_device *hdev);@@ -363,11 +410,19 @@ struct hl_asic_funcs {void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,size_t size,void *vaddr);void (*update_eq_ci)(struct hl_device *hdev,u32 val); +int (*add_device_attr)(struct hl_device *hdev); +void (*remove_device_attr)(struct hl_device *hdev);void (*handle_eqe)(struct hl_device *hdev,struct hl_eq_entry *eq_entry); +void (*set_pll_profile)(struct hl_device *hdev, +enum hl_pll_frequency freq);void * (*get_events_stat)(struct hl_device *hdev,u32 *size); +void (*enable_clock_gating)(struct hl_device *hdev); +void (*disable_clock_gating)(struct hl_device *hdev);void (*hw_queues_lock)(struct hl_device *hdev);void (*hw_queues_unlock)(struct hl_device *hdev); +int (*get_eeprom_data)(struct hl_device *hdev,void *data, +size_t max_size);int (*send_cpu_message)(struct hl_device *hdev,u32 *msg,u16 len,u32 timeout,long *result); };@@ -496,6 +551,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @rmmio: configuration area address on SRAM. * @cdev: related char device. * @dev: realted kernel basic device structure. +* @work_freq: delayed work to lower device frequency if possible. * @asic_name: ASIC specific nmae. * @asic_type: ASIC specific type. * @completion_queue: array of hl_cq. @@ -517,13 +573,23 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @asic_prop: ASIC specific immutable properties. * @asic_funcs: ASIC specific functions. * @asic_specific: ASIC specific information to use only from ASIC files. +* @hwmon_dev: H/W monitor device. +* @pm_mng_profile: current power management profile. +* @hl_chip_info: ASIC's sensors information. * @cb_pool: list of preallocated CBs. * @cb_pool_lock: protects the CB pool. * @user_ctx: current user context executing. +* @curr_pll_profile: current PLL profile. * @fd_open_cnt: number of open context executing. +* @max_power: the max power of the device, as configured by the sysadmin. This +* value is saved so in case of hard-reset, KMD will restore this +* value and update the F/W after the re-initialization * @major: habanalabs KMD major. +* @high_pll: high PLL profile frequency. * @id: device minor. * @disabled: is device disabled. +* @late_init_done: is late init stage was done during initialization. +* @hwmon_initialized: is H/W monitor sensors was initialized. */ struct hl_device {struct pci_dev *pdev;@@ -531,6 +597,7 @@ struct hl_device {void __iomem *rmmio;struct cdev cdev;struct device *dev; +struct delayed_work work_freq;char asic_name[16 ];enum hl_asic_type asic_type;struct hl_cq *completion_queue;@@ -553,16 +620,25 @@ struct hl_device {struct asic_fixed_properties asic_prop;const struct hl_asic_funcs *asic_funcs;void *asic_specific; +struct device *hwmon_dev; +enum hl_pm_mng_profile pm_mng_profile; +struct hwmon_chip_info hl_chip_info;struct list_head cb_pool;spinlock_t cb_pool_lock;/* TODO: The following fields should be moved for multi-context */ struct hl_ctx *user_ctx; + +atomic_t curr_pll_profile;atomic_t fd_open_cnt; +u64 max_power;u32 major; +u32 high_pll;u16 id;u8 disabled; +u8 late_init_done; +u8 hwmon_initialized;/* Parameters for bring-up */ u8 cpu_enable;@@ -647,6 +723,15 @@ int hl_device_suspend(struct hl_device *hdev);int hl_device_resume (struct hl_device *hdev);void hl_hpriv_get (struct hl_fpriv *hpriv);void hl_hpriv_put (struct hl_fpriv *hpriv); +int hl_device_set_frequency (struct hl_device *hdev,enum hl_pll_frequency freq); +int hl_build_hwmon_channel_info (struct hl_device *hdev, +struct armcp_sensor *sensors_arr); + +int hl_sysfs_init (struct hl_device *hdev); +void hl_sysfs_fini (struct hl_device *hdev); + +int hl_hwmon_init (struct hl_device *hdev); +void hl_hwmon_fini (struct hl_device *hdev);int hl_cb_create (struct hl_device *hdev,struct hl_cb_mgr *mgr,u32 cb_size,u64 *handle,int ctx_id);@@ -663,6 +748,18 @@ int hl_cb_pool_fini(struct hl_device *hdev);void goya_set_asic_funcs (struct hl_device *hdev); +long hl_get_frequency (struct hl_device *hdev,u32 pll_index,bool curr); +void hl_set_frequency (struct hl_device *hdev,u32 pll_index,u64 freq); +long hl_get_temperature (struct hl_device *hdev,int sensor_index,u32 attr); +long hl_get_voltage (struct hl_device *hdev,int sensor_index,u32 attr); +long hl_get_current (struct hl_device *hdev,int sensor_index,u32 attr); +long hl_get_fan_speed (struct hl_device *hdev,int sensor_index,u32 attr); +long hl_get_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr); +void hl_set_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr, +long value); +u64 hl_get_max_power (struct hl_device *hdev); +void hl_set_max_power (struct hl_device *hdev,u64 value); +/* IOCTLs */ long hl_ioctl (struct file *filep,unsigned int cmd,unsigned long arg);int hl_cb_ioctl (struct hl_fpriv *hpriv,void *data);drivers/misc/habanalabs/habanalabs_drv.c +7
-0@@ -134,6 +134,13 @@ int hl_device_open(struct inode *inode, struct file *filp) hpriv->taskpid =find_get_pid (current->pid); +/* +* Device is IDLE at this point so it is legal to change PLLs. There +* is no need to check anything because if the PLL is already HIGH, the +* set function will return without doing anything +*/ +hl_device_set_frequency (hdev,PLL_HIGH ); +return 0 ; out_err:drivers/misc/habanalabs/hwmon.c new file +449
-0@@ -0,0 +1,449 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include "habanalabs.h" + +#define SENSORS_PKT_TIMEOUT 100000/* 100ms */ +#define HWMON_NR_SENSOR_TYPES (hwmon_pwm + 1) + +int hl_build_hwmon_channel_info (struct hl_device *hdev, +struct armcp_sensor *sensors_arr) +{ +u32 counts[HWMON_NR_SENSOR_TYPES ] = {0 }; +u32 *sensors_by_type[HWMON_NR_SENSOR_TYPES ] = {0 }; +u32 sensors_by_type_next_index[HWMON_NR_SENSOR_TYPES ] = {0 }; +struct hwmon_channel_info **channels_info; +u32 num_sensors_for_type, num_active_sensor_types =0 , + arr_size =0 , *curr_arr; +enum hwmon_sensor_types type; +int rc, i, j; + +for (i =0 ; i <ARMCP_MAX_SENSORS ; i++) { + type = sensors_arr[i].type; + +if ((type ==0 ) && (sensors_arr[i].flags ==0 )) +break ; + +if (type >=HWMON_NR_SENSOR_TYPES ) { +dev_err (hdev->dev, +"Got wrong sensor type %d from device\n" , type); +return -EINVAL ; + } + + counts[type]++; + arr_size++; + } + +for (i =0 ; i <HWMON_NR_SENSOR_TYPES ; i++) { +if (counts[i] ==0 ) +continue ; + + num_sensors_for_type = counts[i] +1 ; + curr_arr =kcalloc (num_sensors_for_type,sizeof (*curr_arr), +GFP_KERNEL ); +if (!curr_arr) { + rc = -ENOMEM ; + goto sensors_type_err; + } + + num_active_sensor_types++; + sensors_by_type[i] = curr_arr; + } + +for (i =0 ; i < arr_size ; i++) { + type = sensors_arr[i].type; + curr_arr = sensors_by_type[type]; + curr_arr[sensors_by_type_next_index[type]++] = + sensors_arr[i].flags; + } + + channels_info =kcalloc (num_active_sensor_types +1 , +sizeof (*channels_info),GFP_KERNEL ); +if (!channels_info) { + rc = -ENOMEM ; + goto channels_info_array_err; + } + +for (i =0 ; i < num_active_sensor_types ; i++) { + channels_info[i] =kzalloc (sizeof (*channels_info[i]), +GFP_KERNEL ); +if (!channels_info[i]) { + rc = -ENOMEM ; + goto channel_info_err; + } + } + +for (i =0 , j =0 ; i <HWMON_NR_SENSOR_TYPES ; i++) { +if (!sensors_by_type[i]) +continue ; + + channels_info[j]->type = i; + channels_info[j]->config = sensors_by_type[i]; + j++; + } + + hdev->hl_chip_info.info = + (const struct hwmon_channel_info **)channels_info; + +return 0 ; + +channel_info_err: +for (i =0 ; i < num_active_sensor_types ; i++) +if (channels_info[i]) { +kfree (channels_info[i]->config); +kfree (channels_info[i]); + } +kfree (channels_info); +channels_info_array_err: +sensors_type_err: +for (i =0 ; i <HWMON_NR_SENSOR_TYPES ; i++) +kfree (sensors_by_type[i]); + +return rc; +} + +static int hl_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) +return -ENODEV ; + +switch (type) { +case hwmon_temp: +switch (attr) { +case hwmon_temp_input: +case hwmon_temp_max: +case hwmon_temp_crit: +case hwmon_temp_max_hyst: +case hwmon_temp_crit_hyst: +break ; +default : +return -EINVAL ; + } + + *val =hl_get_temperature (hdev, channel, attr); +break ; +case hwmon_in: +switch (attr) { +case hwmon_in_input: +case hwmon_in_min: +case hwmon_in_max: +break ; +default : +return -EINVAL ; + } + + *val =hl_get_voltage (hdev, channel, attr); +break ; +case hwmon_curr: +switch (attr) { +case hwmon_curr_input: +case hwmon_curr_min: +case hwmon_curr_max: +break ; +default : +return -EINVAL ; + } + + *val =hl_get_current (hdev, channel, attr); +break ; +case hwmon_fan: +switch (attr) { +case hwmon_fan_input: +case hwmon_fan_min: +case hwmon_fan_max: +break ; +default : +return -EINVAL ; + } + *val =hl_get_fan_speed (hdev, channel, attr); +break ; +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_input: +case hwmon_pwm_enable: +break ; +default : +return -EINVAL ; + } + *val =hl_get_pwm_info (hdev, channel, attr); +break ; +default : +return -EINVAL ; + } +return 0 ; +} + +static int hl_write (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long val) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) +return -ENODEV ; + +switch (type) { +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_input: +case hwmon_pwm_enable: +break ; +default : +return -EINVAL ; + } +hl_set_pwm_info (hdev, channel, attr, val); +break ; +default : +return -EINVAL ; + } +return 0 ; +} + +static umode_t hl_is_visible (const void *data,enum hwmon_sensor_types type, +u32 attr,int channel) +{ +switch (type) { +case hwmon_temp: +switch (attr) { +case hwmon_temp_input: +case hwmon_temp_max: +case hwmon_temp_max_hyst: +case hwmon_temp_crit: +case hwmon_temp_crit_hyst: +return 0444 ; + } +break ; +case hwmon_in: +switch (attr) { +case hwmon_in_input: +case hwmon_in_min: +case hwmon_in_max: +return 0444 ; + } +break ; +case hwmon_curr: +switch (attr) { +case hwmon_curr_input: +case hwmon_curr_min: +case hwmon_curr_max: +return 0444 ; + } +break ; +case hwmon_fan: +switch (attr) { +case hwmon_fan_input: +case hwmon_fan_min: +case hwmon_fan_max: +return 0444 ; + } +break ; +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_input: +case hwmon_pwm_enable: +return 0644 ; + } +break ; +default : +break ; + } +return 0 ; +} + +static const struct hwmon_ops hl_hwmon_ops = { + .is_visible = hl_is_visible, + .read = hl_read, + .write = hl_write +}; + +long hl_get_temperature (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_TEMPERATURE_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get temperature from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +long hl_get_voltage (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_VOLTAGE_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get voltage from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +long hl_get_current (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_CURRENT_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get current from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +long hl_get_fan_speed (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_FAN_SPEED_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get fan speed from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +long hl_get_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_PWM_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get pwm info from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +void hl_set_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr, +long value) +{ +struct armcp_packet pkt; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_PWM_SET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + pkt.value = value; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT ,NULL ); + +if (rc) +dev_err (hdev->dev, +"Failed to set pwm info to sensor %d, error %d\n" , + sensor_index, rc); +} + +int hl_hwmon_init (struct hl_device *hdev) +{ +struct device *dev = hdev->pdev ? &hdev->pdev->dev : hdev->dev; +int rc; + +if ((hdev->hwmon_initialized) || !(hdev->fw_loading)) +return 0 ; + +if (hdev->hl_chip_info.info) { + hdev->hl_chip_info.ops = &hl_hwmon_ops; + + hdev->hwmon_dev =hwmon_device_register_with_info (dev, +"habanalabs" , hdev, &hdev->hl_chip_info,NULL ); +if (IS_ERR (hdev->hwmon_dev)) { + rc =PTR_ERR (hdev->hwmon_dev); +dev_err (hdev->dev, +"Unable to register hwmon device: %d\n" , rc); +return rc; + } + +dev_info (hdev->dev,"%s: add sensors information\n" , +dev_name (hdev->hwmon_dev)); + + hdev->hwmon_initialized = true; + }else { +dev_info (hdev->dev,"no available sensors\n" ); + } + +return 0 ; +} + +void hl_hwmon_fini (struct hl_device *hdev) +{ +if (!hdev->hwmon_initialized) +return ; + +hwmon_device_unregister (hdev->hwmon_dev); +}drivers/misc/habanalabs/sysfs.c new file +588
-0@@ -0,0 +1,588 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include "habanalabs.h" +#include "include/habanalabs_device_if.h" + +#include <linux/hwmon-sysfs.h> +#include <linux/hwmon.h> + +#define SET_CLK_PKT_TIMEOUT 200000/* 200ms */ +#define SET_PWR_PKT_TIMEOUT 400000/* 400ms */ + +long hl_get_frequency (struct hl_device *hdev,u32 pll_index,bool curr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + +if (curr) + pkt.opcode =ARMCP_PACKET_FREQUENCY_CURR_GET ; +else + pkt.opcode =ARMCP_PACKET_FREQUENCY_GET ; + pkt.pll_index = pll_index; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SET_CLK_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get frequency of PLL %d, error %d\n" , + pll_index, rc); + result = rc; + } + +return result; +} + +void hl_set_frequency (struct hl_device *hdev,u32 pll_index,u64 freq) +{ +struct armcp_packet pkt; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_FREQUENCY_SET ; + pkt.pll_index = pll_index; + pkt.value = freq; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SET_CLK_PKT_TIMEOUT ,NULL ); + +if (rc) +dev_err (hdev->dev, +"Failed to set frequency to PLL %d, error %d\n" , + pll_index, rc); +} + +u64 hl_get_max_power (struct hl_device *hdev) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_MAX_POWER_GET ; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SET_PWR_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev,"Failed to get max power, error %d\n" , rc); + result = rc; + } + +return result; +} + +void hl_set_max_power (struct hl_device *hdev,u64 value) +{ +struct armcp_packet pkt; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_MAX_POWER_SET ; + pkt.value = value; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SET_PWR_PKT_TIMEOUT ,NULL ); + +if (rc) +dev_err (hdev->dev,"Failed to set max power, error %d\n" , rc); +} + +static ssize_t pm_mng_profile_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) +return -ENODEV ; + +return snprintf (buf,PAGE_SIZE ,"%s\n" , + (hdev->pm_mng_profile ==PM_AUTO ) ?"auto" : + (hdev->pm_mng_profile ==PM_MANUAL ) ?"manual" : +"unknown" ); +} + +static ssize_t pm_mng_profile_store (struct device *dev, +struct device_attribute *attr,const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) { + count = -ENODEV ; + goto out; + } + +mutex_lock (&hdev->device_open); + +if (atomic_read (&hdev->fd_open_cnt) >0 ) { +dev_err (hdev->dev, +"Can't change PM profile while user process is opened on the device\n" ); + count = -EPERM ; + goto unlock_mutex; + } + +if (strncmp ("auto" , buf,strlen ("auto" )) ==0 ) { +/* Make sure we are in LOW PLL when changing modes */ +if (hdev->pm_mng_profile ==PM_MANUAL ) { +atomic_set (&hdev->curr_pll_profile,PLL_HIGH ); +hl_device_set_frequency (hdev,PLL_LOW ); + hdev->pm_mng_profile =PM_AUTO ; + } + }else if (strncmp ("manual" , buf,strlen ("manual" )) ==0 ) { +/* Make sure we are in LOW PLL when changing modes */ +if (hdev->pm_mng_profile ==PM_AUTO ) { +flush_delayed_work (&hdev->work_freq); + hdev->pm_mng_profile =PM_MANUAL ; + } + }else { +dev_err (hdev->dev,"value should be auto or manual\n" ); + count = -EINVAL ; + goto unlock_mutex; + } + +unlock_mutex: +mutex_unlock (&hdev->device_open); +out: +return count; +} + +static ssize_t high_pll_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) +return -ENODEV ; + +return snprintf (buf,PAGE_SIZE ,"%u\n" , hdev->high_pll); +} + +static ssize_t high_pll_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; +int rc; + +if (hdev->disabled) { + count = -ENODEV ; + goto out; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto out; + } + + hdev->high_pll = value; + +out: +return count; +} + +static ssize_t uboot_ver_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s\n" , hdev->asic_prop.uboot_ver); +} + +static ssize_t armcp_kernel_ver_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s" , + hdev->asic_prop.armcp_info.kernel_version); +} + +static ssize_t armcp_ver_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s\n" , + hdev->asic_prop.armcp_info.armcp_version); +} + +static ssize_t cpld_ver_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"0x%08x\n" , + hdev->asic_prop.armcp_info.cpld_version); +} + +static ssize_t infineon_ver_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"0x%04x\n" , + hdev->asic_prop.armcp_info.infineon_version); +} + +static ssize_t fuse_ver_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s\n" , + hdev->asic_prop.armcp_info.fuse_version); +} + +static ssize_t thermal_ver_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s" , + hdev->asic_prop.armcp_info.thermal_version); +} + +static ssize_t preboot_btl_ver_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s\n" , hdev->asic_prop.preboot_ver); +} + +static ssize_t device_type_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +char *str; + +switch (hdev->asic_type) { +case ASIC_GOYA : + str ="GOYA" ; +break ; +default : +dev_err (hdev->dev,"Unrecognized ASIC type %d\n" , + hdev->asic_type); +return -EINVAL ; + } + +return snprintf (buf,PAGE_SIZE ,"%s\n" , str); +} + +static ssize_t pci_addr_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +/* Use dummy, fixed address for simulator */ +if (!hdev->pdev) +return snprintf (buf,PAGE_SIZE ,"0000:%02d:00.0\n" , hdev->id); + +return snprintf (buf,PAGE_SIZE ,"%04x:%02x:%02x.%x\n" , +pci_domain_nr (hdev->pdev->bus), + hdev->pdev->bus->number, +PCI_SLOT (hdev->pdev->devfn), +PCI_FUNC (hdev->pdev->devfn)); +} + +static ssize_t status_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +char *str; + +if (hdev->disabled) + str ="Malfunction" ; +else + str ="Operational" ; + +return snprintf (buf,PAGE_SIZE ,"%s\n" , str); +} + +static ssize_t write_open_cnt_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%d\n" , hdev->user_ctx ?1 :0 ); +} + +static ssize_t max_power_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long val; + +if (hdev->disabled) +return -ENODEV ; + + val =hl_get_max_power (hdev); + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , val); +} + +static ssize_t max_power_store (struct device *dev, +struct device_attribute *attr,const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +unsigned long value; +int rc; + +if (hdev->disabled) { + count = -ENODEV ; + goto out; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto out; + } + + hdev->max_power = value; +hl_set_max_power (hdev, value); + +out: +return count; +} + +static ssize_t eeprom_read_handler (struct file *filp,struct kobject *kobj, +struct bin_attribute *attr,char *buf,loff_t offset, +size_t max_size) +{ + struct device *dev = container_of(kobj, struct device, kobj); +struct hl_device *hdev =dev_get_drvdata (dev); +char *data; +int rc; + +if (!max_size) +return -EINVAL ; + + data =kzalloc (max_size,GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + rc = hdev->asic_funcs->get_eeprom_data (hdev, data, max_size); +if (rc) + goto out; + +memcpy (buf, data, max_size); + +out: +kfree (data); + +return max_size; +} + +static DEVICE_ATTR_RW (pm_mng_profile); +static DEVICE_ATTR_RW (high_pll); +static DEVICE_ATTR_RO (uboot_ver); +static DEVICE_ATTR_RO (armcp_kernel_ver); +static DEVICE_ATTR_RO (armcp_ver); +static DEVICE_ATTR_RO (cpld_ver); +static DEVICE_ATTR_RO (infineon_ver); +static DEVICE_ATTR_RO (fuse_ver); +static DEVICE_ATTR_RO (thermal_ver); +static DEVICE_ATTR_RO (preboot_btl_ver); +static DEVICE_ATTR_RO (device_type); +static DEVICE_ATTR_RO (pci_addr); +static DEVICE_ATTR_RO (status); +static DEVICE_ATTR_RO (write_open_cnt); +static DEVICE_ATTR_RW (max_power); + +static const struct bin_attribute bin_attr_eeprom = { + .attr = {.name ="eeprom" , .mode = (0444 )}, + .size =PAGE_SIZE , + .read = eeprom_read_handler +}; + +int hl_sysfs_init (struct hl_device *hdev) +{ +int rc; + + rc = hdev->asic_funcs->add_device_attr (hdev); +if (rc) { +dev_err (hdev->dev,"failed to add device attributes\n" ); +return rc; + } + + rc =device_create_file (hdev->dev, &dev_attr_pm_mng_profile); +if (rc) { +dev_err (hdev->dev, +"failed to create device file pm_mng_profile\n" ); + goto remove_device_attr; + } + + hdev->pm_mng_profile =PM_AUTO ; + + rc =device_create_file (hdev->dev, &dev_attr_high_pll); +if (rc) { +dev_err (hdev->dev, +"failed to create device file pll_profile\n" ); + goto remove_pm_mng_profile; + } + + rc =device_create_file (hdev->dev, &dev_attr_uboot_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file uboot_ver\n" ); + goto remove_pll_profile; + } + + rc =device_create_file (hdev->dev, &dev_attr_armcp_kernel_ver); +if (rc) { +dev_err (hdev->dev, +"failed to create device file armcp_kernel_ver\n" ); + goto remove_uboot_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_armcp_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file armcp_ver\n" ); + goto remove_armcp_kernel_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_cpld_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file cpld_ver\n" ); + goto remove_armcp_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_infineon_ver); +if (rc) { +dev_err (hdev->dev, +"failed to create device file infineon_ver\n" ); + goto remove_cpld_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_fuse_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file fuse_ver\n" ); + goto remove_infineon_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_thermal_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file thermal_ver\n" ); + goto remove_fuse_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_preboot_btl_ver); +if (rc) { +dev_err (hdev->dev, +"failed to create device file preboot_btl_ver\n" ); + goto remove_thermal_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_device_type); +if (rc) { +dev_err (hdev->dev, +"failed to create device file device_type\n" ); + goto remove_preboot_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_pci_addr); +if (rc) { +dev_err (hdev->dev,"failed to create device file pci_addr\n" ); + goto remove_device_type; + } + + rc =device_create_file (hdev->dev, &dev_attr_status); +if (rc) { +dev_err (hdev->dev,"failed to create device file status\n" ); + goto remove_pci_addr; + } + + rc =device_create_file (hdev->dev, &dev_attr_write_open_cnt); +if (rc) { +dev_err (hdev->dev, +"failed to create device file write_open_count\n" ); + goto remove_status; + } + + hdev->max_power = hdev->asic_prop.max_power_default; + + rc =device_create_file (hdev->dev, &dev_attr_max_power); +if (rc) { +dev_err (hdev->dev, +"failed to create device file max_power\n" ); + goto remove_write_open_cnt; + } + + rc =sysfs_create_bin_file (&hdev->dev->kobj, &bin_attr_eeprom); +if (rc) { +dev_err (hdev->dev,"failed to create EEPROM sysfs entry\n" ); + goto remove_attr_max_power; + } + +return 0 ; + +remove_attr_max_power: +device_remove_file (hdev->dev, &dev_attr_max_power); +remove_write_open_cnt: +device_remove_file (hdev->dev, &dev_attr_write_open_cnt); +remove_status: +device_remove_file (hdev->dev, &dev_attr_status); +remove_pci_addr: +device_remove_file (hdev->dev, &dev_attr_pci_addr); +remove_device_type: +device_remove_file (hdev->dev, &dev_attr_device_type); +remove_preboot_ver: +device_remove_file (hdev->dev, &dev_attr_preboot_btl_ver); +remove_thermal_ver: +device_remove_file (hdev->dev, &dev_attr_thermal_ver); +remove_fuse_ver: +device_remove_file (hdev->dev, &dev_attr_fuse_ver); +remove_infineon_ver: +device_remove_file (hdev->dev, &dev_attr_infineon_ver); +remove_cpld_ver: +device_remove_file (hdev->dev, &dev_attr_cpld_ver); +remove_armcp_ver: +device_remove_file (hdev->dev, &dev_attr_armcp_ver); +remove_armcp_kernel_ver: +device_remove_file (hdev->dev, &dev_attr_armcp_kernel_ver); +remove_uboot_ver: +device_remove_file (hdev->dev, &dev_attr_uboot_ver); +remove_pll_profile: +device_remove_file (hdev->dev, &dev_attr_high_pll); +remove_pm_mng_profile: +device_remove_file (hdev->dev, &dev_attr_pm_mng_profile); +remove_device_attr: + hdev->asic_funcs->remove_device_attr (hdev); + +return rc; +} + +void hl_sysfs_fini (struct hl_device *hdev) +{ +sysfs_remove_bin_file (&hdev->dev->kobj, &bin_attr_eeprom); +device_remove_file (hdev->dev, &dev_attr_max_power); +device_remove_file (hdev->dev, &dev_attr_write_open_cnt); +device_remove_file (hdev->dev, &dev_attr_status); +device_remove_file (hdev->dev, &dev_attr_pci_addr); +device_remove_file (hdev->dev, &dev_attr_device_type); +device_remove_file (hdev->dev, &dev_attr_preboot_btl_ver); +device_remove_file (hdev->dev, &dev_attr_thermal_ver); +device_remove_file (hdev->dev, &dev_attr_fuse_ver); +device_remove_file (hdev->dev, &dev_attr_infineon_ver); +device_remove_file (hdev->dev, &dev_attr_cpld_ver); +device_remove_file (hdev->dev, &dev_attr_armcp_ver); +device_remove_file (hdev->dev, &dev_attr_armcp_kernel_ver); +device_remove_file (hdev->dev, &dev_attr_uboot_ver); +device_remove_file (hdev->dev, &dev_attr_high_pll); +device_remove_file (hdev->dev, &dev_attr_pm_mng_profile); + hdev->asic_funcs->remove_device_attr (hdev); +}Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 09/15] habanalabs: add sysfs and hwmon support
Quoted 394 lines · Mike Rapoport
Quoted 392 lines · Oded Gabbay
This patch add the sysfs and hwmon entries that are exposed by the driver.
Goya has several sensors, from various categories such as temperature, voltage, current, etc. The driver exposes those sensors in the standard hwmon mechanism.
In addition, the driver exposes a couple of interfaces in sysfs, both for configuration and for providing status of the device or driver.
The configuration attributes is for Power Management:
- Automatic or manual
- Frequency value when moving to high frequency mode
- Maximum power the device is allowed to consumeThe rest of the attributes are read-only and provide the following information:
- Versions of the various firmwares running on the device
- Contents of the device's EEPROM
- The device type (currently only Goya is supported)
- PCI address of the device (to allow user-space to connect between/dev/hlXto PCI address)
- Status of the device (operational, malfunction,in_reset)
- How many processes are open on the device's fileSigned-off-by Oded Gabbay <[email protected]>--- .../ABI/testing/sysfs-driver-habanalabs | 190 ++++++ drivers/misc/habanalabs/Makefile | 2 +- drivers/misc/habanalabs/device.c | 146 +++++ drivers/misc/habanalabs/goya/Makefile | 2 +- drivers/misc/habanalabs/goya/goya.c | 230 +++++++ drivers/misc/habanalabs/goya/goyaP.h | 21 + drivers/misc/habanalabs/goya/goya_hwmgr.c | 306 +++++++++ drivers/misc/habanalabs/habanalabs.h | 97 +++ drivers/misc/habanalabs/habanalabs_drv.c | 7 + drivers/misc/habanalabs/hwmon.c | 449 +++++++++++++ drivers/misc/habanalabs/sysfs.c | 588 ++++++++++++++++++ 11 files changed, 2036 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-habanalabs create mode 100644 drivers/misc/habanalabs/goya/goya_hwmgr.c create mode 100644 drivers/misc/habanalabs/hwmon.c create mode 100644 drivers/misc/habanalabs/sysfs.cDocumentation/ABI/testing/sysfs-driver-habanalabs new file +190
-0@@ -0,0 +1,190 @@ +What: /sys/class/habanalabs/hl<n>/armcp_kernel_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Linux kernel running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/armcp_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the application running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/cpld_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's CPLD F/W + +What: /sys/class/habanalabs/hl<n>/device_type +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the code name of the device according to its type. + The supported values are: "GOYA" + +What: /sys/class/habanalabs/hl<n>/eeprom +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: A binary file attribute that contains the contents of the + on-board EEPROM + +What: /sys/class/habanalabs/hl<n>/fuse_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the device's version from the eFuse + +What: /sys/class/habanalabs/hl<n>/hard_reset +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Interface to trigger a hard-reset operation for the device. + Hard-reset will reset ALL internal components of the device + except for the PCI interface and the internal PLLs + +What: /sys/class/habanalabs/hl<n>/hard_reset_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays how many times the device have undergone a hard-reset + operation + +What: /sys/class/habanalabs/hl<n>/high_pll +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency for MME, TPC + and IC when the power management profile is set to "automatic". + +What: /sys/class/habanalabs/hl<n>/ic_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + Interconnect fabric. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device IC clock might be set to lower value then the + maximum. The user should read the ic_clk_curr to see the actual + frequency value of the IC + +What: /sys/class/habanalabs/hl<n>/ic_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the Interconnect fabric + +What: /sys/class/habanalabs/hl<n>/infineon_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's power supply F/W code + +What: /sys/class/habanalabs/hl<n>/max_power +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum power consumption of the + device in milliwatts. + +What: /sys/class/habanalabs/hl<n>/mme_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + MME compute engine. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device MME clock might be set to lower value then the + maximum. The user should read the mme_clk_curr to see the actual + frequency value of the MME + +What: /sys/class/habanalabs/hl<n>/mme_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the MME compute engine + +What: /sys/class/habanalabs/hl<n>/pci_addr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the PCI address of the device. This is needed so the + user would be able to open a device based on its PCI address + +What: /sys/class/habanalabs/hl<n>/pm_mng_profile +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Power management profile. Values are "auto", "manual". In "auto" + mode, the driver will set the maximum clock frequency to a high + value when a user-space process opens the device's file (unless + it was already opened by another process). The driver will set + the max clock frequency to a low value when there are no user + processes that are opened on the device's file. In "manual" + mode, the user sets the maximum clock frequency by writing to + ic_clk, mme_clk and tpc_clk + + +What: /sys/class/habanalabs/hl<n>/preboot_btl_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the device's preboot F/W code + +What: /sys/class/habanalabs/hl<n>/soft_reset +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Interface to trigger a soft-reset operation for the device. + Soft-reset will reset only the compute and DMA engines of the + device + +What: /sys/class/habanalabs/hl<n>/soft_reset_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays how many times the device have undergone a soft-reset + operation + +What: /sys/class/habanalabs/hl<n>/status +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Status of the card: "Operational", "Malfunction", "In reset". + +What: /sys/class/habanalabs/hl<n>/thermal_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the Device's thermal daemon + +What: /sys/class/habanalabs/hl<n>/tpc_clk +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Allows the user to set the maximum clock frequency of the + TPC compute engines. Writes to this parameter affect the device + only when the power management profile is set to "manual" mode. + The device TPC clock might be set to lower value then the + maximum. The user should read the tpc_clk_curr to see the actual + frequency value of the TPC + +What: /sys/class/habanalabs/hl<n>/tpc_clk_curr +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the current clock frequency of the TPC compute engines + +What: /sys/class/habanalabs/hl<n>/uboot_ver +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Version of the u-boot running on the device's CPU + +What: /sys/class/habanalabs/hl<n>/write_open_cnt +Date: Jan 2019 +KernelVersion: 5.1 +Contact: [email protected] +Description: Displays the total number of user processes that are currently + opened on the device's filedrivers/misc/habanalabs/Makefile +1
-1@@ -5,7 +5,7 @@ obj-m := habanalabs.o habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \ - command_buffer.o hw_queue.o irq.o + command_buffer.o hw_queue.o irq.o sysfs.o hwmon.o include $(src)/goya/Makefile habanalabs-y += $(HL_GOYA_FILES)drivers/misc/habanalabs/device.c +118
-0@@ -226,6 +226,118 @@ static void device_early_fini(struct hl_device *hdev)mutex_destroy (&hdev->device_open); } +static void set_freq_to_low_job (struct work_struct *work) +{ + struct hl_device *hdev = container_of(work, struct hl_device, + work_freq.work); + +if (atomic_read (&hdev->fd_open_cnt) ==0 ) +hl_device_set_frequency (hdev,PLL_LOW ); + +schedule_delayed_work (&hdev->work_freq, +usecs_to_jiffies (HL_PLL_LOW_JOB_FREQ_USEC )); +} + +/** +* 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; + +INIT_DELAYED_WORK (&hdev->work_freq, set_freq_to_low_job); + hdev->high_pll = hdev->asic_prop.high_pll; + +/* force setting to low frequency */ +atomic_set (&hdev->curr_pll_profile,PLL_LOW ); + +if (hdev->pm_mng_profile ==PM_AUTO ) + hdev->asic_funcs->set_pll_profile (hdev,PLL_LOW ); +else + hdev->asic_funcs->set_pll_profile (hdev,PLL_LAST ); + +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; + } + } + +schedule_delayed_work (&hdev->work_freq, +usecs_to_jiffies (HL_PLL_LOW_JOB_FREQ_USEC )); + + 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 ; + +cancel_delayed_work_sync (&hdev->work_freq); + +if (hdev->asic_funcs->late_fini) + hdev->asic_funcs->late_fini (hdev); + + hdev->late_init_done = false; +} + +/** +* hl_device_set_frequency - set the frequency of the device +* +* @hdev: pointer to habanalabs device structure +* @freq: the new frequency value +* +* Change the frequency if needed. +* We allose to set PLL to low only if there is no user process +* Returns 0 if no change was done, otherwise returns 1; +*/ +int hl_device_set_frequency (struct hl_device *hdev,enum hl_pll_frequency freq) +{ +enum hl_pll_frequency old_freq = + (freq ==PLL_HIGH ) ?PLL_LOW :PLL_HIGH ; +int ret; + +if (hdev->pm_mng_profile ==PM_MANUAL ) +return 0 ; + + ret =atomic_cmpxchg (&hdev->curr_pll_profile, old_freq, freq); +if (ret == freq) +return 0 ; + +/* +* in case we want to lower frequency, check if device is not +* opened. We must have a check here to workaround race condition with +* hl_device_open +*/ +if ((freq ==PLL_LOW ) && (atomic_read (&hdev->fd_open_cnt) >0 )) { +atomic_set (&hdev->curr_pll_profile,PLL_HIGH ); +return 0 ; + } + +dev_dbg (hdev->dev,"Changing device frequency to %s\n" , + freq ==PLL_HIGH ?"high" :"low" ); + + hdev->asic_funcs->set_pll_profile (hdev, freq); + +return 1 ; +} +/** * hl_device_suspend - initiate device suspend * @@ -386,6 +498,12 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) goto release_ctx; } + rc =hl_sysfs_init (hdev); +if (rc) { +dev_err (hdev->dev,"failed to initialize sysfs\n" ); + goto free_cb_pool; + } + rc = hdev->asic_funcs->hw_init (hdev);if (rc) {dev_err (hdev->dev,"failed to initialize the H/W\n" );@@ -403,11 +521,33 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass) goto out_disabled; }
Quoted 8 lines
drivers/misc/habanalabs/device.c int hl_device_init(struct hl_device *hdev, struct class *hclass) · line 529
Quoted 6 lines
drivers/misc/habanalabs/device.c +6
-0+/* After test_queues, KMD can start sending messages to device CPU */ + + rc =device_late_init (hdev); +if (rc) { +dev_err (hdev->dev,"Failed late initialization\n" ); + rc =0 ;Isn't this an error?
nope, same explanation as previous patches
Quoted 15 lines
drivers/misc/habanalabs/device.c int hl_device_init(struct hl_device *hdev, struct class *hclass) · line 540
Quoted 12 lines
drivers/misc/habanalabs/device.c +11
-0+ goto out_disabled; + } + +dev_info (hdev->dev,"Found %s device with %lluGB DRAM\n" , + hdev->asic_name, + hdev->asic_prop.dram_size /1024 /1024 /1024 ); + + rc =hl_hwmon_init (hdev); +if (rc) { +dev_err (hdev->dev,"Failed to initialize hwmon\n" ); + rc =0 ;Ditto
ditto for the answer :)
Quoted 188 lines
Quoted 188 lines
drivers/misc/habanalabs/device.c +11
-0+ goto out_disabled; + } +dev_notice (hdev->dev,"Successfully added device to habanalabs driver\n" );return 0 ; +free_cb_pool: +hl_cb_pool_fini (hdev); release_ctx:if (hl_ctx_put (hdev->kernel_ctx) !=1 ) dev_err(hdev->dev,@@ -457,6 +597,12 @@ void hl_device_fini(struct hl_device *hdev)/* Mark device as disabled */ hdev->disabled = true; +hl_hwmon_fini (hdev); + +device_late_fini (hdev); + +hl_sysfs_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 drivers/misc/habanalabs/goya/Makefile +1
-1@@ -1,3 +1,3 @@ subdir-ccflags-y += -I$(src) -HL_GOYA_FILES := goya/goya.o goya/goya_security.o +HL_GOYA_FILES := goya/goya.o goya/goya_security.o goya/goya_hwmgr.odrivers/misc/habanalabs/goya/goya.c +112
-0@@ -127,6 +127,8 @@ static const char *goya_axi_name[GOYA_MAX_INITIATORS] = {#define GOYA_ASYC_EVENT_GROUP_NON_FATAL_SIZE 121 +static int goya_armcp_info_get (struct hl_device *hdev); +static void goya_get_fixed_properties (struct hl_device *hdev) {struct asic_fixed_properties *prop = &hdev->asic_prop;@@ -174,6 +176,7 @@ static void goya_get_fixed_properties(struct hl_device *hdev) prop->num_of_events =GOYA_ASYNC_EVENT_ID_SIZE ; prop->cb_pool_cb_cnt =GOYA_CB_POOL_CB_CNT ; prop->cb_pool_cb_size =GOYA_CB_POOL_CB_SIZE ; + prop->max_power_default =MAX_POWER_DEFAULT ; prop->tpc_enabled_mask =TPC_ENABLED_MASK ; prop->high_pll =PLL_HIGH_DEFAULT ;@@ -558,6 +561,89 @@ int goya_early_fini(struct hl_device *hdev)return 0 ; } +/** +* goya_fetch_psoc_frequency - Fetch PSOC frequency values +* +* @hdev: pointer to hl_device structure +* +*/ +static void goya_fetch_psoc_frequency (struct hl_device *hdev) +{ +struct asic_fixed_properties *prop = &hdev->asic_prop; + + prop->psoc_pci_pll_nr =RREG32 (mmPSOC_PCI_PLL_NR); + prop->psoc_pci_pll_nf =RREG32 (mmPSOC_PCI_PLL_NF); + prop->psoc_pci_pll_od =RREG32 (mmPSOC_PCI_PLL_OD); + prop->psoc_pci_pll_div_factor =RREG32 (mmPSOC_PCI_PLL_DIV_FACTOR_1); +} + +/** +* goya_late_init - GOYA late initialization code +* +* @hdev: pointer to hl_device structure +* +* Get ArmCP info and send message to CPU to enable PCI access +*/ +static int goya_late_init (struct hl_device *hdev) +{ +struct asic_fixed_properties *prop = &hdev->asic_prop; +struct goya_device *goya = hdev->asic_specific; +int rc; + + rc = goya->armcp_info_get (hdev); +if (rc) { +dev_err (hdev->dev,"Failed to get armcp info\n" ); +return rc; + } + +/* Now that we have the DRAM size in ASIC prop, we need to check +* its size and configure the DMA_IF DDR wrap protection (which is in +* the MMU block) accordingly. The value is the log2 of the DRAM size +*/ +WREG32 (mmMMU_LOG2_DDR_SIZE,ilog2 (prop->dram_size)); + + rc =goya_send_pci_access_msg (hdev,ARMCP_PACKET_ENABLE_PCI_ACCESS ); +if (rc) { +dev_err (hdev->dev,"Failed to enable PCI access from CPU\n" ); +return rc; + } + +WREG32 (mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, +GOYA_ASYNC_EVENT_ID_INTS_REGISTER ); + +goya_fetch_psoc_frequency (hdev); + +return 0 ; +} + +/** +* goya_late_fini - GOYA late tear-down code +* +* @hdev: pointer to hl_device structure +* +* Free sensors allocated structures +*/ +void goya_late_fini (struct hl_device *hdev) +{ +const struct hwmon_channel_info **channel_info_arr; +int i =0 ; + +if (!hdev->hl_chip_info.info) +return ; + + channel_info_arr = hdev->hl_chip_info.info; + +while (channel_info_arr[i]) { +kfree (channel_info_arr[i]->config); +kfree (channel_info_arr[i]); + i++; + } + +kfree (channel_info_arr); + + hdev->hl_chip_info.info =NULL ; +} +/** * goya_sw_init - Goya software initialization code * @@ -575,9 +661,15 @@ static int goya_sw_init(struct hl_device *hdev)return -ENOMEM ; goya->test_cpu_queue = goya_test_cpu_queue; + goya->armcp_info_get = goya_armcp_info_get;/* according to goya_init_iatu */ goya->ddr_bar_cur_addr =DRAM_PHYS_BASE ; + + goya->mme_clk =GOYA_PLL_FREQ_LOW ; + goya->tpc_clk =GOYA_PLL_FREQ_LOW ; + goya->ic_clk =GOYA_PLL_FREQ_LOW ; + hdev->asic_specific = goya;/* Create DMA pool for small allocations */ @@ -4272,6 +4364,87 @@ void *goya_get_events_stat(struct hl_device *hdev, u32 *size)return goya->events_stat; } +static int goya_armcp_info_get (struct hl_device *hdev) +{ +struct goya_device *goya = hdev->asic_specific; +struct asic_fixed_properties *prop = &hdev->asic_prop; +struct armcp_packet pkt; +void *armcp_info_cpu_addr; +dma_addr_t armcp_info_dma_addr; +u64 dram_size; +long result; +int rc; + +if (!(goya->hw_cap_initialized &HW_CAP_CPU_Q )) +return 0 ; + + armcp_info_cpu_addr = + hdev->asic_funcs->cpu_accessible_dma_pool_alloc (hdev, +sizeof (struct armcp_info ), &armcp_info_dma_addr); +if (!armcp_info_cpu_addr) { +dev_err (hdev->dev, +"Failed to allocate DMA memory for ArmCP info packet\n" );
Quoted 8 lines
drivers/misc/habanalabs/goya/goya.c void *goya_get_events_stat(struct hl_device *hdev, u32 *size) · line 4390
Quoted 4 lines
drivers/misc/habanalabs/goya/goya.c +4
-0+return -ENOMEM ; + } + +memset (armcp_info_cpu_addr,0 ,sizeof (struct armcp_info ));Do you expect usage of
cpu_accessible_dma_pool_alloc()without the need to clear the memory?
If not memset(0) can be moved inside that function.
yes, if we allocate a pkt from it then we just memcpy over the entire pkt and no need to memset it.
Quoted 1,827 lines
Quoted 1,820 lines
drivers/misc/habanalabs/goya/goya.c +114
-0+ +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_INFO_GET ; + pkt.addr = armcp_info_dma_addr + prop->host_phys_base_address; + pkt.data_max_size =sizeof (struct armcp_info ); + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +GOYA_ARMCP_INFO_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to send armcp info pkt, error %d\n" , rc); + goto out; + } + +memcpy (&prop->armcp_info, armcp_info_cpu_addr, +sizeof (prop->armcp_info)); + + dram_size = prop->armcp_info.dram_size; +if (dram_size) { +if ((!is_power_of_2 (dram_size)) || + (dram_size <DRAM_PHYS_DEFAULT_SIZE )) { +dev_err (hdev->dev, +"F/W reported invalid DRAM size %llu. Trying to use default size\n" , + dram_size); + dram_size =DRAM_PHYS_DEFAULT_SIZE ; + } + + prop->dram_size = dram_size; + prop->dram_end_address = prop->dram_base_address + dram_size; + } + + rc =hl_build_hwmon_channel_info (hdev, prop->armcp_info.sensors); +if (rc) { +dev_err (hdev->dev, +"Failed to build hwmon channel info, error %d\n" , rc); + rc = -EFAULT ; + goto out; + } + +out: + hdev->asic_funcs->cpu_accessible_dma_pool_free (hdev, +sizeof (struct armcp_info ), armcp_info_cpu_addr); + +return rc; +} + +static void goya_init_clock_gating (struct hl_device *hdev) +{ + +} + +static void goya_disable_clock_gating (struct hl_device *hdev) +{ + +}static void goya_hw_queues_lock (struct hl_device *hdev) {@@ -4287,9 +4460,60 @@ static void goya_hw_queues_unlock(struct hl_device *hdev)spin_unlock (&goya->hw_queues_lock); } +int goya_get_eeprom_data (struct hl_device *hdev,void *data,size_t max_size) +{ +struct goya_device *goya = hdev->asic_specific; +struct asic_fixed_properties *prop = &hdev->asic_prop; +struct armcp_packet pkt; +void *eeprom_info_cpu_addr; +dma_addr_t eeprom_info_dma_addr; +long result; +int rc; + +if (!(goya->hw_cap_initialized &HW_CAP_CPU_Q )) +return 0 ; + + eeprom_info_cpu_addr = + hdev->asic_funcs->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 EEPROM info packet\n" ); +return -ENOMEM ; + } + +memset (eeprom_info_cpu_addr,0 , max_size); + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_EEPROM_DATA_GET ; + pkt.addr = eeprom_info_dma_addr + prop->host_phys_base_address; + pkt.data_max_size = max_size; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +GOYA_ARMCP_EEPROM_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to send armcp EEPROM pkt, error %d\n" , rc); + goto out; + } + +/* result contains the actual 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); + +return rc; +} +static const struct hl_asic_funcs goya_funcs = { .early_init = goya_early_init, .early_fini = goya_early_fini, + .late_init = goya_late_init, + .late_fini = goya_late_fini, .sw_init = goya_sw_init, .sw_fini = goya_sw_fini, .hw_init = goya_hw_init,@@ -4310,10 +4534,16 @@ static const struct hl_asic_funcs goya_funcs = { .cpu_accessible_dma_pool_alloc = goya_cpu_accessible_dma_pool_alloc, .cpu_accessible_dma_pool_free = goya_cpu_accessible_dma_pool_free, .update_eq_ci = goya_update_eq_ci, + .add_device_attr = goya_add_device_attr, + .remove_device_attr = goya_remove_device_attr, .handle_eqe = goya_handle_eqe, + .set_pll_profile = goya_set_pll_profile, .get_events_stat = goya_get_events_stat, + .enable_clock_gating = goya_init_clock_gating, + .disable_clock_gating = goya_disable_clock_gating, .hw_queues_lock = goya_hw_queues_lock, .hw_queues_unlock = goya_hw_queues_unlock, + .get_eeprom_data = goya_get_eeprom_data, .send_cpu_message = goya_send_cpu_message };drivers/misc/habanalabs/goya/goyaP.h +21
-0@@ -48,7 +48,10 @@#define PLL_HIGH_DEFAULT 1575000000/* 1.575 GHz */ +#define MAX_POWER_DEFAULT 200000/* 200W */ +#define GOYA_ARMCP_INFO_TIMEOUT 10000000/* 10s */ +#define GOYA_ARMCP_EEPROM_TIMEOUT 10000000/* 10s */ #define DRAM_PHYS_DEFAULT_SIZE 0x100000000ull/* 4GB */ @@ -119,9 +122,15 @@ enum goya_fw_component {struct goya_device {int (*test_cpu_queue)(struct hl_device *hdev); +int (*armcp_info_get)(struct hl_device *hdev);/* TODO: remove hw_queues_lock after moving to scheduler code */ spinlock_t hw_queues_lock; + +u64 mme_clk; +u64 tpc_clk; +u64 ic_clk; +u64 ddr_bar_cur_addr;u32 events_stat[GOYA_ASYNC_EVENT_ID_SIZE ];u32 hw_cap_initialized;@@ -130,6 +139,18 @@ struct goya_device {int goya_test_cpu_queue (struct hl_device *hdev);int goya_send_cpu_message (struct hl_device *hdev,u32 *msg,u16 len,u32 timeout,long *result); +long goya_get_temperature (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_voltage (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_current (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_fan_speed (struct hl_device *hdev,int sensor_index,u32 attr); +long goya_get_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr); +void goya_set_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr, +long value); +void goya_set_pll_profile (struct hl_device *hdev,enum hl_pll_frequency freq); +int goya_add_device_attr (struct hl_device *hdev); +void goya_remove_device_attr (struct hl_device *hdev);void goya_init_security (struct hl_device *hdev); +u64 goya_get_max_power (struct hl_device *hdev); +void goya_set_max_power (struct hl_device *hdev,u64 value);#endif /* GOYAP_H_ */ drivers/misc/habanalabs/goya/goya_hwmgr.c new file +306
-0@@ -0,0 +1,306 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include "goyaP.h" + +void goya_set_pll_profile (struct hl_device *hdev,enum hl_pll_frequency freq) +{ +struct goya_device *goya = hdev->asic_specific; + +switch (freq) { +case PLL_HIGH : +hl_set_frequency (hdev,MME_PLL , hdev->high_pll); +hl_set_frequency (hdev,TPC_PLL , hdev->high_pll); +hl_set_frequency (hdev,IC_PLL , hdev->high_pll); +break ; +case PLL_LOW : +hl_set_frequency (hdev,MME_PLL ,GOYA_PLL_FREQ_LOW ); +hl_set_frequency (hdev,TPC_PLL ,GOYA_PLL_FREQ_LOW ); +hl_set_frequency (hdev,IC_PLL ,GOYA_PLL_FREQ_LOW ); +break ; +case PLL_LAST : +hl_set_frequency (hdev,MME_PLL , goya->mme_clk); +hl_set_frequency (hdev,TPC_PLL , goya->tpc_clk); +hl_set_frequency (hdev,IC_PLL , goya->ic_clk); +break ; +default : +dev_err (hdev->dev,"unknown frequency setting\n" ); + } +} + +static ssize_t mme_clk_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,MME_PLL , false); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t mme_clk_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +struct goya_device *goya = hdev->asic_specific; +int rc; +long value; + +if (hdev->disabled) { + count = -ENODEV ; + goto fail; + } + +if (hdev->pm_mng_profile ==PM_AUTO ) { + count = -EPERM ; + goto fail; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto fail; + } + +hl_set_frequency (hdev,MME_PLL , value); + goya->mme_clk = value; + +fail: +return count; +} + +static ssize_t tpc_clk_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,TPC_PLL , false); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t tpc_clk_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +struct goya_device *goya = hdev->asic_specific; +int rc; +long value; + +if (hdev->disabled) { + count = -ENODEV ; + goto fail; + } + +if (hdev->pm_mng_profile ==PM_AUTO ) { + count = -EPERM ; + goto fail; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto fail; + } + +hl_set_frequency (hdev,TPC_PLL , value); + goya->tpc_clk = value; + +fail: +return count; +} + +static ssize_t ic_clk_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,IC_PLL , false); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t ic_clk_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +struct goya_device *goya = hdev->asic_specific; +int rc; +long value; + +if (hdev->disabled) { + count = -ENODEV ; + goto fail; + } + +if (hdev->pm_mng_profile ==PM_AUTO ) { + count = -EPERM ; + goto fail; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto fail; + } + +hl_set_frequency (hdev,IC_PLL , value); + goya->ic_clk = value; + +fail: +return count; +} + +static ssize_t mme_clk_curr_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,MME_PLL , true); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t tpc_clk_curr_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,TPC_PLL , true); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static ssize_t ic_clk_curr_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; + +if (hdev->disabled) +return -ENODEV ; + + value =hl_get_frequency (hdev,IC_PLL , true); + +if (value <0 ) +return value; + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , value); +} + +static DEVICE_ATTR_RW (mme_clk); +static DEVICE_ATTR_RW (tpc_clk); +static DEVICE_ATTR_RW (ic_clk); +static DEVICE_ATTR_RO (mme_clk_curr); +static DEVICE_ATTR_RO (tpc_clk_curr); +static DEVICE_ATTR_RO (ic_clk_curr); + +int goya_add_device_attr (struct hl_device *hdev) +{ +int rc; + + rc =device_create_file (hdev->dev, &dev_attr_mme_clk); +if (rc) { +dev_err (hdev->dev,"failed to create device file mme_clk\n" ); +return rc; + } + + rc =device_create_file (hdev->dev, &dev_attr_tpc_clk); +if (rc) { +dev_err (hdev->dev,"failed to create device file tpc_clk\n" ); + goto remove_mme_clk; + } + + rc =device_create_file (hdev->dev, &dev_attr_ic_clk); +if (rc) { +dev_err (hdev->dev,"failed to create device file ic_clk\n" ); + goto remove_tpc_clk; + } + + rc =device_create_file (hdev->dev, &dev_attr_mme_clk_curr); +if (rc) { +dev_err (hdev->dev, +"failed to create device file mme_clk_curr\n" ); + goto remove_ic_clk; + } + + rc =device_create_file (hdev->dev, &dev_attr_tpc_clk_curr); +if (rc) { +dev_err (hdev->dev, +"failed to create device file tpc_clk_curr\n" ); + goto remove_mme_clk_curr; + } + + rc =device_create_file (hdev->dev, &dev_attr_ic_clk_curr); +if (rc) { +dev_err (hdev->dev, +"failed to create device file ic_clk_curr\n" ); + goto remove_tpc_clk_curr; + } + +return 0 ; + +remove_tpc_clk_curr: +device_remove_file (hdev->dev, &dev_attr_tpc_clk_curr); +remove_mme_clk_curr: +device_remove_file (hdev->dev, &dev_attr_mme_clk_curr); +remove_ic_clk: +device_remove_file (hdev->dev, &dev_attr_ic_clk); +remove_tpc_clk: +device_remove_file (hdev->dev, &dev_attr_tpc_clk); +remove_mme_clk: +device_remove_file (hdev->dev, &dev_attr_mme_clk); +return rc; +} + +void goya_remove_device_attr (struct hl_device *hdev) +{ +device_remove_file (hdev->dev, &dev_attr_ic_clk_curr); +device_remove_file (hdev->dev, &dev_attr_tpc_clk_curr); +device_remove_file (hdev->dev, &dev_attr_mme_clk_curr); +device_remove_file (hdev->dev, &dev_attr_ic_clk); +device_remove_file (hdev->dev, &dev_attr_tpc_clk); +device_remove_file (hdev->dev, &dev_attr_mme_clk); +}drivers/misc/habanalabs/habanalabs.h +97
-0@@ -25,6 +25,8 @@#define HL_DEVICE_TIMEOUT_USEC 1000000/* 1 s */ +#define HL_PLL_LOW_JOB_FREQ_USEC 5000000/* 5 s */ +#define HL_MAX_QUEUES 128struct hl_device ;@@ -60,6 +62,8 @@ struct hw_queue_properties {/** * struct asic_fixed_properties - ASIC specific immutable properties. * @hw_queues_props: H/W queues properties. +* @armcp_info: received various information from ArmCP regarding the H/W. e.g. +* available sensors. * @uboot_ver: F/W U-boot version. * @preboot_ver: F/W Preboot version. * @sram_base_address: SRAM physical start address. @@ -72,6 +76,7 @@ struct hw_queue_properties {* @dram_pci_bar_size: size of PCI bar towards DRAM. * @host_phys_base_address: base physical address of host memory for * transactions that the device generates. +* @max_power_default: max power of the device after reset * @va_space_host_start_address: base address of virtual memory range for * mapping host memory. * @va_space_host_end_address: end address of virtual memory range for @@ -84,6 +89,10 @@ struct hw_queue_properties {* @sram_size: total size of SRAM. * @max_asid: maximum number of open contexts (ASIDs). * @num_of_events: number of possible internal H/W IRQs. +* @psoc_pci_pll_nr: PCI PLL NR value. +* @psoc_pci_pll_nf: PCI PLL NF value. +* @psoc_pci_pll_od: PCI PLL OD value. +* @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value. * @completion_queues_count: number of completion queues. * @high_pll: high PLL frequency used by the device. * @cb_pool_cb_cnt: number of CBs in the CB pool. @@ -92,6 +101,7 @@ struct hw_queue_properties {*/ struct asic_fixed_properties {struct hw_queue_properties hw_queues_props[HL_MAX_QUEUES ]; +struct armcp_info armcp_info;char uboot_ver[VERSION_MAX_LEN ];char preboot_ver[VERSION_MAX_LEN ];u64 sram_base_address;@@ -103,6 +113,7 @@ struct asic_fixed_properties {u64 dram_size;u64 dram_pci_bar_size;u64 host_phys_base_address; +u64 max_power_default;u64 va_space_host_start_address;u64 va_space_host_end_address;u64 va_space_dram_start_address;@@ -111,6 +122,10 @@ struct asic_fixed_properties {u32 sram_size;u32 max_asid;u32 num_of_events; +u32 psoc_pci_pll_nr; +u32 psoc_pci_pll_nf; +u32 psoc_pci_pll_od; +u32 psoc_pci_pll_div_factor;u32 high_pll;u32 cb_pool_cb_cnt;u32 cb_pool_cb_size;@@ -296,13 +311,37 @@ enum hl_asic_type { }; +/** +* enum hl_pm_mng_profile - power management profile. +* @PM_AUTO: internal clock is set by KMD. +* @PM_MANUAL: internal clock is set by the user. +* @PM_LAST: last power management type. +*/ +enum hl_pm_mng_profile { +PM_AUTO =1 , +PM_MANUAL , +PM_LAST +}; +/** +* enum hl_pll_frequency - PLL frequency. +* @PLL_HIGH: high frequency. +* @PLL_LOW: low frequency. +* @PLL_LAST: last frequency values that were configured by the user. +*/ +enum hl_pll_frequency { +PLL_HIGH =1 , +PLL_LOW , +PLL_LAST +};/** * struct hl_asic_funcs - ASIC specific functions that are can be called from * common code. * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W. * @early_fini: tears down what was done in early_init. +* @late_init: sets up late driver/hw state (post hw_init) - Optional. +* @late_fini: tears down what was done in late_init (pre hw_fini) - Optional. * @sw_init: sets up driver state, does not configure H/W. * @sw_fini: tears down driver state, does not configure H/W. * @hw_init: sets up the H/W state. @@ -326,15 +365,23 @@ enum hl_asic_type {* @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool. * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool. * @update_eq_ci: update event queue CI. +* @add_device_attr: add ASIC specific device attributes. +* @remove_device_attr: remove ASIC specific device attributes. * @handle_eqe: handle event queue entry (IRQ) from ArmCP. +* @set_pll_profile: change PLL profile (manual/automatic). * @get_events_stat: retrieve event queue entries histogram. +* @enable_clock_gating: enable clock gating for reducing power consumption. +* @disable_clock_gating: disable clock for accessing registers on HBW. * @hw_queues_lock: acquire H/W queues lock. * @hw_queues_unlock: release H/W queues lock. +* @get_eeprom_data: retrieve EEPROM data from F/W. * @send_cpu_message: send buffer to ArmCP. */ struct hl_asic_funcs {int (*early_init)(struct hl_device *hdev);int (*early_fini)(struct hl_device *hdev); +int (*late_init)(struct hl_device *hdev); +void (*late_fini)(struct hl_device *hdev);int (*sw_init)(struct hl_device *hdev);int (*sw_fini)(struct hl_device *hdev);int (*hw_init)(struct hl_device *hdev);@@ -363,11 +410,19 @@ struct hl_asic_funcs {void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,size_t size,void *vaddr);void (*update_eq_ci)(struct hl_device *hdev,u32 val); +int (*add_device_attr)(struct hl_device *hdev); +void (*remove_device_attr)(struct hl_device *hdev);void (*handle_eqe)(struct hl_device *hdev,struct hl_eq_entry *eq_entry); +void (*set_pll_profile)(struct hl_device *hdev, +enum hl_pll_frequency freq);void * (*get_events_stat)(struct hl_device *hdev,u32 *size); +void (*enable_clock_gating)(struct hl_device *hdev); +void (*disable_clock_gating)(struct hl_device *hdev);void (*hw_queues_lock)(struct hl_device *hdev);void (*hw_queues_unlock)(struct hl_device *hdev); +int (*get_eeprom_data)(struct hl_device *hdev,void *data, +size_t max_size);int (*send_cpu_message)(struct hl_device *hdev,u32 *msg,u16 len,u32 timeout,long *result); };@@ -496,6 +551,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @rmmio: configuration area address on SRAM. * @cdev: related char device. * @dev: realted kernel basic device structure. +* @work_freq: delayed work to lower device frequency if possible. * @asic_name: ASIC specific nmae. * @asic_type: ASIC specific type. * @completion_queue: array of hl_cq. @@ -517,13 +573,23 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @asic_prop: ASIC specific immutable properties. * @asic_funcs: ASIC specific functions. * @asic_specific: ASIC specific information to use only from ASIC files. +* @hwmon_dev: H/W monitor device. +* @pm_mng_profile: current power management profile. +* @hl_chip_info: ASIC's sensors information. * @cb_pool: list of preallocated CBs. * @cb_pool_lock: protects the CB pool. * @user_ctx: current user context executing. +* @curr_pll_profile: current PLL profile. * @fd_open_cnt: number of open context executing. +* @max_power: the max power of the device, as configured by the sysadmin. This +* value is saved so in case of hard-reset, KMD will restore this +* value and update the F/W after the re-initialization * @major: habanalabs KMD major. +* @high_pll: high PLL profile frequency. * @id: device minor. * @disabled: is device disabled. +* @late_init_done: is late init stage was done during initialization. +* @hwmon_initialized: is H/W monitor sensors was initialized. */ struct hl_device {struct pci_dev *pdev;@@ -531,6 +597,7 @@ struct hl_device {void __iomem *rmmio;struct cdev cdev;struct device *dev; +struct delayed_work work_freq;char asic_name[16 ];enum hl_asic_type asic_type;struct hl_cq *completion_queue;@@ -553,16 +620,25 @@ struct hl_device {struct asic_fixed_properties asic_prop;const struct hl_asic_funcs *asic_funcs;void *asic_specific; +struct device *hwmon_dev; +enum hl_pm_mng_profile pm_mng_profile; +struct hwmon_chip_info hl_chip_info;struct list_head cb_pool;spinlock_t cb_pool_lock;/* TODO: The following fields should be moved for multi-context */ struct hl_ctx *user_ctx; + +atomic_t curr_pll_profile;atomic_t fd_open_cnt; +u64 max_power;u32 major; +u32 high_pll;u16 id;u8 disabled; +u8 late_init_done; +u8 hwmon_initialized;/* Parameters for bring-up */ u8 cpu_enable;@@ -647,6 +723,15 @@ int hl_device_suspend(struct hl_device *hdev);int hl_device_resume (struct hl_device *hdev);void hl_hpriv_get (struct hl_fpriv *hpriv);void hl_hpriv_put (struct hl_fpriv *hpriv); +int hl_device_set_frequency (struct hl_device *hdev,enum hl_pll_frequency freq); +int hl_build_hwmon_channel_info (struct hl_device *hdev, +struct armcp_sensor *sensors_arr); + +int hl_sysfs_init (struct hl_device *hdev); +void hl_sysfs_fini (struct hl_device *hdev); + +int hl_hwmon_init (struct hl_device *hdev); +void hl_hwmon_fini (struct hl_device *hdev);int hl_cb_create (struct hl_device *hdev,struct hl_cb_mgr *mgr,u32 cb_size,u64 *handle,int ctx_id);@@ -663,6 +748,18 @@ int hl_cb_pool_fini(struct hl_device *hdev);void goya_set_asic_funcs (struct hl_device *hdev); +long hl_get_frequency (struct hl_device *hdev,u32 pll_index,bool curr); +void hl_set_frequency (struct hl_device *hdev,u32 pll_index,u64 freq); +long hl_get_temperature (struct hl_device *hdev,int sensor_index,u32 attr); +long hl_get_voltage (struct hl_device *hdev,int sensor_index,u32 attr); +long hl_get_current (struct hl_device *hdev,int sensor_index,u32 attr); +long hl_get_fan_speed (struct hl_device *hdev,int sensor_index,u32 attr); +long hl_get_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr); +void hl_set_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr, +long value); +u64 hl_get_max_power (struct hl_device *hdev); +void hl_set_max_power (struct hl_device *hdev,u64 value); +/* IOCTLs */ long hl_ioctl (struct file *filep,unsigned int cmd,unsigned long arg);int hl_cb_ioctl (struct hl_fpriv *hpriv,void *data);drivers/misc/habanalabs/habanalabs_drv.c +7
-0@@ -134,6 +134,13 @@ int hl_device_open(struct inode *inode, struct file *filp) hpriv->taskpid =find_get_pid (current->pid); +/* +* Device is IDLE at this point so it is legal to change PLLs. There +* is no need to check anything because if the PLL is already HIGH, the +* set function will return without doing anything +*/ +hl_device_set_frequency (hdev,PLL_HIGH ); +return 0 ; out_err:drivers/misc/habanalabs/hwmon.c new file +449
-0@@ -0,0 +1,449 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include "habanalabs.h" + +#define SENSORS_PKT_TIMEOUT 100000/* 100ms */ +#define HWMON_NR_SENSOR_TYPES (hwmon_pwm + 1) + +int hl_build_hwmon_channel_info (struct hl_device *hdev, +struct armcp_sensor *sensors_arr) +{ +u32 counts[HWMON_NR_SENSOR_TYPES ] = {0 }; +u32 *sensors_by_type[HWMON_NR_SENSOR_TYPES ] = {0 }; +u32 sensors_by_type_next_index[HWMON_NR_SENSOR_TYPES ] = {0 }; +struct hwmon_channel_info **channels_info; +u32 num_sensors_for_type, num_active_sensor_types =0 , + arr_size =0 , *curr_arr; +enum hwmon_sensor_types type; +int rc, i, j; + +for (i =0 ; i <ARMCP_MAX_SENSORS ; i++) { + type = sensors_arr[i].type; + +if ((type ==0 ) && (sensors_arr[i].flags ==0 )) +break ; + +if (type >=HWMON_NR_SENSOR_TYPES ) { +dev_err (hdev->dev, +"Got wrong sensor type %d from device\n" , type); +return -EINVAL ; + } + + counts[type]++; + arr_size++; + } + +for (i =0 ; i <HWMON_NR_SENSOR_TYPES ; i++) { +if (counts[i] ==0 ) +continue ; + + num_sensors_for_type = counts[i] +1 ; + curr_arr =kcalloc (num_sensors_for_type,sizeof (*curr_arr), +GFP_KERNEL ); +if (!curr_arr) { + rc = -ENOMEM ; + goto sensors_type_err; + } + + num_active_sensor_types++; + sensors_by_type[i] = curr_arr; + } + +for (i =0 ; i < arr_size ; i++) { + type = sensors_arr[i].type; + curr_arr = sensors_by_type[type]; + curr_arr[sensors_by_type_next_index[type]++] = + sensors_arr[i].flags; + } + + channels_info =kcalloc (num_active_sensor_types +1 , +sizeof (*channels_info),GFP_KERNEL ); +if (!channels_info) { + rc = -ENOMEM ; + goto channels_info_array_err; + } + +for (i =0 ; i < num_active_sensor_types ; i++) { + channels_info[i] =kzalloc (sizeof (*channels_info[i]), +GFP_KERNEL ); +if (!channels_info[i]) { + rc = -ENOMEM ; + goto channel_info_err; + } + } + +for (i =0 , j =0 ; i <HWMON_NR_SENSOR_TYPES ; i++) { +if (!sensors_by_type[i]) +continue ; + + channels_info[j]->type = i; + channels_info[j]->config = sensors_by_type[i]; + j++; + } + + hdev->hl_chip_info.info = + (const struct hwmon_channel_info **)channels_info; + +return 0 ; + +channel_info_err: +for (i =0 ; i < num_active_sensor_types ; i++) +if (channels_info[i]) { +kfree (channels_info[i]->config); +kfree (channels_info[i]); + } +kfree (channels_info); +channels_info_array_err: +sensors_type_err: +for (i =0 ; i <HWMON_NR_SENSOR_TYPES ; i++) +kfree (sensors_by_type[i]); + +return rc; +} + +static int hl_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) +return -ENODEV ; + +switch (type) { +case hwmon_temp: +switch (attr) { +case hwmon_temp_input: +case hwmon_temp_max: +case hwmon_temp_crit: +case hwmon_temp_max_hyst: +case hwmon_temp_crit_hyst: +break ; +default : +return -EINVAL ; + } + + *val =hl_get_temperature (hdev, channel, attr); +break ; +case hwmon_in: +switch (attr) { +case hwmon_in_input: +case hwmon_in_min: +case hwmon_in_max: +break ; +default : +return -EINVAL ; + } + + *val =hl_get_voltage (hdev, channel, attr); +break ; +case hwmon_curr: +switch (attr) { +case hwmon_curr_input: +case hwmon_curr_min: +case hwmon_curr_max: +break ; +default : +return -EINVAL ; + } + + *val =hl_get_current (hdev, channel, attr); +break ; +case hwmon_fan: +switch (attr) { +case hwmon_fan_input: +case hwmon_fan_min: +case hwmon_fan_max: +break ; +default : +return -EINVAL ; + } + *val =hl_get_fan_speed (hdev, channel, attr); +break ; +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_input: +case hwmon_pwm_enable: +break ; +default : +return -EINVAL ; + } + *val =hl_get_pwm_info (hdev, channel, attr); +break ; +default : +return -EINVAL ; + } +return 0 ; +} + +static int hl_write (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long val) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) +return -ENODEV ; + +switch (type) { +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_input: +case hwmon_pwm_enable: +break ; +default : +return -EINVAL ; + } +hl_set_pwm_info (hdev, channel, attr, val); +break ; +default : +return -EINVAL ; + } +return 0 ; +} + +static umode_t hl_is_visible (const void *data,enum hwmon_sensor_types type, +u32 attr,int channel) +{ +switch (type) { +case hwmon_temp: +switch (attr) { +case hwmon_temp_input: +case hwmon_temp_max: +case hwmon_temp_max_hyst: +case hwmon_temp_crit: +case hwmon_temp_crit_hyst: +return 0444 ; + } +break ; +case hwmon_in: +switch (attr) { +case hwmon_in_input: +case hwmon_in_min: +case hwmon_in_max: +return 0444 ; + } +break ; +case hwmon_curr: +switch (attr) { +case hwmon_curr_input: +case hwmon_curr_min: +case hwmon_curr_max: +return 0444 ; + } +break ; +case hwmon_fan: +switch (attr) { +case hwmon_fan_input: +case hwmon_fan_min: +case hwmon_fan_max: +return 0444 ; + } +break ; +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_input: +case hwmon_pwm_enable: +return 0644 ; + } +break ; +default : +break ; + } +return 0 ; +} + +static const struct hwmon_ops hl_hwmon_ops = { + .is_visible = hl_is_visible, + .read = hl_read, + .write = hl_write +}; + +long hl_get_temperature (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_TEMPERATURE_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get temperature from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +long hl_get_voltage (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_VOLTAGE_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get voltage from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +long hl_get_current (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_CURRENT_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get current from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +long hl_get_fan_speed (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_FAN_SPEED_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get fan speed from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +long hl_get_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_PWM_GET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get pwm info from sensor %d, error %d\n" , + sensor_index, rc); + result =0 ; + } + +return result; +} + +void hl_set_pwm_info (struct hl_device *hdev,int sensor_index,u32 attr, +long value) +{ +struct armcp_packet pkt; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_PWM_SET ; + pkt.sensor_index = sensor_index; + pkt.type = attr; + pkt.value = value; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SENSORS_PKT_TIMEOUT ,NULL ); + +if (rc) +dev_err (hdev->dev, +"Failed to set pwm info to sensor %d, error %d\n" , + sensor_index, rc); +} + +int hl_hwmon_init (struct hl_device *hdev) +{ +struct device *dev = hdev->pdev ? &hdev->pdev->dev : hdev->dev; +int rc; + +if ((hdev->hwmon_initialized) || !(hdev->fw_loading)) +return 0 ; + +if (hdev->hl_chip_info.info) { + hdev->hl_chip_info.ops = &hl_hwmon_ops; + + hdev->hwmon_dev =hwmon_device_register_with_info (dev, +"habanalabs" , hdev, &hdev->hl_chip_info,NULL ); +if (IS_ERR (hdev->hwmon_dev)) { + rc =PTR_ERR (hdev->hwmon_dev); +dev_err (hdev->dev, +"Unable to register hwmon device: %d\n" , rc); +return rc; + } + +dev_info (hdev->dev,"%s: add sensors information\n" , +dev_name (hdev->hwmon_dev)); + + hdev->hwmon_initialized = true; + }else { +dev_info (hdev->dev,"no available sensors\n" ); + } + +return 0 ; +} + +void hl_hwmon_fini (struct hl_device *hdev) +{ +if (!hdev->hwmon_initialized) +return ; + +hwmon_device_unregister (hdev->hwmon_dev); +}drivers/misc/habanalabs/sysfs.c new file +588
-1@@ -0,0 +1,588 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include "habanalabs.h" +#include "include/habanalabs_device_if.h" + +#include <linux/hwmon-sysfs.h> +#include <linux/hwmon.h> + +#define SET_CLK_PKT_TIMEOUT 200000/* 200ms */ +#define SET_PWR_PKT_TIMEOUT 400000/* 400ms */ + +long hl_get_frequency (struct hl_device *hdev,u32 pll_index,bool curr) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + +if (curr) + pkt.opcode =ARMCP_PACKET_FREQUENCY_CURR_GET ; +else + pkt.opcode =ARMCP_PACKET_FREQUENCY_GET ; + pkt.pll_index = pll_index; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SET_CLK_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev, +"Failed to get frequency of PLL %d, error %d\n" , + pll_index, rc); + result = rc; + } + +return result; +} + +void hl_set_frequency (struct hl_device *hdev,u32 pll_index,u64 freq) +{ +struct armcp_packet pkt; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_FREQUENCY_SET ; + pkt.pll_index = pll_index; + pkt.value = freq; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SET_CLK_PKT_TIMEOUT ,NULL ); + +if (rc) +dev_err (hdev->dev, +"Failed to set frequency to PLL %d, error %d\n" , + pll_index, rc); +} + +u64 hl_get_max_power (struct hl_device *hdev) +{ +struct armcp_packet pkt; +long result; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_MAX_POWER_GET ; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SET_PWR_PKT_TIMEOUT , &result); + +if (rc) { +dev_err (hdev->dev,"Failed to get max power, error %d\n" , rc); + result = rc; + } + +return result; +} + +void hl_set_max_power (struct hl_device *hdev,u64 value) +{ +struct armcp_packet pkt; +int rc; + +memset (&pkt,0 ,sizeof (pkt)); + + pkt.opcode =ARMCP_PACKET_MAX_POWER_SET ; + pkt.value = value; + + rc = hdev->asic_funcs->send_cpu_message (hdev, (u32 *) &pkt,sizeof (pkt), +SET_PWR_PKT_TIMEOUT ,NULL ); + +if (rc) +dev_err (hdev->dev,"Failed to set max power, error %d\n" , rc); +} + +static ssize_t pm_mng_profile_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) +return -ENODEV ; + +return snprintf (buf,PAGE_SIZE ,"%s\n" , + (hdev->pm_mng_profile ==PM_AUTO ) ?"auto" : + (hdev->pm_mng_profile ==PM_MANUAL ) ?"manual" : +"unknown" ); +} + +static ssize_t pm_mng_profile_store (struct device *dev, +struct device_attribute *attr,const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) { + count = -ENODEV ; + goto out; + } + +mutex_lock (&hdev->device_open); + +if (atomic_read (&hdev->fd_open_cnt) >0 ) { +dev_err (hdev->dev, +"Can't change PM profile while user process is opened on the device\n" ); + count = -EPERM ; + goto unlock_mutex; + } + +if (strncmp ("auto" , buf,strlen ("auto" )) ==0 ) { +/* Make sure we are in LOW PLL when changing modes */ +if (hdev->pm_mng_profile ==PM_MANUAL ) { +atomic_set (&hdev->curr_pll_profile,PLL_HIGH ); +hl_device_set_frequency (hdev,PLL_LOW ); + hdev->pm_mng_profile =PM_AUTO ; + } + }else if (strncmp ("manual" , buf,strlen ("manual" )) ==0 ) { +/* Make sure we are in LOW PLL when changing modes */ +if (hdev->pm_mng_profile ==PM_AUTO ) { +flush_delayed_work (&hdev->work_freq); + hdev->pm_mng_profile =PM_MANUAL ; + } + }else { +dev_err (hdev->dev,"value should be auto or manual\n" ); + count = -EINVAL ; + goto unlock_mutex; + } + +unlock_mutex: +mutex_unlock (&hdev->device_open); +out: +return count; +} + +static ssize_t high_pll_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +if (hdev->disabled) +return -ENODEV ; + +return snprintf (buf,PAGE_SIZE ,"%u\n" , hdev->high_pll); +} + +static ssize_t high_pll_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long value; +int rc; + +if (hdev->disabled) { + count = -ENODEV ; + goto out; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto out; + } + + hdev->high_pll = value; + +out: +return count; +} + +static ssize_t uboot_ver_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s\n" , hdev->asic_prop.uboot_ver); +} + +static ssize_t armcp_kernel_ver_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s" , + hdev->asic_prop.armcp_info.kernel_version); +} + +static ssize_t armcp_ver_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s\n" , + hdev->asic_prop.armcp_info.armcp_version); +} + +static ssize_t cpld_ver_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"0x%08x\n" , + hdev->asic_prop.armcp_info.cpld_version); +} + +static ssize_t infineon_ver_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"0x%04x\n" , + hdev->asic_prop.armcp_info.infineon_version); +} + +static ssize_t fuse_ver_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s\n" , + hdev->asic_prop.armcp_info.fuse_version); +} + +static ssize_t thermal_ver_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s" , + hdev->asic_prop.armcp_info.thermal_version); +} + +static ssize_t preboot_btl_ver_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%s\n" , hdev->asic_prop.preboot_ver); +} + +static ssize_t device_type_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +char *str; + +switch (hdev->asic_type) { +case ASIC_GOYA : + str ="GOYA" ; +break ; +default : +dev_err (hdev->dev,"Unrecognized ASIC type %d\n" , + hdev->asic_type); +return -EINVAL ; + } + +return snprintf (buf,PAGE_SIZE ,"%s\n" , str); +} + +static ssize_t pci_addr_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +/* Use dummy, fixed address for simulator */ +if (!hdev->pdev) +return snprintf (buf,PAGE_SIZE ,"0000:%02d:00.0\n" , hdev->id); + +return snprintf (buf,PAGE_SIZE ,"%04x:%02x:%02x.%x\n" , +pci_domain_nr (hdev->pdev->bus), + hdev->pdev->bus->number, +PCI_SLOT (hdev->pdev->devfn), +PCI_FUNC (hdev->pdev->devfn)); +} + +static ssize_t status_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +char *str; + +if (hdev->disabled) + str ="Malfunction" ; +else + str ="Operational" ; + +return snprintf (buf,PAGE_SIZE ,"%s\n" , str); +} + +static ssize_t write_open_cnt_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); + +return snprintf (buf,PAGE_SIZE ,"%d\n" , hdev->user_ctx ?1 :0 ); +} + +static ssize_t max_power_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +long val; + +if (hdev->disabled) +return -ENODEV ; + + val =hl_get_max_power (hdev); + +return snprintf (buf,PAGE_SIZE ,"%lu\n" , val); +} + +static ssize_t max_power_store (struct device *dev, +struct device_attribute *attr,const char *buf,size_t count) +{ +struct hl_device *hdev =dev_get_drvdata (dev); +unsigned long value; +int rc; + +if (hdev->disabled) { + count = -ENODEV ; + goto out; + } + + rc =kstrtoul (buf,0 , &value); + +if (rc) { + count = -EINVAL ; + goto out; + } + + hdev->max_power = value; +hl_set_max_power (hdev, value); + +out: +return count; +} + +static ssize_t eeprom_read_handler (struct file *filp,struct kobject *kobj, +struct bin_attribute *attr,char *buf,loff_t offset, +size_t max_size) +{ + struct device *dev = container_of(kobj, struct device, kobj); +struct hl_device *hdev =dev_get_drvdata (dev); +char *data; +int rc; + +if (!max_size) +return -EINVAL ; + + data =kzalloc (max_size,GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + rc = hdev->asic_funcs->get_eeprom_data (hdev, data, max_size); +if (rc) + goto out; + +memcpy (buf, data, max_size); + +out: +kfree (data); + +return max_size; +} + +static DEVICE_ATTR_RW (pm_mng_profile); +static DEVICE_ATTR_RW (high_pll); +static DEVICE_ATTR_RO (uboot_ver); +static DEVICE_ATTR_RO (armcp_kernel_ver); +static DEVICE_ATTR_RO (armcp_ver); +static DEVICE_ATTR_RO (cpld_ver); +static DEVICE_ATTR_RO (infineon_ver); +static DEVICE_ATTR_RO (fuse_ver); +static DEVICE_ATTR_RO (thermal_ver); +static DEVICE_ATTR_RO (preboot_btl_ver); +static DEVICE_ATTR_RO (device_type); +static DEVICE_ATTR_RO (pci_addr); +static DEVICE_ATTR_RO (status); +static DEVICE_ATTR_RO (write_open_cnt); +static DEVICE_ATTR_RW (max_power); + +static const struct bin_attribute bin_attr_eeprom = { + .attr = {.name ="eeprom" , .mode = (0444 )}, + .size =PAGE_SIZE , + .read = eeprom_read_handler +}; + +int hl_sysfs_init (struct hl_device *hdev) +{ +int rc; + + rc = hdev->asic_funcs->add_device_attr (hdev); +if (rc) { +dev_err (hdev->dev,"failed to add device attributes\n" ); +return rc; + } + + rc =device_create_file (hdev->dev, &dev_attr_pm_mng_profile); +if (rc) { +dev_err (hdev->dev, +"failed to create device file pm_mng_profile\n" ); + goto remove_device_attr; + } + + hdev->pm_mng_profile =PM_AUTO ; + + rc =device_create_file (hdev->dev, &dev_attr_high_pll); +if (rc) { +dev_err (hdev->dev, +"failed to create device file pll_profile\n" ); + goto remove_pm_mng_profile; + } + + rc =device_create_file (hdev->dev, &dev_attr_uboot_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file uboot_ver\n" ); + goto remove_pll_profile; + } + + rc =device_create_file (hdev->dev, &dev_attr_armcp_kernel_ver); +if (rc) { +dev_err (hdev->dev, +"failed to create device file armcp_kernel_ver\n" ); + goto remove_uboot_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_armcp_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file armcp_ver\n" ); + goto remove_armcp_kernel_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_cpld_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file cpld_ver\n" ); + goto remove_armcp_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_infineon_ver); +if (rc) { +dev_err (hdev->dev, +"failed to create device file infineon_ver\n" ); + goto remove_cpld_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_fuse_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file fuse_ver\n" ); + goto remove_infineon_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_thermal_ver); +if (rc) { +dev_err (hdev->dev,"failed to create device file thermal_ver\n" ); + goto remove_fuse_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_preboot_btl_ver); +if (rc) { +dev_err (hdev->dev, +"failed to create device file preboot_btl_ver\n" ); + goto remove_thermal_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_device_type); +if (rc) { +dev_err (hdev->dev, +"failed to create device file device_type\n" ); + goto remove_preboot_ver; + } + + rc =device_create_file (hdev->dev, &dev_attr_pci_addr); +if (rc) { +dev_err (hdev->dev,"failed to create device file pci_addr\n" ); + goto remove_device_type; + } + + rc =device_create_file (hdev->dev, &dev_attr_status); +if (rc) { +dev_err (hdev->dev,"failed to create device file status\n" ); + goto remove_pci_addr; + } + + rc =device_create_file (hdev->dev, &dev_attr_write_open_cnt); +if (rc) { +dev_err (hdev->dev, +"failed to create device file write_open_count\n" ); + goto remove_status; + } + + hdev->max_power = hdev->asic_prop.max_power_default; + + rc =device_create_file (hdev->dev, &dev_attr_max_power); +if (rc) { +dev_err (hdev->dev, +"failed to create device file max_power\n" ); + goto remove_write_open_cnt; + } + + rc =sysfs_create_bin_file (&hdev->dev->kobj, &bin_attr_eeprom); +if (rc) { +dev_err (hdev->dev,"failed to create EEPROM sysfs entry\n" ); + goto remove_attr_max_power; + } + +return 0 ; + +remove_attr_max_power: +device_remove_file (hdev->dev, &dev_attr_max_power); +remove_write_open_cnt: +device_remove_file (hdev->dev, &dev_attr_write_open_cnt); +remove_status: +device_remove_file (hdev->dev, &dev_attr_status); +remove_pci_addr: +device_remove_file (hdev->dev, &dev_attr_pci_addr); +remove_device_type: +device_remove_file (hdev->dev, &dev_attr_device_type); +remove_preboot_ver: +device_remove_file (hdev->dev, &dev_attr_preboot_btl_ver); +remove_thermal_ver: +device_remove_file (hdev->dev, &dev_attr_thermal_ver); +remove_fuse_ver: +device_remove_file (hdev->dev, &dev_attr_fuse_ver); +remove_infineon_ver: +device_remove_file (hdev->dev, &dev_attr_infineon_ver); +remove_cpld_ver: +device_remove_file (hdev->dev, &dev_attr_cpld_ver); +remove_armcp_ver: +device_remove_file (hdev->dev, &dev_attr_armcp_ver); +remove_armcp_kernel_ver: +device_remove_file (hdev->dev, &dev_attr_armcp_kernel_ver); +remove_uboot_ver: +device_remove_file (hdev->dev, &dev_attr_uboot_ver); +remove_pll_profile: +device_remove_file (hdev->dev, &dev_attr_high_pll); +remove_pm_mng_profile: +device_remove_file (hdev->dev, &dev_attr_pm_mng_profile); +remove_device_attr: + hdev->asic_funcs->remove_device_attr (hdev); + +return rc; +} + +void hl_sysfs_fini (struct hl_device *hdev) +{ +sysfs_remove_bin_file (&hdev->dev->kobj, &bin_attr_eeprom); +device_remove_file (hdev->dev, &dev_attr_max_power); +device_remove_file (hdev->dev, &dev_attr_write_open_cnt); +device_remove_file (hdev->dev, &dev_attr_status); +device_remove_file (hdev->dev, &dev_attr_pci_addr); +device_remove_file (hdev->dev, &dev_attr_device_type); +device_remove_file (hdev->dev, &dev_attr_preboot_btl_ver); +device_remove_file (hdev->dev, &dev_attr_thermal_ver); +device_remove_file (hdev->dev, &dev_attr_fuse_ver); +device_remove_file (hdev->dev, &dev_attr_infineon_ver); +device_remove_file (hdev->dev, &dev_attr_cpld_ver); +device_remove_file (hdev->dev, &dev_attr_armcp_ver); +device_remove_file (hdev->dev, &dev_attr_armcp_kernel_ver); +device_remove_file (hdev->dev, &dev_attr_uboot_ver); +device_remove_file (hdev->dev, &dev_attr_high_pll); +device_remove_file (hdev->dev, &dev_attr_pm_mng_profile); + hdev->asic_funcs->remove_device_attr (hdev); +} --Quoted 2 lines
2.17.1
--
Sincerely yours,
Mike.
PATCH 10/15 habanalabs: add device reset support
In reply to Oded GabbayThis patch adds support for doing various on-the-fly reset of Goya.
The driver supports two types of resets:
1. soft-reset
2. hard-reset
Soft-reset is done when the device detects a timeout of a command submission that was given to the device. The soft-reset process only resets the engines that are relevant for the submission of compute jobs, i.e. the DMA channels, the TPCs and the MME. The purpose is to bring the device as fast as possible to a working state.
Hard-reset is done in several cases:
1. After soft-reset is done but the device is not responding
2. When fatal errors occur inside the device, e.g. ECC error
3. When the driver is removed
Hard-reset performs a reset of the entire chip except for the PCI controller and the PLLs. It is a much longer process then soft-reset but it helps to recover the device without the need to reboot the Host.
After hard-reset, the driver will restore the max power attribute and in case of manual power management, the frequencies that were set.
This patch also adds two entries to the sysfs, which allows the root user to initiate a soft or hard reset.
--- drivers/misc/habanalabs/command_buffer.c | 11 +- drivers/misc/habanalabs/device.c | 308 +++++++++++++++++++++- drivers/misc/habanalabs/goya/goya.c | 201 ++++++++++++++ drivers/misc/habanalabs/goya/goya_hwmgr.c | 18 +- drivers/misc/habanalabs/habanalabs.h | 35 +++ drivers/misc/habanalabs/habanalabs_drv.c | 9 +- drivers/misc/habanalabs/hwmon.c | 4 +- drivers/misc/habanalabs/irq.c | 31 +++ drivers/misc/habanalabs/sysfs.c | 120 ++++++++- 9 files changed, 712 insertions(+), 25 deletions(-)
drivers/misc/habanalabs/command_buffer.c +9 -2
@@ -81,9 +81,10 @@ int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,bool alloc_new_cb = true;int rc; -if (hdev->disabled) { +if ((hdev->disabled) || ((atomic_read (&hdev->in_reset)) && + (ctx_id !=HL_KERNEL_ASID_ID ))) {dev_warn_ratelimited (hdev->dev, -"Device is disabled !!! Can't create new CBs\n" ); +"Device is disabled or in reset !!! Can't create new CBs\n" ); rc = -EBUSY ; goto out_err; }
@@ -187,6 +188,12 @@ int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)u64 handle;int rc; +if (hdev->hard_reset_pending) { +dev_crit_ratelimited (hdev->dev, +"Device HARD reset pending !!! Please close FD\n" ); +return -ENODEV ; + } +switch (args->in.op) {case HL_CB_OP_CREATE : rc = hl_cb_create(hdev, &hpriv->cb_mgr, args->in.cb_size,
drivers/misc/habanalabs/device.c +307 -1
364 lines not on this page · raw message
drivers/misc/habanalabs/goya/goya.c +201 -0
233 lines not on this page · raw message
drivers/misc/habanalabs/goya/goya_hwmgr.c +9 -9
@@ -38,7 +38,7 @@ static ssize_t mme_clk_show(struct device *dev, struct device_attribute *attr,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,MME_PLL , false);
@@ -57,7 +57,7 @@ static ssize_t mme_clk_store(struct device *dev, struct device_attribute *attr,int rc;long value; -if (hdev->disabled) { +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) { count = -ENODEV ; goto fail; }
@@ -87,7 +87,7 @@ static ssize_t tpc_clk_show(struct device *dev, struct device_attribute *attr,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,TPC_PLL , false);
@@ -106,7 +106,7 @@ static ssize_t tpc_clk_store(struct device *dev, struct device_attribute *attr,int rc;long value; -if (hdev->disabled) { +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) { count = -ENODEV ; goto fail; }
@@ -136,7 +136,7 @@ static ssize_t ic_clk_show(struct device *dev, struct device_attribute *attr,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,IC_PLL , false);
@@ -155,7 +155,7 @@ static ssize_t ic_clk_store(struct device *dev, struct device_attribute *attr,int rc;long value; -if (hdev->disabled) { +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) { count = -ENODEV ; goto fail; }
@@ -185,7 +185,7 @@ static ssize_t mme_clk_curr_show(struct device *dev,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,MME_PLL , true);
@@ -202,7 +202,7 @@ static ssize_t tpc_clk_curr_show(struct device *dev,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,TPC_PLL , true);
@@ -219,7 +219,7 @@ static ssize_t ic_clk_curr_show(struct device *dev,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,IC_PLL , true);
drivers/misc/habanalabs/habanalabs.h +35 -0
@@ -23,8 +23,12 @@#define HL_MMAP_CB_MASK (0x8000000000000000ull >> PAGE_SHIFT) +#define HL_PENDING_RESET_PER_SEC 5 +#define HL_DEVICE_TIMEOUT_USEC 1000000/* 1 s */ +#define HL_HEARTBEAT_PER_USEC 5000000/* 5 s */ +#define HL_PLL_LOW_JOB_FREQ_USEC 5000000/* 5 s */ #define HL_MAX_QUEUES 128
@@ -370,8 +374,10 @@ enum hl_pll_frequency {* @handle_eqe: handle event queue entry (IRQ) from ArmCP. * @set_pll_profile: change PLL profile (manual/automatic). * @get_events_stat: retrieve event queue entries histogram. +* @send_heartbeat: send is-alive packet to ArmCP and verify response. * @enable_clock_gating: enable clock gating for reducing power consumption. * @disable_clock_gating: disable clock for accessing registers on HBW. +* @soft_reset_late_init: perform certain actions needed after soft reset. * @hw_queues_lock: acquire H/W queues lock. * @hw_queues_unlock: release H/W queues lock. * @get_eeprom_data: retrieve EEPROM data from F/W.
@@ -417,8 +423,10 @@ struct hl_asic_funcs {void (*set_pll_profile)(struct hl_device *hdev,enum hl_pll_frequency freq);void * (*get_events_stat)(struct hl_device *hdev,u32 *size); +int (*send_heartbeat)(struct hl_device *hdev);void (*enable_clock_gating)(struct hl_device *hdev);void (*disable_clock_gating)(struct hl_device *hdev); +int (*soft_reset_late_init)(struct hl_device *hdev);void (*hw_queues_lock)(struct hl_device *hdev);void (*hw_queues_unlock)(struct hl_device *hdev); int (*get_eeprom_data)(struct hl_device *hdev, void *data,
@@ -544,6 +552,16 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val); WREG32(mm##reg, (RREG32(mm##reg) & ~REG_FIELD_MASK(reg, field)) | \ (val) << REG_FIELD_SHIFT(reg, field)) +/** +* struct hl_device_reset_work - reset workqueue task wrapper. +* @reset_work: reset work to be done. +* @hdev: habanalabs device structure. +*/ +struct hl_device_reset_work { +struct work_struct reset_work; +struct hl_device *hdev; +}; +/** * struct hl_device - habanalabs device structure. * @pdev: pointer to PCI device, can be NULL in case of simulator device.
@@ -552,6 +570,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @cdev: related char device. * @dev: realted kernel basic device structure. * @work_freq: delayed work to lower device frequency if possible. +* @work_heartbeat: delayed work for ArmCP is-alive check. * @asic_name: ASIC specific nmae. * @asic_type: ASIC specific type. * @completion_queue: array of hl_cq.
@@ -579,6 +598,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* @cb_pool: list of preallocated CBs. * @cb_pool_lock: protects the CB pool. * @user_ctx: current user context executing. +* @in_reset: is device in reset flow. * @curr_pll_profile: current PLL profile. * @fd_open_cnt: number of open context executing. * @max_power: the max power of the device, as configured by the sysadmin. This
@@ -586,10 +606,14 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);* value and update the F/W after the re-initialization * @major: habanalabs KMD major. * @high_pll: high PLL profile frequency. +* @soft_reset_cnt: number of soft reset since KMD loading. +* @hard_reset_cnt: number of hard reset since KMD loading. * @id: device minor. * @disabled: is device disabled. * @late_init_done: is late init stage was done during initialization. * @hwmon_initialized: is H/W monitor sensors was initialized. +* @hard_reset_pending: is there a hard reset work pending. +* @heartbeat: is heartbeat sanity check towards ArmCP enabled. */ struct hl_device {struct pci_dev *pdev;
@@ -598,6 +622,7 @@ struct hl_device {struct cdev cdev;struct device *dev;struct delayed_work work_freq; +struct delayed_work work_heartbeat;char asic_name[16 ];enum hl_asic_type asic_type;struct hl_cq *completion_queue;
@@ -630,15 +655,20 @@ struct hl_device {/* TODO: The following fields should be moved for multi-context */ struct hl_ctx *user_ctx; +atomic_t in_reset;atomic_t curr_pll_profile;atomic_t fd_open_cnt;u64 max_power;u32 major;u32 high_pll; +u32 soft_reset_cnt; +u32 hard_reset_cnt;u16 id;u8 disabled;u8 late_init_done;u8 hwmon_initialized; +u8 hard_reset_pending; +u8 heartbeat;/* Parameters for bring-up */ u8 cpu_enable;
@@ -696,6 +726,7 @@ int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id, u32 cb_size, u64 cb_ptr);u32 hl_hw_queue_add_ptr (u32 ptr,u16 val);void hl_hw_queue_inc_ci_kernel (struct hl_device *hdev,u32 hw_queue_id); +void hl_hw_queue_reset (struct hl_device *hdev,bool hard_reset);#define hl_queue_inc_ptr (p) hl_hw_queue_add_ptr(p, 1)#define hl_pi_2_offset (pi) ((pi) & (HL_QUEUE_LENGTH - 1))
@@ -704,6 +735,8 @@ 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);int hl_eq_init (struct hl_device *hdev,struct hl_eq *q);void hl_eq_fini (struct hl_device *hdev,struct hl_eq *q); +void hl_cq_reset (struct hl_device *hdev,struct hl_cq *q); +void hl_eq_reset (struct hl_device *hdev,struct hl_eq *q);irqreturn_t hl_irq_handler_cq (int irq,void *arg);irqreturn_t hl_irq_handler_eq (int irq,void *arg);int hl_asid_init (struct hl_device *hdev);
@@ -721,6 +754,8 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass);void hl_device_fini (struct hl_device *hdev);int hl_device_suspend (struct hl_device *hdev);int hl_device_resume (struct hl_device *hdev); +int hl_device_reset (struct hl_device *hdev,bool hard_reset, +bool from_hard_reset_thread);void hl_hpriv_get (struct hl_fpriv *hpriv);void hl_hpriv_put (struct hl_fpriv *hpriv);int hl_device_set_frequency (struct hl_device *hdev,enum hl_pll_frequency freq);
drivers/misc/habanalabs/habanalabs_drv.c +7 -2
@@ -91,9 +91,9 @@ int hl_device_open(struct inode *inode, struct file *filp)mutex_lock (&hdev->device_open); -if (hdev->disabled) { +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) {dev_err_ratelimited (hdev->dev, -"Can't open %s because it is disabled\n" , +"Can't open %s because it is disabled or in reset\n" ,dev_name (hdev->dev));mutex_unlock (&hdev->device_open);return -EPERM ;
@@ -195,6 +195,7 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev, hdev->fw_loading =1 ; hdev->ifh =0 ; hdev->pldm =0 ; + hdev->heartbeat =1 ;/* If CPU is disabled, no point in loading FW */ if (!hdev->cpu_enable)
@@ -204,6 +205,10 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,if (!hdev->fw_loading) hdev->cpu_queues_enable =0 ; +/* If CPU queues not enabled, no way to do heartbeat */ +if (!hdev->cpu_queues_enable) + hdev->heartbeat =0 ; + hdev->disabled = true; hdev->pdev = pdev;/* can be NULL in case of simulator device */
drivers/misc/habanalabs/hwmon.c +2 -2
@@ -111,7 +111,7 @@ static int hl_read(struct device *dev, enum hwmon_sensor_types type, {struct hl_device *hdev =dev_get_drvdata (dev); -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ;switch (type) {
@@ -185,7 +185,7 @@ static int hl_write(struct device *dev, enum hwmon_sensor_types type, {struct hl_device *hdev =dev_get_drvdata (dev); -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ;switch (type) {
drivers/misc/habanalabs/irq.c +31 -0
@@ -250,6 +250,23 @@ void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q) (void *) q->kernel_address, q->bus_address); } +void hl_cq_reset (struct hl_device *hdev,struct hl_cq *q) +{ + q->ci =0 ; + q->pi =0 ; + +atomic_set (&q->free_slots_cnt,HL_CQ_LENGTH ); + +/* +* It's not enough to just reset the PI/CI because the H/W may have +* written valid completion entries before it was halted and therefore +* we need to clean the actual queues so we won't process old entries +* when the device is operational again +*/ + +memset ((void *) q->kernel_address,0 ,HL_CQ_SIZE_IN_BYTES ); +} +/** * hl_eq_init - main initialization function for an event queue object *
@@ -292,3 +309,17 @@ void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q) hdev->asic_funcs->dma_free_coherent (hdev,HL_EQ_SIZE_IN_BYTES , (void *) q->kernel_address, q->bus_address); } + +void hl_eq_reset (struct hl_device *hdev,struct hl_eq *q) +{ + q->ci =0 ; + +/* +* It's not enough to just reset the PI/CI because the H/W may have +* written valid completion entries before it was halted and therefore +* we need to clean the actual queues so we won't process old entries +* when the device is operational again +*/ + +memset ((void *) q->kernel_address,0 ,HL_EQ_SIZE_IN_BYTES ); +}
drivers/misc/habanalabs/sysfs.c +111 -9
228 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 10/15] habanalabs: add device reset support
Quoted 58 lines · Oded Gabbay
This patch adds support for doing various on-the-fly reset of Goya.
The driver supports two types of resets:
1. soft-reset
2. hard-resetSoft-reset is done when the device detects a timeout of a command submission that was given to the device. The soft-reset process only resets the engines that are relevant for the submission of compute jobs, i.e. the DMA channels, the TPCs and the MME. The purpose is to bring the device as fast as possible to a working state.
Hard-reset is done in several cases:
1. After soft-reset is done but the device is not responding
2. When fatal errors occur inside the device, e.g. ECC error
3. When the driver is removedHard-reset performs a reset of the entire chip except for the PCI controller and the PLLs. It is a much longer process then soft-reset but it helps to recover the device without the need to reboot the Host.
After hard-reset, the driver will restore the max power attribute and in case of manual power management, the frequencies that were set.
This patch also adds two entries to the sysfs, which allows the root user to initiate a soft or hard reset.
Signed-off-by Oded Gabbay <[email protected]>--- drivers/misc/habanalabs/command_buffer.c | 11 +- drivers/misc/habanalabs/device.c | 308 +++++++++++++++++++++- drivers/misc/habanalabs/goya/goya.c | 201 ++++++++++++++ drivers/misc/habanalabs/goya/goya_hwmgr.c | 18 +- drivers/misc/habanalabs/habanalabs.h | 35 +++ drivers/misc/habanalabs/habanalabs_drv.c | 9 +- drivers/misc/habanalabs/hwmon.c | 4 +- drivers/misc/habanalabs/irq.c | 31 +++ drivers/misc/habanalabs/sysfs.c | 120 ++++++++- 9 files changed, 712 insertions(+), 25 deletions(-)drivers/misc/habanalabs/command_buffer.c +3
-2@@ -81,9 +81,10 @@ int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,bool alloc_new_cb = true;int rc; -if (hdev->disabled) { +if ((hdev->disabled) || ((atomic_read (&hdev->in_reset)) && + (ctx_id !=HL_KERNEL_ASID_ID ))) {dev_warn_ratelimited (hdev->dev, -"Device is disabled !!! Can't create new CBs\n" ); +"Device is disabled or in reset !!! Can't create new CBs\n" ); rc = -EBUSY ; goto out_err; }
drivers/misc/habanalabs/command_buffer.c int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data) · line 195
Quoted 8 lines
drivers/misc/habanalabs/command_buffer.c +5
-0@@ -187,6 +188,12 @@ int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)u64 handle;int rc; +if (hdev->hard_reset_pending) { +dev_crit_ratelimited (hdev->dev, +"Device HARD reset pending !!! Please close FD\n" ); +return -ENODEV ; + }
Probably this check should be done at the top-level ioctl()? And, what will happen if the devices performs hard reset, but the used keeps the file descriptor open?
Quoted 20 lines
drivers/misc/habanalabs/command_buffer.c +1
-0+switch (args->in.op) {case HL_CB_OP_CREATE : rc = hl_cb_create(hdev, &hpriv->cb_mgr, args->in.cb_size,drivers/misc/habanalabs/device.c +1
-0@@ -188,6 +188,7 @@ static int device_early_init(struct hl_device *hdev)mutex_init (&hdev->device_open);mutex_init (&hdev->send_cpu_message_lock); +atomic_set (&hdev->in_reset,0 );atomic_set (&hdev->fd_open_cnt,0 );return 0 ;@@ -238,6 +239,27 @@ static void set_freq_to_low_job(struct work_struct *work) usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC)); }
drivers/misc/habanalabs/device.c static void set_freq_to_low_job(struct work_struct *work) · line 251
Quoted 10 lines
drivers/misc/habanalabs/device.c +10
-0+static void hl_device_heartbeat (struct work_struct *work) +{ + struct hl_device *hdev = container_of(work, struct hl_device, + work_heartbeat.work); + +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) + goto reschedule; + +if (!hdev->asic_funcs->send_heartbeat (hdev)) + goto reschedule;
AFAIU, asic_funcs->send_heartbeat() it set once at init time. The work should not be scheduled it it's NULL, I suppose.
Quoted 56 lines
drivers/misc/habanalabs/device.c +35
-0+ +dev_err (hdev->dev,"Device heartbeat failed !!!\n" ); +hl_device_reset (hdev, true, false); + +return ; + +reschedule: +schedule_delayed_work (&hdev->work_heartbeat, +usecs_to_jiffies (HL_HEARTBEAT_PER_USEC )); +} +/** * device_late_init - do late stuff initialization for the habanalabs device * @@ -273,6 +295,12 @@ static int device_late_init(struct hl_device *hdev)schedule_delayed_work (&hdev->work_freq,usecs_to_jiffies (HL_PLL_LOW_JOB_FREQ_USEC )); +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 )); + } + hdev->late_init_done = true;return 0 ;@@ -290,6 +318,8 @@ static void device_late_fini(struct hl_device *hdev)return ;cancel_delayed_work_sync (&hdev->work_freq); +if (hdev->heartbeat) +cancel_delayed_work_sync (&hdev->work_heartbeat);if (hdev->asic_funcs->late_fini) hdev->asic_funcs->late_fini (hdev);@@ -397,6 +427,254 @@ int hl_device_resume(struct hl_device *hdev)return 0 ; } +static void hl_device_hard_reset_pending (struct work_struct *work) +{ +struct hl_device_reset_work *device_reset_work = + container_of(work, struct hl_device_reset_work, reset_work); +struct hl_device *hdev = device_reset_work->hdev; +u16 pending_cnt =HL_PENDING_RESET_PER_SEC ; +struct task_struct *task =NULL ; + +/* Flush all processes that are inside hl_open */ +mutex_lock (&hdev->device_open); + +while ((atomic_read (&hdev->fd_open_cnt)) && (pending_cnt)) { + + pending_cnt--; + + dev_info(hdev->dev,
drivers/misc/habanalabs/device.c int hl_device_resume(struct hl_device *hdev) · line 455
Quoted 10 lines
drivers/misc/habanalabs/device.c +10
-0+ "Can't HARD reset, waiting for user to close FD\n"); +ssleep (1 ); + } + +if (atomic_read (&hdev->fd_open_cnt)) { + task =get_pid_task (hdev->user_ctx->hpriv->taskpid, +PIDTYPE_PID ); +if (task) { +dev_info (hdev->dev,"Killing user processes\n" ); +send_sig (SIGKILL , task,1 );
Shouldn't the user get a chance for cleanup?
Quoted 13 lines
drivers/misc/habanalabs/device.c +13
-0+msleep (100 ); + +put_task_struct (task); + } + } + +mutex_unlock (&hdev->device_open); + +hl_device_reset (hdev, true, true); + +kfree (device_reset_work); +} +
[ ... ]
Quoted 14 lines
drivers/misc/habanalabs/goya/goya_hwmgr.c +1
-1@@ -38,7 +38,7 @@ static ssize_t mme_clk_show(struct device *dev, struct device_attribute *attr,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,MME_PLL , false);
drivers/misc/habanalabs/goya/goya_hwmgr.c static ssize_t mme_clk_store(struct device *dev, struct device_attribute *attr, · line 60
Quoted 5 lines
drivers/misc/habanalabs/goya/goya_hwmgr.c +1
-1@@ -57,7 +57,7 @@ static ssize_t mme_clk_store(struct device *dev, struct device_attribute *attr,int rc;long value; -if (hdev->disabled) { +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) {
There are quite a few of those, maybe split this check to a helper function?
Quoted 21 lines
drivers/misc/habanalabs/goya/goya_hwmgr.c +2
-2count = -ENODEV ; goto fail; }@@ -87,7 +87,7 @@ static ssize_t tpc_clk_show(struct device *dev, struct device_attribute *attr,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,TPC_PLL , false);@@ -106,7 +106,7 @@ static ssize_t tpc_clk_store(struct device *dev, struct device_attribute *attr,int rc;long value; -if (hdev->disabled) { +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) { count = -ENODEV ; goto fail; }
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 10/15] habanalabs: add device reset support
Quoted 63 lines · Mike Rapoport
Quoted 61 lines · Oded Gabbay
This patch adds support for doing various on-the-fly reset of Goya.
The driver supports two types of resets:
1. soft-reset
2. hard-resetSoft-reset is done when the device detects a timeout of a command submission that was given to the device. The soft-reset process only resets the engines that are relevant for the submission of compute jobs, i.e. the DMA channels, the TPCs and the MME. The purpose is to bring the device as fast as possible to a working state.
Hard-reset is done in several cases:
1. After soft-reset is done but the device is not responding
2. When fatal errors occur inside the device, e.g. ECC error
3. When the driver is removedHard-reset performs a reset of the entire chip except for the PCI controller and the PLLs. It is a much longer process then soft-reset but it helps to recover the device without the need to reboot the Host.
After hard-reset, the driver will restore the max power attribute and in case of manual power management, the frequencies that were set.
This patch also adds two entries to the sysfs, which allows the root user to initiate a soft or hard reset.
Signed-off-by Oded Gabbay <[email protected]>--- drivers/misc/habanalabs/command_buffer.c | 11 +- drivers/misc/habanalabs/device.c | 308 +++++++++++++++++++++- drivers/misc/habanalabs/goya/goya.c | 201 ++++++++++++++ drivers/misc/habanalabs/goya/goya_hwmgr.c | 18 +- drivers/misc/habanalabs/habanalabs.h | 35 +++ drivers/misc/habanalabs/habanalabs_drv.c | 9 +- drivers/misc/habanalabs/hwmon.c | 4 +- drivers/misc/habanalabs/irq.c | 31 +++ drivers/misc/habanalabs/sysfs.c | 120 ++++++++- 9 files changed, 712 insertions(+), 25 deletions(-)drivers/misc/habanalabs/command_buffer.c +3
-2@@ -81,9 +81,10 @@ int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,bool alloc_new_cb = true;int rc; -if (hdev->disabled) { +if ((hdev->disabled) || ((atomic_read (&hdev->in_reset)) && + (ctx_id !=HL_KERNEL_ASID_ID ))) {dev_warn_ratelimited (hdev->dev, -"Device is disabled !!! Can't create new CBs\n" ); +"Device is disabled or in reset !!! Can't create new CBs\n" ); rc = -EBUSY ; goto out_err; }@@ -187,6 +188,12 @@ int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)u64 handle;int rc;
Quoted 7 lines
drivers/misc/habanalabs/command_buffer.c int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data) · line 195
Quoted 5 lines
drivers/misc/habanalabs/command_buffer.c +5
-0+if (hdev->hard_reset_pending) { +dev_crit_ratelimited (hdev->dev, +"Device HARD reset pending !!! Please close FD\n" ); +return -ENODEV ; + }Probably this check should be done at the top-level
ioctl()?
fixed
Quoted 2 lines
And, what will happen if the devices performs hard reset, but the used keeps the file descriptor open?
I take care of that in the reset function. Basically, I don't do the hard-reset until all user processes (and currently I only support a single one) close their FDs.
And if they don't close it after a timeout, I kill the user processes. Take a look at hl_device_hard_reset_pending()
Quoted 21 lines
Quoted 21 lines
drivers/misc/habanalabs/command_buffer.c +1
-0+switch (args->in.op) {case HL_CB_OP_CREATE : rc = hl_cb_create(hdev, &hpriv->cb_mgr, args->in.cb_size,drivers/misc/habanalabs/device.c +1
-0@@ -188,6 +188,7 @@ static int device_early_init(struct hl_device *hdev)mutex_init (&hdev->device_open);mutex_init (&hdev->send_cpu_message_lock); +atomic_set (&hdev->in_reset,0 );atomic_set (&hdev->fd_open_cnt,0 );return 0 ;@@ -238,6 +239,27 @@ static void set_freq_to_low_job(struct work_struct *work) usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC)); }
Quoted 13 lines
drivers/misc/habanalabs/device.c static void set_freq_to_low_job(struct work_struct *work) · line 251
Quoted 10 lines
drivers/misc/habanalabs/device.c +10
-0+static void hl_device_heartbeat (struct work_struct *work) +{ + struct hl_device *hdev = container_of(work, struct hl_device, + work_heartbeat.work); + +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) + goto reschedule; + +if (!hdev->asic_funcs->send_heartbeat (hdev)) + goto reschedule;AFAIU,
asic_funcs->send_heartbeat()it set once at init time. The work should not be scheduled it it's NULL, I suppose.
I don't check her if the function pointer is NULL. I check the return value of the call to the function. The function itself is always implemented
Quoted 61 lines
Quoted 61 lines
drivers/misc/habanalabs/device.c +39
-0+ +dev_err (hdev->dev,"Device heartbeat failed !!!\n" ); +hl_device_reset (hdev, true, false); + +return ; + +reschedule: +schedule_delayed_work (&hdev->work_heartbeat, +usecs_to_jiffies (HL_HEARTBEAT_PER_USEC )); +} +/** * device_late_init - do late stuff initialization for the habanalabs device * @@ -273,6 +295,12 @@ static int device_late_init(struct hl_device *hdev)schedule_delayed_work (&hdev->work_freq,usecs_to_jiffies (HL_PLL_LOW_JOB_FREQ_USEC )); +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 )); + } + hdev->late_init_done = true;return 0 ;@@ -290,6 +318,8 @@ static void device_late_fini(struct hl_device *hdev)return ;cancel_delayed_work_sync (&hdev->work_freq); +if (hdev->heartbeat) +cancel_delayed_work_sync (&hdev->work_heartbeat);if (hdev->asic_funcs->late_fini) hdev->asic_funcs->late_fini (hdev);@@ -397,6 +427,254 @@ int hl_device_resume(struct hl_device *hdev)return 0 ; } +static void hl_device_hard_reset_pending (struct work_struct *work) +{ +struct hl_device_reset_work *device_reset_work = + container_of(work, struct hl_device_reset_work, reset_work); +struct hl_device *hdev = device_reset_work->hdev; +u16 pending_cnt =HL_PENDING_RESET_PER_SEC ; +struct task_struct *task =NULL ; + +/* Flush all processes that are inside hl_open */ +mutex_lock (&hdev->device_open); + +while ((atomic_read (&hdev->fd_open_cnt)) && (pending_cnt)) { + + pending_cnt--; + +dev_info (hdev->dev, +"Can't HARD reset, waiting for user to close FD\n" ); +ssleep (1 ); + } +
Quoted 8 lines
drivers/misc/habanalabs/device.c int hl_device_resume(struct hl_device *hdev) · line 455
Quoted 6 lines
drivers/misc/habanalabs/device.c +6
-0+if (atomic_read (&hdev->fd_open_cnt)) { + task =get_pid_task (hdev->user_ctx->hpriv->taskpid, +PIDTYPE_PID ); +if (task) { +dev_info (hdev->dev,"Killing user processes\n" ); +send_sig (SIGKILL , task,1 );Shouldn't the user get a chance for cleanup?
I give them 5 seconds - It's eternity :)
This is a question where I deliberated with myself a lot about. Should I kill the process to do the hard-reset automatically, or wait until the FD is closed, and potentially never hard-reset because the user will never close the FD.
Currently I decided to do the former. I guess that if users won't like this behavior, I may add a kernel parameter to control this behavior.
Quoted 34 lines
Quoted 14 lines
drivers/misc/habanalabs/device.c +13
-0+msleep (100 ); + +put_task_struct (task); + } + } + +mutex_unlock (&hdev->device_open); + +hl_device_reset (hdev, true, true); + +kfree (device_reset_work); +} +[ ... ]
Quoted 17 lines
drivers/misc/habanalabs/goya/goya_hwmgr.c +1
-1@@ -38,7 +38,7 @@ static ssize_t mme_clk_show(struct device *dev, struct device_attribute *attr,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,MME_PLL , false);@@ -57,7 +57,7 @@ static ssize_t mme_clk_store(struct device *dev, struct device_attribute *attr,int rc;long value;
Quoted 5 lines
drivers/misc/habanalabs/goya/goya_hwmgr.c static ssize_t mme_clk_store(struct device *dev, struct device_attribute *attr, · line 60
Quoted 2 lines
drivers/misc/habanalabs/goya/goya_hwmgr.c +1
-1-if (hdev->disabled) { +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) {There are quite a few of those, maybe split this check to a helper function?
Fixed
Quoted 27 lines
Quoted 14 lines
drivers/misc/habanalabs/goya/goya_hwmgr.c +1
-1count = -ENODEV ; goto fail; }@@ -87,7 +87,7 @@ static ssize_t tpc_clk_show(struct device *dev, struct device_attribute *attr,struct hl_device *hdev =dev_get_drvdata (dev);long value; -if (hdev->disabled) +if ((hdev->disabled) || (atomic_read (&hdev->in_reset)))return -ENODEV ; value =hl_get_frequency (hdev,TPC_PLL , false);drivers/misc/habanalabs/goya/goya_hwmgr.c static ssize_t tpc_clk_store(struct device *dev, struct device_attribute *attr, · line 112
Quoted 8 lines
drivers/misc/habanalabs/goya/goya_hwmgr.c +1
-1@@ -106,7 +106,7 @@ static ssize_t tpc_clk_store(struct device *dev, struct device_attribute *attr,int rc;long value; -if (hdev->disabled) { +if ((hdev->disabled) || (atomic_read (&hdev->in_reset))) { count = -ENODEV ; goto fail; }--
Sincerely yours,
Mike.
PATCH 11/15 habanalabs: add command submission module
In reply to Oded GabbayThis patch adds the main flow for the user to submit work to the device.
Each work is described by a command submission object (CS). The CS contains 3 arrays of command buffers: One for execution, and two for context-switch (store and restore).
For each CB, the user specifies on which queue to put that CB. In case of an internal queue, the entry doesn't contain a pointer to the CB but the address in the on-chip memory that the CB resides at.
The driver parses some of the CBs to enforce security restrictions.
The user receives a sequence number that represents the CS object. The user can then query the driver regarding the status of the CS, using that sequence number.
In case the CS doesn't finish before the timeout expires, the driver will perform a soft-reset of the device.
--- drivers/misc/habanalabs/Makefile | 3 +- drivers/misc/habanalabs/command_submission.c | 787 +++++++++++++ drivers/misc/habanalabs/context.c | 52 +- drivers/misc/habanalabs/device.c | 16 + drivers/misc/habanalabs/goya/goya.c | 1082 ++++++++++++++++++ drivers/misc/habanalabs/habanalabs.h | 274 +++++ drivers/misc/habanalabs/habanalabs_drv.c | 23 + drivers/misc/habanalabs/habanalabs_ioctl.c | 4 +- drivers/misc/habanalabs/hw_queue.c | 250 ++++ drivers/misc/habanalabs/memory.c | 200 ++++ include/uapi/misc/habanalabs.h | 158 ++- 11 files changed, 2842 insertions(+), 7 deletions(-) create mode 100644 drivers/misc/habanalabs/command_submission.c create mode 100644 drivers/misc/habanalabs/memory.c
drivers/misc/habanalabs/Makefile +2 -1
@@ -5,7 +5,8 @@ obj-m := habanalabs.o habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \ - command_buffer.o hw_queue.o irq.o sysfs.o hwmon.o + command_buffer.o hw_queue.o irq.o sysfs.o hwmon.o memory.o \ + command_submission.o include $(src)/goya/Makefile habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/command_submission.c new file +787 -0
787 lines not on this page · raw message
drivers/misc/habanalabs/context.c +48 -4
82 lines not on this page · raw message
drivers/misc/habanalabs/device.c +16 -0
@@ -22,6 +22,8 @@ static void hpriv_release(struct kref *ref)put_pid (hpriv->taskpid); +mutex_destroy (&hpriv->restore_phase_mutex); +kfree (hpriv);/* Now the FD is really closed */
@@ -188,6 +190,8 @@ static int device_early_init(struct hl_device *hdev)mutex_init (&hdev->device_open);mutex_init (&hdev->send_cpu_message_lock); +INIT_LIST_HEAD (&hdev->hw_queues_mirror_list); +spin_lock_init (&hdev->hw_queues_mirror_lock);atomic_set (&hdev->in_reset,0 );atomic_set (&hdev->fd_open_cnt,0 );
@@ -563,6 +567,9 @@ int hl_device_reset(struct hl_device *hdev, bool hard_reset,*/ hdev->asic_funcs->halt_engines (hdev, hard_reset); +/* Go over all the queues, release all CS and their jobs */ +hl_cs_rollback_all (hdev); +if (hard_reset) {/* Release kernel context */ if (hl_ctx_put (hdev->kernel_ctx) !=1 ) {
@@ -586,6 +593,12 @@ int hl_device_reset(struct hl_device *hdev, bool hard_reset,for (i =0 ; i < hdev->asic_prop.completion_queues_count ; i++)hl_cq_reset (hdev, &hdev->completion_queue[i]); +/* Make sure the setup phase for the user context will run again */ +if (hdev->user_ctx) { +atomic_set (&hdev->user_ctx->thread_restore_token,1 ); + hdev->user_ctx->thread_restore_wait_token =0 ; + } +/* Finished tear-down, starting to re-initialize */ if (hard_reset) {
@@ -916,6 +929,9 @@ void hl_device_fini(struct hl_device *hdev)*/ hdev->asic_funcs->halt_engines (hdev, true); +/* Go over all the queues, release all CS and their jobs */ +hl_cs_rollback_all (hdev); +hl_cb_pool_fini (hdev);/* Release kernel context */
drivers/misc/habanalabs/goya/goya.c +1082 -0
1,124 lines not on this page · raw message
drivers/misc/habanalabs/habanalabs.h +274 -0
456 lines not on this page · raw message
drivers/misc/habanalabs/habanalabs_drv.c +23 -0
@@ -30,6 +30,17 @@ static struct class *hl_class;DEFINE_IDR (hl_devs_idr);DEFINE_MUTEX (hl_devs_idr_lock); +static int timeout_locked =5 ; +static int reset_on_lockup =1 ; + +module_param (timeout_locked, int,0444 ); +MODULE_PARM_DESC (timeout_locked, +"Device lockup timeout in seconds (0 = disabled, default 5s)" ); + +module_param (reset_on_lockup, int,0444 ); +MODULE_PARM_DESC (reset_on_lockup, +"Do device reset on lockup (0 = no, 1 = yes, default yes)" ); +#define PCI_VENDOR_ID_HABANALABS 0x1da3#define PCI_IDS_GOYA 0x0001
@@ -120,6 +131,7 @@ int hl_device_open(struct inode *inode, struct file *filp) hpriv->hdev = hdev; filp->private_data = hpriv; hpriv->filp = filp; +mutex_init (&hpriv->restore_phase_mutex);kref_init (&hpriv->refcount);nonseekable_open (inode, filp);
@@ -147,6 +159,7 @@ int hl_device_open(struct inode *inode, struct file *filp) filp->private_data =NULL ;hl_ctx_mgr_fini (hpriv->hdev, &hpriv->ctx_mgr);hl_cb_mgr_fini (hpriv->hdev, &hpriv->cb_mgr); +mutex_destroy (&hpriv->restore_phase_mutex);kfree (hpriv); close_device:
@@ -186,8 +199,10 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev, } hdev->major = hl_major; + hdev->reset_on_lockup = reset_on_lockup;/* Parameters for bring-up - set them to defaults */ + hdev->mmu_enable =0 ; hdev->cpu_enable =1 ; hdev->reset_pcilink =0 ; hdev->config_pll =0 ;
@@ -209,6 +224,14 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,if (!hdev->cpu_queues_enable) hdev->heartbeat =0 ; +if (hdev->ifh) + timeout_locked =0 ; + +if (timeout_locked) + hdev->timeout_jiffies =msecs_to_jiffies (timeout_locked *1000 ); +else + hdev->timeout_jiffies =MAX_SCHEDULE_TIMEOUT ; + hdev->disabled = true; hdev->pdev = pdev;/* can be NULL in case of simulator device */
drivers/misc/habanalabs/habanalabs_ioctl.c +3 -1
@@ -16,7 +16,9 @@ [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}static const struct hl_ioctl_desc hl_ioctls[] = { -HL_IOCTL_DEF (HL_IOCTL_CB , hl_cb_ioctl) +HL_IOCTL_DEF (HL_IOCTL_CB , hl_cb_ioctl), +HL_IOCTL_DEF (HL_IOCTL_CS , hl_cs_ioctl), +HL_IOCTL_DEF (HL_IOCTL_WAIT_CS , hl_cs_wait_ioctl) };#define HL_CORE_IOCTL_COUNT ARRAY_SIZE(hl_ioctls)
drivers/misc/habanalabs/hw_queue.c +250 -0
268 lines not on this page · raw message
drivers/misc/habanalabs/memory.c new file +200 -0
163 lines not on this page · raw message
include/uapi/misc/habanalabs.h +157 -1
158 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.17.1
Part of the diff is not shown here. View the raw message.
Re: [PATCH 11/15] habanalabs: add command submission module
Quoted 72 lines · Oded Gabbay
This patch adds the main flow for the user to submit work to the device.
Each work is described by a command submission object (CS). The CS contains 3 arrays of command buffers: One for execution, and two for context-switch (store and restore).
For each CB, the user specifies on which queue to put that CB. In case of an internal queue, the entry doesn't contain a pointer to the CB but the address in the on-chip memory that the CB resides at.
The driver parses some of the CBs to enforce security restrictions.
The user receives a sequence number that represents the CS object. The user can then query the driver regarding the status of the CS, using that sequence number.
In case the CS doesn't finish before the timeout expires, the driver will perform a soft-reset of the device.
Signed-off-by Oded Gabbay <[email protected]>--- drivers/misc/habanalabs/Makefile | 3 +- drivers/misc/habanalabs/command_submission.c | 787 +++++++++++++ drivers/misc/habanalabs/context.c | 52 +- drivers/misc/habanalabs/device.c | 16 + drivers/misc/habanalabs/goya/goya.c | 1082 ++++++++++++++++++ drivers/misc/habanalabs/habanalabs.h | 274 +++++ drivers/misc/habanalabs/habanalabs_drv.c | 23 + drivers/misc/habanalabs/habanalabs_ioctl.c | 4 +- drivers/misc/habanalabs/hw_queue.c | 250 ++++ drivers/misc/habanalabs/memory.c | 200 ++++ include/uapi/misc/habanalabs.h | 158 ++- 11 files changed, 2842 insertions(+), 7 deletions(-) create mode 100644 drivers/misc/habanalabs/command_submission.c create mode 100644 drivers/misc/habanalabs/memory.cdrivers/misc/habanalabs/Makefile +2
-1@@ -5,7 +5,8 @@ obj-m := habanalabs.o habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \ - command_buffer.o hw_queue.o irq.o sysfs.o hwmon.o + command_buffer.o hw_queue.o irq.o sysfs.o hwmon.o memory.o \ + command_submission.o include $(src)/goya/Makefile habanalabs-y += $(HL_GOYA_FILES)drivers/misc/habanalabs/command_submission.c new file +16
-0@@ -0,0 +1,787 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright 2016-2018 HabanaLabs, Ltd. +* All Rights Reserved. +*/ + +#include <uapi/misc/habanalabs.h> +#include "habanalabs.h" + +#include <linux/sched/mm.h> +#include <linux/sched/task.h> +#include <linux/sched/signal.h> +#include <linux/wait.h> +#include <linux/mm.h> +#include <linux/highmem.h>
[ ... ]
Quoted 43 lines
drivers/misc/habanalabs/command_submission.c new file +43
-0+static void cs_do_release (struct kref *ref) +{ + struct hl_cs *cs = container_of(ref, struct hl_cs, + refcount); +struct hl_device *hdev = cs->ctx->hdev; +struct hl_cs_job *job, *tmp; + + cs->completed = true; + +/* +* Although if we reached here it means that all external jobs have +* finished, because each one of them took refcnt to CS, we still +* need to go over the internal jobs and free them. Otherwise, we +* will have leaked memory and what's worse, the CS object (and +* potentially the CTX object) could be released, while the JOB +* still holds a pointer to them (but no reference). +*/ +list_for_each_entry_safe (job, tmp, &cs->job_list, cs_node) +free_job (hdev, job); + +/* We also need to update CI for internal queues */ +if (cs->submitted) { +hl_int_hw_queue_update_ci (cs); + +spin_lock (&hdev->hw_queues_mirror_lock); +/* remove CS from hw_queues mirror list */ +list_del_init (&cs->mirror_node); +spin_unlock (&hdev->hw_queues_mirror_lock); + +/* +* Don't cancel TDR in case this CS was timedout because we +* might be running from the TDR context +*/ +if ((!cs->timedout) && + (hdev->timeout_jiffies !=MAX_SCHEDULE_TIMEOUT )) { +struct hl_cs *next; + +if (cs->tdr_active) +cancel_delayed_work_sync (&cs->work_tdr); + +spin_lock (&hdev->hw_queues_mirror_lock); +/* queue TDR for next CS */ + next = list_first_entry_or_null(
drivers/misc/habanalabs/command_submission.c line 214
Quoted 10 lines
drivers/misc/habanalabs/command_submission.c new file +10
-0+ &hdev->hw_queues_mirror_list, + struct hl_cs, mirror_node); +if ((next) && (!next->tdr_active)) { + next->tdr_active = true; +schedule_delayed_work (&next->work_tdr, + hdev->timeout_jiffies); +spin_unlock (&hdev->hw_queues_mirror_lock); + }else { +spin_unlock (&hdev->hw_queues_mirror_lock); + }
'else' can be dropped, just move spin_unlock() outside the 'if'
Quoted 15 lines
drivers/misc/habanalabs/command_submission.c new file +15
-0+ } + } + +hl_ctx_put (cs->ctx); + +if (cs->timedout) +dma_fence_set_error (cs->fence, -ETIMEDOUT ); +else if (cs->aborted) +dma_fence_set_error (cs->fence, -EIO ); + +dma_fence_signal (cs->fence); +dma_fence_put (cs->fence); + +kfree (cs); +}
[ ... ]
drivers/misc/habanalabs/command_submission.c line 271
Quoted 11 lines
drivers/misc/habanalabs/command_submission.c new file +11
-0+static int allocate_cs (struct hl_device *hdev,struct hl_ctx *ctx, +struct hl_cs **cs_new) +{ +struct hl_dma_fence *fence; +struct dma_fence *other =NULL ; +struct hl_cs *cs; +int rc; + + cs =kzalloc (sizeof (*cs),GFP_ATOMIC ); +if (!cs) +return -ENOMEM ;
Does this ever run from a context that cannot use GFP_KERNEL? This applies to other allocations below.
drivers/misc/habanalabs/command_submission.c line 281
Quoted 10 lines
drivers/misc/habanalabs/command_submission.c new file +10
-0+ + cs->ctx = ctx; + cs->submitted = false; + cs->completed = false; +INIT_LIST_HEAD (&cs->job_list); +INIT_DELAYED_WORK (&cs->work_tdr, cs_timedout); +kref_init (&cs->refcount); +spin_lock_init (&cs->job_lock); + + fence =kmalloc (sizeof (*fence),GFP_ATOMIC );
kzalloc?
Quoted 45 lines
drivers/misc/habanalabs/command_submission.c new file +45
-0+if (!fence) { + rc = -ENOMEM ; + goto free_cs; + } + + fence->hdev = hdev; +spin_lock_init (&fence->lock); + cs->fence = &fence->base_fence; + +spin_lock (&ctx->cs_lock); + + fence->cs_seq = ctx->cs_sequence; + other = ctx->cs_pending[fence->cs_seq & (HL_MAX_PENDING_CS -1 )]; +if ((other) && (!dma_fence_is_signaled (other))) { +spin_unlock (&ctx->cs_lock); + rc = -EAGAIN ; + goto free_fence; + } + +dma_fence_init (&fence->base_fence, &hl_fence_ops, &fence->lock, + ctx->asid, ctx->cs_sequence); + + cs->sequence = fence->cs_seq; + + ctx->cs_pending[fence->cs_seq & (HL_MAX_PENDING_CS -1 )] = + &fence->base_fence; + ctx->cs_sequence++; + +dma_fence_get (&fence->base_fence); + +dma_fence_put (other); + +spin_unlock (&ctx->cs_lock); + + *cs_new = cs; + +return 0 ; + +free_fence: +kfree (fence); +free_cs: +kfree (cs); +return rc; +} +
[ ... ]
Quoted 19 lines
drivers/misc/habanalabs/command_submission.c new file +19
-0+ +static int goya_validate_cb (struct hl_device *hdev, +struct hl_cs_parser *parser,bool is_mmu) +{ +u32 cb_parsed_length =0 ; +int rc =0 ; + + parser->patched_cb_size =0 ; + +/* cb_user_size is more than 0 so loop will always be executed */ +while ((cb_parsed_length < parser->user_cb_size) && (!rc)) { +enum packet_id pkt_id; +u16 pkt_size; +void *user_pkt; + + user_pkt = (void *) (parser->user_cb->kernel_address + + cb_parsed_length); + + pkt_id = (enum packet_id) (((*(u64 *) user_pkt) &
drivers/misc/habanalabs/command_submission.c
Quoted 10 lines
drivers/misc/habanalabs/command_submission.c new file +10
-0+ PACKET_HEADER_PACKET_ID_MASK) >> + PACKET_HEADER_PACKET_ID_SHIFT); + + pkt_size = goya_packet_sizes[pkt_id]; + cb_parsed_length += pkt_size; +if (cb_parsed_length > parser->user_cb_size) { +dev_err (hdev->dev, +"packet 0x%x is out of CB boundary\n" , pkt_id); + rc = -EINVAL ; +continue ;
For me !rc in the while statement was blind. Please consider break here and
if (!rc) break;
after the switch
Quoted 67 lines
drivers/misc/habanalabs/command_submission.c new file +67
-0+ } + +switch (pkt_id) { +case PACKET_WREG_32 : +/* +* Although it is validated after copy in patch_cb(), +* need to validate here as well because patch_cb() is +* not called in MMU path while this function is called +*/ + rc =goya_validate_wreg32 (hdev, parser, user_pkt); +break ; + +case PACKET_WREG_BULK : +dev_err (hdev->dev, +"User not allowed to use WREG_BULK\n" ); + rc = -EPERM ; +break ; + +case PACKET_MSG_PROT : +dev_err (hdev->dev, +"User not allowed to use MSG_PROT\n" ); + rc = -EPERM ; +break ; + +case PACKET_CP_DMA : +dev_err (hdev->dev,"User not allowed to use CP_DMA\n" ); + rc = -EPERM ; +break ; + +case PACKET_STOP : +dev_err (hdev->dev,"User not allowed to use STOP\n" ); + rc = -EPERM ; +break ; + +case PACKET_LIN_DMA : +if (is_mmu) + rc =goya_validate_dma_pkt_mmu (hdev, parser, + user_pkt); +else + rc =goya_validate_dma_pkt_no_mmu (hdev, parser, + user_pkt); +break ; + +case PACKET_MSG_LONG : +case PACKET_MSG_SHORT : +case PACKET_FENCE : +case PACKET_NOP : + parser->patched_cb_size += pkt_size; +break ; + +default : +dev_err (hdev->dev,"Invalid packet header 0x%x\n" , + pkt_id); + rc = -EINVAL ; +break ; + } + } + +/* +* The new CB should have space at the end for two MSG_PROT packets: +* 1. A packet that will act as a completion packet +* 2. A packet that will generate MSI-X interrupt +*/ + parser->patched_cb_size +=sizeof (struct packet_msg_prot ) *2 ; + +return rc; +}
[ ... ]
Quoted 20 lines
drivers/misc/habanalabs/command_submission.c new file +20
-0+static int goya_patch_cb (struct hl_device *hdev, +struct hl_cs_parser *parser) +{ +u32 cb_parsed_length =0 ; +u32 cb_patched_cur_length =0 ; +int rc =0 ; + +/* cb_user_size is more than 0 so loop will always be executed */ +while ((cb_parsed_length < parser->user_cb_size) && (!rc)) { +enum packet_id pkt_id; +u16 pkt_size; +u32 new_pkt_size =0 ; +void *user_pkt, *kernel_pkt; + + user_pkt = (void *) (parser->user_cb->kernel_address + + cb_parsed_length); + kernel_pkt = (void *) (parser->patched_cb->kernel_address + + cb_patched_cur_length); + + pkt_id = (enum packet_id) (((*(u64 *) user_pkt) &
drivers/misc/habanalabs/command_submission.c
Quoted 10 lines
drivers/misc/habanalabs/command_submission.c new file +10
-0+ PACKET_HEADER_PACKET_ID_MASK) >> + PACKET_HEADER_PACKET_ID_SHIFT); + + pkt_size = goya_packet_sizes[pkt_id]; + cb_parsed_length += pkt_size; +if (cb_parsed_length > parser->user_cb_size) { +dev_err (hdev->dev, +"packet 0x%x is out of CB boundary\n" , pkt_id); + rc = -EINVAL ; +continue ;
Ditto
Quoted 55 lines
drivers/misc/habanalabs/command_submission.c new file +55
-0+ } + +switch (pkt_id) { +case PACKET_LIN_DMA : + rc =goya_patch_dma_packet (hdev, parser, user_pkt, + kernel_pkt, &new_pkt_size); + cb_patched_cur_length += new_pkt_size; +break ; + +case PACKET_WREG_32 : +memcpy (kernel_pkt, user_pkt, pkt_size); + cb_patched_cur_length += pkt_size; + rc =goya_validate_wreg32 (hdev, parser, kernel_pkt); +break ; + +case PACKET_WREG_BULK : +dev_err (hdev->dev, +"User not allowed to use WREG_BULK\n" ); + rc = -EPERM ; +break ; + +case PACKET_MSG_PROT : +dev_err (hdev->dev, +"User not allowed to use MSG_PROT\n" ); + rc = -EPERM ; +break ; + +case PACKET_CP_DMA : +dev_err (hdev->dev,"User not allowed to use CP_DMA\n" ); + rc = -EPERM ; +break ; + +case PACKET_STOP : +dev_err (hdev->dev,"User not allowed to use STOP\n" ); + rc = -EPERM ; +break ; + +case PACKET_MSG_LONG : +case PACKET_MSG_SHORT : +case PACKET_FENCE : +case PACKET_NOP : +memcpy (kernel_pkt, user_pkt, pkt_size); + cb_patched_cur_length += pkt_size; +break ; + +default : +dev_err (hdev->dev,"Invalid packet header 0x%x\n" , + pkt_id); + rc = -EINVAL ; +break ; + } + } + +return rc; +}
[ ... ]
Quoted 35 lines
static void goya_get_axi_name(struct
hl_device*hdev, u32agent_id,drivers/misc/habanalabs/command_submission.c new file +28
-0u16 event_type, char *axi_name, int len) {@@ -4645,6 +5677,48 @@ static void goya_disable_clock_gating(struct hl_device *hdev) } +static bool goya_is_device_idle (struct hl_device *hdev) +{ +u64 offset, dma_qm_reg, tpc_qm_reg, tpc_cmdq_reg, tpc_cfg_reg; +bool val = true; +int i; + + offset = mmDMA_QM_1_GLBL_STS0 - mmDMA_QM_0_GLBL_STS0; + +for (i =0 ; i <DMA_MAX_NUM ; i++) { + dma_qm_reg = mmDMA_QM_0_GLBL_STS0 + i * offset; + + val = val && ((RREG32 (dma_qm_reg) &DMA_QM_IDLE_MASK ) == +DMA_QM_IDLE_MASK ); + } + + offset = mmTPC1_QM_GLBL_STS0 - mmTPC0_QM_GLBL_STS0; + +for (i =0 ; i <TPC_MAX_NUM ; i++) { + tpc_qm_reg = mmTPC0_QM_GLBL_STS0 + i * offset; + tpc_cmdq_reg = mmTPC0_CMDQ_GLBL_STS0 + i * offset; + tpc_cfg_reg = mmTPC0_CFG_STATUS + i * offset; + + val = val && ((RREG32 (tpc_qm_reg) &TPC_QM_IDLE_MASK ) == +TPC_QM_IDLE_MASK ); + val = val && ((RREG32 (tpc_cmdq_reg) &TPC_CMDQ_IDLE_MASK ) == +TPC_CMDQ_IDLE_MASK ); + val = val && ((RREG32 (tpc_cfg_reg) &TPC_CFG_IDLE_MASK ) == +TPC_CFG_IDLE_MASK );
drivers/misc/habanalabs/command_submission.c static void goya_disable_clock_gating(struct hl_device *hdev) · line 5717
Quoted 10 lines
drivers/misc/habanalabs/command_submission.c new file +10
-0+ } + + val = val && ((RREG32 (mmMME_QM_GLBL_STS0) &MME_QM_IDLE_MASK ) == +MME_QM_IDLE_MASK ); + val = val && ((RREG32 (mmMME_CMDQ_GLBL_STS0) &MME_CMDQ_IDLE_MASK ) == +MME_CMDQ_IDLE_MASK ); + val = val && ((RREG32 (mmMME_ARCH_STATUS) &MME_ARCH_IDLE_MASK ) == +MME_ARCH_IDLE_MASK ); + val = val && ((RREG32 (mmMME_SHADOW_0_STATUS) &MME_SHADOW_IDLE_MASK ) == +0 );
Huh, these are neat, but IMHO plain
if ((RREG(reg) & mask) != mask) return false;
are more readable...
Quoted 4 lines
drivers/misc/habanalabs/command_submission.c new file +4
-0+ +return val; +} +
[ ... ]
Signature · 2 lines
Sincerely yours, Mike.