Linux Audio

Check our new training course

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