cam_packet_util.c 22 KB

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