cam_packet_util.c 23 KB

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