1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2016-2018, 2020, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2024, Qualcomm Innovation Center, Inc. All rights reserved.
7 #include <linux/bitfield.h>
8 #include <linux/debugfs.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_reserved_mem.h>
14 #include <linux/platform_device.h>
15 #include <linux/seq_file.h>
16 #include <linux/types.h>
18 #include <soc/qcom/cmd-db.h>
20 #define NUM_PRIORITY 2
22 #define SLAVE_ID_MASK 0x7
23 #define SLAVE_ID_SHIFT 16
24 #define SLAVE_ID(addr) FIELD_GET(GENMASK(19, 16), addr)
25 #define VRM_ADDR(addr) FIELD_GET(GENMASK(19, 4), addr)
28 * struct entry_header: header for each entry in cmddb
30 * @id: resource's identifier
32 * @addr: the address of the resource
33 * @len: length of the data
34 * @offset: offset from :@data_offset, start of the data
38 __le32 priority[NUM_PRIORITY];
45 * struct rsc_hdr: resource header information
47 * @slv_id: id for the resource
48 * @header_offset: entry's header at offset from the end of the cmd_db_header
49 * @data_offset: entry's data at offset from the end of the cmd_db_header
50 * @cnt: number of entries for HW type
51 * @version: MSB is major, LSB is minor
52 * @reserved: reserved for future use.
64 * struct cmd_db_header: The DB header information
66 * @version: The cmd db version
67 * @magic: constant expected in the database
68 * @header: array of resources
69 * @checksum: checksum for the header. Unused.
70 * @reserved: reserved memory
71 * @data: driver specific data
73 struct cmd_db_header {
76 struct rsc_hdr header[MAX_SLV_ID];
83 * DOC: Description of the Command DB database.
85 * At the start of the command DB memory is the cmd_db_header structure.
86 * The cmd_db_header holds the version, checksum, magic key as well as an
87 * array for header for each slave (depicted by the rsc_header). Each h/w
88 * based accelerator is a 'slave' (shared resource) and has slave id indicating
89 * the type of accelerator. The rsc_header is the header for such individual
90 * slaves of a given type. The entries for each of these slaves begin at the
91 * rsc_hdr.header_offset. In addition each slave could have auxiliary data
92 * that may be needed by the driver. The data for the slave starts at the
93 * entry_header.offset to the location pointed to by the rsc_hdr.data_offset.
95 * Drivers have a stringified key to a slave/resource. They can query the slave
96 * information and get the slave id and the auxiliary data and the length of the
97 * data. Using this information, they can format the request to be sent to the
98 * h/w accelerator and request a resource state.
101 static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c };
103 static bool cmd_db_magic_matches(const struct cmd_db_header *header)
105 const u8 *magic = header->magic;
107 return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0;
110 static struct cmd_db_header *cmd_db_header;
112 static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr)
114 u16 offset = le16_to_cpu(hdr->header_offset);
116 return cmd_db_header->data + offset;
120 rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent)
122 u16 offset = le16_to_cpu(hdr->data_offset);
123 u16 loffset = le16_to_cpu(ent->offset);
125 return cmd_db_header->data + offset + loffset;
129 * cmd_db_ready - Indicates if command DB is available
131 * Return: 0 on success, errno otherwise
133 int cmd_db_ready(void)
135 if (cmd_db_header == NULL)
136 return -EPROBE_DEFER;
137 else if (!cmd_db_magic_matches(cmd_db_header))
142 EXPORT_SYMBOL_GPL(cmd_db_ready);
144 static int cmd_db_get_header(const char *id, const struct entry_header **eh,
145 const struct rsc_hdr **rh)
147 const struct rsc_hdr *rsc_hdr;
148 const struct entry_header *ent;
150 u8 query[sizeof(ent->id)] __nonstring;
152 ret = cmd_db_ready();
156 strtomem_pad(query, id, 0);
158 for (i = 0; i < MAX_SLV_ID; i++) {
159 rsc_hdr = &cmd_db_header->header[i];
160 if (!rsc_hdr->slv_id)
163 ent = rsc_to_entry_header(rsc_hdr);
164 for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) {
165 if (memcmp(ent->id, query, sizeof(ent->id)) == 0) {
179 * cmd_db_read_addr() - Query command db for resource id address.
181 * @id: resource id to query for address
183 * Return: resource address on success, 0 on error
185 * This is used to retrieve resource address based on resource
188 u32 cmd_db_read_addr(const char *id)
191 const struct entry_header *ent;
193 ret = cmd_db_get_header(id, &ent, NULL);
195 return ret < 0 ? 0 : le32_to_cpu(ent->addr);
197 EXPORT_SYMBOL_GPL(cmd_db_read_addr);
200 * cmd_db_read_aux_data() - Query command db for aux data.
202 * @id: Resource to retrieve AUX Data on
203 * @len: size of data buffer returned
205 * Return: pointer to data on success, error pointer otherwise
207 const void *cmd_db_read_aux_data(const char *id, size_t *len)
210 const struct entry_header *ent;
211 const struct rsc_hdr *rsc_hdr;
213 ret = cmd_db_get_header(id, &ent, &rsc_hdr);
218 *len = le16_to_cpu(ent->len);
220 return rsc_offset(rsc_hdr, ent);
222 EXPORT_SYMBOL_GPL(cmd_db_read_aux_data);
225 * cmd_db_match_resource_addr() - Compare if both Resource addresses are same
227 * @addr1: Resource address to compare
228 * @addr2: Resource address to compare
230 * Return: true if two addresses refer to the same resource, false otherwise
232 bool cmd_db_match_resource_addr(u32 addr1, u32 addr2)
235 * Each RPMh VRM accelerator resource has 3 or 4 contiguous 4-byte
236 * aligned addresses associated with it. Ignore the offset to check
241 else if (SLAVE_ID(addr1) == CMD_DB_HW_VRM && VRM_ADDR(addr1) == VRM_ADDR(addr2))
246 EXPORT_SYMBOL_GPL(cmd_db_match_resource_addr);
249 * cmd_db_read_slave_id - Get the slave ID for a given resource address
251 * @id: Resource id to query the DB for version
253 * Return: cmd_db_hw_type enum on success, CMD_DB_HW_INVALID on error
255 enum cmd_db_hw_type cmd_db_read_slave_id(const char *id)
258 const struct entry_header *ent;
261 ret = cmd_db_get_header(id, &ent, NULL);
263 return CMD_DB_HW_INVALID;
265 addr = le32_to_cpu(ent->addr);
266 return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
268 EXPORT_SYMBOL_GPL(cmd_db_read_slave_id);
270 #ifdef CONFIG_DEBUG_FS
271 static int cmd_db_debugfs_dump(struct seq_file *seq, void *p)
274 const struct rsc_hdr *rsc;
275 const struct entry_header *ent;
280 seq_puts(seq, "Command DB DUMP\n");
282 for (i = 0; i < MAX_SLV_ID; i++) {
283 rsc = &cmd_db_header->header[i];
287 switch (le16_to_cpu(rsc->slv_id)) {
302 version = le16_to_cpu(rsc->version);
303 major = version >> 8;
306 seq_printf(seq, "Slave %s (v%u.%u)\n", name, major, minor);
307 seq_puts(seq, "-------------------------\n");
309 ent = rsc_to_entry_header(rsc);
310 for (j = 0; j < le16_to_cpu(rsc->cnt); j++, ent++) {
311 seq_printf(seq, "0x%05x: %*pEp", le32_to_cpu(ent->addr),
312 (int)strnlen(ent->id, sizeof(ent->id)), ent->id);
314 len = le16_to_cpu(ent->len);
316 seq_printf(seq, " [%*ph]",
317 len, rsc_offset(rsc, ent));
326 static int open_cmd_db_debugfs(struct inode *inode, struct file *file)
328 return single_open(file, cmd_db_debugfs_dump, inode->i_private);
332 static const struct file_operations cmd_db_debugfs_ops = {
333 #ifdef CONFIG_DEBUG_FS
334 .open = open_cmd_db_debugfs,
338 .release = single_release,
341 static int cmd_db_dev_probe(struct platform_device *pdev)
343 struct reserved_mem *rmem;
346 rmem = of_reserved_mem_lookup(pdev->dev.of_node);
348 dev_err(&pdev->dev, "failed to acquire memory region\n");
352 cmd_db_header = memremap(rmem->base, rmem->size, MEMREMAP_WC);
353 if (!cmd_db_header) {
355 cmd_db_header = NULL;
359 if (!cmd_db_magic_matches(cmd_db_header)) {
360 dev_err(&pdev->dev, "Invalid Command DB Magic\n");
364 debugfs_create_file("cmd-db", 0400, NULL, NULL, &cmd_db_debugfs_ops);
366 device_set_pm_not_required(&pdev->dev);
371 static const struct of_device_id cmd_db_match_table[] = {
372 { .compatible = "qcom,cmd-db" },
375 MODULE_DEVICE_TABLE(of, cmd_db_match_table);
377 static struct platform_driver cmd_db_dev_driver = {
378 .probe = cmd_db_dev_probe,
381 .of_match_table = cmd_db_match_table,
382 .suppress_bind_attrs = true,
386 static int __init cmd_db_device_init(void)
388 return platform_driver_register(&cmd_db_dev_driver);
390 core_initcall(cmd_db_device_init);
392 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Command DB Driver");
393 MODULE_LICENSE("GPL v2");