cmd-db.c 9.0 KB

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