cam_packet_util.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/types.h>
  7. #include <linux/slab.h>
  8. #include "cam_mem_mgr.h"
  9. #include "cam_packet_util.h"
  10. #include "cam_debug_util.h"
  11. #include "cam_common_util.h"
  12. #define CAM_UNIQUE_SRC_HDL_MAX 50
  13. #define CAM_PRESIL_UNIQUE_HDL_MAX 50
  14. struct cam_patch_unique_src_buf_tbl {
  15. int32_t hdl;
  16. dma_addr_t iova;
  17. size_t buf_size;
  18. uint32_t flags;
  19. };
  20. int cam_packet_util_get_cmd_mem_addr(int handle, uint32_t **buf_addr,
  21. size_t *len)
  22. {
  23. int rc = 0;
  24. uintptr_t kmd_buf_addr = 0;
  25. rc = cam_mem_get_cpu_buf(handle, &kmd_buf_addr, len);
  26. if (rc) {
  27. CAM_ERR(CAM_UTIL, "Unable to get the virtual address %d", rc);
  28. } else {
  29. if (kmd_buf_addr && *len) {
  30. *buf_addr = (uint32_t *)kmd_buf_addr;
  31. } else {
  32. CAM_ERR(CAM_UTIL, "Invalid addr and length :%zd", *len);
  33. rc = -ENOMEM;
  34. }
  35. }
  36. return rc;
  37. }
  38. int cam_packet_util_validate_cmd_desc(struct cam_cmd_buf_desc *cmd_desc)
  39. {
  40. if ((cmd_desc->length > cmd_desc->size) ||
  41. (cmd_desc->mem_handle <= 0)) {
  42. CAM_ERR(CAM_UTIL, "invalid cmd arg %d %d %d %d",
  43. cmd_desc->offset, cmd_desc->length,
  44. cmd_desc->mem_handle, cmd_desc->size);
  45. return -EINVAL;
  46. }
  47. return 0;
  48. }
  49. int cam_packet_util_validate_packet(struct cam_packet *packet,
  50. size_t remain_len)
  51. {
  52. size_t sum_cmd_desc = 0;
  53. size_t sum_io_cfgs = 0;
  54. size_t sum_patch_desc = 0;
  55. size_t pkt_wo_payload = 0;
  56. if (!packet)
  57. return -EINVAL;
  58. if ((size_t)packet->header.size > remain_len) {
  59. CAM_ERR(CAM_UTIL,
  60. "Invalid packet size: %zu, CPU buf length: %zu",
  61. (size_t)packet->header.size, remain_len);
  62. return -EINVAL;
  63. }
  64. CAM_DBG(CAM_UTIL, "num cmd buf:%d num of io config:%d kmd buf index:%d",
  65. packet->num_cmd_buf, packet->num_io_configs,
  66. packet->kmd_cmd_buf_index);
  67. sum_cmd_desc = packet->num_cmd_buf * sizeof(struct cam_cmd_buf_desc);
  68. sum_io_cfgs = packet->num_io_configs * sizeof(struct cam_buf_io_cfg);
  69. sum_patch_desc = packet->num_patches * sizeof(struct cam_patch_desc);
  70. pkt_wo_payload = offsetof(struct cam_packet, payload);
  71. if ((!packet->header.size) ||
  72. ((pkt_wo_payload + (size_t)packet->cmd_buf_offset +
  73. sum_cmd_desc) > (size_t)packet->header.size) ||
  74. ((pkt_wo_payload + (size_t)packet->io_configs_offset +
  75. sum_io_cfgs) > (size_t)packet->header.size) ||
  76. ((pkt_wo_payload + (size_t)packet->patch_offset +
  77. sum_patch_desc) > (size_t)packet->header.size)) {
  78. CAM_ERR(CAM_UTIL, "params not within mem len:%zu %zu %zu %zu",
  79. (size_t)packet->header.size, sum_cmd_desc,
  80. sum_io_cfgs, sum_patch_desc);
  81. return -EINVAL;
  82. }
  83. return 0;
  84. }
  85. int cam_packet_util_get_kmd_buffer(struct cam_packet *packet,
  86. struct cam_kmd_buf_info *kmd_buf)
  87. {
  88. int rc = 0;
  89. size_t len = 0;
  90. size_t remain_len = 0;
  91. struct cam_cmd_buf_desc *cmd_desc;
  92. uint32_t *cpu_addr;
  93. if (!packet || !kmd_buf) {
  94. CAM_ERR(CAM_UTIL, "Invalid arg %pK %pK", packet, kmd_buf);
  95. return -EINVAL;
  96. }
  97. if ((packet->kmd_cmd_buf_index < 0) ||
  98. (packet->kmd_cmd_buf_index >= packet->num_cmd_buf)) {
  99. CAM_ERR(CAM_UTIL, "Invalid kmd buf index: %d",
  100. packet->kmd_cmd_buf_index);
  101. return -EINVAL;
  102. }
  103. /* Take first command descriptor and add offset to it for kmd*/
  104. cmd_desc = (struct cam_cmd_buf_desc *) ((uint8_t *)
  105. &packet->payload + packet->cmd_buf_offset);
  106. cmd_desc += packet->kmd_cmd_buf_index;
  107. rc = cam_packet_util_validate_cmd_desc(cmd_desc);
  108. if (rc)
  109. return rc;
  110. rc = cam_packet_util_get_cmd_mem_addr(cmd_desc->mem_handle, &cpu_addr,
  111. &len);
  112. if (rc)
  113. return rc;
  114. remain_len = len;
  115. if (((size_t)cmd_desc->offset >= len) ||
  116. ((size_t)cmd_desc->size > (len - (size_t)cmd_desc->offset))) {
  117. CAM_ERR(CAM_UTIL, "invalid memory len:%zd and cmd desc size:%d",
  118. len, cmd_desc->size);
  119. return -EINVAL;
  120. }
  121. remain_len -= (size_t)cmd_desc->offset;
  122. if ((size_t)packet->kmd_cmd_buf_offset >= remain_len) {
  123. CAM_ERR(CAM_UTIL, "Invalid kmd cmd buf offset: %zu",
  124. (size_t)packet->kmd_cmd_buf_offset);
  125. return -EINVAL;
  126. }
  127. cpu_addr += (cmd_desc->offset / 4) + (packet->kmd_cmd_buf_offset / 4);
  128. CAM_DBG(CAM_UTIL, "total size %d, cmd size: %d, KMD buffer size: %d",
  129. cmd_desc->size, cmd_desc->length,
  130. cmd_desc->size - cmd_desc->length);
  131. CAM_DBG(CAM_UTIL, "hdl 0x%x, cmd offset %d, kmd offset %d, addr 0x%pK",
  132. cmd_desc->mem_handle, cmd_desc->offset,
  133. packet->kmd_cmd_buf_offset, cpu_addr);
  134. kmd_buf->cpu_addr = cpu_addr;
  135. kmd_buf->handle = cmd_desc->mem_handle;
  136. kmd_buf->offset = cmd_desc->offset + packet->kmd_cmd_buf_offset;
  137. kmd_buf->size = cmd_desc->size - cmd_desc->length;
  138. kmd_buf->used_bytes = 0;
  139. return rc;
  140. }
  141. void cam_packet_util_dump_patch_info(struct cam_packet *packet,
  142. int32_t iommu_hdl, int32_t sec_iommu_hdl, struct cam_hw_dump_pf_args *pf_args)
  143. {
  144. struct cam_patch_desc *patch_desc = NULL;
  145. struct cam_context_pf_info *pf_context_info = NULL;
  146. dma_addr_t iova_addr;
  147. size_t dst_buf_len;
  148. size_t src_buf_size;
  149. int i, rc = 0;
  150. int32_t hdl;
  151. uintptr_t cpu_addr = 0;
  152. uint32_t *dst_cpu_addr;
  153. uint32_t flags, buf_fd;
  154. uint32_t value = 0;
  155. if (!packet) {
  156. CAM_ERR(CAM_UTIL, "Invalid packet");
  157. return;
  158. }
  159. patch_desc = (struct cam_patch_desc *)
  160. ((uint32_t *) &packet->payload +
  161. packet->patch_offset/4);
  162. if (pf_args) {
  163. pf_context_info = &(pf_args->pf_context_info);
  164. buf_fd = pf_args->pf_smmu_info->buf_info;
  165. }
  166. CAM_INFO(CAM_UTIL, "Total num of patches : %d",
  167. packet->num_patches);
  168. for (i = 0; i < packet->num_patches; i++) {
  169. hdl = cam_mem_is_secure_buf(patch_desc[i].src_buf_hdl) ?
  170. sec_iommu_hdl : iommu_hdl;
  171. rc = cam_mem_get_io_buf(patch_desc[i].src_buf_hdl,
  172. hdl, &iova_addr, &src_buf_size, &flags);
  173. if (rc < 0) {
  174. CAM_ERR(CAM_UTIL,
  175. "unable to get src buf address for hdl 0x%x",
  176. hdl);
  177. return;
  178. }
  179. if (pf_args &&
  180. GET_FD_FROM_HANDLE(patch_desc[i].src_buf_hdl) == buf_fd &&
  181. pf_context_info->mem_type == CAM_FAULT_BUF_NOT_FOUND) {
  182. /* found PF at this hdl */
  183. pf_context_info->mem_type = CAM_FAULT_PATCH_BUF;
  184. pf_context_info->patch_idx = i;
  185. pf_context_info->buf_hdl = patch_desc[i].src_buf_hdl;
  186. pf_context_info->offset = patch_desc[i].src_offset;
  187. pf_context_info->mem_flag = flags;
  188. pf_context_info->delta =
  189. CAM_SMMU_GET_IOVA_DELTA(pf_args->pf_smmu_info->iova, iova_addr);
  190. pf_context_info->req_id = packet->header.request_id;
  191. pf_context_info->ctx_found = true;
  192. CAM_ERR(CAM_UTIL, "Found PF at patch: %d src buf hdl: 0x%llx",
  193. i, patch_desc[i].src_buf_hdl);
  194. }
  195. rc = cam_mem_get_cpu_buf(patch_desc[i].dst_buf_hdl,
  196. &cpu_addr, &dst_buf_len);
  197. if (rc < 0 || !cpu_addr || (dst_buf_len == 0)) {
  198. CAM_ERR(CAM_UTIL, "unable to get dst buf address");
  199. return;
  200. }
  201. dst_cpu_addr = (uint32_t *)cpu_addr;
  202. dst_cpu_addr = (uint32_t *)((uint8_t *)dst_cpu_addr +
  203. patch_desc[i].dst_offset);
  204. value = *dst_cpu_addr;
  205. CAM_INFO(CAM_UTIL,
  206. "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%x",
  207. i, iova_addr, patch_desc[i].src_buf_hdl,
  208. (iova_addr + patch_desc[i].src_offset),
  209. src_buf_size, flags, dst_cpu_addr,
  210. patch_desc[i].dst_offset,
  211. patch_desc[i].dst_buf_hdl, value);
  212. if (!(*dst_cpu_addr))
  213. CAM_ERR(CAM_ICP, "Null at dst addr %p", dst_cpu_addr);
  214. }
  215. }
  216. static int cam_packet_util_get_patch_iova(
  217. struct cam_patch_unique_src_buf_tbl *tbl,
  218. int32_t hdl, uint32_t buf_hdl, dma_addr_t *iova,
  219. size_t *buf_size, uint32_t *flags)
  220. {
  221. int idx = 0;
  222. int rc = 0;
  223. size_t src_buf_size;
  224. dma_addr_t iova_addr;
  225. bool is_found = false;
  226. for (idx = 0; idx < CAM_UNIQUE_SRC_HDL_MAX; idx++) {
  227. if (buf_hdl == tbl[idx].hdl) {
  228. CAM_DBG(CAM_UTIL,
  229. "Matched entry for src_buf_hdl: 0x%x with src_hdl[%d]: 0x%x",
  230. buf_hdl, idx, tbl[idx].hdl);
  231. *iova = tbl[idx].iova;
  232. *buf_size = tbl[idx].buf_size;
  233. *flags = tbl[idx].flags;
  234. is_found = true;
  235. break;
  236. } else if ((tbl[idx].hdl == 0) || (tbl[idx].iova == 0)) {
  237. CAM_DBG(CAM_UTIL, "New src handle detected 0x%x", buf_hdl);
  238. is_found = false;
  239. break;
  240. }
  241. CAM_DBG(CAM_UTIL,
  242. "Index: %d is filled with differnt src_hdl: 0x%x",
  243. idx, buf_hdl);
  244. }
  245. if (!is_found) {
  246. CAM_DBG(CAM_UTIL, "src_hdl 0x%x not found in table entries",
  247. buf_hdl);
  248. rc = cam_mem_get_io_buf(buf_hdl, hdl, &iova_addr, &src_buf_size, flags);
  249. if (rc < 0) {
  250. CAM_ERR(CAM_UTIL,
  251. "unable to get iova for src_hdl: 0x%x",
  252. buf_hdl);
  253. return rc;
  254. }
  255. /* Update the table entry with unique src buf handle */
  256. if (idx < CAM_UNIQUE_SRC_HDL_MAX && tbl[idx].hdl == 0) {
  257. tbl[idx].buf_size = src_buf_size;
  258. tbl[idx].iova = iova_addr;
  259. tbl[idx].hdl = buf_hdl;
  260. tbl[idx].flags = *flags;
  261. CAM_DBG(CAM_UTIL,
  262. "Updated table index: %d with src_buf_hdl: 0x%x flags: %x",
  263. idx, tbl[idx].hdl, *flags);
  264. }
  265. *iova = iova_addr;
  266. *buf_size = src_buf_size;
  267. }
  268. return rc;
  269. }
  270. int cam_packet_util_process_patches(struct cam_packet *packet,
  271. int32_t iommu_hdl, int32_t sec_mmu_hdl, bool exp_mem)
  272. {
  273. struct cam_patch_desc *patch_desc = NULL;
  274. dma_addr_t iova_addr;
  275. uintptr_t cpu_addr = 0;
  276. dma_addr_t temp;
  277. uint32_t *dst_cpu_addr;
  278. size_t dst_buf_len;
  279. size_t src_buf_size;
  280. int i = 0;
  281. int rc = 0;
  282. uint32_t flags = 0;
  283. int32_t hdl;
  284. struct cam_patch_unique_src_buf_tbl
  285. tbl[CAM_UNIQUE_SRC_HDL_MAX];
  286. memset(tbl, 0, CAM_UNIQUE_SRC_HDL_MAX *
  287. sizeof(struct cam_patch_unique_src_buf_tbl));
  288. /* process patch descriptor */
  289. patch_desc = (struct cam_patch_desc *)
  290. ((uint32_t *) &packet->payload +
  291. packet->patch_offset/4);
  292. CAM_DBG(CAM_UTIL, "packet = %pK patch_desc = %pK size = %lu",
  293. (void *)packet, (void *)patch_desc,
  294. sizeof(struct cam_patch_desc));
  295. for (i = 0; i < packet->num_patches; i++) {
  296. hdl = cam_mem_is_secure_buf(patch_desc[i].src_buf_hdl) ?
  297. sec_mmu_hdl : iommu_hdl;
  298. rc = cam_packet_util_get_patch_iova(&tbl[0], hdl,
  299. patch_desc[i].src_buf_hdl, &iova_addr, &src_buf_size, &flags);
  300. if (rc) {
  301. CAM_ERR(CAM_UTIL,
  302. "get_iova failed for patch[%d], src_buf_hdl: 0x%x: rc: %d",
  303. i, patch_desc[i].src_buf_hdl, rc);
  304. return rc;
  305. }
  306. if ((size_t)patch_desc[i].src_offset >= src_buf_size) {
  307. CAM_ERR(CAM_UTIL,
  308. "Invalid src buf patch offset: patch:src_offset: 0x%x, src_buf_size: %zu",
  309. patch_desc[i].src_offset, src_buf_size);
  310. return -EINVAL;
  311. }
  312. temp = iova_addr;
  313. rc = cam_mem_get_cpu_buf(patch_desc[i].dst_buf_hdl,
  314. &cpu_addr, &dst_buf_len);
  315. if (rc < 0 || !cpu_addr || (dst_buf_len == 0)) {
  316. CAM_ERR(CAM_UTIL, "unable to get dst buf address");
  317. return rc;
  318. }
  319. dst_cpu_addr = (uint32_t *)cpu_addr;
  320. CAM_DBG(CAM_UTIL, "i = %d patch info = %x %x %x %x", i,
  321. patch_desc[i].dst_buf_hdl, patch_desc[i].dst_offset,
  322. patch_desc[i].src_buf_hdl, patch_desc[i].src_offset);
  323. if ((dst_buf_len < sizeof(void *)) ||
  324. ((dst_buf_len - sizeof(void *)) <
  325. (size_t)patch_desc[i].dst_offset)) {
  326. CAM_ERR(CAM_UTIL,
  327. "Invalid dst buf patch offset");
  328. return -EINVAL;
  329. }
  330. dst_cpu_addr = (uint32_t *)((uint8_t *)dst_cpu_addr +
  331. patch_desc[i].dst_offset);
  332. temp += patch_desc[i].src_offset;
  333. if (exp_mem && cam_smmu_is_expanded_memory()) {
  334. if ((flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) ||
  335. (flags & CAM_MEM_FLAG_CMD_BUF_TYPE)) {
  336. *dst_cpu_addr = temp;
  337. } else {
  338. if (CAM_36BIT_INTF_GET_IOVA_OFFSET(temp))
  339. CAM_ERR(CAM_UTIL,
  340. "Buffer address 0x%lx not aligned to 256bytes",
  341. temp);
  342. *dst_cpu_addr = CAM_36BIT_INTF_GET_IOVA_BASE(temp);
  343. }
  344. } else {
  345. *dst_cpu_addr = temp;
  346. }
  347. CAM_DBG(CAM_UTIL,
  348. "patch is done for dst %pK with base iova 0x%lx final iova 0x%lx patched value 0x%x, shared=%s, cmd=%s, HwAndCDM %s",
  349. dst_cpu_addr, iova_addr, temp, *dst_cpu_addr,
  350. CAM_BOOL_TO_YESNO(flags & CAM_MEM_FLAG_HW_SHARED_ACCESS),
  351. CAM_BOOL_TO_YESNO(flags & CAM_MEM_FLAG_CMD_BUF_TYPE),
  352. CAM_BOOL_TO_YESNO(flags & CAM_MEM_FLAG_HW_AND_CDM_OR_SHARED));
  353. }
  354. return rc;
  355. }
  356. void cam_packet_util_dump_io_bufs(struct cam_packet *packet,
  357. int32_t iommu_hdl, int32_t sec_mmu_hdl,
  358. struct cam_hw_dump_pf_args *pf_args, bool res_id_support)
  359. {
  360. struct cam_buf_io_cfg *io_cfg;
  361. struct cam_context_pf_info *pf_context_info;
  362. int32_t mmu_hdl, buf_fd;
  363. dma_addr_t iova_addr;
  364. size_t src_buf_size;
  365. int i, j, rc = 0;
  366. uint32_t resource_type;
  367. if (!packet) {
  368. CAM_ERR(CAM_UTIL, "Invalid packet");
  369. return;
  370. }
  371. io_cfg = (struct cam_buf_io_cfg *)((uint32_t *)&packet->payload +
  372. packet->io_configs_offset / 4);
  373. buf_fd = pf_args->pf_smmu_info->buf_info;
  374. pf_context_info = &(pf_args->pf_context_info);
  375. resource_type = pf_context_info->resource_type;
  376. for (i = 0; i < packet->num_io_configs; i++) {
  377. if (res_id_support && io_cfg[i].resource_type !=
  378. pf_context_info->resource_type)
  379. continue;
  380. for (j = 0; j < CAM_PACKET_MAX_PLANES; j++) {
  381. if (!io_cfg[i].mem_handle[j])
  382. break;
  383. CAM_INFO(CAM_UTIL, "port: 0x%x f: %u format: %d dir %d",
  384. io_cfg[i].resource_type,
  385. io_cfg[i].fence,
  386. io_cfg[i].format,
  387. io_cfg[i].direction);
  388. mmu_hdl = cam_mem_is_secure_buf(
  389. io_cfg[i].mem_handle[j]) ? sec_mmu_hdl :
  390. iommu_hdl;
  391. rc = cam_mem_get_io_buf(io_cfg[i].mem_handle[j],
  392. mmu_hdl, &iova_addr, &src_buf_size, NULL);
  393. if (rc < 0) {
  394. CAM_ERR(CAM_UTIL,
  395. "get src buf address fail mem_handle 0x%x",
  396. io_cfg[i].mem_handle[j]);
  397. continue;
  398. }
  399. if (GET_FD_FROM_HANDLE(io_cfg[i].mem_handle[j]) == buf_fd) {
  400. pf_context_info->mem_type = CAM_FAULT_IO_CFG_BUF;
  401. pf_context_info->buf_hdl = io_cfg[i].mem_handle[j];
  402. pf_context_info->offset = io_cfg[i].offsets[j];
  403. pf_context_info->resource_type = io_cfg[i].resource_type;
  404. pf_context_info->delta =
  405. CAM_SMMU_GET_IOVA_DELTA(pf_args->pf_smmu_info->iova,
  406. iova_addr);
  407. pf_context_info->req_id = packet->header.request_id;
  408. pf_context_info->ctx_found = true;
  409. resource_type = pf_context_info->resource_type;
  410. CAM_INFO(CAM_UTIL,
  411. "Found PF at port: 0x%x mem 0x%x fd: %d plane id: %d delta: %llu",
  412. io_cfg[i].resource_type,
  413. io_cfg[i].mem_handle[j],
  414. buf_fd,
  415. j, pf_context_info->delta);
  416. }
  417. CAM_INFO(CAM_UTIL,
  418. "pln %d w %d h %d s %u size %zu addr 0x%llx end_addr 0x%llx offset %u memh 0x%x",
  419. j, io_cfg[i].planes[j].width,
  420. io_cfg[i].planes[j].height,
  421. io_cfg[i].planes[j].plane_stride,
  422. src_buf_size, iova_addr,
  423. iova_addr + src_buf_size,
  424. io_cfg[i].offsets[j],
  425. io_cfg[i].mem_handle[j]);
  426. }
  427. if (res_id_support)
  428. return;
  429. }
  430. if (res_id_support)
  431. CAM_ERR(CAM_UTIL,
  432. "getting io port for mid resource id failed req id: %llu res id: 0x%x",
  433. packet->header.request_id, resource_type);
  434. }
  435. int cam_packet_util_process_generic_cmd_buffer(
  436. struct cam_cmd_buf_desc *cmd_buf,
  437. cam_packet_generic_blob_handler blob_handler_cb, void *user_data)
  438. {
  439. int rc = 0;
  440. uintptr_t cpu_addr = 0;
  441. size_t buf_size;
  442. size_t remain_len = 0;
  443. uint32_t *blob_ptr;
  444. uint32_t blob_type, blob_size, blob_block_size, len_read;
  445. if (!cmd_buf || !blob_handler_cb) {
  446. CAM_ERR(CAM_UTIL, "Invalid args %pK %pK",
  447. cmd_buf, blob_handler_cb);
  448. return -EINVAL;
  449. }
  450. if (!cmd_buf->length || !cmd_buf->size) {
  451. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  452. cmd_buf->length, cmd_buf->size);
  453. return -EINVAL;
  454. }
  455. rc = cam_mem_get_cpu_buf(cmd_buf->mem_handle, &cpu_addr, &buf_size);
  456. if (rc || !cpu_addr || (buf_size == 0)) {
  457. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  458. rc, (void *)cpu_addr);
  459. return rc;
  460. }
  461. remain_len = buf_size;
  462. if ((buf_size < sizeof(uint32_t)) ||
  463. ((size_t)cmd_buf->offset > (buf_size - sizeof(uint32_t)))) {
  464. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  465. (size_t)cmd_buf->offset);
  466. return -EINVAL;
  467. }
  468. remain_len -= (size_t)cmd_buf->offset;
  469. if (remain_len < (size_t)cmd_buf->length) {
  470. CAM_ERR(CAM_UTIL, "Invalid length for cmd buf: %zu",
  471. (size_t)cmd_buf->length);
  472. return -EINVAL;
  473. }
  474. blob_ptr = (uint32_t *)(((uint8_t *)cpu_addr) +
  475. cmd_buf->offset);
  476. CAM_DBG(CAM_UTIL,
  477. "GenericCmdBuffer cpuaddr=%pK, blobptr=%pK, len=%d",
  478. (void *)cpu_addr, (void *)blob_ptr, cmd_buf->length);
  479. len_read = 0;
  480. while (len_read < cmd_buf->length) {
  481. blob_type =
  482. ((*blob_ptr) & CAM_GENERIC_BLOB_CMDBUFFER_TYPE_MASK) >>
  483. CAM_GENERIC_BLOB_CMDBUFFER_TYPE_SHIFT;
  484. blob_size =
  485. ((*blob_ptr) & CAM_GENERIC_BLOB_CMDBUFFER_SIZE_MASK) >>
  486. CAM_GENERIC_BLOB_CMDBUFFER_SIZE_SHIFT;
  487. blob_block_size = sizeof(uint32_t) +
  488. (((blob_size + sizeof(uint32_t) - 1) /
  489. sizeof(uint32_t)) * sizeof(uint32_t));
  490. CAM_DBG(CAM_UTIL,
  491. "Blob type=%d size=%d block_size=%d len_read=%d total=%d",
  492. blob_type, blob_size, blob_block_size, len_read,
  493. cmd_buf->length);
  494. if (len_read + blob_block_size > cmd_buf->length) {
  495. CAM_ERR(CAM_UTIL, "Invalid Blob %d %d %d %d",
  496. blob_type, blob_size, len_read,
  497. cmd_buf->length);
  498. rc = -EINVAL;
  499. goto end;
  500. }
  501. len_read += blob_block_size;
  502. rc = blob_handler_cb(user_data, blob_type, blob_size,
  503. (uint8_t *)(blob_ptr + 1));
  504. if (rc) {
  505. CAM_ERR(CAM_UTIL, "Error in handling blob type %d %d",
  506. blob_type, blob_size);
  507. goto end;
  508. }
  509. blob_ptr += (blob_block_size / sizeof(uint32_t));
  510. }
  511. end:
  512. return rc;
  513. }
  514. int cam_presil_retrieve_buffers_from_packet(struct cam_packet *packet, int iommu_hdl,
  515. int out_res_id)
  516. {
  517. int rc = 0, i, j;
  518. struct cam_buf_io_cfg *io_cfg = NULL;
  519. dma_addr_t io_addr[CAM_PACKET_MAX_PLANES];
  520. size_t size;
  521. if (!packet || (iommu_hdl < 0)) {
  522. CAM_ERR(CAM_PRESIL, "Invalid params packet %pK iommu_hdl: %d", packet, iommu_hdl);
  523. return -EINVAL;
  524. }
  525. CAM_DBG(CAM_PRESIL, "Retrieving output buffer corresponding to res: 0x%x", out_res_id);
  526. io_cfg = (struct cam_buf_io_cfg *)((uint8_t *)&packet->payload + packet->io_configs_offset);
  527. for (i = 0; i < packet->num_io_configs; i++) {
  528. if ((io_cfg[i].direction != CAM_BUF_OUTPUT) ||
  529. (io_cfg[i].resource_type != out_res_id))
  530. continue;
  531. memset(io_addr, 0, sizeof(io_addr));
  532. for (j = 0; j < CAM_PACKET_MAX_PLANES; j++) {
  533. if (!io_cfg[i].mem_handle[j])
  534. break;
  535. rc = cam_mem_get_io_buf(io_cfg[i].mem_handle[j], iommu_hdl, &io_addr[j],
  536. &size, NULL);
  537. if (rc) {
  538. CAM_ERR(CAM_PRESIL, "no io addr for plane%d", j);
  539. rc = -ENOMEM;
  540. return rc;
  541. }
  542. /* For presil, address should be within 32 bit */
  543. if (io_addr[j] >> 32) {
  544. CAM_ERR(CAM_PRESIL,
  545. "Invalid address, presil mapped address should be 32 bit");
  546. rc = -EINVAL;
  547. return rc;
  548. }
  549. CAM_INFO(CAM_PRESIL,
  550. "Retrieving IO CFG buffer:%d addr: 0x%x offset 0x%x res_id: 0x%x",
  551. io_cfg[i].mem_handle[j], io_addr[j], io_cfg[i].offsets[j],
  552. io_cfg[i].resource_type);
  553. cam_mem_mgr_retrieve_buffer_from_presil(io_cfg[i].mem_handle[j], size,
  554. io_cfg[i].offsets[j], iommu_hdl);
  555. }
  556. }
  557. return rc;
  558. }
  559. static void cam_presil_add_unique_buf_hdl_to_list(int32_t buf_hdl,
  560. int32_t *hdl_list, int *num_hdls, int max_handles)
  561. {
  562. int k;
  563. bool hdl_found = false;
  564. if (!buf_hdl)
  565. return;
  566. if (*num_hdls >= max_handles) {
  567. CAM_ERR(CAM_PRESIL, "Failed to add entry num_hdls: %d max_handles:%d", *num_hdls,
  568. max_handles);
  569. return;
  570. }
  571. for (k = 0; k < *num_hdls; k++) {
  572. if (hdl_list[k] == buf_hdl) {
  573. hdl_found = true;
  574. break;
  575. }
  576. }
  577. if (!hdl_found)
  578. hdl_list[(*num_hdls)++] = buf_hdl;
  579. }
  580. int cam_presil_send_buffers_from_packet(struct cam_packet *packet, int img_iommu_hdl,
  581. int cdm_iommu_hdl)
  582. {
  583. struct cam_buf_io_cfg *io_cfg = NULL;
  584. struct cam_cmd_buf_desc *cmd_desc = NULL;
  585. struct cam_patch_desc *patch_desc = NULL;
  586. int i, j, rc = 0;
  587. int32_t unique_img_buffers[CAM_PRESIL_UNIQUE_HDL_MAX] = {0};
  588. int32_t unique_cmd_buffers[CAM_PRESIL_UNIQUE_HDL_MAX] = {0};
  589. int num_img_handles = 0, num_cmd_handles = 0;
  590. if(!packet) {
  591. CAM_ERR(CAM_PRESIL, "Packet is NULL");
  592. return -EINVAL;
  593. }
  594. if (img_iommu_hdl == -1) {
  595. goto send_cmd_buffers;
  596. }
  597. /* Adding IO config buffer handles to list*/
  598. io_cfg = (struct cam_buf_io_cfg *)((uint8_t *)&packet->payload + packet->io_configs_offset);
  599. for (i = 0; i < packet->num_io_configs; i++) {
  600. if (io_cfg[i].direction == CAM_BUF_OUTPUT)
  601. continue;
  602. for (j = 0; j < CAM_PACKET_MAX_PLANES; j++) {
  603. if (!io_cfg[i].mem_handle[j])
  604. break;
  605. CAM_DBG(CAM_PRESIL, "Adding IO CFG buffer:%d", io_cfg[i].mem_handle[j]);
  606. cam_presil_add_unique_buf_hdl_to_list(io_cfg[i].mem_handle[j],
  607. unique_img_buffers, &num_img_handles, CAM_PRESIL_UNIQUE_HDL_MAX);
  608. }
  609. }
  610. for (i = 0; i < num_img_handles; i++) {
  611. CAM_DBG(CAM_PRESIL, "Sending Image buffer i:%d mem_handle:%d", i,
  612. unique_img_buffers[i]);
  613. rc = cam_mem_mgr_send_buffer_to_presil(img_iommu_hdl,
  614. unique_img_buffers[i]);
  615. if (rc) {
  616. CAM_ERR(CAM_PRESIL, "Failed to send buffer i:%d mem_handle:%d rc:%d",
  617. i, unique_img_buffers[i], rc);
  618. return rc;
  619. }
  620. }
  621. send_cmd_buffers:
  622. if (cdm_iommu_hdl == -1) {
  623. goto end;
  624. }
  625. /* Adding CMD buffer handles to list*/
  626. cmd_desc = (struct cam_cmd_buf_desc *) ((uint8_t *)&packet->payload +
  627. packet->cmd_buf_offset);
  628. for (i = 0; i < packet->num_cmd_buf; i++) {
  629. CAM_DBG(CAM_PRESIL, "Adding CMD buffer:%d", cmd_desc[i].mem_handle);
  630. cam_presil_add_unique_buf_hdl_to_list(cmd_desc[i].mem_handle,
  631. unique_cmd_buffers, &num_cmd_handles, CAM_PRESIL_UNIQUE_HDL_MAX);
  632. }
  633. /* Adding Patch src buffer handles to list */
  634. patch_desc = (struct cam_patch_desc *) ((uint8_t *)&packet->payload + packet->patch_offset);
  635. for (i = 0; i < packet->num_patches; i++) {
  636. CAM_DBG(CAM_PRESIL, "Adding Patch src buffer:%d", patch_desc[i].src_buf_hdl);
  637. cam_presil_add_unique_buf_hdl_to_list(patch_desc[i].src_buf_hdl,
  638. unique_cmd_buffers, &num_cmd_handles, CAM_PRESIL_UNIQUE_HDL_MAX);
  639. }
  640. for (i = 0; i < num_cmd_handles; i++) {
  641. CAM_DBG(CAM_PRESIL, "Sending Command buffer i:%d mem_handle:%d", i,
  642. unique_cmd_buffers[i]);
  643. rc = cam_mem_mgr_send_buffer_to_presil(cdm_iommu_hdl,
  644. unique_cmd_buffers[i]);
  645. if (rc) {
  646. CAM_ERR(CAM_PRESIL, "Failed to send buffer i:%d mem_handle:%d rc:%d",
  647. i, unique_cmd_buffers[i], rc);
  648. return rc;
  649. }
  650. }
  651. end:
  652. return rc;
  653. }