hfi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/io.h>
  6. #include <linux/delay.h>
  7. #include <linux/slab.h>
  8. #include <linux/random.h>
  9. #include <asm/errno.h>
  10. #include <linux/timer.h>
  11. #include <media/cam_icp.h>
  12. #include <linux/iopoll.h>
  13. #include "cam_io_util.h"
  14. #include "hfi_reg.h"
  15. #include "hfi_sys_defs.h"
  16. #include "hfi_session_defs.h"
  17. #include "hfi_intf.h"
  18. #include "cam_icp_hw_mgr_intf.h"
  19. #include "cam_debug_util.h"
  20. #define HFI_VERSION_INFO_MAJOR_VAL 1
  21. #define HFI_VERSION_INFO_MINOR_VAL 1
  22. #define HFI_VERSION_INFO_STEP_VAL 0
  23. #define HFI_VERSION_INFO_STEP_VAL 0
  24. #define HFI_VERSION_INFO_MAJOR_BMSK 0xFF000000
  25. #define HFI_VERSION_INFO_MAJOR_SHFT 24
  26. #define HFI_VERSION_INFO_MINOR_BMSK 0xFFFF00
  27. #define HFI_VERSION_INFO_MINOR_SHFT 8
  28. #define HFI_VERSION_INFO_STEP_BMSK 0xFF
  29. #define HFI_VERSION_INFO_STEP_SHFT 0
  30. #define HFI_MAX_POLL_TRY 5
  31. #define HFI_MAX_PC_POLL_TRY 150
  32. #define HFI_POLL_TRY_SLEEP 1
  33. static struct hfi_info *g_hfi;
  34. unsigned int g_icp_mmu_hdl;
  35. static DEFINE_MUTEX(hfi_cmd_q_mutex);
  36. static DEFINE_MUTEX(hfi_msg_q_mutex);
  37. void cam_hfi_queue_dump(void)
  38. {
  39. struct hfi_qtbl *qtbl;
  40. struct hfi_qtbl_hdr *qtbl_hdr;
  41. struct hfi_q_hdr *cmd_q_hdr, *msg_q_hdr;
  42. struct hfi_mem_info *hfi_mem = NULL;
  43. uint32_t *read_q, *read_ptr;
  44. int i;
  45. hfi_mem = &g_hfi->map;
  46. if (!hfi_mem) {
  47. CAM_ERR(CAM_HFI, "Unable to dump queues hfi memory is NULL");
  48. return;
  49. }
  50. qtbl = (struct hfi_qtbl *)hfi_mem->qtbl.kva;
  51. qtbl_hdr = &qtbl->q_tbl_hdr;
  52. CAM_DBG(CAM_HFI,
  53. "qtbl: version = %x size = %u num q = %u qhdr_size = %u",
  54. qtbl_hdr->qtbl_version, qtbl_hdr->qtbl_size,
  55. qtbl_hdr->qtbl_num_q, qtbl_hdr->qtbl_qhdr_size);
  56. cmd_q_hdr = &qtbl->q_hdr[Q_CMD];
  57. CAM_DBG(CAM_HFI, "cmd: size = %u r_idx = %u w_idx = %u addr = %x",
  58. cmd_q_hdr->qhdr_q_size, cmd_q_hdr->qhdr_read_idx,
  59. cmd_q_hdr->qhdr_write_idx, hfi_mem->cmd_q.iova);
  60. read_q = (uint32_t *)g_hfi->map.cmd_q.kva;
  61. read_ptr = (uint32_t *)(read_q + 0);
  62. CAM_DBG(CAM_HFI, "CMD Q START");
  63. for (i = 0; i < ICP_CMD_Q_SIZE_IN_BYTES >> BYTE_WORD_SHIFT; i++)
  64. CAM_DBG(CAM_HFI, "Word: %d Data: 0x%08x ", i, read_ptr[i]);
  65. msg_q_hdr = &qtbl->q_hdr[Q_MSG];
  66. CAM_DBG(CAM_HFI, "msg: size = %u r_idx = %u w_idx = %u addr = %x",
  67. msg_q_hdr->qhdr_q_size, msg_q_hdr->qhdr_read_idx,
  68. msg_q_hdr->qhdr_write_idx, hfi_mem->msg_q.iova);
  69. read_q = (uint32_t *)g_hfi->map.msg_q.kva;
  70. read_ptr = (uint32_t *)(read_q + 0);
  71. CAM_DBG(CAM_HFI, "MSG Q START");
  72. for (i = 0; i < ICP_MSG_Q_SIZE_IN_BYTES >> BYTE_WORD_SHIFT; i++)
  73. CAM_DBG(CAM_HFI, "Word: %d Data: 0x%08x ", i, read_ptr[i]);
  74. }
  75. int hfi_write_cmd(void *cmd_ptr)
  76. {
  77. uint32_t size_in_words, empty_space, new_write_idx, read_idx, temp;
  78. uint32_t *write_q, *write_ptr;
  79. struct hfi_qtbl *q_tbl;
  80. struct hfi_q_hdr *q;
  81. int rc = 0;
  82. if (!cmd_ptr) {
  83. CAM_ERR(CAM_HFI, "command is null");
  84. return -EINVAL;
  85. }
  86. mutex_lock(&hfi_cmd_q_mutex);
  87. if (!g_hfi) {
  88. CAM_ERR(CAM_HFI, "HFI interface not setup");
  89. rc = -ENODEV;
  90. goto err;
  91. }
  92. if (g_hfi->hfi_state != HFI_READY ||
  93. !g_hfi->cmd_q_state) {
  94. CAM_ERR(CAM_HFI, "HFI state: %u, cmd q state: %u",
  95. g_hfi->hfi_state, g_hfi->cmd_q_state);
  96. rc = -ENODEV;
  97. goto err;
  98. }
  99. q_tbl = (struct hfi_qtbl *)g_hfi->map.qtbl.kva;
  100. q = &q_tbl->q_hdr[Q_CMD];
  101. write_q = (uint32_t *)g_hfi->map.cmd_q.kva;
  102. size_in_words = (*(uint32_t *)cmd_ptr) >> BYTE_WORD_SHIFT;
  103. if (!size_in_words) {
  104. CAM_DBG(CAM_HFI, "failed");
  105. rc = -EINVAL;
  106. goto err;
  107. }
  108. read_idx = q->qhdr_read_idx;
  109. empty_space = (q->qhdr_write_idx >= read_idx) ?
  110. (q->qhdr_q_size - (q->qhdr_write_idx - read_idx)) :
  111. (read_idx - q->qhdr_write_idx);
  112. if (empty_space <= size_in_words) {
  113. CAM_ERR(CAM_HFI, "failed: empty space %u, size_in_words %u",
  114. empty_space, size_in_words);
  115. rc = -EIO;
  116. goto err;
  117. }
  118. new_write_idx = q->qhdr_write_idx + size_in_words;
  119. write_ptr = (uint32_t *)(write_q + q->qhdr_write_idx);
  120. if (new_write_idx < q->qhdr_q_size) {
  121. memcpy(write_ptr, (uint8_t *)cmd_ptr,
  122. size_in_words << BYTE_WORD_SHIFT);
  123. } else {
  124. new_write_idx -= q->qhdr_q_size;
  125. temp = (size_in_words - new_write_idx) << BYTE_WORD_SHIFT;
  126. memcpy(write_ptr, (uint8_t *)cmd_ptr, temp);
  127. memcpy(write_q, (uint8_t *)cmd_ptr + temp,
  128. new_write_idx << BYTE_WORD_SHIFT);
  129. }
  130. /*
  131. * To make sure command data in a command queue before
  132. * updating write index
  133. */
  134. wmb();
  135. q->qhdr_write_idx = new_write_idx;
  136. /*
  137. * Before raising interrupt make sure command data is ready for
  138. * firmware to process
  139. */
  140. wmb();
  141. cam_io_w_mb((uint32_t)INTR_ENABLE,
  142. g_hfi->csr_base + HFI_REG_A5_CSR_HOST2ICPINT);
  143. err:
  144. mutex_unlock(&hfi_cmd_q_mutex);
  145. return rc;
  146. }
  147. int hfi_read_message(uint32_t *pmsg, uint8_t q_id,
  148. uint32_t *words_read)
  149. {
  150. struct hfi_qtbl *q_tbl_ptr;
  151. struct hfi_q_hdr *q;
  152. uint32_t new_read_idx, size_in_words, word_diff, temp;
  153. uint32_t *read_q, *read_ptr, *write_ptr;
  154. uint32_t size_upper_bound = 0;
  155. int rc = 0;
  156. if (!pmsg) {
  157. CAM_ERR(CAM_HFI, "Invalid msg");
  158. return -EINVAL;
  159. }
  160. if (q_id > Q_DBG) {
  161. CAM_ERR(CAM_HFI, "Invalid q :%u", q_id);
  162. return -EINVAL;
  163. }
  164. mutex_lock(&hfi_msg_q_mutex);
  165. if (!g_hfi) {
  166. CAM_ERR(CAM_HFI, "hfi not set up yet");
  167. rc = -ENODEV;
  168. goto err;
  169. }
  170. if ((g_hfi->hfi_state != HFI_READY) ||
  171. !g_hfi->msg_q_state) {
  172. CAM_ERR(CAM_HFI, "hfi state: %u, msg q state: %u",
  173. g_hfi->hfi_state, g_hfi->msg_q_state);
  174. rc = -ENODEV;
  175. goto err;
  176. }
  177. q_tbl_ptr = (struct hfi_qtbl *)g_hfi->map.qtbl.kva;
  178. q = &q_tbl_ptr->q_hdr[q_id];
  179. if (q->qhdr_read_idx == q->qhdr_write_idx) {
  180. CAM_DBG(CAM_HFI, "Q not ready, state:%u, r idx:%u, w idx:%u",
  181. g_hfi->hfi_state, q->qhdr_read_idx, q->qhdr_write_idx);
  182. rc = -EIO;
  183. goto err;
  184. }
  185. if (q_id == Q_MSG) {
  186. read_q = (uint32_t *)g_hfi->map.msg_q.kva;
  187. size_upper_bound = ICP_HFI_MAX_PKT_SIZE_MSGQ_IN_WORDS;
  188. } else {
  189. read_q = (uint32_t *)g_hfi->map.dbg_q.kva;
  190. size_upper_bound = ICP_HFI_MAX_PKT_SIZE_IN_WORDS;
  191. }
  192. read_ptr = (uint32_t *)(read_q + q->qhdr_read_idx);
  193. write_ptr = (uint32_t *)(read_q + q->qhdr_write_idx);
  194. if (write_ptr > read_ptr)
  195. size_in_words = write_ptr - read_ptr;
  196. else {
  197. word_diff = read_ptr - write_ptr;
  198. if (q_id == Q_MSG)
  199. size_in_words = (ICP_MSG_Q_SIZE_IN_BYTES >>
  200. BYTE_WORD_SHIFT) - word_diff;
  201. else
  202. size_in_words = (ICP_DBG_Q_SIZE_IN_BYTES >>
  203. BYTE_WORD_SHIFT) - word_diff;
  204. }
  205. if ((size_in_words == 0) ||
  206. (size_in_words > size_upper_bound)) {
  207. CAM_ERR(CAM_HFI, "invalid HFI message packet size - 0x%08x",
  208. size_in_words << BYTE_WORD_SHIFT);
  209. q->qhdr_read_idx = q->qhdr_write_idx;
  210. rc = -EIO;
  211. goto err;
  212. }
  213. new_read_idx = q->qhdr_read_idx + size_in_words;
  214. if (new_read_idx < q->qhdr_q_size) {
  215. memcpy(pmsg, read_ptr, size_in_words << BYTE_WORD_SHIFT);
  216. } else {
  217. new_read_idx -= q->qhdr_q_size;
  218. temp = (size_in_words - new_read_idx) << BYTE_WORD_SHIFT;
  219. memcpy(pmsg, read_ptr, temp);
  220. memcpy((uint8_t *)pmsg + temp, read_q,
  221. new_read_idx << BYTE_WORD_SHIFT);
  222. }
  223. q->qhdr_read_idx = new_read_idx;
  224. *words_read = size_in_words;
  225. /* Memory Barrier to make sure message
  226. * queue parameters are updated after read
  227. */
  228. wmb();
  229. err:
  230. mutex_unlock(&hfi_msg_q_mutex);
  231. return rc;
  232. }
  233. int hfi_cmd_ubwc_config(uint32_t *ubwc_cfg)
  234. {
  235. uint8_t *prop;
  236. struct hfi_cmd_prop *dbg_prop;
  237. uint32_t size = 0;
  238. size = sizeof(struct hfi_cmd_prop) +
  239. sizeof(struct hfi_cmd_ubwc_cfg);
  240. CAM_DBG(CAM_HFI,
  241. "size of ubwc %u, ubwc_cfg [rd-0x%x,wr-0x%x]",
  242. size, ubwc_cfg[0], ubwc_cfg[1]);
  243. prop = kzalloc(size, GFP_KERNEL);
  244. if (!prop)
  245. return -ENOMEM;
  246. dbg_prop = (struct hfi_cmd_prop *)prop;
  247. dbg_prop->size = size;
  248. dbg_prop->pkt_type = HFI_CMD_SYS_SET_PROPERTY;
  249. dbg_prop->num_prop = 1;
  250. dbg_prop->prop_data[0] = HFI_PROP_SYS_UBWC_CFG;
  251. dbg_prop->prop_data[1] = ubwc_cfg[0];
  252. dbg_prop->prop_data[2] = ubwc_cfg[1];
  253. hfi_write_cmd(prop);
  254. kfree(prop);
  255. return 0;
  256. }
  257. int hfi_cmd_ubwc_config_ext(uint32_t *ubwc_ipe_cfg,
  258. uint32_t *ubwc_bps_cfg)
  259. {
  260. uint8_t *prop;
  261. struct hfi_cmd_prop *dbg_prop;
  262. uint32_t size = 0;
  263. size = sizeof(struct hfi_cmd_prop) +
  264. sizeof(struct hfi_cmd_ubwc_cfg_ext);
  265. CAM_DBG(CAM_HFI,
  266. "size of ubwc %u, ubwc_ipe_cfg[rd-0x%x,wr-0x%x] ubwc_bps_cfg[rd-0x%x,wr-0x%x]",
  267. size, ubwc_ipe_cfg[0], ubwc_ipe_cfg[1],
  268. ubwc_bps_cfg[0], ubwc_bps_cfg[1]);
  269. prop = kzalloc(size, GFP_KERNEL);
  270. if (!prop)
  271. return -ENOMEM;
  272. dbg_prop = (struct hfi_cmd_prop *)prop;
  273. dbg_prop->size = size;
  274. dbg_prop->pkt_type = HFI_CMD_SYS_SET_PROPERTY;
  275. dbg_prop->num_prop = 1;
  276. dbg_prop->prop_data[0] = HFI_PROPERTY_SYS_UBWC_CONFIG_EX;
  277. dbg_prop->prop_data[1] = ubwc_bps_cfg[0];
  278. dbg_prop->prop_data[2] = ubwc_bps_cfg[1];
  279. dbg_prop->prop_data[3] = ubwc_ipe_cfg[0];
  280. dbg_prop->prop_data[4] = ubwc_ipe_cfg[1];
  281. hfi_write_cmd(prop);
  282. kfree(prop);
  283. return 0;
  284. }
  285. int hfi_enable_ipe_bps_pc(bool enable, uint32_t core_info)
  286. {
  287. uint8_t *prop;
  288. struct hfi_cmd_prop *dbg_prop;
  289. uint32_t size = 0;
  290. size = sizeof(struct hfi_cmd_prop) +
  291. sizeof(struct hfi_ipe_bps_pc);
  292. prop = kzalloc(size, GFP_KERNEL);
  293. if (!prop)
  294. return -ENOMEM;
  295. dbg_prop = (struct hfi_cmd_prop *)prop;
  296. dbg_prop->size = size;
  297. dbg_prop->pkt_type = HFI_CMD_SYS_SET_PROPERTY;
  298. dbg_prop->num_prop = 1;
  299. dbg_prop->prop_data[0] = HFI_PROP_SYS_IPEBPS_PC;
  300. dbg_prop->prop_data[1] = enable;
  301. dbg_prop->prop_data[2] = core_info;
  302. hfi_write_cmd(prop);
  303. kfree(prop);
  304. return 0;
  305. }
  306. int hfi_set_debug_level(u64 a5_dbg_type, uint32_t lvl)
  307. {
  308. uint8_t *prop;
  309. struct hfi_cmd_prop *dbg_prop;
  310. uint32_t size = 0, val;
  311. val = HFI_DEBUG_MSG_LOW |
  312. HFI_DEBUG_MSG_MEDIUM |
  313. HFI_DEBUG_MSG_HIGH |
  314. HFI_DEBUG_MSG_ERROR |
  315. HFI_DEBUG_MSG_FATAL |
  316. HFI_DEBUG_MSG_PERF |
  317. HFI_DEBUG_CFG_WFI |
  318. HFI_DEBUG_CFG_ARM9WD;
  319. if (lvl > val)
  320. return -EINVAL;
  321. size = sizeof(struct hfi_cmd_prop) +
  322. sizeof(struct hfi_debug);
  323. prop = kzalloc(size, GFP_KERNEL);
  324. if (!prop)
  325. return -ENOMEM;
  326. dbg_prop = (struct hfi_cmd_prop *)prop;
  327. dbg_prop->size = size;
  328. dbg_prop->pkt_type = HFI_CMD_SYS_SET_PROPERTY;
  329. dbg_prop->num_prop = 1;
  330. dbg_prop->prop_data[0] = HFI_PROP_SYS_DEBUG_CFG;
  331. dbg_prop->prop_data[1] = lvl;
  332. dbg_prop->prop_data[2] = a5_dbg_type;
  333. hfi_write_cmd(prop);
  334. kfree(prop);
  335. return 0;
  336. }
  337. int hfi_set_fw_dump_level(uint32_t lvl)
  338. {
  339. uint8_t *prop = NULL;
  340. struct hfi_cmd_prop *fw_dump_level_switch_prop = NULL;
  341. uint32_t size = 0;
  342. CAM_DBG(CAM_HFI, "fw dump ENTER");
  343. size = sizeof(struct hfi_cmd_prop) + sizeof(lvl);
  344. prop = kzalloc(size, GFP_KERNEL);
  345. if (!prop)
  346. return -ENOMEM;
  347. fw_dump_level_switch_prop = (struct hfi_cmd_prop *)prop;
  348. fw_dump_level_switch_prop->size = size;
  349. fw_dump_level_switch_prop->pkt_type = HFI_CMD_SYS_SET_PROPERTY;
  350. fw_dump_level_switch_prop->num_prop = 1;
  351. fw_dump_level_switch_prop->prop_data[0] = HFI_PROP_SYS_FW_DUMP_CFG;
  352. fw_dump_level_switch_prop->prop_data[1] = lvl;
  353. CAM_DBG(CAM_HFI, "prop->size = %d\n"
  354. "prop->pkt_type = %d\n"
  355. "prop->num_prop = %d\n"
  356. "prop->prop_data[0] = %d\n"
  357. "prop->prop_data[1] = %d\n",
  358. fw_dump_level_switch_prop->size,
  359. fw_dump_level_switch_prop->pkt_type,
  360. fw_dump_level_switch_prop->num_prop,
  361. fw_dump_level_switch_prop->prop_data[0],
  362. fw_dump_level_switch_prop->prop_data[1]);
  363. hfi_write_cmd(prop);
  364. kfree(prop);
  365. return 0;
  366. }
  367. void hfi_send_system_cmd(uint32_t type, uint64_t data, uint32_t size)
  368. {
  369. switch (type) {
  370. case HFI_CMD_SYS_INIT: {
  371. struct hfi_cmd_sys_init init;
  372. memset(&init, 0, sizeof(init));
  373. init.size = sizeof(struct hfi_cmd_sys_init);
  374. init.pkt_type = type;
  375. hfi_write_cmd(&init);
  376. }
  377. break;
  378. case HFI_CMD_SYS_PC_PREP: {
  379. struct hfi_cmd_pc_prep prep;
  380. prep.size = sizeof(struct hfi_cmd_pc_prep);
  381. prep.pkt_type = type;
  382. hfi_write_cmd(&prep);
  383. }
  384. break;
  385. case HFI_CMD_SYS_SET_PROPERTY: {
  386. struct hfi_cmd_prop prop;
  387. if ((uint32_t)data == (uint32_t)HFI_PROP_SYS_DEBUG_CFG) {
  388. prop.size = sizeof(struct hfi_cmd_prop);
  389. prop.pkt_type = type;
  390. prop.num_prop = 1;
  391. prop.prop_data[0] = HFI_PROP_SYS_DEBUG_CFG;
  392. hfi_write_cmd(&prop);
  393. }
  394. }
  395. break;
  396. case HFI_CMD_SYS_GET_PROPERTY:
  397. break;
  398. case HFI_CMD_SYS_PING: {
  399. struct hfi_cmd_ping_pkt ping;
  400. ping.size = sizeof(struct hfi_cmd_ping_pkt);
  401. ping.pkt_type = type;
  402. ping.user_data = (uint64_t)data;
  403. hfi_write_cmd(&ping);
  404. }
  405. break;
  406. case HFI_CMD_SYS_RESET: {
  407. struct hfi_cmd_sys_reset_pkt reset;
  408. reset.size = sizeof(struct hfi_cmd_sys_reset_pkt);
  409. reset.pkt_type = type;
  410. reset.user_data = (uint64_t)data;
  411. hfi_write_cmd(&reset);
  412. }
  413. break;
  414. case HFI_CMD_IPEBPS_CREATE_HANDLE: {
  415. struct hfi_cmd_create_handle handle;
  416. handle.size = sizeof(struct hfi_cmd_create_handle);
  417. handle.pkt_type = type;
  418. handle.handle_type = (uint32_t)data;
  419. handle.user_data1 = 0;
  420. hfi_write_cmd(&handle);
  421. }
  422. break;
  423. case HFI_CMD_IPEBPS_ASYNC_COMMAND_INDIRECT:
  424. break;
  425. default:
  426. CAM_ERR(CAM_HFI, "command not supported :%d", type);
  427. break;
  428. }
  429. }
  430. int hfi_get_hw_caps(void *query_buf)
  431. {
  432. int i = 0;
  433. struct cam_icp_query_cap_cmd *query_cmd = NULL;
  434. if (!query_buf) {
  435. CAM_ERR(CAM_HFI, "query buf is NULL");
  436. return -EINVAL;
  437. }
  438. query_cmd = (struct cam_icp_query_cap_cmd *)query_buf;
  439. query_cmd->fw_version.major = 0x12;
  440. query_cmd->fw_version.minor = 0x12;
  441. query_cmd->fw_version.revision = 0x12;
  442. query_cmd->api_version.major = 0x13;
  443. query_cmd->api_version.minor = 0x13;
  444. query_cmd->api_version.revision = 0x13;
  445. query_cmd->num_ipe = 2;
  446. query_cmd->num_bps = 1;
  447. for (i = 0; i < CAM_ICP_DEV_TYPE_MAX; i++) {
  448. query_cmd->dev_ver[i].dev_type = i;
  449. query_cmd->dev_ver[i].hw_ver.major = 0x34 + i;
  450. query_cmd->dev_ver[i].hw_ver.minor = 0x34 + i;
  451. query_cmd->dev_ver[i].hw_ver.incr = 0x34 + i;
  452. }
  453. return 0;
  454. }
  455. void cam_hfi_disable_cpu(void __iomem *icp_base)
  456. {
  457. uint32_t data;
  458. uint32_t val;
  459. uint32_t try = 0;
  460. while (try < HFI_MAX_PC_POLL_TRY) {
  461. data = cam_io_r_mb(icp_base + HFI_REG_A5_CSR_A5_STATUS);
  462. CAM_DBG(CAM_HFI, "wfi status = %x\n", (int)data);
  463. if (data & ICP_CSR_A5_STATUS_WFI)
  464. break;
  465. /* Need to poll here to confirm that FW is going trigger wfi
  466. * and Host can the proceed. No interrupt is expected from FW
  467. * at this time.
  468. */
  469. usleep_range(HFI_POLL_TRY_SLEEP * 1000,
  470. (HFI_POLL_TRY_SLEEP * 1000) + 1000);
  471. try++;
  472. }
  473. val = cam_io_r(icp_base + HFI_REG_A5_CSR_A5_CONTROL);
  474. val &= ~(ICP_FLAG_CSR_A5_EN | ICP_FLAG_CSR_WAKE_UP_EN);
  475. cam_io_w_mb(val, icp_base + HFI_REG_A5_CSR_A5_CONTROL);
  476. val = cam_io_r(icp_base + HFI_REG_A5_CSR_NSEC_RESET);
  477. cam_io_w_mb(val, icp_base + HFI_REG_A5_CSR_NSEC_RESET);
  478. cam_io_w_mb((uint32_t)ICP_INIT_REQUEST_RESET,
  479. icp_base + HFI_REG_HOST_ICP_INIT_REQUEST);
  480. cam_io_w_mb((uint32_t)INTR_DISABLE,
  481. icp_base + HFI_REG_A5_CSR_A2HOSTINTEN);
  482. }
  483. void cam_hfi_enable_cpu(void __iomem *icp_base)
  484. {
  485. cam_io_w_mb((uint32_t)ICP_FLAG_CSR_A5_EN,
  486. icp_base + HFI_REG_A5_CSR_A5_CONTROL);
  487. cam_io_w_mb((uint32_t)0x10, icp_base + HFI_REG_A5_CSR_NSEC_RESET);
  488. }
  489. int cam_hfi_resume(struct hfi_mem_info *hfi_mem,
  490. void __iomem *icp_base, bool debug)
  491. {
  492. int rc = 0;
  493. uint32_t data;
  494. uint32_t fw_version, status = 0;
  495. uint32_t retry_cnt = 0;
  496. cam_hfi_enable_cpu(icp_base);
  497. g_hfi->csr_base = icp_base;
  498. if (debug) {
  499. cam_io_w_mb(ICP_FLAG_A5_CTRL_DBG_EN,
  500. (icp_base + HFI_REG_A5_CSR_A5_CONTROL));
  501. /* Barrier needed as next write should be done after
  502. * sucessful previous write. Next write enable clock
  503. * gating
  504. */
  505. wmb();
  506. cam_io_w_mb((uint32_t)ICP_FLAG_A5_CTRL_EN,
  507. icp_base + HFI_REG_A5_CSR_A5_CONTROL);
  508. } else {
  509. cam_io_w_mb((uint32_t)ICP_FLAG_A5_CTRL_EN,
  510. icp_base + HFI_REG_A5_CSR_A5_CONTROL);
  511. }
  512. while (retry_cnt < HFI_MAX_POLL_TRY) {
  513. readw_poll_timeout((icp_base + HFI_REG_ICP_HOST_INIT_RESPONSE),
  514. status, (status == ICP_INIT_RESP_SUCCESS), 100, 10000);
  515. CAM_DBG(CAM_HFI, "1: status = %u", status);
  516. status = cam_io_r_mb(icp_base + HFI_REG_ICP_HOST_INIT_RESPONSE);
  517. CAM_DBG(CAM_HFI, "2: status = %u", status);
  518. if (status == ICP_INIT_RESP_SUCCESS)
  519. break;
  520. if (status == ICP_INIT_RESP_FAILED) {
  521. CAM_ERR(CAM_HFI, "ICP Init Failed. status = %u",
  522. status);
  523. fw_version = cam_io_r(icp_base + HFI_REG_FW_VERSION);
  524. CAM_ERR(CAM_HFI, "fw version : [%x]", fw_version);
  525. return -EINVAL;
  526. }
  527. retry_cnt++;
  528. }
  529. if ((retry_cnt == HFI_MAX_POLL_TRY) &&
  530. (status == ICP_INIT_RESP_RESET)) {
  531. CAM_ERR(CAM_HFI, "Reached Max retries. status = %u",
  532. status);
  533. fw_version = cam_io_r(icp_base + HFI_REG_FW_VERSION);
  534. CAM_ERR(CAM_HFI, "fw version : [%x]", fw_version);
  535. return -EINVAL;
  536. }
  537. cam_io_w_mb((uint32_t)(INTR_ENABLE|INTR_ENABLE_WD0),
  538. icp_base + HFI_REG_A5_CSR_A2HOSTINTEN);
  539. fw_version = cam_io_r(icp_base + HFI_REG_FW_VERSION);
  540. CAM_DBG(CAM_HFI, "fw version : [%x]", fw_version);
  541. data = cam_io_r(icp_base + HFI_REG_A5_CSR_A5_STATUS);
  542. CAM_DBG(CAM_HFI, "wfi status = %x", (int)data);
  543. cam_io_w_mb((uint32_t)hfi_mem->qtbl.iova, icp_base + HFI_REG_QTBL_PTR);
  544. cam_io_w_mb((uint32_t)hfi_mem->sfr_buf.iova,
  545. icp_base + HFI_REG_SFR_PTR);
  546. cam_io_w_mb((uint32_t)hfi_mem->shmem.iova,
  547. icp_base + HFI_REG_SHARED_MEM_PTR);
  548. cam_io_w_mb((uint32_t)hfi_mem->shmem.len,
  549. icp_base + HFI_REG_SHARED_MEM_SIZE);
  550. cam_io_w_mb((uint32_t)hfi_mem->sec_heap.iova,
  551. icp_base + HFI_REG_UNCACHED_HEAP_PTR);
  552. cam_io_w_mb((uint32_t)hfi_mem->sec_heap.len,
  553. icp_base + HFI_REG_UNCACHED_HEAP_SIZE);
  554. cam_io_w_mb((uint32_t)hfi_mem->qdss.iova,
  555. icp_base + HFI_REG_QDSS_IOVA);
  556. cam_io_w_mb((uint32_t)hfi_mem->qdss.len,
  557. icp_base + HFI_REG_QDSS_IOVA_SIZE);
  558. cam_io_w_mb((uint32_t)hfi_mem->io_mem.iova,
  559. icp_base + HFI_REG_IO_REGION_IOVA);
  560. cam_io_w_mb((uint32_t)hfi_mem->io_mem.len,
  561. icp_base + HFI_REG_IO_REGION_SIZE);
  562. cam_io_w_mb((uint32_t)hfi_mem->io_mem2.iova,
  563. icp_base + HFI_REG_IO2_REGION_IOVA);
  564. cam_io_w_mb((uint32_t)hfi_mem->io_mem2.len,
  565. icp_base + HFI_REG_IO2_REGION_SIZE);
  566. CAM_INFO(CAM_HFI, "Resume IO1 : [0x%x 0x%x] IO2 [0x%x 0x%x]",
  567. hfi_mem->io_mem.iova, hfi_mem->io_mem.len,
  568. hfi_mem->io_mem2.iova, hfi_mem->io_mem2.len);
  569. return rc;
  570. }
  571. int cam_hfi_init(uint8_t event_driven_mode, struct hfi_mem_info *hfi_mem,
  572. void __iomem *icp_base, bool debug)
  573. {
  574. int rc = 0;
  575. struct hfi_qtbl *qtbl;
  576. struct hfi_qtbl_hdr *qtbl_hdr;
  577. struct hfi_q_hdr *cmd_q_hdr, *msg_q_hdr, *dbg_q_hdr;
  578. uint32_t hw_version, fw_version, status = 0;
  579. uint32_t retry_cnt = 0;
  580. struct sfr_buf *sfr_buffer;
  581. mutex_lock(&hfi_cmd_q_mutex);
  582. mutex_lock(&hfi_msg_q_mutex);
  583. if (!g_hfi) {
  584. g_hfi = kzalloc(sizeof(struct hfi_info), GFP_KERNEL);
  585. if (!g_hfi) {
  586. rc = -ENOMEM;
  587. goto alloc_fail;
  588. }
  589. }
  590. if (g_hfi->hfi_state != HFI_DEINIT) {
  591. CAM_ERR(CAM_HFI, "hfi_init: invalid state");
  592. return -EINVAL;
  593. }
  594. memcpy(&g_hfi->map, hfi_mem, sizeof(g_hfi->map));
  595. g_hfi->hfi_state = HFI_DEINIT;
  596. if (debug) {
  597. cam_io_w_mb(
  598. (uint32_t)(ICP_FLAG_CSR_A5_EN | ICP_FLAG_CSR_WAKE_UP_EN |
  599. ICP_CSR_EDBGRQ | ICP_CSR_DBGSWENABLE),
  600. icp_base + HFI_REG_A5_CSR_A5_CONTROL);
  601. msleep(100);
  602. cam_io_w_mb((uint32_t)(ICP_FLAG_CSR_A5_EN |
  603. ICP_FLAG_CSR_WAKE_UP_EN | ICP_CSR_EN_CLKGATE_WFI),
  604. icp_base + HFI_REG_A5_CSR_A5_CONTROL);
  605. } else {
  606. /* Due to hardware bug in V1 ICP clock gating has to be
  607. * disabled, this is supposed to be fixed in V-2. But enabling
  608. * the clock gating is causing the firmware hang, hence
  609. * disabling the clock gating on both V1 and V2 until the
  610. * hardware team root causes this
  611. */
  612. cam_io_w_mb((uint32_t)ICP_FLAG_CSR_A5_EN |
  613. ICP_FLAG_CSR_WAKE_UP_EN |
  614. ICP_CSR_EN_CLKGATE_WFI,
  615. icp_base + HFI_REG_A5_CSR_A5_CONTROL);
  616. }
  617. qtbl = (struct hfi_qtbl *)hfi_mem->qtbl.kva;
  618. qtbl_hdr = &qtbl->q_tbl_hdr;
  619. qtbl_hdr->qtbl_version = 0xFFFFFFFF;
  620. qtbl_hdr->qtbl_size = sizeof(struct hfi_qtbl);
  621. qtbl_hdr->qtbl_qhdr0_offset = sizeof(struct hfi_qtbl_hdr);
  622. qtbl_hdr->qtbl_qhdr_size = sizeof(struct hfi_q_hdr);
  623. qtbl_hdr->qtbl_num_q = ICP_HFI_NUMBER_OF_QS;
  624. qtbl_hdr->qtbl_num_active_q = ICP_HFI_NUMBER_OF_QS;
  625. /* setup host-to-firmware command queue */
  626. cmd_q_hdr = &qtbl->q_hdr[Q_CMD];
  627. cmd_q_hdr->qhdr_status = QHDR_ACTIVE;
  628. cmd_q_hdr->qhdr_start_addr = hfi_mem->cmd_q.iova;
  629. cmd_q_hdr->qhdr_q_size = ICP_CMD_Q_SIZE_IN_BYTES >> BYTE_WORD_SHIFT;
  630. cmd_q_hdr->qhdr_pkt_size = ICP_HFI_VAR_SIZE_PKT;
  631. cmd_q_hdr->qhdr_pkt_drop_cnt = RESET;
  632. cmd_q_hdr->qhdr_read_idx = RESET;
  633. cmd_q_hdr->qhdr_write_idx = RESET;
  634. /* setup firmware-to-Host message queue */
  635. msg_q_hdr = &qtbl->q_hdr[Q_MSG];
  636. msg_q_hdr->qhdr_status = QHDR_ACTIVE;
  637. msg_q_hdr->qhdr_start_addr = hfi_mem->msg_q.iova;
  638. msg_q_hdr->qhdr_q_size = ICP_MSG_Q_SIZE_IN_BYTES >> BYTE_WORD_SHIFT;
  639. msg_q_hdr->qhdr_pkt_size = ICP_HFI_VAR_SIZE_PKT;
  640. msg_q_hdr->qhdr_pkt_drop_cnt = RESET;
  641. msg_q_hdr->qhdr_read_idx = RESET;
  642. msg_q_hdr->qhdr_write_idx = RESET;
  643. /* setup firmware-to-Host message queue */
  644. dbg_q_hdr = &qtbl->q_hdr[Q_DBG];
  645. dbg_q_hdr->qhdr_status = QHDR_ACTIVE;
  646. dbg_q_hdr->qhdr_start_addr = hfi_mem->dbg_q.iova;
  647. dbg_q_hdr->qhdr_q_size = ICP_DBG_Q_SIZE_IN_BYTES >> BYTE_WORD_SHIFT;
  648. dbg_q_hdr->qhdr_pkt_size = ICP_HFI_VAR_SIZE_PKT;
  649. dbg_q_hdr->qhdr_pkt_drop_cnt = RESET;
  650. dbg_q_hdr->qhdr_read_idx = RESET;
  651. dbg_q_hdr->qhdr_write_idx = RESET;
  652. sfr_buffer = (struct sfr_buf *)hfi_mem->sfr_buf.kva;
  653. sfr_buffer->size = ICP_MSG_SFR_SIZE_IN_BYTES;
  654. switch (event_driven_mode) {
  655. case INTR_MODE:
  656. cmd_q_hdr->qhdr_type = Q_CMD;
  657. cmd_q_hdr->qhdr_rx_wm = SET;
  658. cmd_q_hdr->qhdr_tx_wm = SET;
  659. cmd_q_hdr->qhdr_rx_req = SET;
  660. cmd_q_hdr->qhdr_tx_req = RESET;
  661. cmd_q_hdr->qhdr_rx_irq_status = RESET;
  662. cmd_q_hdr->qhdr_tx_irq_status = RESET;
  663. msg_q_hdr->qhdr_type = Q_MSG;
  664. msg_q_hdr->qhdr_rx_wm = SET;
  665. msg_q_hdr->qhdr_tx_wm = SET;
  666. msg_q_hdr->qhdr_rx_req = SET;
  667. msg_q_hdr->qhdr_tx_req = RESET;
  668. msg_q_hdr->qhdr_rx_irq_status = RESET;
  669. msg_q_hdr->qhdr_tx_irq_status = RESET;
  670. dbg_q_hdr->qhdr_type = Q_DBG;
  671. dbg_q_hdr->qhdr_rx_wm = SET;
  672. dbg_q_hdr->qhdr_tx_wm = SET_WM;
  673. dbg_q_hdr->qhdr_rx_req = RESET;
  674. dbg_q_hdr->qhdr_tx_req = RESET;
  675. dbg_q_hdr->qhdr_rx_irq_status = RESET;
  676. dbg_q_hdr->qhdr_tx_irq_status = RESET;
  677. break;
  678. case POLL_MODE:
  679. cmd_q_hdr->qhdr_type = Q_CMD | TX_EVENT_POLL_MODE_2 |
  680. RX_EVENT_POLL_MODE_2;
  681. msg_q_hdr->qhdr_type = Q_MSG | TX_EVENT_POLL_MODE_2 |
  682. RX_EVENT_POLL_MODE_2;
  683. dbg_q_hdr->qhdr_type = Q_DBG | TX_EVENT_POLL_MODE_2 |
  684. RX_EVENT_POLL_MODE_2;
  685. break;
  686. case WM_MODE:
  687. cmd_q_hdr->qhdr_type = Q_CMD | TX_EVENT_DRIVEN_MODE_2 |
  688. RX_EVENT_DRIVEN_MODE_2;
  689. cmd_q_hdr->qhdr_rx_wm = SET;
  690. cmd_q_hdr->qhdr_tx_wm = SET;
  691. cmd_q_hdr->qhdr_rx_req = RESET;
  692. cmd_q_hdr->qhdr_tx_req = SET;
  693. cmd_q_hdr->qhdr_rx_irq_status = RESET;
  694. cmd_q_hdr->qhdr_tx_irq_status = RESET;
  695. msg_q_hdr->qhdr_type = Q_MSG | TX_EVENT_DRIVEN_MODE_2 |
  696. RX_EVENT_DRIVEN_MODE_2;
  697. msg_q_hdr->qhdr_rx_wm = SET;
  698. msg_q_hdr->qhdr_tx_wm = SET;
  699. msg_q_hdr->qhdr_rx_req = SET;
  700. msg_q_hdr->qhdr_tx_req = RESET;
  701. msg_q_hdr->qhdr_rx_irq_status = RESET;
  702. msg_q_hdr->qhdr_tx_irq_status = RESET;
  703. dbg_q_hdr->qhdr_type = Q_DBG | TX_EVENT_DRIVEN_MODE_2 |
  704. RX_EVENT_DRIVEN_MODE_2;
  705. dbg_q_hdr->qhdr_rx_wm = SET;
  706. dbg_q_hdr->qhdr_tx_wm = SET_WM;
  707. dbg_q_hdr->qhdr_rx_req = RESET;
  708. dbg_q_hdr->qhdr_tx_req = RESET;
  709. dbg_q_hdr->qhdr_rx_irq_status = RESET;
  710. dbg_q_hdr->qhdr_tx_irq_status = RESET;
  711. break;
  712. default:
  713. CAM_ERR(CAM_HFI, "Invalid event driven mode :%u",
  714. event_driven_mode);
  715. break;
  716. }
  717. cam_io_w_mb((uint32_t)hfi_mem->qtbl.iova,
  718. icp_base + HFI_REG_QTBL_PTR);
  719. cam_io_w_mb((uint32_t)hfi_mem->sfr_buf.iova,
  720. icp_base + HFI_REG_SFR_PTR);
  721. cam_io_w_mb((uint32_t)hfi_mem->shmem.iova,
  722. icp_base + HFI_REG_SHARED_MEM_PTR);
  723. cam_io_w_mb((uint32_t)hfi_mem->shmem.len,
  724. icp_base + HFI_REG_SHARED_MEM_SIZE);
  725. cam_io_w_mb((uint32_t)hfi_mem->sec_heap.iova,
  726. icp_base + HFI_REG_UNCACHED_HEAP_PTR);
  727. cam_io_w_mb((uint32_t)hfi_mem->sec_heap.len,
  728. icp_base + HFI_REG_UNCACHED_HEAP_SIZE);
  729. cam_io_w_mb((uint32_t)ICP_INIT_REQUEST_SET,
  730. icp_base + HFI_REG_HOST_ICP_INIT_REQUEST);
  731. cam_io_w_mb((uint32_t)hfi_mem->qdss.iova,
  732. icp_base + HFI_REG_QDSS_IOVA);
  733. cam_io_w_mb((uint32_t)hfi_mem->qdss.len,
  734. icp_base + HFI_REG_QDSS_IOVA_SIZE);
  735. cam_io_w_mb((uint32_t)hfi_mem->io_mem.iova,
  736. icp_base + HFI_REG_IO_REGION_IOVA);
  737. cam_io_w_mb((uint32_t)hfi_mem->io_mem.len,
  738. icp_base + HFI_REG_IO_REGION_SIZE);
  739. cam_io_w_mb((uint32_t)hfi_mem->io_mem2.iova,
  740. icp_base + HFI_REG_IO2_REGION_IOVA);
  741. cam_io_w_mb((uint32_t)hfi_mem->io_mem2.len,
  742. icp_base + HFI_REG_IO2_REGION_SIZE);
  743. CAM_INFO(CAM_HFI, "Init IO1 : [0x%x 0x%x] IO2 [0x%x 0x%x]",
  744. hfi_mem->io_mem.iova, hfi_mem->io_mem.len,
  745. hfi_mem->io_mem2.iova, hfi_mem->io_mem2.len);
  746. hw_version = cam_io_r(icp_base + HFI_REG_A5_HW_VERSION);
  747. while (retry_cnt < HFI_MAX_POLL_TRY) {
  748. readw_poll_timeout((icp_base + HFI_REG_ICP_HOST_INIT_RESPONSE),
  749. status, (status == ICP_INIT_RESP_SUCCESS), 100, 10000);
  750. CAM_DBG(CAM_HFI, "1: status = %u rc = %d", status, rc);
  751. status = cam_io_r_mb(icp_base + HFI_REG_ICP_HOST_INIT_RESPONSE);
  752. CAM_DBG(CAM_HFI, "2: status = %u rc = %d", status, rc);
  753. if (status == ICP_INIT_RESP_SUCCESS)
  754. break;
  755. if (status == ICP_INIT_RESP_FAILED) {
  756. CAM_ERR(CAM_HFI, "ICP Init Failed. status = %u",
  757. status);
  758. fw_version = cam_io_r(icp_base + HFI_REG_FW_VERSION);
  759. CAM_ERR(CAM_HFI, "fw version : [%x]", fw_version);
  760. goto regions_fail;
  761. }
  762. retry_cnt++;
  763. }
  764. if ((retry_cnt == HFI_MAX_POLL_TRY) &&
  765. (status == ICP_INIT_RESP_RESET)) {
  766. CAM_ERR(CAM_HFI, "Reached Max retries. status = %u",
  767. status);
  768. fw_version = cam_io_r(icp_base + HFI_REG_FW_VERSION);
  769. CAM_ERR(CAM_HFI,
  770. "hw version : : [%x], fw version : [%x]",
  771. hw_version, fw_version);
  772. goto regions_fail;
  773. }
  774. fw_version = cam_io_r(icp_base + HFI_REG_FW_VERSION);
  775. CAM_DBG(CAM_HFI, "hw version : : [%x], fw version : [%x]",
  776. hw_version, fw_version);
  777. g_hfi->csr_base = icp_base;
  778. g_hfi->hfi_state = HFI_READY;
  779. g_hfi->cmd_q_state = true;
  780. g_hfi->msg_q_state = true;
  781. cam_io_w_mb((uint32_t)(INTR_ENABLE|INTR_ENABLE_WD0),
  782. icp_base + HFI_REG_A5_CSR_A2HOSTINTEN);
  783. mutex_unlock(&hfi_cmd_q_mutex);
  784. mutex_unlock(&hfi_msg_q_mutex);
  785. return rc;
  786. regions_fail:
  787. kfree(g_hfi);
  788. g_hfi = NULL;
  789. alloc_fail:
  790. mutex_unlock(&hfi_cmd_q_mutex);
  791. mutex_unlock(&hfi_msg_q_mutex);
  792. return rc;
  793. }
  794. void cam_hfi_deinit(void __iomem *icp_base)
  795. {
  796. mutex_lock(&hfi_cmd_q_mutex);
  797. mutex_lock(&hfi_msg_q_mutex);
  798. if (!g_hfi) {
  799. CAM_ERR(CAM_HFI, "hfi path not established yet");
  800. goto err;
  801. }
  802. g_hfi->cmd_q_state = false;
  803. g_hfi->msg_q_state = false;
  804. kzfree(g_hfi);
  805. g_hfi = NULL;
  806. err:
  807. mutex_unlock(&hfi_cmd_q_mutex);
  808. mutex_unlock(&hfi_msg_q_mutex);
  809. }