cmd-db.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. */
  3. #include <linux/kernel.h>
  4. #include <linux/of.h>
  5. #include <linux/of_address.h>
  6. #include <linux/of_platform.h>
  7. #include <linux/of_reserved_mem.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/types.h>
  10. #include <soc/qcom/cmd-db.h>
  11. #define NUM_PRIORITY 2
  12. #define MAX_SLV_ID 8
  13. #define SLAVE_ID_MASK 0x7
  14. #define SLAVE_ID_SHIFT 16
  15. /**
  16. * struct entry_header: header for each entry in cmddb
  17. *
  18. * @id: resource's identifier
  19. * @priority: unused
  20. * @addr: the address of the resource
  21. * @len: length of the data
  22. * @offset: offset from :@data_offset, start of the data
  23. */
  24. struct entry_header {
  25. u8 id[8];
  26. __le32 priority[NUM_PRIORITY];
  27. __le32 addr;
  28. __le16 len;
  29. __le16 offset;
  30. };
  31. /**
  32. * struct rsc_hdr: resource header information
  33. *
  34. * @slv_id: id for the resource
  35. * @header_offset: entry's header at offset from the end of the cmd_db_header
  36. * @data_offset: entry's data at offset from the end of the cmd_db_header
  37. * @cnt: number of entries for HW type
  38. * @version: MSB is major, LSB is minor
  39. * @reserved: reserved for future use.
  40. */
  41. struct rsc_hdr {
  42. __le16 slv_id;
  43. __le16 header_offset;
  44. __le16 data_offset;
  45. __le16 cnt;
  46. __le16 version;
  47. __le16 reserved[3];
  48. };
  49. /**
  50. * struct cmd_db_header: The DB header information
  51. *
  52. * @version: The cmd db version
  53. * @magic: constant expected in the database
  54. * @header: array of resources
  55. * @checksum: checksum for the header. Unused.
  56. * @reserved: reserved memory
  57. * @data: driver specific data
  58. */
  59. struct cmd_db_header {
  60. __le32 version;
  61. u8 magic[4];
  62. struct rsc_hdr header[MAX_SLV_ID];
  63. __le32 checksum;
  64. __le32 reserved;
  65. u8 data[];
  66. };
  67. /**
  68. * DOC: Description of the Command DB database.
  69. *
  70. * At the start of the command DB memory is the cmd_db_header structure.
  71. * The cmd_db_header holds the version, checksum, magic key as well as an
  72. * array for header for each slave (depicted by the rsc_header). Each h/w
  73. * based accelerator is a 'slave' (shared resource) and has slave id indicating
  74. * the type of accelerator. The rsc_header is the header for such individual
  75. * slaves of a given type. The entries for each of these slaves begin at the
  76. * rsc_hdr.header_offset. In addition each slave could have auxiliary data
  77. * that may be needed by the driver. The data for the slave starts at the
  78. * entry_header.offset to the location pointed to by the rsc_hdr.data_offset.
  79. *
  80. * Drivers have a stringified key to a slave/resource. They can query the slave
  81. * information and get the slave id and the auxiliary data and the length of the
  82. * data. Using this information, they can format the request to be sent to the
  83. * h/w accelerator and request a resource state.
  84. */
  85. static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c };
  86. static bool cmd_db_magic_matches(const struct cmd_db_header *header)
  87. {
  88. const u8 *magic = header->magic;
  89. return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0;
  90. }
  91. static struct cmd_db_header *cmd_db_header;
  92. static inline void *rsc_to_entry_header(struct rsc_hdr *hdr)
  93. {
  94. u16 offset = le16_to_cpu(hdr->header_offset);
  95. return cmd_db_header->data + offset;
  96. }
  97. static inline void *
  98. rsc_offset(struct rsc_hdr *hdr, struct entry_header *ent)
  99. {
  100. u16 offset = le16_to_cpu(hdr->data_offset);
  101. u16 loffset = le16_to_cpu(ent->offset);
  102. return cmd_db_header->data + offset + loffset;
  103. }
  104. /**
  105. * cmd_db_ready - Indicates if command DB is available
  106. *
  107. * Return: 0 on success, errno otherwise
  108. */
  109. int cmd_db_ready(void)
  110. {
  111. if (cmd_db_header == NULL)
  112. return -EPROBE_DEFER;
  113. else if (!cmd_db_magic_matches(cmd_db_header))
  114. return -EINVAL;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(cmd_db_ready);
  118. static int cmd_db_get_header(const char *id, struct entry_header *eh,
  119. struct rsc_hdr *rh)
  120. {
  121. struct rsc_hdr *rsc_hdr;
  122. struct entry_header *ent;
  123. int ret, i, j;
  124. u8 query[8];
  125. ret = cmd_db_ready();
  126. if (ret)
  127. return ret;
  128. if (!eh || !rh)
  129. return -EINVAL;
  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. break;
  140. }
  141. if (j < le16_to_cpu(rsc_hdr->cnt)) {
  142. memcpy(eh, ent, sizeof(*ent));
  143. memcpy(rh, rsc_hdr, sizeof(*rh));
  144. return 0;
  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. struct entry_header ent;
  163. struct rsc_hdr rsc_hdr;
  164. ret = cmd_db_get_header(id, &ent, &rsc_hdr);
  165. return ret < 0 ? 0 : le32_to_cpu(ent.addr);
  166. }
  167. EXPORT_SYMBOL(cmd_db_read_addr);
  168. /**
  169. * cmd_db_read_aux_data() - Query command db for aux data.
  170. *
  171. * @id: Resource to retrieve AUX Data on.
  172. * @data: Data buffer to copy returned aux data to. Returns size on NULL
  173. * @len: Caller provides size of data buffer passed in.
  174. *
  175. * Return: size of data on success, errno otherwise
  176. */
  177. int cmd_db_read_aux_data(const char *id, u8 *data, size_t len)
  178. {
  179. int ret;
  180. struct entry_header ent;
  181. struct rsc_hdr rsc_hdr;
  182. u16 ent_len;
  183. if (!data)
  184. return -EINVAL;
  185. ret = cmd_db_get_header(id, &ent, &rsc_hdr);
  186. if (ret)
  187. return ret;
  188. ent_len = le16_to_cpu(ent.len);
  189. if (len < ent_len)
  190. return -EINVAL;
  191. len = min_t(u16, ent_len, len);
  192. memcpy(data, rsc_offset(&rsc_hdr, &ent), len);
  193. return len;
  194. }
  195. EXPORT_SYMBOL(cmd_db_read_aux_data);
  196. /**
  197. * cmd_db_read_aux_data_len - Get the length of the auxiliary data stored in DB.
  198. *
  199. * @id: Resource to retrieve AUX Data.
  200. *
  201. * Return: size on success, 0 on error
  202. */
  203. size_t cmd_db_read_aux_data_len(const char *id)
  204. {
  205. int ret;
  206. struct entry_header ent;
  207. struct rsc_hdr rsc_hdr;
  208. ret = cmd_db_get_header(id, &ent, &rsc_hdr);
  209. return ret < 0 ? 0 : le16_to_cpu(ent.len);
  210. }
  211. EXPORT_SYMBOL(cmd_db_read_aux_data_len);
  212. /**
  213. * cmd_db_read_slave_id - Get the slave ID for a given resource address
  214. *
  215. * @id: Resource id to query the DB for version
  216. *
  217. * Return: cmd_db_hw_type enum on success, CMD_DB_HW_INVALID on error
  218. */
  219. enum cmd_db_hw_type cmd_db_read_slave_id(const char *id)
  220. {
  221. int ret;
  222. struct entry_header ent;
  223. struct rsc_hdr rsc_hdr;
  224. u32 addr;
  225. ret = cmd_db_get_header(id, &ent, &rsc_hdr);
  226. if (ret < 0)
  227. return CMD_DB_HW_INVALID;
  228. addr = le32_to_cpu(ent.addr);
  229. return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
  230. }
  231. EXPORT_SYMBOL(cmd_db_read_slave_id);
  232. static int cmd_db_dev_probe(struct platform_device *pdev)
  233. {
  234. struct reserved_mem *rmem;
  235. int ret = 0;
  236. rmem = of_reserved_mem_lookup(pdev->dev.of_node);
  237. if (!rmem) {
  238. dev_err(&pdev->dev, "failed to acquire memory region\n");
  239. return -EINVAL;
  240. }
  241. cmd_db_header = memremap(rmem->base, rmem->size, MEMREMAP_WB);
  242. if (!cmd_db_header) {
  243. ret = -ENOMEM;
  244. cmd_db_header = NULL;
  245. return ret;
  246. }
  247. if (!cmd_db_magic_matches(cmd_db_header)) {
  248. dev_err(&pdev->dev, "Invalid Command DB Magic\n");
  249. return -EINVAL;
  250. }
  251. return 0;
  252. }
  253. static const struct of_device_id cmd_db_match_table[] = {
  254. { .compatible = "qcom,cmd-db" },
  255. { },
  256. };
  257. static struct platform_driver cmd_db_dev_driver = {
  258. .probe = cmd_db_dev_probe,
  259. .driver = {
  260. .name = "cmd-db",
  261. .of_match_table = cmd_db_match_table,
  262. },
  263. };
  264. static int __init cmd_db_device_init(void)
  265. {
  266. return platform_driver_register(&cmd_db_dev_driver);
  267. }
  268. arch_initcall(cmd_db_device_init);