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
7 #include <linux/bitfield.h>
8 #include <linux/debugfs.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/of.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>
17
18 #include <soc/qcom/cmd-db.h>
19
20 #define NUM_PRIORITY 2
21 #define MAX_SLV_ID 8
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)
26
27 /**
28 * struct entry_header: header for each entry in cmddb
29 *
30 * @id: resource's identifier
31 * @priority: unused
32 * @addr: the address of the resource
33 * @len: length of the data
34 * @offset: offset from :@data_offset, start of the data
35 */
36 struct entry_header {
37 u8 id[8];
38 __le32 priority[NUM_PRIORITY];
39 __le32 addr;
40 __le16 len;
41 __le16 offset;
42 };
43
44 /**
45 * struct rsc_hdr: resource header information
46 *
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.
53 */
54 struct rsc_hdr {
55 __le16 slv_id;
56 __le16 header_offset;
57 __le16 data_offset;
58 __le16 cnt;
59 __le16 version;
60 __le16 reserved[3];
61 };
62
63 /**
64 * struct cmd_db_header: The DB header information
65 *
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
72 */
73 struct cmd_db_header {
74 __le32 version;
75 u8 magic[4];
76 struct rsc_hdr header[MAX_SLV_ID];
77 __le32 checksum;
78 __le32 reserved;
79 u8 data[];
80 };
81
82 /**
83 * DOC: Description of the Command DB database.
84 *
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.
94 *
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.
99 */
100
101 static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c };
102
cmd_db_magic_matches(const struct cmd_db_header * header)103 static bool cmd_db_magic_matches(const struct cmd_db_header *header)
104 {
105 const u8 *magic = header->magic;
106
107 return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0;
108 }
109
110 static struct cmd_db_header *cmd_db_header;
111
rsc_to_entry_header(const struct rsc_hdr * hdr)112 static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr)
113 {
114 u16 offset = le16_to_cpu(hdr->header_offset);
115
116 return cmd_db_header->data + offset;
117 }
118
119 static inline void *
rsc_offset(const struct rsc_hdr * hdr,const struct entry_header * ent)120 rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent)
121 {
122 u16 offset = le16_to_cpu(hdr->data_offset);
123 u16 loffset = le16_to_cpu(ent->offset);
124
125 return cmd_db_header->data + offset + loffset;
126 }
127
128 /**
129 * cmd_db_ready - Indicates if command DB is available
130 *
131 * Return: 0 on success, errno otherwise
132 */
cmd_db_ready(void)133 int cmd_db_ready(void)
134 {
135 if (cmd_db_header == NULL)
136 return -EPROBE_DEFER;
137 else if (!cmd_db_magic_matches(cmd_db_header))
138 return -EINVAL;
139
140 return 0;
141 }
142 EXPORT_SYMBOL(cmd_db_ready);
143
cmd_db_get_header(const char * id,const struct entry_header ** eh,const struct rsc_hdr ** rh)144 static int cmd_db_get_header(const char *id, const struct entry_header **eh,
145 const struct rsc_hdr **rh)
146 {
147 const struct rsc_hdr *rsc_hdr;
148 const struct entry_header *ent;
149 int ret, i, j;
150 u8 query[sizeof(ent->id)] __nonstring;
151
152 ret = cmd_db_ready();
153 if (ret)
154 return ret;
155
156 /*
157 * Pad out query string to same length as in DB. NOTE: the output
158 * query string is not necessarily '\0' terminated if it bumps up
159 * against the max size. That's OK and expected.
160 */
161 strncpy(query, id, sizeof(query));
162
163 for (i = 0; i < MAX_SLV_ID; i++) {
164 rsc_hdr = &cmd_db_header->header[i];
165 if (!rsc_hdr->slv_id)
166 break;
167
168 ent = rsc_to_entry_header(rsc_hdr);
169 for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) {
170 if (memcmp(ent->id, query, sizeof(ent->id)) == 0) {
171 if (eh)
172 *eh = ent;
173 if (rh)
174 *rh = rsc_hdr;
175 return 0;
176 }
177 }
178 }
179
180 return -ENODEV;
181 }
182
183 /**
184 * cmd_db_read_addr() - Query command db for resource id address.
185 *
186 * @id: resource id to query for address
187 *
188 * Return: resource address on success, 0 on error
189 *
190 * This is used to retrieve resource address based on resource
191 * id.
192 */
cmd_db_read_addr(const char * id)193 u32 cmd_db_read_addr(const char *id)
194 {
195 int ret;
196 const struct entry_header *ent;
197
198 ret = cmd_db_get_header(id, &ent, NULL);
199
200 return ret < 0 ? 0 : le32_to_cpu(ent->addr);
201 }
202 EXPORT_SYMBOL(cmd_db_read_addr);
203
204 /**
205 * cmd_db_read_aux_data() - Query command db for aux data.
206 *
207 * @id: Resource to retrieve AUX Data on
208 * @len: size of data buffer returned
209 *
210 * Return: pointer to data on success, error pointer otherwise
211 */
cmd_db_read_aux_data(const char * id,size_t * len)212 const void *cmd_db_read_aux_data(const char *id, size_t *len)
213 {
214 int ret;
215 const struct entry_header *ent;
216 const struct rsc_hdr *rsc_hdr;
217
218 ret = cmd_db_get_header(id, &ent, &rsc_hdr);
219 if (ret)
220 return ERR_PTR(ret);
221
222 if (len)
223 *len = le16_to_cpu(ent->len);
224
225 return rsc_offset(rsc_hdr, ent);
226 }
227 EXPORT_SYMBOL(cmd_db_read_aux_data);
228
229 /**
230 * cmd_db_match_resource_addr() - Compare if both Resource addresses are same
231 *
232 * @addr1: Resource address to compare
233 * @addr2: Resource address to compare
234 *
235 * Return: true if two addresses refer to the same resource, false otherwise
236 */
cmd_db_match_resource_addr(u32 addr1,u32 addr2)237 bool cmd_db_match_resource_addr(u32 addr1, u32 addr2)
238 {
239 /*
240 * Each RPMh VRM accelerator resource has 3 or 4 contiguous 4-byte
241 * aligned addresses associated with it. Ignore the offset to check
242 * for VRM requests.
243 */
244 if (addr1 == addr2)
245 return true;
246 else if (SLAVE_ID(addr1) == CMD_DB_HW_VRM && VRM_ADDR(addr1) == VRM_ADDR(addr2))
247 return true;
248
249 return false;
250 }
251 EXPORT_SYMBOL_GPL(cmd_db_match_resource_addr);
252
253 /**
254 * cmd_db_read_slave_id - Get the slave ID for a given resource address
255 *
256 * @id: Resource id to query the DB for version
257 *
258 * Return: cmd_db_hw_type enum on success, CMD_DB_HW_INVALID on error
259 */
cmd_db_read_slave_id(const char * id)260 enum cmd_db_hw_type cmd_db_read_slave_id(const char *id)
261 {
262 int ret;
263 const struct entry_header *ent;
264 u32 addr;
265
266 ret = cmd_db_get_header(id, &ent, NULL);
267 if (ret < 0)
268 return CMD_DB_HW_INVALID;
269
270 addr = le32_to_cpu(ent->addr);
271 return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
272 }
273 EXPORT_SYMBOL(cmd_db_read_slave_id);
274
275 #ifdef CONFIG_DEBUG_FS
cmd_db_debugfs_dump(struct seq_file * seq,void * p)276 static int cmd_db_debugfs_dump(struct seq_file *seq, void *p)
277 {
278 int i, j;
279 const struct rsc_hdr *rsc;
280 const struct entry_header *ent;
281 const char *name;
282 u16 len, version;
283 u8 major, minor;
284
285 seq_puts(seq, "Command DB DUMP\n");
286
287 for (i = 0; i < MAX_SLV_ID; i++) {
288 rsc = &cmd_db_header->header[i];
289 if (!rsc->slv_id)
290 break;
291
292 switch (le16_to_cpu(rsc->slv_id)) {
293 case CMD_DB_HW_ARC:
294 name = "ARC";
295 break;
296 case CMD_DB_HW_VRM:
297 name = "VRM";
298 break;
299 case CMD_DB_HW_BCM:
300 name = "BCM";
301 break;
302 default:
303 name = "Unknown";
304 break;
305 }
306
307 version = le16_to_cpu(rsc->version);
308 major = version >> 8;
309 minor = version;
310
311 seq_printf(seq, "Slave %s (v%u.%u)\n", name, major, minor);
312 seq_puts(seq, "-------------------------\n");
313
314 ent = rsc_to_entry_header(rsc);
315 for (j = 0; j < le16_to_cpu(rsc->cnt); j++, ent++) {
316 seq_printf(seq, "0x%05x: %*pEp", le32_to_cpu(ent->addr),
317 (int)strnlen(ent->id, sizeof(ent->id)), ent->id);
318
319 len = le16_to_cpu(ent->len);
320 if (len) {
321 seq_printf(seq, " [%*ph]",
322 len, rsc_offset(rsc, ent));
323 }
324 seq_putc(seq, '\n');
325 }
326 }
327
328 return 0;
329 }
330
open_cmd_db_debugfs(struct inode * inode,struct file * file)331 static int open_cmd_db_debugfs(struct inode *inode, struct file *file)
332 {
333 return single_open(file, cmd_db_debugfs_dump, inode->i_private);
334 }
335 #endif
336
337 static const struct file_operations cmd_db_debugfs_ops = {
338 #ifdef CONFIG_DEBUG_FS
339 .open = open_cmd_db_debugfs,
340 #endif
341 .read = seq_read,
342 .llseek = seq_lseek,
343 .release = single_release,
344 };
345
cmd_db_dev_probe(struct platform_device * pdev)346 static int cmd_db_dev_probe(struct platform_device *pdev)
347 {
348 struct reserved_mem *rmem;
349 int ret = 0;
350
351 rmem = of_reserved_mem_lookup(pdev->dev.of_node);
352 if (!rmem) {
353 dev_err(&pdev->dev, "failed to acquire memory region\n");
354 return -EINVAL;
355 }
356
357 cmd_db_header = memremap(rmem->base, rmem->size, MEMREMAP_WB);
358 if (!cmd_db_header) {
359 ret = -ENOMEM;
360 cmd_db_header = NULL;
361 return ret;
362 }
363
364 if (!cmd_db_magic_matches(cmd_db_header)) {
365 dev_err(&pdev->dev, "Invalid Command DB Magic\n");
366 return -EINVAL;
367 }
368
369 debugfs_create_file("cmd-db", 0400, NULL, NULL, &cmd_db_debugfs_ops);
370
371 device_set_pm_not_required(&pdev->dev);
372
373 return 0;
374 }
375
376 static const struct of_device_id cmd_db_match_table[] = {
377 { .compatible = "qcom,cmd-db" },
378 { }
379 };
380 MODULE_DEVICE_TABLE(of, cmd_db_match_table);
381
382 static struct platform_driver cmd_db_dev_driver = {
383 .probe = cmd_db_dev_probe,
384 .driver = {
385 .name = "cmd-db",
386 .of_match_table = cmd_db_match_table,
387 .suppress_bind_attrs = true,
388 },
389 };
390
cmd_db_device_init(void)391 static int __init cmd_db_device_init(void)
392 {
393 return platform_driver_register(&cmd_db_dev_driver);
394 }
395 arch_initcall(cmd_db_device_init);
396
397 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Command DB Driver");
398 MODULE_LICENSE("GPL v2");
399