cmd-db.c 8.5 KB

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