cmd-db.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #include <linux/bitfield.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_reserved_mem.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/types.h>
  16. #include <soc/qcom/cmd-db.h>
  17. #define NUM_PRIORITY 2
  18. #define MAX_SLV_ID 8
  19. #define SLAVE_ID_MASK 0x7
  20. #define SLAVE_ID_SHIFT 16
  21. #define SLAVE_ID(addr) FIELD_GET(GENMASK(19, 16), addr)
  22. #define VRM_ADDR(addr) FIELD_GET(GENMASK(19, 4), addr)
  23. /**
  24. * struct entry_header: header for each entry in cmddb
  25. *
  26. * @id: resource's identifier
  27. * @priority: unused
  28. * @addr: the address of the resource
  29. * @len: length of the data
  30. * @offset: offset from :@data_offset, start of the data
  31. */
  32. struct entry_header {
  33. u8 id[8];
  34. __le32 priority[NUM_PRIORITY];
  35. __le32 addr;
  36. __le16 len;
  37. __le16 offset;
  38. };
  39. /**
  40. * struct rsc_hdr: resource header information
  41. *
  42. * @slv_id: id for the resource
  43. * @header_offset: entry's header at offset from the end of the cmd_db_header
  44. * @data_offset: entry's data at offset from the end of the cmd_db_header
  45. * @cnt: number of entries for HW type
  46. * @version: MSB is major, LSB is minor
  47. * @reserved: reserved for future use.
  48. */
  49. struct rsc_hdr {
  50. __le16 slv_id;
  51. __le16 header_offset;
  52. __le16 data_offset;
  53. __le16 cnt;
  54. __le16 version;
  55. __le16 reserved[3];
  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. */
  67. struct 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. * 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. static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c };
  94. static bool cmd_db_magic_matches(const struct cmd_db_header *header)
  95. {
  96. const u8 *magic = header->magic;
  97. return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0;
  98. }
  99. static struct cmd_db_header *cmd_db_header;
  100. static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr)
  101. {
  102. u16 offset = le16_to_cpu(hdr->header_offset);
  103. return cmd_db_header->data + offset;
  104. }
  105. static inline void *
  106. rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent)
  107. {
  108. u16 offset = le16_to_cpu(hdr->data_offset);
  109. u16 loffset = le16_to_cpu(ent->offset);
  110. return cmd_db_header->data + offset + loffset;
  111. }
  112. /**
  113. * cmd_db_ready - Indicates if command DB is available
  114. *
  115. * Return: 0 on success, errno otherwise
  116. */
  117. int cmd_db_ready(void)
  118. {
  119. if (cmd_db_header == NULL)
  120. return -EPROBE_DEFER;
  121. else if (!cmd_db_magic_matches(cmd_db_header))
  122. return -EINVAL;
  123. return 0;
  124. }
  125. EXPORT_SYMBOL_GPL(cmd_db_ready);
  126. static int cmd_db_get_header(const char *id, const struct entry_header **eh,
  127. const struct rsc_hdr **rh)
  128. {
  129. const struct rsc_hdr *rsc_hdr;
  130. const struct entry_header *ent;
  131. int ret, i, j;
  132. u8 query[sizeof(ent->id)] __nonstring;
  133. ret = cmd_db_ready();
  134. if (ret)
  135. return ret;
  136. strtomem_pad(query, id, 0);
  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_GPL(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_GPL(cmd_db_read_aux_data);
  193. /**
  194. * cmd_db_match_resource_addr() - Compare if both Resource addresses are same
  195. *
  196. * @addr1: Resource address to compare
  197. * @addr2: Resource address to compare
  198. *
  199. * Return: true if two addresses refer to the same resource, false otherwise
  200. */
  201. bool cmd_db_match_resource_addr(u32 addr1, u32 addr2)
  202. {
  203. /*
  204. * Each RPMh VRM accelerator resource has 3 or 4 contiguous 4-byte
  205. * aligned addresses associated with it. Ignore the offset to check
  206. * for VRM requests.
  207. */
  208. if (addr1 == addr2)
  209. return true;
  210. else if (SLAVE_ID(addr1) == CMD_DB_HW_VRM && VRM_ADDR(addr1) == VRM_ADDR(addr2))
  211. return true;
  212. return false;
  213. }
  214. EXPORT_SYMBOL_GPL(cmd_db_match_resource_addr);
  215. /**
  216. * cmd_db_read_slave_id - Get the slave ID for a given resource address
  217. *
  218. * @id: Resource id to query the DB for version
  219. *
  220. * Return: cmd_db_hw_type enum on success, CMD_DB_HW_INVALID on error
  221. */
  222. enum cmd_db_hw_type cmd_db_read_slave_id(const char *id)
  223. {
  224. int ret;
  225. const struct entry_header *ent;
  226. u32 addr;
  227. ret = cmd_db_get_header(id, &ent, NULL);
  228. if (ret < 0)
  229. return CMD_DB_HW_INVALID;
  230. addr = le32_to_cpu(ent->addr);
  231. return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
  232. }
  233. EXPORT_SYMBOL_GPL(cmd_db_read_slave_id);
  234. #ifdef CONFIG_DEBUG_FS
  235. static int cmd_db_debugfs_dump(struct seq_file *seq, void *p)
  236. {
  237. int i, j;
  238. const struct rsc_hdr *rsc;
  239. const struct entry_header *ent;
  240. const char *name;
  241. u16 len, version;
  242. u8 major, minor;
  243. seq_puts(seq, "Command DB DUMP\n");
  244. for (i = 0; i < MAX_SLV_ID; i++) {
  245. rsc = &cmd_db_header->header[i];
  246. if (!rsc->slv_id)
  247. break;
  248. switch (le16_to_cpu(rsc->slv_id)) {
  249. case CMD_DB_HW_ARC:
  250. name = "ARC";
  251. break;
  252. case CMD_DB_HW_VRM:
  253. name = "VRM";
  254. break;
  255. case CMD_DB_HW_BCM:
  256. name = "BCM";
  257. break;
  258. default:
  259. name = "Unknown";
  260. break;
  261. }
  262. version = le16_to_cpu(rsc->version);
  263. major = version >> 8;
  264. minor = version;
  265. seq_printf(seq, "Slave %s (v%u.%u)\n", name, major, minor);
  266. seq_puts(seq, "-------------------------\n");
  267. ent = rsc_to_entry_header(rsc);
  268. for (j = 0; j < le16_to_cpu(rsc->cnt); j++, ent++) {
  269. seq_printf(seq, "0x%05x: %*pEp", le32_to_cpu(ent->addr),
  270. (int)strnlen(ent->id, sizeof(ent->id)), ent->id);
  271. len = le16_to_cpu(ent->len);
  272. if (len) {
  273. seq_printf(seq, " [%*ph]",
  274. len, rsc_offset(rsc, ent));
  275. }
  276. seq_putc(seq, '\n');
  277. }
  278. }
  279. return 0;
  280. }
  281. static int open_cmd_db_debugfs(struct inode *inode, struct file *file)
  282. {
  283. return single_open(file, cmd_db_debugfs_dump, inode->i_private);
  284. }
  285. #endif
  286. static const struct file_operations cmd_db_debugfs_ops = {
  287. #ifdef CONFIG_DEBUG_FS
  288. .open = open_cmd_db_debugfs,
  289. #endif
  290. .read = seq_read,
  291. .llseek = seq_lseek,
  292. .release = single_release,
  293. };
  294. static int cmd_db_dev_probe(struct platform_device *pdev)
  295. {
  296. struct reserved_mem *rmem;
  297. int ret = 0;
  298. rmem = of_reserved_mem_lookup(pdev->dev.of_node);
  299. if (!rmem) {
  300. dev_err(&pdev->dev, "failed to acquire memory region\n");
  301. return -EINVAL;
  302. }
  303. cmd_db_header = devm_memremap(&pdev->dev, rmem->base, rmem->size, MEMREMAP_WC);
  304. if (IS_ERR(cmd_db_header)) {
  305. ret = PTR_ERR(cmd_db_header);
  306. cmd_db_header = NULL;
  307. return ret;
  308. }
  309. if (!cmd_db_magic_matches(cmd_db_header)) {
  310. dev_err(&pdev->dev, "Invalid Command DB Magic\n");
  311. cmd_db_header = NULL;
  312. return -EINVAL;
  313. }
  314. debugfs_create_file("cmd-db", 0400, NULL, NULL, &cmd_db_debugfs_ops);
  315. device_set_pm_not_required(&pdev->dev);
  316. return 0;
  317. }
  318. static const struct of_device_id cmd_db_match_table[] = {
  319. { .compatible = "qcom,cmd-db" },
  320. { }
  321. };
  322. MODULE_DEVICE_TABLE(of, cmd_db_match_table);
  323. static struct platform_driver cmd_db_dev_driver = {
  324. .probe = cmd_db_dev_probe,
  325. .driver = {
  326. .name = "cmd-db",
  327. .of_match_table = cmd_db_match_table,
  328. .suppress_bind_attrs = true,
  329. },
  330. };
  331. static int __init cmd_db_device_init(void)
  332. {
  333. return platform_driver_register(&cmd_db_dev_driver);
  334. }
  335. core_initcall(cmd_db_device_init);
  336. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Command DB Driver");
  337. MODULE_LICENSE("GPL v2");