cc_request_mgr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2018 ARM Limited or its affiliates. */
  3. #include <linux/kernel.h>
  4. #include "cc_driver.h"
  5. #include "cc_buffer_mgr.h"
  6. #include "cc_request_mgr.h"
  7. #include "cc_ivgen.h"
  8. #include "cc_pm.h"
  9. #define CC_MAX_POLL_ITER 10
  10. /* The highest descriptor count in used */
  11. #define CC_MAX_DESC_SEQ_LEN 23
  12. struct cc_req_mgr_handle {
  13. /* Request manager resources */
  14. unsigned int hw_queue_size; /* HW capability */
  15. unsigned int min_free_hw_slots;
  16. unsigned int max_used_sw_slots;
  17. struct cc_crypto_req req_queue[MAX_REQUEST_QUEUE_SIZE];
  18. u32 req_queue_head;
  19. u32 req_queue_tail;
  20. u32 axi_completed;
  21. u32 q_free_slots;
  22. /* This lock protects access to HW register
  23. * that must be single request at a time
  24. */
  25. spinlock_t hw_lock;
  26. struct cc_hw_desc compl_desc;
  27. u8 *dummy_comp_buff;
  28. dma_addr_t dummy_comp_buff_dma;
  29. /* backlog queue */
  30. struct list_head backlog;
  31. unsigned int bl_len;
  32. spinlock_t bl_lock; /* protect backlog queue */
  33. #ifdef COMP_IN_WQ
  34. struct workqueue_struct *workq;
  35. struct delayed_work compwork;
  36. #else
  37. struct tasklet_struct comptask;
  38. #endif
  39. };
  40. struct cc_bl_item {
  41. struct cc_crypto_req creq;
  42. struct cc_hw_desc desc[CC_MAX_DESC_SEQ_LEN];
  43. unsigned int len;
  44. struct list_head list;
  45. bool notif;
  46. };
  47. static void comp_handler(unsigned long devarg);
  48. #ifdef COMP_IN_WQ
  49. static void comp_work_handler(struct work_struct *work);
  50. #endif
  51. void cc_req_mgr_fini(struct cc_drvdata *drvdata)
  52. {
  53. struct cc_req_mgr_handle *req_mgr_h = drvdata->request_mgr_handle;
  54. struct device *dev = drvdata_to_dev(drvdata);
  55. if (!req_mgr_h)
  56. return; /* Not allocated */
  57. if (req_mgr_h->dummy_comp_buff_dma) {
  58. dma_free_coherent(dev, sizeof(u32), req_mgr_h->dummy_comp_buff,
  59. req_mgr_h->dummy_comp_buff_dma);
  60. }
  61. dev_dbg(dev, "max_used_hw_slots=%d\n", (req_mgr_h->hw_queue_size -
  62. req_mgr_h->min_free_hw_slots));
  63. dev_dbg(dev, "max_used_sw_slots=%d\n", req_mgr_h->max_used_sw_slots);
  64. #ifdef COMP_IN_WQ
  65. flush_workqueue(req_mgr_h->workq);
  66. destroy_workqueue(req_mgr_h->workq);
  67. #else
  68. /* Kill tasklet */
  69. tasklet_kill(&req_mgr_h->comptask);
  70. #endif
  71. kzfree(req_mgr_h);
  72. drvdata->request_mgr_handle = NULL;
  73. }
  74. int cc_req_mgr_init(struct cc_drvdata *drvdata)
  75. {
  76. struct cc_req_mgr_handle *req_mgr_h;
  77. struct device *dev = drvdata_to_dev(drvdata);
  78. int rc = 0;
  79. req_mgr_h = kzalloc(sizeof(*req_mgr_h), GFP_KERNEL);
  80. if (!req_mgr_h) {
  81. rc = -ENOMEM;
  82. goto req_mgr_init_err;
  83. }
  84. drvdata->request_mgr_handle = req_mgr_h;
  85. spin_lock_init(&req_mgr_h->hw_lock);
  86. spin_lock_init(&req_mgr_h->bl_lock);
  87. INIT_LIST_HEAD(&req_mgr_h->backlog);
  88. #ifdef COMP_IN_WQ
  89. dev_dbg(dev, "Initializing completion workqueue\n");
  90. req_mgr_h->workq = create_singlethread_workqueue("ccree");
  91. if (!req_mgr_h->workq) {
  92. dev_err(dev, "Failed creating work queue\n");
  93. rc = -ENOMEM;
  94. goto req_mgr_init_err;
  95. }
  96. INIT_DELAYED_WORK(&req_mgr_h->compwork, comp_work_handler);
  97. #else
  98. dev_dbg(dev, "Initializing completion tasklet\n");
  99. tasklet_init(&req_mgr_h->comptask, comp_handler,
  100. (unsigned long)drvdata);
  101. #endif
  102. req_mgr_h->hw_queue_size = cc_ioread(drvdata,
  103. CC_REG(DSCRPTR_QUEUE_SRAM_SIZE));
  104. dev_dbg(dev, "hw_queue_size=0x%08X\n", req_mgr_h->hw_queue_size);
  105. if (req_mgr_h->hw_queue_size < MIN_HW_QUEUE_SIZE) {
  106. dev_err(dev, "Invalid HW queue size = %u (Min. required is %u)\n",
  107. req_mgr_h->hw_queue_size, MIN_HW_QUEUE_SIZE);
  108. rc = -ENOMEM;
  109. goto req_mgr_init_err;
  110. }
  111. req_mgr_h->min_free_hw_slots = req_mgr_h->hw_queue_size;
  112. req_mgr_h->max_used_sw_slots = 0;
  113. /* Allocate DMA word for "dummy" completion descriptor use */
  114. req_mgr_h->dummy_comp_buff =
  115. dma_alloc_coherent(dev, sizeof(u32),
  116. &req_mgr_h->dummy_comp_buff_dma,
  117. GFP_KERNEL);
  118. if (!req_mgr_h->dummy_comp_buff) {
  119. dev_err(dev, "Not enough memory to allocate DMA (%zu) dropped buffer\n",
  120. sizeof(u32));
  121. rc = -ENOMEM;
  122. goto req_mgr_init_err;
  123. }
  124. /* Init. "dummy" completion descriptor */
  125. hw_desc_init(&req_mgr_h->compl_desc);
  126. set_din_const(&req_mgr_h->compl_desc, 0, sizeof(u32));
  127. set_dout_dlli(&req_mgr_h->compl_desc, req_mgr_h->dummy_comp_buff_dma,
  128. sizeof(u32), NS_BIT, 1);
  129. set_flow_mode(&req_mgr_h->compl_desc, BYPASS);
  130. set_queue_last_ind(drvdata, &req_mgr_h->compl_desc);
  131. return 0;
  132. req_mgr_init_err:
  133. cc_req_mgr_fini(drvdata);
  134. return rc;
  135. }
  136. static void enqueue_seq(struct cc_drvdata *drvdata, struct cc_hw_desc seq[],
  137. unsigned int seq_len)
  138. {
  139. int i, w;
  140. void __iomem *reg = drvdata->cc_base + CC_REG(DSCRPTR_QUEUE_WORD0);
  141. struct device *dev = drvdata_to_dev(drvdata);
  142. /*
  143. * We do indeed write all 6 command words to the same
  144. * register. The HW supports this.
  145. */
  146. for (i = 0; i < seq_len; i++) {
  147. for (w = 0; w <= 5; w++)
  148. writel_relaxed(seq[i].word[w], reg);
  149. if (cc_dump_desc)
  150. dev_dbg(dev, "desc[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
  151. i, seq[i].word[0], seq[i].word[1],
  152. seq[i].word[2], seq[i].word[3],
  153. seq[i].word[4], seq[i].word[5]);
  154. }
  155. }
  156. /*!
  157. * Completion will take place if and only if user requested completion
  158. * by cc_send_sync_request().
  159. *
  160. * \param dev
  161. * \param dx_compl_h The completion event to signal
  162. */
  163. static void request_mgr_complete(struct device *dev, void *dx_compl_h,
  164. int dummy)
  165. {
  166. struct completion *this_compl = dx_compl_h;
  167. complete(this_compl);
  168. }
  169. static int cc_queues_status(struct cc_drvdata *drvdata,
  170. struct cc_req_mgr_handle *req_mgr_h,
  171. unsigned int total_seq_len)
  172. {
  173. unsigned long poll_queue;
  174. struct device *dev = drvdata_to_dev(drvdata);
  175. /* SW queue is checked only once as it will not
  176. * be chaned during the poll because the spinlock_bh
  177. * is held by the thread
  178. */
  179. if (((req_mgr_h->req_queue_head + 1) & (MAX_REQUEST_QUEUE_SIZE - 1)) ==
  180. req_mgr_h->req_queue_tail) {
  181. dev_err(dev, "SW FIFO is full. req_queue_head=%d sw_fifo_len=%d\n",
  182. req_mgr_h->req_queue_head, MAX_REQUEST_QUEUE_SIZE);
  183. return -ENOSPC;
  184. }
  185. if (req_mgr_h->q_free_slots >= total_seq_len)
  186. return 0;
  187. /* Wait for space in HW queue. Poll constant num of iterations. */
  188. for (poll_queue = 0; poll_queue < CC_MAX_POLL_ITER ; poll_queue++) {
  189. req_mgr_h->q_free_slots =
  190. cc_ioread(drvdata, CC_REG(DSCRPTR_QUEUE_CONTENT));
  191. if (req_mgr_h->q_free_slots < req_mgr_h->min_free_hw_slots)
  192. req_mgr_h->min_free_hw_slots = req_mgr_h->q_free_slots;
  193. if (req_mgr_h->q_free_slots >= total_seq_len) {
  194. /* If there is enough place return */
  195. return 0;
  196. }
  197. dev_dbg(dev, "HW FIFO is full. q_free_slots=%d total_seq_len=%d\n",
  198. req_mgr_h->q_free_slots, total_seq_len);
  199. }
  200. /* No room in the HW queue try again later */
  201. dev_dbg(dev, "HW FIFO full, timeout. req_queue_head=%d sw_fifo_len=%d q_free_slots=%d total_seq_len=%d\n",
  202. req_mgr_h->req_queue_head, MAX_REQUEST_QUEUE_SIZE,
  203. req_mgr_h->q_free_slots, total_seq_len);
  204. return -ENOSPC;
  205. }
  206. /*!
  207. * Enqueue caller request to crypto hardware.
  208. * Need to be called with HW lock held and PM running
  209. *
  210. * \param drvdata
  211. * \param cc_req The request to enqueue
  212. * \param desc The crypto sequence
  213. * \param len The crypto sequence length
  214. * \param add_comp If "true": add an artificial dout DMA to mark completion
  215. *
  216. * \return int Returns -EINPROGRESS or error code
  217. */
  218. static int cc_do_send_request(struct cc_drvdata *drvdata,
  219. struct cc_crypto_req *cc_req,
  220. struct cc_hw_desc *desc, unsigned int len,
  221. bool add_comp, bool ivgen)
  222. {
  223. struct cc_req_mgr_handle *req_mgr_h = drvdata->request_mgr_handle;
  224. unsigned int used_sw_slots;
  225. unsigned int iv_seq_len = 0;
  226. unsigned int total_seq_len = len; /*initial sequence length*/
  227. struct cc_hw_desc iv_seq[CC_IVPOOL_SEQ_LEN];
  228. struct device *dev = drvdata_to_dev(drvdata);
  229. int rc;
  230. if (ivgen) {
  231. dev_dbg(dev, "Acquire IV from pool into %d DMA addresses %pad, %pad, %pad, IV-size=%u\n",
  232. cc_req->ivgen_dma_addr_len,
  233. &cc_req->ivgen_dma_addr[0],
  234. &cc_req->ivgen_dma_addr[1],
  235. &cc_req->ivgen_dma_addr[2],
  236. cc_req->ivgen_size);
  237. /* Acquire IV from pool */
  238. rc = cc_get_iv(drvdata, cc_req->ivgen_dma_addr,
  239. cc_req->ivgen_dma_addr_len,
  240. cc_req->ivgen_size, iv_seq, &iv_seq_len);
  241. if (rc) {
  242. dev_err(dev, "Failed to generate IV (rc=%d)\n", rc);
  243. return rc;
  244. }
  245. total_seq_len += iv_seq_len;
  246. }
  247. used_sw_slots = ((req_mgr_h->req_queue_head -
  248. req_mgr_h->req_queue_tail) &
  249. (MAX_REQUEST_QUEUE_SIZE - 1));
  250. if (used_sw_slots > req_mgr_h->max_used_sw_slots)
  251. req_mgr_h->max_used_sw_slots = used_sw_slots;
  252. /* Enqueue request - must be locked with HW lock*/
  253. req_mgr_h->req_queue[req_mgr_h->req_queue_head] = *cc_req;
  254. req_mgr_h->req_queue_head = (req_mgr_h->req_queue_head + 1) &
  255. (MAX_REQUEST_QUEUE_SIZE - 1);
  256. /* TODO: Use circ_buf.h ? */
  257. dev_dbg(dev, "Enqueue request head=%u\n", req_mgr_h->req_queue_head);
  258. /*
  259. * We are about to push command to the HW via the command registers
  260. * that may refernece hsot memory. We need to issue a memory barrier
  261. * to make sure there are no outstnading memory writes
  262. */
  263. wmb();
  264. /* STAT_PHASE_4: Push sequence */
  265. if (ivgen)
  266. enqueue_seq(drvdata, iv_seq, iv_seq_len);
  267. enqueue_seq(drvdata, desc, len);
  268. if (add_comp) {
  269. enqueue_seq(drvdata, &req_mgr_h->compl_desc, 1);
  270. total_seq_len++;
  271. }
  272. if (req_mgr_h->q_free_slots < total_seq_len) {
  273. /* This situation should never occur. Maybe indicating problem
  274. * with resuming power. Set the free slot count to 0 and hope
  275. * for the best.
  276. */
  277. dev_err(dev, "HW free slot count mismatch.");
  278. req_mgr_h->q_free_slots = 0;
  279. } else {
  280. /* Update the free slots in HW queue */
  281. req_mgr_h->q_free_slots -= total_seq_len;
  282. }
  283. /* Operation still in process */
  284. return -EINPROGRESS;
  285. }
  286. static void cc_enqueue_backlog(struct cc_drvdata *drvdata,
  287. struct cc_bl_item *bli)
  288. {
  289. struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;
  290. spin_lock_bh(&mgr->bl_lock);
  291. list_add_tail(&bli->list, &mgr->backlog);
  292. ++mgr->bl_len;
  293. spin_unlock_bh(&mgr->bl_lock);
  294. tasklet_schedule(&mgr->comptask);
  295. }
  296. static void cc_proc_backlog(struct cc_drvdata *drvdata)
  297. {
  298. struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;
  299. struct cc_bl_item *bli;
  300. struct cc_crypto_req *creq;
  301. struct crypto_async_request *req;
  302. bool ivgen;
  303. unsigned int total_len;
  304. struct device *dev = drvdata_to_dev(drvdata);
  305. int rc;
  306. spin_lock(&mgr->bl_lock);
  307. while (mgr->bl_len) {
  308. bli = list_first_entry(&mgr->backlog, struct cc_bl_item, list);
  309. spin_unlock(&mgr->bl_lock);
  310. creq = &bli->creq;
  311. req = (struct crypto_async_request *)creq->user_arg;
  312. /*
  313. * Notify the request we're moving out of the backlog
  314. * but only if we haven't done so already.
  315. */
  316. if (!bli->notif) {
  317. req->complete(req, -EINPROGRESS);
  318. bli->notif = true;
  319. }
  320. ivgen = !!creq->ivgen_dma_addr_len;
  321. total_len = bli->len + (ivgen ? CC_IVPOOL_SEQ_LEN : 0);
  322. spin_lock(&mgr->hw_lock);
  323. rc = cc_queues_status(drvdata, mgr, total_len);
  324. if (rc) {
  325. /*
  326. * There is still not room in the FIFO for
  327. * this request. Bail out. We'll return here
  328. * on the next completion irq.
  329. */
  330. spin_unlock(&mgr->hw_lock);
  331. return;
  332. }
  333. rc = cc_do_send_request(drvdata, &bli->creq, bli->desc,
  334. bli->len, false, ivgen);
  335. spin_unlock(&mgr->hw_lock);
  336. if (rc != -EINPROGRESS) {
  337. cc_pm_put_suspend(dev);
  338. creq->user_cb(dev, req, rc);
  339. }
  340. /* Remove ourselves from the backlog list */
  341. spin_lock(&mgr->bl_lock);
  342. list_del(&bli->list);
  343. --mgr->bl_len;
  344. kfree(bli);
  345. }
  346. spin_unlock(&mgr->bl_lock);
  347. }
  348. int cc_send_request(struct cc_drvdata *drvdata, struct cc_crypto_req *cc_req,
  349. struct cc_hw_desc *desc, unsigned int len,
  350. struct crypto_async_request *req)
  351. {
  352. int rc;
  353. struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;
  354. bool ivgen = !!cc_req->ivgen_dma_addr_len;
  355. unsigned int total_len = len + (ivgen ? CC_IVPOOL_SEQ_LEN : 0);
  356. struct device *dev = drvdata_to_dev(drvdata);
  357. bool backlog_ok = req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG;
  358. gfp_t flags = cc_gfp_flags(req);
  359. struct cc_bl_item *bli;
  360. rc = cc_pm_get(dev);
  361. if (rc) {
  362. dev_err(dev, "ssi_power_mgr_runtime_get returned %x\n", rc);
  363. return rc;
  364. }
  365. spin_lock_bh(&mgr->hw_lock);
  366. rc = cc_queues_status(drvdata, mgr, total_len);
  367. #ifdef CC_DEBUG_FORCE_BACKLOG
  368. if (backlog_ok)
  369. rc = -ENOSPC;
  370. #endif /* CC_DEBUG_FORCE_BACKLOG */
  371. if (rc == -ENOSPC && backlog_ok) {
  372. spin_unlock_bh(&mgr->hw_lock);
  373. bli = kmalloc(sizeof(*bli), flags);
  374. if (!bli) {
  375. cc_pm_put_suspend(dev);
  376. return -ENOMEM;
  377. }
  378. memcpy(&bli->creq, cc_req, sizeof(*cc_req));
  379. memcpy(&bli->desc, desc, len * sizeof(*desc));
  380. bli->len = len;
  381. bli->notif = false;
  382. cc_enqueue_backlog(drvdata, bli);
  383. return -EBUSY;
  384. }
  385. if (!rc)
  386. rc = cc_do_send_request(drvdata, cc_req, desc, len, false,
  387. ivgen);
  388. spin_unlock_bh(&mgr->hw_lock);
  389. return rc;
  390. }
  391. int cc_send_sync_request(struct cc_drvdata *drvdata,
  392. struct cc_crypto_req *cc_req, struct cc_hw_desc *desc,
  393. unsigned int len)
  394. {
  395. int rc;
  396. struct device *dev = drvdata_to_dev(drvdata);
  397. struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;
  398. init_completion(&cc_req->seq_compl);
  399. cc_req->user_cb = request_mgr_complete;
  400. cc_req->user_arg = &cc_req->seq_compl;
  401. rc = cc_pm_get(dev);
  402. if (rc) {
  403. dev_err(dev, "ssi_power_mgr_runtime_get returned %x\n", rc);
  404. return rc;
  405. }
  406. while (true) {
  407. spin_lock_bh(&mgr->hw_lock);
  408. rc = cc_queues_status(drvdata, mgr, len + 1);
  409. if (!rc)
  410. break;
  411. spin_unlock_bh(&mgr->hw_lock);
  412. if (rc != -EAGAIN) {
  413. cc_pm_put_suspend(dev);
  414. return rc;
  415. }
  416. wait_for_completion_interruptible(&drvdata->hw_queue_avail);
  417. reinit_completion(&drvdata->hw_queue_avail);
  418. }
  419. rc = cc_do_send_request(drvdata, cc_req, desc, len, true, false);
  420. spin_unlock_bh(&mgr->hw_lock);
  421. if (rc != -EINPROGRESS) {
  422. cc_pm_put_suspend(dev);
  423. return rc;
  424. }
  425. wait_for_completion(&cc_req->seq_compl);
  426. return 0;
  427. }
  428. /*!
  429. * Enqueue caller request to crypto hardware during init process.
  430. * assume this function is not called in middle of a flow,
  431. * since we set QUEUE_LAST_IND flag in the last descriptor.
  432. *
  433. * \param drvdata
  434. * \param desc The crypto sequence
  435. * \param len The crypto sequence length
  436. *
  437. * \return int Returns "0" upon success
  438. */
  439. int send_request_init(struct cc_drvdata *drvdata, struct cc_hw_desc *desc,
  440. unsigned int len)
  441. {
  442. struct cc_req_mgr_handle *req_mgr_h = drvdata->request_mgr_handle;
  443. unsigned int total_seq_len = len; /*initial sequence length*/
  444. int rc = 0;
  445. /* Wait for space in HW and SW FIFO. Poll for as much as FIFO_TIMEOUT.
  446. */
  447. rc = cc_queues_status(drvdata, req_mgr_h, total_seq_len);
  448. if (rc)
  449. return rc;
  450. set_queue_last_ind(drvdata, &desc[(len - 1)]);
  451. /*
  452. * We are about to push command to the HW via the command registers
  453. * that may refernece hsot memory. We need to issue a memory barrier
  454. * to make sure there are no outstnading memory writes
  455. */
  456. wmb();
  457. enqueue_seq(drvdata, desc, len);
  458. /* Update the free slots in HW queue */
  459. req_mgr_h->q_free_slots =
  460. cc_ioread(drvdata, CC_REG(DSCRPTR_QUEUE_CONTENT));
  461. return 0;
  462. }
  463. void complete_request(struct cc_drvdata *drvdata)
  464. {
  465. struct cc_req_mgr_handle *request_mgr_handle =
  466. drvdata->request_mgr_handle;
  467. complete(&drvdata->hw_queue_avail);
  468. #ifdef COMP_IN_WQ
  469. queue_delayed_work(request_mgr_handle->workq,
  470. &request_mgr_handle->compwork, 0);
  471. #else
  472. tasklet_schedule(&request_mgr_handle->comptask);
  473. #endif
  474. }
  475. #ifdef COMP_IN_WQ
  476. static void comp_work_handler(struct work_struct *work)
  477. {
  478. struct cc_drvdata *drvdata =
  479. container_of(work, struct cc_drvdata, compwork.work);
  480. comp_handler((unsigned long)drvdata);
  481. }
  482. #endif
  483. static void proc_completions(struct cc_drvdata *drvdata)
  484. {
  485. struct cc_crypto_req *cc_req;
  486. struct device *dev = drvdata_to_dev(drvdata);
  487. struct cc_req_mgr_handle *request_mgr_handle =
  488. drvdata->request_mgr_handle;
  489. unsigned int *tail = &request_mgr_handle->req_queue_tail;
  490. unsigned int *head = &request_mgr_handle->req_queue_head;
  491. while (request_mgr_handle->axi_completed) {
  492. request_mgr_handle->axi_completed--;
  493. /* Dequeue request */
  494. if (*head == *tail) {
  495. /* We are supposed to handle a completion but our
  496. * queue is empty. This is not normal. Return and
  497. * hope for the best.
  498. */
  499. dev_err(dev, "Request queue is empty head == tail %u\n",
  500. *head);
  501. break;
  502. }
  503. cc_req = &request_mgr_handle->req_queue[*tail];
  504. if (cc_req->user_cb)
  505. cc_req->user_cb(dev, cc_req->user_arg, 0);
  506. *tail = (*tail + 1) & (MAX_REQUEST_QUEUE_SIZE - 1);
  507. dev_dbg(dev, "Dequeue request tail=%u\n", *tail);
  508. dev_dbg(dev, "Request completed. axi_completed=%d\n",
  509. request_mgr_handle->axi_completed);
  510. cc_pm_put_suspend(dev);
  511. }
  512. }
  513. static inline u32 cc_axi_comp_count(struct cc_drvdata *drvdata)
  514. {
  515. return FIELD_GET(AXIM_MON_COMP_VALUE,
  516. cc_ioread(drvdata, drvdata->axim_mon_offset));
  517. }
  518. /* Deferred service handler, run as interrupt-fired tasklet */
  519. static void comp_handler(unsigned long devarg)
  520. {
  521. struct cc_drvdata *drvdata = (struct cc_drvdata *)devarg;
  522. struct cc_req_mgr_handle *request_mgr_handle =
  523. drvdata->request_mgr_handle;
  524. u32 irq;
  525. irq = (drvdata->irq & CC_COMP_IRQ_MASK);
  526. if (irq & CC_COMP_IRQ_MASK) {
  527. /* To avoid the interrupt from firing as we unmask it,
  528. * we clear it now
  529. */
  530. cc_iowrite(drvdata, CC_REG(HOST_ICR), CC_COMP_IRQ_MASK);
  531. /* Avoid race with above clear: Test completion counter
  532. * once more
  533. */
  534. request_mgr_handle->axi_completed +=
  535. cc_axi_comp_count(drvdata);
  536. while (request_mgr_handle->axi_completed) {
  537. do {
  538. proc_completions(drvdata);
  539. /* At this point (after proc_completions()),
  540. * request_mgr_handle->axi_completed is 0.
  541. */
  542. request_mgr_handle->axi_completed =
  543. cc_axi_comp_count(drvdata);
  544. } while (request_mgr_handle->axi_completed > 0);
  545. cc_iowrite(drvdata, CC_REG(HOST_ICR),
  546. CC_COMP_IRQ_MASK);
  547. request_mgr_handle->axi_completed +=
  548. cc_axi_comp_count(drvdata);
  549. }
  550. }
  551. /* after verifing that there is nothing to do,
  552. * unmask AXI completion interrupt
  553. */
  554. cc_iowrite(drvdata, CC_REG(HOST_IMR),
  555. cc_ioread(drvdata, CC_REG(HOST_IMR)) & ~irq);
  556. cc_proc_backlog(drvdata);
  557. }