Skip to content

Commit 7ea3325

Browse files
committed
Merge tag 'misc-habanalabs-fixes-2019-08-12' of git://people.freedesktop.org/~gabbayo/linux into char-misc-next
Oded writes: This tag contains a couple of important fixes: - Four fixes when running on s390 architecture (BE). With these fixes, the driver is fully functional on Big-endian architectures. The fixes include: - Validation/Patching of user packets - Completion queue handling - Internal H/W queues submission - Device IRQ unmasking operation - Fix to double free in an error path to avoid kernel corruption - Fix to DRAM usage accounting when a user process is terminated forcefully. * tag 'misc-habanalabs-fixes-2019-08-12' of git://people.freedesktop.org/~gabbayo/linux: habanalabs: fix device IRQ unmasking for BE host habanalabs: fix endianness handling for internal QMAN submission habanalabs: fix completion queue handling when host is BE habanalabs: fix endianness handling for packets from user habanalabs: fix DRAM usage accounting on context tear down habanalabs: Avoid double free in error flow
2 parents d45331b + b421d83 commit 7ea3325

8 files changed

Lines changed: 90 additions & 54 deletions

File tree

‎drivers/misc/habanalabs/device.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,8 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
970970
rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
971971
if (rc) {
972972
dev_err(hdev->dev, "failed to initialize kernel context\n");
973-
goto free_ctx;
973+
kfree(hdev->kernel_ctx);
974+
goto mmu_fini;
974975
}
975976

976977
rc = hl_cb_pool_init(hdev);
@@ -1053,8 +1054,6 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
10531054
if (hl_ctx_put(hdev->kernel_ctx) != 1)
10541055
dev_err(hdev->dev,
10551056
"kernel ctx is still alive on initialization failure\n");
1056-
free_ctx:
1057-
kfree(hdev->kernel_ctx);
10581057
mmu_fini:
10591058
hl_mmu_fini(hdev);
10601059
eq_fini:

‎drivers/misc/habanalabs/goya/goya.c‎

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,9 +2729,10 @@ void goya_ring_doorbell(struct hl_device *hdev, u32 hw_queue_id, u32 pi)
27292729
GOYA_ASYNC_EVENT_ID_PI_UPDATE);
27302730
}
27312731

2732-
void goya_flush_pq_write(struct hl_device *hdev, u64 *pq, u64 exp_val)
2732+
void goya_pqe_write(struct hl_device *hdev, __le64 *pqe, struct hl_bd *bd)
27332733
{
2734-
/* Not needed in Goya */
2734+
/* The QMANs are on the SRAM so need to copy to IO space */
2735+
memcpy_toio((void __iomem *) pqe, bd, sizeof(struct hl_bd));
27352736
}
27362737

27372738
static void *goya_dma_alloc_coherent(struct hl_device *hdev, size_t size,
@@ -3313,9 +3314,11 @@ static int goya_validate_dma_pkt_no_mmu(struct hl_device *hdev,
33133314
int rc;
33143315

33153316
dev_dbg(hdev->dev, "DMA packet details:\n");
3316-
dev_dbg(hdev->dev, "source == 0x%llx\n", user_dma_pkt->src_addr);
3317-
dev_dbg(hdev->dev, "destination == 0x%llx\n", user_dma_pkt->dst_addr);
3318-
dev_dbg(hdev->dev, "size == %u\n", user_dma_pkt->tsize);
3317+
dev_dbg(hdev->dev, "source == 0x%llx\n",
3318+
le64_to_cpu(user_dma_pkt->src_addr));
3319+
dev_dbg(hdev->dev, "destination == 0x%llx\n",
3320+
le64_to_cpu(user_dma_pkt->dst_addr));
3321+
dev_dbg(hdev->dev, "size == %u\n", le32_to_cpu(user_dma_pkt->tsize));
33193322

33203323
ctl = le32_to_cpu(user_dma_pkt->ctl);
33213324
user_dir = (ctl & GOYA_PKT_LIN_DMA_CTL_DMA_DIR_MASK) >>
@@ -3344,9 +3347,11 @@ static int goya_validate_dma_pkt_mmu(struct hl_device *hdev,
33443347
struct packet_lin_dma *user_dma_pkt)
33453348
{
33463349
dev_dbg(hdev->dev, "DMA packet details:\n");
3347-
dev_dbg(hdev->dev, "source == 0x%llx\n", user_dma_pkt->src_addr);
3348-
dev_dbg(hdev->dev, "destination == 0x%llx\n", user_dma_pkt->dst_addr);
3349-
dev_dbg(hdev->dev, "size == %u\n", user_dma_pkt->tsize);
3350+
dev_dbg(hdev->dev, "source == 0x%llx\n",
3351+
le64_to_cpu(user_dma_pkt->src_addr));
3352+
dev_dbg(hdev->dev, "destination == 0x%llx\n",
3353+
le64_to_cpu(user_dma_pkt->dst_addr));
3354+
dev_dbg(hdev->dev, "size == %u\n", le32_to_cpu(user_dma_pkt->tsize));
33503355

33513356
/*
33523357
* WA for HW-23.
@@ -3386,7 +3391,8 @@ static int goya_validate_wreg32(struct hl_device *hdev,
33863391

33873392
dev_dbg(hdev->dev, "WREG32 packet details:\n");
33883393
dev_dbg(hdev->dev, "reg_offset == 0x%x\n", reg_offset);
3389-
dev_dbg(hdev->dev, "value == 0x%x\n", wreg_pkt->value);
3394+
dev_dbg(hdev->dev, "value == 0x%x\n",
3395+
le32_to_cpu(wreg_pkt->value));
33903396

33913397
if (reg_offset != (mmDMA_CH_0_WR_COMP_ADDR_LO & 0x1FFF)) {
33923398
dev_err(hdev->dev, "WREG32 packet with illegal address 0x%x\n",
@@ -3428,12 +3434,13 @@ static int goya_validate_cb(struct hl_device *hdev,
34283434
while (cb_parsed_length < parser->user_cb_size) {
34293435
enum packet_id pkt_id;
34303436
u16 pkt_size;
3431-
void *user_pkt;
3437+
struct goya_packet *user_pkt;
34323438

3433-
user_pkt = (void *) (uintptr_t)
3439+
user_pkt = (struct goya_packet *) (uintptr_t)
34343440
(parser->user_cb->kernel_address + cb_parsed_length);
34353441

3436-
pkt_id = (enum packet_id) (((*(u64 *) user_pkt) &
3442+
pkt_id = (enum packet_id) (
3443+
(le64_to_cpu(user_pkt->header) &
34373444
PACKET_HEADER_PACKET_ID_MASK) >>
34383445
PACKET_HEADER_PACKET_ID_SHIFT);
34393446

@@ -3453,7 +3460,8 @@ static int goya_validate_cb(struct hl_device *hdev,
34533460
* need to validate here as well because patch_cb() is
34543461
* not called in MMU path while this function is called
34553462
*/
3456-
rc = goya_validate_wreg32(hdev, parser, user_pkt);
3463+
rc = goya_validate_wreg32(hdev,
3464+
parser, (struct packet_wreg32 *) user_pkt);
34573465
break;
34583466

34593467
case PACKET_WREG_BULK:
@@ -3481,10 +3489,10 @@ static int goya_validate_cb(struct hl_device *hdev,
34813489
case PACKET_LIN_DMA:
34823490
if (is_mmu)
34833491
rc = goya_validate_dma_pkt_mmu(hdev, parser,
3484-
user_pkt);
3492+
(struct packet_lin_dma *) user_pkt);
34853493
else
34863494
rc = goya_validate_dma_pkt_no_mmu(hdev, parser,
3487-
user_pkt);
3495+
(struct packet_lin_dma *) user_pkt);
34883496
break;
34893497

34903498
case PACKET_MSG_LONG:
@@ -3657,15 +3665,16 @@ static int goya_patch_cb(struct hl_device *hdev,
36573665
enum packet_id pkt_id;
36583666
u16 pkt_size;
36593667
u32 new_pkt_size = 0;
3660-
void *user_pkt, *kernel_pkt;
3668+
struct goya_packet *user_pkt, *kernel_pkt;
36613669

3662-
user_pkt = (void *) (uintptr_t)
3670+
user_pkt = (struct goya_packet *) (uintptr_t)
36633671
(parser->user_cb->kernel_address + cb_parsed_length);
3664-
kernel_pkt = (void *) (uintptr_t)
3672+
kernel_pkt = (struct goya_packet *) (uintptr_t)
36653673
(parser->patched_cb->kernel_address +
36663674
cb_patched_cur_length);
36673675

3668-
pkt_id = (enum packet_id) (((*(u64 *) user_pkt) &
3676+
pkt_id = (enum packet_id) (
3677+
(le64_to_cpu(user_pkt->header) &
36693678
PACKET_HEADER_PACKET_ID_MASK) >>
36703679
PACKET_HEADER_PACKET_ID_SHIFT);
36713680

@@ -3680,15 +3689,18 @@ static int goya_patch_cb(struct hl_device *hdev,
36803689

36813690
switch (pkt_id) {
36823691
case PACKET_LIN_DMA:
3683-
rc = goya_patch_dma_packet(hdev, parser, user_pkt,
3684-
kernel_pkt, &new_pkt_size);
3692+
rc = goya_patch_dma_packet(hdev, parser,
3693+
(struct packet_lin_dma *) user_pkt,
3694+
(struct packet_lin_dma *) kernel_pkt,
3695+
&new_pkt_size);
36853696
cb_patched_cur_length += new_pkt_size;
36863697
break;
36873698

36883699
case PACKET_WREG_32:
36893700
memcpy(kernel_pkt, user_pkt, pkt_size);
36903701
cb_patched_cur_length += pkt_size;
3691-
rc = goya_validate_wreg32(hdev, parser, kernel_pkt);
3702+
rc = goya_validate_wreg32(hdev, parser,
3703+
(struct packet_wreg32 *) kernel_pkt);
36923704
break;
36933705

36943706
case PACKET_WREG_BULK:
@@ -4352,6 +4364,8 @@ static int goya_unmask_irq_arr(struct hl_device *hdev, u32 *irq_arr,
43524364
size_t total_pkt_size;
43534365
long result;
43544366
int rc;
4367+
int irq_num_entries, irq_arr_index;
4368+
__le32 *goya_irq_arr;
43554369

43564370
total_pkt_size = sizeof(struct armcp_unmask_irq_arr_packet) +
43574371
irq_arr_size;
@@ -4369,8 +4383,16 @@ static int goya_unmask_irq_arr(struct hl_device *hdev, u32 *irq_arr,
43694383
if (!pkt)
43704384
return -ENOMEM;
43714385

4372-
pkt->length = cpu_to_le32(irq_arr_size / sizeof(irq_arr[0]));
4373-
memcpy(&pkt->irqs, irq_arr, irq_arr_size);
4386+
irq_num_entries = irq_arr_size / sizeof(irq_arr[0]);
4387+
pkt->length = cpu_to_le32(irq_num_entries);
4388+
4389+
/* We must perform any necessary endianness conversation on the irq
4390+
* array being passed to the goya hardware
4391+
*/
4392+
for (irq_arr_index = 0, goya_irq_arr = (__le32 *) &pkt->irqs;
4393+
irq_arr_index < irq_num_entries ; irq_arr_index++)
4394+
goya_irq_arr[irq_arr_index] =
4395+
cpu_to_le32(irq_arr[irq_arr_index]);
43744396

43754397
pkt->armcp_pkt.ctl = cpu_to_le32(ARMCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY <<
43764398
ARMCP_PKT_CTL_OPCODE_SHIFT);
@@ -5042,7 +5064,7 @@ static const struct hl_asic_funcs goya_funcs = {
50425064
.resume = goya_resume,
50435065
.cb_mmap = goya_cb_mmap,
50445066
.ring_doorbell = goya_ring_doorbell,
5045-
.flush_pq_write = goya_flush_pq_write,
5067+
.pqe_write = goya_pqe_write,
50465068
.asic_dma_alloc_coherent = goya_dma_alloc_coherent,
50475069
.asic_dma_free_coherent = goya_dma_free_coherent,
50485070
.get_int_queue_base = goya_get_int_queue_base,

‎drivers/misc/habanalabs/goya/goyaP.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ int goya_late_init(struct hl_device *hdev);
177177
void goya_late_fini(struct hl_device *hdev);
178178

179179
void goya_ring_doorbell(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
180-
void goya_flush_pq_write(struct hl_device *hdev, u64 *pq, u64 exp_val);
180+
void goya_pqe_write(struct hl_device *hdev, __le64 *pqe, struct hl_bd *bd);
181181
void goya_update_eq_ci(struct hl_device *hdev, u32 val);
182182
void goya_restore_phase_topology(struct hl_device *hdev);
183183
int goya_context_switch(struct hl_device *hdev, u32 asid);

‎drivers/misc/habanalabs/habanalabs.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,11 @@ enum hl_pll_frequency {
441441
* @resume: handles IP specific H/W or SW changes for resume.
442442
* @cb_mmap: maps a CB.
443443
* @ring_doorbell: increment PI on a given QMAN.
444-
* @flush_pq_write: flush PQ entry write if necessary, WARN if flushing failed.
444+
* @pqe_write: Write the PQ entry to the PQ. This is ASIC-specific
445+
* function because the PQs are located in different memory areas
446+
* per ASIC (SRAM, DRAM, Host memory) and therefore, the method of
447+
* writing the PQE must match the destination memory area
448+
* properties.
445449
* @asic_dma_alloc_coherent: Allocate coherent DMA memory by calling
446450
* dma_alloc_coherent(). This is ASIC function because
447451
* its implementation is not trivial when the driver
@@ -510,7 +514,8 @@ struct hl_asic_funcs {
510514
int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
511515
u64 kaddress, phys_addr_t paddress, u32 size);
512516
void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
513-
void (*flush_pq_write)(struct hl_device *hdev, u64 *pq, u64 exp_val);
517+
void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
518+
struct hl_bd *bd);
514519
void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
515520
dma_addr_t *dma_handle, gfp_t flag);
516521
void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,

‎drivers/misc/habanalabs/hw_queue.c‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,23 +290,19 @@ static void int_hw_queue_schedule_job(struct hl_cs_job *job)
290290
struct hl_device *hdev = job->cs->ctx->hdev;
291291
struct hl_hw_queue *q = &hdev->kernel_queues[job->hw_queue_id];
292292
struct hl_bd bd;
293-
u64 *pi, *pbd = (u64 *) &bd;
293+
__le64 *pi;
294294

295295
bd.ctl = 0;
296-
bd.len = __cpu_to_le32(job->job_cb_size);
297-
bd.ptr = __cpu_to_le64((u64) (uintptr_t) job->user_cb);
296+
bd.len = cpu_to_le32(job->job_cb_size);
297+
bd.ptr = cpu_to_le64((u64) (uintptr_t) job->user_cb);
298298

299-
pi = (u64 *) (uintptr_t) (q->kernel_address +
299+
pi = (__le64 *) (uintptr_t) (q->kernel_address +
300300
((q->pi & (q->int_queue_len - 1)) * sizeof(bd)));
301301

302-
pi[0] = pbd[0];
303-
pi[1] = pbd[1];
304-
305302
q->pi++;
306303
q->pi &= ((q->int_queue_len << 1) - 1);
307304

308-
/* Flush PQ entry write. Relevant only for specific ASICs */
309-
hdev->asic_funcs->flush_pq_write(hdev, pi, pbd[0]);
305+
hdev->asic_funcs->pqe_write(hdev, pi, &bd);
310306

311307
hdev->asic_funcs->ring_doorbell(hdev, q->hw_queue_id, q->pi);
312308
}

‎drivers/misc/habanalabs/include/goya/goya_packets.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ enum goya_dma_direction {
5252
#define GOYA_PKT_CTL_MB_SHIFT 31
5353
#define GOYA_PKT_CTL_MB_MASK 0x80000000
5454

55+
/* All packets have, at least, an 8-byte header, which contains
56+
* the packet type. The kernel driver uses the packet header for packet
57+
* validation and to perform any necessary required preparation before
58+
* sending them off to the hardware.
59+
*/
60+
struct goya_packet {
61+
__le64 header;
62+
/* The rest of the packet data follows. Use the corresponding
63+
* packet_XXX struct to deference the data, based on packet type
64+
*/
65+
u8 contents[0];
66+
};
67+
5568
struct packet_nop {
5669
__le32 reserved;
5770
__le32 ctl;

‎drivers/misc/habanalabs/irq.c‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg)
8080
struct hl_cs_job *job;
8181
bool shadow_index_valid;
8282
u16 shadow_index;
83-
u32 *cq_entry;
84-
u32 *cq_base;
83+
struct hl_cq_entry *cq_entry, *cq_base;
8584

8685
if (hdev->disabled) {
8786
dev_dbg(hdev->dev,
@@ -90,29 +89,29 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg)
9089
return IRQ_HANDLED;
9190
}
9291

93-
cq_base = (u32 *) (uintptr_t) cq->kernel_address;
92+
cq_base = (struct hl_cq_entry *) (uintptr_t) cq->kernel_address;
9493

9594
while (1) {
96-
bool entry_ready = ((cq_base[cq->ci] & CQ_ENTRY_READY_MASK)
95+
bool entry_ready = ((le32_to_cpu(cq_base[cq->ci].data) &
96+
CQ_ENTRY_READY_MASK)
9797
>> CQ_ENTRY_READY_SHIFT);
9898

9999
if (!entry_ready)
100100
break;
101101

102-
cq_entry = (u32 *) &cq_base[cq->ci];
102+
cq_entry = (struct hl_cq_entry *) &cq_base[cq->ci];
103103

104-
/*
105-
* Make sure we read CQ entry contents after we've
104+
/* Make sure we read CQ entry contents after we've
106105
* checked the ownership bit.
107106
*/
108107
dma_rmb();
109108

110-
shadow_index_valid =
111-
((*cq_entry & CQ_ENTRY_SHADOW_INDEX_VALID_MASK)
109+
shadow_index_valid = ((le32_to_cpu(cq_entry->data) &
110+
CQ_ENTRY_SHADOW_INDEX_VALID_MASK)
112111
>> CQ_ENTRY_SHADOW_INDEX_VALID_SHIFT);
113112

114-
shadow_index = (u16)
115-
((*cq_entry & CQ_ENTRY_SHADOW_INDEX_MASK)
113+
shadow_index = (u16) ((le32_to_cpu(cq_entry->data) &
114+
CQ_ENTRY_SHADOW_INDEX_MASK)
116115
>> CQ_ENTRY_SHADOW_INDEX_SHIFT);
117116

118117
queue = &hdev->kernel_queues[cq->hw_queue_id];
@@ -122,16 +121,16 @@ irqreturn_t hl_irq_handler_cq(int irq, void *arg)
122121
queue_work(hdev->cq_wq, &job->finish_work);
123122
}
124123

125-
/*
126-
* Update ci of the context's queue. There is no
124+
/* Update ci of the context's queue. There is no
127125
* need to protect it with spinlock because this update is
128126
* done only inside IRQ and there is a different IRQ per
129127
* queue
130128
*/
131129
queue->ci = hl_queue_inc_ptr(queue->ci);
132130

133131
/* Clear CQ entry ready bit */
134-
cq_base[cq->ci] &= ~CQ_ENTRY_READY_MASK;
132+
cq_entry->data = cpu_to_le32(le32_to_cpu(cq_entry->data) &
133+
~CQ_ENTRY_READY_MASK);
135134

136135
cq->ci = hl_cq_inc_ptr(cq->ci);
137136

‎drivers/misc/habanalabs/memory.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,8 @@ void hl_vm_ctx_fini(struct hl_ctx *ctx)
16291629
dev_dbg(hdev->dev,
16301630
"page list 0x%p of asid %d is still alive\n",
16311631
phys_pg_list, ctx->asid);
1632+
atomic64_sub(phys_pg_list->total_size,
1633+
&hdev->dram_used_mem);
16321634
free_phys_pg_pack(hdev, phys_pg_list);
16331635
idr_remove(&vm->phys_pg_pack_handles, i);
16341636
}

0 commit comments

Comments
 (0)