cam_packet_util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, 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. #include "cam_common_util.h"
  11. #define CAM_UNIQUE_SRC_HDL_MAX 50
  12. #define CAM_PRESIL_UNIQUE_HDL_MAX 50
  13. struct cam_patch_unique_src_buf_tbl {
  14. int32_t hdl;
  15. dma_addr_t iova;
  16. size_t buf_size;
  17. uint32_t flags;
  18. };
  19. int cam_packet_util_get_cmd_mem_addr(int handle, uint32_t **buf_addr,
  20. size_t *len)
  21. {
  22. int rc = 0;
  23. uintptr_t kmd_buf_addr = 0;
  24. rc = cam_mem_get_cpu_buf(handle, &kmd_buf_addr, len);
  25. if (rc) {
  26. CAM_ERR(CAM_UTIL, "Unable to get the virtual address %d", rc);
  27. } else {
  28. if (kmd_buf_addr && *len) {
  29. *buf_addr = (uint32_t *)kmd_buf_addr;
  30. } else {
  31. CAM_ERR(CAM_UTIL, "Invalid addr and length :%zd", *len);
  32. rc = -ENOMEM;
  33. }
  34. }
  35. return rc;
  36. }
  37. int cam_packet_util_validate_cmd_desc(struct cam_cmd_buf_desc *cmd_desc)
  38. {
  39. if ((cmd_desc->length > cmd_desc->size) ||
  40. (cmd_desc->mem_handle <= 0)) {
  41. CAM_ERR(CAM_UTIL, "invalid cmd arg %d %d %d %d",
  42. cmd_desc->offset, cmd_desc->length,
  43. cmd_desc->mem_handle, cmd_desc->size);
  44. return -EINVAL;
  45. }
  46. return 0;
  47. }
  48. int cam_packet_util_validate_packet(struct cam_packet *packet,
  49. size_t remain_len)
  50. {
  51. size_t sum_cmd_desc = 0;
  52. size_t sum_io_cfgs = 0;
  53. size_t sum_patch_desc = 0;
  54. size_t pkt_wo_payload = 0;
  55. if (!packet)
  56. return -EINVAL;
  57. if ((size_t)packet->header.size > remain_len) {
  58. CAM_ERR(CAM_UTIL,
  59. "Invalid packet size: %zu, CPU buf length: %zu",
  60. (size_t)packet->header.size, remain_len);
  61. return -EINVAL;
  62. }
  63. CAM_DBG(CAM_UTIL, "num cmd buf:%d num of io config:%d kmd buf index:%d",
  64. packet->num_cmd_buf, packet->num_io_configs,
  65. packet->kmd_cmd_buf_index);
  66. sum_cmd_desc = packet->num_cmd_buf * sizeof(struct cam_cmd_buf_desc);
  67. sum_io_cfgs = packet->num_io_configs * sizeof(struct cam_buf_io_cfg);
  68. sum_patch_desc = packet->num_patches * sizeof(struct cam_patch_desc);
  69. pkt_wo_payload = offsetof(struct cam_packet, payload);
  70. if ((!packet->header.size) ||
  71. ((pkt_wo_payload + (size_t)packet->cmd_buf_offset +
  72. sum_cmd_desc) > (size_t)packet->header.size) ||
  73. ((pkt_wo_payload + (size_t)packet->io_configs_offset +
  74. sum_io_cfgs) > (size_t)packet->header.size) ||
  75. ((pkt_wo_payload + (size_t)packet->patch_offset +
  76. sum_patch_desc) > (size_t)packet->header.size)) {
  77. CAM_ERR(CAM_UTIL, "params not within mem len:%zu %zu %zu %zu",
  78. (size_t)packet->header.size, sum_cmd_desc,
  79. sum_io_cfgs, sum_patch_desc);
  80. return -EINVAL;
  81. }
  82. return 0;
  83. }
  84. int cam_packet_util_get_kmd_buffer(struct cam_packet *packet,
  85. struct cam_kmd_buf_info *kmd_buf)
  86. {
  87. int rc = 0;
  88. size_t len = 0;
  89. size_t remain_len = 0;
  90. struct cam_cmd_buf_desc *cmd_desc;
  91. uint32_t *cpu_addr;
  92. if (!packet || !kmd_buf) {
  93. CAM_ERR(CAM_UTIL, "Invalid arg %pK %pK", packet, kmd_buf);
  94. return -EINVAL;
  95. }
  96. if ((packet->kmd_cmd_buf_index < 0) ||
  97. (packet->kmd_cmd_buf_index >= packet->num_cmd_buf)) {
  98. CAM_ERR(CAM_UTIL, "Invalid kmd buf index: %d",
  99. packet->kmd_cmd_buf_index);
  100. return -EINVAL;
  101. }
  102. /* Take first command descriptor and add offset to it for kmd*/
  103. cmd_desc = (struct cam_cmd_buf_desc *) ((uint8_t *)
  104. &packet->payload + packet->cmd_buf_offset);
  105. cmd_desc += packet->kmd_cmd_buf_index;
  106. rc = cam_packet_util_validate_cmd_desc(cmd_desc);
  107. if (rc)
  108. return rc;
  109. rc = cam_packet_util_get_cmd_mem_addr(cmd_desc->mem_handle, &cpu_addr,
  110. &len);
  111. if (rc)
  112. return rc;
  113. remain_len = len;
  114. if (((size_t)cmd_desc->offset >= len) ||
  115. ((size_t)cmd_desc->size > (len - (size_t)cmd_desc->offset))) {
  116. CAM_ERR(CAM_UTIL, "invalid memory len:%zd and cmd desc size:%d",
  117. len, cmd_desc->size);
  118. return -EINVAL;
  119. }
  120. remain_len -= (size_t)cmd_desc->offset;
  121. if ((size_t)packet->kmd_cmd_buf_offset >= remain_len) {
  122. CAM_ERR(CAM_UTIL, "Invalid kmd cmd buf offset: %zu",
  123. (size_t)packet->kmd_cmd_buf_offset);
  124. return -EINVAL;
  125. }
  126. cpu_addr += (cmd_desc->offset / 4) + (packet->kmd_cmd_buf_offset / 4);
  127. CAM_DBG(CAM_UTIL, "total size %d, cmd size: %d, KMD buffer size: %d",
  128. cmd_desc->size, cmd_desc->length,
  129. cmd_desc->size - cmd_desc->length);
  130. CAM_DBG(CAM_UTIL, "hdl 0x%x, cmd offset %d, kmd offset %d, addr 0x%pK",
  131. cmd_desc->mem_handle, cmd_desc->offset,
  132. packet->kmd_cmd_buf_offset, cpu_addr);
  133. kmd_buf->cpu_addr = cpu_addr;
  134. kmd_buf->handle = cmd_desc->mem_handle;
  135. kmd_buf->offset = cmd_desc->offset + packet->kmd_cmd_buf_offset;
  136. kmd_buf->size = cmd_desc->size - cmd_desc->length;
  137. kmd_buf->used_bytes = 0;
  138. return rc;
  139. }
  140. void cam_packet_dump_patch_info(struct cam_packet *packet,
  141. int32_t iommu_hdl, int32_t sec_mmu_hdl)
  142. {
  143. struct cam_patch_desc *patch_desc = NULL;
  144. dma_addr_t iova_addr;
  145. size_t dst_buf_len;
  146. size_t src_buf_size;
  147. int i, rc = 0;
  148. int32_t hdl;
  149. uintptr_t cpu_addr = 0;
  150. uint32_t *dst_cpu_addr;
  151. uint32_t flags;
  152. uint64_t value = 0;
  153. patch_desc = (struct cam_patch_desc *)
  154. ((uint32_t *) &packet->payload +
  155. packet->patch_offset/4);
  156. CAM_INFO(CAM_UTIL, "Total num of patches : %d",
  157. packet->num_patches);
  158. for (i = 0; i < packet->num_patches; i++) {
  159. hdl = cam_mem_is_secure_buf(patch_desc[i].src_buf_hdl) ?
  160. sec_mmu_hdl : iommu_hdl;
  161. rc = cam_mem_get_io_buf(patch_desc[i].src_buf_hdl,
  162. hdl, &iova_addr, &src_buf_size, &flags);
  163. if (rc < 0) {
  164. CAM_ERR(CAM_UTIL,
  165. "unable to get src buf address for hdl 0x%x",
  166. hdl);
  167. return;
  168. }
  169. rc = cam_mem_get_cpu_buf(patch_desc[i].dst_buf_hdl,
  170. &cpu_addr, &dst_buf_len);
  171. if (rc < 0 || !cpu_addr || (dst_buf_len == 0)) {
  172. CAM_ERR(CAM_UTIL, "unable to get dst buf address");
  173. return;
  174. }
  175. dst_cpu_addr = (uint32_t *)cpu_addr;
  176. dst_cpu_addr = (uint32_t *)((uint8_t *)dst_cpu_addr +
  177. patch_desc[i].dst_offset);
  178. value = *((uint64_t *)dst_cpu_addr);
  179. CAM_INFO(CAM_UTIL,
  180. "i = %d src_buf 0x%llx src_hdl 0x%x src_buf_with_offset 0x%llx src_size 0x%llx src_flags: %x dst %p dst_offset %u dst_hdl 0x%x value 0x%llx",
  181. i, iova_addr, patch_desc[i].src_buf_hdl,
  182. (iova_addr + patch_desc[i].src_offset),
  183. src_buf_size, flags, dst_cpu_addr,
  184. patch_desc[i].dst_offset,
  185. patch_desc[i].dst_buf_hdl, value);
  186. if (!(*dst_cpu_addr))
  187. CAM_ERR(CAM_ICP, "Null at dst addr %p", dst_cpu_addr);
  188. }
  189. }
  190. static int cam_packet_util_get_patch_iova(
  191. struct cam_patch_unique_src_buf_tbl *tbl,
  192. int32_t hdl, uint32_t buf_hdl, dma_addr_t *iova,
  193. size_t *buf_size, uint32_t *flags)
  194. {
  195. int idx = 0;
  196. int rc = 0;
  197. size_t src_buf_size;
  198. dma_addr_t iova_addr;
  199. bool is_found = false;
  200. for (idx = 0; idx < CAM_UNIQUE_SRC_HDL_MAX; idx++) {
  201. if (buf_hdl == tbl[idx].hdl) {
  202. CAM_DBG(CAM_UTIL,
  203. "Matched entry for src_buf_hdl: 0x%x with src_hdl[%d]: 0x%x",
  204. buf_hdl, idx, tbl[idx].hdl);
  205. *iova = tbl[idx].iova;
  206. *buf_size = tbl[idx].buf_size;
  207. *flags = tbl[idx].flags;
  208. is_found = true;
  209. break;
  210. } else if ((tbl[idx].hdl == 0) || (tbl[idx].iova == 0)) {
  211. CAM_DBG(CAM_UTIL, "New src handle detected 0x%x", buf_hdl);
  212. is_found = false;
  213. break;
  214. }
  215. CAM_DBG(CAM_UTIL,
  216. "Index: %d is filled with differnt src_hdl: 0x%x",
  217. idx, buf_hdl);
  218. }
  219. if (!is_found) {
  220. CAM_DBG(CAM_UTIL, "src_hdl 0x%x not found in table entries",
  221. buf_hdl);
  222. rc = cam_mem_get_io_buf(buf_hdl, hdl, &iova_addr, &src_buf_size, flags);
  223. if (rc < 0) {
  224. CAM_ERR(CAM_UTIL,
  225. "unable to get iova for src_hdl: 0x%x",
  226. buf_hdl);
  227. return rc;
  228. }
  229. /* Update the table entry with unique src buf handle */
  230. if (idx < CAM_UNIQUE_SRC_HDL_MAX && tbl[idx].hdl == 0) {
  231. tbl[idx].buf_size = src_buf_size;
  232. tbl[idx].iova = iova_addr;
  233. tbl[idx].hdl = buf_hdl;
  234. tbl[idx].flags = *flags;
  235. CAM_DBG(CAM_UTIL,
  236. "Updated table index: %d with src_buf_hdl: 0x%x flags: %x",
  237. idx, tbl[idx].hdl, *flags);
  238. }
  239. *iova = iova_addr;
  240. *buf_size = src_buf_size;
  241. }
  242. return rc;
  243. }
  244. int cam_packet_util_process_patches(struct cam_packet *packet,
  245. int32_t iommu_hdl, int32_t sec_mmu_hdl)
  246. {
  247. struct cam_patch_desc *patch_desc = NULL;
  248. dma_addr_t iova_addr;
  249. uintptr_t cpu_addr = 0;
  250. dma_addr_t temp;
  251. uint32_t *dst_cpu_addr;
  252. size_t dst_buf_len;
  253. size_t src_buf_size;
  254. int i = 0;
  255. int rc = 0;
  256. uint32_t flags = 0;
  257. int32_t hdl;
  258. struct cam_patch_unique_src_buf_tbl
  259. tbl[CAM_UNIQUE_SRC_HDL_MAX];
  260. memset(tbl, 0, CAM_UNIQUE_SRC_HDL_MAX *
  261. sizeof(struct cam_patch_unique_src_buf_tbl));
  262. /* process patch descriptor */
  263. patch_desc = (struct cam_patch_desc *)
  264. ((uint32_t *) &packet->payload +
  265. packet->patch_offset/4);
  266. CAM_DBG(CAM_UTIL, "packet = %pK patch_desc = %pK size = %lu",
  267. (void *)packet, (void *)patch_desc,
  268. sizeof(struct cam_patch_desc));
  269. for (i = 0; i < packet->num_patches; i++) {
  270. hdl = cam_mem_is_secure_buf(patch_desc[i].src_buf_hdl) ?
  271. sec_mmu_hdl : iommu_hdl;
  272. rc = cam_packet_util_get_patch_iova(&tbl[0], hdl,
  273. patch_desc[i].src_buf_hdl, &iova_addr, &src_buf_size, &flags);
  274. if (rc) {
  275. CAM_ERR(CAM_UTIL,
  276. "get_iova failed for patch[%d], src_buf_hdl: 0x%x: rc: %d",
  277. i, patch_desc[i].src_buf_hdl, rc);
  278. return rc;
  279. }
  280. if ((size_t)patch_desc[i].src_offset >= src_buf_size) {
  281. CAM_ERR(CAM_UTIL,
  282. "Invalid src buf patch offset: patch:src_offset: 0x%x, src_buf_size: %zu",
  283. patch_desc[i].src_offset, src_buf_size);
  284. return -EINVAL;
  285. }
  286. temp = iova_addr;
  287. rc = cam_mem_get_cpu_buf(patch_desc[i].dst_buf_hdl,
  288. &cpu_addr, &dst_buf_len);
  289. if (rc < 0 || !cpu_addr || (dst_buf_len == 0)) {
  290. CAM_ERR(CAM_UTIL, "unable to get dst buf address");
  291. return rc;
  292. }
  293. dst_cpu_addr = (uint32_t *)cpu_addr;
  294. CAM_DBG(CAM_UTIL, "i = %d patch info = %x %x %x %x", i,
  295. patch_desc[i].dst_buf_hdl, patch_desc[i].dst_offset,
  296. patch_desc[i].src_buf_hdl, patch_desc[i].src_offset);
  297. if ((dst_buf_len < sizeof(void *)) ||
  298. ((dst_buf_len - sizeof(void *)) <
  299. (size_t)patch_desc[i].dst_offset)) {
  300. CAM_ERR(CAM_UTIL,
  301. "Invalid dst buf patch offset");
  302. return -EINVAL;
  303. }
  304. dst_cpu_addr = (uint32_t *)((uint8_t *)dst_cpu_addr +
  305. patch_desc[i].dst_offset);
  306. temp += patch_desc[i].src_offset;
  307. if ((flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) ||
  308. (flags & CAM_MEM_FLAG_CMD_BUF_TYPE))
  309. *dst_cpu_addr = temp;
  310. else
  311. *dst_cpu_addr = cam_smmu_is_expanded_memory() ?
  312. CAM_36BIT_INTF_GET_IOVA_BASE(temp) : temp;
  313. CAM_DBG(CAM_UTIL,
  314. "patch is done for dst %pk with src 0x%llx value 0x%llx",
  315. dst_cpu_addr, iova_addr, *((uint64_t *)dst_cpu_addr));
  316. }
  317. return rc;
  318. }
  319. int cam_packet_util_process_generic_cmd_buffer(
  320. struct cam_cmd_buf_desc *cmd_buf,
  321. cam_packet_generic_blob_handler blob_handler_cb, void *user_data)
  322. {
  323. int rc = 0;
  324. uintptr_t cpu_addr = 0;
  325. size_t buf_size;
  326. size_t remain_len = 0;
  327. uint32_t *blob_ptr;
  328. uint32_t blob_type, blob_size, blob_block_size, len_read;
  329. if (!cmd_buf || !blob_handler_cb) {
  330. CAM_ERR(CAM_UTIL, "Invalid args %pK %pK",
  331. cmd_buf, blob_handler_cb);
  332. return -EINVAL;
  333. }
  334. if (!cmd_buf->length || !cmd_buf->size) {
  335. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  336. cmd_buf->length, cmd_buf->size);
  337. return -EINVAL;
  338. }
  339. rc = cam_mem_get_cpu_buf(cmd_buf->mem_handle, &cpu_addr, &buf_size);
  340. if (rc || !cpu_addr || (buf_size == 0)) {
  341. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  342. rc, (void *)cpu_addr);
  343. return rc;
  344. }
  345. remain_len = buf_size;
  346. if ((buf_size < sizeof(uint32_t)) ||
  347. ((size_t)cmd_buf->offset > (buf_size - sizeof(uint32_t)))) {
  348. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  349. (size_t)cmd_buf->offset);
  350. return -EINVAL;
  351. }
  352. remain_len -= (size_t)cmd_buf->offset;
  353. if (remain_len < (size_t)cmd_buf->length) {
  354. CAM_ERR(CAM_UTIL, "Invalid length for cmd buf: %zu",
  355. (size_t)cmd_buf->length);
  356. return -EINVAL;
  357. }
  358. blob_ptr = (uint32_t *)(((uint8_t *)cpu_addr) +
  359. cmd_buf->offset);
  360. CAM_DBG(CAM_UTIL,
  361. "GenericCmdBuffer cpuaddr=%pK, blobptr=%pK, len=%d",
  362. (void *)cpu_addr, (void *)blob_ptr, cmd_buf->length);
  363. len_read = 0;
  364. while (len_read < cmd_buf->length) {
  365. blob_type =
  366. ((*blob_ptr) & CAM_GENERIC_BLOB_CMDBUFFER_TYPE_MASK) >>
  367. CAM_GENERIC_BLOB_CMDBUFFER_TYPE_SHIFT;
  368. blob_size =
  369. ((*blob_ptr) & CAM_GENERIC_BLOB_CMDBUFFER_SIZE_MASK) >>
  370. CAM_GENERIC_BLOB_CMDBUFFER_SIZE_SHIFT;
  371. blob_block_size = sizeof(uint32_t) +
  372. (((blob_size + sizeof(uint32_t) - 1) /
  373. sizeof(uint32_t)) * sizeof(uint32_t));
  374. CAM_DBG(CAM_UTIL,
  375. "Blob type=%d size=%d block_size=%d len_read=%d total=%d",
  376. blob_type, blob_size, blob_block_size, len_read,
  377. cmd_buf->length);
  378. if (len_read + blob_block_size > cmd_buf->length) {
  379. CAM_ERR(CAM_UTIL, "Invalid Blob %d %d %d %d",
  380. blob_type, blob_size, len_read,
  381. cmd_buf->length);
  382. rc = -EINVAL;
  383. goto end;
  384. }
  385. len_read += blob_block_size;
  386. rc = blob_handler_cb(user_data, blob_type, blob_size,
  387. (uint8_t *)(blob_ptr + 1));
  388. if (rc) {
  389. CAM_ERR(CAM_UTIL, "Error in handling blob type %d %d",
  390. blob_type, blob_size);
  391. goto end;
  392. }
  393. blob_ptr += (blob_block_size / sizeof(uint32_t));
  394. }
  395. end:
  396. return rc;
  397. }
  398. int cam_presil_retrieve_buffers_from_packet(struct cam_packet *packet, int iommu_hdl,
  399. int out_res_id)
  400. {
  401. int rc = 0, i, j;
  402. struct cam_buf_io_cfg *io_cfg = NULL;
  403. dma_addr_t io_addr[CAM_PACKET_MAX_PLANES];
  404. size_t size;
  405. if (!packet || (iommu_hdl < 0)) {
  406. CAM_ERR(CAM_PRESIL, "Invalid params packet %pK iommu_hdl: %d", packet, iommu_hdl);
  407. return -EINVAL;
  408. }
  409. CAM_DBG(CAM_PRESIL, "Retrieving output buffer corresponding to res: 0x%x", out_res_id);
  410. io_cfg = (struct cam_buf_io_cfg *)((uint8_t *)&packet->payload + packet->io_configs_offset);
  411. for (i = 0; i < packet->num_io_configs; i++) {
  412. if ((io_cfg[i].direction != CAM_BUF_OUTPUT) ||
  413. (io_cfg[i].resource_type != out_res_id))
  414. continue;
  415. memset(io_addr, 0, sizeof(io_addr));
  416. for (j = 0; j < CAM_PACKET_MAX_PLANES; j++) {
  417. if (!io_cfg[i].mem_handle[j])
  418. break;
  419. rc = cam_mem_get_io_buf(io_cfg[i].mem_handle[j], iommu_hdl, &io_addr[j],
  420. &size, NULL);
  421. if (rc) {
  422. CAM_ERR(CAM_PRESIL, "no io addr for plane%d", j);
  423. rc = -ENOMEM;
  424. return rc;
  425. }
  426. /* For presil, address should be within 32 bit */
  427. if (io_addr[j] >> 32) {
  428. CAM_ERR(CAM_PRESIL,
  429. "Invalid address, presil mapped address should be 32 bit");
  430. rc = -EINVAL;
  431. return rc;
  432. }
  433. CAM_INFO(CAM_PRESIL,
  434. "Retrieving IO CFG buffer:%d addr: 0x%x offset 0x%x res_id: 0x%x",
  435. io_cfg[i].mem_handle[j], io_addr[j], io_cfg[i].offsets[j],
  436. io_cfg[i].resource_type);
  437. cam_mem_mgr_retrieve_buffer_from_presil(io_cfg[i].mem_handle[j], size,
  438. io_cfg[i].offsets[j], iommu_hdl);
  439. }
  440. }
  441. return rc;
  442. }
  443. static void cam_presil_add_unique_buf_hdl_to_list(int32_t buf_hdl,
  444. int32_t *hdl_list, int *num_hdls, int max_handles)
  445. {
  446. int k;
  447. bool hdl_found = false;
  448. if (!buf_hdl)
  449. return;
  450. if (*num_hdls >= max_handles) {
  451. CAM_ERR(CAM_PRESIL, "Failed to add entry num_hdls: %d max_handles:%d", *num_hdls,
  452. max_handles);
  453. return;
  454. }
  455. for (k = 0; k < *num_hdls; k++) {
  456. if (hdl_list[k] == buf_hdl) {
  457. hdl_found = true;
  458. break;
  459. }
  460. }
  461. if (!hdl_found)
  462. hdl_list[(*num_hdls)++] = buf_hdl;
  463. }
  464. int cam_presil_send_buffers_from_packet(struct cam_packet *packet, int img_iommu_hdl,
  465. int cdm_iommu_hdl)
  466. {
  467. struct cam_buf_io_cfg *io_cfg = NULL;
  468. struct cam_cmd_buf_desc *cmd_desc = NULL;
  469. struct cam_patch_desc *patch_desc = NULL;
  470. int i, j, rc = 0;
  471. int32_t unique_img_buffers[CAM_PRESIL_UNIQUE_HDL_MAX] = {0};
  472. int32_t unique_cmd_buffers[CAM_PRESIL_UNIQUE_HDL_MAX] = {0};
  473. int num_img_handles = 0, num_cmd_handles = 0;
  474. if(!packet) {
  475. CAM_ERR(CAM_PRESIL, "Packet is NULL");
  476. return -EINVAL;
  477. }
  478. if (img_iommu_hdl == -1) {
  479. goto send_cmd_buffers;
  480. }
  481. /* Adding IO config buffer handles to list*/
  482. io_cfg = (struct cam_buf_io_cfg *)((uint8_t *)&packet->payload + packet->io_configs_offset);
  483. for (i = 0; i < packet->num_io_configs; i++) {
  484. if (io_cfg[i].direction == CAM_BUF_OUTPUT)
  485. continue;
  486. for (j = 0; j < CAM_PACKET_MAX_PLANES; j++) {
  487. if (!io_cfg[i].mem_handle[j])
  488. break;
  489. CAM_DBG(CAM_PRESIL, "Adding IO CFG buffer:%d", io_cfg[i].mem_handle[j]);
  490. cam_presil_add_unique_buf_hdl_to_list(io_cfg[i].mem_handle[j],
  491. unique_img_buffers, &num_img_handles, CAM_PRESIL_UNIQUE_HDL_MAX);
  492. }
  493. }
  494. for (i = 0; i < num_img_handles; i++) {
  495. CAM_DBG(CAM_PRESIL, "Sending Image buffer i:%d mem_handle:%d", i,
  496. unique_img_buffers[i]);
  497. rc = cam_mem_mgr_send_buffer_to_presil(img_iommu_hdl,
  498. unique_img_buffers[i]);
  499. if (rc) {
  500. CAM_ERR(CAM_PRESIL, "Failed to send buffer i:%d mem_handle:%d rc:%d",
  501. i, unique_img_buffers[i], rc);
  502. return rc;
  503. }
  504. }
  505. send_cmd_buffers:
  506. if (cdm_iommu_hdl == -1) {
  507. goto end;
  508. }
  509. /* Adding CMD buffer handles to list*/
  510. cmd_desc = (struct cam_cmd_buf_desc *) ((uint8_t *)&packet->payload +
  511. packet->cmd_buf_offset);
  512. for (i = 0; i < packet->num_cmd_buf; i++) {
  513. CAM_DBG(CAM_PRESIL, "Adding CMD buffer:%d", cmd_desc[i].mem_handle);
  514. cam_presil_add_unique_buf_hdl_to_list(cmd_desc[i].mem_handle,
  515. unique_cmd_buffers, &num_cmd_handles, CAM_PRESIL_UNIQUE_HDL_MAX);
  516. }
  517. /* Adding Patch src buffer handles to list */
  518. patch_desc = (struct cam_patch_desc *) ((uint8_t *)&packet->payload + packet->patch_offset);
  519. for (i = 0; i < packet->num_patches; i++) {
  520. CAM_DBG(CAM_PRESIL, "Adding Patch src buffer:%d", patch_desc[i].src_buf_hdl);
  521. cam_presil_add_unique_buf_hdl_to_list(patch_desc[i].src_buf_hdl,
  522. unique_cmd_buffers, &num_cmd_handles, CAM_PRESIL_UNIQUE_HDL_MAX);
  523. }
  524. for (i = 0; i < num_cmd_handles; i++) {
  525. CAM_DBG(CAM_PRESIL, "Sending Command buffer i:%d mem_handle:%d", i,
  526. unique_cmd_buffers[i]);
  527. rc = cam_mem_mgr_send_buffer_to_presil(cdm_iommu_hdl,
  528. unique_cmd_buffers[i]);
  529. if (rc) {
  530. CAM_ERR(CAM_PRESIL, "Failed to send buffer i:%d mem_handle:%d rc:%d",
  531. i, unique_cmd_buffers[i], rc);
  532. return rc;
  533. }
  534. }
  535. end:
  536. return rc;
  537. }