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