cam_packet_util.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/slab.h>
  7. #include "cam_mem_mgr.h"
  8. #include "cam_packet_util.h"
  9. #include "cam_debug_util.h"
  10. int cam_packet_util_get_cmd_mem_addr(int handle, uint32_t **buf_addr,
  11. size_t *len)
  12. {
  13. int rc = 0;
  14. uintptr_t kmd_buf_addr = 0;
  15. rc = cam_mem_get_cpu_buf(handle, &kmd_buf_addr, len);
  16. if (rc) {
  17. CAM_ERR(CAM_UTIL, "Unable to get the virtual address %d", rc);
  18. } else {
  19. if (kmd_buf_addr && *len) {
  20. *buf_addr = (uint32_t *)kmd_buf_addr;
  21. } else {
  22. CAM_ERR(CAM_UTIL, "Invalid addr and length :%zd", *len);
  23. rc = -ENOMEM;
  24. }
  25. }
  26. return rc;
  27. }
  28. int cam_packet_util_validate_cmd_desc(struct cam_cmd_buf_desc *cmd_desc)
  29. {
  30. if ((cmd_desc->length > cmd_desc->size) ||
  31. (cmd_desc->mem_handle <= 0)) {
  32. CAM_ERR(CAM_UTIL, "invalid cmd arg %d %d %d %d",
  33. cmd_desc->offset, cmd_desc->length,
  34. cmd_desc->mem_handle, cmd_desc->size);
  35. return -EINVAL;
  36. }
  37. return 0;
  38. }
  39. int cam_packet_util_validate_packet(struct cam_packet *packet,
  40. size_t remain_len)
  41. {
  42. size_t sum_cmd_desc = 0;
  43. size_t sum_io_cfgs = 0;
  44. size_t sum_patch_desc = 0;
  45. size_t pkt_wo_payload = 0;
  46. if (!packet)
  47. return -EINVAL;
  48. if ((size_t)packet->header.size > remain_len) {
  49. CAM_ERR(CAM_UTIL,
  50. "Invalid packet size: %zu, CPU buf length: %zu",
  51. (size_t)packet->header.size, remain_len);
  52. return -EINVAL;
  53. }
  54. CAM_DBG(CAM_UTIL, "num cmd buf:%d num of io config:%d kmd buf index:%d",
  55. packet->num_cmd_buf, packet->num_io_configs,
  56. packet->kmd_cmd_buf_index);
  57. sum_cmd_desc = packet->num_cmd_buf * sizeof(struct cam_cmd_buf_desc);
  58. sum_io_cfgs = packet->num_io_configs * sizeof(struct cam_buf_io_cfg);
  59. sum_patch_desc = packet->num_patches * sizeof(struct cam_patch_desc);
  60. pkt_wo_payload = offsetof(struct cam_packet, payload);
  61. if ((!packet->header.size) ||
  62. ((pkt_wo_payload + (size_t)packet->cmd_buf_offset +
  63. sum_cmd_desc) > (size_t)packet->header.size) ||
  64. ((pkt_wo_payload + (size_t)packet->io_configs_offset +
  65. sum_io_cfgs) > (size_t)packet->header.size) ||
  66. ((pkt_wo_payload + (size_t)packet->patch_offset +
  67. sum_patch_desc) > (size_t)packet->header.size)) {
  68. CAM_ERR(CAM_UTIL, "params not within mem len:%zu %zu %zu %zu",
  69. (size_t)packet->header.size, sum_cmd_desc,
  70. sum_io_cfgs, sum_patch_desc);
  71. return -EINVAL;
  72. }
  73. return 0;
  74. }
  75. int cam_packet_util_get_kmd_buffer(struct cam_packet *packet,
  76. struct cam_kmd_buf_info *kmd_buf)
  77. {
  78. int rc = 0;
  79. size_t len = 0;
  80. size_t remain_len = 0;
  81. struct cam_cmd_buf_desc *cmd_desc;
  82. uint32_t *cpu_addr;
  83. if (!packet || !kmd_buf) {
  84. CAM_ERR(CAM_UTIL, "Invalid arg %pK %pK", packet, kmd_buf);
  85. return -EINVAL;
  86. }
  87. if ((packet->kmd_cmd_buf_index < 0) ||
  88. (packet->kmd_cmd_buf_index >= packet->num_cmd_buf)) {
  89. CAM_ERR(CAM_UTIL, "Invalid kmd buf index: %d",
  90. packet->kmd_cmd_buf_index);
  91. return -EINVAL;
  92. }
  93. /* Take first command descriptor and add offset to it for kmd*/
  94. cmd_desc = (struct cam_cmd_buf_desc *) ((uint8_t *)
  95. &packet->payload + packet->cmd_buf_offset);
  96. cmd_desc += packet->kmd_cmd_buf_index;
  97. rc = cam_packet_util_validate_cmd_desc(cmd_desc);
  98. if (rc)
  99. return rc;
  100. rc = cam_packet_util_get_cmd_mem_addr(cmd_desc->mem_handle, &cpu_addr,
  101. &len);
  102. if (rc)
  103. return rc;
  104. remain_len = len;
  105. if (((size_t)cmd_desc->offset >= len) ||
  106. ((size_t)cmd_desc->size > (len - (size_t)cmd_desc->offset))) {
  107. CAM_ERR(CAM_UTIL, "invalid memory len:%zd and cmd desc size:%d",
  108. len, cmd_desc->size);
  109. return -EINVAL;
  110. }
  111. remain_len -= (size_t)cmd_desc->offset;
  112. if ((size_t)packet->kmd_cmd_buf_offset >= remain_len) {
  113. CAM_ERR(CAM_UTIL, "Invalid kmd cmd buf offset: %zu",
  114. (size_t)packet->kmd_cmd_buf_offset);
  115. return -EINVAL;
  116. }
  117. cpu_addr += (cmd_desc->offset / 4) + (packet->kmd_cmd_buf_offset / 4);
  118. CAM_DBG(CAM_UTIL, "total size %d, cmd size: %d, KMD buffer size: %d",
  119. cmd_desc->size, cmd_desc->length,
  120. cmd_desc->size - cmd_desc->length);
  121. CAM_DBG(CAM_UTIL, "hdl 0x%x, cmd offset %d, kmd offset %d, addr 0x%pK",
  122. cmd_desc->mem_handle, cmd_desc->offset,
  123. packet->kmd_cmd_buf_offset, cpu_addr);
  124. kmd_buf->cpu_addr = cpu_addr;
  125. kmd_buf->handle = cmd_desc->mem_handle;
  126. kmd_buf->offset = cmd_desc->offset + packet->kmd_cmd_buf_offset;
  127. kmd_buf->size = cmd_desc->size - cmd_desc->length;
  128. kmd_buf->used_bytes = 0;
  129. return rc;
  130. }
  131. void cam_packet_dump_patch_info(struct cam_packet *packet,
  132. int32_t iommu_hdl, int32_t sec_mmu_hdl)
  133. {
  134. struct cam_patch_desc *patch_desc = NULL;
  135. dma_addr_t iova_addr;
  136. size_t dst_buf_len;
  137. size_t src_buf_size;
  138. int i, rc = 0;
  139. int32_t hdl;
  140. uintptr_t cpu_addr = 0;
  141. uint32_t *dst_cpu_addr;
  142. uint64_t value = 0;
  143. patch_desc = (struct cam_patch_desc *)
  144. ((uint32_t *) &packet->payload +
  145. packet->patch_offset/4);
  146. for (i = 0; i < packet->num_patches; i++) {
  147. hdl = cam_mem_is_secure_buf(patch_desc[i].src_buf_hdl) ?
  148. sec_mmu_hdl : iommu_hdl;
  149. rc = cam_mem_get_io_buf(patch_desc[i].src_buf_hdl,
  150. hdl, &iova_addr, &src_buf_size);
  151. if (rc < 0) {
  152. CAM_ERR(CAM_UTIL,
  153. "unable to get src buf address for hdl 0x%x",
  154. hdl);
  155. return;
  156. }
  157. rc = cam_mem_get_cpu_buf(patch_desc[i].dst_buf_hdl,
  158. &cpu_addr, &dst_buf_len);
  159. if (rc < 0 || !cpu_addr || (dst_buf_len == 0)) {
  160. CAM_ERR(CAM_UTIL, "unable to get dst buf address");
  161. return;
  162. }
  163. dst_cpu_addr = (uint32_t *)cpu_addr;
  164. dst_cpu_addr = (uint32_t *)((uint8_t *)dst_cpu_addr +
  165. patch_desc[i].dst_offset);
  166. value = *((uint64_t *)dst_cpu_addr);
  167. CAM_INFO(CAM_UTIL,
  168. "i = %d src_buf 0x%llx src_hdl 0x%x src_buf_with_offset 0x%llx size 0x%llx dst %p dst_offset %u dst_hdl 0x%x value 0x%llx",
  169. i, iova_addr, patch_desc[i].src_buf_hdl,
  170. (iova_addr + patch_desc[i].src_offset),
  171. src_buf_size, dst_cpu_addr,
  172. patch_desc[i].dst_offset,
  173. patch_desc[i].dst_buf_hdl, value);
  174. if (!(*dst_cpu_addr))
  175. CAM_ERR(CAM_ICP, "Null at dst addr %p", dst_cpu_addr);
  176. }
  177. }
  178. int cam_packet_util_process_patches(struct cam_packet *packet,
  179. int32_t iommu_hdl, int32_t sec_mmu_hdl)
  180. {
  181. struct cam_patch_desc *patch_desc = NULL;
  182. dma_addr_t iova_addr;
  183. uintptr_t cpu_addr = 0;
  184. uint32_t temp;
  185. uint32_t *dst_cpu_addr;
  186. uint32_t *src_buf_iova_addr;
  187. size_t dst_buf_len;
  188. size_t src_buf_size;
  189. int i;
  190. int rc = 0;
  191. int32_t hdl;
  192. /* process patch descriptor */
  193. patch_desc = (struct cam_patch_desc *)
  194. ((uint32_t *) &packet->payload +
  195. packet->patch_offset/4);
  196. CAM_DBG(CAM_UTIL, "packet = %pK patch_desc = %pK size = %lu",
  197. (void *)packet, (void *)patch_desc,
  198. sizeof(struct cam_patch_desc));
  199. for (i = 0; i < packet->num_patches; i++) {
  200. hdl = cam_mem_is_secure_buf(patch_desc[i].src_buf_hdl) ?
  201. sec_mmu_hdl : iommu_hdl;
  202. rc = cam_mem_get_io_buf(patch_desc[i].src_buf_hdl,
  203. hdl, &iova_addr, &src_buf_size);
  204. if (rc < 0) {
  205. CAM_ERR(CAM_UTIL, "unable to get src buf address");
  206. return rc;
  207. }
  208. src_buf_iova_addr = (uint32_t *)iova_addr;
  209. temp = iova_addr;
  210. rc = cam_mem_get_cpu_buf(patch_desc[i].dst_buf_hdl,
  211. &cpu_addr, &dst_buf_len);
  212. if (rc < 0 || !cpu_addr || (dst_buf_len == 0)) {
  213. CAM_ERR(CAM_UTIL, "unable to get dst buf address");
  214. return rc;
  215. }
  216. dst_cpu_addr = (uint32_t *)cpu_addr;
  217. CAM_DBG(CAM_UTIL, "i = %d patch info = %x %x %x %x", i,
  218. patch_desc[i].dst_buf_hdl, patch_desc[i].dst_offset,
  219. patch_desc[i].src_buf_hdl, patch_desc[i].src_offset);
  220. if ((size_t)patch_desc[i].src_offset >= src_buf_size) {
  221. CAM_ERR(CAM_UTIL,
  222. "Invalid src buf patch offset");
  223. return -EINVAL;
  224. }
  225. if ((dst_buf_len < sizeof(void *)) ||
  226. ((dst_buf_len - sizeof(void *)) <
  227. (size_t)patch_desc[i].dst_offset)) {
  228. CAM_ERR(CAM_UTIL,
  229. "Invalid dst buf patch offset");
  230. return -EINVAL;
  231. }
  232. dst_cpu_addr = (uint32_t *)((uint8_t *)dst_cpu_addr +
  233. patch_desc[i].dst_offset);
  234. temp += patch_desc[i].src_offset;
  235. *dst_cpu_addr = temp;
  236. CAM_DBG(CAM_UTIL,
  237. "patch is done for dst %pK with src %pK value %llx",
  238. dst_cpu_addr, src_buf_iova_addr,
  239. *((uint64_t *)dst_cpu_addr));
  240. }
  241. return rc;
  242. }
  243. int cam_packet_util_process_generic_cmd_buffer(
  244. struct cam_cmd_buf_desc *cmd_buf,
  245. cam_packet_generic_blob_handler blob_handler_cb, void *user_data)
  246. {
  247. int rc = 0;
  248. uintptr_t cpu_addr = 0;
  249. size_t buf_size;
  250. size_t remain_len = 0;
  251. uint32_t *blob_ptr;
  252. uint32_t blob_type, blob_size, blob_block_size, len_read;
  253. if (!cmd_buf || !blob_handler_cb) {
  254. CAM_ERR(CAM_UTIL, "Invalid args %pK %pK",
  255. cmd_buf, blob_handler_cb);
  256. return -EINVAL;
  257. }
  258. if (!cmd_buf->length || !cmd_buf->size) {
  259. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  260. cmd_buf->length, cmd_buf->size);
  261. return -EINVAL;
  262. }
  263. rc = cam_mem_get_cpu_buf(cmd_buf->mem_handle, &cpu_addr, &buf_size);
  264. if (rc || !cpu_addr || (buf_size == 0)) {
  265. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  266. rc, (void *)cpu_addr);
  267. return rc;
  268. }
  269. remain_len = buf_size;
  270. if ((buf_size < sizeof(uint32_t)) ||
  271. ((size_t)cmd_buf->offset > (buf_size - sizeof(uint32_t)))) {
  272. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  273. (size_t)cmd_buf->offset);
  274. return -EINVAL;
  275. }
  276. remain_len -= (size_t)cmd_buf->offset;
  277. if (remain_len < (size_t)cmd_buf->length) {
  278. CAM_ERR(CAM_UTIL, "Invalid length for cmd buf: %zu",
  279. (size_t)cmd_buf->length);
  280. return -EINVAL;
  281. }
  282. blob_ptr = (uint32_t *)(((uint8_t *)cpu_addr) +
  283. cmd_buf->offset);
  284. CAM_DBG(CAM_UTIL,
  285. "GenericCmdBuffer cpuaddr=%pK, blobptr=%pK, len=%d",
  286. (void *)cpu_addr, (void *)blob_ptr, cmd_buf->length);
  287. len_read = 0;
  288. while (len_read < cmd_buf->length) {
  289. blob_type =
  290. ((*blob_ptr) & CAM_GENERIC_BLOB_CMDBUFFER_TYPE_MASK) >>
  291. CAM_GENERIC_BLOB_CMDBUFFER_TYPE_SHIFT;
  292. blob_size =
  293. ((*blob_ptr) & CAM_GENERIC_BLOB_CMDBUFFER_SIZE_MASK) >>
  294. CAM_GENERIC_BLOB_CMDBUFFER_SIZE_SHIFT;
  295. blob_block_size = sizeof(uint32_t) +
  296. (((blob_size + sizeof(uint32_t) - 1) /
  297. sizeof(uint32_t)) * sizeof(uint32_t));
  298. CAM_DBG(CAM_UTIL,
  299. "Blob type=%d size=%d block_size=%d len_read=%d total=%d",
  300. blob_type, blob_size, blob_block_size, len_read,
  301. cmd_buf->length);
  302. if (len_read + blob_block_size > cmd_buf->length) {
  303. CAM_ERR(CAM_UTIL, "Invalid Blob %d %d %d %d",
  304. blob_type, blob_size, len_read,
  305. cmd_buf->length);
  306. rc = -EINVAL;
  307. goto end;
  308. }
  309. len_read += blob_block_size;
  310. rc = blob_handler_cb(user_data, blob_type, blob_size,
  311. (uint8_t *)(blob_ptr + 1));
  312. if (rc) {
  313. CAM_ERR(CAM_UTIL, "Error in handling blob type %d %d",
  314. blob_type, blob_size);
  315. goto end;
  316. }
  317. blob_ptr += (blob_block_size / sizeof(uint32_t));
  318. }
  319. end:
  320. return rc;
  321. }