Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Copyright (c) 2016-2018, 2020, The Linux Foundation. All rights reserved. |
| 4 | * Copyright (c) 2024, Qualcomm Innovation Center, Inc. All rights reserved. |
| 5 | */ |
| 6 | |
Caleb Connolly | 31edd02 | 2024-07-15 12:08:03 +0200 | [diff] [blame] | 7 | #define pr_fmt(fmt) "cmd-db: " fmt |
| 8 | |
| 9 | #include <dm.h> |
| 10 | #include <dm/ofnode.h> |
| 11 | #include <dm/device_compat.h> |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 13 | #include <linux/types.h> |
Caleb Connolly | 31edd02 | 2024-07-15 12:08:03 +0200 | [diff] [blame] | 14 | #include <linux/ioport.h> |
| 15 | #include <linux/byteorder/generic.h> |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 16 | |
| 17 | #include <soc/qcom/cmd-db.h> |
| 18 | |
| 19 | #define NUM_PRIORITY 2 |
| 20 | #define MAX_SLV_ID 8 |
| 21 | #define SLAVE_ID_MASK 0x7 |
| 22 | #define SLAVE_ID_SHIFT 16 |
| 23 | #define SLAVE_ID(addr) FIELD_GET(GENMASK(19, 16), addr) |
| 24 | #define VRM_ADDR(addr) FIELD_GET(GENMASK(19, 4), addr) |
| 25 | |
| 26 | /** |
| 27 | * struct entry_header: header for each entry in cmddb |
| 28 | * |
| 29 | * @id: resource's identifier |
| 30 | * @priority: unused |
| 31 | * @addr: the address of the resource |
| 32 | * @len: length of the data |
| 33 | * @offset: offset from :@data_offset, start of the data |
| 34 | */ |
| 35 | struct entry_header { |
| 36 | u8 id[8]; |
| 37 | __le32 priority[NUM_PRIORITY]; |
| 38 | __le32 addr; |
| 39 | __le16 len; |
| 40 | __le16 offset; |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * struct rsc_hdr: resource header information |
| 45 | * |
| 46 | * @slv_id: id for the resource |
| 47 | * @header_offset: entry's header at offset from the end of the cmd_db_header |
| 48 | * @data_offset: entry's data at offset from the end of the cmd_db_header |
| 49 | * @cnt: number of entries for HW type |
| 50 | * @version: MSB is major, LSB is minor |
| 51 | * @reserved: reserved for future use. |
| 52 | */ |
| 53 | struct rsc_hdr { |
| 54 | __le16 slv_id; |
| 55 | __le16 header_offset; |
| 56 | __le16 data_offset; |
| 57 | __le16 cnt; |
| 58 | __le16 version; |
| 59 | __le16 reserved[3]; |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * struct cmd_db_header: The DB header information |
| 64 | * |
| 65 | * @version: The cmd db version |
| 66 | * @magic: constant expected in the database |
| 67 | * @header: array of resources |
| 68 | * @checksum: checksum for the header. Unused. |
| 69 | * @reserved: reserved memory |
| 70 | * @data: driver specific data |
| 71 | */ |
| 72 | struct cmd_db_header { |
| 73 | __le32 version; |
| 74 | u8 magic[4]; |
| 75 | struct rsc_hdr header[MAX_SLV_ID]; |
| 76 | __le32 checksum; |
| 77 | __le32 reserved; |
| 78 | u8 data[]; |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * DOC: Description of the Command DB database. |
| 83 | * |
| 84 | * At the start of the command DB memory is the cmd_db_header structure. |
| 85 | * The cmd_db_header holds the version, checksum, magic key as well as an |
| 86 | * array for header for each slave (depicted by the rsc_header). Each h/w |
| 87 | * based accelerator is a 'slave' (shared resource) and has slave id indicating |
| 88 | * the type of accelerator. The rsc_header is the header for such individual |
| 89 | * slaves of a given type. The entries for each of these slaves begin at the |
| 90 | * rsc_hdr.header_offset. In addition each slave could have auxiliary data |
| 91 | * that may be needed by the driver. The data for the slave starts at the |
| 92 | * entry_header.offset to the location pointed to by the rsc_hdr.data_offset. |
| 93 | * |
| 94 | * Drivers have a stringified key to a slave/resource. They can query the slave |
| 95 | * information and get the slave id and the auxiliary data and the length of the |
| 96 | * data. Using this information, they can format the request to be sent to the |
| 97 | * h/w accelerator and request a resource state. |
| 98 | */ |
| 99 | |
| 100 | static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c }; |
| 101 | |
| 102 | static bool cmd_db_magic_matches(const struct cmd_db_header *header) |
| 103 | { |
| 104 | const u8 *magic = header->magic; |
| 105 | |
| 106 | return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0; |
| 107 | } |
| 108 | |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 109 | static struct cmd_db_header *cmd_db_header __section(".data") = NULL; |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 110 | |
| 111 | static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr) |
| 112 | { |
| 113 | u16 offset = le16_to_cpu(hdr->header_offset); |
| 114 | |
| 115 | return cmd_db_header->data + offset; |
| 116 | } |
| 117 | |
| 118 | static inline void * |
| 119 | rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent) |
| 120 | { |
| 121 | u16 offset = le16_to_cpu(hdr->data_offset); |
| 122 | u16 loffset = le16_to_cpu(ent->offset); |
| 123 | |
| 124 | return cmd_db_header->data + offset + loffset; |
| 125 | } |
| 126 | |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 127 | static int cmd_db_get_header(const char *id, const struct entry_header **eh, |
| 128 | const struct rsc_hdr **rh) |
| 129 | { |
| 130 | const struct rsc_hdr *rsc_hdr; |
| 131 | const struct entry_header *ent; |
Caleb Connolly | b005a24 | 2024-07-15 12:08:06 +0200 | [diff] [blame] | 132 | int i, j; |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 133 | u8 query[sizeof(ent->id)] __nonstring; |
| 134 | |
Caleb Connolly | b005a24 | 2024-07-15 12:08:06 +0200 | [diff] [blame] | 135 | strncpy(query, id, sizeof(query)); |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 136 | |
| 137 | for (i = 0; i < MAX_SLV_ID; i++) { |
| 138 | rsc_hdr = &cmd_db_header->header[i]; |
| 139 | if (!rsc_hdr->slv_id) |
| 140 | break; |
| 141 | |
| 142 | ent = rsc_to_entry_header(rsc_hdr); |
| 143 | for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) { |
| 144 | if (memcmp(ent->id, query, sizeof(ent->id)) == 0) { |
| 145 | if (eh) |
| 146 | *eh = ent; |
| 147 | if (rh) |
| 148 | *rh = rsc_hdr; |
| 149 | return 0; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return -ENODEV; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * cmd_db_read_addr() - Query command db for resource id address. |
| 159 | * |
| 160 | * @id: resource id to query for address |
| 161 | * |
| 162 | * Return: resource address on success, 0 on error |
| 163 | * |
| 164 | * This is used to retrieve resource address based on resource |
| 165 | * id. |
| 166 | */ |
| 167 | u32 cmd_db_read_addr(const char *id) |
| 168 | { |
| 169 | int ret; |
| 170 | const struct entry_header *ent; |
| 171 | |
Caleb Connolly | b005a24 | 2024-07-15 12:08:06 +0200 | [diff] [blame] | 172 | debug("%s(%s)\n", __func__, id); |
| 173 | |
| 174 | if (!cmd_db_header) { |
| 175 | log_err("%s: Command DB not initialized\n", __func__); |
| 176 | return 0; |
| 177 | } |
| 178 | |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 179 | ret = cmd_db_get_header(id, &ent, NULL); |
| 180 | |
| 181 | return ret < 0 ? 0 : le32_to_cpu(ent->addr); |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(cmd_db_read_addr); |
| 184 | |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 185 | int cmd_db_bind(struct udevice *dev) |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 186 | { |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 187 | void __iomem *base; |
| 188 | ofnode node; |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 189 | |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 190 | if (cmd_db_header) |
| 191 | return 0; |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 192 | |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 193 | node = dev_ofnode(dev); |
| 194 | |
| 195 | debug("%s(%s)\n", __func__, ofnode_get_name(node)); |
| 196 | |
| 197 | base = (void __iomem *)ofnode_get_addr(node); |
| 198 | if ((fdt_addr_t)base == FDT_ADDR_T_NONE) { |
| 199 | log_err("%s: Failed to read base address\n", __func__); |
| 200 | return -ENOENT; |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 203 | cmd_db_header = base; |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 204 | if (!cmd_db_magic_matches(cmd_db_header)) { |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 205 | log_err("%s: Invalid Command DB Magic\n", __func__); |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 206 | return -EINVAL; |
| 207 | } |
| 208 | |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 212 | static const struct udevice_id cmd_db_ids[] = { |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 213 | { .compatible = "qcom,cmd-db" }, |
| 214 | { } |
| 215 | }; |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 216 | |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 217 | U_BOOT_DRIVER(qcom_cmd_db) = { |
| 218 | .name = "qcom_cmd_db", |
| 219 | .id = UCLASS_MISC, |
Caleb Connolly | b005a24 | 2024-07-15 12:08:06 +0200 | [diff] [blame] | 220 | .bind = cmd_db_bind, |
Caleb Connolly | f8bd3bb | 2024-07-15 12:08:05 +0200 | [diff] [blame] | 221 | .of_match = cmd_db_ids, |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 222 | }; |
| 223 | |
Caleb Connolly | 90ff581 | 2024-07-15 12:08:02 +0200 | [diff] [blame] | 224 | MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Command DB Driver"); |
| 225 | MODULE_LICENSE("GPL v2"); |