1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. */
4 #include <linux/debugfs.h>
5 #include <linux/kernel.h>
7 #include <linux/of_address.h>
8 #include <linux/of_reserved_mem.h>
9 #include <linux/platform_device.h>
10 #include <linux/seq_file.h>
11 #include <linux/types.h>
13 #include <soc/qcom/cmd-db.h>
15 #define NUM_PRIORITY 2
17 #define SLAVE_ID_MASK 0x7
18 #define SLAVE_ID_SHIFT 16
21 * struct entry_header: header for each entry in cmddb
23 * @id: resource's identifier
25 * @addr: the address of the resource
26 * @len: length of the data
27 * @offset: offset from :@data_offset, start of the data
31 __le32 priority[NUM_PRIORITY];
38 * struct rsc_hdr: resource header information
40 * @slv_id: id for the resource
41 * @header_offset: entry's header at offset from the end of the cmd_db_header
42 * @data_offset: entry's data at offset from the end of the cmd_db_header
43 * @cnt: number of entries for HW type
44 * @version: MSB is major, LSB is minor
45 * @reserved: reserved for future use.
57 * struct cmd_db_header: The DB header information
59 * @version: The cmd db version
60 * @magic: constant expected in the database
61 * @header: array of resources
62 * @checksum: checksum for the header. Unused.
63 * @reserved: reserved memory
64 * @data: driver specific data
66 struct cmd_db_header {
69 struct rsc_hdr header[MAX_SLV_ID];
76 * DOC: Description of the Command DB database.
78 * At the start of the command DB memory is the cmd_db_header structure.
79 * The cmd_db_header holds the version, checksum, magic key as well as an
80 * array for header for each slave (depicted by the rsc_header). Each h/w
81 * based accelerator is a 'slave' (shared resource) and has slave id indicating
82 * the type of accelerator. The rsc_header is the header for such individual
83 * slaves of a given type. The entries for each of these slaves begin at the
84 * rsc_hdr.header_offset. In addition each slave could have auxiliary data
85 * that may be needed by the driver. The data for the slave starts at the
86 * entry_header.offset to the location pointed to by the rsc_hdr.data_offset.
88 * Drivers have a stringified key to a slave/resource. They can query the slave
89 * information and get the slave id and the auxiliary data and the length of the
90 * data. Using this information, they can format the request to be sent to the
91 * h/w accelerator and request a resource state.
94 static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c };
96 static bool cmd_db_magic_matches(const struct cmd_db_header *header)
98 const u8 *magic = header->magic;
100 return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0;
103 static struct cmd_db_header *cmd_db_header;
105 static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr)
107 u16 offset = le16_to_cpu(hdr->header_offset);
109 return cmd_db_header->data + offset;
113 rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent)
115 u16 offset = le16_to_cpu(hdr->data_offset);
116 u16 loffset = le16_to_cpu(ent->offset);
118 return cmd_db_header->data + offset + loffset;
122 * cmd_db_ready - Indicates if command DB is available
124 * Return: 0 on success, errno otherwise
126 int cmd_db_ready(void)
128 if (cmd_db_header == NULL)
129 return -EPROBE_DEFER;
130 else if (!cmd_db_magic_matches(cmd_db_header))
135 EXPORT_SYMBOL(cmd_db_ready);
137 static int cmd_db_get_header(const char *id, const struct entry_header **eh,
138 const struct rsc_hdr **rh)
140 const struct rsc_hdr *rsc_hdr;
141 const struct entry_header *ent;
145 ret = cmd_db_ready();
149 /* Pad out query string to same length as in DB */
150 strncpy(query, id, sizeof(query));
152 for (i = 0; i < MAX_SLV_ID; i++) {
153 rsc_hdr = &cmd_db_header->header[i];
154 if (!rsc_hdr->slv_id)
157 ent = rsc_to_entry_header(rsc_hdr);
158 for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) {
159 if (memcmp(ent->id, query, sizeof(ent->id)) == 0) {
173 * cmd_db_read_addr() - Query command db for resource id address.
175 * @id: resource id to query for address
177 * Return: resource address on success, 0 on error
179 * This is used to retrieve resource address based on resource
182 u32 cmd_db_read_addr(const char *id)
185 const struct entry_header *ent;
187 ret = cmd_db_get_header(id, &ent, NULL);
189 return ret < 0 ? 0 : le32_to_cpu(ent->addr);
191 EXPORT_SYMBOL(cmd_db_read_addr);
194 * cmd_db_read_aux_data() - Query command db for aux data.
196 * @id: Resource to retrieve AUX Data on
197 * @len: size of data buffer returned
199 * Return: pointer to data on success, error pointer otherwise
201 const void *cmd_db_read_aux_data(const char *id, size_t *len)
204 const struct entry_header *ent;
205 const struct rsc_hdr *rsc_hdr;
207 ret = cmd_db_get_header(id, &ent, &rsc_hdr);
212 *len = le16_to_cpu(ent->len);
214 return rsc_offset(rsc_hdr, ent);
216 EXPORT_SYMBOL(cmd_db_read_aux_data);
219 * cmd_db_read_slave_id - Get the slave ID for a given resource address
221 * @id: Resource id to query the DB for version
223 * Return: cmd_db_hw_type enum on success, CMD_DB_HW_INVALID on error
225 enum cmd_db_hw_type cmd_db_read_slave_id(const char *id)
228 const struct entry_header *ent;
231 ret = cmd_db_get_header(id, &ent, NULL);
233 return CMD_DB_HW_INVALID;
235 addr = le32_to_cpu(ent->addr);
236 return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
238 EXPORT_SYMBOL(cmd_db_read_slave_id);
240 #ifdef CONFIG_DEBUG_FS
241 static int cmd_db_debugfs_dump(struct seq_file *seq, void *p)
244 const struct rsc_hdr *rsc;
245 const struct entry_header *ent;
250 seq_puts(seq, "Command DB DUMP\n");
252 for (i = 0; i < MAX_SLV_ID; i++) {
253 rsc = &cmd_db_header->header[i];
257 switch (le16_to_cpu(rsc->slv_id)) {
272 version = le16_to_cpu(rsc->version);
273 major = version >> 8;
276 seq_printf(seq, "Slave %s (v%u.%u)\n", name, major, minor);
277 seq_puts(seq, "-------------------------\n");
279 ent = rsc_to_entry_header(rsc);
280 for (j = 0; j < le16_to_cpu(rsc->cnt); j++, ent++) {
281 seq_printf(seq, "0x%05x: %*pEp", le32_to_cpu(ent->addr),
282 (int)sizeof(ent->id), ent->id);
284 len = le16_to_cpu(ent->len);
286 seq_printf(seq, " [%*ph]",
287 len, rsc_offset(rsc, ent));
296 static int open_cmd_db_debugfs(struct inode *inode, struct file *file)
298 return single_open(file, cmd_db_debugfs_dump, inode->i_private);
302 static const struct file_operations cmd_db_debugfs_ops = {
303 #ifdef CONFIG_DEBUG_FS
304 .open = open_cmd_db_debugfs,
308 .release = single_release,
311 static int cmd_db_dev_probe(struct platform_device *pdev)
313 struct reserved_mem *rmem;
316 rmem = of_reserved_mem_lookup(pdev->dev.of_node);
318 dev_err(&pdev->dev, "failed to acquire memory region\n");
322 cmd_db_header = memremap(rmem->base, rmem->size, MEMREMAP_WB);
323 if (!cmd_db_header) {
325 cmd_db_header = NULL;
329 if (!cmd_db_magic_matches(cmd_db_header)) {
330 dev_err(&pdev->dev, "Invalid Command DB Magic\n");
334 debugfs_create_file("cmd-db", 0400, NULL, NULL, &cmd_db_debugfs_ops);
339 static const struct of_device_id cmd_db_match_table[] = {
340 { .compatible = "qcom,cmd-db" },
344 static struct platform_driver cmd_db_dev_driver = {
345 .probe = cmd_db_dev_probe,
348 .of_match_table = cmd_db_match_table,
352 static int __init cmd_db_device_init(void)
354 return platform_driver_register(&cmd_db_dev_driver);
356 arch_initcall(cmd_db_device_init);