habanalabs: fix endianness handling for packets from user
PATCH 1/2 habanalabs: fix endianness handling for packets from user
From: Ben Segal <[email protected]>
Packets that arrive from the user and need to be parsed by the driver are assumed to be in LE format.
This patch fix all the places where the code handles these packets and use the correct endianness macros to handle them, as the driver handles the packets in CPU format (LE or BE depending on the arch).
Signed-off-by Ben Segal <[email protected]>
Reviewed-by Oded Gabbay <[email protected]>
Signed-off-by Oded Gabbay <[email protected]>
--- drivers/misc/habanalabs/goya/goya.c | 32 +++++++++++-------- .../habanalabs/include/goya/goya_packets.h | 13 ++++++++ 2 files changed, 32 insertions(+), 13 deletions(-)
drivers/misc/habanalabs/goya/goya.c +19 -13
@@ -3428,12 +3428,13 @@ static int goya_validate_cb(struct hl_device *hdev,while (cb_parsed_length < parser->user_cb_size) {enum packet_id pkt_id;u16 pkt_size; -void *user_pkt; +struct goya_packet *user_pkt; - user_pkt = (void *) (uintptr_t ) + user_pkt = (struct goya_packet *) (uintptr_t ) (parser->user_cb->kernel_address + cb_parsed_length); - pkt_id = (enum packet_id ) (((*(u64 *) user_pkt) & + pkt_id = (enum packet_id ) ( + (le64_to_cpu (user_pkt->header) &PACKET_HEADER_PACKET_ID_MASK ) >>PACKET_HEADER_PACKET_ID_SHIFT );
@@ -3453,7 +3454,8 @@ static int goya_validate_cb(struct hl_device *hdev,* 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); + rc =goya_validate_wreg32 (hdev, + parser, (struct packet_wreg32 *) user_pkt);break ;case PACKET_WREG_BULK :
@@ -3481,10 +3483,10 @@ static int goya_validate_cb(struct hl_device *hdev,case PACKET_LIN_DMA :if (is_mmu) rc =goya_validate_dma_pkt_mmu (hdev, parser, - user_pkt); + (struct packet_lin_dma *) user_pkt);else rc =goya_validate_dma_pkt_no_mmu (hdev, parser, - user_pkt); + (struct packet_lin_dma *) user_pkt);break ;case PACKET_MSG_LONG :
@@ -3657,15 +3659,16 @@ static int goya_patch_cb(struct hl_device *hdev,enum packet_id pkt_id;u16 pkt_size;u32 new_pkt_size =0 ; -void *user_pkt, *kernel_pkt; +struct goya_packet *user_pkt, *kernel_pkt; - user_pkt = (void *) (uintptr_t ) + user_pkt = (struct goya_packet *) (uintptr_t ) (parser->user_cb->kernel_address + cb_parsed_length); - kernel_pkt = (void *) (uintptr_t ) + kernel_pkt = (struct goya_packet *) (uintptr_t ) (parser->patched_cb->kernel_address + cb_patched_cur_length); - pkt_id = (enum packet_id ) (((*(u64 *) user_pkt) & + pkt_id = (enum packet_id ) ( + (le64_to_cpu (user_pkt->header) &PACKET_HEADER_PACKET_ID_MASK ) >>PACKET_HEADER_PACKET_ID_SHIFT );
@@ -3680,15 +3683,18 @@ static int goya_patch_cb(struct hl_device *hdev,switch (pkt_id) {case PACKET_LIN_DMA : - rc =goya_patch_dma_packet (hdev, parser, user_pkt, - kernel_pkt, &new_pkt_size); + rc =goya_patch_dma_packet (hdev, parser, + (struct packet_lin_dma *) user_pkt, + (struct packet_lin_dma *) 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); + rc =goya_validate_wreg32 (hdev, parser, + (struct packet_wreg32 *) kernel_pkt);break ;case PACKET_WREG_BULK :
drivers/misc/habanalabs/include/goya/goya_packets.h +13 -0
@@ -52,6 +52,19 @@ enum goya_dma_direction {#define GOYA_PKT_CTL_MB_SHIFT 31#define GOYA_PKT_CTL_MB_MASK 0x80000000 +/* All packets have, at least, an 8-byte header, which contains +* the packet type. The kernel driver uses the packet header for packet +* validation and to perform any necessary required preparation before +* sending them off to the hardware. +*/ +struct goya_packet { +__le64 header; +/* The rest of the packet data follows. Use the corresponding +* packet_XXX struct to deference the data, based on packet type +*/ +u8 contents[0 ]; +}; +struct packet_nop {__le32 reserved;__le32 ctl;
Signature · 1 line
2.17.1
PATCH 2/2 habanalabs: fix completion queue handling when host is BE
From: Ben Segal <[email protected]>
This patch fix the CQ irq handler to work in hosts with BE architecture. It adds the correct endian-swapping macros around the relevant memory accesses.
Signed-off-by Ben Segal <[email protected]>
Reviewed-by Oded Gabbay <[email protected]>
Signed-off-by Oded Gabbay <[email protected]>
--- drivers/misc/habanalabs/irq.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)
drivers/misc/habanalabs/irq.c +13 -14
@@ -80,8 +80,7 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg)struct hl_cs_job *job;bool shadow_index_valid;u16 shadow_index; -u32 *cq_entry; -u32 *cq_base; +struct hl_cq_entry *cq_entry, *cq_base;if (hdev->disabled) { dev_dbg(hdev->dev,
@@ -90,29 +89,29 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg)return IRQ_HANDLED ; } - cq_base = (u32 *) (uintptr_t ) cq->kernel_address; + cq_base = (struct hl_cq_entry *) (uintptr_t ) cq->kernel_address;while (1 ) { -bool entry_ready = ((cq_base[cq->ci] &CQ_ENTRY_READY_MASK ) +bool entry_ready = ((le32_to_cpu (cq_base[cq->ci].data) & +CQ_ENTRY_READY_MASK ) >>CQ_ENTRY_READY_SHIFT );if (!entry_ready)break ; - cq_entry = (u32 *) &cq_base[cq->ci]; + cq_entry = (struct hl_cq_entry *) &cq_base[cq->ci]; -/* -* Make sure we read CQ entry contents after we've +/* Make sure we read CQ entry contents after we've * checked the ownership bit. */ dma_rmb (); - shadow_index_valid = - ((*cq_entry &CQ_ENTRY_SHADOW_INDEX_VALID_MASK ) + shadow_index_valid = ((le32_to_cpu (cq_entry->data) & +CQ_ENTRY_SHADOW_INDEX_VALID_MASK ) >>CQ_ENTRY_SHADOW_INDEX_VALID_SHIFT ); - shadow_index = (u16) - ((*cq_entry &CQ_ENTRY_SHADOW_INDEX_MASK ) + shadow_index = (u16) ((le32_to_cpu (cq_entry->data) & +CQ_ENTRY_SHADOW_INDEX_MASK ) >>CQ_ENTRY_SHADOW_INDEX_SHIFT ); queue = &hdev->kernel_queues[cq->hw_queue_id];
@@ -122,8 +121,7 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg)queue_work (hdev->cq_wq, &job->finish_work); } -/* -* Update ci of the context's queue. There is no +/* Update ci of the context's queue. There is no * need to protect it with spinlock because this update is * done only inside IRQ and there is a different IRQ per * queue
@@ -131,7 +129,8 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg) queue->ci =hl_queue_inc_ptr (queue->ci);/* Clear CQ entry ready bit */ - cq_base[cq->ci] &= ~CQ_ENTRY_READY_MASK ; + cq_entry->data =cpu_to_le32 (le32_to_cpu (cq_entry->data) & + ~CQ_ENTRY_READY_MASK ); cq->ci =hl_cq_inc_ptr (cq->ci);
Signature · 1 line
2.17.1