Skip to content

Commit 3f1911e

Browse files
naushirpopcornmix
authored andcommitted
drivers: media: pci: Update Hailo accelerator device driver to v4.19
Sourced from https://github.com/hailo-ai/hailort-drivers/ Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
1 parent 6b2a7c0 commit 3f1911e

38 files changed

+1223
-888
lines changed

drivers/media/pci/hailo/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ obj-$(CONFIG_MEDIA_PCI_HAILO) := hailo_pci.o
88

99
hailo_pci-objs += src/pcie.o
1010
hailo_pci-objs += src/fops.o
11-
hailo_pci-objs += src/utils.o
1211
hailo_pci-objs += src/sysfs.o
13-
hailo_pci-objs += src/pci_soc_ioctl.o
12+
hailo_pci-objs += src/nnc.o
13+
hailo_pci-objs += src/soc.o
1414

1515
hailo_pci-objs += $(COMMON_SRC_DIRECTORY)/fw_validation.o
1616
hailo_pci-objs += $(COMMON_SRC_DIRECTORY)/fw_operation.o

drivers/media/pci/hailo/common/fw_operation.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
/**
3-
* Copyright (c) 2022 Hailo Technologies Ltd. All rights reserved.
4-
**/
3+
* Copyright (c) 2019-2024 Hailo Technologies Ltd. All rights reserved.
4+
**/
55

66
#include "fw_operation.h"
77

@@ -15,7 +15,10 @@ typedef struct {
1515
u32 chip_offset;
1616
} FW_DEBUG_BUFFER_HEADER_t;
1717

18-
#define DEBUG_BUFFER_DATA_SIZE (DEBUG_BUFFER_TOTAL_SIZE - sizeof(FW_DEBUG_BUFFER_HEADER_t))
18+
#define DEBUG_BUFFER_DATA_SIZE (DEBUG_BUFFER_TOTAL_SIZE - sizeof(FW_DEBUG_BUFFER_HEADER_t))
19+
#define PCIE_D2H_NOTIFICATION_SRAM_OFFSET (0x640 + 0x640)
20+
#define PCIE_APP_CPU_DEBUG_OFFSET (8*1024)
21+
#define PCIE_CORE_CPU_DEBUG_OFFSET (PCIE_APP_CPU_DEBUG_OFFSET + DEBUG_BUFFER_TOTAL_SIZE)
1922

2023
int hailo_read_firmware_notification(struct hailo_resource *resource, struct hailo_d2h_notification *notification)
2124
{
@@ -35,6 +38,21 @@ int hailo_read_firmware_notification(struct hailo_resource *resource, struct hai
3538
return 0;
3639
}
3740

41+
int hailo_pcie_read_firmware_notification(struct hailo_resource *resource,
42+
struct hailo_d2h_notification *notification)
43+
{
44+
struct hailo_resource notification_resource;
45+
46+
if (PCIE_D2H_NOTIFICATION_SRAM_OFFSET > resource->size) {
47+
return -EINVAL;
48+
}
49+
50+
notification_resource.address = resource->address + PCIE_D2H_NOTIFICATION_SRAM_OFFSET,
51+
notification_resource.size = sizeof(struct hailo_d2h_notification);
52+
53+
return hailo_read_firmware_notification(&notification_resource, notification);
54+
}
55+
3856
static inline size_t calculate_log_ready_to_read(FW_DEBUG_BUFFER_HEADER_t *header)
3957
{
4058
size_t ready_to_read = 0;
@@ -99,5 +117,31 @@ long hailo_read_firmware_log(struct hailo_resource *fw_logger_resource, struct h
99117
(u32)(read_offset - sizeof(debug_buffer_header)));
100118

101119
params->read_bytes = ready_to_read;
120+
return 0;
121+
}
122+
123+
long hailo_pcie_read_firmware_log(struct hailo_resource *resource, struct hailo_read_log_params *params)
124+
{
125+
long err = 0;
126+
struct hailo_resource log_resource = {resource->address, DEBUG_BUFFER_TOTAL_SIZE};
127+
128+
if (HAILO_CPU_ID_CPU0 == params->cpu_id) {
129+
log_resource.address += PCIE_APP_CPU_DEBUG_OFFSET;
130+
} else if (HAILO_CPU_ID_CPU1 == params->cpu_id) {
131+
log_resource.address += PCIE_CORE_CPU_DEBUG_OFFSET;
132+
} else {
133+
return -EINVAL;
134+
}
135+
136+
if (0 == params->buffer_size) {
137+
params->read_bytes = 0;
138+
return 0;
139+
}
140+
141+
err = hailo_read_firmware_log(&log_resource, params);
142+
if (0 != err) {
143+
return err;
144+
}
145+
102146
return 0;
103147
}

drivers/media/pci/hailo/common/fw_operation.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
/**
3-
* Copyright (c) 2022 Hailo Technologies Ltd. All rights reserved.
4-
**/
3+
* Copyright (c) 2019-2024 Hailo Technologies Ltd. All rights reserved.
4+
**/
55

66
#ifndef _HAILO_COMMON_FIRMWARE_OPERATION_H_
77
#define _HAILO_COMMON_FIRMWARE_OPERATION_H_
@@ -16,8 +16,12 @@ extern "C" {
1616

1717
int hailo_read_firmware_notification(struct hailo_resource *resource, struct hailo_d2h_notification *notification);
1818

19+
int hailo_pcie_read_firmware_notification(struct hailo_resource *resource, struct hailo_d2h_notification *notification);
20+
1921
long hailo_read_firmware_log(struct hailo_resource *fw_logger_resource, struct hailo_read_log_params *params);
2022

23+
long hailo_pcie_read_firmware_log(struct hailo_resource *resource, struct hailo_read_log_params *params);
24+
2125
#ifdef __cplusplus
2226
}
2327
#endif

drivers/media/pci/hailo/common/fw_validation.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
/**
3-
* Copyright (c) 2019-2022 Hailo Technologies Ltd. All rights reserved.
3+
* Copyright (c) 2019-2024 Hailo Technologies Ltd. All rights reserved.
44
**/
55

66
#include "fw_validation.h"
@@ -85,15 +85,15 @@ int FW_VALIDATION__validate_fw_header(uintptr_t firmware_base_address,
8585
}
8686

8787
int FW_VALIDATION__validate_cert_header(uintptr_t firmware_base_address,
88-
size_t firmware_size, u32 *outer_consumed_firmware_offset, secure_boot_certificate_t **out_firmware_cert)
88+
size_t firmware_size, u32 *outer_consumed_firmware_offset, secure_boot_certificate_header_t **out_firmware_cert)
8989
{
9090

91-
secure_boot_certificate_t *firmware_cert = NULL;
91+
secure_boot_certificate_header_t *firmware_cert = NULL;
9292
int err = -EINVAL;
9393
u32 consumed_firmware_offset = *outer_consumed_firmware_offset;
9494

95-
firmware_cert = (secure_boot_certificate_t *) (firmware_base_address + consumed_firmware_offset);
96-
CONSUME_FIRMWARE(sizeof(secure_boot_certificate_t), -EINVAL);
95+
firmware_cert = (secure_boot_certificate_header_t *) (firmware_base_address + consumed_firmware_offset);
96+
CONSUME_FIRMWARE(sizeof(secure_boot_certificate_header_t), -EINVAL);
9797

9898
if ((MAXIMUM_FIRMWARE_CERT_KEY_SIZE < firmware_cert->key_size) ||
9999
(MAXIMUM_FIRMWARE_CERT_CONTENT_SIZE < firmware_cert->content_size)) {

drivers/media/pci/hailo/common/fw_validation.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
/**
3-
* Copyright (c) 2019-2022 Hailo Technologies Ltd. All rights reserved.
3+
* Copyright (c) 2019-2024 Hailo Technologies Ltd. All rights reserved.
44
**/
55

66
#ifndef PCIE_COMMON_FIRMWARE_HEADER_UTILS_H_
@@ -44,8 +44,7 @@ typedef struct {
4444
typedef struct {
4545
u32 key_size;
4646
u32 content_size;
47-
u8 certificates_data[0];
48-
} secure_boot_certificate_t;
47+
} secure_boot_certificate_header_t;
4948

5049
#ifdef _MSC_VER
5150
#pragma warning(pop)
@@ -60,6 +59,6 @@ int FW_VALIDATION__validate_fw_header(uintptr_t firmware_base_address,
6059
firmware_header_t **out_firmware_header, enum hailo_board_type board_type);
6160

6261
int FW_VALIDATION__validate_cert_header(uintptr_t firmware_base_address,
63-
size_t firmware_size, u32 *outer_consumed_firmware_offset, secure_boot_certificate_t **out_firmware_cert);
62+
size_t firmware_size, u32 *outer_consumed_firmware_offset, secure_boot_certificate_header_t **out_firmware_cert);
6463

6564
#endif

drivers/media/pci/hailo/common/hailo_ioctl_common.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// SPDX-License-Identifier: (GPL-2.0 WITH Linux-syscall-note) AND MIT
22
/**
3-
* Copyright (c) 2019-2022 Hailo Technologies Ltd. All rights reserved.
3+
* Copyright (c) 2019-2024 Hailo Technologies Ltd. All rights reserved.
44
**/
55

66
#ifndef _HAILO_IOCTL_COMMON_H_
77
#define _HAILO_IOCTL_COMMON_H_
88

99
#define HAILO_DRV_VER_MAJOR 4
10-
#define HAILO_DRV_VER_MINOR 18
10+
#define HAILO_DRV_VER_MINOR 19
1111
#define HAILO_DRV_VER_REVISION 0
1212

1313
#define _STRINGIFY_EXPANDED( x ) #x
@@ -17,10 +17,11 @@
1717

1818
// This value is not easily changeable.
1919
// For example: the channel interrupts ioctls assume we have up to 32 channels
20-
#define MAX_VDMA_CHANNELS_PER_ENGINE (32)
21-
#define MAX_VDMA_ENGINES (3)
22-
#define SIZE_OF_VDMA_DESCRIPTOR (16)
23-
#define VDMA_DEST_CHANNELS_START (16)
20+
#define MAX_VDMA_CHANNELS_PER_ENGINE (32)
21+
#define VDMA_CHANNELS_PER_ENGINE_PER_DIRECTION (16)
22+
#define MAX_VDMA_ENGINES (3)
23+
#define SIZE_OF_VDMA_DESCRIPTOR (16)
24+
#define VDMA_DEST_CHANNELS_START (16)
2425

2526
#define HAILO_VDMA_MAX_ONGOING_TRANSFERS (128)
2627
#define HAILO_VDMA_MAX_ONGOING_TRANSFERS_MASK (HAILO_VDMA_MAX_ONGOING_TRANSFERS - 1)
@@ -37,8 +38,8 @@
3738
#define FW_ACCESS_APP_CPU_CONTROL_MASK (1 << FW_ACCESS_CONTROL_INTERRUPT_SHIFT)
3839
#define FW_ACCESS_DRIVER_SHUTDOWN_SHIFT (2)
3940
#define FW_ACCESS_DRIVER_SHUTDOWN_MASK (1 << FW_ACCESS_DRIVER_SHUTDOWN_SHIFT)
40-
#define FW_ACCESS_SOC_CONNECT_SHIFT (3)
41-
#define FW_ACCESS_SOC_CONNECT_MASK (1 << FW_ACCESS_SOC_CONNECT_SHIFT)
41+
#define FW_ACCESS_SOC_CONTROL_SHIFT (3)
42+
#define FW_ACCESS_SOC_CONTROL_MASK (1 << FW_ACCESS_SOC_CONTROL_SHIFT)
4243

4344
#define INVALID_VDMA_CHANNEL (0xff)
4445

@@ -245,6 +246,12 @@ struct hailo_desc_list_release_params {
245246
uintptr_t desc_handle; // in
246247
};
247248

249+
struct hailo_write_action_list_params {
250+
uint8_t *data; // in
251+
size_t size; // in
252+
uint64_t dma_address; // out
253+
};
254+
248255
/* structure used in ioctl HAILO_DESC_LIST_BIND_VDMA_BUFFER */
249256
struct hailo_desc_list_program_params {
250257
size_t buffer_handle; // in
@@ -508,6 +515,7 @@ struct hailo_vdma_launch_transfer_params {
508515

509516
/* structure used in ioctl HAILO_SOC_CONNECT */
510517
struct hailo_soc_connect_params {
518+
uint16_t port_number; // in
511519
uint8_t input_channel_index; // out
512520
uint8_t output_channel_index; // out
513521
uintptr_t input_desc_handle; // in
@@ -522,6 +530,7 @@ struct hailo_soc_close_params {
522530

523531
/* structure used in ioctl HAILO_PCI_EP_ACCEPT */
524532
struct hailo_pci_ep_accept_params {
533+
uint16_t port_number; // in
525534
uint8_t input_channel_index; // out
526535
uint8_t output_channel_index; // out
527536
uintptr_t input_desc_handle; // in
@@ -562,6 +571,7 @@ struct tCompatibleHailoIoctlData
562571
struct hailo_soc_close_params SocCloseParams;
563572
struct hailo_pci_ep_accept_params AcceptParams;
564573
struct hailo_pci_ep_close_params PciEpCloseParams;
574+
struct hailo_write_action_list_params WriteActionListParams;
565575
} Buffer;
566576
};
567577
#endif // _MSC_VER
@@ -632,6 +642,7 @@ enum hailo_nnc_ioctl_code {
632642
HAILO_DISABLE_NOTIFICATION_CODE,
633643
HAILO_READ_LOG_CODE,
634644
HAILO_RESET_NN_CORE_CODE,
645+
HAILO_WRITE_ACTION_LIST_CODE,
635646

636647
// Must be last
637648
HAILO_NNC_IOCTL_MAX_NR
@@ -642,6 +653,7 @@ enum hailo_nnc_ioctl_code {
642653
#define HAILO_DISABLE_NOTIFICATION _IO_(HAILO_NNC_IOCTL_MAGIC, HAILO_DISABLE_NOTIFICATION_CODE)
643654
#define HAILO_READ_LOG _IOWR_(HAILO_NNC_IOCTL_MAGIC, HAILO_READ_LOG_CODE, struct hailo_read_log_params)
644655
#define HAILO_RESET_NN_CORE _IO_(HAILO_NNC_IOCTL_MAGIC, HAILO_RESET_NN_CORE_CODE)
656+
#define HAILO_WRITE_ACTION_LIST _IOW_(HAILO_NNC_IOCTL_MAGIC, HAILO_WRITE_ACTION_LIST_CODE, struct hailo_write_action_list_params)
645657

646658
enum hailo_soc_ioctl_code {
647659
HAILO_SOC_IOCTL_CONNECT_CODE,

drivers/media/pci/hailo/common/hailo_resource.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
// SPDX-License-Identifier: MIT
22
/**
3-
* Copyright (c) 2019-2022 Hailo Technologies Ltd. All rights reserved.
3+
* Copyright (c) 2019-2024 Hailo Technologies Ltd. All rights reserved.
44
**/
55

66
#include "hailo_resource.h"
77

8+
#include "utils.h"
9+
810
#include <linux/io.h>
911
#include <linux/errno.h>
1012
#include <linux/types.h>
1113
#include <linux/kernel.h>
1214

15+
#define ALIGN_TO_32_BIT(addr) ((addr) & (~((uintptr_t)0x3)))
1316

1417
u8 hailo_resource_read8(struct hailo_resource *resource, size_t offset)
1518
{
16-
return ioread8((u8*)resource->address + offset);
19+
u32 val = ioread32((u8*)ALIGN_TO_32_BIT(resource->address + offset));
20+
u64 offset_in_bits = BITS_IN_BYTE * ((resource->address + offset) - ALIGN_TO_32_BIT(resource->address + offset));
21+
return (u8)READ_BITS_AT_OFFSET(BYTE_SIZE * BITS_IN_BYTE, offset_in_bits, val);
1722
}
1823

1924
u16 hailo_resource_read16(struct hailo_resource *resource, size_t offset)
2025
{
21-
return ioread16((u8*)resource->address + offset);
26+
u32 val = ioread32((u8*)ALIGN_TO_32_BIT(resource->address + offset));
27+
u64 offset_in_bits = BITS_IN_BYTE * ((resource->address + offset) - ALIGN_TO_32_BIT(resource->address + offset));
28+
return (u16)READ_BITS_AT_OFFSET(WORD_SIZE * BITS_IN_BYTE, offset_in_bits, val);
2229
}
2330

2431
u32 hailo_resource_read32(struct hailo_resource *resource, size_t offset)
@@ -28,12 +35,18 @@ u32 hailo_resource_read32(struct hailo_resource *resource, size_t offset)
2835

2936
void hailo_resource_write8(struct hailo_resource *resource, size_t offset, u8 value)
3037
{
31-
iowrite8(value, (u8*)resource->address + offset);
38+
u32 initial_val = ioread32((u8*)ALIGN_TO_32_BIT(resource->address + offset));
39+
u64 offset_in_bits = BITS_IN_BYTE * ((resource->address + offset) - ALIGN_TO_32_BIT(resource->address + offset));
40+
iowrite32(WRITE_BITS_AT_OFFSET(BYTE_SIZE * BITS_IN_BYTE, offset_in_bits, initial_val, value),
41+
(u8*)ALIGN_TO_32_BIT(resource->address + offset));
3242
}
3343

3444
void hailo_resource_write16(struct hailo_resource *resource, size_t offset, u16 value)
3545
{
36-
iowrite16(value, (u8*)resource->address + offset);
46+
u32 initial_val = ioread32((u8*)ALIGN_TO_32_BIT(resource->address + offset));
47+
u64 offset_in_bits = BITS_IN_BYTE * ((resource->address + offset) - ALIGN_TO_32_BIT(resource->address + offset));
48+
iowrite32(WRITE_BITS_AT_OFFSET(WORD_SIZE * BITS_IN_BYTE, offset_in_bits, initial_val, value),
49+
(u8*)ALIGN_TO_32_BIT(resource->address + offset));
3750
}
3851

3952
void hailo_resource_write32(struct hailo_resource *resource, size_t offset, u32 value)

drivers/media/pci/hailo/common/hailo_resource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
/**
3-
* Copyright (c) 2019-2022 Hailo Technologies Ltd. All rights reserved.
3+
* Copyright (c) 2019-2024 Hailo Technologies Ltd. All rights reserved.
44
**/
55

66
#ifndef _HAILO_COMMON_HAILO_RESOURCE_H_

0 commit comments

Comments
 (0)