cmd-db.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 const void *rsc_to_entry_header(const 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(const struct rsc_hdr *hdr, const 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, const struct entry_header **eh,
  119. const struct rsc_hdr **rh)
  120. {
  121. const struct rsc_hdr *rsc_hdr;
  122. const 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. /* Pad out query string to same length as in DB */
  129. strncpy(query, id, sizeof(query));
  130. for (i = 0; i < MAX_SLV_ID; i++) {
  131. rsc_hdr = &cmd_db_header->header[i];
  132. if (!rsc_hdr->slv_id)
  133. break;
  134. ent = rsc_to_entry_header(rsc_hdr);
  135. for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) {
  136. if (memcmp(ent->id, query, sizeof(ent->id)) == 0) {
  137. if (eh)
  138. *eh = ent;
  139. if (rh)
  140. *rh = rsc_hdr;
  141. return 0;
  142. }
  143. }
  144. }
  145. return -ENODEV;
  146. }
  147. /**
  148. * cmd_db_read_addr() - Query command db for resource id address.
  149. *
  150. * @id: resource id to query for address
  151. *
  152. * Return: resource address on success, 0 on error
  153. *
  154. * This is used to retrieve resource address based on resource
  155. * id.
  156. */
  157. u32 cmd_db_read_addr(const char *id)
  158. {
  159. int ret;
  160. const struct entry_header *ent;
  161. ret = cmd_db_get_header(id, &ent, NULL);
  162. return ret < 0 ? 0 : le32_to_cpu(ent->addr);
  163. }
  164. EXPORT_SYMBOL(cmd_db_read_addr);
  165. /**
  166. * cmd_db_read_aux_data() - Query command db for aux data.
  167. *
  168. * @id: Resource to retrieve AUX Data on
  169. * @len: size of data buffer returned
  170. *
  171. * Return: pointer to data on success, error pointer otherwise
  172. */
  173. const void *cmd_db_read_aux_data(const char *id, size_t *len)
  174. {
  175. int ret;
  176. const struct entry_header *ent;
  177. const struct rsc_hdr *rsc_hdr;
  178. ret = cmd_db_get_header(id, &ent, &rsc_hdr);
  179. if (ret)
  180. return ERR_PTR(ret);
  181. if (len)
  182. *len = le16_to_cpu(ent->len);
  183. return rsc_offset(rsc_hdr, ent);
  184. }
  185. EXPORT_SYMBOL(cmd_db_read_aux_data);
  186. /**
  187. * cmd_db_read_slave_id - Get the slave ID for a given resource address
  188. *
  189. * @id: Resource id to query the DB for version
  190. *
  191. * Return: cmd_db_hw_type enum on success, CMD_DB_HW_INVALID on error
  192. */
  193. enum cmd_db_hw_type cmd_db_read_slave_id(const char *id)
  194. {
  195. int ret;
  196. const struct entry_header *ent;
  197. u32 addr;
  198. ret = cmd_db_get_header(id, &ent, NULL);
  199. if (ret < 0)
  200. return CMD_DB_HW_INVALID;
  201. addr = le32_to_cpu(ent->addr);
  202. return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
  203. }
  204. EXPORT_SYMBOL(cmd_db_read_slave_id);
  205. static int cmd_db_dev_probe(struct platform_device *pdev)
  206. {
  207. struct reserved_mem *rmem;
  208. int ret = 0;
  209. rmem = of_reserved_mem_lookup(pdev->dev.of_node);
  210. if (!rmem) {
  211. dev_err(&pdev->dev, "failed to acquire memory region\n");
  212. return -EINVAL;
  213. }
  214. cmd_db_header = memremap(rmem->base, rmem->size, MEMREMAP_WB);
  215. if (!cmd_db_header) {
  216. ret = -ENOMEM;
  217. cmd_db_header = NULL;
  218. return ret;
  219. }
  220. if (!cmd_db_magic_matches(cmd_db_header)) {
  221. dev_err(&pdev->dev, "Invalid Command DB Magic\n");
  222. return -EINVAL;
  223. }
  224. return 0;
  225. }
  226. static const struct of_device_id cmd_db_match_table[] = {
  227. { .compatible = "qcom,cmd-db" },
  228. { },
  229. };
  230. static struct platform_driver cmd_db_dev_driver = {
  231. .probe = cmd_db_dev_probe,
  232. .driver = {
  233. .name = "cmd-db",
  234. .of_match_table = cmd_db_match_table,
  235. },
  236. };
  237. static int __init cmd_db_device_init(void)
  238. {
  239. return platform_driver_register(&cmd_db_dev_driver);
  240. }
  241. arch_initcall(cmd_db_device_init);