1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. */
4 #include <linux/kernel.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>
12 #include <soc/qcom/cmd-db.h>
14 #define NUM_PRIORITY 2
16 #define SLAVE_ID_MASK 0x7
17 #define SLAVE_ID_SHIFT 16
20 * struct entry_header: header for each entry in cmddb
22 * @id: resource's identifier
24 * @addr: the address of the resource
25 * @len: length of the data
26 * @offset: offset from :@data_offset, start of the data
30 __le32 priority
[NUM_PRIORITY
];
37 * struct rsc_hdr: resource header information
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.
56 * struct cmd_db_header: The DB header information
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
65 struct cmd_db_header
{
68 struct rsc_hdr header
[MAX_SLV_ID
];
75 * DOC: Description of the Command DB database.
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.
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.
93 static const u8 CMD_DB_MAGIC
[] = { 0xdb, 0x30, 0x03, 0x0c };
95 static bool cmd_db_magic_matches(const struct cmd_db_header
*header
)
97 const u8
*magic
= header
->magic
;
99 return memcmp(magic
, CMD_DB_MAGIC
, ARRAY_SIZE(CMD_DB_MAGIC
)) == 0;
102 static struct cmd_db_header
*cmd_db_header
;
104 static inline const void *rsc_to_entry_header(const struct rsc_hdr
*hdr
)
106 u16 offset
= le16_to_cpu(hdr
->header_offset
);
108 return cmd_db_header
->data
+ offset
;
112 rsc_offset(const struct rsc_hdr
*hdr
, const struct entry_header
*ent
)
114 u16 offset
= le16_to_cpu(hdr
->data_offset
);
115 u16 loffset
= le16_to_cpu(ent
->offset
);
117 return cmd_db_header
->data
+ offset
+ loffset
;
121 * cmd_db_ready - Indicates if command DB is available
123 * Return: 0 on success, errno otherwise
125 int cmd_db_ready(void)
127 if (cmd_db_header
== NULL
)
128 return -EPROBE_DEFER
;
129 else if (!cmd_db_magic_matches(cmd_db_header
))
134 EXPORT_SYMBOL(cmd_db_ready
);
136 static int cmd_db_get_header(const char *id
, const struct entry_header
**eh
,
137 const struct rsc_hdr
**rh
)
139 const struct rsc_hdr
*rsc_hdr
;
140 const struct entry_header
*ent
;
144 ret
= cmd_db_ready();
148 /* Pad out query string to same length as in DB */
149 strncpy(query
, id
, sizeof(query
));
151 for (i
= 0; i
< MAX_SLV_ID
; i
++) {
152 rsc_hdr
= &cmd_db_header
->header
[i
];
153 if (!rsc_hdr
->slv_id
)
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) {
172 * cmd_db_read_addr() - Query command db for resource id address.
174 * @id: resource id to query for address
176 * Return: resource address on success, 0 on error
178 * This is used to retrieve resource address based on resource
181 u32
cmd_db_read_addr(const char *id
)
184 const struct entry_header
*ent
;
186 ret
= cmd_db_get_header(id
, &ent
, NULL
);
188 return ret
< 0 ? 0 : le32_to_cpu(ent
->addr
);
190 EXPORT_SYMBOL(cmd_db_read_addr
);
193 * cmd_db_read_aux_data() - Query command db for aux data.
195 * @id: Resource to retrieve AUX Data on
196 * @len: size of data buffer returned
198 * Return: pointer to data on success, error pointer otherwise
200 const void *cmd_db_read_aux_data(const char *id
, size_t *len
)
203 const struct entry_header
*ent
;
204 const struct rsc_hdr
*rsc_hdr
;
206 ret
= cmd_db_get_header(id
, &ent
, &rsc_hdr
);
211 *len
= le16_to_cpu(ent
->len
);
213 return rsc_offset(rsc_hdr
, ent
);
215 EXPORT_SYMBOL(cmd_db_read_aux_data
);
218 * cmd_db_read_slave_id - Get the slave ID for a given resource address
220 * @id: Resource id to query the DB for version
222 * Return: cmd_db_hw_type enum on success, CMD_DB_HW_INVALID on error
224 enum cmd_db_hw_type
cmd_db_read_slave_id(const char *id
)
227 const struct entry_header
*ent
;
230 ret
= cmd_db_get_header(id
, &ent
, NULL
);
232 return CMD_DB_HW_INVALID
;
234 addr
= le32_to_cpu(ent
->addr
);
235 return (addr
>> SLAVE_ID_SHIFT
) & SLAVE_ID_MASK
;
237 EXPORT_SYMBOL(cmd_db_read_slave_id
);
239 static int cmd_db_dev_probe(struct platform_device
*pdev
)
241 struct reserved_mem
*rmem
;
244 rmem
= of_reserved_mem_lookup(pdev
->dev
.of_node
);
246 dev_err(&pdev
->dev
, "failed to acquire memory region\n");
250 cmd_db_header
= memremap(rmem
->base
, rmem
->size
, MEMREMAP_WB
);
251 if (IS_ERR_OR_NULL(cmd_db_header
)) {
252 ret
= PTR_ERR(cmd_db_header
);
253 cmd_db_header
= NULL
;
257 if (!cmd_db_magic_matches(cmd_db_header
)) {
258 dev_err(&pdev
->dev
, "Invalid Command DB Magic\n");
265 static const struct of_device_id cmd_db_match_table
[] = {
266 { .compatible
= "qcom,cmd-db" },
270 static struct platform_driver cmd_db_dev_driver
= {
271 .probe
= cmd_db_dev_probe
,
274 .of_match_table
= cmd_db_match_table
,
278 static int __init
cmd_db_device_init(void)
280 return platform_driver_register(&cmd_db_dev_driver
);
282 arch_initcall(cmd_db_device_init
);